refactor: move requires in place where they used (#22)

* refactor(comment): move require to a function

* refactor(gotests): move requires inside of functions

* refactor(health): move requires inside of function

* refactor(iferr): move requires inside of a function

* refactor(impl): move some requires inside of functions

* refactor(installer): move requires inside of function

* refactor(struct_tags): move requires into function

* refactor(dap): move import into function

* refactor(utils): move import into functions
This commit is contained in:
Smirnov Oleksandr 2022-12-19 11:47:43 +02:00 committed by GitHub
parent e8fe6c5b15
commit 2f0edbfdfc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 43 additions and 34 deletions

View file

@ -1,11 +1,11 @@
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` ---Run any go commands like `go generate`, `go get`, `go mod`
---@param cmd string ---@param cmd string
---@param ... string|string[] ---@param ... string|string[]
return function(cmd, ...) return function(cmd, ...)
local Job = require "plenary.job"
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
local args = { ... } local args = { ... }
if #args == 0 then if #args == 0 then
u.notify("please provice any arguments", "error") u.notify("please provice any arguments", "error")

View file

@ -1,7 +1,3 @@
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 M = {}
local function intersects(row, col, sRow, sCol, eRow, eCol) local function intersects(row, col, sRow, sCol, eRow, eCol)
@ -57,6 +53,10 @@ end
---@param pos_row string ---@param pos_row string
---@return string ---@return string
function M.get_all_nodes(query, lang, _, bufnr, pos_row, _) 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 bufnr = bufnr or 0
pos_row = pos_row or 30000 pos_row = pos_row or 30000
@ -113,6 +113,8 @@ end
---@param col string ---@param col string
---@return table ---@return table
function M.nodes_at_cursor(query, default, bufnr, row, col) function M.nodes_at_cursor(query, default, bufnr, row, col)
local u = require "gopher._utils"
bufnr = bufnr or vim.api.nvim_get_current_buf() bufnr = bufnr or vim.api.nvim_get_current_buf()
local ft = vim.api.nvim_buf_get_option(bufnr, "ft") local ft = vim.api.nvim_buf_get_option(bufnr, "ft")
if row == nil or col == nil then if row == nil or col == nil then

View file

@ -1,6 +1,5 @@
local ts_utils = require "gopher._utils.ts"
local function generate(row, col) local function generate(row, col)
local ts_utils = require "gopher._utils.ts"
local comment, ns = nil, nil local comment, ns = nil, nil
ns = ts_utils.get_package_node_at_pos(row, col, nil, false) ns = ts_utils.get_package_node_at_pos(row, col, nil, false)

View file

@ -1,9 +1,10 @@
local cfg = require "gopher.dap.config"
local u = require "gopher._utils"
local M = {} local M = {}
---setup nvim-dap for golang using ---setup nvim-dap for golang using
function M.setup() function M.setup()
local cfg = require "gopher.dap.config"
local u = require "gopher._utils"
local dap = u.sreq "dap" local dap = u.sreq "dap"
dap.adapters.go = cfg.adapter dap.adapters.go = cfg.adapter
dap.configurations.go = cfg.configuration dap.configurations.go = cfg.configuration

View file

@ -1,11 +1,11 @@
local Job = require "plenary.job"
local ts_utils = require "gopher._utils.ts"
local c = require("gopher.config").config.commands
local u = require "gopher._utils" local u = require "gopher._utils"
local M = {} local M = {}
---@param cmd_args table ---@param cmd_args table
local function run(cmd_args) local function run(cmd_args)
local Job = require "plenary.job"
local c = require("gopher.config").config.commands
Job:new({ Job:new({
command = c.gotests, command = c.gotests,
args = cmd_args, args = cmd_args,
@ -31,6 +31,8 @@ end
---generate unit test for one function ---generate unit test for one function
---@param parallel boolean ---@param parallel boolean
function M.func_test(parallel) function M.func_test(parallel)
local ts_utils = require "gopher._utils.ts"
local ns = ts_utils.get_func_method_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0))) local ns = ts_utils.get_func_method_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0)))
if ns == nil or ns.name == nil then if ns == nil or ns.name == nil then
u.notify("cursor on func/method and execute the command again", "info") u.notify("cursor on func/method and execute the command again", "info")

View file

@ -1,13 +1,11 @@
local health = vim.health or require "health"
local utils = require "gopher._utils._health"
local c = require("gopher.config").config.commands local c = require("gopher.config").config.commands
local requried = "Gopher.nvim will not work without it!" local requried_for_work_msg = "Gopher.nvim will not work without it!"
local M = { local M = {
_required = { _required = {
plugins = { plugins = {
{ lib = "plenary", help = requried }, { lib = "plenary", help = requried_for_work_msg },
{ lib = "nvim-treesitter", help = requried }, { lib = "nvim-treesitter", help = requried_for_work_msg },
{ lib = "dap", help = "Required for set upping debugger" }, { lib = "dap", help = "Required for set upping debugger" },
}, },
binarys = { binarys = {
@ -21,9 +19,12 @@ local M = {
} }
function M.check() function M.check()
local health = vim.health or require "health"
local u = require "gopher._utils._health"
health.report_start "Required plugins" health.report_start "Required plugins"
for _, plugin in ipairs(M._required.plugins) do for _, plugin in ipairs(M._required.plugins) do
if utils.lualib_is_found(plugin.lib) then if u.lualib_is_found(plugin.lib) then
health.report_ok(plugin.lib .. " installed.") health.report_ok(plugin.lib .. " installed.")
else else
health.report_error(plugin.lib .. " not found. " .. plugin.help) health.report_error(plugin.lib .. " not found. " .. plugin.help)
@ -32,7 +33,7 @@ function M.check()
health.report_start "Required go tools" health.report_start "Required go tools"
for _, binary in ipairs(M._required.binarys) do for _, binary in ipairs(M._required.binarys) do
if utils.binary_is_found(binary.bin) then if u.binary_is_found(binary.bin) then
health.report_ok(binary.bin .. " installed") health.report_ok(binary.bin .. " installed")
else else
health.report_warn(binary.bin .. " is not installed but " .. binary.help) health.report_warn(binary.bin .. " is not installed but " .. binary.help)

View file

@ -1,10 +1,10 @@
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
---Add iferr declaration ---Add iferr declaration
---That's Lua of vimscript implementation of: ---That's Lua of vimscript implementation of:
---github.com/koron/iferr ---github.com/koron/iferr
return function() return function()
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
local boff = vim.fn.wordcount().cursor_bytes local boff = vim.fn.wordcount().cursor_bytes
local cmd = (c.iferr .. " -pos " .. boff) local cmd = (c.iferr .. " -pos " .. boff)
local data = vim.fn.systemlist(cmd, vim.fn.bufnr "%") local data = vim.fn.systemlist(cmd, vim.fn.bufnr "%")

View file

@ -1,10 +1,9 @@
local Job = require "plenary.job"
local ts_utils = require "gopher._utils.ts"
local c = require("gopher.config").config.commands
local u = require "gopher._utils" local u = require "gopher._utils"
---@return string ---@return string
local function get_struct() local function get_struct()
local ts_utils = require "gopher._utils.ts"
local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0))) local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0)))
if ns == nil then if ns == nil then
u.notify("put cursor on a struct or specify a receiver", "info") u.notify("put cursor on a struct or specify a receiver", "info")
@ -20,6 +19,9 @@ local function get_struct()
end end
return function(...) return function(...)
local c = require("gopher.config").config.commands
local Job = require "plenary.job"
local args = { ... } local args = { ... }
local iface, recv_name = "", "" local iface, recv_name = "", ""
local recv = get_struct() local recv = get_struct()

View file

@ -1,5 +1,3 @@
local Job = require "plenary.job"
local u = require "gopher._utils"
local urls = { local urls = {
gomodifytags = "github.com/fatih/gomodifytags", gomodifytags = "github.com/fatih/gomodifytags",
impl = "github.com/josharian/impl", impl = "github.com/josharian/impl",
@ -10,6 +8,9 @@ local urls = {
---@param pkg string ---@param pkg string
local function install(pkg) local function install(pkg)
local Job = require "plenary.job"
local u = require "gopher._utils"
local url = urls[pkg] .. "@latest" local url = urls[pkg] .. "@latest"
Job:new({ Job:new({

View file

@ -1,10 +1,11 @@
local Job = require "plenary.job"
local ts_utils = require "gopher._utils.ts"
local u = require "gopher._utils"
local c = require("gopher.config").config.commands
local M = {} local M = {}
local function modify(...) local function modify(...)
local ts_utils = require "gopher._utils.ts"
local Job = require "plenary.job"
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
local fpath = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter local fpath = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter
local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0))) local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0)))
if ns == nil then if ns == nil then