Skip to content

Commit b7959f8

Browse files
committed
resolvePath
1 parent 7dd617d commit b7959f8

File tree

6 files changed

+160
-48
lines changed

6 files changed

+160
-48
lines changed

lua/java-deps.lua

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local jdtls = require("java-deps.java.jdtls")
22
local config = require("java-deps.config")
33
local View = require("java-deps.view")
4-
local data_node = require("java-deps.views.data_node")
54
local provider = require("java-deps.views.data_provider")
65
local writer = require("java-deps.writer")
76

@@ -14,24 +13,16 @@ local M = {
1413
code_win = nil,
1514
root_items = nil,
1615
current_node = nil,
16+
current_path = nil,
1717
},
1818
}
1919

20-
local function handle_projects(projects)
21-
if not projects or #projects == 0 then
22-
return
23-
end
24-
local project_nodes = {}
25-
for _, project in ipairs(projects) do
26-
if project then
27-
local root = data_node.createNode(project)
28-
if root then
29-
root:getChildren()
30-
table.insert(project_nodes, root)
31-
end
32-
end
33-
end
34-
local result = provider.flattenTree(project_nodes, 0)
20+
local function handle_projects()
21+
local uri = vim.uri_from_fname(jdtls.root_dir())
22+
local data = provider.DataProvider:new(uri, M.state.current_path)
23+
data:revealPaths()
24+
local result = data:flattenTree()
25+
vim.print(vim.inspect(result))
3526
writer.parse_and_write(M.view.bufnr, result)
3627
end
3728

@@ -46,11 +37,10 @@ end
4637
function M.open_outline()
4738
if not M.view:is_open() then
4839
M.state.code_buf = vim.api.nvim_get_current_buf()
40+
M.state.current_path = vim.uri_from_bufnr(M.state.code_buf)
4941
M.view:open()
50-
local uri = vim.uri_from_fname(jdtls.root_dir())
5142
local wf = coroutine.wrap(function()
52-
local resp = jdtls.getProjects(uri)
53-
handle_projects(resp)
43+
handle_projects()
5444
end)
5545
local ok, err = pcall(wf)
5646
if not ok then

lua/java-deps/config.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ local M = {
1111
show_relative_numbers = false,
1212
preview_bg_highlight = "Pmenu",
1313
winblend = 0,
14-
request_timeout = 3000,
1514
autofold_depth = 99,
1615
fold_markers = { "", "" },
1716
position = "right",

lua/java-deps/java/jdtls.lua

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ local M = {}
77
---@return INodeData[]
88
M.getProjects = function(params)
99
local err, resp = lsp_command.execute_command({
10-
command = "java.project.list",
10+
command = lsp_command.JAVA_PROJECT_LIST,
1111
arguments = params,
12-
}, nil, 0)
12+
})
1313
if err then
1414
vim.notify(err.message or vim.inspect(err), vim.log.levels.WARN)
1515
return {}
@@ -31,11 +31,6 @@ M.getProjectUris = function()
3131
return resp or {}
3232
end
3333

34-
-- interface IPackageDataParam {
35-
-- projectUri: string | undefined;
36-
-- [key: string]: any;
37-
-- }
38-
3934
---@return INodeData[]
4035
M.getPackageData = function(params)
4136
local excludePatterns = {}
@@ -84,4 +79,16 @@ M.getPackageData = function(params)
8479
return nodeData
8580
end
8681

82+
---@return INodeData[]
83+
M.resolvePath = function(params)
84+
local err, resp = lsp_command.execute_command({
85+
command = lsp_command.JAVA_RESOLVEPATH,
86+
arguments = params,
87+
})
88+
if err then
89+
vim.notify(err.message or vim.inspect(err), vim.log.levels.WARN)
90+
return {}
91+
end
92+
return resp or {}
93+
end
8794
return M

lua/java-deps/parser.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ function M.get_lines(flattened_outline_items)
5959
-- else add a middle marker
6060
elseif index == #line then
6161
-- add fold markers
62-
if config.options.fold_markers and data_node.is_folded(node) then
63-
if data_node.is_folded(node) then
62+
local folded = data_node.is_folded(node)
63+
if config.options.fold_markers and folded then
64+
if folded then
6465
line[index] = config.options.fold_markers[1]
6566
else
6667
line[index] = config.options.fold_markers[2]

lua/java-deps/views/data_node.lua

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ end
6565
---@field _parent DataNode?
6666
---@field _project DataNode?
6767
---@field _rootNode DataNode?
68-
---@field _hierarchicalNode boolean
68+
---@field _hierarchicalPackageNode boolean
69+
---@field _hierarchicalPackageRootNode boolean
6970
local DataNode = ExplorerNode:new()
7071

