Skip to content

Commit d107d75

Browse files
authored
fix(lsp): escape character inserted before } on code action (#338)
1 parent 2826321 commit d107d75

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ All notable changes to this project will be documented in this file.
66
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
77
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88

9+
## [Unreleased]
10+
11+
### Fixed
12+
13+
- Escape character inserted before `}` when applying code action
14+
with `SnippetTextEdit` [[#303](https://github.com/mrcjkb/rustaceanvim/issues/303)].
15+
916
## [4.18.2] - 2024-03-28
1017

1118
### Changed

lua/rustaceanvim/overrides.lua

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
local M = {}
22

3+
---@param input string unparsed snippet
4+
---@return string parsed snippet
5+
local function parse_snippet_fallback(input)
6+
local output = input
7+
-- $0 -> Nothing
8+
:gsub('%$%d', '')
9+
-- ${0:_} -> _
10+
:gsub('%${%d:(.-)}', '%1')
11+
:gsub([[\}]], '}')
12+
return output
13+
end
14+
15+
---@param input string unparsed snippet
16+
---@return string parsed snippet
17+
local function parse_snippet(input)
18+
local ok, parsed = pcall(function()
19+
return vim.lsp._snippet_grammar.parse(input)
20+
end)
21+
return ok and tostring(parsed) or parse_snippet_fallback(input)
22+
end
23+
324
---@param spe? table
425
function M.snippet_text_edits_to_text_edits(spe)
526
if type(spe) ~= 'table' then
627
return
728
end
829
for _, value in ipairs(spe) do
930
if value.newText and value.insertTextFormat then
10-
-- $0 -> Nothing
11-
value.newText = string.gsub(value.newText, '%$%d', '')
12-
-- ${0:_} -> _
13-
value.newText = string.gsub(value.newText, '%${%d:(.-)}', '%1')
31+
value.newText = parse_snippet(value.newText)
1432
end
1533
end
1634
end

0 commit comments

Comments
 (0)