|
| 1 | +local ls = require("luasnip") |
| 2 | +local f = ls.function_node |
| 3 | +local tsp = require("luasnip.extras.treesitter_postfix") |
| 4 | +local Utils = require("luasnip-snippets.utils") |
| 5 | +local fmt = require("luasnip.extras.fmt").fmt |
| 6 | +local i = require("luasnip-snippets.nodes").insert_node |
| 7 | +local c = require("luasnip-snippets.nodes").choice_node |
| 8 | +local t = ls.text_node |
| 9 | + |
| 10 | +local expr_query = [[ |
| 11 | +[ |
| 12 | + (call_expression) |
| 13 | + (identifier) |
| 14 | + (template_function) |
| 15 | + (subscript_expression) |
| 16 | + (field_expression) |
| 17 | + (user_defined_literal) |
| 18 | +] @prefix |
| 19 | +]] |
| 20 | + |
| 21 | +---@param trig string |
| 22 | +---@param expand string |
| 23 | +---@param dscr string? |
| 24 | +local function expr_tsp(trig, expand, dscr) |
| 25 | + local name = ("(%s) %s"):format(trig, expand) |
| 26 | + if dscr == nil then |
| 27 | + dscr = ("Wraps an expression with %s"):format(expand) |
| 28 | + else |
| 29 | + dscr = dscr:format(expand) |
| 30 | + end |
| 31 | + local replaced = expand:gsub("?", "%%s") |
| 32 | + |
| 33 | + return tsp.treesitter_postfix({ |
| 34 | + trig = trig, |
| 35 | + name = name, |
| 36 | + dscr = dscr, |
| 37 | + wordTrig = false, |
| 38 | + reparseBuffer = "live", |
| 39 | + matchTSNode = { |
| 40 | + query = expr_query, |
| 41 | + query_lang = "cpp", |
| 42 | + }, |
| 43 | + }, { |
| 44 | + f(function(_, parent) |
| 45 | + return Utils.replace_all(parent.snippet.env.LS_TSMATCH, replaced) |
| 46 | + end, {}), |
| 47 | + }) |
| 48 | +end |
| 49 | + |
| 50 | +return { |
| 51 | + expr_tsp( |
| 52 | + ".be", |
| 53 | + "?.begin(), ?.end()", |
| 54 | + "Completes an expr with both begin() and end()" |
| 55 | + ), |
| 56 | + expr_tsp(".mv", "std::move(?)"), |
| 57 | + expr_tsp(".fwd", "std::forward<decltype(?)>(?)"), |
| 58 | + expr_tsp(".val", "std::declval<?>()"), |
| 59 | + expr_tsp(".dt", "decltype(?)"), |
| 60 | + tsp.treesitter_postfix( |
| 61 | + { |
| 62 | + trig = ".sc", |
| 63 | + name = "static_cast<TYPE>(?)", |
| 64 | + dscr = "Wraps an expression with static_cast<TYPE>(?)", |
| 65 | + wordTrig = false, |
| 66 | + reparseBuffer = "live", |
| 67 | + matchTSNode = { |
| 68 | + query = expr_query, |
| 69 | + query_lang = "cpp", |
| 70 | + }, |
| 71 | + }, |
| 72 | + fmt( |
| 73 | + [[ |
| 74 | + static_cast<{body}>({expr}){end} |
| 75 | + ]], |
| 76 | + { |
| 77 | + body = i(1), |
| 78 | + expr = f(function(_, parent) |
| 79 | + return Utils.replace_all(parent.snippet.env.LS_TSMATCH, "%s") |
| 80 | + end, {}), |
| 81 | + ["end"] = i(0), |
| 82 | + } |
| 83 | + ) |
| 84 | + ), |
| 85 | +} |
0 commit comments