-
Notifications
You must be signed in to change notification settings - Fork 223
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
New-EditorFile works on non-powershell untitled files #774
Conversation
@@ -101,6 +101,25 @@ public ScriptFile GetFile(string filePath) | |||
return scriptFile; | |||
} | |||
|
|||
/// <summary> | |||
/// Tries to get an open file in the workspace. Returns true or false if it succeeds. |
There was a problem hiding this comment.
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.".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tylerl0706
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️ this change. One suggestion and one nit
ScriptFile scriptFile = null; | ||
if (!this.editorSession.Workspace.TryGetFile(clientContext.CurrentFilePath, out scriptFile)) | ||
{ | ||
scriptFile = this.editorSession.Workspace.GetFileBuffer(clientContext.CurrentFilePath, clientContext.CurrentFileContent); |
There was a problem hiding this comment.
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
scriptFile = GetFile(filePath); | ||
return true; | ||
} | ||
catch (FileNotFoundException) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing there are other reasons for failure here, like in:
PowerShellEditorServices/src/PowerShellEditorServices/Language/LanguageService.cs
Lines 345 to 355 in 13f2107
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) |
It looks like we need to consolidate all our file access into a single internal API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should leave GetFile()
, move this large catch inside TryGetFile()
, and reuse TryGetFile()
in the LanguageService
and DebugAdapter
. It looks like the opposite consideration is in GetFile()
itself:
PowerShellEditorServices/src/PowerShellEditorServices/Workspace/Workspace.cs
Lines 81 to 98 in 834ae3d
if (!this.workspaceFiles.TryGetValue(keyName, out scriptFile)) | |
{ | |
// This method allows FileNotFoundException to bubble up | |
// if the file isn't found. | |
using (FileStream fileStream = new FileStream(resolvedFilePath, FileMode.Open, FileAccess.Read)) | |
using (StreamReader streamReader = new StreamReader(fileStream, Encoding.UTF8)) | |
{ | |
scriptFile = | |
new ScriptFile( | |
resolvedFilePath, | |
filePath, | |
streamReader, | |
this.powerShellVersion); | |
this.workspaceFiles.Add(keyName, scriptFile); | |
} | |
this.logger.Write(LogLevel.Verbose, "Opened file on disk: " + resolvedFilePath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to call this out.
We are logging the exception as well in the current implementation. Typically I've not seen TryX
functions give access to the underlying exception.
That said, I guess we could add an overload that will let you out the exception as well:
public bool TryGetFile(string filePath, out ScriptFile scriptFile, out Exception exception)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume we don't want to log the exception within the Try_
method? It would contain a full stack trace, but we could log it as a warning, since we used a Try_
.
3374d06
to
5807f08
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
oo CI failure on docs that's nice (pls still review as soon as you can because of time change) |
Ok this is ready for review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't wait for this :) awesome job! Just one question
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM -- just need to look at @rkeithhill's comment
Partnered with: [vscode-powershell issue]
This fixes the problem where
$psEditor.GetEditorContext()
assumed that the file open was a real file on the file system OR a powershell untitled file. Since that isn't always the case, It would throw an exception:This also means that
New-EditorFile -Value 'asdf'
actually works when you don't have your default language mode set to PowerShell.Now we try to get the file and if that throws a FileNotFound, we grab the ScriptFile from the FileBuffer
fixes #434