I was getting a little annoyed with intellisense not loading automatically so I made this snippet.
Get-ChildItem $PWD*.ps1 -Recurse | ForEach-Object { [System.Management.Automation.Language.Parser]::ParseFile($PSItem.FullName, [ref]$null, [ref]$null).FindAll({ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] },$true).Extent.Text } | ForEach-Object { [scriptblock]::Create( $PSItem ) } | ForEach-Object { $PSItem. GetType(). GetMethod('DoInvokeReturnAsIs', [System.Reflection.BindingFlags]'NonPublic,Instance'). Invoke($PSItem, @( <# useLocalScope: #> $false, <# errorHandlingBehavior: #> [psobject].Assembly.GetType('System.Management.Automation.ScriptBlock+ErrorHandlingBehavior')::WriteToCurrentErrorPipe, <# dollarUnder: #> $null, <# input: #> $null, <# scriptThis: #> $null, <# args: #> $null )) }
Throw this in your VSCode profile (write code $profile
, highlight it, press F8). It will look in any ps1 files in your current workspace for function definitions, pull them out and load them into the current scope.
WARNING It will do this recursively and without warning. If you load a workspace with thousands of ps1 files you might have a bad time.
Sorry for the readability (or lack there of), just something I wrote quickly and thought I’d share because I know I’m not the only one having issues.
The purpose of using the DoInvokeReturnAsIs method is so it the functions aren’t defined in a isolated scope.
submitted by /u/SeeminglyScience
[link] [comments]
The post For anyone writing modules in VSCode appeared first on How to Code .NET.