Skip to content

Commit 4859f7c

Browse files
committed
Addresses issue #40 - improved parameter completion list order
This takes advantage of the fact that PowerShell returns us a list of parameter names already in the preferred order (common params at the end). We just need to make the SortText for each completion result preserve the initial order.
1 parent 15ce25f commit 4859f7c

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,13 +431,15 @@ await editorSession.LanguageService.GetCompletionsInFile(
431431

432432
if (completionResults != null)
433433
{
434+
int sortIndex = 1;
434435
completionItems =
435436
completionResults
436437
.Completions
437438
.Select(
438439
c => CreateCompletionItem(
439440
c,
440-
completionResults.ReplacedRange))
441+
completionResults.ReplacedRange,
442+
sortIndex++))
441443
.ToArray();
442444
}
443445
else
@@ -982,7 +984,8 @@ private static CompletionItemKind MapCompletionKind(CompletionType completionTyp
982984

983985
private static CompletionItem CreateCompletionItem(
984986
CompletionDetails completionDetails,
985-
BufferRange completionRange)
987+
BufferRange completionRange,
988+
int sortIndex)
986989
{
987990
string detailString = null;
988991
string documentationString = null;
@@ -1031,13 +1034,26 @@ private static CompletionItem CreateCompletionItem(
10311034
}
10321035
}
10331036

1037+
// We want a special "sort order" for parameters that is not lexicographical.
1038+
// Fortunately, PowerShell returns parameters in the preferred sort order by
1039+
// default (with common params at the end). We just need to make sure the default
1040+
// order also be the lexicographical order which we do by prefixig the ListItemText
1041+
// with a leading 0's four digit index. This would not sort correctly for a list
1042+
// > 999 parameters but surely we won't have so many items in the "parameter name"
1043+
// completion list. Technically we don't need the ListItemText at all but it may come
1044+
// in handy during debug.
1045+
var sortText = (completionDetails.CompletionType == CompletionType.ParameterName)
1046+
? string.Format("{0:D3}{1}", sortIndex, completionDetails.ListItemText)
1047+
: null;
1048+
10341049
return new CompletionItem
10351050
{
10361051
InsertText = completionDetails.CompletionText,
10371052
Label = labelString,
10381053
Kind = MapCompletionKind(completionDetails.CompletionType),
10391054
Detail = detailString,
10401055
Documentation = documentationString,
1056+
SortText = sortText,
10411057
TextEdit = new TextEdit
10421058
{
10431059
NewText = completionDetails.CompletionText,

0 commit comments

Comments
 (0)