Made snippets better
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
"harpoon": { "branch": "harpoon2", "commit": "a84ab829eaf3678b586609888ef52f7779102263" },
|
"harpoon": { "branch": "harpoon2", "commit": "a84ab829eaf3678b586609888ef52f7779102263" },
|
||||||
"hererocks": { "branch": "master", "commit": "c9c5444dea1e07e005484014a8231aa667be30b6" },
|
"hererocks": { "branch": "master", "commit": "c9c5444dea1e07e005484014a8231aa667be30b6" },
|
||||||
"lazy.nvim": { "branch": "main", "commit": "7527af40ddd4a93a02911be570b32609b9d4ea53" },
|
"lazy.nvim": { "branch": "main", "commit": "7527af40ddd4a93a02911be570b32609b9d4ea53" },
|
||||||
|
"lazydev.nvim": { "branch": "main", "commit": "a1b78b2ac6f978c72e76ea90ae92a94edf380cfc" },
|
||||||
"markdown-preview.nvim": { "branch": "master", "commit": "a923f5fc5ba36a3b17e289dc35dc17f66d0548ee" },
|
"markdown-preview.nvim": { "branch": "master", "commit": "a923f5fc5ba36a3b17e289dc35dc17f66d0548ee" },
|
||||||
"mason-lspconfig.nvim": { "branch": "main", "commit": "f75e877f5266e87523eb5a18fcde2081820d087b" },
|
"mason-lspconfig.nvim": { "branch": "main", "commit": "f75e877f5266e87523eb5a18fcde2081820d087b" },
|
||||||
"mason.nvim": { "branch": "main", "commit": "e2f7f9044ec30067bc11800a9e266664b88cda22" },
|
"mason.nvim": { "branch": "main", "commit": "e2f7f9044ec30067bc11800a9e266664b88cda22" },
|
||||||
|
|||||||
@@ -1,6 +1,15 @@
|
|||||||
return {
|
return {
|
||||||
"neovim/nvim-lspconfig",
|
"neovim/nvim-lspconfig",
|
||||||
dependencies = {
|
dependencies = {
|
||||||
|
{
|
||||||
|
"folke/lazydev.nvim",
|
||||||
|
ft = "lua",
|
||||||
|
opts = {
|
||||||
|
library = {
|
||||||
|
{ path = "luvit-meta/library", words = { "vim%.uv" } },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
"williamboman/mason.nvim",
|
"williamboman/mason.nvim",
|
||||||
"williamboman/mason-lspconfig.nvim",
|
"williamboman/mason-lspconfig.nvim",
|
||||||
"hrsh7th/cmp-nvim-lsp",
|
"hrsh7th/cmp-nvim-lsp",
|
||||||
@@ -25,6 +34,7 @@ return {
|
|||||||
require("fidget").setup({})
|
require("fidget").setup({})
|
||||||
require("mason").setup()
|
require("mason").setup()
|
||||||
require("mason-lspconfig").setup({
|
require("mason-lspconfig").setup({
|
||||||
|
automatic_installation = false,
|
||||||
ensure_installed = {
|
ensure_installed = {
|
||||||
"lua_ls",
|
"lua_ls",
|
||||||
"bashls",
|
"bashls",
|
||||||
@@ -103,7 +113,7 @@ return {
|
|||||||
focusable = false,
|
focusable = false,
|
||||||
style = "minimal",
|
style = "minimal",
|
||||||
border = "rounded",
|
border = "rounded",
|
||||||
source = "always",
|
source = true,
|
||||||
header = "",
|
header = "",
|
||||||
prefix = "",
|
prefix = "",
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -17,10 +17,13 @@ return {
|
|||||||
require("luasnip.loaders.from_" .. type).lazy_load()
|
require("luasnip.loaders.from_" .. type).lazy_load()
|
||||||
vim.keymap.set("n", "<leader>ts", "<cmd>Telescope luasnip<CR>", { desc = "Search Snippets" })
|
vim.keymap.set("n", "<leader>ts", "<cmd>Telescope luasnip<CR>", { desc = "Search Snippets" })
|
||||||
end, { "vscode", "snipmate", "lua" })
|
end, { "vscode", "snipmate", "lua" })
|
||||||
local lua_snippets = require("haelnorr.snippets.lua")
|
for _, ft_path in ipairs(vim.api.nvim_get_runtime_file("lua/haelnorr/snippets/*.lua", true)) do
|
||||||
ls.add_snippets("lua", lua_snippets)
|
loadfile(ft_path)()
|
||||||
local go_snippets = require("haelnorr.snippets.go")
|
end
|
||||||
ls.add_snippets("go", go_snippets)
|
-- local lua_snippets = require("haelnorr.snippets.lua")
|
||||||
|
-- ls.add_snippets("lua", lua_snippets)
|
||||||
|
-- local go_snippets = require("haelnorr.snippets.go")
|
||||||
|
-- ls.add_snippets("go", go_snippets)
|
||||||
vim.keymap.set({ "i", "s" }, "<C-k>", function()
|
vim.keymap.set({ "i", "s" }, "<C-k>", function()
|
||||||
if ls.expand_or_jumpable() then
|
if ls.expand_or_jumpable() then
|
||||||
ls.expand_or_jump()
|
ls.expand_or_jump()
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ return {
|
|||||||
},
|
},
|
||||||
lazy = false,
|
lazy = false,
|
||||||
config = function()
|
config = function()
|
||||||
require("refactoring").setup()
|
require("refactoring").setup({})
|
||||||
vim.keymap.set("x", "<leader>re", ":Refactor extract ", { desc = "Extract selection" })
|
vim.keymap.set("x", "<leader>re", ":Refactor extract ", { desc = "Extract selection" })
|
||||||
vim.keymap.set("x", "<leader>rf", ":Refactor extract_to_file ", { desc = "Extract selection to new file" })
|
vim.keymap.set("x", "<leader>rf", ":Refactor extract_to_file ", { desc = "Extract selection to new file" })
|
||||||
|
|
||||||
|
|||||||
@@ -1,46 +1,51 @@
|
|||||||
return {
|
return {
|
||||||
"nvim-treesitter/nvim-treesitter",
|
"nvim-treesitter/nvim-treesitter",
|
||||||
build = ":TSUpdate",
|
build = ":TSUpdate",
|
||||||
config = function()
|
config = function()
|
||||||
require("nvim-treesitter.configs").setup({
|
---@diagnostic disable-next-line: missing-fields
|
||||||
-- A list of parser names, or "all"
|
require("nvim-treesitter.configs").setup({
|
||||||
ensure_installed = {
|
-- A list of parser names, or "all"
|
||||||
"vimdoc", "javascript", "typescript", "c", "lua", "rust",
|
ensure_installed = {
|
||||||
"jsdoc", "bash",
|
"vimdoc",
|
||||||
},
|
"javascript",
|
||||||
|
"typescript",
|
||||||
|
"c",
|
||||||
|
"lua",
|
||||||
|
"go",
|
||||||
|
"bash",
|
||||||
|
"templ",
|
||||||
|
},
|
||||||
|
|
||||||
-- Install parsers synchronously (only applied to `ensure_installed`)
|
-- Install parsers synchronously (only applied to `ensure_installed`)
|
||||||
sync_install = false,
|
sync_install = false,
|
||||||
|
|
||||||
-- Automatically install missing parsers when entering buffer
|
-- Automatically install missing parsers when entering buffer
|
||||||
-- Recommendation: set to false if you don"t have `tree-sitter` CLI installed locally
|
-- Recommendation: set to false if you don"t have `tree-sitter` CLI installed locally
|
||||||
auto_install = true,
|
auto_install = true,
|
||||||
|
|
||||||
indent = {
|
indent = {
|
||||||
enable = true
|
enable = true,
|
||||||
},
|
},
|
||||||
|
|
||||||
highlight = {
|
highlight = {
|
||||||
-- `false` will disable the whole extension
|
-- `false` will disable the whole extension
|
||||||
enable = true,
|
enable = true,
|
||||||
|
|
||||||
-- Setting this to true will run `:h syntax` and tree-sitter at the same time.
|
-- Setting this to true will run `:h syntax` and tree-sitter at the same time.
|
||||||
-- Set this to `true` if you depend on "syntax" being enabled (like for indentation).
|
-- Set this to `true` if you depend on "syntax" being enabled (like for indentation).
|
||||||
-- Using this option may slow down your editor, and you may see some duplicate highlights.
|
-- Using this option may slow down your editor, and you may see some duplicate highlights.
|
||||||
-- Instead of true it can also be a list of languages
|
-- Instead of true it can also be a list of languages
|
||||||
additional_vim_regex_highlighting = { "markdown" },
|
additional_vim_regex_highlighting = { "markdown" },
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
local treesitter_parser_config = require("nvim-treesitter.parsers").get_parser_configs()
|
-- local treesitter_parser_config = require("nvim-treesitter.parsers").get_parser_configs()
|
||||||
treesitter_parser_config.templ = {
|
-- treesitter_parser_config.templ = {
|
||||||
install_info = {
|
-- install_info = {
|
||||||
url = "https://github.com/vrischmann/tree-sitter-templ.git",
|
-- url = "https://github.com/vrischmann/tree-sitter-templ.git",
|
||||||
files = {"src/parser.c", "src/scanner.c"},
|
-- files = { "src/parser.c", "src/scanner.c" },
|
||||||
branch = "master",
|
-- branch = "master",
|
||||||
},
|
-- },
|
||||||
}
|
-- }
|
||||||
|
end,
|
||||||
end
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,13 @@ return {
|
|||||||
|
|
||||||
{ "<leader><leader>", group = "Reload Plugins", icon = { icon = "" } },
|
{ "<leader><leader>", group = "Reload Plugins", icon = { icon = "" } },
|
||||||
{ "<leader><leader>w", "<cmd>Lazy reload which-key.nvim<CR>", desc = "Reload Which-key" },
|
{ "<leader><leader>w", "<cmd>Lazy reload which-key.nvim<CR>", desc = "Reload Which-key" },
|
||||||
{ "<leader><leader>s", "<cmd>Lazy reload LuaSnip<CR>", desc = "Reload LuaSnip" },
|
{
|
||||||
|
"<leader><leader>s",
|
||||||
|
"<cmd>source ~/.config/nvim/lua/haelnorr/snippets/lua.lua<CR>\
|
||||||
|
<cmd>source ~/.config/nvim/lua/haelnorr/snippets/go.lua<CR>\
|
||||||
|
<cmd>Lazy reload LuaSnip<CR>",
|
||||||
|
desc = "Reload LuaSnip",
|
||||||
|
},
|
||||||
{ "<leader><leader>l", "<cmd>Lazy reload nvim-lspconfig<CR>", desc = "Reload LSP" },
|
{ "<leader><leader>l", "<cmd>Lazy reload nvim-lspconfig<CR>", desc = "Reload LSP" },
|
||||||
|
|
||||||
-- Harpoon
|
-- Harpoon
|
||||||
|
|||||||
@@ -1,31 +1,149 @@
|
|||||||
|
require("luasnip.session.snippet_collection").clear_snippets("go")
|
||||||
local ls = require("luasnip")
|
local ls = require("luasnip")
|
||||||
local s = ls.snippet
|
local s = ls.snippet
|
||||||
local t = ls.text_node
|
|
||||||
local i = ls.insert_node
|
|
||||||
local c = ls.choice_node
|
local c = ls.choice_node
|
||||||
local f = ls.function_node
|
local d = ls.dynamic_node
|
||||||
local fmt = require("luasnip.extras.fmt").fmt
|
local i = ls.insert_node
|
||||||
return {
|
local t = ls.text_node
|
||||||
|
local sn = ls.snippet_node
|
||||||
|
local rep = require("luasnip.extras").rep
|
||||||
|
local fmta = require("luasnip.extras.fmt").fmta
|
||||||
|
|
||||||
|
local default_values = {
|
||||||
|
int = "0",
|
||||||
|
bool = "false",
|
||||||
|
string = '""',
|
||||||
|
|
||||||
|
error = function(_, info)
|
||||||
|
if info then
|
||||||
|
info.index = info.index + 1
|
||||||
|
|
||||||
|
return c(info.index, {
|
||||||
|
t(info.err_name),
|
||||||
|
t(string.format('errors.Wrap(%s, "%s")', info.err_name, info.func_name)),
|
||||||
|
})
|
||||||
|
else
|
||||||
|
return t("err")
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
|
||||||
|
[function(text)
|
||||||
|
return string.find(text, "*", 1, true) ~= nil
|
||||||
|
end] = function(_, _)
|
||||||
|
return t("nil")
|
||||||
|
end,
|
||||||
|
|
||||||
|
[function(text)
|
||||||
|
return not string.find(text, "*", 1, true) and string.upper(string.sub(text, 1, 1)) == string.sub(text, 1, 1)
|
||||||
|
end] = function(text, info)
|
||||||
|
info.index = info.index + 1
|
||||||
|
return c(info.index, {
|
||||||
|
t(text .. "{}"),
|
||||||
|
t(text),
|
||||||
|
})
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
|
local transform = function(text, info)
|
||||||
|
local condition_matches = function(condition, ...)
|
||||||
|
if type(condition) == "string" then
|
||||||
|
return condition == text
|
||||||
|
else
|
||||||
|
return condition(...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
for condition, result in pairs(default_values) do
|
||||||
|
if condition_matches(condition, text, info) then
|
||||||
|
if type(result) == "string" then
|
||||||
|
return t(result)
|
||||||
|
else
|
||||||
|
return result(text, info)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return t(text)
|
||||||
|
end
|
||||||
|
|
||||||
|
local handlers = {
|
||||||
|
parameter_list = function(node, info)
|
||||||
|
local result = {}
|
||||||
|
local count = node:named_child_count()
|
||||||
|
for idx = 0, count - 1 do
|
||||||
|
local matching_node = node:named_child(idx)
|
||||||
|
local type_node = matching_node:field("type")[1]
|
||||||
|
table.insert(result, transform(vim.treesitter.get_node_text(type_node, 0), info))
|
||||||
|
if idx ~= count - 1 then
|
||||||
|
table.insert(result, t({ ", " }))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end,
|
||||||
|
|
||||||
|
type_indentifier = function(node, info)
|
||||||
|
local text = vim.treesitter.get_node_text(node, 0)
|
||||||
|
return { transform(text, info) }
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
|
local function go_result_type(info)
|
||||||
|
local function_node_types = {
|
||||||
|
function_declaration = true,
|
||||||
|
method_declaration = true,
|
||||||
|
func_literal = true,
|
||||||
|
}
|
||||||
|
|
||||||
|
local node = vim.treesitter.get_node()
|
||||||
|
while node ~= nil do
|
||||||
|
if function_node_types[node:type()] then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
node = node:parent()
|
||||||
|
end
|
||||||
|
if not node then
|
||||||
|
vim.notify("Not inside of a function")
|
||||||
|
return (t(""))
|
||||||
|
end
|
||||||
|
|
||||||
|
local query = assert(vim.treesitter.query.get("go", "return-snippet"), "No query")
|
||||||
|
for _, capture in query:iter_captures(node, 0) do
|
||||||
|
if handlers[capture:type()] then
|
||||||
|
return handlers[capture:type()](capture, info)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local go_return_values = function(args)
|
||||||
|
return sn(
|
||||||
|
nil,
|
||||||
|
go_result_type({
|
||||||
|
index = 0,
|
||||||
|
err_name = args[1][1],
|
||||||
|
func_name = args[2][1],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
ls.add_snippets("go", {
|
||||||
s(
|
s(
|
||||||
"efi",
|
"efi",
|
||||||
fmt(
|
fmta(
|
||||||
[[
|
[[
|
||||||
{}, err := {}({})
|
<val>, <err> := <f>(<args>)
|
||||||
if err != nil {{
|
if <err_same> != nil {
|
||||||
return "", false, {}
|
return <result>
|
||||||
}}
|
}
|
||||||
]],
|
<finish>
|
||||||
|
]],
|
||||||
{
|
{
|
||||||
i(1, "var"),
|
val = i(1),
|
||||||
i(2, "f"),
|
err = i(2, "err"),
|
||||||
i(3, ""),
|
f = i(3),
|
||||||
c(4, {
|
args = i(4),
|
||||||
f(function(args)
|
err_same = rep(2),
|
||||||
return 'errors.Wrap(err, "' .. args[1][1] .. '")'
|
result = d(5, go_return_values, { 2, 3 }),
|
||||||
end, { 2 }),
|
finish = i(0),
|
||||||
t("err"),
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
}
|
})
|
||||||
|
|||||||
@@ -1,14 +1,41 @@
|
|||||||
|
require("luasnip.session.snippet_collection").clear_snippets("lua")
|
||||||
local ls = require("luasnip")
|
local ls = require("luasnip")
|
||||||
local s = ls.snippet
|
local s = ls.snippet
|
||||||
|
local c = ls.choice_node
|
||||||
local i = ls.insert_node
|
local i = ls.insert_node
|
||||||
local fmt = require("luasnip.extras.fmt").fmt
|
local fmta = require("luasnip.extras.fmt").fmta
|
||||||
return {
|
ls.add_snippets("lua", {
|
||||||
s(
|
s(
|
||||||
"snip",
|
"snip",
|
||||||
fmt('s("{}", fmt(\n "{}"\n, {{\n {}\n}})),', {
|
fmta(
|
||||||
i(1, "trigger"),
|
[=[
|
||||||
i(2, "snippet body"),
|
s("<trigger>",
|
||||||
i(3, "insert nodes"),
|
fmta(
|
||||||
})
|
[[
|
||||||
|
<snippet>
|
||||||
|
]],
|
||||||
|
{
|
||||||
|
<node_choice>
|
||||||
|
}
|
||||||
|
)
|
||||||
|
),<finish>
|
||||||
|
]=],
|
||||||
|
{
|
||||||
|
trigger = i(1),
|
||||||
|
snippet = i(2),
|
||||||
|
node_choice = c(3, {
|
||||||
|
fmta([[<node> = <node_type>(1, "<node_placeholder>"),]], {
|
||||||
|
node = i(1),
|
||||||
|
node_type = i(2, "type"),
|
||||||
|
node_placeholder = i(3, "placeholder"),
|
||||||
|
}),
|
||||||
|
fmta("<node> = <node_type>(1)", {
|
||||||
|
node = i(1),
|
||||||
|
node_type = i(2, "type"),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
finish = i(0),
|
||||||
|
}
|
||||||
|
)
|
||||||
),
|
),
|
||||||
}
|
})
|
||||||
|
|||||||
3
queries/go/return-snippet.scm
Normal file
3
queries/go/return-snippet.scm
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[(method_declaration result: (_) @type)
|
||||||
|
(function_declaration result: (_) @type)
|
||||||
|
(func_literal result: (_) @type)]
|
||||||
Reference in New Issue
Block a user