1414// See the Apache Version 2.0 License for specific language governing
1515// permissions and limitations under the License.
1616
17+ using System ;
1718using System . Collections . Generic ;
1819using System . Linq ;
1920using System . Threading ;
2627
2728namespace Microsoft . Python . LanguageServer . Implementation {
2829 public sealed partial class Server {
29- private static int _symbolHierarchyDepthLimit = 1 ;
30+ private static int _symbolHierarchyDepthLimit = 10 ;
31+ private static int _symbolHierarchyMaxSymbols = 1000 ;
3032
3133 public override async Task < SymbolInformation [ ] > WorkspaceSymbols ( WorkspaceSymbolParams @params , CancellationToken cancellationToken ) {
3234 await WaitForCompleteAnalysisAsync ( cancellationToken ) ;
@@ -72,8 +74,9 @@ private static async Task<List<IMemberResult>> GetModuleVariablesAsync(ProjectEn
7274 }
7375
7476 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
7780 . Where ( m => {
7881 if ( m . Values . Any ( v => v . DeclaringModule == entry || v . Locations . Any ( l => l . DocumentUri == entry . DocumentUri ) ) ) {
7982 if ( string . IsNullOrEmpty ( prefix ) || m . Name . StartsWithOrdinal ( prefix , ignoreCase : true ) ) {
@@ -82,17 +85,10 @@ private static IEnumerable<IMemberResult> GetModuleVariables(ProjectEntry entry,
8285 }
8386 return false ;
8487 } )
85- . Concat ( GetChildScopesVariables ( analysis , analysis . Scope , opts , 0 ) ) ;
88+ . Take ( _symbolHierarchyMaxSymbols ) ;
89+ return result ;
8690 }
8791
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-
9692 private SymbolInformation ToSymbolInformation ( IMemberResult m ) {
9793 var res = new SymbolInformation {
9894 name = m . Name ,
@@ -115,8 +111,8 @@ private SymbolInformation ToSymbolInformation(IMemberResult m) {
115111 }
116112
117113 private DocumentSymbol [ ] ToDocumentSymbols ( List < IMemberResult > members ) {
118- var childMap = new Dictionary < IMemberResult , List < IMemberResult > > ( ) ;
119114 var topLevel = new List < IMemberResult > ( ) ;
115+ var childMap = new Dictionary < IMemberResult , List < IMemberResult > > ( ) ;
120116
121117 foreach ( var m in members ) {
122118 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) {
131127 }
132128
133129 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 ( ) ;
138134
139135 return symbols ;
140136 }
@@ -149,9 +145,11 @@ private DocumentSymbol ToDocumentSymbol(IMemberResult m, Dictionary<IMemberResul
149145 } ;
150146
151147 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 ( ) ;
153151 } else {
154- res . children = new DocumentSymbol [ 0 ] ;
152+ res . children = Array . Empty < DocumentSymbol > ( ) ;
155153 }
156154
157155 var loc = m . Locations . FirstOrDefault ( l => ! string . IsNullOrEmpty ( l . FilePath ) ) ;
0 commit comments