|
| 1 | +local ls = require("luasnip") |
| 2 | +---@type luasnip-snippets.nodes |
| 3 | +local Nodes = require("luasnip-snippets.nodes") |
| 4 | +local snippet = Nodes.construct_snippet |
| 5 | +local i = Nodes.insert_node |
| 6 | +local c = Nodes.choice_node |
| 7 | +local fmta = require("luasnip.extras.fmt").fmta |
| 8 | +local f = ls.function_node |
| 9 | +local t = ls.text_node |
| 10 | +local rep = require("luasnip.extras").rep |
| 11 | +local CommonCond = require("luasnip-snippets.utils.common_cond") |
| 12 | +---@type luasnip-snippets.utils.cond |
| 13 | +local Cond = require("luasnip-snippets.utils.cond") |
| 14 | +---@type luasnip-snippets.config |
| 15 | +local Config = require("luasnip-snippets.config") |
| 16 | +---@type luasnip-snippets.utils |
| 17 | +local Utils = require("luasnip-snippets.utils") |
| 18 | + |
| 19 | +---@param lines string[] |
| 20 | +---@return string[] |
| 21 | +local function fix_leading_whitespace(lines, indent) |
| 22 | + indent = vim.F.if_nil(indent, 2) |
| 23 | + local leading_whitespace = string.rep(" ", indent) |
| 24 | + local ret = {} |
| 25 | + local first = true |
| 26 | + for _, line in ipairs(lines) do |
| 27 | + if not first then |
| 28 | + table.insert(ret, leading_whitespace .. line) |
| 29 | + else |
| 30 | + first = false |
| 31 | + table.insert(ret, line) |
| 32 | + end |
| 33 | + end |
| 34 | + return ret |
| 35 | +end |
| 36 | + |
| 37 | +local function has_select_raw_fn(_, _, _) |
| 38 | + return Utils.get_buf_var(0, "LUASNIP_SELECT_RAW") ~= nil |
| 39 | +end |
| 40 | +local has_select_raw = Cond.make_condition(has_select_raw_fn, has_select_raw_fn) |
| 41 | + |
| 42 | +return { |
| 43 | + snippet { |
| 44 | + "#if", |
| 45 | + name = "(#if) #if ... #endif", |
| 46 | + dscr = "Expands to #if ... #endif", |
| 47 | + mode = "bw", |
| 48 | + lang = "cpp", |
| 49 | + cond = has_select_raw, |
| 50 | + nodes = fmta( |
| 51 | + [[ |
| 52 | + #if <condition> |
| 53 | + <selected><cursor> |
| 54 | + #endif // <condition_r> |
| 55 | + ]], |
| 56 | + { |
| 57 | + condition = i(1, "condition"), |
| 58 | + cursor = i(0), |
| 59 | + selected = f(function(_, snip) |
| 60 | + local _, env = {}, snip.env |
| 61 | + return env.LS_SELECT_RAW |
| 62 | + end), |
| 63 | + condition_r = rep(1), |
| 64 | + } |
| 65 | + ), |
| 66 | + }, |
| 67 | + |
| 68 | + snippet { |
| 69 | + "if", |
| 70 | + name = "(if) if (...) { ... }", |
| 71 | + dscr = "Expands to if expression", |
| 72 | + mode = "bw", |
| 73 | + lang = "cpp", |
| 74 | + cond = has_select_raw, |
| 75 | + nodes = fmta( |
| 76 | + [[ |
| 77 | + if (<condition>) { |
| 78 | + <selected><cursor> |
| 79 | + } |
| 80 | + ]], |
| 81 | + { |
| 82 | + condition = i(1, "condition"), |
| 83 | + cursor = i(0), |
| 84 | + selected = f(function(_, snip) |
| 85 | + local _, env = {}, snip.env |
| 86 | + return fix_leading_whitespace(env.LS_SELECT_RAW) |
| 87 | + end), |
| 88 | + } |
| 89 | + ), |
| 90 | + }, |
| 91 | + |
| 92 | + snippet { |
| 93 | + "do", |
| 94 | + name = "(do) do { ... } while (0)", |
| 95 | + dscr = "Expands to do ... while(0) expression", |
| 96 | + mode = "bw", |
| 97 | + lang = "cpp", |
| 98 | + cond = has_select_raw, |
| 99 | + nodes = fmta( |
| 100 | + [[ |
| 101 | + do { |
| 102 | + <selected><cursor> |
| 103 | + } while (0); |
| 104 | + ]], |
| 105 | + { |
| 106 | + cursor = i(0), |
| 107 | + selected = f(function(_, snip) |
| 108 | + local _, env = {}, snip.env |
| 109 | + return fix_leading_whitespace(env.LS_SELECT_RAW) |
| 110 | + end), |
| 111 | + } |
| 112 | + ), |
| 113 | + }, |
| 114 | + |
| 115 | + snippet { |
| 116 | + "while", |
| 117 | + name = "(while) while (...) { ... }", |
| 118 | + dscr = "Expands to while (...) { ... } expression", |
| 119 | + mode = "bw", |
| 120 | + lang = "cpp", |
| 121 | + cond = has_select_raw, |
| 122 | + nodes = fmta( |
| 123 | + [[ |
| 124 | + while (<condition>) { |
| 125 | + <selected><cursor> |
| 126 | + } |
| 127 | + ]], |
| 128 | + { |
| 129 | + condition = i(1, "condition"), |
| 130 | + cursor = i(0), |
| 131 | + selected = f(function(_, snip) |
| 132 | + local _, env = {}, snip.env |
| 133 | + return fix_leading_whitespace(env.LS_SELECT_RAW) |
| 134 | + end), |
| 135 | + } |
| 136 | + ), |
| 137 | + }, |
| 138 | +} |
0 commit comments