Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Analysis/Engine/Impl/ModuleAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,23 @@ private VariablesResult GetVariablesFromNameExpression(Expression expr, Analysis
mainDefinition = mainDefinition ?? definitions.FirstOrDefault();
if (mainDefinition != null) {
// Drop definitions in outer scopes and convert those in inner scopes to references.
// Exception is when variable is an import as in
// abc = 1
// import abc
var imports = definitions
.Where(d => d.Variable.Variable.Types.Any(t => t.TypeId == BuiltinTypeId.Module) && d.Location.DocumentUri != DocumentUri)
.ToArray();

// Scope levels are numbered in reverse (X == main definition level, x+1 == one up).
var defsToRefs = definitions
.Except(imports)
.Where(d => d != mainDefinition && d.ScopeLevel <= mainDefinition.ScopeLevel)
.Select(v => new VariableScopePair(new AnalysisVariable(v.Variable.Variable, VariableType.Reference, v.Location), v.ScopeLevel));

var others = variables
.Where(v => (v.VariableType == VariableType.Reference || v.VariableType == VariableType.Value) &&
v.ScopeLevel <= mainDefinition.ScopeLevel);
variables = new[] { mainDefinition }.Concat(others.Concat(defsToRefs));
variables = new[] { mainDefinition }.Concat(imports).Concat(others.Concat(defsToRefs));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it just rearrangement, or some of the variables will be excluded?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No exclusion, just keeping imports as definitions. Before they were converted to references since they were below in the same scope such as

abc = 1
import abc

}

return new VariablesResult(variables.Select(v => v.Variable), unit.Tree);
Expand Down
13 changes: 9 additions & 4 deletions src/Analysis/Engine/Test/AnalysisTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
using Microsoft.Python.UnitTests.Core.MSTest;
using Microsoft.PythonTools.Analysis;
using Microsoft.PythonTools.Analysis.FluentAssertions;
using Microsoft.PythonTools.Analysis.Infrastructure;
using Microsoft.PythonTools.Analysis.Values;
using Microsoft.PythonTools.Interpreter;
using Microsoft.PythonTools.Interpreter.Ast;
Expand Down Expand Up @@ -2936,7 +2935,6 @@ print abc
}

[TestMethod, Priority(0)]
[Ignore("https://github.com/Microsoft/python-language-server/issues/40")]
public async Task References_GrammarTest_Statements() {
using (var server = await CreateServerAsync(PythonVersions.LatestAvailable2X)) {
var uri = TestData.GetDefaultModuleUri();
Expand Down Expand Up @@ -2992,12 +2990,19 @@ print abc
await server.SendDidOpenTextDocument(uri, text);

var references = await server.SendFindReferences(uri, 3, 12);

// External module 'abc', URI varies depending on install
var externalUri = references[1].uri;
externalUri.LocalPath.Should().EndWith("abc.py");

references.Should().OnlyHaveReferences(
(uri, (1, 6, 1, 9), ReferenceKind.Definition),
(externalUri, (0, 0, 0, 0), ReferenceKind.Definition),

(uri, (3, 11, 3, 14), ReferenceKind.Reference),
(uri, (6, 22, 6, 25), ReferenceKind.Definition),
(uri, (6, 22, 6, 25), ReferenceKind.Reference),

(uri, (8, 4, 8, 7), ReferenceKind.Definition),
(uri, (8, 4, 8, 7), ReferenceKind.Reference),
(uri, (9, 4, 9, 7), ReferenceKind.Reference),
(uri, (10, 4, 10, 7), ReferenceKind.Reference),
(uri, (11, 4, 11, 7), ReferenceKind.Reference),
Expand Down
1 change: 0 additions & 1 deletion src/Analysis/Engine/Test/AstAnalysisTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,6 @@ public async Task FullStdLibV37() {


[TestMethod, TestCategory("60s"), Priority(0)]
[Ignore("https://github.com/Microsoft/python-language-server/issues/64")]
public async Task FullStdLibV36() {
var v = PythonVersions.Python36 ?? PythonVersions.Python36_x64;
await FullStdLibTest(v);
Expand Down