revise java snippets
This commit is contained in:
@ -1,92 +1,137 @@
|
|||||||
require('core.snippets.shorthands')
|
require('core.snippets.shorthands')
|
||||||
require('core.snippets.functions')
|
require('core.snippets.functions')
|
||||||
|
|
||||||
|
--- shortcut to choose between java access modifiers
|
||||||
|
---@param idx number index of the node
|
||||||
|
---@return table choice_node
|
||||||
|
local function access_modifiers(idx)
|
||||||
|
return c(idx, {
|
||||||
|
t("public "),
|
||||||
|
t("private "),
|
||||||
|
t("protected "),
|
||||||
|
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 {
|
return {
|
||||||
-- method snippet
|
-- method snippet
|
||||||
s("fn", {
|
s({ trig = [[fn\|constr]], trigEngine = "vim" }, {
|
||||||
c(1, {
|
access_modifiers(1),
|
||||||
t("public "),
|
modifiers(2),
|
||||||
t("private "),
|
d(3, function(_, snip)
|
||||||
}),
|
if snip.trigger == "constr" then
|
||||||
c(2, {
|
return sn(nil, {
|
||||||
t("void"),
|
f(java_class, {})
|
||||||
t("String"),
|
})
|
||||||
t("char"),
|
else
|
||||||
t("int"),
|
return sn(nil, {
|
||||||
t("double"),
|
c(1, {
|
||||||
t("boolean"),
|
t("void"),
|
||||||
i(nil, "myType"),
|
t("int"),
|
||||||
}),
|
t("double"),
|
||||||
t(" "),
|
t("boolean"),
|
||||||
i(3, "myFunc"),
|
t("String"),
|
||||||
|
t("char"),
|
||||||
|
i(nil, "myType"),
|
||||||
|
}),
|
||||||
|
t(" "),
|
||||||
|
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") }),
|
||||||
}),
|
}),
|
||||||
t({ " {", "\t" }),
|
d(6, function(args)
|
||||||
i(0),
|
if args[1][1] == "abstract " then
|
||||||
t({ "", "}" }),
|
-- 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" }),
|
||||||
|
i(0),
|
||||||
|
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
|
||||||
|
Reference in New Issue
Block a user