Skip to content

Commit 34f949c

Browse files
authored
Merge pull request #385 from daviwil/fix-566
Enable editor IntelliSense while stopped at a breakpoint
2 parents aa2b193 + bbc15c1 commit 34f949c

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

src/PowerShellEditorServices/Language/AstOperations.cs

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
using System;
88
using System.Collections.Generic;
99
using System.Linq;
10-
using System.Management.Automation;
11-
using System.Management.Automation.Language;
12-
using System.Management.Automation.Runspaces;
1310
using System.Reflection;
11+
using System.Threading;
12+
using System.Threading.Tasks;
1413

1514
namespace Microsoft.PowerShell.EditorServices
1615
{
16+
using System.Management.Automation;
17+
using System.Management.Automation.Language;
18+
using System.Management.Automation.Runspaces;
19+
1720
/// <summary>
1821
/// Provides common operations for the syntax tree of a parsed script.
1922
/// </summary>
@@ -32,18 +35,22 @@ internal static class AstOperations
3235
/// <param name="fileOffset">
3336
/// The 1-based file offset at which a symbol will be located.
3437
/// </param>
35-
/// <param name="runspace">
36-
/// The Runspace to use for gathering completions.
38+
/// <param name="powerShellContext">
39+
/// The PowerShellContext to use for gathering completions.
40+
/// </param>
41+
/// <param name="cancellationToken">
42+
/// A CancellationToken to cancel completion requests.
3743
/// </param>
3844
/// <returns>
3945
/// A CommandCompletion instance that contains completions for the
4046
/// symbol at the given offset.
4147
/// </returns>
42-
static public CommandCompletion GetCompletions(
48+
static public async Task<CommandCompletion> GetCompletions(
4349
Ast scriptAst,
4450
Token[] currentTokens,
4551
int fileOffset,
46-
Runspace runspace)
52+
PowerShellContext powerShellContext,
53+
CancellationToken cancellationToken)
4754
{
4855
var type = scriptAst.Extent.StartScriptPosition.GetType();
4956
var method =
@@ -73,20 +80,34 @@ static public CommandCompletion GetCompletions(
7380
cursorPosition.ColumnNumber));
7481

7582
CommandCompletion commandCompletion = null;
76-
if (runspace.RunspaceAvailability == RunspaceAvailability.Available)
83+
if (powerShellContext.IsDebuggerStopped)
84+
{
85+
PSCommand command = new PSCommand();
86+
command.AddCommand("TabExpansion2");
87+
command.AddParameter("Ast", scriptAst);
88+
command.AddParameter("Tokens", currentTokens);
89+
command.AddParameter("PositionOfCursor", cursorPosition);
90+
command.AddParameter("Options", null);
91+
92+
commandCompletion =
93+
(await powerShellContext.ExecuteCommand<CommandCompletion>(command, false, false))
94+
.FirstOrDefault();
95+
}
96+
else if (powerShellContext.CurrentRunspace.Runspace.RunspaceAvailability ==
97+
RunspaceAvailability.Available)
7798
{
78-
using (System.Management.Automation.PowerShell powerShell =
79-
System.Management.Automation.PowerShell.Create())
99+
using (RunspaceHandle runspaceHandle = await powerShellContext.GetRunspaceHandle(cancellationToken))
100+
using (PowerShell powerShell = PowerShell.Create())
80101
{
81-
powerShell.Runspace = runspace;
102+
powerShell.Runspace = runspaceHandle.Runspace;
82103

83-
commandCompletion =
104+
commandCompletion =
84105
CommandCompletion.CompleteInput(
85-
scriptAst,
86-
currentTokens,
87-
cursorPosition,
88-
null,
89-
powerShell);
106+
scriptAst,
107+
currentTokens,
108+
cursorPosition,
109+
null,
110+
powerShell);
90111
}
91112
}
92113

src/PowerShellEditorServices/Language/LanguageService.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,13 @@ public async Task<CompletionResults> GetCompletionsInFile(
8989
lineNumber,
9090
columnNumber);
9191

92-
RunspaceHandle runspaceHandle =
93-
await this.powerShellContext.GetRunspaceHandle(
94-
new CancellationTokenSource(DefaultWaitTimeoutMilliseconds).Token);
95-
9692
CommandCompletion commandCompletion =
97-
AstOperations.GetCompletions(
93+
await AstOperations.GetCompletions(
9894
scriptFile.ScriptAst,
9995
scriptFile.ScriptTokens,
10096
fileOffset,
101-
runspaceHandle.Runspace);
102-
103-
runspaceHandle.Dispose();
97+
this.powerShellContext,
98+
new CancellationTokenSource(DefaultWaitTimeoutMilliseconds).Token);
10499

105100
if (commandCompletion != null)
106101
{

0 commit comments

Comments
 (0)