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`
---@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")

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 function intersects(row, col, sRow, sCol, eRow, eCol)
@ -57,6 +53,10 @@ 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,6 +113,8 @@ 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

View file

@ -1,6 +1,5 @@
local ts_utils = require "gopher._utils.ts"
local function generate(row, col)
local ts_utils = require "gopher._utils.ts"
local comment, ns = nil, nil
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 = {}
---setup nvim-dap for golang using
function M.setup()
local cfg = require "gopher.dap.config"
local u = require "gopher._utils"
local dap = u.sreq "dap"
dap.adapters.go = cfg.adapter
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 M = {}
---@param cmd_args table
local function run(cmd_args)
local Job = require "plenary.job"
local c = require("gopher.config").config.commands
Job:new({
command = c.gotests,
args = cmd_args,
@ -31,6 +31,8 @@ end
---generate unit test for one function
---@param parallel boolean
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)))
if ns == nil or ns.name == nil then
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 requried = "Gopher.nvim will not work without it!"
local requried_for_work_msg = "Gopher.nvim will not work without it!"
local M = {
_required = {
plugins = {
{ lib = "plenary", help = requried },
{ lib = "nvim-treesitter", help = requried },
{ lib = "plenary", help = requried_for_work_msg },
{ lib = "nvim-treesitter", help = requried_for_work_msg },
{ lib = "dap", help = "Required for set upping debugger" },
},
binarys = {
@ -21,9 +19,12 @@ local M = {
}
function M.check()
local health = vim.health or require "health"
local u = require "gopher._utils._health"
health.report_start "Required plugins"
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.")
else
health.report_error(plugin.lib .. " not found. " .. plugin.help)
@ -32,7 +33,7 @@ function M.check()
health.report_start "Required go tools"
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")
else
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
---That's Lua of vimscript implementation of:
---github.com/koron/iferr
return function()
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
local boff = vim.fn.wordcount().cursor_bytes
local cmd = (c.iferr .. " -pos " .. boff)
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"
---@return string
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)))
if ns == nil then
u.notify("put cursor on a struct or specify a receiver", "info")
@ -20,6 +19,9 @@ local function get_struct()
end
return function(...)
local c = require("gopher.config").config.commands
local Job = require "plenary.job"
local args = { ... }
local iface, recv_name = "", ""
local recv = get_struct()

View file

@ -1,5 +1,3 @@
local Job = require "plenary.job"
local u = require "gopher._utils"
local urls = {
gomodifytags = "github.com/fatih/gomodifytags",
impl = "github.com/josharian/impl",
@ -10,6 +8,9 @@ local urls = {
---@param pkg string
local function install(pkg)
local Job = require "plenary.job"
local u = require "gopher._utils"
local url = urls[pkg] .. "@latest"
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 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 ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0)))
if ns == nil then