diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f4765af..ffd50a07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lua/rustaceanvim/overrides.lua b/lua/rustaceanvim/overrides.lua index 0074713a..d1bc4422 100644 --- a/lua/rustaceanvim/overrides.lua +++ b/lua/rustaceanvim/overrides.lua @@ -1,5 +1,26 @@ 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 @@ -7,10 +28,7 @@ function M.snippet_text_edits_to_text_edits(spe) 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