Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use luasnip snippet text edit handler if available #420

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- LSP: Use LuaSnip's [`SnippetTextEdit`](https://github.com/rust-lang/rust-analyzer/blob/master/docs/dev/lsp-extensions.md#snippet-textedit)
handler if available.
This enables snippet text edits in code actions.

### Fixed

- Error when decoding invalid JSON or blank string from cargo metadata.
Expand Down
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
Loading