EditorAstProvider icon indicating copy to clipboard operation
EditorAstProvider copied to clipboard

Question: Can this be done for files outside of vs code?

Open dfinke opened this issue 8 years ago • 3 comments

dfinke avatar Nov 16 '17 21:11 dfinke

There isn't a great way currently. I initially tied this to VSCode for a few reasons:

  1. There wasn't a great way to pass parameters to SHiPS, so tying it to a specific object like $psEditor made a lot of sense. In hindsight, you can just as easily set a static property when you create the drive.

  2. 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.

  3. 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
    }
}

SeeminglyScience avatar Nov 16 '17 23:11 SeeminglyScience

Yeah, passing params would be nice. For my Excel integration I passed the file name by setting it in a file :)

dfinke avatar Nov 17 '17 00:11 dfinke

Can't wait to see :)

SeeminglyScience avatar Nov 17 '17 04:11 SeeminglyScience