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

Commit d811d02

Browse files
Merge pull request #361 from AlexanderSher/NamespacePackages
Use PathResolverSnapshot to find modules.
2 parents 5131b37 + 784311f commit d811d02

File tree

67 files changed

+2832
-2114
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+2832
-2114
lines changed

src/Analysis/Engine/Impl/Analyzer/DDG.cs

+177-206
Large diffs are not rendered by default.

src/Analysis/Engine/Impl/Analyzer/ImportStatementWalker.cs

-112
This file was deleted.

src/Analysis/Engine/Impl/Analyzer/ModuleScope.cs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
namespace Microsoft.PythonTools.Analysis.Analyzer {
2020
sealed class ModuleScope : InterpreterScope, IModuleScope {
21-
2221
public ModuleScope(ModuleInfo moduleInfo)
2322
: base(moduleInfo, null) {
2423
}

src/Analysis/Engine/Impl/Definitions/IPythonAnalyzer.cs

-27
Original file line numberDiff line numberDiff line change
@@ -67,38 +67,11 @@ public interface IPythonAnalyzer: IGroupableAnalysisProject {
6767

6868
AnalysisValue GetAnalysisValueFromObjects(object attr);
6969

70-
/// <summary>
71-
/// Returns true if a module has been imported.
72-
/// </summary>
73-
/// <param name="importFrom">
74-
/// The entry of the module doing the import. If null, the module name
75-
/// is resolved as an absolute name.
76-
/// </param>
77-
/// <param name="relativeModuleName">
78-
/// The absolute or relative name of the module. If a relative name is
79-
/// passed here, <paramref name="importFrom"/> must be provided.
80-
/// </param>
81-
/// <param name="absoluteImports">
82-
/// True if Python 2.6/3.x style imports should be used.
83-
/// </param>
84-
/// <returns>
85-
/// True if the module was imported during analysis; otherwise, false.
86-
/// </returns>
87-
bool IsModuleResolved(IPythonProjectEntry importFrom, string relativeModuleName, bool absoluteImports);
88-
8970
/// <summary>
9071
/// Gets a top-level list of all the available modules as a list of MemberResults.
9172
/// </summary>
9273
IMemberResult[] GetModules();
9374

94-
/// <summary>
95-
/// Searches all modules which match the given name and searches in the modules
96-
/// for top-level items which match the given name. Returns a list of all the
97-
/// available names fully qualified to their name.
98-
/// </summary>
99-
/// <param name="name"></param>
100-
IEnumerable<ExportedMemberInfo> FindNameInAllModules(string name);
101-
10275
/// <summary>
10376
/// Returns the interpreter that the analyzer is using.
10477
/// This property is thread safe.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Python Tools for Visual Studio
2+
// Copyright(c) Microsoft Corporation
3+
// All rights reserved.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the License); you may not use
6+
// this file except in compliance with the License. You may obtain a copy of the
7+
// License at http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
10+
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
11+
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
12+
// MERCHANTABILITY OR NON-INFRINGEMENT.
13+
//
14+
// See the Apache Version 2.0 License for specific language governing
15+
// permissions and limitations under the License.
16+
17+
using System.Linq;
18+
using Microsoft.PythonTools.Parsing.Ast;
19+
20+
namespace Microsoft.PythonTools.Analysis.DependencyResolution {
21+
internal static class AstUtilities {
22+
public static IImportSearchResult FindImports(this PathResolverSnapshot pathResolver, string modulePath, FromImportStatement fromImportStatement) {
23+
var rootNames = fromImportStatement.Root.Names.Select(n => n.Name);
24+
return fromImportStatement.Root is RelativeModuleName relativeName
25+
? pathResolver.GetImportsFromRelativePath(modulePath, relativeName.DotCount, rootNames)
26+
: pathResolver.GetImportsFromAbsoluteName(modulePath, rootNames, fromImportStatement.ForceAbsolute);
27+
}
28+
}
29+
}

src/Analysis/Engine/Impl/DependencyResolution/IAvailableImports.cs renamed to src/Analysis/Engine/Impl/DependencyResolution/IImportSearchResult.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,5 @@
1515
// permissions and limitations under the License.
1616

1717
namespace Microsoft.PythonTools.Analysis.DependencyResolution {
18-
internal interface IAvailableImports {
19-
string Name { get; }
20-
}
21-
}
18+
internal interface IImportSearchResult {}
19+
}

src/Analysis/Engine/Impl/DependencyResolution/AvailableModuleImports.cs renamed to src/Analysis/Engine/Impl/DependencyResolution/ImportNotFound.cs

+5-8
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
// permissions and limitations under the License.
1616

1717
namespace Microsoft.PythonTools.Analysis.DependencyResolution {
18-
internal class AvailableModuleImports : IAvailableImports {
19-
public string Name { get; }
20-
public string ModulePath { get; }
21-
22-
public AvailableModuleImports(string name, string modulePath) {
23-
Name = name;
24-
ModulePath = modulePath;
18+
internal class ImportNotFound : IImportSearchResult {
19+
public string FullName { get; }
20+
public ImportNotFound(string fullName) {
21+
FullName = fullName;
2522
}
2623
}
27-
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Python Tools for Visual Studio
2+
// Copyright(c) Microsoft Corporation
3+
// All rights reserved.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the License); you may not use
6+
// this file except in compliance with the License. You may obtain a copy of the
7+
// License at http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
10+
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
11+
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
12+
// MERCHANTABILITY OR NON-INFRINGEMENT.
13+
//
14+
// See the Apache Version 2.0 License for specific language governing
15+
// permissions and limitations under the License.
16+
17+
namespace Microsoft.PythonTools.Analysis.DependencyResolution {
18+
internal class ModuleImport : IImportSearchResult {
19+
public string Name { get; }
20+
public string FullName { get; }
21+
public string RootPath { get; }
22+
public string ModulePath { get; }
23+
public bool IsCompiled { get; }
24+
public bool IsBuiltin => IsCompiled && ModulePath == null;
25+
26+
public ModuleImport(string name, string fullName, string rootPath, string modulePath, bool isCompiled) {
27+
Name = name;
28+
FullName = fullName;
29+
RootPath = rootPath;
30+
ModulePath = modulePath;
31+
IsCompiled = isCompiled;
32+
}
33+
}
34+
}

src/Analysis/Engine/Impl/DependencyResolution/AvailablePackageImports.cs renamed to src/Analysis/Engine/Impl/DependencyResolution/PackageImport.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
using System.Collections.Generic;
1919

2020
namespace Microsoft.PythonTools.Analysis.DependencyResolution {
21-
internal class AvailablePackageImports : IAvailableImports {
21+
internal class PackageImport : IImportSearchResult {
2222
public string Name { get; }
23-
public IReadOnlyDictionary<string, string> Modules { get; }
23+
public ModuleImport[] Modules { get; }
2424
public string[] Packages { get; }
2525

26-
public AvailablePackageImports(string name, IReadOnlyDictionary<string, string> modules, string[] packages) {
26+
public PackageImport(string name, ModuleImport[] modules, string[] packages) {
2727
Name = name;
2828
Modules = modules;
2929
Packages = packages;
3030
}
3131
}
32-
}
32+
}

src/Analysis/Engine/Impl/DependencyResolution/PathResolver.cs

+23-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// permissions and limitations under the License.
1616

1717
using System.Collections.Generic;
18+
using System.Linq;
1819
using Microsoft.PythonTools.Parsing;
1920

2021
namespace Microsoft.PythonTools.Analysis.DependencyResolution {
@@ -25,9 +26,28 @@ public PathResolver(PythonLanguageVersion pythonLanguageVersion) {
2526
_currentSnapshot = new PathResolverSnapshot(pythonLanguageVersion);
2627
}
2728

28-
public void SetRoot(string root) => _currentSnapshot = _currentSnapshot.SetRoot(root);
29-
public void SetSearchPaths(IEnumerable<string> searchPaths) => _currentSnapshot = _currentSnapshot.SetSearchPaths(searchPaths);
30-
public void AddModulePath(string path) => _currentSnapshot = _currentSnapshot.AddModulePath(path);
29+
public IEnumerable<string> SetRoot(in string root) {
30+
_currentSnapshot = _currentSnapshot.SetWorkDirectory(root, out var addedRoots);
31+
return addedRoots;
32+
}
33+
34+
public IEnumerable<string> SetUserSearchPaths(in IEnumerable<string> searchPaths) {
35+
_currentSnapshot = _currentSnapshot.SetUserSearchPaths(searchPaths, out var addedRoots);
36+
return addedRoots;
37+
}
38+
39+
public IEnumerable<string> SetInterpreterSearchPaths(in IEnumerable<string> searchPaths) {
40+
_currentSnapshot = _currentSnapshot.SetInterpreterPaths(searchPaths, out var addedRoots);
41+
return addedRoots;
42+
}
43+
44+
public void SetBuiltins(in IEnumerable<string> builtinModuleNames) => _currentSnapshot = _currentSnapshot.SetBuiltins(builtinModuleNames);
45+
public void RemoveModulePath(in string path) => _currentSnapshot = _currentSnapshot.RemoveModulePath(path);
46+
public bool TryAddModulePath(in string path, out string fullModuleName) {
47+
_currentSnapshot = _currentSnapshot.AddModulePath(path, out fullModuleName);
48+
return fullModuleName != null;
49+
}
50+
3151
public PathResolverSnapshot CurrentSnapshot => _currentSnapshot;
3252
}
3353
}

0 commit comments

Comments
 (0)