From d95dc4442c001db5ccf53a482c6684e0ad6761d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20L=C3=BCdecke?= Date: Fri, 20 Sep 2024 11:58:51 +0200 Subject: [PATCH] Added luasnip snippets, enabled completion, go, org, rust, all snippets - url for vim snippets is: https://github.com/honza/vim-snippets --- lua/plugins.lua | 12 +- snippets/all.snippets | 11 ++ snippets/go.snippets | 280 +++++++++++++++++++++++++++++++++++++++++ snippets/org.snippets | 135 ++++++++++++++++++++ snippets/rust.snippets | 252 +++++++++++++++++++++++++++++++++++++ 5 files changed, 686 insertions(+), 4 deletions(-) create mode 100644 snippets/all.snippets create mode 100644 snippets/go.snippets create mode 100644 snippets/org.snippets create mode 100644 snippets/rust.snippets diff --git a/lua/plugins.lua b/lua/plugins.lua index 40483e0..d0b7248 100644 --- a/lua/plugins.lua +++ b/lua/plugins.lua @@ -23,10 +23,11 @@ require("lazy").setup({ "hrsh7th/nvim-cmp", dependencies = { "lspkind.nvim", - "hrsh7th/cmp-nvim-lsp", -- lsp auto-completion - "hrsh7th/cmp-buffer", -- buffer auto-completion - "hrsh7th/cmp-path", -- path auto-completion - "hrsh7th/cmp-cmdline", -- cmdline auto-completion + "hrsh7th/cmp-nvim-lsp", -- lsp auto-completion + "hrsh7th/cmp-buffer", -- buffer auto-completion + "hrsh7th/cmp-path", -- path auto-completion + "hrsh7th/cmp-cmdline", -- cmdline auto-completion + "saadparwaiz1/cmp_luasnip", -- luasnip auto-completion }, config = function() require("config.nvim-cmp") @@ -37,6 +38,9 @@ require("lazy").setup({ { "L3MON4D3/LuaSnip", version = "v2.*", + config = function() + require("luasnip.loaders.from_snipmate").lazy_load() + end, }, -- LSP manager diff --git a/snippets/all.snippets b/snippets/all.snippets new file mode 100644 index 0000000..1121825 --- /dev/null +++ b/snippets/all.snippets @@ -0,0 +1,11 @@ +# ccflow local snippets for all modes + +snippet todo "Add a TODO marker" + // TODO.`strftime("%Y-%m-%d")` + +snippet note "Add a NOTE marker" + // NOTE.`strftime("%Y-%m-%d")` + + +snippet lorem + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. diff --git a/snippets/go.snippets b/snippets/go.snippets new file mode 100644 index 0000000..f5831b9 --- /dev/null +++ b/snippets/go.snippets @@ -0,0 +1,280 @@ +snippet v "shorthand variable declaration" + ${1} := ${2} + +snippet vr "variable initialization" + var ${1:t} ${0:string} + +snippet var "variable declaration" + var ${1} ${2} = ${3} + +snippet vars "variables declaration" + var ( + ${1} ${2} = ${3} + ) + +snippet ap "append" + append(${1:slice}, ${0:value}) + +snippet bl "bool" + bool + +snippet bt "byte" + byte + +snippet br "break" + break + +snippet ch "channel" + chan ${0:int} + +snippet cs "case" + case ${1:value}: + ${0:${VISUAL}} + +snippet co "constants with iota" + const ( + ${1:NAME1} = iota + ${0:NAME2} + ) + +snippet cn "continue" + continue + +snippet df "defer" + defer ${0:func}() + +snippet dfr "defer recover" + defer func() { + if err := recover(); err != nil { + ${0:${VISUAL}} + } + }() + +snippet im "import" + import ( + "${1:package}" + ) + +snippet in "interface" + interface{} + +snippet inf "full interface " + type ${1:name} interface { + ${2:/* methods */} + } + +snippet if "if condition" + if $1 { + ${2:${VISUAL}} + } + + +snippet ife "if else condition" + if $1 { + ${2:${VISUAL}} + } else { + ${0} + } + +snippet el "else" + else { + ${0:${VISUAL}} + } + +snippet ir "if error not nil, return err" + if err != nil { + return err + } + ${0} + +snippet f "false" + false + +snippet ft "fallthrough" + fallthrough + +snippet fl "float" + float32 + +snippet f3 "float32" + float32 + +snippet f6 "float64" + float64 + +snippet for "for loop" + for ${1}{ + ${0:${VISUAL}} + } + +snippet fori "for int loop" + for ${2:i} := 0; $2 < ${1:count}; $2${3:++} { + ${0:${VISUAL}} + } + +snippet forr "for range loop" + for ${1:e} := range ${2:collection} { + ${0:${VISUAL}} + } + +snippet fun "function" + func ${1:funcName}(${2}) ${3:error} { + ${4} + } + ${0} + +snippet fum "method" + func (${1:receiver} ${2:type}) ${3:funcName}(${4}) ${5:error} { + ${6} + } + ${0} + +snippet fumh "http handler function on receiver" + func (${1:receiver} ${2:type}) ${3:funcName}(${4:w} http.ResponseWriter, ${5:r} *http.Request) { + ${0:${VISUAL}} + } + +snippet lf "log printf" + log.Printf("%${1:s}", ${2:var}) + +snippet lp "log println" + log.Println("${1}") + +snippet mk "make" + make(${1:[]string}, ${0:0}) + +snippet mp "map" + map[${1:string}]${0:int} + +snippet main "func main()" + func main() { + ${1} + } + ${0} + +snippet nw "new" + new(${0:type}) + +snippet pa "package" + package ${1:main} + +snippet pn "panic" + panic("${0:msg}") + +snippet pf "fmt.Printf()" + fmt.Printf("%${1:s}\n", ${2:var}) + +snippet pl "fmt.Println()" + fmt.Println("${1:s}") + +snippet rn "range" + range ${0} + +snippet rt "return" + return ${0} + +snippet rs "result" + result + +snippet sl "select" + select { + case ${1:v1} := <-${2:chan1} + ${3} + default: + ${0} + } + +snippet sr "string" + string + +snippet st "struct" + type ${1:name} struct { + ${2:/* data */} + } + ${0} + +snippet sw "switch" + switch ${1:var} { + case ${2:value1}: + ${3} + case ${4:value2}: + ${5} + default: + ${0} + } + +snippet ps "fmt.Sprintf" + fmt.Sprintf("%${1:s}", ${2:var}) + +snippet t "true" + true + +snippet g "goroutine named function" + go ${1:funcName}(${0}) + +snippet ga "goroutine anonymous function" + go func(${1} ${2:type}) { + ${3:/* code */} + }(${0}) + +snippet test "test function" + func Test${1:name}(t *testing.T) { + ${0:${VISUAL}} + } + +snippet testt "table test function" + func Test${1:name}(t *testing.T) { + tests := []struct { + name string + }{ + { + name: "${2:test name}", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + ${0:${VISUAL}} + }) + } + } + +snippet bench "benchmark function" + func Benchmark${1:name}(b *testing.B) { + for i := 0; i < b.N; i++ { + ${2} + } + } + ${0} + +snippet cl "composite literals" + type ${1:name} struct { + ${2:attrName} ${3:attrType} + } + +snippet om "if key in a map" + if ${1:value}, ok := ${2:map}[${3:key}]; ok == true { + ${4:/* code */} + } + + +snippet gg "Grouped globals with anonymous struct" + var ${1:var} = struct{ + ${2:name} ${3:type} + }{ + $2: ${4:value}, + } + + +snippet ja "Marshalable json alias" + type ${1:parentType}Alias $1 + + func (p *$1) MarshalJSON() ([]byte, error) { + return json.Marshal(&struct{ *$1Alias }{(*$1Alias)(p)}) + } + + +snippet errwr "Error handling with fmt.Errorf" + if ${1}err != nil { + return fmt.Errorf("${2} %w", err) + } diff --git a/snippets/org.snippets b/snippets/org.snippets new file mode 100644 index 0000000..6c9cd61 --- /dev/null +++ b/snippets/org.snippets @@ -0,0 +1,135 @@ +# Org Mode Snippets Imported from (https://github.com/doomemacs/snippets/) +# Imported by ybenel (github.com/m1ndo) + +# Begin +snippet begin + #+begin_${1:type} ${2:options} + $0 + #+end_$1 +# Begin Center +snippet ${1:Self} { + $1 { ${3} } + } +snippet main "Main function" + pub fn main() { + ${0} + } +snippet let "let variable declaration with type inference" + let ${1} = ${2}; +snippet lett "let variable declaration with explicit type annotation" + let ${1}: ${2} = ${3}; +snippet letm "let mut variable declaration with type inference" + let mut ${1} = ${2}; +snippet lettm "let mut variable declaration with explicit type annotation" + let mut ${1}: ${2} = ${3}; +snippet pri "print!" + print!("${1}"); +snippet pri, "print! with format param" + print!("${1}{${2}}", ${3}); +snippet pln "println!" + println!("${1}"); +snippet pln, "println! with format param" + println!("${1}{${2}}", ${3}); +snippet fmt "format!" + format!("${1}{${2}}", ${3}); +snippet d "dbg! debugging macro" + dbg!(${0:${VISUAL}}) +snippet d; "dbg! debugging macro statement" + dbg!(&${1}); + ${0} +# Modules +snippet ec "extern crate" + extern crate ${1:sync}; +snippet ecl "extern crate log" + #[macro_use] + extern crate log; +snippet mod + mod ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} { + ${0} + } /* $1 */ +# Testing +snippet as "assert!" + assert!(${1:predicate}); +snippet ase "assert_eq!" + assert_eq!(${1:expected}, ${2:actual}); +snippet test "Unit test function" + #[test] + fn ${1:function_name}_test() { + ${0} + } +snippet testmod "Test module" b + #[cfg(test)] + mod tests { + use super::${1:*}; + + test${0} + } +snippet ig "#[ignore]" + #[ignore] +# Attributes +snippet allow "allow lint attribute" b + #[allow(${1:unused_variables})] +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 = "${1:crate_name}"] + // Additional metadata attributes + #![desc = "${2:Description.}"] + #![license = "${3:BSD}"] + #![comment = "${4:Comment.}"] + // Specify the output type + #![crate_type = "${5:lib}"] +# Common types +snippet opt "Option" + Option<${1:i32}> +snippet res "Result" + Result<${1:&str}, ${2:()}> +# Control structures +snippet if + if ${1} { + ${0:${VISUAL}} + } +snippet ife "if / else" + if ${1} { + ${2:${VISUAL}} + } else { + ${0} + } +snippet ifl "if let (...)" + if let ${1:Some($2)} = $3 { + ${0:${VISUAL}} + } +snippet el "else" + else { + ${0:${VISUAL}} + } +snippet eli "else if" + else if ${1} { + ${0:${VISUAL}} + } +snippet mat "match pattern" + match ${1} { + ${2} => ${3} + } +snippet case "Case clause of pattern match" + ${1:_} => ${2:expression} +snippet = "=> " + => $0 +snippet loop "loop {}" b + loop { + ${0:${VISUAL}} + } +snippet wh "while loop" + while $1 { + ${0:${VISUAL}} + } +snippet whl "while let (...)" + while let ${1:Some($2)} = $3 { + ${0:${VISUAL}} + } +snippet for "for ... in ... loop" + for ${1:i} in ${2} { + ${0} + } +# TODO commenting +snippet todo "TODO comment" + // TODO: $0 +snippet fixme "FIXME comment" + // FIXME: $0 +# Struct +snippet st "Struct definition" + struct ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} { + ${0} + } +snippet impl "Struct/Trait implementation" + impl ${1:Type/Trait}${2: for $3} { + ${0} + } +snippet stn "Struct with new constructor" + pub struct ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} { + ${0} + } + + impl$2 $1$2 { + pub fn new(${4}) -> Self { + $1 { ${5} } + } + } +snippet ty "Type alias" + type ${1:NewName} = $2; +snippet enum "enum definition" + enum ${1:Name} { + ${2}, + } +snippet penum "pub enum definition" + pub enum ${1:Name} { + ${2}, + } +# Traits +snippet trait "Trait definition" + trait ${1:Name} { + ${0} + } +snippet drop "Drop trait implementation (destructor)" + impl Drop for $1 { + fn drop(&mut self) { + ${0} + } + } +# Statics +snippet ss "static string declaration" + static ${1}: &'static str = "${0}"; +snippet stat "static item declaration" + static ${1}: ${2:usize} = ${0}; +# Concurrency +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(); +# Implementations +snippet asref "AsRef trait implementation" + impl AsRef<${1:Ref}> for ${2:Type} { + fn as_ref(&self) -> &${3:$1} { + &self.${0:field} + } + } +snippet asmut "AsMut trait implementation" + impl AsMut<${1:Ref}> for ${2:Type} { + fn as_mut(&mut self) -> &mut ${3:$1} { + &mut self.${0:field} + } + } +snippet fd "Struct field definition" w + ${1:name}: ${2:Type}, +snippet || "Closure, anonymous function (inline)" i + ${1:move }|$2| { $3 } +snippet |} "Closure, anonymous function (block)" i + ${1:move }|$2| { + $3 + } +snippet macro "macro_rules!" b + macro_rules! ${1:name} { + (${2:matcher}) => ( + $3 + ) + } +snippet boxp "Box::new()" + Box::new(${0:${VISUAL}}) +snippet rc "Rc::new()" + Rc::new(${0:${VISUAL}}) +snippet unim "unimplemented!()" + unimplemented!() +snippet use "use ...;" b + use ${1:std::$2};