Add alternative command to swtich between go and test file

pull/21/head
ray-x 3 years ago
parent 3619f328b8
commit 034ea57ffe

@ -104,6 +104,14 @@ Modify struct tags by [`gomodifytags`](https://github.com/fatih/gomodifytags) an
nvim-lsp support goimport by default. The plugin provided a new formatter, goline + gofumports (stricter version of
goimport)
## Swtich between go and test file
| command | Description |
| ----------- | ----------- |
| GoAlt / GoAlt! | open alternative go file (use ! to create if not exist) |
| GoAltS / GoAltS! | open alternative go file in split |
| GoAltV / GoAltV! | open alternative go file in vertical split |
## Comments and Doc
Auto doc (to suppress golang-lint warning), generate comments by treesitter parsing result
@ -140,7 +148,7 @@ Setup for Debug provided. Need Dap and Dap UI plugin
![dap](https://user-images.githubusercontent.com/1681295/125160289-743ba080-e1bf-11eb-804f-6a6d227ec33b.jpg)
GDB style key mapping is used
### Keymaps
| Command | Description |
| key | Description |
| ----------- | ----------- |
| c | continue |
| n | next |
@ -230,11 +238,14 @@ e.g
## Nvim LSP setup
For golang, the default gopls setup works perfectly fine, or you can install [navigator.lua](https://github.com/ray-x/navigator.lua) which can auto setup all lsp clients.
go.nvim provided a better non-default setup for gopls (includes debounce, staticcheck, diagnosticsDelay etc)
This gopls setup provided by go.nvim works perfectly fine for most of the cases. You can also install [navigator.lua](https://github.com/ray-x/navigator.lua) which can auto setup all lsp clients and provides a better GUI.
For diagnostic issue, you can use the default setup. There are also quite a few plugins that you can use to explore issues, e.g. [navigator.lua](https://github.com/ray-x/navigator.lua), [folke/lsp-trouble.nvim](https://github.com/folke/lsp-trouble.nvim). [Nvim-tree](https://github.com/kyazdani42/nvim-tree.lua) and [Bufferline](https://github.com/akinsho/nvim-bufferline.lua) also introduced lsp diagnostic hooks.
## Sample vimrc
The following vimrc will enable all features provided by go.nvim
```viml
set termguicolors

@ -69,6 +69,9 @@ function go.setup(cfg)
vim.cmd([[command! Gfstruct lua require("go.reftool").fillstruct()]])
vim.cmd([[command! Gfswitch lua require("go.reftool").fillswitch()]])
vim.cmd([[command! -bang GoAlt lua require"go.alternate".switch("<bang>"=="!", '')]])
vim.cmd([[command! -bang GoAltV lua require"go.alternate".switch("<bang>"=="!", 'vsplit')]])
vim.cmd([[command! -bang GoAltS lua require"go.alternate".switch("<bang>"=="!", 'split')]])
vim.cmd("au FileType go au QuickFixCmdPost [^l]* nested cwindow")
vim.cmd("au FileType go au QuickFixCmdPost l* nested lwindow")

@ -0,0 +1,34 @@
local M = {}
function M.switch(bang, cmd)
local file = vim.fn.expand('%')
local root = ""
local alt_file = ""
if #file <= 1 then
print("no buffer name")
return
end
local s, e = string.find(file, "_test.go$")
local s2, e2 = string.find(file, ".go$")
if s ~= nil then
root = vim.fn.split(file, '_test.go')[1]
alt_file = root .. '.go'
elseif s2 ~= nil then
root = vim.fn.split(file, '.go')[1]
alt_file = root .. '_test.go'
else
print('not a go file')
end
if not vim.fn.filereadable(alt_file) and not vim.fn.bufexists(alt_file) and not bang then
print("couldn't find " .. alt_file)
return
elseif #cmd <= 1 then
local ocmd = "e " .. alt_file
vim.cmd(ocmd)
else
local ocmd = cmd .. " " .. alt_file
vim.cmd(ocmd)
end
end
return M
Loading…
Cancel
Save