From b8e8397c541bd4c52dff5421e8fabd5c9494f417 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Tue, 15 Jun 2021 11:24:07 -0700 Subject: [PATCH 1/7] Fix name of `WorkspaceService _workplaceService` to `_workspaceService` Annoying typo. --- .../Services/Analysis/AnalysisService.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs b/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs index 11c604ccc..859198f8f 100644 --- a/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs +++ b/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs @@ -86,7 +86,7 @@ internal static string GetUniqueIdFromDiagnostic(Diagnostic diagnostic) private readonly ConfigurationService _configurationService; - private readonly WorkspaceService _workplaceService; + private readonly WorkspaceService _workspaceService; private readonly int _analysisDelayMillis; @@ -115,7 +115,7 @@ public AnalysisService( _logger = loggerFactory.CreateLogger(); _languageServer = languageServer; _configurationService = configurationService; - _workplaceService = workspaceService; + _workspaceService = workspaceService; _analysisDelayMillis = 750; _mostRecentCorrectionsByFile = new ConcurrentDictionary(); _analysisEngineLazy = new Lazy(InstantiateAnalysisEngine); @@ -334,7 +334,7 @@ private bool TryFindSettingsFile(out string settingsFilePath) return false; } - settingsFilePath = _workplaceService.ResolveWorkspacePath(configuredPath); + settingsFilePath = _workspaceService.ResolveWorkspacePath(configuredPath); if (settingsFilePath == null || !File.Exists(settingsFilePath)) @@ -349,7 +349,7 @@ private bool TryFindSettingsFile(out string settingsFilePath) private void ClearOpenFileMarkers() { - foreach (ScriptFile file in _workplaceService.GetOpenedFiles()) + foreach (ScriptFile file in _workspaceService.GetOpenedFiles()) { ClearMarkers(file); } From fcf2ee7d8221a1ffe883ed54ea4542228515dd9b Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Tue, 15 Jun 2021 11:25:25 -0700 Subject: [PATCH 2/7] Fix `GetMostRecentCodeActionsForFileAsync` to take `DocumentUri` again This was actually the documented parameter, but at some point it changed to take a file. It was better to use the `_workspaceService` service directly and safely attempt to get the file than to require the user to do that beforehand. --- .../Services/Analysis/AnalysisService.cs | 10 ++++++++-- .../TextDocument/Handlers/CodeActionHandler.cs | 7 +++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs b/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs index 859198f8f..1d698ed82 100644 --- a/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs +++ b/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs @@ -223,9 +223,15 @@ public async Task GetCommentHelpText(string functionText, string helpLoc /// /// The URI string of the file to get code actions for. /// A threadsafe readonly dictionary of the code actions of the particular file. - public async Task> GetMostRecentCodeActionsForFileAsync(ScriptFile scriptFile) + public async Task> GetMostRecentCodeActionsForFileAsync(DocumentUri uri) { - if (!_mostRecentCorrectionsByFile.TryGetValue(scriptFile, out CorrectionTableEntry corrections)) + ScriptFile file = null; + if (!_workspaceService.TryGetFile(uri, out file)) + { + return null; + } + + if (!_mostRecentCorrectionsByFile.TryGetValue(file, out CorrectionTableEntry corrections)) { return null; } diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeActionHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeActionHandler.cs index 5c2529225..9513d7a1c 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeActionHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeActionHandler.cs @@ -52,13 +52,12 @@ public override async Task Handle(CodeActionParams { if (cancellationToken.IsCancellationRequested) { - _logger.LogDebug("CodeAction request canceled at range: {0}", request.Range); + _logger.LogDebug($"CodeAction request canceled at range: {request.Range}"); return Array.Empty(); } - // On Windows, VSCode still gives us file URIs like "file:///c%3a/...", so we need to escape them - IReadOnlyDictionary corrections = await _analysisService.GetMostRecentCodeActionsForFileAsync( - _workspaceService.GetFile(request.TextDocument.Uri)).ConfigureAwait(false); + IReadOnlyDictionary corrections = await + _analysisService.GetMostRecentCodeActionsForFileAsync(request.TextDocument.Uri).ConfigureAwait(false); if (corrections == null) { From f9ab55823c0c39c8e507b3d0fe0c048bab411b56 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Tue, 15 Jun 2021 11:26:42 -0700 Subject: [PATCH 3/7] Support `inmemory` schema Used by the `MonacoLanguageClient`. Because it has no real file backing, we had to check that no file was returned in the prior commit. --- .../Services/Workspace/WorkspaceService.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs b/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs index 8ea3b77f5..b999da6c4 100644 --- a/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs +++ b/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs @@ -192,6 +192,7 @@ public bool TryGetFile(DocumentUri documentUri, out ScriptFile scriptFile) { // List supported schemes here case "file": + case "inmemory": case "untitled": case "vscode-notebook-cell": break; From 9e5c66122682a6752d368671ba14fb107cb508db Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Tue, 15 Jun 2021 11:34:16 -0700 Subject: [PATCH 4/7] Safely set result on `s_editorObjectReady` As it can fail if the editor is being re-initialized. --- src/PowerShellEditorServices/Extensions/EditorObject.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices/Extensions/EditorObject.cs b/src/PowerShellEditorServices/Extensions/EditorObject.cs index aa678cdc0..b88f41f84 100644 --- a/src/PowerShellEditorServices/Extensions/EditorObject.cs +++ b/src/PowerShellEditorServices/Extensions/EditorObject.cs @@ -137,7 +137,7 @@ public EditorContext GetEditorContext() internal void SetAsStaticInstance() { EditorObject.Instance = this; - s_editorObjectReady.SetResult(true); + s_editorObjectReady.TrySetResult(true); } } } From 0cdd0a4785ec07fd28667e8a61b76b77cf494728 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Tue, 15 Jun 2021 11:35:34 -0700 Subject: [PATCH 5/7] Guard against `incomingSettings` being null --- .../Services/Workspace/Handlers/ConfigurationHandler.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/PowerShellEditorServices/Services/Workspace/Handlers/ConfigurationHandler.cs b/src/PowerShellEditorServices/Services/Workspace/Handlers/ConfigurationHandler.cs index 073c176cb..af9d93b04 100644 --- a/src/PowerShellEditorServices/Services/Workspace/Handlers/ConfigurationHandler.cs +++ b/src/PowerShellEditorServices/Services/Workspace/Handlers/ConfigurationHandler.cs @@ -157,6 +157,12 @@ await _powerShellContextService.SetWorkingDirectoryAsync( private void SendFeatureChangesTelemetry(LanguageServerSettingsWrapper incomingSettings) { var configChanges = new Dictionary(); + if (incomingSettings == null) + { + this._logger.LogTrace("Incoming settings were null"); + return; + } + // Send telemetry if the user opted-out of ScriptAnalysis if (incomingSettings.Powershell.ScriptAnalysis.Enable == false && _configurationService.CurrentSettings.ScriptAnalysis.Enable != incomingSettings.Powershell.ScriptAnalysis.Enable) From 8a075fa08744df26780e637225e10912a7f2be77 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Tue, 15 Jun 2021 12:17:55 -0700 Subject: [PATCH 6/7] Apply suggestions from code review Co-authored-by: Robert Holt --- .../Services/Analysis/AnalysisService.cs | 9 ++------- .../Services/TextDocument/Handlers/CodeActionHandler.cs | 5 +++-- .../Services/Workspace/Handlers/ConfigurationHandler.cs | 4 ++-- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs b/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs index 1d698ed82..eee266e0f 100644 --- a/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs +++ b/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs @@ -225,13 +225,8 @@ public async Task GetCommentHelpText(string functionText, string helpLoc /// A threadsafe readonly dictionary of the code actions of the particular file. public async Task> GetMostRecentCodeActionsForFileAsync(DocumentUri uri) { - ScriptFile file = null; - if (!_workspaceService.TryGetFile(uri, out file)) - { - return null; - } - - if (!_mostRecentCorrectionsByFile.TryGetValue(file, out CorrectionTableEntry corrections)) + if (!_workspaceService.TryGetFile(uri, out ScriptFile file) + || !_mostRecentCorrectionsByFile.TryGetValue(file, out CorrectionTableEntry corrections)) { return null; } diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeActionHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeActionHandler.cs index 9513d7a1c..062a312ff 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeActionHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeActionHandler.cs @@ -56,8 +56,9 @@ public override async Task Handle(CodeActionParams return Array.Empty(); } - IReadOnlyDictionary corrections = await - _analysisService.GetMostRecentCodeActionsForFileAsync(request.TextDocument.Uri).ConfigureAwait(false); + IReadOnlyDictionary corrections = await _analysisService.GetMostRecentCodeActionsForFileAsync( + request.TextDocument.Uri) + .ConfigureAwait(false); if (corrections == null) { diff --git a/src/PowerShellEditorServices/Services/Workspace/Handlers/ConfigurationHandler.cs b/src/PowerShellEditorServices/Services/Workspace/Handlers/ConfigurationHandler.cs index af9d93b04..b89c3d127 100644 --- a/src/PowerShellEditorServices/Services/Workspace/Handlers/ConfigurationHandler.cs +++ b/src/PowerShellEditorServices/Services/Workspace/Handlers/ConfigurationHandler.cs @@ -156,13 +156,13 @@ await _powerShellContextService.SetWorkingDirectoryAsync( private void SendFeatureChangesTelemetry(LanguageServerSettingsWrapper incomingSettings) { - var configChanges = new Dictionary(); - if (incomingSettings == null) + if (incomingSettings is null) { this._logger.LogTrace("Incoming settings were null"); return; } + var configChanges = new Dictionary(); // Send telemetry if the user opted-out of ScriptAnalysis if (incomingSettings.Powershell.ScriptAnalysis.Enable == false && _configurationService.CurrentSettings.ScriptAnalysis.Enable != incomingSettings.Powershell.ScriptAnalysis.Enable) From f917e9bcbfd45a9f06b083b65deb88ead617deaa Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Tue, 15 Jun 2021 15:18:04 -0700 Subject: [PATCH 7/7] Finish adding Git commit to build info --- PowerShellEditorServices.build.ps1 | 2 +- src/PowerShellEditorServices.Hosting/BuildInfo.cs | 3 +-- src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs | 1 + 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/PowerShellEditorServices.build.ps1 b/PowerShellEditorServices.build.ps1 index 48599e10c..fbd126708 100644 --- a/PowerShellEditorServices.build.ps1 +++ b/PowerShellEditorServices.build.ps1 @@ -216,7 +216,7 @@ namespace Microsoft.PowerShell.EditorServices.Hosting { public static readonly string BuildVersion = "$buildVersion"; public static readonly string BuildOrigin = "$buildOrigin"; - public static readonly string BuildCommit= "$buildCommit"; + public static readonly string BuildCommit = "$buildCommit"; public static readonly System.DateTime? BuildTime = System.DateTime.Parse("$buildTime", CultureInfo.InvariantCulture.DateTimeFormat); } } diff --git a/src/PowerShellEditorServices.Hosting/BuildInfo.cs b/src/PowerShellEditorServices.Hosting/BuildInfo.cs index 1149b7cee..78953ca18 100644 --- a/src/PowerShellEditorServices.Hosting/BuildInfo.cs +++ b/src/PowerShellEditorServices.Hosting/BuildInfo.cs @@ -4,10 +4,9 @@ namespace Microsoft.PowerShell.EditorServices.Hosting { public static class BuildInfo { - // TODO: Include a Git commit hash in this. public static readonly string BuildVersion = ""; public static readonly string BuildOrigin = ""; - public static readonly string BuildCommit= ""; + public static readonly string BuildCommit = ""; public static readonly System.DateTime? BuildTime = System.DateTime.Parse("2019-12-06T21:43:41", CultureInfo.InvariantCulture.DateTimeFormat); } } diff --git a/src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs b/src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs index b55d7a70a..59956df1a 100644 --- a/src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs +++ b/src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs @@ -269,6 +269,7 @@ private void LogHostInformation() == Build Details == - Editor Services version: {BuildInfo.BuildVersion} - Build origin: {BuildInfo.BuildOrigin} +- Build commit: {BuildInfo.BuildCommit} - Build time: {BuildInfo.BuildTime} ");