Ari*_*pta 2 lua neovim nvim-lspconfig
如何实现保存时组织导入?我正在使用它来创建组织导入命令
_G.lsp_organize_imports = function()
local params = {
command = "_typescript.organizeImports",
arguments = {vim.api.nvim_buf_get_name(0)},
title = ""
}
vim.lsp.buf.execute_command(params)
end
local on_attach = function(client, bufnr)
local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end
--Enable completion triggered by <c-x><c-o>
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
-- Mappings.
local opts = { noremap=true, silent=true }
local buf_map = vim.api.nvim_buf_set_keymap
vim.cmd("command! LspDef lua vim.lsp.buf.definition()")
vim.cmd("command! LspFormatting lua vim.lsp.buf.formatting_sync()")
vim.cmd("command! LspCodeAction lua vim.lsp.buf.code_action()")
vim.cmd("command! LspHover lua vim.lsp.buf.hover()")
vim.cmd("command! LspRename lua vim.lsp.buf.rename()")
vim.cmd("command! LspOrganize lua lsp_organize_imports()")
vim.cmd("command! LspRefs lua vim.lsp.buf.references()")
vim.cmd("command! LspTypeDef lua vim.lsp.buf.type_definition()")
vim.cmd("command! LspImplementation lua vim.lsp.buf.implementation()")
vim.cmd("command! LspDiagPrev lua vim.lsp.diagnostic.goto_prev()")
vim.cmd("command! LspDiagNext lua vim.lsp.diagnostic.goto_next()")
vim.cmd(
"command! LspDiagLine lua vim.lsp.diagnostic.show_line_diagnostics()")
vim.cmd("command! LspSignatureHelp lua vim.lsp.buf.signature_help()")buf_map(bufnr, "n", "gd", ":LspDef<CR>", {silent = true})
buf_map(bufnr, "n", "gr", ":LspRename<CR>", {silent = true})
buf_map(bufnr, "n", "gR", ":LspRefs<CR>", {silent = true})
buf_map(bufnr, "n", "gy", ":LspTypeDef<CR>", {silent = true})
buf_map(bufnr, "n", "K", ":LspHover<CR>", {silent = true})
buf_map(bufnr, "n", "gs", ":LspOrganize<CR>", {silent = true})
buf_map(bufnr, "n", "[a", ":LspDiagPrev<CR>", {silent = true})
buf_map(bufnr, "n", "]a", ":LspDiagNext<CR>", {silent = true})
buf_map(bufnr, "n", "ga", ":LspCodeAction<CR>", {silent = true})
buf_map(bufnr, "n", "<Leader>a", ":LspDiagLine<CR>", {silent = true})
buf_map(bufnr, "i", "<C-x><C-x>", "<cmd> LspSignatureHelp<CR>",
{silent = true})
if client.resolved_capabilities.document_formatting then
vim.api.nvim_exec([[
augroup LspAutocommands
autocmd! * <buffer>
autocmd BufWritePre <buffer> LspOrganize
autocmd BufWritePre <buffer> LspFormatting
augroup END
]], true)
end
end
Run Code Online (Sandbox Code Playgroud)
这让我可以执行 :LspOrganize 来组织导入,但在保存缓冲区之前它不会执行此操作。自动命令组已执行,但导入是在保存文件后组织的,因为我相信这是一个异步任务。有人可以帮我同步吗?我想组织导入然后保存缓冲区。
您不想vim.lsp.buf.execute_command使用 neovim lsp API 提供的同步版本。看一下:help vim.lsp.buf_request_sync。使用它,您可以编写组织导入函数的同步版本。
_G.lsp_organize_imports_sync = function(bufnr)
-- gets the current bufnr if no bufnr is passed
if not bufnr then bufnr = vim.api.nvim_get_current_buf() end
-- params for the request
local params = {
command = "_typescript.organizeImports",
arguments = {vim.api.nvim_buf_get_name(bufnr)},
title = ""
}
-- perform a syncronous request
-- 500ms timeout depending on the size of file a bigger timeout may be needed
vim.lsp.buf_request_sync(bufnr, "workspace/executeCommand", params, 500)
end
Run Code Online (Sandbox Code Playgroud)