Skip to content

Commit d2038e8

Browse files
committed
Make completionItem/resolve cancellable
1 parent 7734d29 commit d2038e8

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

src/PowerShellEditorServices/Services/PowerShell/Utility/CommandHelpers.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ internal static class CommandHelpers
6464
public static async Task<CommandInfo> GetCommandInfoAsync(
6565
string commandName,
6666
IRunspaceInfo currentRunspace,
67-
IInternalPowerShellExecutionService executionService)
67+
IInternalPowerShellExecutionService executionService,
68+
CancellationToken cancellationToken = default)
6869
{
6970
// This mechanism only works in-process
7071
if (currentRunspace.RunspaceOrigin != RunspaceOrigin.Local)
@@ -98,7 +99,7 @@ public static async Task<CommandInfo> GetCommandInfoAsync(
9899
.AddParameter("ErrorAction", "Ignore");
99100

100101
IReadOnlyList<CommandInfo> results = await executionService
101-
.ExecutePSCommandAsync<CommandInfo>(command, CancellationToken.None)
102+
.ExecutePSCommandAsync<CommandInfo>(command, cancellationToken)
102103
.ConfigureAwait(false);
103104

104105
CommandInfo commandInfo = results.Count > 0 ? results[0] : null;
@@ -120,7 +121,8 @@ public static async Task<CommandInfo> GetCommandInfoAsync(
120121
/// <returns>The synopsis.</returns>
121122
public static async Task<string> GetCommandSynopsisAsync(
122123
CommandInfo commandInfo,
123-
IInternalPowerShellExecutionService executionService)
124+
IInternalPowerShellExecutionService executionService,
125+
CancellationToken cancellationToken = default)
124126
{
125127
Validate.IsNotNull(nameof(commandInfo), commandInfo);
126128
Validate.IsNotNull(nameof(executionService), executionService);
@@ -151,7 +153,7 @@ public static async Task<string> GetCommandSynopsisAsync(
151153
.AddParameter("ErrorAction", "Ignore");
152154

153155
IReadOnlyList<PSObject> results = await executionService
154-
.ExecutePSCommandAsync<PSObject>(command, CancellationToken.None)
156+
.ExecutePSCommandAsync<PSObject>(command, cancellationToken)
155157
.ConfigureAwait(false);
156158

157159
// Extract the synopsis string from the object

src/PowerShellEditorServices/Services/TextDocument/Handlers/CompletionHandler.cs

+14-12
Original file line numberDiff line numberDiff line change
@@ -80,30 +80,32 @@ public static bool CanResolve(CompletionItem value)
8080
// Handler for "completionItem/resolve". In VSCode this is fired when a completion item is highlighted in the completion list.
8181
public async Task<CompletionItem> Handle(CompletionItem request, CancellationToken cancellationToken)
8282
{
83-
// We currently only support this request for anything that returns a CommandInfo: functions, cmdlets, aliases.
84-
if (request.Kind != CompletionItemKind.Function)
85-
{
86-
return request;
87-
}
88-
89-
// No details means the module hasn't been imported yet and Intellisense shouldn't import the module to get this info.
90-
if (request.Detail is null)
83+
// We currently only support this request for anything that returns a CommandInfo:
84+
// functions, cmdlets, aliases. No detail means the module hasn't been imported yet and
85+
// IntelliSense shouldn't import the module to get this info.
86+
if (request.Kind is not CompletionItemKind.Function || request.Detail is null)
9187
{
9288
return request;
9389
}
9490

9591
// Get the documentation for the function
96-
CommandInfo commandInfo = await CommandHelpers.GetCommandInfoAsync(request.Label, _runspaceContext.CurrentRunspace, _executionService).ConfigureAwait(false);
92+
CommandInfo commandInfo = await CommandHelpers.GetCommandInfoAsync(
93+
request.Label,
94+
_runspaceContext.CurrentRunspace,
95+
_executionService,
96+
cancellationToken).ConfigureAwait(false);
9797

9898
if (commandInfo is not null)
9999
{
100-
request = request with
100+
return request with
101101
{
102-
Documentation = await CommandHelpers.GetCommandSynopsisAsync(commandInfo, _executionService).ConfigureAwait(false)
102+
Documentation = await CommandHelpers.GetCommandSynopsisAsync(
103+
commandInfo,
104+
_executionService,
105+
cancellationToken).ConfigureAwait(false)
103106
};
104107
}
105108

106-
// Send back the updated CompletionItem
107109
return request;
108110
}
109111

0 commit comments

Comments
 (0)