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

Commit 9b5c74f

Browse files
author
Mikhail Arkhipov
committed
Fix gotodef test
1 parent 5db4970 commit 9b5c74f

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/Analysis/Ast/Impl/Analyzer/Handlers/FromImportHandler.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ private void AssignVariables(FromImportStatement node, IImportSearchResult impor
6969
DeclareVariable(variableModule, memberName, imports, variableName, node.StartIndex, nameExpression);
7070
}
7171

72-
if (imports is IImportChildrenSource cs
73-
&& cs.TryGetChildImport(memberName, out var csr)
72+
if (imports is IImportChildrenSource cs
73+
&& cs.TryGetChildImport(memberName, out var csr)
7474
&& HandleImportSearchResult(csr, variableModule, null, names[i], out var childModule)) {
7575

7676
_importedVariableHandler.EnsureModule(childModule);
@@ -119,17 +119,31 @@ private void DeclareVariable(PythonVariableModule variableModule, string memberN
119119
// Value may be variable or submodule. If it is variable, we need it in order to add reference.
120120
var variable = _importedVariableHandler.GetVariable(variableModule, memberName);
121121

122-
// If nothing is exported, variables are still accessible.
123-
value = value ?? member as PythonVariableModule ?? (variable?.Value != null ? variable : member) ?? Eval.UnknownType;
122+
if (member is PythonVariableModule vm && vm.Equals(variable?.Value)) {
123+
// If member is submodule, use actual variable so it can be linked through for goto definition.
124+
value = variable;
125+
} else if (value == null) {
126+
if (member is PythonVariableModule) {
127+
// If member is submodule, use it.
128+
value = member;
129+
} else if (variable?.Value != null) {
130+
// Otherwise use variable, if available so references can be linked.
131+
value = variable;
132+
} else if (member != null) {
133+
value = member;
134+
} else {
135+
value = Eval.UnknownType;
136+
}
137+
}
124138
}
125139

126140
// Do not allow imported variables to override local declarations
127141
var canOverwrite = CanOverwriteVariable(variableName, importPosition, value);
128-
142+
129143
// Do not declare references to '*'
130144
var locationExpression = nameLocation is NameExpression nex && nex.Name == "*" ? null : nameLocation;
131145
Eval.DeclareVariable(variableName, value, VariableSource.Import, locationExpression, canOverwrite);
132-
146+
133147
// Make sure module is loaded and analyzed.
134148
if (value is IPythonModule m) {
135149
ModuleResolution.GetOrLoadModule(m.Name);
@@ -141,8 +155,8 @@ private bool CanOverwriteVariable(string name, int importPosition, IMember newVa
141155
if (v == null) {
142156
return true; // Variable does not exist
143157
}
144-
145-
if(newValue.IsUnknown()) {
158+
159+
if (newValue.IsUnknown()) {
146160
return false; // Do not overwrite potentially good value with unknowns.
147161
}
148162

src/LanguageServer/Test/GoToDefinitionTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,13 +525,15 @@ from os import path as os_path
525525

526526
reference = ds.FindDefinition(analysis, new SourceLocation(4, 12), out _);
527527
reference.Should().NotBeNull();
528-
line = File.ReadAllLines(reference.uri.AbsolutePath)[reference.range.start.line];
528+
var osPyPath = reference.uri.AbsolutePath;
529+
line = File.ReadAllLines(osPyPath)[reference.range.start.line];
529530
line.Should().EndWith("as path");
530531
line.Substring(reference.range.start.character).Should().Be("path");
531532

532533
reference = ds.FindDefinition(analysis, new SourceLocation(5, 12), out _);
533534
reference.Should().NotBeNull();
534-
line = File.ReadAllLines(reference.uri.AbsolutePath)[reference.range.start.line];
535+
reference.uri.AbsolutePath.Should().Be(osPyPath);
536+
line = File.ReadAllLines(osPyPath)[reference.range.start.line];
535537
line.Should().EndWith("as path");
536538
line.Substring(reference.range.start.character).Should().Be("path");
537539
}

0 commit comments

Comments
 (0)