Skip to content

Fix FileNotFoundEx crash when Fold happens on untitled: scheme doc #839

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 2 commits into from
Jan 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
20 changes: 15 additions & 5 deletions src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1376,15 +1376,24 @@ protected Task HandleEvaluateRequest(

#region Event Handlers

private FoldingRange[] Fold(
string documentUri)
private FoldingRange[] Fold(string documentUri)
{
// TODO Should be using dynamic registrations
if (!this.currentSettings.CodeFolding.Enable) { return null; }

// Avoid crash when using untitled: scheme or any other scheme where the document doesn't
// have a backing file. https://github.com/PowerShell/vscode-powershell/issues/1676
// Perhaps a better option would be to parse the contents of the document as a string
// as opposed to reading a file but the senario of "no backing file" probably doesn't
// warrant the extra effort.
ScriptFile scriptFile;
if (!editorSession.Workspace.TryGetFile(documentUri, out scriptFile)) { return null; }

var result = new List<FoldingRange>();
foreach (FoldingReference fold in TokenOperations.FoldableRegions(
editorSession.Workspace.GetFile(documentUri).ScriptTokens,
this.currentSettings.CodeFolding.ShowLastLine))
FoldingReference[] foldableRegions =
TokenOperations.FoldableRegions(scriptFile.ScriptTokens, this.currentSettings.CodeFolding.ShowLastLine);

foreach (FoldingReference fold in foldableRegions)
{
result.Add(new FoldingRange {
EndCharacter = fold.EndCharacter,
Expand All @@ -1394,6 +1403,7 @@ private FoldingRange[] Fold(
StartLine = fold.StartLine
});
}

return result.ToArray();
}

Expand Down
9 changes: 4 additions & 5 deletions src/PowerShellEditorServices/Workspace/Workspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ public ScriptFile CreateScriptFileFromFileBuffer(string filePath, string initial
}

/// <summary>
/// Gets an open file in the workspace. If the file isn't open but
/// exists on the filesystem, load and return it.
/// Gets an open file in the workspace. If the file isn't open but exists on the filesystem, load and return it.
/// <para>IMPORTANT: Not all documents have a backing file e.g. untitled: scheme documents. Consider using
/// <see cref="Workspace.TryGetFile(string, out ScriptFile)"/> instead.</para>
/// </summary>
/// <param name="filePath">The file path at which the script resides.</param>
/// <exception cref="FileNotFoundException">
Expand Down Expand Up @@ -154,9 +155,7 @@ e is DirectoryNotFoundException ||
e is PathTooLongException ||
e is UnauthorizedAccessException)
{
this.logger.WriteException(
$"Failed to set breakpoint on file: {filePath}",
e);
this.logger.WriteHandledException($"Failed to get file for {nameof(filePath)}: '{filePath}'", e);
scriptFile = null;
return false;
}
Expand Down