Skip to content

Addresses issue #40 - improved parameter completion list order #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,15 @@ await editorSession.LanguageService.GetCompletionsInFile(

if (completionResults != null)
{
int sortIndex = 1;
completionItems =
completionResults
.Completions
.Select(
c => CreateCompletionItem(
c,
completionResults.ReplacedRange))
completionResults.ReplacedRange,
sortIndex++))
.ToArray();
}
else
Expand Down Expand Up @@ -982,7 +984,8 @@ private static CompletionItemKind MapCompletionKind(CompletionType completionTyp

private static CompletionItem CreateCompletionItem(
CompletionDetails completionDetails,
BufferRange completionRange)
BufferRange completionRange,
int sortIndex)
{
string detailString = null;
string documentationString = null;
Expand Down Expand Up @@ -1031,13 +1034,26 @@ private static CompletionItem CreateCompletionItem(
}
}

// We want a special "sort order" for parameters that is not lexicographical.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent comment, thank you!

// Fortunately, PowerShell returns parameters in the preferred sort order by
// default (with common params at the end). We just need to make sure the default
// order also be the lexicographical order which we do by prefixig the ListItemText
// with a leading 0's four digit index. This would not sort correctly for a list
// > 999 parameters but surely we won't have so many items in the "parameter name"
// completion list. Technically we don't need the ListItemText at all but it may come
// in handy during debug.
var sortText = (completionDetails.CompletionType == CompletionType.ParameterName)
? string.Format("{0:D3}{1}", sortIndex, completionDetails.ListItemText)
: null;

return new CompletionItem
{
InsertText = completionDetails.CompletionText,
Label = labelString,
Kind = MapCompletionKind(completionDetails.CompletionType),
Detail = detailString,
Documentation = documentationString,
SortText = sortText,
TextEdit = new TextEdit
{
NewText = completionDetails.CompletionText,
Expand Down