feat(nvim): improve pythondef snippet

This commit is contained in:
Price Hiller 2023-09-16 17:52:49 -05:00
parent 3a11699976
commit 050354d11a
No known key found for this signature in database
2 changed files with 54 additions and 6 deletions

View File

@ -46,6 +46,9 @@ return {
-- Load Snippets
require("luasnip.loaders.from_vscode").lazy_load()
require("luasnip.loaders.from_lua").load({
paths = vim.fn.stdpath("config") .. "/lua/plugins/snippets"
})
local colors_bg_color = vim.api.nvim_get_hl(0, { name = "CmpCustomSelectionColor" }).bg
local cached_colors = {}
@ -107,8 +110,8 @@ return {
local standard_sources = function(sources)
sources = sources or {}
local default_sources = {
{ name = "nvim_lsp", priority = 11 },
{ name = "luasnip", priority = 10 }, -- For luasnip users.
{ name = "nvim_lsp", priority = 11 },
{ name = "luasnip", priority = 10 }, -- For luasnip users.
{ name = "fuzzy_buffer", priority = 9, keyword_length = 3, max_item_count = 10 },
{
name = "rg",
@ -117,12 +120,12 @@ return {
max_item_count = 10,
},
{ name = "async_path", priority = 6 },
{ name = "zsh", priority = 5 },
{ name = "emoji", keyword_length = 2 },
{ name = "zsh", priority = 5 },
{ name = "emoji", keyword_length = 2 },
{ name = "neorg" },
{ name = "calc" },
{ name = "npm", keyword_length = 2 },
{ name = "spell", keyword_length = 2 },
{ name = "npm", keyword_length = 2 },
{ name = "spell", keyword_length = 2 },
}
return vim.tbl_deep_extend("force", default_sources, sources)

View File

@ -0,0 +1,45 @@
-- Thanks u/1linguini1
return {
s({ trig = "main", name = "Main gaurd.", dscr = "Python __main__ gaurd." }, {
t({ 'if __name__ == "__main__":', "\t" }),
c(1, {
t(""),
t("main()"),
}),
}),
s({ trig = "def", dscr = "Create a function/method definition with standard parameters." }, {
t({ "def " }),
i(1, "fn_name"),
d(2, function(_)
-- Immediate parent must be a class definition (first method created in a class will have the current node
-- as a class definition)
local in_class = false
local node = vim.treesitter.get_node()
if node then
in_class = node:type() == "class_definition" or node:parent():type() == "class_definition"
end
local pos = vim.api.nvim_win_get_cursor(0)
local decorator = vim.api.nvim_buf_get_lines(0, pos[1] - 3, pos[1] - 2, false)
-- Decide default arguments based on nodes
local arguments = "("
if in_class then
if string.find(decorator[1], "classmethod") then
arguments = arguments .. "cls, "
elseif not string.find(decorator[1], "staticmethod") then
arguments = arguments .. "self, "
end
end
return sn(nil, {
t({ arguments }),
i(1, ""),
t({ ") -> " }),
})
end),
i(3, "None"),
t({ ":", "\t" }),
i(0, "pass"),
}),
}