diff --git a/lua/go/format.lua b/lua/go/format.lua index 6a1607b..4a542fa 100644 --- a/lua/go/format.lua +++ b/lua/go/format.lua @@ -70,7 +70,7 @@ local M = {} M.gofmt = function(buf) if _GO_NVIM_CFG.gofmt == "gopls" then -- log("gopls format") - vim.lsp.buf.formatting() + vim.lsp.buf.format({ async = true }) return end vim.env.GO_FMT = "gofumpt" diff --git a/lua/go/lsp.lua b/lua/go/lsp.lua index 0040be1..8e68583 100644 --- a/lua/go/lsp.lua +++ b/lua/go/lsp.lua @@ -5,8 +5,16 @@ local diagnostic_map = function(bufnr) local opts = { noremap = true, silent = true } api.nvim_buf_set_keymap(bufnr, "n", "]O", ":lua vim.lsp.diagnostic.set_loclist()", opts) end + +if vim.lsp.buf.format == nil then + -- neovim < 0.7 only + log.error("LSP: format is not available") + vim.lsp.buf.format = vim.lsp.buf.formatting +end + local codelens_enabled = false local on_attach = function(client, bufnr) + local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end @@ -27,13 +35,13 @@ local on_attach = function(client, bufnr) if _GO_NVIM_CFG.lsp_codelens then -- codelens_enabled = client.resolved_capabilities["code_lens"] - codelens_enabled = client.server_capabilities.codeLensProvider + codelens_enabled = (client.server_capabilities.codeLensProvider ~= false) if not codelens_enabled then vim.notify("codelens not support by your gopls", vim.lsp.log_levels.WARN) end end if _GO_NVIM_CFG.lsp_keymaps == true then - buf_set_keymap("n", "gD", ":lua vim.lsp.buf.formatting()", opts) + buf_set_keymap("n", "gD", ":lua vim.lsp.buf.format({async = true}))", opts) buf_set_keymap("n", "gD", "lua vim.lsp.buf.declaration()", opts) buf_set_keymap("n", "gd", "lua vim.lsp.buf.definition()", opts) buf_set_keymap("n", "K", "lua vim.lsp.buf.hover()", opts) @@ -52,10 +60,10 @@ local on_attach = function(client, bufnr) buf_set_keymap("n", "q", "lua vim.lsp.diagnostic.set_loclist()", opts) if client.server_capabilities.documentFormattingProvider then - buf_set_keymap("n", "ff", "lua vim.lsp.buf.formatting()", opts) + buf_set_keymap("n", "ff", "lua vim.lsp.buf.format({async = true})", opts) end if client.resolved_capabilities.document_formatting then - buf_set_keymap("n", "ff", "lua vim.lsp.buf.formatting()", opts) + buf_set_keymap("n", "ff", "lua vim.lsp.buf.format({async = true})", opts) end elseif type(_GO_NVIM_CFG.lsp_keymaps) == "function" then _GO_NVIM_CFG.lsp_keymaps(bufnr) @@ -97,7 +105,7 @@ function M.config() end if _GO_NVIM_CFG == nil then vim.notify("please setup go.nvim", vim.lsp.log_levels.WARN) - require'go'.setup{} + require("go").setup({}) end gopls.on_attach = on_attach if type(_GO_NVIM_CFG.lsp_on_attach) == "function" then diff --git a/lua/go/reftool.lua b/lua/go/reftool.lua index 68f2dba..67ed856 100644 --- a/lua/go/reftool.lua +++ b/lua/go/reftool.lua @@ -25,7 +25,7 @@ local function insert_result(result) vim.cmd("write") -- format(#inserts, curpos) fn.setpos(".", curpos) - vim.lsp.buf.formatting() + vim.lsp.buf.format({ async = true }) end -- can only be fillstruct and fillswitch