1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
require('core.snippets.shorthands')
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 {
-- 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({"", "}"}) })
})
})
}
|