Skip to content

Commit 31fe80d

Browse files
author
Mikhail Arkhipov
committed
Fix typeshed path
1 parent 39690f8 commit 31fe80d

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

src/Analysis/Ast/Impl/Analyzer/StubMerger.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ private void TryReplaceMember(IVariable v, IPythonType sourceType, IPythonType s
149149
MergeMembers(v, sourceFunction, stubType, cancellationToken);
150150
break;
151151

152+
case PythonPropertyType sourceProperty:
153+
MergeMembers(v, sourceProperty, stubType, cancellationToken);
154+
break;
155+
152156
case IPythonModule _:
153157
// We do not re-declare modules.
154158
break;
@@ -203,7 +207,7 @@ private void MergeMembers(IVariable v, IPythonType sourceType, IPythonType stubT
203207
continue; // Do not add unknowns to the stub.
204208
}
205209
var sourceMemberType = sourceMember?.GetPythonType();
206-
if (sourceMemberType is IPythonClassMember cm && cm.DeclaringType != sourceType) {
210+
if (sourceMemberType is IPythonClassMember cm && !cm.DeclaringModule.Equals(sourceType.DeclaringModule)) {
207211
continue; // Only take members from this class and not from bases.
208212
}
209213
if (!IsFromThisModuleOrSubmodules(sourceMemberType)) {

src/Analysis/Ast/Impl/Modules/Resolution/TypeshedResolution.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ internal sealed class TypeshedResolution : ModuleResolutionBase, IModuleResoluti
3232

3333
public TypeshedResolution(string root, IServiceContainer services) : base(root, services) {
3434
// TODO: merge with user-provided stub paths
35-
var stubs = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Stubs");
35+
var asmLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
36+
var stubs = Path.Combine(asmLocation, "Stubs");
37+
var typeshed = Path.Combine(asmLocation, "Typeshed");
38+
3639
_typeStubPaths = GetTypeShedPaths(Root)
40+
.Concat(GetTypeShedPaths(typeshed))
3741
.Concat(GetTypeShedPaths(stubs))
3842
.Where(services.GetService<IFileSystem>().DirectoryExists)
3943
.ToImmutableArray();

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,23 @@ public async Task<Reference[]> GotoDefinition(TextDocumentPositionParams @params
8989
_log?.Log(TraceEventType.Verbose, $"Goto Definition in {uri} at {@params.position}");
9090

9191
var analysis = await Document.GetAnalysisAsync(uri, Services, CompletionAnalysisTimeout, cancellationToken);
92-
var reference = new DefinitionSource(Services).FindDefinition(analysis, @params.position, out _);
93-
return reference != null ? new[] { reference } : Array.Empty<Reference>();
92+
var ds = new DefinitionSource(Services);
93+
var reference = ds.FindDefinition(analysis, @params.position, out _);
94+
return reference != null && ds.CanNavigateToModule(reference.uri)
95+
? new[] { reference }
96+
: Array.Empty<Reference>();
9497
}
9598

9699
public async Task<Location> GotoDeclaration(TextDocumentPositionParams @params, CancellationToken cancellationToken) {
97100
var uri = @params.textDocument.uri;
98101
_log?.Log(TraceEventType.Verbose, $"Goto Declaration in {uri} at {@params.position}");
99102

100103
var analysis = await Document.GetAnalysisAsync(uri, Services, CompletionAnalysisTimeout, cancellationToken);
101-
var reference = new DeclarationSource(Services).FindDefinition(analysis, @params.position, out _);
102-
return reference != null ? new Location { uri = reference.uri, range = reference.range } : null;
104+
var ds = new DeclarationSource(Services);
105+
var reference = ds.FindDefinition(analysis, @params.position, out _);
106+
return reference != null && ds.CanNavigateToModule(reference.uri)
107+
? new Location { uri = reference.uri, range = reference.range }
108+
: null;
103109
}
104110

105111
public Task<Reference[]> FindReferences(ReferencesParams @params, CancellationToken cancellationToken) {
@@ -137,8 +143,8 @@ public async Task<CodeAction[]> CodeAction(CodeActionParams @params, Cancellatio
137143

138144
return codeActions.ToArray();
139145

140-
static bool AskedFor(CodeActionParams @params, string codeActionKind) {
141-
return @params.context.only == null || @params.context.only.Any(s => s.StartsWith(codeActionKind));
146+
bool AskedFor(CodeActionParams p, string codeActionKind) {
147+
return p.context.only == null || p.context.only.Any(s => s.StartsWith(codeActionKind));
142148
}
143149
}
144150
}

src/LanguageServer/Impl/Sources/DefinitionSource.cs

+18-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// permissions and limitations under the License.
1515

1616
using System;
17+
using System.IO;
1718
using System.Linq;
1819
using Microsoft.Python.Analysis;
1920
using Microsoft.Python.Analysis.Analyzer;
@@ -360,22 +361,32 @@ private Reference FromMember(IMember m) {
360361
return null;
361362
}
362363

363-
private bool CanNavigateToModule(Uri uri) {
364+
public bool CanNavigateToModule(Uri uri) {
364365
if (uri == null) {
365366
return false;
366367
}
368+
369+
if (!CanNavigateToPath(uri.LocalPath)) {
370+
return false;
371+
}
367372
var rdt = _services.GetService<IRunningDocumentTable>();
368373
var doc = rdt.GetDocument(uri);
369374
// Allow navigation to modules not in RDT - most probably
370375
// it is a module that was restored from database.
371376
return doc == null || CanNavigateToModule(doc);
372377
}
373378

374-
private static bool CanNavigateToModule(IPythonModule m)
375-
=> m?.ModuleType == ModuleType.User ||
376-
m?.ModuleType == ModuleType.Stub ||
377-
m?.ModuleType == ModuleType.Package ||
378-
m?.ModuleType == ModuleType.Library ||
379-
m?.ModuleType == ModuleType.Specialized;
379+
private static bool CanNavigateToModule(IPythonModule m) {
380+
if(m == null || !CanNavigateToPath(m.FilePath)) {
381+
return false;
382+
}
383+
return m.ModuleType == ModuleType.User ||
384+
m.ModuleType == ModuleType.Stub ||
385+
m.ModuleType == ModuleType.Package ||
386+
m.ModuleType == ModuleType.Library ||
387+
m.ModuleType == ModuleType.Specialized;
388+
}
389+
390+
private static bool CanNavigateToPath(string path) => Path.GetExtension(path) != ".exe";
380391
}
381392
}

0 commit comments

Comments
 (0)