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

Commit f399d21

Browse files
author
Mikhail Arkhipov
authored
Merge pull request #201 from MikhailArkhipov/depth
Limit amount of symbols calculated for a file
2 parents faac611 + 37a3f78 commit f399d21

File tree

7 files changed

+45
-21
lines changed

7 files changed

+45
-21
lines changed

src/Analysis/Engine/Impl/Infrastructure/Extensions/EnumerableExtensions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,24 @@ public static bool SetEquals<T>(this IEnumerable<T> source, IEnumerable<T> other
5959

6060
public static IEnumerable<T> Keys<T, U>(this IEnumerable<KeyValuePair<T, U>> source) => source.Select(GetKey);
6161
public static IEnumerable<T> ExcludeDefault<T>(this IEnumerable<T> source) => source.Where(i => !Equals(i, default(T)));
62+
63+
64+
public static IEnumerable<T> TraverseBreadthFirst<T>(this T root, Func<T, IEnumerable<T>> selectChildren) {
65+
var items = new Queue<T>();
66+
items.Enqueue(root);
67+
while (items.Count > 0) {
68+
var item = items.Dequeue();
69+
yield return item;
70+
71+
var childen = selectChildren(item);
72+
if (childen == null) {
73+
continue;
74+
}
75+
76+
foreach (var child in childen) {
77+
items.Enqueue(child);
78+
}
79+
}
80+
}
6281
}
6382
}

src/Analysis/Engine/Impl/Values/Protocols.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,13 @@ protected override Protocol UnionMergeTypes(Protocol p) {
385385
}
386386

387387
class TupleProtocol : IterableProtocol {
388-
internal readonly IAnalysisSet[] _values;
388+
private readonly IAnalysisSet[] _values;
389389

390390
public TupleProtocol(ProtocolInfo self, IEnumerable<IAnalysisSet> values) : base(self, AnalysisSet.UnionAll(values)) {
391391
_values = values.Select(s => s.AsUnion(1)).ToArray();
392+
393+
var argTypes = _values.SelectMany(e => e.Select(v => v is IHasQualifiedName qn ? qn.FullyQualifiedName : v.ShortDescription));
394+
Name = "tuple[{0}]".FormatInvariant(string.Join(", ", argTypes));
392395
}
393396

394397
protected override void EnsureMembers(IDictionary<string, IAnalysisSet> members) {
@@ -415,7 +418,7 @@ public override IAnalysisSet GetIndex(Node node, AnalysisUnit unit, IAnalysisSet
415418
return AnalysisSet.UnionAll(constants.Select(GetItem));
416419
}
417420

418-
public override string Name => "tuple";
421+
public override string Name { get; }
419422

420423
public override IEnumerable<KeyValuePair<string, string>> GetRichDescription() {
421424
if (_values.Any()) {

src/LanguageServer/Impl/Definitions/ServerSettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class PythonAnalysisOptions {
2525

2626
public bool openFilesOnly;
2727
public int symbolsHierarchyDepthLimit = 10;
28+
public int symbolsHierarchyMaxSymbols = 1000;
2829

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

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

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// See the Apache Version 2.0 License for specific language governing
1515
// permissions and limitations under the License.
1616

17+
using System;
1718
using System.Collections.Generic;
1819
using System.Linq;
1920
using System.Threading;
@@ -26,7 +27,8 @@
2627

2728
namespace 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));

src/LanguageServer/Impl/Implementation/Server.cs

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

687687
_symbolHierarchyDepthLimit = Settings.analysis.symbolsHierarchyDepthLimit;
688+
_symbolHierarchyMaxSymbols = Settings.analysis.symbolsHierarchyMaxSymbols;
688689

689690
if (oldSettings == null) {
690691
return true;

src/LanguageServer/Impl/LanguageServer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ public async Task DidChangeConfiguration(JToken token, CancellationToken cancell
144144
settings.analysis.openFilesOnly = GetSetting(analysis, "openFilesOnly", false);
145145
settings.diagnosticPublishDelay = GetSetting(analysis, "diagnosticPublishDelay", 1000);
146146
settings.symbolsHierarchyDepthLimit = GetSetting(analysis, "symbolsHierarchyDepthLimit", 10);
147+
settings.symbolsHierarchyMaxSymbols = GetSetting(analysis, "symbolsHierarchyMaxSymbols", 1000);
147148

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

src/LanguageServer/Impl/LanguageServerSettings.cs

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

0 commit comments

Comments
 (0)