From 08b41dc9a27e85277c3f377eb45dac00f73990b4 Mon Sep 17 00:00:00 2001 From: MikhailArkhipov Date: Tue, 18 Sep 2018 11:39:17 -0700 Subject: [PATCH 1/3] Add ability to get document content from API --- src/Analysis/Engine/Impl/Definitions/IProjectEntry.cs | 7 +++++++ .../Engine/Impl/Interpreter/Ast/AstPythonModule.cs | 2 +- src/Analysis/Engine/Impl/ProjectEntry.cs | 4 ++++ .../Impl/Definitions/IPythonLanguageServer.cs | 3 +++ src/LanguageServer/Impl/Implementation/ProjectFiles.cs | 3 +-- .../Impl/Implementation/Server.WorkspaceSymbols.cs | 4 ++-- src/LanguageServer/Impl/Implementation/Server.cs | 1 + 7 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Analysis/Engine/Impl/Definitions/IProjectEntry.cs b/src/Analysis/Engine/Impl/Definitions/IProjectEntry.cs index 4be3b2ee6..8217bad42 100644 --- a/src/Analysis/Engine/Impl/Definitions/IProjectEntry.cs +++ b/src/Analysis/Engine/Impl/Definitions/IProjectEntry.cs @@ -45,5 +45,12 @@ public interface IProjectEntry : IAnalyzable, IVersioned, IDisposable { Dictionary Properties { get; } IModuleContext AnalysisContext { get; } + + /// + /// Document object corresponding to the entry. + /// Can be null for entries that are not user documents + /// such as modules. + /// + IDocument Document { get; } } } diff --git a/src/Analysis/Engine/Impl/Interpreter/Ast/AstPythonModule.cs b/src/Analysis/Engine/Impl/Interpreter/Ast/AstPythonModule.cs index b3664dba2..3c4d73e39 100644 --- a/src/Analysis/Engine/Impl/Interpreter/Ast/AstPythonModule.cs +++ b/src/Analysis/Engine/Impl/Interpreter/Ast/AstPythonModule.cs @@ -185,8 +185,8 @@ public string Documentation { public IModuleContext AnalysisContext => null; public bool IsAnalyzed => true; public void Analyze(CancellationToken cancel) { } - public IEnumerable ParseErrors { get; } + public IDocument Document => null; private static IEnumerable GetChildModules(string filePath, string prefix, IPythonInterpreter interpreter) { if (interpreter == null || string.IsNullOrEmpty(filePath)) { diff --git a/src/Analysis/Engine/Impl/ProjectEntry.cs b/src/Analysis/Engine/Impl/ProjectEntry.cs index babdb397c..c4979def5 100644 --- a/src/Analysis/Engine/Impl/ProjectEntry.cs +++ b/src/Analysis/Engine/Impl/ProjectEntry.cs @@ -320,6 +320,7 @@ where lastDot > 0 public IGroupableAnalysisProject AnalysisGroup => ProjectState; + #region IPythonProjectEntry public IModuleAnalysis Analysis { get; private set; } public string FilePath { get; } @@ -340,6 +341,9 @@ where lastDot > 0 public Dictionary Properties { get; } = new Dictionary(); + public IDocument Document => this as IDocument; + #endregion + public void Dispose() { lock (this) { AnalysisVersion = -1; diff --git a/src/LanguageServer/Impl/Definitions/IPythonLanguageServer.cs b/src/LanguageServer/Impl/Definitions/IPythonLanguageServer.cs index 0944abb31..c380c5100 100644 --- a/src/LanguageServer/Impl/Definitions/IPythonLanguageServer.cs +++ b/src/LanguageServer/Impl/Definitions/IPythonLanguageServer.cs @@ -15,6 +15,7 @@ // permissions and limitations under the License. using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Microsoft.PythonTools.Analysis; @@ -27,5 +28,7 @@ public interface IPythonLanguageServer : IPythonLanguageServerProtocol { PythonAst GetCurrentAst(Uri documentUri); Task GetAstAsync(Uri documentUri, CancellationToken token); Task GetAnalysisAsync(Uri documentUri, CancellationToken token); + IProjectEntry GetProjectEntry(Uri documentUri); + IEnumerable GetLoadedFiles(); } } diff --git a/src/LanguageServer/Impl/Implementation/ProjectFiles.cs b/src/LanguageServer/Impl/Implementation/ProjectFiles.cs index 8333db093..084c60203 100644 --- a/src/LanguageServer/Impl/Implementation/ProjectFiles.cs +++ b/src/LanguageServer/Impl/Implementation/ProjectFiles.cs @@ -51,7 +51,6 @@ public IEnumerable GetLoadedFiles() { return _projectFiles.Keys.Select(k => k.AbsoluteUri); } - public IProjectEntry GetEntry(TextDocumentIdentifier document) => GetEntry(document.uri); public IProjectEntry GetEntry(Uri documentUri, bool throwIfMissing = true) { ThrowIfDisposed(); @@ -65,7 +64,7 @@ public IProjectEntry GetEntry(Uri documentUri, bool throwIfMissing = true) { public void GetEntry(TextDocumentIdentifier document, int? expectedVersion, out ProjectEntry entry, out PythonAst tree) { ThrowIfDisposed(); - entry = GetEntry(document) as ProjectEntry; + entry = GetEntry(document.uri) as ProjectEntry; if (entry == null) { throw new LanguageServerException(LanguageServerException.UnsupportedDocumentType, "unsupported document"); } diff --git a/src/LanguageServer/Impl/Implementation/Server.WorkspaceSymbols.cs b/src/LanguageServer/Impl/Implementation/Server.WorkspaceSymbols.cs index abf81e369..9aa8699af 100644 --- a/src/LanguageServer/Impl/Implementation/Server.WorkspaceSymbols.cs +++ b/src/LanguageServer/Impl/Implementation/Server.WorkspaceSymbols.cs @@ -45,7 +45,7 @@ await GetModuleVariablesAsync(entry as ProjectEntry, opts, @params.query, 50, to public override async Task DocumentSymbol(DocumentSymbolParams @params, CancellationToken token) { var opts = GetMemberOptions.ExcludeBuiltins | GetMemberOptions.DeclaredOnly; - var entry = ProjectFiles.GetEntry(@params.textDocument); + var entry = ProjectFiles.GetEntry(@params.textDocument.uri); var members = await GetModuleVariablesAsync(entry as ProjectEntry, opts, string.Empty, 50, token); return members @@ -57,7 +57,7 @@ public override async Task DocumentSymbol(DocumentSymbolPar public override async Task HierarchicalDocumentSymbol(DocumentSymbolParams @params, CancellationToken cancellationToken) { var opts = GetMemberOptions.ExcludeBuiltins | GetMemberOptions.DeclaredOnly; - var entry = ProjectFiles.GetEntry(@params.textDocument); + var entry = ProjectFiles.GetEntry(@params.textDocument.uri); var members = await GetModuleVariablesAsync(entry as ProjectEntry, opts, string.Empty, 50, cancellationToken); return ToDocumentSymbols(members); diff --git a/src/LanguageServer/Impl/Implementation/Server.cs b/src/LanguageServer/Impl/Implementation/Server.cs index 123d84615..0af581cbc 100644 --- a/src/LanguageServer/Impl/Implementation/Server.cs +++ b/src/LanguageServer/Impl/Implementation/Server.cs @@ -361,6 +361,7 @@ public Task GetAnalysisAsync(Uri documentUri, CancellationToken return entry.GetAnalysisAsync(Timeout.Infinite, token); } + public IProjectEntry GetProjectEntry(Uri documentUri) => ProjectFiles.GetEntry(documentUri); #endregion #region Private Helpers From 2d3caa2478ec11c08f36fe5d694e20617f904ee5 Mon Sep 17 00:00:00 2001 From: MikhailArkhipov Date: Tue, 18 Sep 2018 12:04:06 -0700 Subject: [PATCH 2/3] Fix OverrideCompletions* tests --- src/Analysis/Engine/Test/AnalysisTest.cs | 4 ++-- src/LanguageServer/Impl/Implementation/CompletionAnalysis.cs | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Analysis/Engine/Test/AnalysisTest.cs b/src/Analysis/Engine/Test/AnalysisTest.cs index bd5e92c9f..5e15ab899 100644 --- a/src/Analysis/Engine/Test/AnalysisTest.cs +++ b/src/Analysis/Engine/Test/AnalysisTest.cs @@ -4403,7 +4403,7 @@ class oar(list): var completions = await server.SendCompletion(uri, 2, 8); completions.Should().HaveItem("append") - .Which.Should().HaveInsertText("append(self, value):\r\n\treturn super(oar, self).append(value)"); + .Which.Should().HaveInsertText("append(self, value):\r\n return super(oar, self).append(value)"); } } @@ -4419,7 +4419,7 @@ class oar(list): var completions = await server.SendCompletion(uri, 2, 8); completions.Should().HaveItem("append") - .Which.Should().HaveInsertText("append(self, value):\r\n\treturn super().append(value)"); + .Which.Should().HaveInsertText("append(self, value):\r\n return super().append(value)"); } } diff --git a/src/LanguageServer/Impl/Implementation/CompletionAnalysis.cs b/src/LanguageServer/Impl/Implementation/CompletionAnalysis.cs index d2711f7dd..5c4878e91 100644 --- a/src/LanguageServer/Impl/Implementation/CompletionAnalysis.cs +++ b/src/LanguageServer/Impl/Implementation/CompletionAnalysis.cs @@ -899,9 +899,7 @@ private string MakeOverrideCompletionString(string indentation, IOverloadResult var sb = new StringBuilder(); sb.AppendLine(result.Name + "(" + string.Join(", ", result.Parameters.Select((p, i) => GetSafeParameterName(p, i))) + "):"); - sb.Append(indentation); - sb.Append('\t'); if (result.Parameters.Length > 0) { var parameterString = string.Join(", ", result.Parameters.Skip(1).Select((p, i) => GetSafeArgumentName(p, i + 1))); From 081f131b247f03da7456531a3d673e4f963ccf2c Mon Sep 17 00:00:00 2001 From: MikhailArkhipov Date: Tue, 18 Sep 2018 12:30:26 -0700 Subject: [PATCH 3/3] PR feedback --- src/Analysis/Engine/Impl/ProjectEntry.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Analysis/Engine/Impl/ProjectEntry.cs b/src/Analysis/Engine/Impl/ProjectEntry.cs index c4979def5..b1982d18b 100644 --- a/src/Analysis/Engine/Impl/ProjectEntry.cs +++ b/src/Analysis/Engine/Impl/ProjectEntry.cs @@ -341,7 +341,7 @@ where lastDot > 0 public Dictionary Properties { get; } = new Dictionary(); - public IDocument Document => this as IDocument; + public IDocument Document => this; #endregion public void Dispose() {