revise java snippets

This commit is contained in:
2024-11-28 22:33:06 -05:00
parent ea7ec0f890
commit a1fc008355

View File

@ -1,92 +1,137 @@
require('core.snippets.shorthands') require('core.snippets.shorthands')
require('core.snippets.functions') require('core.snippets.functions')
return { --- shortcut to choose between java access modifiers
-- method snippet ---@param idx number index of the node
s("fn", { ---@return table choice_node
c(1, { local function access_modifiers(idx)
return c(idx, {
t("public "), t("public "),
t("private "), t("private "),
}), t("protected "),
c(2, { t("default "),
})
end
--- shortcut to choose between java modifiers
---@param idx number index of the node
---@return table choice_node
local function modifiers(idx)
return c(idx, {
t(""),
t("abstract "),
t("final ")
})
end
--- find the current class and return its name if not available defaults to
--- the file name
---@return string
local function java_class()
-- set the starting node to the node under the cursor
local node = require("nvim-treesitter.ts_utils").get_node_at_cursor()
while node do
-- check if we're in a class
if node:type() == "class_declaration" then
-- find the class name in the class declaration and return it
for i = 0, node:child_count() - 1 do
local child = node:child(i)
if child and child:type() == "identifier" then
return vim.treesitter.get_node_text(child, 0)
end
end
end
node = node:parent()
end
-- if no class can be found default to the current file name
return file_name()
end
return {
-- method snippet
s({ trig = [[fn\|constr]], trigEngine = "vim" }, {
access_modifiers(1),
modifiers(2),
d(3, function(_, snip)
if snip.trigger == "constr" then
return sn(nil, {
f(java_class, {})
})
else
return sn(nil, {
c(1, {
t("void"), t("void"),
t("String"),
t("char"),
t("int"), t("int"),
t("double"), t("double"),
t("boolean"), t("boolean"),
t("String"),
t("char"),
i(nil, "myType"), i(nil, "myType"),
}), }),
t(" "), t(" "),
i(3, "myFunc"), i(2, "myFunc"),
})
end
end, {}),
t("("), t("("),
i(4), i(4),
t(")"), t(")"),
c(5, { c(5, {
t(""), t(""),
sn(nil, {t({ "", " throws " }), i(1)}), sn(nil, { t({ "", " throws " }), i(1, "Error") }),
}), }),
d(6, function(args)
if args[1][1] == "abstract " then
-- if the function is abstract do not include a body
return sn(nil, {
t(";")
})
else
-- if the function isn't abstract include a body
return sn(nil, {
t({ " {", "\t" }), t({ " {", "\t" }),
i(0), i(0),
t({ "", "}" }), t({ "", "}" })
})
end
end, { 2 })
}), }),
s("main", { s("main", {
t({ "public static void main(String[] args) {", "\t" }), t({ "public static void main(String[] args) {", "\t" }),
i(0), i(0),
t({ "", "}" }), t({ "", "}" })
}),
-- constructor snippet
s("constr", {
c(1, {
t("public "),
t("private "),
t("protected ")
}),
f(file_name, {}),
t("("),
i(2),
t(")"),
c(3, {
t(""),
sn(nil, {t({ "", " throws " }), i(1)}),
}),
t({ " {", "\t" }),
i(0),
t({ "", "}" }),
}), }),
-- class snippet -- class snippet
s("class", { s("class", {
c(1, { access_modifiers(1),
t("public "), modifiers(2),
t("private "), c(3, {
t("protected "),
t("")
}),
c(2, {
t("class "), t("class "),
t("interface ") t("interface ")
}), }),
c(3, {
i(0, "MyClass"),
f(file_name, {})
}),
c(4, { c(4, {
f(file_name, {}),
i(0, "MyClass")
}),
c(5, {
t(" "), t(" "),
sn(nil, { t({" implements "}), i(1), t(" ") }), sn(nil, { t({" implements "}), i(1), t(" ") }),
sn(nil, { t({" extends "}), i(1), t(" ") }), sn(nil, { t({" extends "}), i(1), t(" ") }),
}), }),
t({ "{", "\t" }), t({ "{", "\t" }),
i(0), i(0),
t({ "", "}" }), t({ "", "}" })
}), }),
-- pacakge snippet -- pacakage snippet
s("package", { s("package", {
t("package "), t("package "),
f(function(args, parent, user_args) f(function()
-- get path -- get path
local dir = vim.fn.expand("%:h") local dir = vim.fn.expand("%:h")
-- remove prefix -- remove prefix