From 021d381c9d6eabf5f3cdc7586ed9dc3eb0d523ca Mon Sep 17 00:00:00 2001 From: Darren Kattan Date: Wed, 13 Dec 2023 10:50:21 -0600 Subject: [PATCH] Added null check to GetCompletionsAsync --- PowerShellEditorServices.build.ps1 | 2 +- .../Symbols/Visitors/AstOperations.cs | 41 ++++++++++--------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/PowerShellEditorServices.build.ps1 b/PowerShellEditorServices.build.ps1 index b8acc4b24..7c0319a95 100644 --- a/PowerShellEditorServices.build.ps1 +++ b/PowerShellEditorServices.build.ps1 @@ -158,7 +158,7 @@ namespace Microsoft.PowerShell.EditorServices.Hosting Task SetupHelpForTests { # TODO: Check if it must be updated in a compatible way! Write-Host "Updating help for tests." - Update-Help -Module Microsoft.PowerShell.Management,Microsoft.PowerShell.Utility -Force -Scope CurrentUser + Update-Help -Module Microsoft.PowerShell.Management,Microsoft.PowerShell.Utility -Force -Scope CurrentUser -UICulture en-US } Task Build FindDotNet, CreateBuildInfo, { diff --git a/src/PowerShellEditorServices/Services/Symbols/Visitors/AstOperations.cs b/src/PowerShellEditorServices/Services/Symbols/Visitors/AstOperations.cs index 93b8fd924..65d2d03e0 100644 --- a/src/PowerShellEditorServices/Services/Symbols/Visitors/AstOperations.cs +++ b/src/PowerShellEditorServices/Services/Symbols/Visitors/AstOperations.cs @@ -76,13 +76,10 @@ static AstOperations() CancellationToken cancellationToken) { IScriptPosition cursorPosition = s_clonePositionWithNewOffset(scriptAst.Extent.StartScriptPosition, fileOffset); - - logger.LogTrace($"Getting completions at offset {fileOffset} (line: {cursorPosition.LineNumber}, column: {cursorPosition.ColumnNumber})"); - Stopwatch stopwatch = new(); + logger.LogTrace($"Getting completions at offset {fileOffset} (line: {cursorPosition.LineNumber}, column: {cursorPosition.ColumnNumber})"); - CommandCompletion commandCompletion = null; - await executionService.ExecuteDelegateAsync( + CommandCompletion commandCompletion = await executionService.ExecuteDelegateAsync( representation: "CompleteInput", new ExecutionOptions { Priority = ExecutionPriority.Next }, (pwsh, _) => @@ -108,35 +105,41 @@ static AstOperations() if (completionResults is { Count: > 0 }) { - commandCompletion = completionResults[0]; + return completionResults[0]; } - return; + return null; } // If the current runspace is out of process, we can't call TabExpansion2 // because the output will be serialized. - commandCompletion = CommandCompletion.CompleteInput( + return CommandCompletion.CompleteInput( scriptAst, currentTokens, cursorPosition, options: null, powershell: pwsh); }, - cancellationToken) - .ConfigureAwait(false); + cancellationToken).ConfigureAwait(false); stopwatch.Stop(); - logger.LogTrace( - "IntelliSense completed in {elapsed}ms - WordToComplete: \"{word}\" MatchCount: {count}", - stopwatch.ElapsedMilliseconds, - commandCompletion.ReplacementLength > 0 - ? scriptAst.Extent.StartScriptPosition.GetFullScript()?.Substring( - commandCompletion.ReplacementIndex, - commandCompletion.ReplacementLength) - : null, - commandCompletion.CompletionMatches.Count); + if (commandCompletion is null) + { + logger.LogError("Error Occurred in TabExpansion2"); + } + else + { + logger.LogTrace( + "IntelliSense completed in {elapsed}ms - WordToComplete: \"{word}\" MatchCount: {count}", + stopwatch.ElapsedMilliseconds, + commandCompletion.ReplacementLength > 0 + ? scriptAst.Extent.StartScriptPosition.GetFullScript()?.Substring( + commandCompletion.ReplacementIndex, + commandCompletion.ReplacementLength) + : null, + commandCompletion.CompletionMatches.Count); + } return commandCompletion; }