diff --git a/bashrc.d/go.bashrc b/bashrc.d/go.bashrc new file mode 100755 index 0000000..892b3f3 --- /dev/null +++ b/bashrc.d/go.bashrc @@ -0,0 +1,2 @@ +export GOPATH=${HOME}/go +export PATH=${PATH}:${GOPATH}/bin diff --git a/nvim/after/ftplugin/go.lua b/nvim/after/ftplugin/go.lua new file mode 100644 index 0000000..9a6b42e --- /dev/null +++ b/nvim/after/ftplugin/go.lua @@ -0,0 +1,17 @@ +local function map(mode, lhs, rhs, desc) + local opts = { silent = true, noremap = true, desc = desc or '' } + vim.keymap.set(mode, lhs, rhs, opts) +end + +map('n', 'ga', 'GoCodeAction', 'Go: Code [A]ction') +map('n', 'gA', 'GoCodeLenAct', 'Go: Code Lens [A]ction') +map('n', 'gb', 'GoBuild', 'Go: [B]uild') +map('n', 'gr', 'GoRun', 'Go: [R]un') +map('n', 'gd', 'GoDebug', 'Go: [D]ebug') +map('n', 'gt', 'GoTest', 'Go: [T]est') +map('n', 'gf', 'GoFmt', 'Go: [F]ormat') +map('n', 'gi', 'GoImports', 'Go: [I]mports') +map('n', 'gg', 'GoGet', 'Go: [G]et') + +map('n', 'gN', 'GoInstallBinaries', 'Go: I[N]stall Dependent Tools') +map('n', 'gU', 'GoUpdateBinaries', 'Go: [U]pdate Dependent Tools') diff --git a/nvim/after/ftplugin/gomod.lua b/nvim/after/ftplugin/gomod.lua new file mode 100644 index 0000000..83f5335 --- /dev/null +++ b/nvim/after/ftplugin/gomod.lua @@ -0,0 +1 @@ +vim.cmd.runtime { 'after/ftplugin/go.lua', bang = true } diff --git a/nvim/install-deps.sh b/nvim/install-deps.sh index b388ec7..d70be1d 100755 --- a/nvim/install-deps.sh +++ b/nvim/install-deps.sh @@ -5,7 +5,8 @@ sudo emerge -av --noreplace \ x11-misc/xclip \ sys-apps/ripgrep \ net-libs/nodejs \ - sys-devel/clang + sys-devel/clang \ + dev-lang/go # don't want to be root for this, rustup is typically installed by non-root users and won't be # present for root at all. diff --git a/nvim/lazy-lock.json b/nvim/lazy-lock.json index 535c9d1..17c56bb 100644 --- a/nvim/lazy-lock.json +++ b/nvim/lazy-lock.json @@ -11,6 +11,8 @@ "dashboard-nvim": { "branch": "master", "commit": "fabf5feec96185817c732d47d363f34034212685" }, "fidget.nvim": { "branch": "main", "commit": "d855eed8a06531a7e8fd0684889b2943f373c469" }, "gitsigns.nvim": { "branch": "main", "commit": "863903631e676b33e8be2acb17512fdc1b80b4fb" }, + "go.nvim": { "branch": "master", "commit": "51676b430fc9288073769319ba0ccb2a3bcd79c9" }, + "guihua.lua": { "branch": "master", "commit": "225db770e36aae6a1e9e3a65578095c8eb4038d3" }, "indent-blankline.nvim": { "branch": "master", "commit": "e7a4442e055ec953311e77791546238d1eaae507" }, "lazy.nvim": { "branch": "main", "commit": "1159bdccd8910a0fd0914b24d6c3d186689023d9" }, "lazydev.nvim": { "branch": "main", "commit": "491452cf1ca6f029e90ad0d0368848fac717c6d2" }, @@ -29,6 +31,7 @@ "nvim-colorizer.lua": { "branch": "master", "commit": "a065833f35a3a7cc3ef137ac88b5381da2ba302e" }, "nvim-dap": { "branch": "master", "commit": "7ff6936010b7222fea2caea0f67ed77f1b7c60dd" }, "nvim-dap-ui": { "branch": "master", "commit": "ffa89839f97bad360e78428d5c740fdad9a0ff02" }, + "nvim-dap-virtual-text": { "branch": "master", "commit": "52638640ae309cacdaff785fdbb854437bd1ee5c" }, "nvim-lspconfig": { "branch": "master", "commit": "04680101ff79e99b4e33a4386ec27cbd0d360c75" }, "nvim-nio": { "branch": "master", "commit": "a428f309119086dc78dd4b19306d2d67be884eee" }, "nvim-notify": { "branch": "master", "commit": "fbef5d32be8466dd76544a257d3f3dce20082a07" }, diff --git a/nvim/lua/helper/init.lua b/nvim/lua/helper/init.lua index 301c061..5153132 100644 --- a/nvim/lua/helper/init.lua +++ b/nvim/lua/helper/init.lua @@ -20,6 +20,12 @@ function M.in_git_project() return vim.fn.finddir('.git', '.;') ~= '' end +-- TODO: is there some better way to do this? +-- TODO: also add `in_golang_project()` function to detect based on presence of go.mod file? +function M.is_golang_filetype() + return M.contains({ 'go', 'gomod', 'gosum', 'gotmpl' }, vim.bo.filetype) +end + function M.contains(tbl, v) for i, value in ipairs(tbl) do if value == v then diff --git a/nvim/lua/plugins/go.lua b/nvim/lua/plugins/go.lua new file mode 100644 index 0000000..57ab193 --- /dev/null +++ b/nvim/lua/plugins/go.lua @@ -0,0 +1,26 @@ +return { + { + 'ray-x/go.nvim', + dependencies = { + 'ray-x/guihua.lua', + 'neovim/nvim-lspconfig', + 'nvim-treesitter/nvim-treesitter', + }, + config = function() + require('go').setup { + dap_debug_keymap = false, + } + + vim.api.nvim_create_autocmd('BufWritePre', { + group = vim.api.nvim_create_augroup('gered-gofmt-goimports', {}), + pattern = '*.go', + callback = function() + require('go.format').goimports() + end, + }) + end, + event = { 'CmdlineEnter' }, + ft = { 'go', 'gomod' }, + build = ':lua require("go.install").update_all_sync()', + }, +} diff --git a/nvim/lua/plugins/mason.lua b/nvim/lua/plugins/mason.lua index 8576bf9..05411eb 100644 --- a/nvim/lua/plugins/mason.lua +++ b/nvim/lua/plugins/mason.lua @@ -21,6 +21,7 @@ return { 'css-lsp', 'docker-compose-language-service', 'dockerfile-language-server', + 'gopls', 'html-lsp', 'jdtls', 'json-lsp', diff --git a/nvim/lua/plugins/menu.lua b/nvim/lua/plugins/menu.lua index 7516c67..7fc6e6d 100644 --- a/nvim/lua/plugins/menu.lua +++ b/nvim/lua/plugins/menu.lua @@ -73,6 +73,15 @@ function get_default_menu() }) end + if helper.is_golang_filetype() then + table.insert(menu, 1, { + name = '󰟓 Golang Actions', + hl = 'ExBlue', + items = get_golang_menu(), + rtxt = 'g', + }) + end + if helper.in_git_project() then table.insert(menu, 1, { name = ' Git Actions', @@ -398,6 +407,95 @@ function get_cmake_menu() } end +function get_golang_menu() + return { + { + name = 'Code Actions', + cmd = function() + vim.cmd 'GoCodeAction' + end, + rtxt = 'ga', + }, + { + name = 'Code Lens Actions', + cmd = function() + vim.cmd 'GoCodeLenAct' + end, + rtxt = 'gA', + }, + { name = 'separator' }, + { + name = 'Build', + hl = 'ExRed', + cmd = function() + vim.cmd 'GoBuild' + end, + rtxt = 'gb', + }, + { + name = ' Run', + hl = 'ExGreen', + cmd = function() + vim.cmd 'GoRun' + end, + rtxt = 'gr', + }, + { + name = '󰃤 Debug', + hl = 'ExBlue', + cmd = function() + vim.cmd 'GoDebug' + end, + rtxt = 'gd', + }, + { + name = '󰸞 Test', + hl = 'ExRed', + cmd = function() + vim.cmd 'GoTest' + end, + rtxt = 'gt', + }, + { name = 'separator' }, + { + name = 'Format', + cmd = function() + vim.cmd 'GoFmt' + end, + rtxt = 'gf', + }, + { + name = 'Imports', + cmd = function() + vim.cmd 'GoImports' + end, + rtxt = 'gi', + }, + { + name = 'Get', + cmd = function() + vim.cmd 'GoGet' + end, + rtxt = 'gg', + }, + { name = 'separator' }, + { + name = 'Install Dependent Tools', + cmd = function() + vim.cmd 'GoInstallBinaries' + end, + rtxt = 'gN', + }, + { + name = 'Update Dependent Tools', + cmd = function() + vim.cmd = 'GoUpdateBinaries' + end, + rtxt = 'gU', + }, + } +end + function get_git_menu() local gitsigns = require 'gitsigns' return { diff --git a/nvim/lua/plugins/nvim-dap.lua b/nvim/lua/plugins/nvim-dap.lua index 38c8aa3..f838411 100644 --- a/nvim/lua/plugins/nvim-dap.lua +++ b/nvim/lua/plugins/nvim-dap.lua @@ -14,6 +14,9 @@ return { 'williamboman/mason.nvim', 'jay-babu/mason-nvim-dap.nvim', + -- mainly added for go.nvim + 'theHamsta/nvim-dap-virtual-text', + -- Add your own debuggers here --'leoluz/nvim-dap-go', }, diff --git a/nvim/lua/plugins/nvim-treesitter.lua b/nvim/lua/plugins/nvim-treesitter.lua index e1bf8e1..c7e4c2e 100644 --- a/nvim/lua/plugins/nvim-treesitter.lua +++ b/nvim/lua/plugins/nvim-treesitter.lua @@ -13,6 +13,10 @@ return { 'csv', 'diff', 'dockerfile', + 'go', + 'gomod', + 'gosum', + 'gotmpl', 'html', 'ini', 'java', diff --git a/nvim/lua/plugins/which-key.lua b/nvim/lua/plugins/which-key.lua index c62c1ea..0f09311 100644 --- a/nvim/lua/plugins/which-key.lua +++ b/nvim/lua/plugins/which-key.lua @@ -49,6 +49,7 @@ return { { 'b', group = '[B]uffer' }, { 'r', group = '[R]ust' }, { 'c', group = '[C]Make' }, + { 'g', group = '[G]o', icon = { icon = '󰟓' } }, { 'h', group = 'Git [H]unk', mode = { 'n', 'v' } }, }, },