如何在保存时组织导入?我正在使用它来创建一个组织导入命令
_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
这让我可以 :LspOrganize 来组织导入,但在保存缓冲区之前它不会这样做。执行自动命令组,但在保存文件后组织导入,因为我相信这是一个异步任务。有人可以帮我做这个同步吗?我想组织导入,然后保存缓冲区。