lazy.nvim based lazy loading

pull/285/head
ray-x 1 year ago
parent 87263fbeff
commit 470349cff5

@ -151,46 +151,52 @@ function go.setup(cfg)
if _GO_NVIM_CFG.run_in_floaterm then
vim.cmd([[command! -nargs=* GoTermClose lua require("go.term").close()]])
end
vim.defer_fn(function()
require('go.utils').set_nulls()
require('go.utils').set_nulls()
if _GO_NVIM_CFG.lsp_cfg then
require('go.lsp').setup()
if _GO_NVIM_CFG.lsp_diag_hdlr then
require('go.lsp_diag')
if _GO_NVIM_CFG.lsp_cfg then
require('go.lsp').setup()
if _GO_NVIM_CFG.lsp_diag_hdlr then
require('go.lsp_diag')
end
elseif not _GO_NVIM_CFG.lsp_cfg and _GO_NVIM_CFG.lsp_on_attach then
vim.notify('lsp_on_attach ignored, because lsp_cfg is false', vim.lsp.log_levels.WARN)
end
require('go.coverage').setup()
if _GO_NVIM_CFG.lsp_codelens then
require('go.codelens').setup()
end
elseif not _GO_NVIM_CFG.lsp_cfg and _GO_NVIM_CFG.lsp_on_attach then
vim.notify('lsp_on_attach ignored, because lsp_cfg is false', vim.lsp.log_levels.WARN)
end
require('go.coverage').setup()
if _GO_NVIM_CFG.lsp_codelens then
require('go.codelens').setup()
end
if _GO_NVIM_CFG.textobjects then
require('go.ts.textobjects').setup()
end
if _GO_NVIM_CFG.textobjects then
require('go.ts.textobjects').setup()
end
require('go.env').setup()
require('go.env').setup()
end, 4)
if _GO_NVIM_CFG.luasnip then
local ls = require('go.utils').load_plugin('LuaSnip', 'luasnip')
if ls then
require('snips.go')
require('snips.all')
end
vim.defer_fn(function()
local ls = require('go.utils').load_plugin('LuaSnip', 'luasnip')
if ls then
require('snips.go')
require('snips.all')
end
end, 10)
end
if _GO_NVIM_CFG.lsp_inlay_hints.enable then
require('go.inlay').setup()
end
go.doc_complete = require('go.godoc').doc_complete
go.package_complete = require('go.package').complete
go.dbg_complete = require('go.complete').dbg_complete
go.tools_complete = require('go.complete').tools_complete
go.impl_complete = require('go.complete').impl_complete
go.modify_tags_complete = require('go.complete').modify_tags_complete
go.add_tags_complete = require('go.complete').add_tags_complete
require('go.mod').setup()
vim.defer_fn(function()
go.doc_complete = require('go.godoc').doc_complete
go.package_complete = require('go.package').complete
go.dbg_complete = require('go.complete').dbg_complete
go.tools_complete = require('go.complete').tools_complete
go.impl_complete = require('go.complete').impl_complete
go.modify_tags_complete = require('go.complete').modify_tags_complete
go.add_tags_complete = require('go.complete').add_tags_complete
require('go.mod').setup()
end, 10)
-- make sure TS installed
end

@ -65,9 +65,6 @@ local dap_config = function()
require('go.launch').config()
end)
create_cmd('GoCreateLaunch', function(_)
require('go.launch').config()
end)
create_cmd('GoBreakSave', function(_)
gdap.save_brks()
end)

@ -24,7 +24,9 @@ function M.setup()
group = cmd_group,
pattern = { '*.go', '*.mod' },
callback = function()
require('go.inlay').set_inlay_hints()
if not vim.wo.diff then
require('go.inlay').set_inlay_hints()
end
end,
})
@ -39,7 +41,8 @@ end
local function get_params()
local start_pos = api.nvim_buf_get_mark(0, '<')
local end_pos = api.nvim_buf_get_mark(0, '>')
local params = { range = { start = { character = 0, line = 0 }, ['end'] = { character = 0, line = 0 } } }
local params =
{ range = { start = { character = 0, line = 0 }, ['end'] = { character = 0, line = 0 } } }
local len = vim.api.nvim_buf_line_count(0)
if end_pos[1] <= len then
params = vim.lsp.util.make_given_range_params()
@ -229,7 +232,8 @@ local function handler(err, result, ctx)
if config.max_len_align then
local max_len = get_max_len(bufnr, parsed)
virt_text = string.rep(' ', max_len - current_line_len + config.max_len_align_padding) .. virt_text
virt_text = string.rep(' ', max_len - current_line_len + config.max_len_align_padding)
.. virt_text
end
-- set the virtual text if it is not empty
@ -279,6 +283,9 @@ function M.set_inlay_hints()
if not found then
return
end
if vim.wo.diff then
return
end
vim.defer_fn(function()
vim.lsp.buf_request(bufnr, 'textDocument/inlayHint', get_params(), handler)
end, 500)

