|
| 1 | +local UtilsTS = require("luasnip-snippets.utils.treesitter") |
| 2 | +local ls = require("luasnip") |
| 3 | +local d = ls.dynamic_node |
| 4 | +local sn = ls.snippet_node |
| 5 | +local t = ls.text_node |
| 6 | +local fmta = require("luasnip.extras.fmt").fmta |
| 7 | +local i = require("luasnip-snippets.nodes").insert_node |
| 8 | +local c = require("luasnip-snippets.nodes").choice_node |
| 9 | + |
| 10 | +local function inject_expanding_environment(_, _, match, captures) |
| 11 | + local row, col = unpack(vim.api.nvim_win_get_cursor(0)) |
| 12 | + local buf = vim.api.nvim_get_current_buf() |
| 13 | + |
| 14 | + return UtilsTS.invoke_after_reparse_buffer(buf, match, function(parser, _) |
| 15 | + local pos = { |
| 16 | + row - 1, |
| 17 | + col - #match, |
| 18 | + } |
| 19 | + local node = parser:named_node_for_range { |
| 20 | + pos[1], |
| 21 | + pos[2], |
| 22 | + pos[1], |
| 23 | + pos[2], |
| 24 | + } |
| 25 | + |
| 26 | + local ret = { |
| 27 | + trigger = match, |
| 28 | + capture = captures, |
| 29 | + env_override = { |
| 30 | + IMPL_ITEM_START = UtilsTS.start_pos( |
| 31 | + UtilsTS.find_first_parent(node, { "impl_item" }) |
| 32 | + ), |
| 33 | + FUNCTION_ITEM_START = UtilsTS.start_pos( |
| 34 | + UtilsTS.find_first_parent(node, { "function_item" }) |
| 35 | + ), |
| 36 | + CLOSURE_EXPRESSION_START = UtilsTS.start_pos( |
| 37 | + UtilsTS.find_first_parent(node, { "closure_expression" }) |
| 38 | + ), |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + vim.api.nvim_win_set_cursor(0, { row, col }) |
| 43 | + return ret |
| 44 | + end) |
| 45 | +end |
| 46 | + |
| 47 | +---@class LSSnippets.Rust.Fn.Env |
| 48 | +---@field IMPL_ITEM_START? { [1]: number, [2]: number } |
| 49 | +---@field FUNCTION_ITEM_START? { [1]: number, [2]: number } |
| 50 | +---@field CLOSURE_EXPRESSION_START? { [1]: number, [2]: number } |
| 51 | + |
| 52 | +return { |
| 53 | + ls.s( |
| 54 | + { |
| 55 | + trig = "fn", |
| 56 | + wordTrig = true, |
| 57 | + name = "(fn) Function-Definition/Lambda", |
| 58 | + resolveExpandParams = inject_expanding_environment, |
| 59 | + }, |
| 60 | + d(1, function(_, parent) |
| 61 | + local env = parent.env |
| 62 | + local last_type, last_type_row, last_type_col |
| 63 | + local keys = { |
| 64 | + "CPP_ARGUMENT_START", |
| 65 | + "CPP_FUNCTION_BODY_START", |
| 66 | + "CPP_CLASS_BODY_START", |
| 67 | + } |
| 68 | + for _, key in ipairs(keys) do |
| 69 | + if env[key] ~= nil then |
| 70 | + if last_type == nil then |
| 71 | + last_type = key |
| 72 | + last_type_row = env[key][1] |
| 73 | + last_type_col = env[key][2] |
| 74 | + else |
| 75 | + if |
| 76 | + last_type_row < env[key][1] |
| 77 | + or (last_type_row == env[key][1] and last_type_col < env[key][2]) |
| 78 | + then |
| 79 | + last_type = key |
| 80 | + last_type_row = env[key][1] |
| 81 | + last_type_col = env[key][2] |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + if |
| 88 | + env.FUNCTION_ITEM_START ~= nil or env.CLOSURE_EXPRESSION_START ~= nil |
| 89 | + then |
| 90 | + -- closure expression |
| 91 | + return sn( |
| 92 | + nil, |
| 93 | + fmta( |
| 94 | + [[ |
| 95 | + <modifier>|<args>| { |
| 96 | + <body> |
| 97 | + } |
| 98 | + ]], |
| 99 | + { |
| 100 | + modifier = c(1, { |
| 101 | + t(""), |
| 102 | + t("async "), |
| 103 | + t("move "), |
| 104 | + }, { desc = "function modifier" }), |
| 105 | + args = i(2, "args"), |
| 106 | + body = i(0), |
| 107 | + } |
| 108 | + ) |
| 109 | + ) |
| 110 | + else |
| 111 | + -- function item |
| 112 | + return sn( |
| 113 | + nil, |
| 114 | + fmta( |
| 115 | + [[ |
| 116 | + <modifier><visible>fn <name>(<args>) { |
| 117 | + <body> |
| 118 | + } |
| 119 | + ]], |
| 120 | + { |
| 121 | + modifier = c(1, { |
| 122 | + t(""), |
| 123 | + t("async "), |
| 124 | + }, { desc = "function modifier" }), |
| 125 | + visible = c(2, { |
| 126 | + t(""), |
| 127 | + t("pub "), |
| 128 | + t("pub(crate) "), |
| 129 | + t("pub(super) "), |
| 130 | + }, { desc = "visibility" }), |
| 131 | + name = i(3, "new_fn", { desc = "function name" }), |
| 132 | + args = i(4, "args", { desc = "function arguments" }), |
| 133 | + body = i(0), |
| 134 | + } |
| 135 | + ) |
| 136 | + ) |
| 137 | + end |
| 138 | + end, {}) |
| 139 | + ), |
| 140 | +} |
0 commit comments