Skip to content

Various fixes and improvements for 0.8.0 #314

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
Dec 9, 2016
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 @@ -141,6 +141,13 @@ protected async Task HandleInitializeRequest(
// Grab the workspace path from the parameters
editorSession.Workspace.WorkspacePath = initializeParams.RootPath;

// Set the working directory of the PowerShell session to the workspace path
if (editorSession.Workspace.WorkspacePath != null)
{
editorSession.PowerShellContext.SetWorkingDirectory(
editorSession.Workspace.WorkspacePath);
}

await requestContext.SendResult(
new InitializeResult
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.PowerShell.EditorServices.Protocol.LanguageServer;
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
using System.Threading.Tasks;
using System;

namespace Microsoft.PowerShell.EditorServices.Protocol.Server
{
Expand Down Expand Up @@ -111,6 +110,16 @@ public Task OpenFile(string filePath)
true);
}

public string GetWorkspacePath()
{
return this.editorSession.Workspace.WorkspacePath;
}

public string GetWorkspaceRelativePath(string filePath)
{
return this.editorSession.Workspace.GetRelativePath(filePath);
}

public Task ShowInformationMessage(string message)
{
return
Expand Down
12 changes: 12 additions & 0 deletions src/PowerShellEditorServices/Extensions/EditorWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ public class EditorWorkspace

#endregion

#region Properties

/// <summary>
/// Gets the current workspace path if there is one or null otherwise.
/// </summary>
public string Path
{
get { return this.editorOperations.GetWorkspacePath(); }
}

#endregion

#region Constructors

internal EditorWorkspace(IEditorOperations editorOperations)
Expand Down
13 changes: 13 additions & 0 deletions src/PowerShellEditorServices/Extensions/FileContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ public string Path
get { return this.scriptFile.FilePath; }
}

/// <summary>
/// Gets the workspace-relative path of the file.
/// </summary>
public string WorkspacePath
{
get
{
return
this.editorOperations.GetWorkspaceRelativePath(
this.scriptFile.FilePath);
}
}

/// <summary>
/// Gets the parsed abstract syntax tree for the file.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions src/PowerShellEditorServices/Extensions/IEditorOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ public interface IEditorOperations
/// <returns>A new EditorContext object.</returns>
Task<EditorContext> GetEditorContext();

/// <summary>
/// Gets the path to the editor's active workspace.
/// </summary>
/// <returns>The workspace path or null if there isn't one.</returns>
string GetWorkspacePath();

/// <summary>
/// Resolves the given file path relative to the current workspace path.
/// </summary>
/// <param name="filePath">The file path to be resolved.</param>
/// <returns>The resolved file path.</returns>
string GetWorkspaceRelativePath(string filePath);

/// <summary>
/// Causes a file to be opened in the editor. If the file is
/// already open, the editor must switch to the file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public FindDotSourcedVisitor()
/// or a decision to continue if it wasn't found</returns>
public override AstVisitAction VisitCommand(CommandAst commandAst)
{
if (commandAst.InvocationOperator.Equals(TokenKind.Dot))
if (commandAst.InvocationOperator.Equals(TokenKind.Dot) &&
commandAst.CommandElements[0] is StringConstantExpressionAst)
{
// Strip any quote characters off of the string
string fileName = commandAst.CommandElements[0].Extent.Text.Trim('\'', '"');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,8 @@ private void WaitForPromptCompletion<TResult>(
try
{
// This will synchronously block on the prompt task
// method which gets run on another thread. Use a
// 30 second timeout so that everything doesn't get
// backed up if the user doesn't respond.
promptTask.Wait(15000);
// method which gets run on another thread.
promptTask.Wait();

if (promptTask.Status == TaskStatus.WaitingForActivation)
{
Expand Down
33 changes: 31 additions & 2 deletions src/PowerShellEditorServices/Workspace/Workspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,32 @@ public ScriptFile[] ExpandScriptReferences(ScriptFile scriptFile)
return expandedReferences.ToArray();
}

/// <summary>
/// Gets the workspace-relative path of the given file path.
/// </summary>
/// <param name="filePath">The original full file path.</param>
/// <returns>A relative file path</returns>
public string GetRelativePath(string filePath)
{
string resolvedPath = filePath;

if (!IsPathInMemory(filePath) && !string.IsNullOrEmpty(this.WorkspacePath))
{
Uri workspaceUri = new Uri(this.WorkspacePath);
Uri fileUri = new Uri(filePath);

resolvedPath = workspaceUri.MakeRelativeUri(fileUri).ToString();

// Convert the directory separators if necessary
if (System.IO.Path.DirectorySeparatorChar == '\\')
{
resolvedPath = resolvedPath.Replace('/', '\\');
}
}

return resolvedPath;
}

#endregion

#region Private Methods
Expand Down Expand Up @@ -264,10 +290,13 @@ internal static bool IsPathInMemory(string filePath)
// When viewing PowerShell files in the Git diff viewer, VS Code
// sends the contents of the file at HEAD with a URI that starts
// with 'inmemory'. Untitled files which have been marked of
// type PowerShell have a path starting with 'untitled'.
// type PowerShell have a path starting with 'untitled'. Files
// opened from the find/replace view will be prefixed with
// 'private'.
return
filePath.StartsWith("inmemory") ||
filePath.StartsWith("untitled");
filePath.StartsWith("untitled") ||
filePath.StartsWith("private");
}

private string GetBaseFilePath(string filePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ await this.extensionEventQueue.EnqueueAsync(

public class TestEditorOperations : IEditorOperations
{
public string GetWorkspacePath()
{
throw new NotImplementedException();
}

public string GetWorkspaceRelativePath(string filePath)
{
throw new NotImplementedException();
}

public Task OpenFile(string filePath)
{
throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Compile Include="Console\ConsoleServiceTests.cs" />
<Compile Include="Console\ChoicePromptHandlerTests.cs" />
<Compile Include="Session\ScriptFileTests.cs" />
<Compile Include="Session\WorkspaceTests.cs" />
<Compile Include="Utility\AsyncDebouncerTests.cs" />
<Compile Include="Utility\AsyncLockTests.cs" />
<Compile Include="Utility\AsyncQueueTests.cs" />
Expand Down
38 changes: 38 additions & 0 deletions test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using Microsoft.PowerShell.EditorServices;
using System;
using System.IO;
using System.Linq;
using Xunit;

namespace Microsoft.PowerShell.EditorServices.Test.Session
{
public class WorkspaceTests
{
private static readonly Version PowerShellVersion = new Version("5.0");

[Fact]
public void CanResolveWorkspaceRelativePath()
{
string workspacePath = @"c:\Test\Workspace\";
string testPathInside = @"c:\Test\Workspace\SubFolder\FilePath.ps1";
string testPathOutside = @"c:\Test\PeerPath\FilePath.ps1";
string testPathAnotherDrive = @"z:\TryAndFindMe\FilePath.ps1";

Workspace workspace = new Workspace(PowerShellVersion);

// Test without a workspace path
Assert.Equal(testPathOutside, workspace.GetRelativePath(testPathOutside));

// Test with a workspace path
workspace.WorkspacePath = workspacePath;
Assert.Equal(@"SubFolder\FilePath.ps1", workspace.GetRelativePath(testPathInside));
Assert.Equal(@"..\PeerPath\FilePath.ps1", workspace.GetRelativePath(testPathOutside));
Assert.Equal(testPathAnotherDrive, workspace.GetRelativePath(testPathAnotherDrive));
}
}
}