@ -1,21 +1,31 @@
-- first version: from https://github.com/arsham/shark
local ls = require("luasnip")
local fmt = require("luasnip.extras.fmt").fmt
local ok, ts_utils = pcall(require, "nvim-treesitter.ts_utils")
local ls = require('luasnip')
local fmt = require('luasnip.extras.fmt').fmt
local ok, ts_utils = pcall(require, 'nvim-treesitter.ts_utils')
if not ok then
require("packer").loader("nvim-treesitter")
ts_utils = require("nvim-treesitter.ts_utils")
local ok, packer = pcall(require, 'packer')
if ok then
require('packer').loader('nvim-treesitter')
else
local ok, lazy = pcall(require, 'lazy')
if lazy then
lazy.load({ plugins = { 'nvim-treesitter' } })
else
error('Please install nvim-treesitter')
end
end
ts_utils = require('nvim-treesitter.ts_utils')
end
local ts_locals = require("nvim-treesitter.locals")
local rep = require("luasnip.extras").rep
local ai = require("luasnip.nodes.absolute_indexer")
local ts_locals = require('nvim-treesitter.locals')
local rep = require('luasnip.extras').rep
local ai = require('luasnip.nodes.absolute_indexer')
local M = {}
M.go_err_snippet = function(args, _, _, spec)
local err_name = args[1][1]
local index = spec and spec.index or nil
local msg = spec and spec[1] or ""
local msg = spec and spec[1] or ''
if spec and spec[2] then
err_name = err_name .. spec[2]
end
@ -30,10 +40,10 @@ M.go_err_snippet = function(args, _, _, spec)
nil,
fmt('internal.GrpcError({},\n\t\tcodes.{}, "{}", "{}", {})', {
ls.t(err_name),
ls.i(1, "Internal"),
ls.i(2, "Description"),
ls.i(3, "Field"),
ls.i(4, "fields"),
ls.i(1, 'Internal'),
ls.i(2, 'Description'),
ls.i(3, 'Field'),
ls.i(4, 'fields'),
})
),
ls.t(err_name),
@ -48,18 +58,18 @@ local function transform(text, info)
return ls.sn(info.index, fmt(template, ls.i(1, default)))
end
local new_sn = function(default)
return string_sn("{}", default)
return string_sn('{}', default)
end
-- cutting the name if exists.
if text:find([[^[^\[]*string$]]) then
text = "string"
elseif text:find("^[^%[]*map%[[^%]]+") then
text = "map"
elseif text:find("%[%]") then
text = "slice"
text = 'string'
elseif text:find('^[^%[]*map%[[^%]]+') then
text = 'map'
elseif text:find('%[%]') then
text = 'slice'
elseif text:find([[ ?chan +[%a%d]+]]) then
return ls.t("nil")
return ls.t('nil')
end
-- separating the type from the name if exists.
@ -68,34 +78,34 @@ local function transform(text, info)
text = type
end
if text == "int" or text == "int64" or text == "int32" then
return new_sn("0")
elseif text == "float32" or text == "float64" then
return new_sn("0")
elseif text == "error" then
if text == 'int' or text == 'int64' or text == 'int32' then
return new_sn('0')
elseif text == 'float32' or text == 'float64' then
return new_sn('0')
elseif text == 'error' then
if not info then
return ls.t("err")
return ls.t('err')
end
info.index = info.index + 1
return M.go_err_snippet({ { info.err_name } }, nil, nil, { index = info.index })
elseif text == "bool" then
elseif text == 'bool' then
info.index = info.index + 1
return ls.c(info.index, { ls.i(1, "false"), ls.i(2, "true") })
elseif text == "string" then
return string_sn('"{}"', "")
elseif text == "map" or text == "slice" then
return ls.t("nil")
elseif string.find(text, "*", 1, true) then
return new_sn("nil")
return ls.c(info.index, { ls.i(1, 'false'), ls.i(2, 'true') })
elseif text == 'string' then
return string_sn('"{}"', '')
elseif text == 'map' or text == 'slice' then
return ls.t('nil')
elseif string.find(text, '*', 1, true) then
return new_sn('nil')
end
text = text:match("[^ ]+$")
if text == "context.Context" then
text = "context.Background()"
text = text:match('[^ ]+$')
if text == 'context.Context' then
text = 'context.Background()'
else
-- when the type is concrete
text = text .. "{}"
text = text .. '{}'
end
return ls.t(text)
@ -110,7 +120,7 @@ local handlers = {
for idx = 0, count - 1 do
table.insert(result, transform(get_node_text(node:named_child(idx), 0), info))
if idx ~= count - 1 then
table.insert(result, ls.t({ ", " }))
table.insert(result, ls.t({ ', ' }))
end
end
@ -131,8 +141,8 @@ local function set_query()
end
query_is_set = true
vim.treesitter.set_query(
"go",
"LuaSnip_Result",
'go',
'LuaSnip_Result',
[[
[
(method_declaration result: (_) @id)
@ -151,9 +161,9 @@ local function return_value_nodes(info)
local function_node
for _, scope in ipairs(scope_tree) do
if
scope:type() == "function_declaration"
or scope:type() == "method_declaration"
or scope:type() == "func_literal"
scope:type() == 'function_declaration'
or scope:type() == 'method_declaration'
or scope:type() == 'func_literal'
then
function_node = scope
break
@ -164,16 +174,16 @@ local function return_value_nodes(info)
return
end
local query = vim.treesitter.get_query("go", "LuaSnip_Result")
local query = vim.treesitter.get_query('go', 'LuaSnip_Result')
for _, node in query:iter_captures(function_node, 0) do
if handlers[node:type()] then
return handlers[node:type()](node, info)
end
end
return ls.t({ "" })
return ls.t({ '' })
end
local is_in_function = require("go.ts.go").in_func()
local is_in_function = require('go.ts.go').in_func()
---Transforms the given arguments into nodes wrapped in a snippet node.
M.make_return_nodes = function(args)
@ -182,29 +192,28 @@ M.make_return_nodes = function(args)
return ls.sn(nil, return_value_nodes(info))
end
M.make_default_return_nodes = function()
local info = { index = 0, err_name = "nil" }
local info = { index = 0, err_name = 'nil' }
return ls.sn(nil, return_value_nodes(info))
end
M.fill_return = function()
local info = { index = 0, err_name = 'nil'}
local info = { index = 0, err_name = 'nil' }
return ls.sn(nil, return_value_nodes(info))
end
---Runs the command in shell.
-- @param command string
-- @return table
M.shell = require("go.utils").run_command
M.shell = require('go.utils').run_command
M.last_lua_module_section = function(args)
local text = args[1][1] or ""
local split = vim.split(text, ".", { plain = true })
local text = args[1][1] or ''
local split = vim.split(text, '.', { plain = true })
local options = {}
for len = 0, #split - 1 do
local node = ls.t(table.concat(vim.list_slice(split, #split - len, #split), "_"))
local node = ls.t(table.concat(vim.list_slice(split, #split - len, #split), '_'))
table.insert(options, node)
end
@ -214,8 +223,8 @@ M.last_lua_module_section = function(args)
end
function M.is_in_test_file()
local filename = vim.fn.expand("%:p")
return vim.endswith(filename, "_test.go")
local filename = vim.fn.expand('%:p')
return vim.endswith(filename, '_test.go')
end
function M.is_in_test_function()
@ -225,11 +234,11 @@ end
M.create_t_run = function(args)
return ls.sn(1, {
ls.c(1, {
ls.t({ "" }),
ls.t({ '' }),
ls.sn(
nil,
fmt('\tt.Run("{}", {}{})\n{}', {
ls.i(1, "Case"),
ls.i(1, 'Case'),
ls.t(args[1]),
rep(1),
ls.d(2, M.create_t_run, ai[1]),
@ -244,13 +253,13 @@ M.mirror_t_run_funcs = function(args)
for _, v in ipairs(args[1]) do
local name = v:match('^%s*t%.Run%s*%(%s*".*", (.*)%)')
if name then
local node = string.format("func %s(t *testing.T) {{\n\tt.Parallel()\n}}\n\n", name)
local node = string.format('func %s(t *testing.T) {{\n\tt.Parallel()\n}}\n\n', name)
table.insert(strs, node)
end
end
local str = table.concat(strs, "")
local str = table.concat(strs, '')
if #str == 0 then
return ls.sn(1, ls.t(""))
return ls.sn(1, ls.t(''))
end
return ls.sn(1, fmt(str, {}))
end

@ -363,14 +363,13 @@ function util.load_plugin(name, modulename)
if has then
return plugin
end
local pkg = packer_plugins or nil
if pkg ~= nil then
local pkg = packer_plugins
local has_packer = pcall(require, 'packer')
local has_lazy = pcall(require, 'lazy')
if has_packer or has_lazy then
-- packer installed
local has_packer = pcall(require, 'packer')
if not has_packer then
error('packer not found')
return nil
end
if has_packer then
local loader = require('packer').loader
if not pkg[name] or not pkg[name].loaded then
util.log('packer loader ' .. name)
@ -379,6 +378,9 @@ function util.load_plugin(name, modulename)
loader(name)
end
end
else
require('lazy').load({plugins =name})
end
else
util.log('packadd ' .. name)
local paths = vim.o.runtimepath

Loading…
Cancel
Save