6565--- @field _parent DataNode ?
6666--- @field _project DataNode ?
6767--- @field _rootNode DataNode ?
68- --- @field _hierarchicalNode boolean
68+ --- @field _hierarchicalPackageNode boolean
69+ --- @field _hierarchicalPackageRootNode boolean
6970local DataNode = ExplorerNode :new ()
7071
7172DataNode .__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 )
228230end
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[]
231259function 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
276322end
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
0 commit comments