Skip to content

Commit dccf461

Browse files
committed
Fix #50: High CPU usage on complete/hover app path
This change fixes an issue which causes high CPU usage and a delay when an application path is completed or hovered over. This is due to an unnecesary call to Get-Help which takes some time before it returns in this case.
1 parent be44edc commit dccf461

File tree

3 files changed

+48
-28
lines changed

3 files changed

+48
-28
lines changed

src/PowerShellEditorServices.Host/LanguageServer.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -427,13 +427,10 @@ protected async Task HandleCompletionResolveRequest(
427427
completionItem.Label,
428428
runspaceHandle.Runspace);
429429

430-
if (commandInfo != null)
431-
{
432-
completionItem.Documentation =
433-
CommandHelpers.GetCommandSynopsis(
434-
commandInfo,
435-
runspaceHandle.Runspace);
436-
}
430+
completionItem.Documentation =
431+
CommandHelpers.GetCommandSynopsis(
432+
commandInfo,
433+
runspaceHandle.Runspace);
437434

438435
runspaceHandle.Dispose();
439436
}

src/PowerShellEditorServices/Language/CommandHelpers.cs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,31 @@ public static string GetCommandSynopsis(
5252

5353
PSObject helpObject = null;
5454

55-
using (PowerShell powerShell = PowerShell.Create())
56-
{
57-
powerShell.Runspace = runspace;
58-
powerShell.AddCommand("Get-Help");
59-
powerShell.AddArgument(commandInfo);
60-
helpObject = powerShell.Invoke<PSObject>().FirstOrDefault();
61-
}
62-
63-
if (helpObject != null)
55+
if (commandInfo != null &&
56+
(commandInfo.CommandType == CommandTypes.Cmdlet ||
57+
commandInfo.CommandType == CommandTypes.Function ||
58+
commandInfo.CommandType == CommandTypes.Filter))
6459
{
65-
// Extract the synopsis string from the object
66-
synopsisString =
67-
(string)helpObject.Properties["synopsis"].Value ??
68-
string.Empty;
60+
using (PowerShell powerShell = PowerShell.Create())
61+
{
62+
powerShell.Runspace = runspace;
63+
powerShell.AddCommand("Get-Help");
64+
powerShell.AddArgument(commandInfo);
65+
helpObject = powerShell.Invoke<PSObject>().FirstOrDefault();
66+
}
6967

70-
// Ignore the placeholder value for this field
71-
if (string.Equals(synopsisString, "SHORT DESCRIPTION", System.StringComparison.InvariantCultureIgnoreCase))
68+
if (helpObject != null)
7269
{
73-
synopsisString = string.Empty;
70+
// Extract the synopsis string from the object
71+
synopsisString =
72+
(string)helpObject.Properties["synopsis"].Value ??
73+
string.Empty;
74+
75+
// Ignore the placeholder value for this field
76+
if (string.Equals(synopsisString, "SHORT DESCRIPTION", System.StringComparison.InvariantCultureIgnoreCase))
77+
{
78+
synopsisString = string.Empty;
79+
}
7480
}
7581
}
7682

src/PowerShellEditorServices/Language/SymbolDetails.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,29 @@ internal SymbolDetails(
5151
symbolReference.SymbolName,
5252
runspace);
5353

54-
this.Documentation =
55-
CommandHelpers.GetCommandSynopsis(
56-
commandInfo,
57-
runspace);
54+
if (commandInfo != null)
55+
{
56+
this.Documentation =
57+
CommandHelpers.GetCommandSynopsis(
58+
commandInfo,
59+
runspace);
5860

59-
this.DisplayString = "function " + symbolReference.SymbolName;
61+
if (commandInfo.CommandType == CommandTypes.Application)
62+
{
63+
this.DisplayString = "(application) " + symbolReference.SymbolName;
64+
}
65+
else
66+
{
67+
this.DisplayString = "function " + symbolReference.SymbolName;
68+
}
69+
}
70+
else
71+
{
72+
// Command information can't be loaded. This is likely due to
73+
// the symbol being a function that is defined in a file that
74+
// hasn't been loaded in the runspace yet.
75+
this.DisplayString = "function " + symbolReference.SymbolName;
76+
}
6077
}
6178
else if (symbolReference.SymbolType == SymbolType.Parameter)
6279
{

0 commit comments

Comments
 (0)