fix(nvim): update code formatting with gopls
This commit is contained in:
parent
a932b58c4f
commit
0907383db4
1 changed files with 17 additions and 2 deletions
|
@ -1,5 +1,20 @@
|
|||
local common = require('lsp.common')
|
||||
|
||||
function OrgImports(wait_ms)
|
||||
local params = vim.lsp.util.make_range_params()
|
||||
params.context = {only = {"source.organizeImports"}}
|
||||
local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, wait_ms)
|
||||
for _, res in pairs(result or {}) do
|
||||
for _, r in pairs(res.result or {}) do
|
||||
if r.edit then
|
||||
vim.lsp.util.apply_workspace_edit(r.edit, "utf-16")
|
||||
else
|
||||
vim.lsp.buf.execute_command(r.command)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function goimports(timeout_ms)
|
||||
local context = { only = { "source.organizeImports" } }
|
||||
vim.validate { context = { context, "t", true } }
|
||||
|
@ -38,14 +53,14 @@ function goimports(timeout_ms)
|
|||
end
|
||||
|
||||
function format_with_imports(timeout_ms)
|
||||
goimports(1000)
|
||||
OrgImports(1000)
|
||||
vim.lsp.buf.formatting()
|
||||
end
|
||||
|
||||
vim.cmd [[
|
||||
augroup gopls
|
||||
autocmd!
|
||||
autocmd BufWritePre *.go lua format_with_imports(1000)
|
||||
autocmd BufWritePre *.go lua format_with_imports(1000, "utf-16")
|
||||
augroup END
|
||||
]]
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue