14
14
// See the Apache Version 2.0 License for specific language governing
15
15
// permissions and limitations under the License.
16
16
17
+ using System ;
17
18
using System . Collections . Generic ;
18
19
using System . Linq ;
19
20
using System . Threading ;
26
27
27
28
namespace Microsoft . Python . LanguageServer . Implementation {
28
29
public sealed partial class Server {
29
- private static int _symbolHierarchyDepthLimit = 1 ;
30
+ private static int _symbolHierarchyDepthLimit = 10 ;
31
+ private static int _symbolHierarchyMaxSymbols = 1000 ;
30
32
31
33
public override async Task < SymbolInformation [ ] > WorkspaceSymbols ( WorkspaceSymbolParams @params , CancellationToken cancellationToken ) {
32
34
await WaitForCompleteAnalysisAsync ( cancellationToken ) ;
@@ -72,8 +74,9 @@ private static async Task<List<IMemberResult>> GetModuleVariablesAsync(ProjectEn
72
74
}
73
75
74
76
private static IEnumerable < IMemberResult > GetModuleVariables ( ProjectEntry entry , GetMemberOptions opts , string prefix , IModuleAnalysis analysis ) {
75
- var all = analysis . GetAllMembers ( SourceLocation . None , opts ) ;
76
- return all
77
+ var breadthFirst = analysis . Scope . TraverseBreadthFirst ( s => s . Children ) ;
78
+ var all = breadthFirst . SelectMany ( c => analysis . GetAllAvailableMembersFromScope ( c , opts ) ) ;
79
+ var result = all
77
80
. Where ( m => {
78
81
if ( m . Values . Any ( v => v . DeclaringModule == entry || v . Locations . Any ( l => l . DocumentUri == entry . DocumentUri ) ) ) {
79
82
if ( string . IsNullOrEmpty ( prefix ) || m . Name . StartsWithOrdinal ( prefix , ignoreCase : true ) ) {
@@ -82,17 +85,10 @@ private static IEnumerable<IMemberResult> GetModuleVariables(ProjectEntry entry,
82
85
}
83
86
return false ;
84
87
} )
85
- . Concat ( GetChildScopesVariables ( analysis , analysis . Scope , opts , 0 ) ) ;
88
+ . Take ( _symbolHierarchyMaxSymbols ) ;
89
+ return result ;
86
90
}
87
91
88
- private static IEnumerable < IMemberResult > GetChildScopesVariables ( IModuleAnalysis analysis , IScope scope , GetMemberOptions opts , int currentDepth )
89
- => currentDepth < _symbolHierarchyDepthLimit
90
- ? scope . Children . SelectMany ( c => GetScopeVariables ( analysis , c , opts , currentDepth ) )
91
- : Enumerable . Empty < IMemberResult > ( ) ;
92
-
93
- private static IEnumerable < IMemberResult > GetScopeVariables ( IModuleAnalysis analysis , IScope scope , GetMemberOptions opts , int currentDepth )
94
- => analysis . GetAllAvailableMembersFromScope ( scope , opts ) . Concat ( GetChildScopesVariables ( analysis , scope , opts , currentDepth + 1 ) ) ;
95
-
96
92
private SymbolInformation ToSymbolInformation ( IMemberResult m ) {
97
93
var res = new SymbolInformation {
98
94
name = m . Name ,
@@ -115,8 +111,8 @@ private SymbolInformation ToSymbolInformation(IMemberResult m) {
115
111
}
116
112
117
113
private DocumentSymbol [ ] ToDocumentSymbols ( List < IMemberResult > members ) {
118
- var childMap = new Dictionary < IMemberResult , List < IMemberResult > > ( ) ;
119
114
var topLevel = new List < IMemberResult > ( ) ;
115
+ var childMap = new Dictionary < IMemberResult , List < IMemberResult > > ( ) ;
120
116
121
117
foreach ( var m in members ) {
122
118
var parent = members . FirstOrDefault ( x => x . Scope ? . Node == m . Scope ? . OuterScope ? . Node && x . Name == m . Scope ? . Name ) ;
@@ -131,10 +127,10 @@ private DocumentSymbol[] ToDocumentSymbols(List<IMemberResult> members) {
131
127
}
132
128
133
129
var symbols = topLevel
134
- . GroupBy ( mr => mr . Name )
135
- . Select ( g => g . First ( ) )
136
- . Select ( m => ToDocumentSymbol ( m , childMap , 0 ) )
137
- . ToArray ( ) ;
130
+ . GroupBy ( mr => mr . Name )
131
+ . Select ( g => g . First ( ) )
132
+ . Select ( m => ToDocumentSymbol ( m , childMap , 0 ) )
133
+ . ToArray ( ) ;
138
134
139
135
return symbols ;
140
136
}
@@ -149,9 +145,11 @@ private DocumentSymbol ToDocumentSymbol(IMemberResult m, Dictionary<IMemberResul
149
145
} ;
150
146
151
147
if ( childMap . TryGetValue ( m , out var children ) && currentDepth < _symbolHierarchyDepthLimit ) {
152
- res . children = children . Select ( x => ToDocumentSymbol ( x , childMap , currentDepth + 1 ) ) . ToArray ( ) ;
148
+ res . children = children
149
+ . Select ( x => ToDocumentSymbol ( x , childMap , currentDepth + 1 ) )
150
+ . ToArray ( ) ;
153
151
} else {
154
- res . children = new DocumentSymbol [ 0 ] ;
152
+ res . children = Array . Empty < DocumentSymbol > ( ) ;
155
153
}
156
154
157
155
var loc = m . Locations . FirstOrDefault ( l => ! string . IsNullOrEmpty ( l . FilePath ) ) ;
0 commit comments