Skip to content

Commit

Permalink
fix(lsp): escape character inserted before } on code action (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored Apr 1, 2024
1 parent 2826321 commit d107d75
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- Escape character inserted before `}` when applying code action
with `SnippetTextEdit` [[#303](https://github.com/mrcjkb/rustaceanvim/issues/303)].

## [4.18.2] - 2024-03-28

### Changed
Expand Down
26 changes: 22 additions & 4 deletions lua/rustaceanvim/overrides.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
local M = {}

---@param input string unparsed snippet
---@return string parsed snippet
local function parse_snippet_fallback(input)
local output = input
-- $0 -> Nothing
:gsub('%$%d', '')
-- ${0:_} -> _
:gsub('%${%d:(.-)}', '%1')
:gsub([[\}]], '}')
return output
end

---@param input string unparsed snippet
---@return string parsed snippet
local function parse_snippet(input)
local ok, parsed = pcall(function()
return vim.lsp._snippet_grammar.parse(input)
end)
return ok and tostring(parsed) or parse_snippet_fallback(input)
end

---@param spe? table
function M.snippet_text_edits_to_text_edits(spe)
if type(spe) ~= 'table' then
return
end
for _, value in ipairs(spe) do
if value.newText and value.insertTextFormat then
-- $0 -> Nothing
value.newText = string.gsub(value.newText, '%$%d', '')
-- ${0:_} -> _
value.newText = string.gsub(value.newText, '%${%d:(.-)}', '%1')
value.newText = parse_snippet(value.newText)
end
end
end
Expand Down

0 comments on commit d107d75

Please sign in to comment.