Skip to content

Commit b780271

Browse files
committed
feat: use luasnip snippet text edit handler if available
1 parent 00a9508 commit b780271

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [Unreleased]
1010

11+
### Added
12+
13+
- LSP: Use LuaSnip's [`SnippetTextEdit`](https://github.com/rust-lang/rust-analyzer/blob/master/docs/dev/lsp-extensions.md#snippet-textedit)
14+
handler if available.
15+
This enables snippet text edits in code actions.
16+
1117
### Fixed
1218

1319
- Error when decoding invalid JSON or blank string from cargo metadata.

lua/rustaceanvim/config/internal.lua

+11
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ local function get_test_executor()
8484
return get_crate_test_executor()
8585
end
8686

87+
---@return fun(edits: rust.lsp.SnippetTextEdit[], bufnr: integer, offset_encoding: string, apply_text_edits: function) | nil
88+
local function get_snippet_text_edit_handler()
89+
local ok, luasnip = pcall(require, 'luasnip.extras.lsp')
90+
if ok and luasnip.apply_text_edits then
91+
return luasnip.apply_text_edits
92+
end
93+
end
94+
8795
---@class RustaceanConfig
8896
local RustaceanDefaultConfig = {
8997
---@class RustaceanToolsConfig
@@ -100,6 +108,9 @@ local RustaceanDefaultConfig = {
100108
---@type RustaceanExecutor
101109
crate_test_executor = get_crate_test_executor(),
102110

111+
---@type fun(edits: rust.lsp.SnippetTextEdit[], bufnr: integer, offset_encoding: string, apply_text_edits: function) | nil
112+
snippet_text_edit_handler = get_snippet_text_edit_handler(),
113+
103114
---@type string | nil
104115
cargo_override = nil,
105116

lua/rustaceanvim/lsp.lua

+15-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,21 @@ local os = require('rustaceanvim.os')
1111
local function override_apply_text_edits()
1212
local old_func = vim.lsp.util.apply_text_edits
1313
---@diagnostic disable-next-line
14-
vim.lsp.util.apply_text_edits = function(edits, bufnr, offset_encoding)
15-
local overrides = require('rustaceanvim.overrides')
16-
overrides.snippet_text_edits_to_text_edits(edits)
17-
old_func(edits, bufnr, offset_encoding)
18-
end
14+
vim.lsp.util.apply_text_edits = config.tools.snippet_text_edit_handler
15+
---@param edits rust.lsp.SnippetTextEdit[]
16+
---@param bufnr number
17+
---@param offset_encoding string
18+
and function(edits, bufnr, offset_encoding)
19+
config.tools.snippet_text_edit_handler(edits, bufnr, offset_encoding, old_func)
20+
end
21+
---@param edits rust.lsp.SnippetTextEdit[]
22+
---@param bufnr number
23+
---@param offset_encoding string
24+
or function(edits, bufnr, offset_encoding)
25+
local overrides = require('rustaceanvim.overrides')
26+
overrides.snippet_text_edits_to_text_edits(edits)
27+
old_func(edits, bufnr, offset_encoding)
28+
end
1929
end
2030

2131
---@param client lsp.Client

lua/rustaceanvim/meta.lua

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---@class rust.lsp.SnippetTextEdit
2+
---@field insertTextFormat number
3+
---@field newText string
4+
---@field range table see |vim.lsp.util.make_range_params()|

lua/rustaceanvim/overrides.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ local function parse_snippet(input)
2323
return ok and tostring(parsed) or parse_snippet_fallback(input)
2424
end
2525

26-
---@param spe? table
26+
---@param spe? rust.lsp.SnippetTextEdit[]
2727
function M.snippet_text_edits_to_text_edits(spe)
2828
if type(spe) ~= 'table' then
2929
return

0 commit comments

Comments
 (0)