Skip to content

Commit bbcc337

Browse files
committed
fix: 添加说明,更新 vscode-java-dependency 依赖
1 parent a4db373 commit bbcc337

File tree

5 files changed

+325
-166
lines changed

5 files changed

+325
-166
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,22 @@
44
- [vscode-java-dependency](https://github.com/Microsoft/vscode-java-dependency) 提供数据支持
55

66
![java-deps](https://javahello.github.io/dev/nvim-lean/images/java-deps.png)
7+
8+
## 使用说明
9+
10+
- lazy.nvim
11+
12+
```lua
13+
{
14+
"JavaHello/java-deps.nvim",
15+
lazy = true,
16+
ft = "java",
17+
dependencies = "mfussenegger/nvim-jdtls",
18+
config = function()
19+
require("java-deps").setup({})
20+
end,
21+
}
22+
23+
-- jdtls lsp attach
24+
require("java-deps").attach(client, buffer, root_dir)
25+
```

lua/java-deps.lua

Lines changed: 130 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ local M = {
2424
flattened_outline_items = {},
2525
code_buf = nil,
2626
code_win = nil,
27-
outline_items = nil,
27+
root_items = nil,
28+
current_node = nil,
2829
},
2930
}
3031

@@ -59,11 +60,21 @@ local function setup_buffer_autocmd()
5960
end
6061

6162
local function wipe_state()
62-
M.state = { outline_items = {}, flattened_outline_items = {}, code_win = 0, code_buf = 0 }
63+
M.state = {
64+
preview_buf = nil,
65+
preview_win = nil,
66+
hover_buf = nil,
67+
hover_win = nil,
68+
flattened_outline_items = {},
69+
code_buf = nil,
70+
code_win = nil,
71+
root_items = nil,
72+
current_node = nil,
73+
}
6374
end
6475

6576
local function _update_lines()
66-
M.state.flattened_outline_items = parser.flatten(M.state.outline_items)
77+
M.state.flattened_outline_items = parser.flatten(M.state.root_items)
6778
writer.parse_and_write(M.view.bufnr, M.state.flattened_outline_items)
6879
end
6980

@@ -83,15 +94,123 @@ local function goto_location(change_focus)
8394
end
8495
end
8596

97+
local function reveal_paths(children, parent)
98+
if children and #children < 1 then
99+
return
100+
end
101+
for i = 1, #children, 1 do
102+
local node = children[i]
103+
if node == nil then
104+
return
105+
end
106+
local perfix
107+
node.parent = parent
108+
if node.cname == nil and node.kind == node_kind.Package then
109+
node.cname = node.name
110+
local name = node.name:match(".+%.(%w+)$")
111+
if name ~= nil then
112+
node.name = name
113+
end
114+
perfix = node.cname .. "."
115+
else
116+
perfix = node.name .. "."
117+
end
118+
local j = i + 1
119+
local next_children = nil
120+
while j < #children do
121+
local next_node = children[j]
122+
if next_node.cname == nil and node.kind == node_kind.Package then
123+
next_node.cname = next_node.name
124+
local name = next_node.name:match(".+%.(%w+)$")
125+
if name ~= nil then
126+
next_node.name = name
127+
end
128+
end
129+
if node.kind == next_node.kind and vim.startswith(next_node.cname, perfix) then
130+
if next_children == nil then
131+
next_children = {}
132+
end
133+
table.insert(next_children, next_node)
134+
table.remove(children, j)
135+
else
136+
j = j + 1
137+
end
138+
end
139+
node.children = next_children
140+
if node.children ~= nil then
141+
reveal_paths(node.children, node)
142+
end
143+
end
144+
end
145+
146+
local function node_eq(a, b)
147+
if a == nil or b == nil then
148+
return false
149+
end
150+
return a.kind == b.kind and a.name == b.name and a.path == b.path
151+
end
152+
local function find_pkg(node)
153+
if node == nil then
154+
return nil
155+
end
156+
if node.kind > node_kind.Package then
157+
return nil
158+
end
159+
if node.kind == node_kind.Package then
160+
return node
161+
else
162+
find_pkg(node.parent)
163+
end
164+
end
165+
166+
function open_pkg(node)
167+
if node.kind == node_kind.Package then
168+
if node.children == nil then
169+
node.children = {}
170+
end
171+
local c = lsp_command.get_package_data(M.state.code_buf, node)
172+
if c ~= nil and type(c) == "table" and #c > 0 then
173+
vim.list_extend(node.children, c)
174+
end
175+
end
176+
end
177+
function open_pkgs(node)
178+
if node.kind == node_kind.Package then
179+
if node.children == nil then
180+
node.children = {}
181+
else
182+
open_pkgs(node.children[1])
183+
end
184+
local c = lsp_command.get_package_data(M.state.code_buf, node)
185+
if c ~= nil and type(c) == "table" and #c > 0 then
186+
vim.list_extend(node.children, c)
187+
end
188+
end
189+
end
190+
86191
local function package_handler(node)
87192
if not folding.is_foldable(node) then
88193
return
89194
end
90195
if M.view:is_open() then
91196
local response = lsp_command.get_package_data(M.state.code_buf, node)
92-
if response == nil or type(response) ~= "table" then
197+
if response == nil or type(response) ~= "table" or #response < 1 then
93198
return
94199
end
200+
if node.kind == node_kind.PackageRoot then
201+
parser.sort_result(response)
202+
reveal_paths(response, node)
203+
local pkg = find_pkg(M.state.current_node)
204+
if pkg ~= nil then
205+
for _, n in ipairs(response) do
206+
if n.kind == node_kind.Package and node_eq(n, pkg) then
207+
open_pkg(n)
208+
end
209+
end
210+
else
211+
open_pkgs(response[1])
212+
end
213+
end
95214
local child_hir = t_utils.array_copy(node.hierarchy)
96215
table.insert(child_hir, node.isLast)
97216
node.children = parser.parse(response, node.depth + 1, child_hir, node)
@@ -117,6 +236,7 @@ end
117236

118237
function M._set_folded_or_open(open, move_cursor, node_index)
119238
local node = M.state.flattened_outline_items[node_index] or M._current_node()
239+
M.state.current_node = node
120240
local folded = false
121241
if node.folded ~= nil then
122242
folded = not node.folded
@@ -134,6 +254,7 @@ function M._set_folded_or_open(open, move_cursor, node_index)
134254
end
135255
function M._set_folded(folded, move_cursor, node_index)
136256
local node = M.state.flattened_outline_items[node_index] or M._current_node()
257+
M.state.current_node = node
137258
if folding.is_foldable(node) then
138259
node.folded = folded
139260

@@ -153,7 +274,7 @@ function M._set_folded(folded, move_cursor, node_index)
153274
end
154275

155276
function M._set_all_folded(folded, nodes)
156-
nodes = nodes or M.state.outline_items
277+
nodes = nodes or M.state.root_items
157278

158279
for _, node in ipairs(nodes) do
159280
node.folded = folded
@@ -200,7 +321,7 @@ function M._highlight_current_item(winnr)
200321
end
201322
end
202323

203-
utils.items_dfs(cb, M.state.outline_items)
324+
utils.items_dfs(cb, M.state.root_items)
204325

205326
_update_lines()
206327

@@ -272,7 +393,7 @@ local function handler(response)
272393

273394
local items = parser.parse(response)
274395

275-
M.state.outline_items = items
396+
M.state.root_items = items
276397
M.state.flattened_outline_items = parser.flatten(items)
277398

278399
writer.parse_and_write(M.view.bufnr, M.state.flattened_outline_items)
@@ -293,11 +414,11 @@ local function resolve_path(path)
293414
local function find_root(node)
294415
for _, value in ipairs(M.state.flattened_outline_items) do
295416
if value.kind == node.kind then
296-
if node.kind == 5 then
417+
if node.kind == node_kind.PrimaryType then
297418
if value.name == node.name then
298419
return value
299420
end
300-
elseif node.kind == 6 then
421+
elseif node.kind == node_kind.CompilationUnit then
301422
if value.uri == node.uri then
302423
return value
303424
end

lua/java-deps/config.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ local M = {
1212
preview_bg_highlight = "Pmenu",
1313
winblend = 0,
1414
request_timeout = 3000,
15-
autofold_depth = 0,
15+
autofold_depth = 99,
1616
fold_markers = { "", "" },
1717
position = "right",
1818
wrap = false,
@@ -33,14 +33,14 @@ local M = {
3333
Project = { icon = "", hl = "@text.uri" },
3434
PackageRoot = { icon = "", hl = "@text.uri" },
3535
Package = { icon = "", hl = "@namespace" },
36-
PrimaryType = { icon = "", hl = "@type" },
37-
CompilationUnit = { icon = "", hl = "@text.uri" },
36+
PrimaryType = { icon = "󰠱", hl = "@type" },
37+
CompilationUnit = { icon = "", hl = "@text.uri" },
3838
ClassFile = { icon = "", hl = "@text.uri" },
3939
Container = { icon = "󰆧", hl = "@text.uri" },
4040
Folder = { icon = "󰉋", hl = "@method" },
4141
File = { icon = "󰈙", hl = "@method" },
4242

43-
CLASS = { icon = "", hl = "@class" },
43+
CLASS = { icon = "󰠱", hl = "@class" },
4444
ENUM = { icon = "", hl = "@enum" },
4545
INTERFACE = { icon = "", hl = "@interface" },
4646
JAR = { icon = "", hl = "@conditional" },

0 commit comments

Comments
 (0)