Skip to content

Commit

Permalink
Merge pull request #1323 from zooba/issue-1251
Browse files Browse the repository at this point in the history
Fixes #1251 No completions in virtual env REPL
  • Loading branch information
zooba committed May 26, 2016
2 parents 546ffac + 2c69ea7 commit a5b78ed
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 32 deletions.
11 changes: 11 additions & 0 deletions Python/Product/Analysis/Interpreter/IInterpreterRegistryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ public interface IInterpreterRegistryService {

InterpreterConfiguration FindConfiguration(string id);

/// <summary>
/// Gets a property value relating to a specific interpreter.
///
/// If the property is not set, returns <c>null</c>.
/// </summary>
/// <param name="id">The interpreter identifier.</param>
/// <param name="propName">A case-sensitive string identifying the
/// property. Values will be compared by ordinal.</param>
/// <returns>The property value, or <c>null</c> if not set.</returns>
object GetProperty(string id, string propName);

/// <summary>
/// Raised when the set of interpreters changes. This is not raised when
/// the set is first initialized.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ public interface IPythonInterpreterFactoryProvider {
/// Gets a specific configured interpreter
/// </summary>
IPythonInterpreterFactory GetInterpreterFactory(string id);

/// <summary>
/// Gets a property value associated with the specified interpreter. If
/// the property is not set or available, return <c>null</c>.
///
/// Property values should not change over the process lifetime.
/// </summary>
/// <param name="id">The interpreter id.</param>
/// <param name="propName">A case-sensitive string identifying the name
/// of the property. Values will be compared by ordinal.</param>
object GetProperty(string id, string propName);
}

public static class PythonInterpreterExtensions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,11 @@ public InterpreterConfiguration FindConfiguration(string id) {
return null;
}

public object GetProperty(string id, string propName) {
var factoryProvider = GetFactoryProvider(id);
return factoryProvider?.GetProperty(id, propName);
}

