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

Use PathResolverSnapshot to find modules. #361

Merged
merged 8 commits into from
Dec 4, 2018
383 changes: 177 additions & 206 deletions src/Analysis/Engine/Impl/Analyzer/DDG.cs

Large diffs are not rendered by default.

112 changes: 0 additions & 112 deletions src/Analysis/Engine/Impl/Analyzer/ImportStatementWalker.cs

This file was deleted.

1 change: 0 additions & 1 deletion src/Analysis/Engine/Impl/Analyzer/ModuleScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

namespace Microsoft.PythonTools.Analysis.Analyzer {
sealed class ModuleScope : InterpreterScope, IModuleScope {

public ModuleScope(ModuleInfo moduleInfo)
: base(moduleInfo, null) {
}
Expand Down
27 changes: 0 additions & 27 deletions src/Analysis/Engine/Impl/Definitions/IPythonAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,38 +67,11 @@ public interface IPythonAnalyzer: IGroupableAnalysisProject {

AnalysisValue GetAnalysisValueFromObjects(object attr);

/// <summary>
/// Returns true if a module has been imported.
/// </summary>
/// <param name="importFrom">
/// The entry of the module doing the import. If null, the module name
/// is resolved as an absolute name.
/// </param>
/// <param name="relativeModuleName">
/// The absolute or relative name of the module. If a relative name is
/// passed here, <paramref name="importFrom"/> must be provided.
/// </param>
/// <param name="absoluteImports">
/// True if Python 2.6/3.x style imports should be used.
/// </param>
/// <returns>
/// True if the module was imported during analysis; otherwise, false.
/// </returns>
bool IsModuleResolved(IPythonProjectEntry importFrom, string relativeModuleName, bool absoluteImports);

/// <summary>
/// Gets a top-level list of all the available modules as a list of MemberResults.
/// </summary>
IMemberResult[] GetModules();

/// <summary>
/// Searches all modules which match the given name and searches in the modules
/// for top-level items which match the given name. Returns a list of all the
/// available names fully qualified to their name.
/// </summary>
/// <param name="name"></param>
IEnumerable<ExportedMemberInfo> FindNameInAllModules(string name);

/// <summary>
/// Returns the interpreter that the analyzer is using.
/// This property is thread safe.
Expand Down
29 changes: 29 additions & 0 deletions src/Analysis/Engine/Impl/DependencyResolution/AstUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Python Tools for Visual Studio
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System.Linq;
using Microsoft.PythonTools.Parsing.Ast;

namespace Microsoft.PythonTools.Analysis.DependencyResolution {
internal static class AstUtilities {
public static IImportSearchResult FindImports(this PathResolverSnapshot pathResolver, string modulePath, FromImportStatement fromImportStatement) {
var rootNames = fromImportStatement.Root.Names.Select(n => n.Name);
return fromImportStatement.Root is RelativeModuleName relativeName
? pathResolver.GetImportsFromRelativePath(modulePath, relativeName.DotCount, rootNames)
: pathResolver.GetImportsFromAbsoluteName(modulePath, rootNames, fromImportStatement.ForceAbsolute);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,5 @@
// permissions and limitations under the License.

namespace Microsoft.PythonTools.Analysis.DependencyResolution {
internal interface IAvailableImports {
string Name { get; }
}
}
internal interface IImportSearchResult {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@
// permissions and limitations under the License.

namespace Microsoft.PythonTools.Analysis.DependencyResolution {
internal class AvailableModuleImports : IAvailableImports {
public string Name { get; }
public string ModulePath { get; }

public AvailableModuleImports(string name, string modulePath) {
Name = name;
ModulePath = modulePath;
internal class ImportNotFound : IImportSearchResult {
public string FullName { get; }
public ImportNotFound(string fullName) {
FullName = fullName;
}
}
}
}
34 changes: 34 additions & 0 deletions src/Analysis/Engine/Impl/DependencyResolution/ModuleImport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Python Tools for Visual Studio
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

namespace Microsoft.PythonTools.Analysis.DependencyResolution {
internal class ModuleImport : IImportSearchResult {
public string Name { get; }
public string FullName { get; }
public string RootPath { get; }
public string ModulePath { get; }
public bool IsCompiled { get; }
public bool IsBuiltin => IsCompiled && ModulePath == null;

public ModuleImport(string name, string fullName, string rootPath, string modulePath, bool isCompiled) {
Name = name;
FullName = fullName;
RootPath = rootPath;
ModulePath = modulePath;
IsCompiled = isCompiled;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
using System.Collections.Generic;

namespace Microsoft.PythonTools.Analysis.DependencyResolution {
internal class AvailablePackageImports : IAvailableImports {
internal class PackageImport : IImportSearchResult {
public string Name { get; }
public IReadOnlyDictionary<string, string> Modules { get; }
public ModuleImport[] Modules { get; }
public string[] Packages { get; }

public AvailablePackageImports(string name, IReadOnlyDictionary<string, string> modules, string[] packages) {
public PackageImport(string name, ModuleImport[] modules, string[] packages) {
Name = name;
Modules = modules;
Packages = packages;
}
}
}
}
26 changes: 23 additions & 3 deletions src/Analysis/Engine/Impl/DependencyResolution/PathResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// permissions and limitations under the License.

using System.Collections.Generic;
using System.Linq;
using Microsoft.PythonTools.Parsing;

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

public void SetRoot(string root) => _currentSnapshot = _currentSnapshot.SetRoot(root);
public void SetSearchPaths(IEnumerable<string> searchPaths) => _currentSnapshot = _currentSnapshot.SetSearchPaths(searchPaths);
public void AddModulePath(string path) => _currentSnapshot = _currentSnapshot.AddModulePath(path);
public IEnumerable<string> SetRoot(in string root) {
_currentSnapshot = _currentSnapshot.SetWorkDirectory(root, out var addedRoots);
return addedRoots;
}

public IEnumerable<string> SetUserSearchPaths(in IEnumerable<string> searchPaths) {
_currentSnapshot = _currentSnapshot.SetUserSearchPaths(searchPaths, out var addedRoots);
return addedRoots;
}

public IEnumerable<string> SetInterpreterSearchPaths(in IEnumerable<string> searchPaths) {
_currentSnapshot = _currentSnapshot.SetInterpreterPaths(searchPaths, out var addedRoots);
return addedRoots;
}

public void SetBuiltins(in IEnumerable<string> builtinModuleNames) => _currentSnapshot = _currentSnapshot.SetBuiltins(builtinModuleNames);
public void RemoveModulePath(in string path) => _currentSnapshot = _currentSnapshot.RemoveModulePath(path);
public bool TryAddModulePath(in string path, out string fullModuleName) {
_currentSnapshot = _currentSnapshot.AddModulePath(path, out fullModuleName);
return fullModuleName != null;
}

public PathResolverSnapshot CurrentSnapshot => _currentSnapshot;
}
}
Loading