-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjava-deps.lua
More file actions
456 lines (404 loc) · 11.4 KB
/
java-deps.lua
File metadata and controls
456 lines (404 loc) · 11.4 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
local context = require("java-deps.context")
local parser = require("java-deps.parser")
local providers = require("java-deps.providers.init")
local lsp_command = require("java-deps.lsp-command")
local ui = require("java-deps.ui")
local writer = require("java-deps.writer")
local config = require("java-deps.config")
local utils = require("java-deps.utils.init")
local View = require("java-deps.view")
local folding = require("java-deps.folding")
local node_kind = require("java-deps.symbols").NodeKind
local t_utils = require("java-deps.utils.table")
local M = {
view = nil,
-------------------------
-- STATE
-------------------------
state = {
preview_buf = nil,
preview_win = nil,
hover_buf = nil,
hover_win = nil,
flattened_outline_items = {},
code_buf = nil,
code_win = nil,
root_items = nil,
current_node = nil,
},
}
local function setup_global_autocmd()
if config.options.highlight_hovered_item or config.options.auto_unfold_hover then
vim.api.nvim_create_autocmd("CursorHold", {
pattern = "*",
callback = function()
M._highlight_current_item(nil)
end,
})
end
vim.api.nvim_create_autocmd("WinEnter", {
pattern = "*",
callback = require("java-deps.preview").close,
})
end
local function setup_buffer_autocmd()
if config.options.auto_preview then
vim.api.nvim_create_autocmd("CursorHold", {
buffer = 0,
callback = require("java-deps.preview").show,
})
else
vim.api.nvim_create_autocmd("CursorMoved", {
buffer = 0,
callback = require("java-deps.preview").close,
})
end
end
local function wipe_state()
M.state = {
preview_buf = nil,
preview_win = nil,
hover_buf = nil,
hover_win = nil,
flattened_outline_items = {},
code_buf = nil,
code_win = nil,
root_items = nil,
current_node = nil,
}
end
local function _update_lines()
M.state.flattened_outline_items = parser.flatten(M.state.root_items)
writer.parse_and_write(M.view.bufnr, M.state.flattened_outline_items)
end
function M._current_node()
local current_line = vim.api.nvim_win_get_cursor(M.view.winnr)[1]
return M.state.flattened_outline_items[current_line]
end
local function goto_location(change_focus)
local node = M._current_node()
vim.api.nvim_win_set_cursor(M.state.code_win, { node.line + 1, node.character })
if change_focus then
vim.fn.win_gotoid(M.state.code_win)
end
if config.options.auto_close then
M.close_outline()
end
end
local function reveal_paths(children, parent)
if children and #children < 1 then
return
end
for i = 1, #children, 1 do
local node = children[i]
if node == nil then
return
end
local perfix
node.parent = parent
if node.cname == nil and node.kind == node_kind.Package then
node.cname = node.name
local name = node.name:match(".+%.(%w+)$")
if name ~= nil then
node.name = name
end
perfix = node.cname .. "."
else
perfix = node.name .. "."
end
local j = i + 1
local next_children = nil
while j < #children do
local next_node = children[j]
if next_node.cname == nil and node.kind == node_kind.Package then
next_node.cname = next_node.name
local name = next_node.name:match(".+%.(%w+)$")
if name ~= nil then
next_node.name = name
end
end
if node.kind == next_node.kind and vim.startswith(next_node.cname, perfix) then
if next_children == nil then
next_children = {}
end
table.insert(next_children, next_node)
table.remove(children, j)
else
j = j + 1
end
end
node.children = next_children
if node.children ~= nil then
reveal_paths(node.children, node)
end
end
end
local function node_eq(a, b)
if a == nil or b == nil then
return false
end
return a.kind == b.kind and a.name == b.name and a.path == b.path
end
local function find_pkg(node)
if node == nil then
return nil
end
if node.kind > node_kind.Package then
return nil
end
if node.kind == node_kind.Package then
return node
else
find_pkg(node.parent)
end
end
function open_pkg(node)
if node.kind == node_kind.Package then
if node.children == nil then
node.children = {}
end
local c = lsp_command.get_package_data(M.state.code_buf, node)
if c ~= nil and type(c) == "table" and #c > 0 then
vim.list_extend(node.children, c)
end
end
end
function open_pkgs(node)
if node.kind == node_kind.Package then
if node.children == nil then
node.children = {}
else
open_pkgs(node.children[1])
end
local c = lsp_command.get_package_data(M.state.code_buf, node)
if c ~= nil and type(c) == "table" and #c > 0 then
vim.list_extend(node.children, c)
end
end
end
local function package_handler(node)
if not folding.is_foldable(node) then
return
end
if M.view:is_open() then
local response = lsp_command.get_package_data(M.state.code_buf, node)
if response == nil or type(response) ~= "table" or #response < 1 then
return
end
parser.sort_result(response)
local child_hir = t_utils.array_copy(node.hierarchy)
table.insert(child_hir, node.isLast)
node.children = parser.parse(response, node.depth + 1, child_hir, node)
return response
end
end
local function open_file(node)
node = node or M._current_node()
-- open_file
local fname = node.uri
if vim.startswith(fname, "file://") or vim.startswith(fname, "jdt://") then
vim.fn.win_gotoid(M.state.code_win)
local bufnr = vim.uri_to_bufnr(fname)
vim.bo[bufnr].buflisted = true
vim.api.nvim_win_set_buf(M.state.code_win, bufnr)
if config.options.auto_close then
M.close_outline()
end
end
end
function M._set_folded_or_open(open, move_cursor, node_index)
local node = M.state.flattened_outline_items[node_index] or M._current_node()
M.state.current_node = node
local folded = false
if node.folded ~= nil then
folded = not node.folded
end
if node.kind == node_kind.File or node.kind == node_kind.PrimaryType or node.kind == node_kind.ClassFile then
if move_cursor then
vim.api.nvim_win_set_cursor(M.view.winnr, { node_index, 0 })
end
if open then
open_file(node)
end
else
M._set_folded(folded, move_cursor, node_index)
end
end
function M._set_folded(folded, move_cursor, node_index)
local node = M.state.flattened_outline_items[node_index] or M._current_node()
M.state.current_node = node
if folding.is_foldable(node) then
node.folded = folded
if move_cursor then
vim.api.nvim_win_set_cursor(M.view.winnr, { node_index, 0 })
end
package_handler(node)
_update_lines()
elseif node.parent then
local parent_node = M.state.flattened_outline_items[node.parent.line_in_outline]
if parent_node then
M._set_folded(folded, not parent_node.folded and folded, parent_node.line_in_outline)
end
else
node.folded = false
end
end
function M._set_all_folded(folded, nodes)
nodes = nodes or M.state.root_items
for _, node in ipairs(nodes) do
node.folded = folded
if node.children then
M._set_all_folded(folded, node.children)
end
end
_update_lines()
end
function M._highlight_current_item(winnr)
local has_provider = providers.has_provider()
local is_current_buffer_the_outline = M.view.bufnr == vim.api.nvim_get_current_buf()
local doesnt_have_outline_buf = not M.view.bufnr
local should_exit = not has_provider or doesnt_have_outline_buf or is_current_buffer_the_outline
-- Make a special case if we have a window number
-- Because we might use this to manually focus so we dont want to quit this
-- function
if winnr then
should_exit = false
end
if should_exit then
return
end
local win = winnr or vim.api.nvim_get_current_win()
local hovered_line = vim.api.nvim_win_get_cursor(win)[1] - 1
local leaf_node = nil
local cb = function(value)
value.hovered = nil
if value.line == hovered_line then
value.hovered = true
leaf_node = value
end
end
utils.items_dfs(cb, M.state.root_items)
_update_lines()
if leaf_node then
for index, node in ipairs(M.state.flattened_outline_items) do
if node == leaf_node then
vim.api.nvim_win_set_cursor(M.view.winnr, { index, 1 })
break
end
end
end
end
local function setup_keymaps(bufnr)
local map = function(...)
utils.nmap(bufnr, ...)
end
-- show help
map(config.options.keymaps.show_help, require("java-deps.config").show_help)
-- close outline
map(config.options.keymaps.close, function()
M.view:close()
end)
-- open_file
map(config.options.keymaps.open_file, function()
M._set_folded_or_open(true)
end)
-- preview symbol
map(config.options.keymaps.toggle_preview, require("java-deps.preview").toggle)
-- fold selection
map(config.options.keymaps.fold, function()
M._set_folded(true)
end)
-- unfold selection
map(config.options.keymaps.unfold, function()
M._set_folded(false)
end)
-- fold all
map(config.options.keymaps.fold_all, function()
M._set_all_folded(true)
end)
-- unfold all
map(config.options.keymaps.unfold_all, function()
M._set_all_folded(false)
end)
-- fold reset
map(config.options.keymaps.fold_reset, function()
M._set_all_folded(nil)
end)
end
local function handler(response)
if response == nil or type(response) ~= "table" then
return
end
M.state.code_win = vim.api.nvim_get_current_win()
M.view:setup_view()
-- clear state when buffer is closed
vim.api.nvim_buf_attach(M.view.bufnr, false, {
on_detach = function(_, _)
wipe_state()
end,
})
setup_keymaps(M.view.bufnr)
setup_buffer_autocmd()
local items = parser.parse(response)
M.state.root_items = items
M.state.flattened_outline_items = parser.flatten(items)
writer.parse_and_write(M.view.bufnr, M.state.flattened_outline_items)
M._highlight_current_item(M.state.code_win)
end
function M.toggle_outline()
if M.view:is_open() then
M.close_outline()
else
M.open_outline()
end
end
local function resolve_path(path)
local resp = lsp_command.resolve_path(M.state.code_buf, path)
local function find_root(node)
for _, value in ipairs(M.state.flattened_outline_items) do
if value.kind == node.kind then
if node.kind == node_kind.PrimaryType then
if value.name == node.name then
return value
end
elseif node.kind == node_kind.CompilationUnit then
if value.uri == node.uri then
return value
end
elseif node.path ~= nil and value.path == node.path then
return value
end
end
end
end
if resp ~= nil then
for _, value in ipairs(resp) do
local node = find_root(value)
if node ~= nil then
M._set_folded_or_open(false, true, node.line_in_outline)
end
end
end
end
function M.open_outline()
if not M.view:is_open() then
M.state.code_buf = vim.api.nvim_get_current_buf()
local resp = lsp_command.get_projects(M.state.code_buf, context.current_config().root_uri)
local path = vim.uri_from_bufnr(M.state.code_buf)
handler(resp)
resolve_path(path)
end
end
function M.close_outline()
M.view:close()
end
function M.setup(opts)
config.setup(opts)
ui.setup_highlights()
M.view = View:new()
setup_global_autocmd()
end
M.attach = function(client, buf, root_dir)
context.attach(client, buf, root_dir)
end
return M