Skip to content

Improve path auto-completion #902

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
merged 5 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions src/PowerShellEditorServices.Protocol/LanguageServer/Completion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public enum CompletionItemKind
Folder = 19
}

public enum InsertTextFormat
{
PlainText = 1,
Snippet = 2,
}

[DebuggerDisplay("NewText = {NewText}, Range = {Range.Start.Line}:{Range.Start.Character} - {Range.End.Line}:{Range.End.Character}")]
public class TextEdit
{
Expand All @@ -69,6 +75,11 @@ public class TextEdit
[DebuggerDisplay("Kind = {Kind.ToString()}, Label = {Label}, Detail = {Detail}")]
public class CompletionItem
{
public CompletionItem()
{
this.InsertTextFormat = InsertTextFormat.PlainText;
}

public string Label { get; set; }

public CompletionItemKind? Kind { get; set; }
Expand All @@ -86,6 +97,8 @@ public class CompletionItem

public string InsertText { get; set; }

public InsertTextFormat InsertTextFormat { get; set; }

public Range Range { get; set; }

public string[] CommitCharacters { get; set; }
Expand Down
21 changes: 19 additions & 2 deletions src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,8 @@ private static CompletionItem CreateCompletionItem(
{
string detailString = null;
string documentationString = null;
string completionText = completionDetails.CompletionText;
InsertTextFormat insertTextFormat = InsertTextFormat.PlainText;

if ((completionDetails.CompletionType == CompletionType.Variable) ||
(completionDetails.CompletionType == CompletionType.ParameterName))
Expand Down Expand Up @@ -1956,6 +1958,20 @@ private static CompletionItem CreateCompletionItem(
}
}
}
else if (((completionDetails.CompletionType == CompletionType.File) ||
(completionDetails.CompletionType == CompletionType.Folder)) &&
(completionText.EndsWith("\"") || completionText.EndsWith("'")))
{
// Insert a final "tab stop" as identified by $0 in the snippet provided for completion.
// For paths, we take the path returned by PowerShell e.g. 'C:\Program Files' and insert
// the tab stop marker before the closing quote char e.g. 'C:\Program Files$0'.
// This causes the editing cursor to be placed *before* the final quote after completion,
// which makes subsequent path completions work. See this part of the LSP spec for details:
// https://microsoft.github.io/language-server-protocol/specification#textDocument_completion
int len = completionDetails.CompletionText.Length;
completionText = completionDetails.CompletionText.Insert(len - 1, "$0");
insertTextFormat = InsertTextFormat.Snippet;
}

// Force the client to maintain the sort order in which the
// original completion results were returned. We just need to
Expand All @@ -1966,7 +1982,8 @@ private static CompletionItem CreateCompletionItem(

return new CompletionItem
{
InsertText = completionDetails.CompletionText,
InsertText = completionText,
InsertTextFormat = insertTextFormat,
Label = completionDetails.ListItemText,
Kind = MapCompletionKind(completionDetails.CompletionType),
Detail = detailString,
Expand All @@ -1975,7 +1992,7 @@ private static CompletionItem CreateCompletionItem(
FilterText = completionDetails.CompletionText,
TextEdit = new TextEdit
{
NewText = completionDetails.CompletionText,
NewText = completionText,
Range = new Range
{
Start = new Position
Expand Down