Merge pull request #528 from ches/rust

Update Rust snippets
This commit is contained in:
Louis Pilfold 2015-03-12 08:36:11 +00:00
commit 8d956a2a24
2 changed files with 120 additions and 73 deletions

View File

@ -4,12 +4,34 @@
priority -50 priority -50
snippet fn "A function, optionally with arguments and return type." b snippet let "let variable declaration" b
let ${1:name}${2:: ${3:type}} = ${4};
endsnippet
snippet letm "let mut variable declaration" b
let mut ${1:name}${2:: ${3:type}} = ${4};
endsnippet
snippet fn "A function, optionally with arguments and return type."
fn ${1:function_name}(${2})${3/..*/ -> /}${3} { fn ${1:function_name}(${2})${3/..*/ -> /}${3} {
${VISUAL}${0} ${VISUAL}${0}
} }
endsnippet endsnippet
snippet arg "Function Arguments" i
${1:a}: ${2:T}${3:, arg}
endsnippet
snippet || "Closure, anonymous function (inline)" i
${1:move }|${2}| { $3 }
endsnippet
snippet |} "Closure, anonymous function (block)" i
${1:move }|${2}| {
$3
}
endsnippet
snippet pri "print!(..)" b snippet pri "print!(..)" b
print!("${1}"${2/..*/, /}${2}); print!("${1}"${2/..*/, /}${2});
endsnippet endsnippet
@ -30,7 +52,7 @@ macro_rules! ${1:name} (
) )
endsnippet endsnippet
snippet mod "A module" b snippet mod "A module" b
mod ${1:`!p snip.rv = snip.basename.lower() or "name"`} { mod ${1:`!p snip.rv = snip.basename.lower() or "name"`} {
${VISUAL}${0} ${VISUAL}${0}
} }
@ -52,18 +74,27 @@ struct ${1:`!p snip.rv = snip.basename.title() or "Name"`} {
} }
endsnippet endsnippet
# TODO: fancy dynamic field mirroring like Python slotclass
snippet stn "Struct with new constructor." b snippet stn "Struct with new constructor." b
pub struct ${1:`!p snip.rv = snip.basename.title() or "Name"`} { pub struct ${1:`!p snip.rv = snip.basename.title() or "Name"`} {
${3} fd${0}
} }
impl $1 { impl $1 {
pub fn new(${2}) -> $1 { pub fn new(${2}) -> $1 {
${4}return $1 { $1 { ${3} };
${5}
};
} }
} }
endsnippet endsnippet
snippet fd "Struct field definition" w
${1:name}: ${2:Type},
endsnippet
snippet impl "Struct/Trait implementation" b
impl ${1:Type/Trait}${2: for ${3:Type}} {
${0}
}
endsnippet
# vim:ft=snippets: # vim:ft=snippets:

View File

