7 files changed,
105 insertions(+),
69 deletions(-)
Author:
Oleksandr Smirnov
olexsmir@gmail.com
Committed at:
2025-09-10 16:17:15 +0300
Change ID:
pxtxzkttoqtyosvmwyrlsrurznywmkmr
Parent:
440a7cc
M
README.md
@@ -201,6 +201,12 @@
-- timeout for running internal commands timeout = 2000, + -- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync) + installer_timeout = 999999, + + -- restart gopls server after commands like `:GoMod`, `:GoGet`, `:GoWork` + restart_lsp = false, + commands = { go = "go", gomodifytags = "gomodifytags",
M
doc/gopher.nvim.txt
@@ -45,6 +45,12 @@
============================================================================== ------------------------------------------------------------------------------ + *config* + `config` +Type ~ +`(gopher.Config)` + +------------------------------------------------------------------------------ *gopher.nvim-config* `default_config` >lua@@ -57,8 +63,11 @@ -- timeout for running internal commands
---@type number timeout = 2000, - --- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync) + -- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync) installer_timeout = 999999, + + -- restart gopls server after commands like `:GoMod`, `:GoGet`, `:GoWork` + restart_lsp = false, -- user specified paths to binaries ---@class gopher.ConfigCommand@@ -96,6 +105,8 @@ }
< Class ~ {gopher.Config} +Fields ~ +{setup} `(fun(user_config?: gopher.Config))` ==============================================================================
D
@@ -1,51 +0,0 @@
-local r = require "gopher._utils.runner" -local c = require("gopher.config").commands -local u = require "gopher._utils" -local gocmd = {} - ----@param args string[] ----@return string[] -local function if_get(args) - for i, arg in ipairs(args) do - local m = string.match(arg, "^https://(.*)$") or string.match(arg, "^http://(.*)$") or arg - table.remove(args, i) - table.insert(args, i, m) - end - return args -end - ----@param args unknown[] ----@return string[] -local function if_generate(args) - if #args == 1 and args[1] == "%" then - args[1] = vim.fn.expand "%" - end - return args -end - ----@param subcmd string ----@param args string[] ----@return string -function gocmd.run(subcmd, args) - if #args == 0 and subcmd ~= "generate" then - error "please provide any arguments" - end - - if subcmd == "get" then - args = if_get(args) - end - - if subcmd == "generate" then - args = if_generate(args) - end - - local rs = r.sync { c.go, subcmd, unpack(args) } - if rs.code ~= 0 then - error("go " .. subcmd .. " failed: " .. rs.stderr) - end - - u.notify(c.go .. " " .. subcmd .. " ran successful") - return rs.stdout -end - -return gocmd
A
lua/gopher/_utils/lsp.lua
@@ -0,0 +1,11 @@
+local lsp = {} + +local gopls = "gopls" + +-- Restarts gopls server (see: `:h lsp-restart`) +function lsp.restart() + vim.lsp.enable(gopls, false) + vim.lsp.enable(gopls, true) +end + +return lsp
M
lua/gopher/config.lua
@@ -25,8 +25,11 @@ -- timeout for running internal commands
---@type number timeout = 2000, - --- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync) + -- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync) installer_timeout = 999999, + + -- restart gopls server after commands like `:GoMod`, `:GoGet`, `:GoWork` + restart_lsp = false, -- user specified paths to binaries ---@class gopher.ConfigCommand@@ -84,6 +87,7 @@
vim.validate("log_level", _config.log_level, "number") vim.validate("timeout", _config.timeout, "number") vim.validate("installer_timeout", _config.timeout, "number") + vim.validate("restart_lsp", _config.restart_lsp, "boolean") vim.validate("commands", _config.commands, "table") vim.validate("commands.go", _config.commands.go, "string") vim.validate("commands.gomodifytags", _config.commands.gomodifytags, "string")
A
lua/gopher/go.lua
@@ -0,0 +1,66 @@
+local c = require "gopher.config" +local u = require "gopher._utils" +local lsp = require "gopher._utils.lsp" +local r = require "gopher._utils.runner" +local go = {} + +local function run(subcmd, args) + local rs = r.sync { c.commands.go, subcmd, unpack(args) } + if rs.code ~= 0 then + error("go " .. subcmd .. " failed: " .. rs.stderr) + end + + u.notify(c.commands.go .. " " .. subcmd .. " ran successful") + return rs.stdout +end + +local function restart_lsp() + if c.restart_lsp then + lsp.restart() + end +end + +---@param args string[] +function go.get(args) + for i, arg in ipairs(args) do + local m = string.match(arg, "^https://(.*)$") or string.match(arg, "^http://(.*)$") or arg + table.remove(args, i) + table.insert(args, i, m) + end + + run("get", args) + restart_lsp() +end + +---@param args string[] +function go.mod(args) + run("mod", args) + restart_lsp() +end + +---@param args string[] +function go.work(args) + -- TODO: use `gopls.tidy` + + run("work", args) + restart_lsp() +end + +---Executes `go generate` +---If only argument is `%` it's going to be equivalent to `go generate <path to current file>` +---@param args string[] +function go.generate(args) + -- TODO: use `gopls.generate` + + if #args == 0 then + error "please provide arguments" + end + + if #args == 1 and args[1] == "%" then + args[1] = vim.fn.expand "%" + end + + run("generate", args) +end + +return go
M
lua/gopher/init.lua
@@ -13,7 +13,7 @@
local log = require "gopher._utils.log" local tags = require "gopher.struct_tags" local tests = require "gopher.gotests" -local gocmd = require("gopher._utils.gocmd").run +local go = require "gopher.go" local gopher = {} ---@toc_entry Setup@@ -58,20 +58,9 @@ exported = tests.all_exported_tests,
all = tests.all_tests, } -gopher.get = function(...) - gocmd("get", ...) -end - -gopher.mod = function(...) - gocmd("mod", ...) -end - -gopher.generate = function(...) - gocmd("generate", ...) -end - -gopher.work = function(...) - gocmd("work", ...) -end +gopher.get = go.get +gopher.mod = go.mod +gopher.work = go.work +gopher.generate = go.generate return gopher