Skip to content

Commit

Permalink
feat: use luasnip snippet text edit handler if available
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jun 7, 2024
1 parent d69653a commit 7bbb5b5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
11 changes: 11 additions & 0 deletions lua/rustaceanvim/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ local function get_test_executor()
return get_crate_test_executor()
end

---@return fun(edits: rust.lsp.SnippetTextEdit[], bufnr: integer, offset_encoding: string, apply_text_edits: function) | nil
local function get_snippet_text_edit_handler()
local ok, luasnip = pcall(require, 'luasnip.extras.lsp')
if ok and luasnip.apply_text_edits then
return luasnip.apply_text_edits
end
end

---@class RustaceanConfig
local RustaceanDefaultConfig = {
---@class RustaceanToolsConfig
Expand All @@ -100,6 +108,9 @@ local RustaceanDefaultConfig = {
---@type RustaceanExecutor
crate_test_executor = get_crate_test_executor(),

---@type fun(edits: rust.lsp.SnippetTextEdit[], bufnr: integer, offset_encoding: string, apply_text_edits: function) | nil
snippet_text_edit_handler = get_snippet_text_edit_handler(),

---@type string | nil
cargo_override = nil,

Expand Down
20 changes: 15 additions & 5 deletions lua/rustaceanvim/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ local os = require('rustaceanvim.os')
local function override_apply_text_edits()
local old_func = vim.lsp.util.apply_text_edits
---@diagnostic disable-next-line
vim.lsp.util.apply_text_edits = function(edits, bufnr, offset_encoding)
local overrides = require('rustaceanvim.overrides')
overrides.snippet_text_edits_to_text_edits(edits)
old_func(edits, bufnr, offset_encoding)
end
vim.lsp.util.apply_text_edits = config.tools.snippet_text_edit_handler
---@param edits rust.lsp.SnippetTextEdit[]
---@param bufnr number
---@param offset_encoding string
and function(edits, bufnr, offset_encoding)
config.tools.snippet_text_edit_handler(edits, bufnr, offset_encoding, old_func)
end
---@param edits rust.lsp.SnippetTextEdit[]
---@param bufnr number
---@param offset_encoding string
or function(edits, bufnr, offset_encoding)
local overrides = require('rustaceanvim.overrides')
overrides.snippet_text_edits_to_text_edits(edits)
old_func(edits, bufnr, offset_encoding)
end
end

---@param client lsp.Client
Expand Down
4 changes: 4 additions & 0 deletions lua/rustaceanvim/meta.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---@class rust.lsp.SnippetTextEdit
---@field insertTextFormat number
---@field newText string
---@field range table see |vim.lsp.util.make_range_params()|
2 changes: 1 addition & 1 deletion lua/rustaceanvim/overrides.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ local function parse_snippet(input)
return ok and tostring(parsed) or parse_snippet_fallback(input)
end

---@param spe? table
---@param spe? rust.lsp.SnippetTextEdit[]
function M.snippet_text_edits_to_text_edits(spe)
if type(spe) ~= 'table' then
return
Expand Down

0 comments on commit 7bbb5b5

Please sign in to comment.