refactor of public plugin's api (#37)

* refactor: move all plugin functionality to init.lua

* fix(commands): now it uses correct module paths

* refactor(config): change way how it handles options

* refactor(gotests): use correct config, change way how deps required, fix some typos

* fix(healthchecker): use correct config

* refactor(iferr): change api

* refactor(impl): change api

* refactor(installer): change api

* refactor(struct_tags): change way of importting deps

* refactor(struct_tags): rename M to struct_tags

* run stylua

* refactor(dap): make it all in one file, and make some refactoring

* refactor(_utils): change way how it organizes

* fix: use new _utils api

* refactor(_utils.health): reorganize module

* refactor(_utils.ts): some renameing, moving requires lines

* run stylua
This commit is contained in:
Smirnov Oleksandr 2023-07-19 23:38:23 +03:00 committed by GitHub
parent 94250bb08a
commit 26b41bf68c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 359 additions and 341 deletions

View file

@ -1,14 +1,14 @@
local Job = require "plenary.job"
local c = require("gopher.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 Job = require "plenary.job"
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
local args = { ... }
if #args == 0 then
u.notify("please provice any arguments", "error")
u.deferred_notify("please provice any arguments", vim.log.levels.ERROR)
return
end
@ -29,12 +29,15 @@ return function(cmd, ...)
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")
u.deferred_notify(
"command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval,
vim.log.levels.ERROR
)
u.deferred_notify(cmd .. " " .. unpack(cmd_args), vim.log.levels.DEBUG)
return
end
u.notify("go " .. cmd .. " was success runned", "info")
u.deferred_notify("go " .. cmd .. " was success runned", vim.log.levels.INFO)
end,
}):start()
end

View file

@ -1,17 +1,26 @@
return {
---@param lib string
---@return boolean
lualib_is_found = function(lib)
local is_found, _ = pcall(require, lib)
return is_found
end,
local h = vim.health or require "health"
local health = {}
---@param bin string
---@return boolean
binary_is_found = function(bin)
if vim.fn.executable(bin) == 1 then
return true
end
return false
end,
}
health.start = h.start or h.report_start
health.ok = h.ok or h.report_ok
health.warn = h.warn or h.report_warn
health.error = h.error or h.report_error
health.info = h.info or h.report_info
---@param module string
---@return boolean
function health.is_lualib_found(module)
local is_found, _ = pcall(require, module)
return is_found
end
---@param bin string
---@return boolean
function health.is_binary_found(bin)
if vim.fn.executable(bin) == 1 then
return true
end
return false
end
return health

View file

@ -1,48 +1,39 @@
---@diagnostic disable: param-type-mismatch
return {
---@param t table
---@return boolean
empty = function(t)
if t == nil then
return true
end
local utils = {}
return next(t) == nil
end,
---@param t table
---@return boolean
function utils.is_tbl_empty(t)
if t == nil then
return true
end
return next(t) == nil
end
---@param s string
---@return string
rtrim = function(s)
local n = #s
while n > 0 and s:find("^%s", n) do
n = n - 1
end
---@param s string
---@return string
function utils.rtrim(s)
local n = #s
while n > 0 and s:find("^%s", n) do
n = n - 1
end
return s:sub(1, n)
end,
return s:sub(1, n)
end
---@param msg string
---@param lvl string|integer
notify = function(msg, lvl)
local l
if lvl == "error" or lvl == 4 then
l = vim.log.levels.ERROR
elseif lvl == "info" or lvl == 2 then
l = vim.log.levels.INFO
elseif lvl == "debug" or lvl == 1 then
l = vim.log.levels.DEBUG
end
---@param msg string
---@param lvl any
function utils.deferred_notify(msg, lvl)
vim.defer_fn(function()
vim.notify(msg, lvl)
end, 0)
end
vim.defer_fn(function()
vim.notify(msg, l)
end, 0)
end,
-- safe require
---@param module string module name
function utils.sreq(module)
local ok, m = pcall(require, module)
assert(ok, string.format("gopher.nvim dependency error: %s not installed", module))
return m
end
---safe require
---@param name string module name
sreq = function(name)
local ok, m = pcall(require, name)
assert(ok, string.format("gopher.nvim dependency error: %s not installed", name))
return m
end,
}
return utils

