Skip to content

Commit b763257

Browse files
committed
tree item
1 parent b87cfda commit b763257

File tree

7 files changed

+107
-140
lines changed

7 files changed

+107
-140
lines changed

lua/java-deps.lua

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ local jdtls = require("java-deps.java.jdtls")
22
local config = require("java-deps.config")
33
local View = require("java-deps.view")
44
local data_node = require("java-deps.views.data_node")
5+
local provider = require("java-deps.views.data_provider")
6+
local writer = require("java-deps.writer")
57

68
local M = {
79
view = nil,
@@ -16,16 +18,21 @@ local M = {
1618
}
1719

1820
local function handle_projects(projects)
19-
if not projects or #projects < 1 then
21+
if not projects or #projects == 0 then
2022
return
2123
end
2224
local project_nodes = {}
2325
for _, project in ipairs(projects) do
2426
if project then
2527
local root = data_node.createNode(project)
26-
table.insert(project_nodes, root)
28+
if root then
29+
root:getChildren()
30+
table.insert(project_nodes, root)
31+
end
2732
end
2833
end
34+
local result = provider.flattenTree(project_nodes, 0)
35+
writer.parse_and_write(M.view.bufnr, result)
2936
end
3037

3138
function M.toggle_outline()
@@ -39,11 +46,16 @@ end
3946
function M.open_outline()
4047
if not M.view:is_open() then
4148
M.state.code_buf = vim.api.nvim_get_current_buf()
49+
M.view:open()
4250
local uri = vim.uri_from_fname(jdtls.root_dir())
43-
vim.defer_fn(function()
51+
local wf = coroutine.wrap(function()
4452
local resp = jdtls.getProjects(uri)
4553
handle_projects(resp)
46-
end, 0)
54+
end)
55+
local ok, err = pcall(wf)
56+
if not ok then
57+
print(err.message or vim.inspect(err))
58+
end
4759
end
4860
end
4961

lua/java-deps/java/jdtls.lua

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ M.getProjects = function(params)
1414
vim.notify(err.message or vim.inspect(err), vim.log.levels.WARN)
1515
return {}
1616
end
17-
return resp and resp.reslut or {}
17+
return resp or {}
1818
end
1919
M.root_dir = function()
2020
return lsp_command.get_client().root_dir
@@ -28,7 +28,7 @@ M.getProjectUris = function()
2828
vim.notify(err.message or vim.inspect(err), vim.log.levels.WARN)
2929
return {}
3030
end
31-
return resp and resp.reslut or {}
31+
return resp or {}
3232
end
3333

3434
-- interface IPackageDataParam {
@@ -39,13 +39,16 @@ end
3939
---@return INodeData[]
4040
M.getPackageData = function(params)
4141
local excludePatterns = {}
42-
local err, resp = lsp_command.execute_command(lsp_command.JAVA_GETPACKAGEDATA, params)
42+
local err, resp = lsp_command.execute_command({
43+
command = lsp_command.JAVA_GETPACKAGEDATA,
44+
arguments = params,
45+
})
4346
if err then
4447
vim.notify(err.message or vim.inspect(err), vim.log.levels.WARN)
4548
return {}
4649
end
4750
---@type INodeData[]
48-
local nodeData = resp and resp.reslut or {}
51+
local nodeData = resp and resp or {}
4952
-- Filter out non java resources
5053
if true then
5154
nodeData = vim.tbl_filter(function(data)

lua/java-deps/parser.lua

Lines changed: 36 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,43 @@
11
local config = require("java-deps.config")
2-
local symbols = require("java-deps.symbols")
3-
local folding = require("java-deps.folding")
4-
local t_utils = require("java-deps.utils.table")
5-
local ui = require("java-deps.ui")
2+
local data_node = require("java-deps.views.data_node")
63
local M = {}
7-
---@param result DataNode[]
8-
---@param depth integer
9-
---@return table
10-
local function parse_result(result, depth)
11-
local ret = nil
12-
13-
for index, value in pairs(result) do
14-
local level = depth or 1
15-
-- whether this node is the last in its group
16-
local isLast = index == #result
17-
18-
local node = {
19-
kind = value.kind,
20-
depth = level,
21-
parent = parent,
22-
}
23-
if ret == nil then
24-
ret = {}
25-
end
26-
27-
table.insert(ret, node)
28-
29-
local children = nil
30-
if value.children ~= nil then
31-
-- copy by value because we dont want it messing with the hir table
32-
local child_hir = t_utils.array_copy(hir)
33-
table.insert(child_hir, isLast)
34-
children = parse_result(value._childrenNodes, level + 1, child_hir, node)
35-
end
364

37-
node.children = children
5+
local function str_to_table(str)
6+
local t = {}
7+
for i = 1, #str do
8+
t[i] = str:sub(i, i)
389
end
39-
return ret
10+
return t
4011
end
4112

42-
M.sort_result = function(result)
43-
table.sort(result, function(a, b)
44-
if a.kind == b.kind then
45-
return a.name < b.name
46-
end
47-
return a.kind < b.kind
48-
end)
49-
return result
50-
end
51-
52-
function M.parse(response, depth, hierarchy, parent)
53-
local sorted = M.sort_result(response)
54-
return parse_result(sorted, depth, hierarchy, parent)
55-
end
56-
57-
function M.flatten(outline_items, ret)
58-
ret = ret or {}
59-
for _, value in ipairs(outline_items) do
60-
table.insert(ret, value)
61-
value.line_in_outline = #ret
62-
if value.children ~= nil and not folding.is_folded(value) then
63-
M.flatten(value.children, ret)
64-
end
13+
local function table_to_str(t)
14+
local ret = ""
15+
for _, value in ipairs(t) do
16+
ret = ret .. tostring(value)
6517
end
6618
return ret
6719
end
6820

21+
local guides = {
22+
markers = {
23+
bottom = "",
24+
middle = "",
25+
vertical = "",
26+
horizontal = "",
27+
},
28+
}
29+
---@param flattened_outline_items TreeItem
30+
---@return table
31+
---@return table
6932
function M.get_lines(flattened_outline_items)
7033
local lines = {}
7134
local hl_info = {}
7235

7336
for node_line, node in ipairs(flattened_outline_items) do
7437
local depth = node.depth
75-
local marker_space = (config.options.fold_markers and 1) or 0
38+
local marker_space = config.options.fold_markers and 1 or 0
7639

77-
local line = t_utils.str_to_table(string.rep(" ", depth + marker_space))
40+
local line = str_to_table(string.rep(" ", depth + marker_space))
7841
local running_length = 1
7942

8043
local function add_guide_hl(from, to)
@@ -96,8 +59,8 @@ function M.get_lines(flattened_outline_items)
9659
-- else add a middle marker
9760
elseif index == #line then
9861
-- add fold markers
99-
if config.options.fold_markers and folding.is_foldable(node) then
100-
if folding.is_folded(node) then
62+
if config.options.fold_markers and data_node.is_folded(node) then
63+
if data_node.is_folded(node) then
10164
line[index] = config.options.fold_markers[1]
10265
else
10366
line[index] = config.options.fold_markers[2]
@@ -108,21 +71,21 @@ function M.get_lines(flattened_outline_items)
10871
-- the root level has no vertical markers
10972
elseif depth > 1 then
11073
if node.isLast then
111-
line[index] = ui.markers.bottom
112-
add_guide_hl(running_length, running_length + vim.fn.strlen(ui.markers.bottom) - 1)
74+
line[index] = guides.markers.bottom
75+
add_guide_hl(running_length, running_length + vim.fn.strlen(guides.markers.bottom) - 1)
11376
else
114-
line[index] = ui.markers.middle
115-
add_guide_hl(running_length, running_length + vim.fn.strlen(ui.markers.middle) - 1)
77+
line[index] = guides.markers.middle
78+
add_guide_hl(running_length, running_length + vim.fn.strlen(guides.markers.middle) - 1)
11679
end
11780
end
11881
-- else if the parent was not the last in its group, add a
11982
-- vertical marker because there are items under us and we need
12083
-- to point to those
12184
elseif not node.hierarchy[index] and depth > 1 then
122-
line[index + marker_space] = ui.markers.vertical
85+
line[index + marker_space] = guides.markers.vertical
12386
add_guide_hl(
12487
running_length - 1 + 2 * marker_space,
125-
running_length + vim.fn.strlen(ui.markers.vertical) - 1 + 2 * marker_space
88+
running_length + vim.fn.strlen(guides.markers.vertical) - 1 + 2 * marker_space
12689
)
12790
end
12891
end
@@ -134,13 +97,14 @@ function M.get_lines(flattened_outline_items)
13497

13598
local final_prefix = line
13699

137-
local string_prefix = t_utils.table_to_str(final_prefix)
100+
local string_prefix = table_to_str(final_prefix)
138101

139-
table.insert(lines, string_prefix .. node.icon .. " " .. node.name)
102+
table.insert(lines, string_prefix .. node.icon .. " " .. node.label)
140103

141104
local hl_start = #string_prefix
142105
local hl_end = #string_prefix + #node.icon
143-
local hl_type = config.options.symbols[symbols.kinds[node.kind]].hl
106+
local hl = config.options.symbols[node.kind]
107+
local hl_type = hl and hl.hl or "@lsp.type.class"
144108
table.insert(hl_info, { node_line, hl_start, hl_end, hl_type })
145109

146110
node.prefix_length = #string_prefix + #node.icon + 1
@@ -152,9 +116,7 @@ function M.get_details(flattened_outline_items)
152116
local lines = {}
153117
for _, value in ipairs(flattened_outline_items) do
154118
local detail
155-
if symbols.type_kind(value) == symbols.NodeKind.JAR then
156-
detail = value.path
157-
end
119+
-- TODO
158120
table.insert(lines, detail or "")
159121
end
160122
return lines

lua/java-deps/view.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
local config = require("java-deps.config")
2+
3+
---@class View
4+
---@field bufnr number
5+
---@field winnr number
26
local View = {}
7+
View.__index = View
38

49
function View:new()
5-
return setmetatable({ bufnr = nil, winnr = nil }, { __index = View })
10+
return setmetatable({ bufnr = nil, winnr = nil }, self)
611
end
712

813
---creates the outline window and sets it up
@@ -56,6 +61,9 @@ function View:close()
5661
self.winnr = nil
5762
self.bufnr = nil
5863
end
64+
function View:open()
65+
self:setup_view()
66+
end
5967

6068
function View:is_open()
6169
return self.winnr and self.bufnr and vim.api.nvim_buf_is_valid(self.bufnr) and vim.api.nvim_win_is_valid(self.winnr)

lua/java-deps/views/data_node.lua

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ M.ContainerPath = {
4747
}
4848

4949
---@enum TreeItemCollapsibleState
50-
local TreeItemCollapsibleState = {
50+
M.TreeItemCollapsibleState = {
5151
None = 0,
5252
Collapsed = 1,
5353
Expanded = 2,
@@ -77,7 +77,7 @@ DataNode._hierarchicalNode = false
7777
---@param rootNode DataNode?
7878
---@return DataNode
7979
function DataNode:new(nodeData, parent, project, rootNode)
80-
local data = setmetatable(ExplorerNode:new(), self)
80+
local data = setmetatable({}, self)
8181
data._nodeData = nodeData
8282
data._parent = parent
8383
data._project = project
@@ -206,19 +206,21 @@ function DataNode:loadData()
206206
end
207207
end
208208

209-
function DataNode:icon() end
209+
function DataNode:icon()
210+
return "C"
211+
end
210212
function DataNode:kind()
211213
return self._nodeData.kind
212214
end
213215

214216
function DataNode:sort()
215217
table.sort(self._childrenNodes, function(a, b)
216218
---@diagnostic disable: undefined-field
217-
if a._nodeData.kind and a._nodeData.kind then
219+
if a._nodeData.kind and b._nodeData.kind and a._nodeData.name and b._nodeData.name then
218220
if a._nodeData.kind == b._nodeData.kind then
219-
return a._nodeData.name < b._nodeData.name and false or true
221+
return a._nodeData.name < b._nodeData.name
220222
else
221-
return a._nodeData.kind - b._nodeData.kind
223+
return a._nodeData.kind < b._nodeData.kind
222224
end
223225
end
224226
return false
@@ -300,15 +302,14 @@ function DataNode:getChildren()
300302
end
301303
self._childrenNodes = self:createChildNodeList() or {}
302304
self:sort()
303-
return self._childrenNodes
304305
end
306+
return self._childrenNodes
305307
else
306308
if not self._nodeData.children then
307309
local data = self:loadData()
308310
self._nodeData.children = data
309311
self._childrenNodes = self:createChildNodeList() or {}
310312
self:sort()
311-
return self._childrenNodes
312313
end
313314
return self._childrenNodes
314315
end
@@ -407,15 +408,20 @@ function DataNode:hasChildren()
407408
return self._childrenNodes and #self._childrenNodes > 0
408409
end
409410

411+
function M.is_folded(tree)
412+
return tree.collapsible == M.TreeItemCollapsibleState.Collapsed
413+
end
414+
410415
function DataNode:getTreeItem()
411416
---@type TreeItem
412417
local item = {
413418
label = self._nodeData.displayName or self._nodeData.name,
414-
collapsible = self:hasChildren() and TreeItemCollapsibleState.Collapsed or TreeItemCollapsibleState.None,
419+
collapsible = self:hasChildren() and M.TreeItemCollapsibleState.Collapsed or M.TreeItemCollapsibleState.None,
415420
}
416421
item.description = self:description()
417422
item.icon = self:icon()
418423
item.command = self:command()
424+
item.data = self
419425
if self._nodeData.uri then
420426
local kind = self:kind()
421427
if

0 commit comments

Comments
 (0)