fix(typescript): implemented move to new file. Fixes #3534

This commit is contained in:
Folke Lemaitre
2024-06-08 07:30:49 +02:00
parent 1394070aab
commit d90930b13e
2 changed files with 55 additions and 3 deletions

View File

@ -119,6 +119,56 @@ return {
return true
end,
vtsls = function(_, opts)
LazyVim.lsp.on_attach(function(client, buffer)
client.commands["_typescript.moveToFileRefactoring"] = function(command, ctx)
---@type string, string, lsp.Range
local action, uri, range = unpack(command.arguments)
local function move(newf)
client.request("workspace/executeCommand", {
command = command.command,
arguments = { action, uri, range, newf },
})
end
local fname = vim.uri_to_fname(uri)
client.request("workspace/executeCommand", {
command = "typescript.tsserverRequest",
arguments = {
"getMoveToRefactoringFileSuggestions",
{
file = fname,
startLine = range.start.line + 1,
startOffset = range.start.character + 1,
endLine = range["end"].line + 1,
endOffset = range["end"].character + 1,
},
},
}, function(_, result)
---@type string[]
local files = result.body.files
table.insert(files, 1, "Enter new path...")
vim.ui.select(files, {
prompt = "Select move destination:",
format_item = function(f)
return vim.fn.fnamemodify(f, ":~:.")
end,
}, function(f)
if f and f:find("^Enter new path") then
vim.ui.input({
prompt = "Enter move destination:",
default = vim.fn.fnamemodify(fname, ":h") .. "/",
completion = "file",
}, function(newf)
return newf and move(newf)
end)
elseif f then
move(f)
end
end)
end)
end
end, "vtsls")
-- copy typescript settings to javascript
opts.settings.javascript =
vim.tbl_deep_extend("force", {}, opts.settings.typescript, opts.settings.javascript or {})

View File

@ -22,12 +22,13 @@ function M.get_clients(opts)
end
---@param on_attach fun(client:vim.lsp.Client, buffer)
function M.on_attach(on_attach)
---@param name? string
function M.on_attach(on_attach, name)
return vim.api.nvim_create_autocmd("LspAttach", {
callback = function(args)
local buffer = args.buf ---@type number
local client = vim.lsp.get_client_by_id(args.data.client_id)
if client then
if client and (not name or client.name == name) then
return on_attach(client, buffer)
end
end,
@ -340,6 +341,7 @@ M.action = setmetatable({}, {
---@class LspCommand: lsp.ExecuteCommandParams
---@field open? boolean
---@field handler? lsp.Handler
---@param opts LspCommand
function M.execute(opts)
@ -353,7 +355,7 @@ function M.execute(opts)
params = params,
})
else
return vim.lsp.buf_request(0, "workspace/executeCommand", params)
return vim.lsp.buf_request(0, "workspace/executeCommand", params, opts.handler)
end
end