7172
DataNode.__index = DataNode
72-
DataNode._hierarchicalNode = false
73+
DataNode._hierarchicalPackageNode = false
74+
DataNode._hierarchicalPackageRootNode = false
7375

7476
---@param nodeData INodeData
7577
---@param parent DataNode?
@@ -227,32 +229,83 @@ function DataNode:sort()
227229
end)
228230
end
229231

232+
function DataNode:baseRevealPaths(paths)
233+
if #paths == 0 then
234+
return self
235+
end
236+
local childNodeData = table.remove(paths, 1)
237+
---@type DataNode[]
238+
local children = self:getChildren()
239+
---@type DataNode[]?
240+
local childNode = vim.tbl_filter(function(child)
241+
return childNodeData.name == child._nodeData.name and childNodeData.path == child._nodeData.path
242+
end, children)
243+
childNode = (childNode and #childNode > 0) and childNode[1] or nil
244+
return (childNode and #paths > 0) and childNode:revealPaths(paths) or childNode
245+
end
246+
247+
---@param uri string
248+
---@return boolean
249+
local function is_workspace_file(uri)
250+
local rootPath = jdtls.root_dir()
251+
if vim.startswith(uri, "file:/") then
252+
local path = vim.uri_to_fname(uri)
253+
return path == rootPath or vim.startswith(path, rootPath)
254+
end
255+
return false
256+
end
257+
230258
---@param paths INodeData[]
231259
function DataNode:revealPaths(paths)
232260
if #paths == 0 then
233261
return self
234262
end
235263
local kind = self:kind()
236-
if kind == NodeKind.PackageRoot then
264+
if kind == NodeKind.Project then
265+
if not self._nodeData.uri then
266+
return
267+
end
268+
269+
if is_workspace_file(self._nodeData.uri) then
270+
return self:baseRevealPaths(paths)
271+
end
272+
273+
local childNodeData = paths[1]
274+
---@type DataNode[]
275+
local children = self:getChildren()
276+
---@type DataNode[]?
277+
local childNode = vim.tbl_filter(function(child)
278+
return vim.startswith(childNodeData.name, child._nodeData.name .. ".")
279+
or childNodeData.name == child._nodeData.name
280+
end, children)
281+
---@type DataNode?
282+
childNode = (childNode and #childNode > 0) and childNode[1] or nil
283+
if childNode and childNode._hierarchicalPackageNode then
284+
table.remove(paths, 1)
285+
end
286+
return (childNode and #paths > 0) and childNode:revealPaths(paths) or childNode
287+
elseif kind == NodeKind.PackageRoot and self._hierarchicalPackageRootNode then
237288
local hierarchicalNodeData = paths[1]
289+
---@type DataNode[]
238290
local children = self:getChildren()
239291
---@type DataNode[]?
240292
local childNode = vim.tbl_filter(function(child)
241293
return vim.startswith(hierarchicalNodeData.name, child._nodeData.name .. ".")
242294
or hierarchicalNodeData.name == child._nodeData.name
243295
end, children)
244296
---@type DataNode?
245-
childNode = childNode and #childNode > 0 and childNode[1] or nil
246-
if childNode and not childNode[1]._hierarchicalNode then
297+
childNode = (childNode and #childNode > 0) and childNode[1] or nil
298+
if childNode and not childNode._hierarchicalPackageNode then
247299
table.remove(paths, 1)
248300
end
249301
return (childNode and #paths > 0) and childNode:revealPaths(paths) or childNode
250-
elseif kind == NodeKind.Package then
302+
elseif kind == NodeKind.Package and self._hierarchicalPackageNode then
251303
local hierarchicalNodeData = paths[1]
252304
if hierarchicalNodeData.name == self._nodeData.name then
253305
table.remove(paths, 1)
254-
return self:revealPaths(paths)
306+
return self:baseRevealPaths(paths)
255307
else
308+
---@type DataNode[]
256309
local children = self:getChildren()
257310
---@type DataNode[]?
258311
local childNode = vim.tbl_filter(function(child)
@@ -264,14 +317,7 @@ function DataNode:revealPaths(paths)
264317
return (childNode and #paths > 0) and childNode:revealPaths(paths) or nil
265318
end
266319
else
267-
local childNodeData = table.remove(paths, 1)
268-
local children = self:getChildren()
269-
---@type DataNode?
270-
local childNode = vim.tbl_filter(function(child)
271-
return childNodeData.name == child._nodeData.name and childNodeData.path == child._nodeData.path
272-
end, children)
273-
childNode = childNode and #childNode > 0 and childNode[1] or nil
274-
return (childNode and #paths > 0) and childNode:revealPaths(paths) or childNode
320+
return self:baseRevealPaths(paths)
275321
end
276322
end
277323

@@ -339,14 +385,16 @@ M.createNode = function(nodeData, parent, project, rootNode)
339385
vim.notify("Package root node must have parent and project", vim.log.levels.ERROR)
340386
return nil
341387
end
342-
return DataNode:new(nodeData, parent, project, rootNode)
388+
local data = DataNode:new(nodeData, parent, project, rootNode)
389+
data._hierarchicalPackageRootNode = true
390+
return data
343391
elseif nodeData.kind == NodeKind.Package then
344392
if not parent or not project or not rootNode then
345393
vim.notify("Package node must have parent, project and root node", vim.log.levels.ERROR)
346394
return nil
347395
end
348396
local data = DataNode:new(nodeData, parent, project, rootNode)
349-
data._hierarchicalNode = true
397+
data._hierarchicalPackageNode = true
350398
return data
351399
elseif nodeData.kind == NodeKind.PrimaryType then
352400
if nodeData.metaData and nodeData.metaData[M.K_TYPE_KIND] then

lua/java-deps/views/data_provider.lua

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local jdtls = require("java-deps.java.jdtls")
2+
local data_node = require("java-deps.views.data_node")
13
local M = {}
24

35
---@class TreeItem
@@ -14,20 +16,78 @@ local M = {}
1416
---@field hierarchy? table
1517
local TreeItem = {}
1618

19+
---@class DataProvider
20+
---@field rootPath string
21+
---@field currentPath string
22+
---@field _rootItems DataNode[]?
23+
local DataProvider = {}
24+
DataProvider.__index = DataProvider
25+
26+
function DataProvider:new(rootPath, currentPath)
27+
return setmetatable({
28+
rootPath = rootPath,
29+
currentPath = currentPath,
30+
_rootItems = {},
31+
}, self)
32+
end
33+
---@return INodeData[]?
34+
function DataProvider:getRootNodes()
35+
return jdtls.getProjects(self.rootPath)
36+
end
37+
38+
---@param projects? INodeData[]
39+
---@return DataNode[]?
40+
function DataProvider:_revealPaths(projects)
41+
if not projects or #projects == 0 then
42+
return
43+
end
44+
45+
---@type DataNode[]
46+
local project_nodes = {}
47+
for _, project in ipairs(projects) do
48+
if project then
49+
local root = data_node.createNode(project)
50+
if root then
51+
table.insert(project_nodes, root)
52+
end
53+
end
54+
end
55+
---@type INodeData[]
56+
local rpath = jdtls.resolvePath(self.currentPath)
57+
---@type INodeData
58+
local cpath = (rpath and #rpath > 0) and table.remove(rpath, 1) or nil
59+
for _, root in ipairs(project_nodes) do
60+
if cpath and cpath.name == root._nodeData.name and cpath.path == root._nodeData.path then
61+
root:revealPaths(rpath)
62+
break
63+
end
64+
end
65+
self._rootItems = project_nodes
66+
return project_nodes
67+
end
68+
69+
---@return DataNode[]?
70+
function DataProvider:revealPaths()
71+
return self:_revealPaths(self:getRootNodes())
72+
end
73+
1774
---@param nodes DataNode[]
1875
---@return TreeItem[]
19-
function M.flattenTree(nodes, _level)
76+
local function _flattenTree(nodes, _level, hierarchy)
2077
local level = _level or 0
2178
local result = {}
2279
for idx, node in ipairs(nodes) do
2380
local c = node:getTreeItem()
81+
local _hierarchy = hierarchy or {}
2482
c.depth = level
2583
if idx == #nodes then
84+
_hierarchy[level] = false
2685
c.isLast = true
2786
end
87+
c.hierarchy = vim.deepcopy(_hierarchy)
2888
table.insert(result, c)
2989
if node:hasChildren() then
30-
local children = M.flattenTree(node._childrenNodes, level + 1)
90+
local children = _flattenTree(node._childrenNodes, level + 1, _hierarchy)
3191
for _, child in ipairs(children) do
3292
table.insert(result, child)
3393
end
@@ -36,4 +96,11 @@ function M.flattenTree(nodes, _level)
3696
return result
3797
end
3898

99+
---@return TreeItem[]
100+
function DataProvider:flattenTree()
101+
return _flattenTree(self._rootItems, 0, nil)
102+
end
103+
104+
M.DataProvider = DataProvider
105+
39106
return M

0 commit comments

Comments
 (0)