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 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
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 Down Expand Up @@ -86,6 +92,8 @@ public class CompletionItem

public string InsertText { get; set; }

public InsertTextFormat InsertTextFormat { get; set; } = InsertTextFormat.PlainText;

public Range Range { get; set; }

public string[] CommitCharacters { get; set; }
Expand Down
20 changes: 18 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,19 @@ private static CompletionItem CreateCompletionItem(
}
}
}
else if ((completionDetails.CompletionType == CompletionType.Folder) &&
(completionText.EndsWith("\"") || completionText.EndsWith("'")))
{
// Insert a final "tab stop" as identified by $0 in the snippet provided for completion.
// For folder 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 +1981,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 +1991,7 @@ private static CompletionItem CreateCompletionItem(
FilterText = completionDetails.CompletionText,
TextEdit = new TextEdit
{
NewText = completionDetails.CompletionText,
NewText = completionText,
Range = new Range
{
Start = new Position
Expand Down
113 changes: 113 additions & 0 deletions test/PowerShellEditorServices.Test.Host/LanguageServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -257,6 +258,97 @@ await this.SendRequest(
Assert.True(updatedCompletionItem.Documentation.Length > 0);
}

[Fact]
public async Task CompletesDetailOnFilePathSuggestion()
{
string expectedPathSnippet;

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
expectedPathSnippet = @".\TestFiles\CompleteFunctionName.ps1";
}
else
{
expectedPathSnippet = "./TestFiles/CompleteFunctionName.ps1";
}

// Change dir to root of this test project's folder
await this.SetLocationForServerTest(this.TestRootDir);

await this.SendOpenFileEvent(TestUtilities.NormalizePath("TestFiles/CompleteFunctionName.ps1"));

CompletionItem[] completions =
await this.SendRequest(
CompletionRequest.Type,
new TextDocumentPositionParams
{
TextDocument = new TextDocumentIdentifier
{
Uri = TestUtilities.NormalizePath("TestFiles/CompleteFunctionName.ps1")
},
Position = new Position
{
Line = 8,
Character = 35
}
});

CompletionItem completionItem =
completions
.FirstOrDefault(
c => c.InsertText == expectedPathSnippet);

Assert.NotNull(completionItem);
Assert.Equal(InsertTextFormat.PlainText, completionItem.InsertTextFormat);
}

[Fact]
public async Task CompletesDetailOnFolderPathSuggestion()
{
string expectedPathSnippet;
InsertTextFormat insertTextFormat;

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
expectedPathSnippet = @"'.\TestFiles\Folder With Spaces$0'";
insertTextFormat = InsertTextFormat.Snippet;
}
else
{
expectedPathSnippet = @"'./TestFiles/Folder With Spaces$0'";
insertTextFormat = InsertTextFormat.Snippet;
}

// Change dir to root of this test project's folder
await this.SetLocationForServerTest(this.TestRootDir);

await this.SendOpenFileEvent(TestUtilities.NormalizePath("TestFiles/CompleteFunctionName.ps1"));

CompletionItem[] completions =
await this.SendRequest(
CompletionRequest.Type,
new TextDocumentPositionParams
{
TextDocument = new TextDocumentIdentifier
{
Uri = TestUtilities.NormalizePath("TestFiles/CompleteFunctionName.ps1")
},
Position = new Position
{
Line = 7,
Character = 32
}
});

CompletionItem completionItem =
completions
.FirstOrDefault(
c => c.InsertText == expectedPathSnippet);

Assert.NotNull(completionItem);
Assert.Equal(insertTextFormat, completionItem.InsertTextFormat);
}

[Fact]
public async Task FindsReferencesOfVariable()
{
Expand Down Expand Up @@ -826,6 +918,27 @@ await this.SendRequest(
Assert.Equal(expectedArchitecture, versionDetails.Architecture);
}

private string TestRootDir
{
get
{
string assemblyDir = Path.GetDirectoryName(this.GetType().Assembly.Location);
return Path.Combine(assemblyDir, @"..\..\..");
}
}

private async Task SetLocationForServerTest(string path)
{
// Change dir to root of this test project's folder
await this.SendRequest(
EvaluateRequest.Type,
new EvaluateRequestArguments
{
Expression = $"Set-Location {path}",
Context = "repl"
});
}

private async Task SendOpenFileEvent(string filePath, bool waitForDiagnostics = true)
{
string fileContents = string.Join(Environment.NewLine, File.ReadAllLines(filePath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ function My-Function
$Cons
My-
Get-Proc
$HKC
$HKC
Get-ChildItem ./TestFiles/Folder
Get-ChildItem ./TestFiles/CompleteF