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

Commit 51c6b46

Browse files
author
MikhailArkhipov
committed
Fix analysis version set
Fix analysis of modules that import changed one
1 parent f6ea95c commit 51c6b46

File tree

9 files changed

+109
-100
lines changed

9 files changed

+109
-100
lines changed

src/Analysis/Engine/Impl/ProjectEntry.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,13 @@ public IPythonParse GetCurrentParse() {
157157

158158
internal void SetCompleteAnalysis() {
159159
lock (this) {
160-
_analysisTcs.TrySetResult(Analysis);
160+
if (AnalysisVersion == _expectedParse) {
161+
_analysisTcs.TrySetResult(Analysis);
162+
}
163+
}
164+
if (AnalysisVersion == _expectedParse) {
165+
RaiseNewAnalysis();
161166
}
162-
RaiseNewAnalysis();
163167
}
164168

165169
internal void ResetCompleteAnalysis() {

src/Analysis/Engine/Test/AnalysisTest.cs

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ public async Task SpecialArgTypes() {
156156
[TestMethod, Priority(0)]
157157
public async Task TestPackageImportStar() {
158158
using (var server = await CreateServerAsync(PythonVersions.LatestAvailable3X)) {
159-
var fob = server.AddModuleWithContent("fob", "fob\\__init__.py", "from oar import *");
160-
var oar = server.AddModuleWithContent("fob.oar", "fob\\oar\\__init__.py", "from .baz import *");
161-
var baz = server.AddModuleWithContent("fob.oar.baz", "fob\\oar\\baz.py", "import fob.oar.quox as quox\r\nfunc = quox.func");
162-
var quox = server.AddModuleWithContent("fob.oar.quox", "fob\\oar\\quox.py", "def func(): return 42");
159+
var fob = await server.AddModuleWithContentAsync("fob", "fob\\__init__.py", "from oar import *");
160+
var oar = await server.AddModuleWithContentAsync("fob.oar", "fob\\oar\\__init__.py", "from .baz import *");
161+
var baz = await server.AddModuleWithContentAsync("fob.oar.baz", "fob\\oar\\baz.py", "import fob.oar.quox as quox\r\nfunc = quox.func");
162+
var quox = await server.AddModuleWithContentAsync("fob.oar.quox", "fob\\oar\\quox.py", "def func(): return 42");
163163

164164
var fobAnalysis = await fob.GetAnalysisAsync();
165165
var oarAnalysis = await oar.GetAnalysisAsync();
@@ -559,7 +559,7 @@ public async Task RecursiveDictionaryKeyValues() {
559559
i = x['y']['x']['value']
560560
s = y['x']['y']['value']
561561
";
562-
server.SendDidChangeTextDocument(uri, code);
562+
await server.SendDidChangeTextDocumentAsync(uri, code);
563563
analysis = await server.GetAnalysisAsync(uri);
564564

565565
analysis.Should().HaveVariable("i").OfTypes(BuiltinTypeId.Int)
@@ -745,7 +745,7 @@ def __init__(self, value):
745745
);
746746

747747
text1 = text1.Substring(0, text1.IndexOf(" def")) + Environment.NewLine + text1.Substring(text1.IndexOf(" def"));
748-
server.SendDidChangeTextDocument(uri1, text1);
748+
await server.SendDidChangeTextDocumentAsync(uri1, text1);
749749

750750
references = await server.SendFindReferences(uri1, 5, 9);
751751
references.Should().OnlyHaveReferences(
@@ -755,7 +755,7 @@ def __init__(self, value):
755755
);
756756

757757
text2 = Environment.NewLine + text2;
758-
server.SendDidChangeTextDocument(uri2, text2);
758+
await server.SendDidChangeTextDocumentAsync(uri2, text2);
759759

760760
references = await server.SendFindReferences(uri1, 5, 9);
761761
references.Should().OnlyHaveReferences(
@@ -799,7 +799,7 @@ import mod1
799799
z = mod1.f('abc')
800800
";
801801

802-
server.SendDidChangeTextDocument(uri2, text2);
802+
await server.SendDidChangeTextDocumentAsync(uri2, text2);
803803
analysis2 = await server.GetAnalysisAsync(uri2);
804804

805805
analysis1.Should().HaveFunction("f")
@@ -902,7 +902,7 @@ def __init__(self):
902902
self.f(_C__A=42) # sig help should be _C__A
903903
";
904904

905-
server.SendDidChangeTextDocument(uri, code);
905+
await server.SendDidChangeTextDocumentAsync(uri, code);
906906
await server.GetAnalysisAsync(uri);
907907

908908
var signatures = await server.SendSignatureHelp(uri, 3, 15);
@@ -928,7 +928,7 @@ def __init__(self):
928928
929929
";
930930

931-
server.SendDidChangeTextDocument(uri, code);
931+
await server.SendDidChangeTextDocumentAsync(uri, code);
932932
await server.GetAnalysisAsync(uri);
933933

934934
completions = await server.SendCompletion(uri, 3, 13);
@@ -948,7 +948,7 @@ def f(self):
948948
xyz = C._C__FOB # Advanced members completion should work here
949949
";
950950

951-
server.SendDidChangeTextDocument(uri, code);
951+
await server.SendDidChangeTextDocumentAsync(uri, code);
952952
await server.GetAnalysisAsync(uri);
953953

954954
completions = await server.SendCompletion(uri, 5, 16);
@@ -1018,7 +1018,7 @@ class C(A, B): pass
10181018
c = C()
10191019
";
10201020

1021-
server.SendDidChangeTextDocument(uri, code);
1021+
await server.SendDidChangeTextDocumentAsync(uri, code);
10221022
analysis = await server.GetAnalysisAsync(uri);
10231023

10241024
analysis.Should().HaveClassInfo("C")
@@ -1032,7 +1032,7 @@ class G(F,E): pass
10321032
G.remember2buy
10331033
";
10341034

1035-
server.SendDidChangeTextDocument(uri, code);
1035+
await server.SendDidChangeTextDocumentAsync(uri, code);
10361036
analysis = await server.GetAnalysisAsync(uri);
10371037

10381038
analysis.Should().HaveClassInfo("G")
@@ -1047,7 +1047,7 @@ class G(E,F): pass
10471047
G.remember2buy
10481048
";
10491049

1050-
server.SendDidChangeTextDocument(uri, code);
1050+
await server.SendDidChangeTextDocumentAsync(uri, code);
10511051
analysis = await server.GetAnalysisAsync(uri);
10521052

10531053
analysis.Should().HaveClassInfo("G")
@@ -1067,7 +1067,7 @@ class Z(K1,K2,K3): pass
10671067
z = Z()
10681068
";
10691069

1070-
server.SendDidChangeTextDocument(uri, code);
1070+
await server.SendDidChangeTextDocumentAsync(uri, code);
10711071
analysis = await server.GetAnalysisAsync(uri);
10721072

10731073
analysis.Should().HaveClassInfo("Z")
@@ -1081,7 +1081,7 @@ class C(str): pass
10811081
z = None
10821082
";
10831083

1084-
server.SendDidChangeTextDocument(uri, code);
1084+
await server.SendDidChangeTextDocumentAsync(uri, code);
10851085
analysis = await server.GetAnalysisAsync(uri);
10861086

10871087
analysis.Should().HaveClassInfo("A").WithMethodResolutionOrder("A", "type int", "type object")
@@ -1141,7 +1141,7 @@ await server.SendDidOpenTextDocument(uri, @"
11411141
.And.HaveVariable("iB").OfType(BuiltinTypeId.StrIterator)
11421142
.And.HaveVariable("iC").OfType(BuiltinTypeId.ListIterator);
11431143

1144-
server.SendDidChangeTextDocument(uri, @"
1144+
await server.SendDidChangeTextDocumentAsync(uri, @"
11451145
A = [1, 2, 3]
11461146
B = 'abc'
11471147
C = [1.0, 'a', 3]
@@ -1157,7 +1157,7 @@ await server.SendDidOpenTextDocument(uri, @"
11571157
.And.HaveVariable("iB").OfType(BuiltinTypeId.StrIterator)
11581158
.And.HaveVariable("iC").OfType(BuiltinTypeId.ListIterator);
11591159

1160-
server.SendDidChangeTextDocument(uri, @"
1160+
await server.SendDidChangeTextDocumentAsync(uri, @"
11611161
A = [1, 2, 3]
11621162
B = 'abc'
11631163
C = [1.0, 'a', 3]
@@ -1174,7 +1174,7 @@ await server.SendDidOpenTextDocument(uri, @"
11741174
.And.HaveVariable("b").OfType(BuiltinTypeId.Str)
11751175
.And.HaveVariable("c").OfTypes(BuiltinTypeId.Int, BuiltinTypeId.Str, BuiltinTypeId.Float);
11761176

1177-
server.SendDidChangeTextDocument(uri, @"
1177+
await server.SendDidChangeTextDocumentAsync(uri, @"
11781178
iA = iter(lambda: 1, 2)
11791179
iB = iter(lambda: 'abc', None)
11801180
iC = iter(lambda: 1, 'abc')
@@ -1213,7 +1213,7 @@ await server.SendDidOpenTextDocument(uri, @"
12131213
.And.HaveVariable("b").OfType(BuiltinTypeId.Unicode)
12141214
.And.HaveVariable("c").OfTypes(BuiltinTypeId.Int, BuiltinTypeId.Unicode, BuiltinTypeId.Float);
12151215

1216-
server.SendDidChangeTextDocument(uri, @"
1216+
await server.SendDidChangeTextDocumentAsync(uri, @"
12171217
iA = iter(lambda: 1, 2)
12181218
iB = iter(lambda: 'abc', None)
12191219
iC = iter(lambda: 1, 'abc')
@@ -2751,7 +2751,7 @@ del self.abc
27512751
print self.abc
27522752
27532753
D(42)";
2754-
server.SendDidChangeTextDocument(uri, text);
2754+
await server.SendDidChangeTextDocumentAsync(uri, text);
27552755

27562756
referencesAbc = await server.SendFindReferences(uri, 4, 15);
27572757
referencesFob = await server.SendFindReferences(uri, 4, 21);
@@ -2795,7 +2795,7 @@ def f(): pass
27952795
def f(): pass
27962796
27972797
x = f";
2798-
server.SendDidChangeTextDocument(uri, text);
2798+
await server.SendDidChangeTextDocumentAsync(uri, text);
27992799
referencesF = await server.SendFindReferences(uri, 3, 5);
28002800

28012801
referencesF.Should().OnlyHaveReferences(
@@ -3695,7 +3695,8 @@ public async Task SetLiteral() {
36953695
using (var server = await CreateServerAsync()) {
36963696
var analysis = await server.OpenDefaultDocumentAndGetAnalysisAsync(code);
36973697

3698-
analysis.Should().HaveVariable("x").WithDescription("set[int]")
3698+
analysis.Should()
3699+
.HaveVariable("x").WithDescription("set[int]")
36993700
.And.HaveVariable("abc").OfType(BuiltinTypeId.Int);
37003701
}
37013702
}
@@ -5347,26 +5348,29 @@ class C(object):
53475348
var uriFob = await server.OpenDocumentAndGetUriAsync("fob.py", fobSrc);
53485349
var uriOar = await server.OpenDocumentAndGetUriAsync("oar.py", oarSrc);
53495350
var uriBaz = await server.OpenDocumentAndGetUriAsync("baz.py", bazSrc);
5350-
server.SendDidChangeTextDocument(uriFob, "from oar import C");
5351+
await server.SendDidChangeTextDocumentAsync(uriFob, "from oar import C");
53515352

53525353
var references = await server.SendFindReferences(uriFob, 0, 17);
53535354
references.Should().OnlyHaveReferences(
53545355
(uriFob, (0, 16, 0, 17), ReferenceKind.Reference),
53555356
(uriOar, (1, 0, 2, 8), ReferenceKind.Value),
5356-
(uriOar, (1, 6, 1, 7), ReferenceKind.Definition)
5357+
(uriOar, (1, 6, 1, 7), ReferenceKind.Definition),
5358+
(uriOar, (0, 0, 0, 0), ReferenceKind.Definition)
53575359
);
53585360

5361+
await server.WaitForCompleteAnalysisAsync(CancellationToken.None);
53595362
var analysis = await server.GetAnalysisAsync(uriFob);
53605363
analysis.Should().HaveVariable("C").WithDescription("C");
53615364

53625365
// delete the class..
5363-
server.SendDidChangeTextDocument(uriOar, "");
5366+
await server.SendDidChangeTextDocumentAsync(uriOar, "");
53645367

5368+
await server.WaitForCompleteAnalysisAsync(CancellationToken.None);
53655369
analysis = await server.GetAnalysisAsync(uriFob);
5366-
analysis.Should().NotHaveVariable("C");
5370+
analysis.Should().HaveVariable("C").WithNoTypes();
53675371

53685372
// Change location of the class
5369-
server.SendDidChangeTextDocument(uriFob, "from baz import C");
5373+
await server.SendDidChangeTextDocumentAsync(uriFob, "from baz import C");
53705374

53715375
references = await server.SendFindReferences(uriFob, 0, 17);
53725376
references.Should().OnlyHaveReferences(
@@ -5409,7 +5413,8 @@ import fob.y as y
54095413
await server.WaitForCompleteAnalysisAsync(CancellationToken.None);
54105414
var analysis = await server.GetAnalysisAsync(uriSrc2);
54115415

5412-
analysis.Should().HaveVariable("y").WithDescription("Python module fob.y")
5416+
analysis.Should()
5417+
.HaveVariable("y").WithDescription("Python module fob.y")
54135418
.And.HaveVariable("abc").OfType(BuiltinTypeId.Int);
54145419
}
54155420
}
@@ -7662,7 +7667,7 @@ private async Task PermutedTestAsync(string prefix, string[] code, Action<IReadO
76627667
var content = code[p[i]];
76637668
var filename = name.Replace('.', '\\') + ".py";
76647669

7665-
entries[p[i]] = server.AddModuleWithContent(name, filename, content);
7670+
entries[p[i]] = await server.AddModuleWithContentAsync(name, filename, content);
76667671
}
76677672

76687673
var analysis = entries

src/Analysis/Engine/Test/LanguageServerTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ params DocumentChange[] e
205205

206206
var parseComplete = EventTaskSources.Server.OnParseComplete.Create(s);
207207

208-
s.DidChangeTextDocument(new DidChangeTextDocumentParams {
208+
await s.DidChangeTextDocument(new DidChangeTextDocumentParams {
209209
textDocument = new VersionedTextDocumentIdentifier {
210210
uri = document,
211211
version = finalVersion,
@@ -214,7 +214,7 @@ params DocumentChange[] e
214214
range = c.WholeBuffer ? null : (Range?)c.ReplacedSpan,
215215
text = c.InsertedText
216216
}).ToArray()
217-
});
217+
}, CancellationToken.None);
218218

219219
await parseComplete;
220220

@@ -576,7 +576,7 @@ await AssertCompletion(s, mod,
576576
);
577577

578578
// Send the document update.
579-
s.DidChangeTextDocument(new DidChangeTextDocumentParams {
579+
await s.DidChangeTextDocument(new DidChangeTextDocumentParams {
580580
textDocument = new VersionedTextDocumentIdentifier { uri = mod, version = 1 },
581581
contentChanges = new[] { new TextDocumentContentChangedEvent {
582582
text = ".",
@@ -587,7 +587,7 @@ await AssertCompletion(s, mod,
587587
} },
588588
// Suppress reanalysis to avoid a race
589589
_enqueueForAnalysis = false
590-
});
590+
}, CancellationToken.None);
591591

592592
// Now with the "." event sent, we should see this as a dot completion
593593
await AssertCompletion(s, mod,
@@ -957,14 +957,14 @@ public async Task ParseIndentationDiagnostics() {
957957
Trace.TraceInformation("Testing {0}", tc);
958958

959959
var mod = await AddModule(s, "");
960-
s.DidChangeTextDocument(new DidChangeTextDocumentParams {
960+
await s.DidChangeTextDocument(new DidChangeTextDocumentParams {
961961
contentChanges = new[] {
962962
new TextDocumentContentChangedEvent {
963963
text = "def f():\r\n pass\r\n\tpass"
964964
}
965965
},
966966
textDocument = new VersionedTextDocumentIdentifier { uri = mod, version = 2 }
967-
});
967+
}, CancellationToken.None);
968968
await s.WaitForCompleteAnalysisAsync(CancellationToken.None);
969969

970970
var messages = GetDiagnostics(diags, mod).ToArray();

src/Analysis/Engine/Test/ServerExtensions.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,20 @@ await server.Initialize(new InitializeParams {
6464
public static Task<IModuleAnalysis> GetAnalysisAsync(this Server server, Uri uri, int waitingTimeout = -1, int failAfter = 30000)
6565
=> ((ProjectEntry)server.ProjectFiles.GetEntry(uri)).GetAnalysisAsync(waitingTimeout, new CancellationTokenSource(failAfter).Token);
6666

67-
public static void EnqueueItem(this Server server, Uri uri)
68-
=> server.EnqueueItem((IDocument)server.ProjectFiles.GetEntry(uri));
67+
public static Task EnqueueItemAsync(this Server server, Uri uri)
68+
=> server.EnqueueItemAsync((IDocument)server.ProjectFiles.GetEntry(uri));
6969

70-
public static void EnqueueItems(this Server server, params IDocument[] projectEntries) {
70+
public static async Task EnqueueItemsAsync(this Server server, CancellationToken cancellationToken, params IDocument[] projectEntries) {
7171
foreach (var document in projectEntries) {
72-
server.EnqueueItem(document);
72+
await server.EnqueueItemAsync(document);
7373
}
7474
}
7575

7676
// TODO: Replace usages of AddModuleWithContent with OpenDefaultDocumentAndGetUriAsync
77-
public static ProjectEntry AddModuleWithContent(this Server server, string moduleName, string relativePath, string content) {
77+
public static async Task<ProjectEntry> AddModuleWithContentAsync(this Server server, string moduleName, string relativePath, string content) {
7878
var entry = (ProjectEntry)server.Analyzer.AddModule(moduleName, TestData.GetTestSpecificPath(relativePath));
7979
entry.ResetDocument(0, content);
80-
server.EnqueueItem(entry);
80+
await server.EnqueueItemAsync(entry);
8181
return entry;
8282
}
8383

@@ -153,8 +153,8 @@ public static async Task<IModuleAnalysis> OpenDefaultDocumentAndGetAnalysisAsync
153153
return await projectEntry.GetAnalysisAsync(cancellationToken: cancellationToken);
154154
}
155155

156-
public static void SendDidChangeTextDocument(this Server server, Uri uri, string text) {
157-
server.DidChangeTextDocument(new DidChangeTextDocumentParams {
156+
public static Task SendDidChangeTextDocumentAsync(this Server server, Uri uri, string text) {
157+
return server.DidChangeTextDocument(new DidChangeTextDocumentParams {
158158
textDocument = new VersionedTextDocumentIdentifier {
159159
uri = uri
160160
},
@@ -163,12 +163,12 @@ public static void SendDidChangeTextDocument(this Server server, Uri uri, string
163163
text = text,
164164
},
165165
}
166-
});
166+
}, CancellationToken.None);
167167
}
168168

169169
public static async Task<IModuleAnalysis> ChangeDefaultDocumentAndGetAnalysisAsync(this Server server, string text, int failAfter = 30000) {
170170
var projectEntry = (ProjectEntry) server.ProjectFiles.All.Single();
171-
server.SendDidChangeTextDocument(projectEntry.DocumentUri, text);
171+
await server.SendDidChangeTextDocumentAsync(projectEntry.DocumentUri, text);
172172
return await projectEntry.GetAnalysisAsync(cancellationToken: new CancellationTokenSource(failAfter).Token);
173173
}
174174

0 commit comments

Comments
 (0)