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 6 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,10 @@ public static readonly

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

public string CurrentFileLanguage { 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,18 +90,23 @@ public Task SetSelection(BufferRange selectionRange)
public EditorContext ConvertClientEditorContext(
ClientEditorContext clientContext)
{
ScriptFile scriptFile = this.editorSession.Workspace.CreateScriptFileFromFileBuffer(
clientContext.CurrentFilePath,
clientContext.CurrentFileContent);

return
new EditorContext(
this,
this.editorSession.Workspace.GetFile(clientContext.CurrentFilePath),
scriptFile,
new BufferPosition(
clientContext.CursorPosition.Line + 1,
clientContext.CursorPosition.Character + 1),
new BufferRange(
clientContext.SelectionRange.Start.Line + 1,
clientContext.SelectionRange.Start.Character + 1,
clientContext.SelectionRange.End.Line + 1,
clientContext.SelectionRange.End.Character + 1));
clientContext.SelectionRange.End.Character + 1),
clientContext.CurrentFileLanguage);
}

public Task NewFile()
Expand Down
6 changes: 4 additions & 2 deletions src/PowerShellEditorServices/Extensions/EditorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ public class EditorContext
/// <param name="currentFile">The ScriptFile that is in the active editor buffer.</param>
/// <param name="cursorPosition">The position of the user's cursor in the active editor buffer.</param>
/// <param name="selectedRange">The range of the user's selection in the active editor buffer.</param>
/// <param name="language">Determines the language of the file.false If it is not specified, then it defaults to "Unknown"</param>
public EditorContext(
IEditorOperations editorOperations,
ScriptFile currentFile,
BufferPosition cursorPosition,
BufferRange selectedRange)
BufferRange selectedRange,
string language = "Unknown")
{
this.editorOperations = editorOperations;
this.CurrentFile = new FileContext(currentFile, this, editorOperations);
this.CurrentFile = new FileContext(currentFile, this, editorOperations, language);
this.SelectedRange = selectedRange;
this.CursorPosition = new FilePosition(currentFile, cursorPosition);
}
Expand Down
51 changes: 32 additions & 19 deletions src/PowerShellEditorServices/Extensions/FileContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,33 @@ public class FileContext
#region Properties

/// <summary>
/// Gets the filesystem path of the file.
/// Gets the parsed abstract syntax tree for the file.
/// </summary>
public string Path
public Ast Ast
{
get { return this.scriptFile.FilePath; }
get { return this.scriptFile.ScriptAst; }
}

/// <summary>
/// Gets the workspace-relative path of the file.
/// Gets a BufferRange which represents the entire content
/// range of the file.
/// </summary>
public string WorkspacePath
public BufferRange FileRange
{
get
{
return
this.editorOperations.GetWorkspaceRelativePath(
this.scriptFile.FilePath);
}
get { return this.scriptFile.FileRange; }
}

/// <summary>
/// Gets the parsed abstract syntax tree for the file.
/// Gets the language of the file.
/// </summary>
public Ast Ast
public string Language { get; private set; }

/// <summary>
/// Gets the filesystem path of the file.
/// </summary>
public string Path
{
get { return this.scriptFile.ScriptAst; }
get { return this.scriptFile.FilePath; }
}

/// <summary>
Expand All @@ -63,12 +64,16 @@ public Token[] Tokens
}

/// <summary>
/// Gets a BufferRange which represents the entire content
/// range of the file.
/// Gets the workspace-relative path of the file.
/// </summary>
public BufferRange FileRange
public string WorkspacePath
{
get { return this.scriptFile.FileRange; }
get
{
return
this.editorOperations.GetWorkspaceRelativePath(
this.scriptFile.FilePath);
}
}

#endregion
Expand All @@ -81,14 +86,22 @@ public BufferRange FileRange
/// <param name="scriptFile">The ScriptFile to which this file refers.</param>
/// <param name="editorContext">The EditorContext to which this file relates.</param>
/// <param name="editorOperations">An IEditorOperations implementation which performs operations in the editor.</param>
/// <param name="language">Determines the language of the file.false If it is not specified, then it defaults to "Unknown"</param>
public FileContext(
ScriptFile scriptFile,
EditorContext editorContext,
IEditorOperations editorOperations)
IEditorOperations editorOperations,
string language = "Unknown")
{
if (string.IsNullOrWhiteSpace(language))
{
language = "Unknown";
}

this.scriptFile = scriptFile;
this.editorContext = editorContext;
this.editorOperations = editorOperations;
this.Language = language;
}

#endregion
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
57 changes: 57 additions & 0 deletions src/PowerShellEditorServices/Workspace/Workspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,35 @@ public Workspace(Version powerShellVersion, ILogger logger)

#region Public Methods

/// <summary>
/// Creates a new ScriptFile instance which is identified by the given file
/// path and initially contains the given buffer contents.
/// </summary>
/// <param name="filePath">The file path for which a buffer will be retrieved.</param>
/// <param name="initialBuffer">The initial buffer contents if there is not an existing ScriptFile for this path.</param>
/// <returns>A ScriptFile instance for the specified path.</returns>
public ScriptFile CreateScriptFileFromFileBuffer(string filePath, string initialBuffer)
{
Validate.IsNotNullOrEmptyString("filePath", filePath);

// Resolve the full file path
string resolvedFilePath = this.ResolveFilePath(filePath);
string keyName = resolvedFilePath.ToLower();

ScriptFile scriptFile =
new ScriptFile(
resolvedFilePath,
filePath,
initialBuffer,
this.powerShellVersion);

this.workspaceFiles[keyName] = scriptFile;

this.logger.Write(LogLevel.Verbose, "Opened file as in-memory buffer: " + resolvedFilePath);

return scriptFile;
}

/// <summary>
/// Gets an open file in the workspace. If the file isn't open but
/// exists on the filesystem, load and return it.
Expand Down Expand Up @@ -101,6 +130,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