@@ -4,6 +4,7 @@ local ExplorerNode = require("java-deps.views.explorer_node").ExplorerNode
44local hieararchicalPackageNodeData = require (" java-deps.java.hieararchicalPackageNodeData" )
55
66local M = {}
7+ M .isHierarchicalView = true
78
89M .K_TYPE_KIND = " TypeKind"
910M .NATURE_ID = " NatureId"
@@ -87,6 +88,41 @@ function DataNode:new(nodeData, parent, project, rootNode)
8788 return data
8889end
8990
91+ --- @return DataNode[]
92+ function DataNode :createHierarchicalPackageRootNode ()
93+ local result = {}
94+ local packageData = {}
95+ if self ._nodeData .children then
96+ for _ , child in ipairs (self ._nodeData .children ) do
97+ if child .kind == NodeKind .Package then
98+ table.insert (packageData , child )
99+ else
100+ table.insert (result , M .createNode (child , self , self ._project , self ))
101+ end
102+ end
103+ if # packageData > 0 then
104+ local data = hieararchicalPackageNodeData .createHierarchicalNodeDataByPackageList (packageData )
105+ if data and data .children then
106+ for _ , child in ipairs (data .children ) do
107+ table.insert (result , M .createNode (child , self , self ._project , self ))
108+ end
109+ end
110+ end
111+ end
112+ return result
113+ end
114+
115+ --- @return DataNode[]
116+ function DataNode :createHierarchicalPackageNode ()
117+ local result = {}
118+ if self ._nodeData .children then
119+ for _ , child in ipairs (self ._nodeData .children ) do
120+ table.insert (result , M .createNode (child , self , self ._project , self ._rootNode ))
121+ end
122+ end
123+ return result
124+ end
125+
90126function DataNode :createChildNodeList ()
91127 local kind = self :kind ()
92128 if kind == NodeKind .Workspace then
@@ -106,10 +142,17 @@ function DataNode:createChildNodeList()
106142 table.insert (result , M .createNode (child , self , self , nil ))
107143 end
108144 end
145+
109146 if # packageData > 0 then
110- local data = hieararchicalPackageNodeData .createHierarchicalNodeDataByPackageList (packageData )
111- if data and data .children then
112- for _ , child in ipairs (data .children ) do
147+ if M .isHierarchicalView then
148+ local data = hieararchicalPackageNodeData .createHierarchicalNodeDataByPackageList (packageData )
149+ if data and data .children then
150+ for _ , child in ipairs (data .children ) do
151+ table.insert (result , M .createNode (child , self , self , self ))
152+ end
153+ end
154+ else
155+ for _ , child in ipairs (packageData ) do
113156 table.insert (result , M .createNode (child , self , self , self ))
114157 end
115158 end
@@ -125,21 +168,29 @@ function DataNode:createChildNodeList()
125168 end
126169 return result
127170 elseif kind == NodeKind .PackageRoot then
128- local result = {}
129- if self ._nodeData .children then
130- for _ , child in ipairs (self ._nodeData .children ) do
131- table.insert (result , M .createNode (child , self , self ._project , self ))
171+ if M .isHierarchicalView then
172+ return self :createHierarchicalPackageRootNode ()
173+ else
174+ local result = {}
175+ if self ._nodeData .children then
176+ for _ , child in ipairs (self ._nodeData .children ) do
177+ table.insert (result , M .createNode (child , self , self ._project , self ))
178+ end
132179 end
180+ return result
133181 end
134- return result
135182 elseif kind == NodeKind .Package then
136- local result = {}
137- if self ._nodeData .children then
138- for _ , child in ipairs (self ._nodeData .children ) do
139- table.insert (result , M .createNode (child , self , self ._project , self ._rootNode ))
183+ if M .isHierarchicalView then
184+ return self :createHierarchicalPackageNode ()
185+ else
186+ local result = {}
187+ if self ._nodeData .children then
188+ for _ , child in ipairs (self ._nodeData .children ) do
189+ table.insert (result , M .createNode (child , self , self ._project , self ._rootNode ))
190+ end
140191 end
192+ return result
141193 end
142- return result
143194 elseif kind == NodeKind .Folder then
144195 local result = {}
145196 if self ._nodeData .children then
@@ -154,7 +205,6 @@ function DataNode:createChildNodeList()
154205 return nil
155206 end
156207end
157-
158208function DataNode :loadData ()
159209 local kind = self :kind ()
160210 if kind == NodeKind .Workspace then
@@ -176,7 +226,7 @@ function DataNode:loadData()
176226 projectUri = self ._project ._nodeData .uri ,
177227 rootPath = self ._nodeData .path ,
178228 handlerIdentifier = self ._nodeData .handlerIdentifier ,
179- isHierarchicalView = true ,
229+ isHierarchicalView = M . isHierarchicalView ,
180230 })
181231 elseif kind == NodeKind .Package then
182232 return jdtls .getPackageData ({
@@ -284,21 +334,26 @@ function DataNode:revealPaths(paths)
284334 table.remove (paths , 1 )
285335 end
286336 return (childNode and # paths > 0 ) and childNode :revealPaths (paths ) or childNode
287- elseif kind == NodeKind .PackageRoot and self ._hierarchicalPackageRootNode then
288- local hierarchicalNodeData = paths [1 ]
289- --- @type DataNode[]
290- local children = self :getChildren ()
291- --- @type DataNode[] ?
292- local childNode = vim .tbl_filter (function (child )
293- return vim .startswith (hierarchicalNodeData .name , child ._nodeData .name .. " ." )
294- or hierarchicalNodeData .name == child ._nodeData .name
295- end , children )
296- --- @type DataNode ?
297- childNode = (childNode and # childNode > 0 ) and childNode [1 ] or nil
298- if childNode and not childNode ._hierarchicalPackageNode then
299- table.remove (paths , 1 )
337+ elseif kind == NodeKind .PackageRoot then
338+ if self ._hierarchicalPackageRootNode then
339+ local hierarchicalNodeData = paths [1 ]
340+
341+ --- @type DataNode[]
342+ local children = self :getChildren ()
343+ --- @type DataNode[] ?
344+ local childNode = vim .tbl_filter (function (child )
345+ return vim .startswith (hierarchicalNodeData .name , child ._nodeData .name .. " ." )
346+ or hierarchicalNodeData .name == child ._nodeData .name
347+ end , children )
348+ --- @type DataNode ?
349+ childNode = (childNode and # childNode > 0 ) and childNode [1 ] or nil
350+ if childNode and not childNode ._hierarchicalPackageNode then
351+ table.remove (paths , 1 )
352+ end
353+ return (childNode and # paths > 0 ) and childNode :revealPaths (paths ) or childNode
354+ else
355+ return self :baseRevealPaths (paths )
300356 end
301- return (childNode and # paths > 0 ) and childNode :revealPaths (paths ) or childNode
302357 elseif kind == NodeKind .Package and self ._hierarchicalPackageNode then
303358 local hierarchicalNodeData = paths [1 ]
304359 if hierarchicalNodeData .name == self ._nodeData .name then
@@ -313,7 +368,7 @@ function DataNode:revealPaths(paths)
313368 or hierarchicalNodeData .name == child ._nodeData .name
314369 end , children )
315370 --- @type DataNode ?
316- childNode = childNode and # childNode > 0 and childNode [1 ] or nil
371+ childNode = ( childNode and # childNode > 0 ) and childNode [1 ] or nil
317372 return (childNode and # paths > 0 ) and childNode :revealPaths (paths ) or nil
318373 end
319374 else
@@ -386,15 +441,19 @@ M.createNode = function(nodeData, parent, project, rootNode)
386441 return nil
387442 end
388443 local data = DataNode :new (nodeData , parent , project , rootNode )
389- data ._hierarchicalPackageRootNode = true
444+ if M .isHierarchicalView then
445+ data ._hierarchicalPackageRootNode = true
446+ end
390447 return data
391448 elseif nodeData .kind == NodeKind .Package then
392449 if not parent or not project or not rootNode then
393450 vim .notify (" Package node must have parent, project and root node" , vim .log .levels .ERROR )
394451 return nil
395452 end
396453 local data = DataNode :new (nodeData , parent , project , rootNode )
397- data ._hierarchicalPackageNode = true
454+ if M .isHierarchicalView then
455+ data ._hierarchicalPackageNode = true
456+ end
398457 return data
399458 elseif nodeData .kind == NodeKind .PrimaryType then
400459 if nodeData .metaData and nodeData .metaData [M .K_TYPE_KIND ] then
0 commit comments