Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit f5cc097

Browse files
author
MikhailArkhipov
committed
Limit depth and count
1 parent 3889bc4 commit f5cc097

File tree

5 files changed

+16
-20
lines changed

5 files changed

+16
-20
lines changed

src/LanguageServer/Impl/Definitions/ServerSettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class PythonAnalysisOptions {
2424
private Dictionary<string, DiagnosticSeverity> _map = new Dictionary<string, DiagnosticSeverity>();
2525

2626
public bool openFilesOnly;
27+
public int symbolsHierarchyDepthLimit = 10;
2728
public int symbolsHierarchyMaxSymbols = 1000;
2829

2930
public string[] errors { get; } = Array.Empty<string>();

src/LanguageServer/Impl/Implementation/Server.WorkspaceSymbols.cs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
namespace Microsoft.Python.LanguageServer.Implementation {
2929
public sealed partial class Server {
30+
private static int _symbolHierarchyDepthLimit = 10;
3031
private static int _symbolHierarchyMaxSymbols = 1000;
3132

3233
public override async Task<SymbolInformation[]> WorkspaceSymbols(WorkspaceSymbolParams @params, CancellationToken cancellationToken) {
@@ -73,8 +74,9 @@ private static async Task<List<IMemberResult>> GetModuleVariablesAsync(ProjectEn
7374
}
7475

7576
private static IEnumerable<IMemberResult> GetModuleVariables(ProjectEntry entry, GetMemberOptions opts, string prefix, IModuleAnalysis analysis) {
76-
var all = analysis.GetAllMembers(SourceLocation.None, opts);
77-
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
7880
.Where(m => {
7981
if (m.Values.Any(v => v.DeclaringModule == entry || v.Locations.Any(l => l.DocumentUri == entry.DocumentUri))) {
8082
if (string.IsNullOrEmpty(prefix) || m.Name.StartsWithOrdinal(prefix, ignoreCase: true)) {
@@ -83,17 +85,10 @@ private static IEnumerable<IMemberResult> GetModuleVariables(ProjectEntry entry,
8385
}
8486
return false;
8587
})
86-
.Take(_symbolHierarchyMaxSymbols)
87-
.Concat(GetChildScopesVariables(analysis, analysis.Scope, opts))
8888
.Take(_symbolHierarchyMaxSymbols);
89+
return result;
8990
}
9091

91-
private static IEnumerable<IMemberResult> GetChildScopesVariables(IModuleAnalysis analysis, IScope scope, GetMemberOptions opts)
92-
=> scope.TraverseBreadthFirst(s => s.Children).SelectMany(c => GetScopeVariables(analysis, c, opts));
93-
94-
private static IEnumerable<IMemberResult> GetScopeVariables(IModuleAnalysis analysis, IScope scope, GetMemberOptions opts)
95-
=> analysis.GetAllAvailableMembersFromScope(scope, opts).Concat(GetChildScopesVariables(analysis, scope, opts));
96-
9792
private SymbolInformation ToSymbolInformation(IMemberResult m) {
9893
var res = new SymbolInformation {
9994
name = m.Name,
@@ -118,9 +113,8 @@ private SymbolInformation ToSymbolInformation(IMemberResult m) {
118113
private DocumentSymbol[] ToDocumentSymbols(List<IMemberResult> members) {
119114
var topLevel = new List<IMemberResult>();
120115
var childMap = new Dictionary<IMemberResult, List<IMemberResult>>();
121-
var totalCount = 0;
122116

123-
foreach (var m in members.Take(_symbolHierarchyMaxSymbols)) {
117+
foreach (var m in members) {
124118
var parent = members.FirstOrDefault(x => x.Scope?.Node == m.Scope?.OuterScope?.Node && x.Name == m.Scope?.Name);
125119
if (parent != null) {
126120
if (!childMap.TryGetValue(parent, out var children)) {
@@ -132,18 +126,16 @@ private DocumentSymbol[] ToDocumentSymbols(List<IMemberResult> members) {
132126
}
133127
}
134128

135-
var symbols = topLevel.SelectMany(t => t
136-
.TraverseBreadthFirst(c => childMap.ContainsKey(c) ? childMap[c] : Enumerable.Empty<IMemberResult>())
137-
.Take(_symbolHierarchyMaxSymbols)
129+
var symbols = topLevel
138130
.GroupBy(mr => mr.Name)
139131
.Select(g => g.First())
140-
.Select(m => ToDocumentSymbol(m, childMap)))
132+
.Select(m => ToDocumentSymbol(m, childMap, 0))
141133
.ToArray();
142134

143135
return symbols;
144136
}
145137

146-
private DocumentSymbol ToDocumentSymbol(IMemberResult m, Dictionary<IMemberResult, List<IMemberResult>> childMap) {
138+
private DocumentSymbol ToDocumentSymbol(IMemberResult m, Dictionary<IMemberResult, List<IMemberResult>> childMap, int currentDepth) {
147139
var res = new DocumentSymbol {
148140
name = m.Name,
149141
detail = m.Name,
@@ -152,9 +144,9 @@ private DocumentSymbol ToDocumentSymbol(IMemberResult m, Dictionary<IMemberResul
152144
_functionKind = GetFunctionKind(m)
153145
};
154146

155-
if (childMap.TryGetValue(m, out var children)) {
147+
if (childMap.TryGetValue(m, out var children) && currentDepth < _symbolHierarchyDepthLimit) {
156148
res.children = children
157-
.Select(x => ToDocumentSymbol(x, childMap))
149+
.Select(x => ToDocumentSymbol(x, childMap, currentDepth + 1))
158150
.ToArray();
159151
} else {
160152
res.children = Array.Empty<DocumentSymbol>();

src/LanguageServer/Impl/Implementation/Server.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ private bool HandleConfigurationChanges(ServerSettings newSettings) {
684684
var oldSettings = Settings;
685685
Settings = newSettings;
686686

687+
_symbolHierarchyDepthLimit = Settings.analysis.symbolsHierarchyDepthLimit;
687688
_symbolHierarchyMaxSymbols = Settings.analysis.symbolsHierarchyMaxSymbols;
688689

689690
if (oldSettings == null) {

src/LanguageServer/Impl/LanguageServer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ public async Task DidChangeConfiguration(JToken token, CancellationToken cancell
142142
var analysis = pythonSection["analysis"];
143143
settings.analysis.openFilesOnly = GetSetting(analysis, "openFilesOnly", false);
144144
settings.diagnosticPublishDelay = GetSetting(analysis, "diagnosticPublishDelay", 1000);
145-
settings.symbolsHierarchyMaxSymbols = GetSetting(analysis, "symbolsHierarchyDepthLimit", 1000);
145+
settings.symbolsHierarchyDepthLimit = GetSetting(analysis, "symbolsHierarchyDepthLimit", 10);
146+
settings.symbolsHierarchyMaxSymbols = GetSetting(analysis, "symbolsHierarchyMaxSymbols", 1000);
146147

147148
_ui.SetLogLevel(GetLogLevel(analysis));
148149

src/LanguageServer/Impl/LanguageServerSettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace Microsoft.Python.LanguageServer.Implementation {
1818
public sealed class LanguageServerSettings: ServerSettings {
1919
public int diagnosticPublishDelay = 1000;
20+
public int symbolsHierarchyDepthLimit = 10;
2021
public int symbolsHierarchyMaxSymbols = 1000;
2122
}
2223
}

0 commit comments

Comments
 (0)