View file

@ -1,7 +1,7 @@
---@diagnostic disable: param-type-mismatch
local nodes = require "gopher._utils.ts.nodes"
local u = require "gopher._utils"
local M = {
local ts = {
querys = {
struct_block = [[((type_declaration (type_spec name:(type_identifier) @struct.name type: (struct_type)))@struct.declaration)]],
em_struct_block = [[(field_declaration name:(field_identifier)@struct.name type: (struct_type)) @struct.declaration]],
@ -27,14 +27,14 @@ end
---@param bufnr string|nil
---@param do_notify boolean|nil
---@return table|nil
function M.get_struct_node_at_pos(row, col, bufnr, do_notify)
function ts.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 query = ts.querys.struct_block .. " " .. ts.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
if notify then
u.notify("struct not found", "warn")
u.deferred_notify("struct not found", vim.log.levels.WARN)
end
else
return ns[#ns]
@ -46,14 +46,14 @@ end
---@param bufnr string|nil
---@param do_notify boolean|nil
---@return table|nil
function M.get_func_method_node_at_pos(row, col, bufnr, do_notify)
function ts.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 query = ts.querys.func .. " " .. ts.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
if notify then
u.notify("function not found", "warn")
u.deferred_notify("function not found", vim.log.levels.WARN)
end
else
return ns[#ns]
@ -65,16 +65,16 @@ end
---@param bufnr string|nil
---@param do_notify boolean|nil
---@return table|nil
function M.get_package_node_at_pos(row, col, bufnr, do_notify)
function ts.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 query = ts.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
if notify then
u.notify("package not found", "warn")
u.deferred_notify("package not found", vim.log.levels.WARN)
return nil
end
else
@ -87,18 +87,18 @@ end
---@param bufnr string|nil
---@param do_notify boolean|nil
---@return table|nil
function M.get_interface_node_at_pos(row, col, bufnr, do_notify)
function ts.get_interface_node_at_pos(row, col, bufnr, do_notify)
local notify = do_notify or true
local query = M.querys.interface
local query = ts.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
if notify then
u.notify("interface not found", "warn")
u.deferred_notify("interface not found", vim.log.levels.WARN)
end
else
return ns[#ns]
end
end
return M
return ts

View file

@ -1,3 +1,7 @@
local ts_query = require "nvim-treesitter.query"
local parsers = require "nvim-treesitter.parsers"
local locals = require "nvim-treesitter.locals"
local u = require "gopher._utils"
local M = {}
local function intersects(row, col, sRow, sCol, eRow, eCol)
@ -53,10 +57,6 @@ end
---@param pos_row string
---@return string
function M.get_all_nodes(query, lang, _, bufnr, pos_row, _)
local ts_query = require "nvim-treesitter.query"
local parsers = require "nvim-treesitter.parsers"
local locals = require "nvim-treesitter.locals"
bufnr = bufnr or 0
pos_row = pos_row or 30000
@ -113,8 +113,6 @@ end
---@param col string
---@return table
function M.nodes_at_cursor(query, default, bufnr, row, col)
local u = require "gopher._utils"
bufnr = bufnr or vim.api.nvim_get_current_buf()
local ft = vim.api.nvim_buf_get_option(bufnr, "ft")
if row == nil or col == nil then
@ -123,13 +121,19 @@ function M.nodes_at_cursor(query, default, bufnr, row, col)
local nodes = M.get_all_nodes(query, ft, default, bufnr, row, col)
if nodes == nil then
u.notify("Unable to find any nodes. Place your cursor on a go symbol and try again", "debug")
u.deferred_notify(
"Unable to find any nodes. Place your cursor on a go symbol and try again",
vim.log.levels.DEBUG
)
return nil
end
nodes = M.sort_nodes(M.intersect_nodes(nodes, row, col))
if nodes == nil or #nodes == 0 then
u.notify("Unable to find any nodes at pos. " .. tostring(row) .. ":" .. tostring(col), "debug")
u.deferred_notify(
"Unable to find any nodes at pos. " .. tostring(row) .. ":" .. tostring(col),
vim.log.levels.DEBUG
)
return nil
end