feat(nvim): improve pythondef
snippet
This commit is contained in:
parent
3a11699976
commit
050354d11a
@ -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 = {}
|
||||
|
45
dots/.config/nvim/lua/plugins/snippets/python.lua
Normal file
45
dots/.config/nvim/lua/plugins/snippets/python.lua
Normal 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"),
|
||||
}),
|
||||
}
|
Loading…
Reference in New Issue
Block a user