Skip to content

Commit

Permalink
refactor of public plugin's api (#37)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
olexsmir committed Jul 19, 2023
1 parent 94250bb commit 26b41bf
Show file tree
Hide file tree
Showing 18 changed files with 359 additions and 341 deletions.
19 changes: 11 additions & 8 deletions lua/gopher/_utils/commands.lua
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
41 changes: 25 additions & 16 deletions lua/gopher/_utils/health.lua
Original file line number Diff line number Diff line change
@@ -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
75 changes: 33 additions & 42 deletions lua/gopher/_utils/init.lua
Original file line number Diff line number Diff line change
@@ -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
28 changes: 14 additions & 14 deletions lua/gopher/_utils/ts/init.lua
Original file line number Diff line number Diff line change
@@ -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]],
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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
Expand All @@ -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
20 changes: 12 additions & 8 deletions lua/gopher/_utils/ts/nodes.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
29 changes: 0 additions & 29 deletions lua/gopher/api.lua

This file was deleted.

48 changes: 21 additions & 27 deletions lua/gopher/config.lua
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
---@class Config
---@field commands ConfigCommands
---@class gopher.Config
local config = {}

---@class ConfigCommands
---@field go string
---@field gomodifytags string
---@field gotests string
---@field impl string
---@field iferr string
---@field dlv string

local M = {
---@type Config
config = {
---set custom commands for tools
commands = {
go = "go",
gomodifytags = "gomodifytags",
gotests = "gotests",
impl = "impl",
iferr = "iferr",
dlv = "dlv",
},
---@class gopher.Config
---@field commands gopher.ConfigCommands
local default_config = {
---@class gopher.ConfigCommands
commands = {
go = "go",
gomodifytags = "gomodifytags",
gotests = "gotests",
impl = "impl",
iferr = "iferr",
dlv = "dlv",
},
}

---Plugin setup function
---@param opts Config user config
function M.setup(opts)
M.config = vim.tbl_deep_extend("force", M.config, opts or {})
---@param user_config gopher.Config|nil
function config.setup(user_config)
config = vim.tbl_deep_extend("force", {}, default_config, user_config or {})
end

return M
-- setup ifself, needs for ability to get
-- default config without calling .setup()
config.setup()

return config
Loading

0 comments on commit 26b41bf

Please sign in to comment.