Skip to content

Commit ade6c72

Browse files
committed
Address PR feedback, use TryGetFile() instead of modifying GetFile
1 parent 830c40d commit ade6c72

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1386,8 +1386,8 @@ private FoldingRange[] Fold(string documentUri)
13861386
// Perhaps a better option would be to parse the contents of the document as a string
13871387
// as opposed to reading a file but the senario of "no backing file" probably doesn't
13881388
// warrant the extra effort.
1389-
ScriptFile scriptFile = editorSession.Workspace.GetFile(documentUri);
1390-
if (scriptFile == null) { return null; }
1389+
ScriptFile scriptFile;
1390+
if (!editorSession.Workspace.TryGetFile(documentUri, out scriptFile)) { return null; }
13911391

13921392
var result = new List<FoldingRange>();
13931393
FoldingReference[] foldableRegions =

src/PowerShellEditorServices/Workspace/Workspace.cs

+6-9
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ public ScriptFile CreateScriptFileFromFileBuffer(string filePath, string initial
9191
}
9292

9393
/// <summary>
94-
/// Gets an open file in the workspace. If the file isn't open but
95-
/// exists on the filesystem, load and return it.
94+
/// Gets an open file in the workspace. If the file isn't open but exists on the filesystem, load and return it.
95+
/// <para>IMPORTANT: Not all documents have a backing file e.g. untitled: scheme documents. Consider using
96+
/// <see cref="Workspace.TryGetFile(string, out ScriptFile)"/> instead.</para>
9697
/// </summary>
9798
/// <param name="filePath">The file path at which the script resides.</param>
9899
/// <exception cref="FileNotFoundException">
@@ -109,11 +110,9 @@ public ScriptFile GetFile(string filePath)
109110
string resolvedFilePath = this.ResolveFilePath(filePath);
110111
string keyName = resolvedFilePath.ToLower();
111112

112-
// Make sure the file isn't already loaded into the workspace and that the filePath isn't an "in-memory" path.
113-
// There have been crashes caused by this method being called with an "untitled:" document uri. See:
114-
// https://github.com/PowerShell/vscode-powershell/issues/1676
113+
// Make sure the file isn't already loaded into the workspace
115114
ScriptFile scriptFile = null;
116-
if (!this.workspaceFiles.TryGetValue(keyName, out scriptFile) && !IsPathInMemory(filePath))
115+
if (!this.workspaceFiles.TryGetValue(keyName, out scriptFile))
117116
{
118117
// This method allows FileNotFoundException to bubble up
119118
// if the file isn't found.
@@ -156,9 +155,7 @@ e is DirectoryNotFoundException ||
156155
e is PathTooLongException ||
157156
e is UnauthorizedAccessException)
158157
{
159-
this.logger.WriteException(
160-
$"Failed to set breakpoint on file: {filePath}",
161-
e);
158+
this.logger.WriteHandledException($"Failed to get file for {nameof(filePath)}: '{filePath}'", e);
162159
scriptFile = null;
163160
return false;
164161
}

0 commit comments

Comments
 (0)