private IPythonInterpreterFactoryProvider GetFactoryProvider(string id) {
var interpAndId = id.Split(new[] { '|' }, 2);
if (interpAndId.Length == 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ public IPythonInterpreterFactory GetInterpreterFactory(string id) {
.FirstOrDefault();
}

public object GetProperty(string id, string propName) => null;

public event EventHandler InterpreterFactoriesChanged;

private void OnInterpreterFactoriesChanged() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ private void DiscoverInterpreterFactories() {

public event EventHandler InterpreterFactoriesChanged;

public object GetProperty(string id, string propName) => null;

#endregion

Expand Down
20 changes: 17 additions & 3 deletions Python/Product/PythonTools/PythonTools/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,15 @@ internal static bool TryGetAnalysisEntry(this ITextView view, ITextBuffer buffer

string path = buffer.GetFilePath();
if (path != null) {
var docTable = provider.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable4;
var cookie = docTable.GetDocumentCookie(path);
var docTable = (IVsRunningDocumentTable4)provider.GetService(typeof(SVsRunningDocumentTable));
var cookie = VSConstants.VSCOOKIE_NIL;
try {
cookie = docTable.GetDocumentCookie(path);
} catch (ArgumentException) {
// Exception may be raised while VS is shutting down
entry = null;
return false;
}
VsProjectAnalyzer analyzer = null;
if (cookie != VSConstants.VSCOOKIE_NIL) {
IVsHierarchy hierarchy;
Expand Down Expand Up @@ -486,7 +493,14 @@ internal static PythonProjectNode GetProjectFromFile(this IServiceProvider servi
IVsHierarchy hierarchy;
uint itemid;
docTable.GetDocumentHierarchyItem(cookie, out hierarchy, out itemid);
return hierarchy.GetProject()?.GetPythonProject();
var project = hierarchy.GetProject();
if (project != null) {
return project.GetPythonProject();
}

object projectObj;
ErrorHandler.ThrowOnFailure(hierarchy.GetProperty(itemid, (int)__VSHPROPID.VSHPROPID_ExtObject, out projectObj));
return (projectObj as EnvDTE.Project)?.GetPythonProject();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,43 @@ internal virtual void OnConnected() { }
internal virtual void OnAttach() { }
internal virtual void OnDetach() { }

private PythonProjectNode GetAssociatedPythonProject(InterpreterConfiguration interpreter = null) {
var moniker = ProjectMoniker;
if (interpreter == null) {
interpreter = Configuration?.Interpreter;
}

if (string.IsNullOrEmpty(moniker) && interpreter != null) {
var interpreterService = _serviceProvider.GetComponentModel().GetService<IInterpreterRegistryService>();
moniker = interpreterService.GetProperty(interpreter.Id, "ProjectMoniker") as string;
}

if (string.IsNullOrEmpty(moniker)) {
return null;
}

return _serviceProvider.GetProjectFromFile(moniker);
}


public VsProjectAnalyzer Analyzer {
get {
if (_analyzer != null) {
return _analyzer;
}

var interpreterService = _serviceProvider.GetComponentModel().GetService<IInterpreterRegistryService>();
var config = Configuration;

if (config == null) {
_analyzer = _serviceProvider.GetPythonToolsService().DefaultAnalyzer;
} else {
_analyzer = new VsProjectAnalyzer(_serviceProvider, interpreterService.FindInterpreter(config.Interpreter.Id));
var projectFile = GetAssociatedPythonProject(config.Interpreter)?.BuildProject;
var interpreterService = _serviceProvider.GetComponentModel().GetService<IInterpreterRegistryService>();
_analyzer = new VsProjectAnalyzer(
_serviceProvider,
interpreterService.FindInterpreter(config.Interpreter.Id),
projectFile: projectFile
);
}
return _analyzer;
}
Expand Down Expand Up @@ -283,22 +307,20 @@ protected async Task<CommandProcessorThread> EnsureConnectedAsync() {
}

return await _serviceProvider.GetUIThread().InvokeTask(async () => {
if (!string.IsNullOrEmpty(ProjectMoniker)) {
try {
UpdatePropertiesFromProjectMoniker();
} catch (NoInterpretersException ex) {
WriteError(ex.ToString());
return null;
} catch (MissingInterpreterException ex) {
WriteError(ex.ToString());
return null;
} catch (DirectoryNotFoundException ex) {
WriteError(ex.ToString());
return null;
} catch (Exception ex) when (!ex.IsCriticalException()) {
WriteError(ex.ToUnhandledExceptionMessage(GetType()));
return null;
}
try {
UpdatePropertiesFromProjectMoniker();
} catch (NoInterpretersException ex) {
WriteError(ex.ToString());
return null;
} catch (MissingInterpreterException ex) {
WriteError(ex.ToString());
return null;
} catch (DirectoryNotFoundException ex) {
WriteError(ex.ToString());
return null;
} catch (Exception ex) when (!ex.IsCriticalException()) {
WriteError(ex.ToUnhandledExceptionMessage(GetType()));
return null;
}

var scriptsPath = ScriptsPath;
Expand Down Expand Up @@ -351,17 +373,7 @@ protected virtual async Task ExecuteStartupScripts(string scriptsPath) {
}

internal void UpdatePropertiesFromProjectMoniker() {
var solution = _serviceProvider.GetService(typeof(SVsSolution)) as IVsSolution;
if (solution == null) {
return;
}

IVsHierarchy hier;
if (string.IsNullOrEmpty(ProjectMoniker) ||
ErrorHandler.Failed(solution.GetProjectOfUniqueName(ProjectMoniker, out hier))) {
return;
}
var pyProj = hier?.GetProject()?.GetPythonProject();
var pyProj = GetAssociatedPythonProject();
if (pyProj == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,7 @@ public IPythonInterpreterFactory GetInterpreterFactory(string id) {
.Where(x => x.Configuration.Id == id)
.FirstOrDefault();
}

public object GetProperty(string id, string propName) => null;
}
}
10 changes: 10 additions & 0 deletions Python/Product/VSInterpreters/CPythonInterpreterFactoryProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.PythonTools.Infrastructure;
using Microsoft.PythonTools.Parsing;
using Microsoft.Win32;

namespace Microsoft.PythonTools.Interpreter {
Expand Down Expand Up @@ -180,6 +181,13 @@ private static void TryRegisterInterpreter(Dictionary<string, InterpreterInforma
version = new Version(2, 7);
}

try {
var langVer = version.ToLanguageVersion();
} catch (InvalidOperationException) {
// Version is not currently supported
return;
}

var archStr = interpKey.GetValue("Architecture") as string;
switch (archStr) {
case "x64": arch = ProcessorArchitecture.Amd64; break;
Expand Down Expand Up @@ -353,6 +361,8 @@ private void OnInterpreterFactoriesChanged() {
_interpFactoriesChanged?.Invoke(this, EventArgs.Empty);
}

public object GetProperty(string id, string propName) => null;

#endregion

class InterpreterInformation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ public IPythonInterpreterFactory GetInterpreterFactory(string id) {
return null;
}

public object GetProperty(string id, string propName) {
if (propName == "ProjectMoniker") {
var moniker = id.Substring(id.LastIndexOf('|') + 1);
return PathUtils.IsValidPath(moniker) ? moniker : null;
}
return null;
}

public static string GetInterpreterId(string file, string id) {
return String.Join("|", MSBuildProviderName, id, file);
}
Expand Down
2 changes: 2 additions & 0 deletions Python/Tests/Core/ReplEvaluatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ public IPythonInterpreterFactory GetInterpreterFactory(string id) {
.FirstOrDefault();
}

public object GetProperty(string id, string propName) => null;

public event EventHandler InterpreterFactoriesChanged { add { } remove { } }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,9 @@ public InterpreterConfiguration FindConfiguration(string id) {
public string AddConfigurableInterpreter(string name, InterpreterConfiguration config) {
throw new NotImplementedException();
}

public object GetProperty(string id, string propName) {
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public IPythonInterpreterFactory GetInterpreterFactory(string id) {
.FirstOrDefault();
}

public object GetProperty(string id, string propName) => null;

public event EventHandler InterpreterFactoriesChanged;
}
}

0 comments on commit a5b78ed

Please sign in to comment.