Some refactoring (#20)

* feat: move all lua api into `api` module

* feat: remove boilerplate code, add go work suport

* refactor(utils): separete module for health

* refactor(dap): remove copy-paste code

* fix: comment

* chore(lsp): disable type checking

* feat: add `go work` command
This commit is contained in:
Smirnov Oleksandr 2022-10-07 17:31:54 +03:00 committed by GitHub
parent d65884b182
commit f835464d7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 144 additions and 155 deletions

View file

@ -0,0 +1,17 @@
return {
---@param lib string
---@return boolean
lualib_is_found = function(lib)
local is_found, _ = pcall(require, lib)
return is_found
end,
---@param bin string
---@return boolean
binary_is_found = function(bin)
if vim.fn.executable(bin) == 1 then
return true
end
return false
end,
}

View file

@ -0,0 +1,40 @@
local Job = require "plenary.job"
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
---Run any go commands like `go generate`, `go get`, `go mod`
---@param cmd string
---@param ... string|string[]
return function(cmd, ...)
local args = { ... }
if #args == 0 then
u.notify("please provice any arguments", "error")
return
end
if cmd == "generate" and #args == 1 and args[1] == "%" then
args[1] = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter
elseif cmd == "get" then
for i, arg in ipairs(args) do
---@diagnostic disable-next-line: param-type-mismatch
local m = string.match(arg, "^https://(.*)$") or string.match(arg, "^http://(.*)$") or arg
table.remove(args, i)
table.insert(args, i, m)
end
end
local cmd_args = vim.list_extend({ cmd }, args) ---@diagnostic disable-line: missing-parameter
Job:new({
command = c.go,
args = cmd_args,
on_exit = function(_, retval)
if retval ~= 0 then
u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error")
u.notify(cmd .. " " .. unpack(cmd_args), "debug")
return
end
u.notify("go " .. cmd .. " was success runned", "info")
end,
}):start()
end

View file

@ -1,3 +1,4 @@
---@diagnostic disable: param-type-mismatch
return {
---@param t table
---@return boolean
@ -20,23 +21,6 @@ return {
return s:sub(1, n)
end,
---@param lib string
---@return boolean
lualib_is_found = function(lib)
local is_found, _ = pcall(require, lib)
return is_found
end,
---@param bin string
---@return boolean
binary_is_found = function(bin)
if vim.fn.executable(bin) == 1 then
return true
end
return false
end,
---@param msg string
---@param lvl string|integer
notify = function(msg, lvl)

View file

@ -1,3 +1,4 @@
---@diagnostic disable: param-type-mismatch
local nodes = require "gopher._utils.ts.nodes"
local u = require "gopher._utils"
local M = {
@ -24,13 +25,17 @@ end
---@param row string
---@param col string
---@param bufnr string|nil
---@param do_notify boolean|nil
---@return table|nil
function M.get_struct_node_at_pos(row, col, bufnr)
function M.get_struct_node_at_pos(row, col, bufnr, do_notify)
local notify = do_notify or true
local query = M.querys.struct_block .. " " .. M.querys.em_struct_block
local bufn = bufnr or vim.api.nvim_get_current_buf()
local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col)
if ns == nil then
u.notify("struct not found", "warn")
if notify then
u.notify("struct not found", "warn")
end
else
return ns[#ns]
end
@ -39,13 +44,17 @@ end
---@param row string
---@param col string
---@param bufnr string|nil
---@param do_notify boolean|nil
---@return table|nil
function M.get_func_method_node_at_pos(row, col, bufnr)
function M.get_func_method_node_at_pos(row, col, bufnr, do_notify)
local notify = do_notify or true
local query = M.querys.func .. " " .. M.querys.method_name
local bufn = bufnr or vim.api.nvim_get_current_buf()
local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col)
if ns == nil then
u.notify("function not found", "warn")
if notify then
u.notify("function not found", "warn")
end
else
return ns[#ns]
end
@ -54,16 +63,20 @@ end
---@param row string
---@param col string
---@param bufnr string|nil
---@param do_notify boolean|nil
---@return table|nil
function M.get_package_node_at_pos(row, col, bufnr)
function M.get_package_node_at_pos(row, col, bufnr, do_notify)
local notify = do_notify or true
-- stylua: ignore
if row > 10 then return end
local query = M.querys.package
local bufn = bufnr or vim.api.nvim_get_current_buf()
local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col)
if ns == nil then
u.notify("package not found", "warn")
return nil
if notify then
u.notify("package not found", "warn")
return nil
end
else
return ns[#ns]
end
@ -72,13 +85,17 @@ end
---@param row string
---@param col string
---@param bufnr string|nil
---@param do_notify boolean|nil
---@return table|nil
function M.get_interface_node_at_pos(row, col, bufnr)
function M.get_interface_node_at_pos(row, col, bufnr, do_notify)
local notify = do_notify or true
local query = M.querys.interface
local bufn = bufnr or vim.api.nvim_get_current_buf()
local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col)
if ns == nil then
u.notify("interface not found", "warn")
if notify then
u.notify("interface not found", "warn")
end
else
return ns[#ns]
end