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
Show all changes
20 commits
Select commit Hold shift + click to select a range
c6a9c22
Remove stale reference
Sep 30, 2019
1360827
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 1, 2019
ccaaa02
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 4, 2019
da40dcc
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 4, 2019
c348ac3
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 5, 2019
53bc044
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 7, 2019
484a92a
Merge branch 'master' of https://github.com/MikhailArkhipov/python-la…
Oct 7, 2019
e6df3aa
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 8, 2019
1d289d8
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 8, 2019
126f355
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 12, 2019
7e715f3
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 25, 2019
32923a5
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 31, 2019
1b72f4b
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 2, 2019
f74d5b6
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 7, 2019
0b28fa4
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 12, 2019
6109ac7
Don't suppress LHS diagnostics on augmented assign
Nov 12, 2019
88ece8d
Look for module in RDT
Nov 12, 2019
2511bc5
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 13, 2019
c9abbb7
Less nesting
Nov 13, 2019
72554e3
Add test
Nov 13, 2019
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
41 changes: 26 additions & 15 deletions src/LanguageServer/Impl/Sources/ReferenceSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,35 @@ public ReferenceSource(IServiceContainer services) {
}

public async Task<Reference[]> FindAllReferencesAsync(Uri uri, SourceLocation location, ReferenceSearchOptions options, CancellationToken cancellationToken = default) {
if (uri != null) {
var analysis = await Document.GetAnalysisAsync(uri, _services, FindReferencesAnalysisTimeout, cancellationToken);
if (uri == null) {
return Array.Empty<Reference>();
}
var analysis = await Document.GetAnalysisAsync(uri, _services, FindReferencesAnalysisTimeout, cancellationToken);

var definitionSource = new DefinitionSource(_services);
var definition = definitionSource.FindDefinition(analysis, location, out var definingMember);
if (definition == null) {
return Array.Empty<Reference>();
}
var definitionSource = new DefinitionSource(_services);
var definition = definitionSource.FindDefinition(analysis, location, out var definingMember);
if (definition == null) {
return Array.Empty<Reference>();
}

var rootDefinition = definingMember.GetRootDefinition();
var name = definingMember.GetName();
var rootDefinition = definingMember.GetRootDefinition();
if (rootDefinition == null) {
return Array.Empty<Reference>();
}

var name = definingMember.GetName();
var moduleUri = rootDefinition.Definition?.DocumentUri;
if (moduleUri == null) {
return Array.Empty<Reference>();
}

var rdt = _services.GetService<IRunningDocumentTable>();
var declaringModule = rdt.GetDocument(moduleUri);

// If it is an implicitly declared variable, such as function or a class
// then the location is invalid and the module is null. Use current module.
var declaringModule = rootDefinition?.DeclaringModule ?? analysis.Document;
if (!string.IsNullOrEmpty(name) && (declaringModule.ModuleType == ModuleType.User || options == ReferenceSearchOptions.All)) {
return await FindAllReferencesAsync(name, declaringModule, rootDefinition, location, definitionSource, cancellationToken);
}
// If it is an implicitly declared variable, such as function or a class
// then the location is invalid and the module is null. Use current module.
if (!string.IsNullOrEmpty(name) && (declaringModule.ModuleType == ModuleType.User || options == ReferenceSearchOptions.All)) {
return await FindAllReferencesAsync(name, declaringModule, rootDefinition, location, definitionSource, cancellationToken);
}
return Array.Empty<Reference>();
}
Expand Down
15 changes: 14 additions & 1 deletion src/LanguageServer/Test/RenameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public async Task NoRenameInCompiled() {
}

[TestMethod, Priority(0)]
public async Task NoRenameInLibrary() {
public async Task NoRenameInSystemLibrary() {
const string code = @"from logging import BASIC_FORMAT";
var analysis = await GetAnalysisAsync(code, PythonVersions.LatestAvailable3X);
var rs = new RenameSource(Services);
Expand All @@ -231,5 +231,18 @@ public async Task NoRenameInLibrary() {
wse = await rs.RenameAsync(analysis.Document.Uri, new SourceLocation(1, 23), "z");
wse.Should().BeNull();
}

[TestMethod, Priority(0)]
public async Task NoRenameInUserLibrary() {
const string code = @"
import Library1
Library1.make_foo()
";
var analysis = await GetAnalysisAsync(code, PythonVersions.LatestAvailable3X);
var rs = new RenameSource(Services);

var wse = await rs.RenameAsync(analysis.Document.Uri, new SourceLocation(3, 16), "z");
wse.Should().BeNull();
}
}
}
14 changes: 14 additions & 0 deletions src/UnitTests/TestData/AstAnalysis/Library1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Foo():
"""This is Foo and the docs should be here"""
def __init__(self):
self.x = 1234
self.y = "str"


def make_foo():
"""This is make_foo and the docs should be here"""
return Foo()

def print_foo(f):
"""This is print_foo and the docs should be here"""
print(f"{f.x}, {f.y}")