Question: Can this be done for files outside of vs code?
There isn't a great way currently. I initially tied this to VSCode for a few reasons:
-
There wasn't a great way to pass parameters to SHiPS, so tying it to a specific object like
$psEditormade a lot of sense. In hindsight, you can just as easily set a static property when you create the drive. -
I intend to support some other provider capabilities like set content as soon as SHiPS supports it. Those capabilities make less sense outside of VSCode. I could just as easily throw a not supported exception though.
-
The initial design relied on functions from
PowerShellEditorServices.Commands. In the end, to get it working how I wanted I had to handle each AST type differently, which removed the need for that module.
I'll leave this open as an enhancement as I would like to add proper support for it. In the mean time, here's a work around if you want to test it out
function New-SpecificAstPSDrive {
[CmdletBinding()]
param([System.Management.Automation.Language.Ast] $Ast)
end {
# Create a mock $psEditor
class EditorObject {
[System.Management.Automation.Language.Ast] $_ast;
}
$psEditor = [EditorObject]::new()
$psEditor.psobject.Methods.Add(
[psscriptmethod]::new(
'GetEditorContext',
{
[PSCustomObject]@{
CurrentFile = [PSCustomObject]@{
Ast = $this._ast
}
}
# GetScriptBlock recreates the sb so it's not bound to this runspace, it's
# really slow without this.
}.Ast.GetScriptBlock()))
$psEditor._ast = $Ast
$module = Import-Module EditorAstProvider -Force -PassThru
New-EditorAstPSDrive
# Set the drives context to our mocked object
& $module {
param($psEditor)
[CurrentFileAst]::IsCommandsLoaded = $true
[CurrentFileAst]::EditorObject = $psEditor
} $psEditor
}
}
Yeah, passing params would be nice. For my Excel integration I passed the file name by setting it in a file :)
Can't wait to see :)