Skip to content

Commit

Permalink
fix(lsp): use own rename
Browse files Browse the repository at this point in the history
Fixes #5.

The previous rename was asynchronous. Local rename somehow interfered
with that. I rewrote the rename implementation to use coroutines for
proper operation sequencing.
  • Loading branch information
gregorias committed Jun 10, 2024
1 parent 00db852 commit 290c93c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
7 changes: 6 additions & 1 deletion lua/coerce.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local M = {}

local case_m = require("coerce.case")
local coroutine_m = require("coerce.coroutine")
local selector_m = require("coerce.selector")
local transformer_m = require("coerce.transformer")
local conversion_m = require("coerce.conversion")
Expand All @@ -23,7 +24,11 @@ M.default_modes = {
keymap_prefix = "cr",
selector = selector_m.select_current_word,
transformer = function(selected_region, apply)
return transformer_m.transform_lsp_rename_with_local_failover(selected_region, apply)
return coroutine_m.fire_and_forget(
transformer_m.transform_lsp_rename_with_local_failover,
selected_region,
apply
)
end,
},
{
Expand Down
24 changes: 15 additions & 9 deletions lua/coerce/transformer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
--@module coerce.transformer
local M = {}

local lsp_rename = require("coerce.vim.lsp").rename

--- Returns transformed selected text.
---
---@tparam Region selected_region The selected region to change.
Expand Down Expand Up @@ -45,6 +47,8 @@ end

--- Changes the selected text with the apply function using LSP rename.
---
--- This is a fire-and-forget coroutine function.
---
---@tparam Region selected_region The selected region to change.
---@tparam function apply The function to apply to the selected region.
---@treturn boolean Whether the function has succeeded.
Expand All @@ -53,10 +57,10 @@ M.transform_lsp_rename = function(selected_region, apply)
return false
end
local transformed_text = apply_case_to_selected_region(selected_region, apply)
return vim.lsp.buf.rename(transformed_text)
return lsp_rename(transformed_text)
end

--- Returns a transform functions that tries out all transforms until one works.
--- Returns a transform function that tries out all transforms until one works.
---
--- If any of the transforms is a coroutine function, the returned function will also be one.
---
Expand All @@ -75,13 +79,15 @@ M.coalesce_transforms = function(transforms)
end

--- Changes the selected text with the apply function using LSP rename.
--
-- The LSP rename only works on the symbol under the cursor, so it’s best not
-- to use this function for any other selection mode.
--
--@tparam Region selected_region The selected region to change.
--@tparam function apply The function to apply to the selected region.
--@treturn boolean Whether the function has succeeded.
---
--- This is a fire-and-forget coroutine function.
---
--- The LSP rename only works on the symbol under the cursor, so it’s best not
--- to use this function for any other selection mode.
---
---@tparam Region selected_region The selected region to change.
---@tparam function apply The function to apply to the selected region.
---@treturn boolean Whether the function has succeeded.
M.transform_lsp_rename_with_local_failover = function(selected_region, apply)
return M.coalesce_transforms({
M.transform_lsp_rename,
Expand Down

0 comments on commit 290c93c

Please sign in to comment.