neovim configuration using rocks.nvim plugin manager

feat(snippets): cleanup, update

+4 -98
+3 -98
lua/snippets/all.lua
··· 7 7 local t = ls.text_node 8 8 local c = ls.choice_node 9 9 local r = ls.restore_node 10 - local fmt = require("luasnip.extras.fmt").fmt 11 10 12 - local function prevent_reexpand(_line_to_cursor, matched_trigger) 13 - local current_node = ls.session.current_nodes[vim.api.nvim_get_current_buf()] 14 - if not current_node then 15 - return true 16 - end 17 - local is_same_trigger = current_node.parent.snippet.trigger == matched_trigger 18 - local current_is_0 = current_node.pos == 0 19 - if not (is_same_trigger and current_is_0) then 20 - return true 21 - end 22 - local node_pos = current_node:get_buf_position() 23 - local cur_pos = vim.api.nvim_win_get_cursor(0) 24 - local same_pos = node_pos[1] + 1 == cur_pos[1] and node_pos[2] == cur_pos[2] 25 - return not same_pos 26 - end 27 - 28 - local function create_autopair(open, close) 29 - return s({ 30 - trig = open .. close, 31 - wordTrig = false, 32 - }, { 33 - t(open), 34 - c(1, { 35 - r(1, "content", i(1)), 36 - sn(nil, { t({ "", "\t" }), r(1, "content", i(1)), t({ "", "" }) }), 37 - }), 38 - t(close), 39 - }, { condition = prevent_reexpand }) 40 - end 41 - 42 - -- stylua: ignore 43 - ls.add_snippets(nil, { 44 - create_autopair("(", ")"), 45 - create_autopair("[", "]"), 46 - create_autopair("{", "}"), 47 - create_autopair("(", "),"), 48 - create_autopair("[", "],"), 49 - create_autopair("{", "},"), 50 - s({ trig = '""', wordTrig = false }, { t('"'), i(1), t('"') }, { condition = prevent_reexpand }), 51 - s({ trig = "''", wordTrig = false }, { t("'"), i(1), t("'") }, { condition = prevent_reexpand }), 52 - s({ trig = "``", wordTrig = false }, { t("`"), i(1), t("`") }, { condition = prevent_reexpand }), 53 - }) 54 - 55 - -- autopair code from Luasnip's wiki 56 - 57 - local function char_count_same(c1, c2) 58 - local line = vim.api.nvim_get_current_line() 59 - -- '%'-escape chars to force explicit match (gsub accepts patterns). 60 - -- second return value is number of substitutions. 61 - local _, ct1 = string.gsub(line, "%" .. c1, "") 62 - local _, ct2 = string.gsub(line, "%" .. c2, "") 63 - return ct1 == ct2 64 - end 65 - 66 - local function even_count(ch) 67 - local line = vim.api.nvim_get_current_line() 68 - local _, ct = string.gsub(line, ch, "") 69 - return ct % 2 == 0 70 - end 71 - 72 - local function neg(fn, ...) 73 - return not fn(...) 74 - end 75 - 76 - local function posi() 77 - return true 78 - end 79 - 80 - local function part(fn, ...) 81 - local args = { ... } 82 - return function() 83 - return fn(unpack(args)) 84 - end 85 - end 86 - 87 - -- This makes creation of pair-type snippets easier. 88 - local function pair(pair_begin, pair_end, expand_func, ...) 89 - -- triggerd by opening part of pair, wordTrig=false to trigger anywhere. 90 - -- ... is used to pass any args following the expand_func to it. 11 + local function pair(pair_begin, pair_end) 12 + -- autopair implementation inspired by Luasnip wiki 13 + -- https://github.com/L3MON4D3/LuaSnip/wiki/Cool-Snippets#all---pairs 91 14 return s({ trig = pair_begin, wordTrig = false }, { 92 15 t({ pair_begin }), 93 16 c(1, { ··· 95 18 sn(nil, { t({ "", "\t" }), r(1, "content", i(1)), t({ "", "" }) }), 96 19 }), 97 20 t({ pair_end }), 98 - }, { 99 - condition = expand_func and part(expand_func, part(..., pair_begin, pair_end)), 100 21 }) 101 22 end 102 - 103 - -- FIXME: pair() also doesn't work bc of this case: 104 - -- ``` 105 - -- vim.schedule(function (|) 106 - -- end) 107 - -- ``` 108 - -- here, I get unwanted autopair (|)) 109 - ls.add_snippets(nil, { 110 - pair("(", ")", neg, char_count_same), 111 - pair("{", "}", neg, char_count_same), 112 - pair("[", "]", neg, char_count_same), 113 - pair("<", ">", neg, char_count_same), 114 - pair("'", "'", neg, even_count), 115 - pair('"', '"', neg, even_count), 116 - pair("`", "`", neg, even_count), 117 - }) 118 23 119 24 ls.add_snippets("all", { 120 25 pair("(", ")"),
+1
lua/snippets/go.lua
··· 15 15 t"\t", c(1, { 16 16 sn(nil, { t"return", i(1), t" err" }), 17 17 t"log.Fatalln(err)", 18 + t"panic(err)", 18 19 }), 19 20 t{"", "}"}, i(0), 20 21 }),