From 44d6a9d8f3673d2fe3f8bba6b71b530a889a35d6 Mon Sep 17 00:00:00 2001 From: ray-x Date: Thu, 16 Jun 2022 14:10:49 +1000 Subject: [PATCH] add list_imports support --- README.md | 3 ++- lua/go/codeaction.lua | 12 +++++++++++- lua/go/gopls.lua | 23 +++++++++++++++++++++++ lua/go/lsp.lua | 1 + lua/go/rename.lua | 2 +- 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 639597a..cc77d68 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,10 @@ The plugin covers most features required for a gopher. - Go to alternative go file (between test and source) - Test with ginkgo, richgo inside floaterm (to enable floaterm, guihua.lua has to be installed) - Go 1.18 support, configure your go to `go1.18` in config -- GoFixPlural, FixStruct, FixSwitch, Add comment, IfErr, ModTidy, GoGet ... Most of the tools are built on top of +- Code refactor made easy: GoFixPlural, FixStruct, FixSwitch, Add comment, IfErr, ModTidy, GoGet ... Most of the tools are built on top of treesitter AST or go AST. It is fast and accurate. - GoCheat get go cheatsheet from [cheat.sh](https://cheat.sh/). +- Smart build tag detection when debug/run tests (e.g. `//go:build integration`) ## Installation diff --git a/lua/go/codeaction.lua b/lua/go/codeaction.lua index 2a4f7fa..503160b 100644 --- a/lua/go/codeaction.lua +++ b/lua/go/codeaction.lua @@ -9,16 +9,26 @@ function M.run_action() local guihua = utils.load_plugin("guihua.lua", "guihua.gui") local original_select = vim.ui.select + local original_input = vim.ui.input if guihua then vim.ui.select = require("guihua.gui").select + vim.ui.input = require("guihua.input").input end log("codeaction") - vim.lsp.buf.code_action() + if vim.api.nvim_get_mode().mode ~= "v" then + vim.lsp.buf.code_action() + else + vim.lsp.buf.range_code_action() + end vim.defer_fn(function() vim.ui.select = original_select end, 1000) + + vim.defer_fn(function() + vim.ui.input = original_input + end, 10000) end return M diff --git a/lua/go/gopls.lua b/lua/go/gopls.lua index 63e58ca..e63ee27 100644 --- a/lua/go/gopls.lua +++ b/lua/go/gopls.lua @@ -90,6 +90,29 @@ M.import = function(path) }) end +M.list_imports = function(path) + path = path or vim.fn.expand("%:p") + local resp = cmds.list_imports({ + URI = path, + }) + result = {} + for _, v in pairs(resp) do + if v.result then + for k, val in pairs(v.result) do + result[k] = {} + for _, imp in ipairs(val) do + if imp.Name and imp.Name ~= "" then + table.insert(result[k], imp.Name .. ":" .. imp.Path) + else + table.insert(result[k], imp.Path) + end + end + end + end + end + return result, resp +end + M.list_pkgs = function() local resp = cmds.list_known_packages() or {} diff --git a/lua/go/lsp.lua b/lua/go/lsp.lua index 2d04d3b..c7f5423 100644 --- a/lua/go/lsp.lua +++ b/lua/go/lsp.lua @@ -52,6 +52,7 @@ local on_attach = function(client, bufnr) buf_set_keymap("n", "D", "lua vim.lsp.buf.type_definition()", opts) buf_set_keymap("n", "rn", "lua require('go.rename').run()", opts) buf_set_keymap("n", "ca", "lua require('go.codeaction').run_action()", opts) + buf_set_keymap("v", "ca", "lua require('go.codeaction').run_action()", opts) buf_set_keymap("n", "gr", "lua vim.lsp.buf.references()", opts) buf_set_keymap("n", "e", "lua vim.lsp.diagnostic.show_line_diagnostics()", opts) buf_set_keymap("n", "[d", "lua vim.lsp.diagnostic.goto_prev()", opts) diff --git a/lua/go/rename.lua b/lua/go/rename.lua index 553c636..2d5dc9e 100644 --- a/lua/go/rename.lua +++ b/lua/go/rename.lua @@ -10,7 +10,7 @@ local lsprename = function() local input = vim.ui.input if guihua then - vim.ui.input = require('guihua.floating').input + vim.ui.input = require('guihua.input').input end vim.lsp.buf.rename() return vim.defer_fn(function()