Files
nvim/lua/snippets/java.lua
2025-05-31 03:32:58 -04:00

169 lines
3.7 KiB
Lua

dofile(core.snippets)
--- 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 {
-- 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("int"),
t("double"),
t("boolean"),
t("String"),
t("char"),
i(nil, "myType"),
}),
t(" "),
i(2, "myFunc"),
})
end
end, {}),
t("("),
i(4),
t(")"),
c(5, {
t(""),
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" }),
i(1),
t({ "", "}" })
})
end
end, { 2 })
}),
s("main", {
t({ "public static void main(String[] args) {", "\t" }),
i(0),
t({ "", "}" })
}),
-- class snippet
s({ trig = [[class\|interface]], trigEngine = "vim" }, {
access_modifiers(1),
modifiers(2),
d(3, function(_, snip)
local opts = { t("class "), t("interface ") }
if snip.trigger == "interface" then
-- flip interface and class
opts[1], opts[2] = opts[2], opts[1]
end
return sn(nil, {
c(1, opts)
})
end, {}),
c(4, {
f(file_name, {}),
i(0, "MyClass")
}),
c(5, {
t(" "),
sn(nil, { t({" implements "}), i(1), t(" ") }),
sn(nil, { t({" extends "}), i(1), t(" ") }),
}),
t({ "{", "\t" }),
i(0),
t({ "", "}" })
}),
-- pacakage snippet
s("package", {
t("package "),
f(function()
-- get path
local dir = vim.fn.expand("%:h")
-- remove prefix
dir = dir:gsub("src/main/java/", "")
-- convert to package path
dir = dir:gsub("/", ".")
return dir
end, {}),
t(";"),
t({ "", "", "" })
}),
-- try, catch, finally snippet
s("try", {
t({"try {", "\t"}),
i(1),
t({"", "} catch ("}),
i(2, "Error e"),
t({") {", "\t"}),
i(3),
t({"", "}"}),
c(4, {
t(""),
sn(nil, { t({" finally {", "\t"}), i(1), t({"", "}"}) })
})
})
}