@ -3,11 +3,11 @@
################# #################
# Functions # Functions
snippet fn snippet fn "Function definition"
fn ${1:function_name}(${2})${3} { fn ${1:function_name}(${2})${3} {
${0} ${0}
} }
snippet test snippet test "Unit test function"
#[test] #[test]
fn ${1:test_function_name}() { fn ${1:test_function_name}() {
${0} ${0}
@ -19,32 +19,51 @@ snippet bench "Bench function" b
${0} ${0}
}) })
} }
snippet new snippet new "Constructor function"
pub fn new(${2}) -> ${1:Name} { pub fn new(${2}) -> ${1:Name} {
${0}return $1 { ${3} }; $1 { ${3} };
} }
snippet main snippet main "Main function"
pub fn main() { pub fn main() {
${0} ${0}
} }
snippet let snippet let "let variable declaration"
let ${1:name: type} = ${2}; let ${1:name}${2:: ${3:type}} = ${4};
snippet let snippet letm "let mut variable declaration"
let mut ${1:name: type} = ${2}; let mut ${1:name}${2:: ${3:type}} = ${4};
snippet pln snippet pln "println!"
println!("${1}"); println!("${1}");
snippet pln, snippet pln, "println! with format param"
println!("${1}", ${2}); println!("${1}", ${2});
snippet ec # Modules
snippet ec "extern crate"
extern crate ${1:sync}; extern crate ${1:sync};
snippet ecl snippet ecl "extern crate log"
#![feature(phase)] #[macro_use]
#[phase(plugin, link)] extern crate log; extern crate log;
snippet mod snippet mod
mod ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} { mod ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} {
${0} ${0}
} /* $1 */ } /* $1 */
snippet crate snippet testmod "Test module" b
#[cfg(test)]
mod tests {
use super::${1:*};
test${0}
}
# Attributes
snippet allow "allow lint attribute" b
#[allow(${1:unused_variable})]
snippet cfg "cfg attribute" b
#[cfg(${1:target_os = "linux"})]
snippet feat "feature attribute" b
#![feature(${1:plugin})]
snippet der "#[derive(..)]" b
#[derive(${1:Debug})]
snippet attr "#[..]" b
#[${1:inline}]
snippet crate "Define create meta attributes"
// Crate name // Crate name
#![crate_name = "${1:crate_name}"] #![crate_name = "${1:crate_name}"]
// Additional metadata attributes // Additional metadata attributes
@ -53,110 +72,107 @@ snippet crate
#![comment = "${4:Comment.}"] #![comment = "${4:Comment.}"]
// Specify the output type // Specify the output type
#![crate_type = "${5:lib}"] #![crate_type = "${5:lib}"]
snippet allow
#[allow(${1:unused_variable})]
snippet feat
#![feature(${1:macro_rules})]
snippet der "#[deriving(..)]" b
#[deriving(${1:Show})]
snippet attr "#[..]" b
#[${1:inline}]
# Common types # Common types
snippet opt snippet opt "Option<T>"
Option<${1:int}> Option<${1:i32}>
snippet res snippet res "Result<T, E>"
Result<${1:~str}, ${2:()}> Result<${1:~str}, ${2:()}>
# Control structures
snippet if snippet if
if ${1} { if ${1} {
${0} ${0}
} }
snippet ife snippet ife "if / else"
if ${1} { if ${1} {
${2} ${2}
} else { } else {
${0} ${0}
} }
snippet el snippet el "else"
else { else {
${0} ${0}
} }
snippet eli snippet eli "else if"
else if ${1} { else if ${1} {
${0} ${0}
} }
snippet mat snippet mat "match pattern"
match ${1} { match ${1} {
${2} => ${3}, ${2} => ${3}
} }
snippet case "Case clause of pattern match"
${1:_} => ${2:expression}
snippet loop "loop {}" b snippet loop "loop {}" b
loop { loop {
${0} ${0}
} }
snippet while snippet while "while loop"
while ${1:condition} { while ${1:condition} {
${0} ${0}
} }
snippet for snippet for "for ... in ... loop"
for ${1:i} in ${2:0u..10} { for ${1:i} in ${2:0u..10} {
${0} ${0}
} }
snippet spawn
spawn(proc() {
${0}
});
snippet chan
let (${1:tx}, ${2:rx}): (Sender<${3:int}>, Receiver<${4:int}>) = channel();
snippet duplex
let (${1:from_child}, ${2:to_child}) = sync::duplex();
# TODO commenting # TODO commenting
snippet todo snippet todo "TODO comment"
// [TODO]: ${0:Description} // [TODO]: ${0:Description}
snippet fixme "FIXME comment" snippet fixme "FIXME comment"
// FIXME: $0 // FIXME: $0
# Struct # Struct
snippet st snippet st "Struct definition"
struct ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} { struct ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} {
${0} ${0}
} }
snippet stn snippet impl "Struct/Trait implementation"
struct ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} { impl ${1:Type/Trait}${2: for ${3:Type}} {
${0}
}
snippet stn "Struct with new constructor"
pub struct ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} {
${0} ${0}
} }
impl $1 { impl $1 {
pub fn new(${2}) -> $1 { pub fn new(${2}) -> $1 {
${4}return $1 { $1 { ${3} };
${5}
};
} }
} }
snippet typ snippet type "Type alias"
type ${1:NewName} = $0; type ${1:NewName} = $2;
# Enum snippet enum "enum definition"
snippet enum
enum ${1:Name} { enum ${1:Name} {
${0}, ${2},
} }
# Impl # Traits
snippet imp snippet trait "Trait definition"
impl ${1:Name} { trait ${1:Name} {
${0} ${0}
} }
snippet drop snippet drop "Drop trait implementation (destructor)"
impl Drop for ${1:Name} { impl Drop for ${1:Name} {
fn drop(&mut self) { fn drop(&mut self) {
${0} ${0}
} }
} }
# Traits
snippet trait
trait ${1:Name} {
${0}
}
# Statics # Statics
snippet ss snippet ss "static string declaration"
static ${1}: &'static str = "${0}"; static ${1}: &'static str = "${0}";
snippet stat snippet stat "static item declaration"
static ${1}: ${2:usize} = ${0}; static ${1}: ${2:usize} = ${0};
# Concurrency
snippet scoped "spawn a scoped thread"
thread::scoped(${1:move }|| {
${0}
});
snippet spawn "spawn a thread"
thread::spawn(${1:move }|| {
${0}
});
snippet chan "Declare (Sender, Receiver) pair of asynchronous channel()"
let (${1:tx}, ${2:rx}): (Sender<${3:i32}>, Receiver<${4:i32}>) = channel();
# Testing
snippet as "assert!"
assert!(${1:predicate})
snippet ase "assert_eq!"
assert_eq!(${1:expected}, ${2:actual})