Skip to content

New-EditorFile works on non-powershell untitled files #774

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 7 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -38,6 +38,8 @@ public static readonly

public class ClientEditorContext
{
public string CurrentFileContent { get; set; }

public string CurrentFilePath { get; set; }

public Position CursorPosition { get; set; }
Expand Down
31 changes: 7 additions & 24 deletions src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -494,31 +494,14 @@ protected async Task HandleSetBreakpointsRequest(
{
ScriptFile scriptFile = null;

// Fix for issue #195 - user can change name of file outside of VSCode in which case
// VSCode sends breakpoint requests with the original filename that doesn't exist anymore.
try
{
// When you set a breakpoint in the right pane of a Git diff window on a PS1 file,
// the Source.Path comes through as Untitled-X.
if (!ScriptFile.IsUntitledPath(setBreakpointsParams.Source.Path))
{
scriptFile = _editorSession.Workspace.GetFile(setBreakpointsParams.Source.Path);
}
}
catch (Exception e) when (
e is FileNotFoundException ||
e is DirectoryNotFoundException ||
e is IOException ||
e is NotSupportedException ||
e is PathTooLongException ||
e is SecurityException ||
e is UnauthorizedAccessException)
// When you set a breakpoint in the right pane of a Git diff window on a PS1 file,
// the Source.Path comes through as Untitled-X. That's why we check for IsUntitledPath.
if (!ScriptFile.IsUntitledPath(setBreakpointsParams.Source.Path) &&
!_editorSession.Workspace.TryGetFile(
setBreakpointsParams.Source.Path,
out scriptFile))
{
Logger.WriteException(
$"Failed to set breakpoint on file: {setBreakpointsParams.Source.Path}",
e);

string message = _noDebug ? string.Empty : "Source file could not be accessed, breakpoint not set - " + e.Message;
string message = _noDebug ? string.Empty : "Source file could not be accessed, breakpoint not set.";
var srcBreakpoints = setBreakpointsParams.Breakpoints
.Select(srcBkpt => Protocol.DebugAdapter.Breakpoint.Create(
srcBkpt, setBreakpointsParams.Source.Path, message, verified: _noDebug));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using Microsoft.PowerShell.EditorServices;
using Microsoft.PowerShell.EditorServices.Extensions;
using Microsoft.PowerShell.EditorServices.Protocol.LanguageServer;
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
Expand Down Expand Up @@ -89,10 +90,17 @@ public Task SetSelection(BufferRange selectionRange)
public EditorContext ConvertClientEditorContext(
ClientEditorContext clientContext)
{

ScriptFile scriptFile = null;
if (!this.editorSession.Workspace.TryGetFile(clientContext.CurrentFilePath, out scriptFile))
{
scriptFile = this.editorSession.Workspace.GetFileBuffer(clientContext.CurrentFilePath, clientContext.CurrentFileContent);
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Consider putting each arg on a new line

}

return
new EditorContext(
this,
this.editorSession.Workspace.GetFile(clientContext.CurrentFilePath),
scriptFile,
new BufferPosition(
clientContext.CursorPosition.Line + 1,
clientContext.CursorPosition.Character + 1),
Expand Down
12 changes: 1 addition & 11 deletions src/PowerShellEditorServices/Language/LanguageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,17 +342,7 @@ public async Task<FindReferencesResult> FindReferencesOfSymbol(
{
if (!fileMap.Contains(file))
{
ScriptFile scriptFile;
try
{
scriptFile = workspace.GetFile(file);
}
catch (Exception e) when (e is IOException
|| e is SecurityException
|| e is FileNotFoundException
|| e is DirectoryNotFoundException
|| e is PathTooLongException
|| e is UnauthorizedAccessException)
if (!workspace.TryGetFile(file, out ScriptFile scriptFile))
{
// If we can't access the file for some reason, just ignore it
continue;
Expand Down
28 changes: 28 additions & 0 deletions src/PowerShellEditorServices/Workspace/Workspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,34 @@ public ScriptFile GetFile(string filePath)
return scriptFile;
}

/// <summary>
/// Tries to get an open file in the workspace. Returns true or false if it succeeds.
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor nit - would probably phrase the end of the summary this way "Returns true if it succeeds, false otherwise.".

Copy link
Contributor

Choose a reason for hiding this comment

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

@tylerl0706

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed :)

/// </summary>
/// <param name="filePath">The file path at which the script resides.</param>
/// <param name="scriptFile">The out parameter that will contain the ScriptFile object.</param>
public bool TryGetFile(string filePath, out ScriptFile scriptFile)
{
try
{
scriptFile = GetFile(filePath);
return true;
}
catch (Exception e) when (
e is IOException ||
e is SecurityException ||
e is FileNotFoundException ||
e is DirectoryNotFoundException ||
e is PathTooLongException ||
e is UnauthorizedAccessException)
{
this.logger.WriteException(
$"Failed to set breakpoint on file: {filePath}",
e);
scriptFile = null;
return false;
}
}

/// <summary>
/// Gets a new ScriptFile instance which is identified by the given file path.
/// </summary>
Expand Down