有一個(gè)讓我非常喜歡Windows PowerShell ISE的理由,就是它將它的基礎(chǔ)腳本對象模型暴露給用戶,這樣就允許用戶按照自己的方式和需要去自定義腳本體驗(yàn)。
自定義ISE的核心是$psISE對象。$psISE對象允許用戶去控制ISE許多方面的功能。你可以從這里獲取關(guān)于$psISE的分層對象模型的介紹,和與這些對象相關(guān)聯(lián)的功能。
想象一下,你不得不分析一個(gè)相對龐大的PowerShell腳本。那這個(gè)腳本可能是別人寫的,也有可能是你自己幾個(gè)月前寫的,扔了好久了。PowerShell ISE已經(jīng)做了件非常棒的工作了,它提供了腳本環(huán)境。你可以通過添加Add-On(附加工具)來擴(kuò)充它的功能,讓你的腳本體驗(yàn)更好,更高效。從PowerShell 3.0開始,腳本的抽象語法樹(AST)就可以使用語法解釋器接口非常方便的獲取了。下面的腳本行會獲取當(dāng)前打開的ISE中的腳本的AST:
撇開函數(shù)定位的定義,如果我們能回到光標(biāo)之前出現(xiàn)的位置,那將太漂亮了。實(shí)現(xiàn)這個(gè)也非常簡單。我們所要做的只是存儲這些行號,然后按照反轉(zhuǎn)順序反轉(zhuǎn)他們。(是否有人已經(jīng)知道了,“堆棧”)
#Define some useful global variables
$global:__ISEGoToAddOncurrLine=1
$global:__ISEGoToAddOncurrcol=1
$global:__ISEGoToAddOnlineToGoTo=1
$global:__ISEGoToAddOncolToGoTo=1
#We need two stacks - one each for line and column
$global:__ISEGoToAddOnstackOfLine = New-Object System.Collections.Stack
$global:__ISEGoToAddOnstackOfCol = New-Object System.Collections.Stack
#This script block has the logic for the implementation of the Go-To definition functionality
$global:__ISEGoToAddOnscriptBlockGoTo =
{
$AbstractSyntaxTree =[System.Management.Automation.Language.Parser]::ParseInput($psISE.CurrentFile.Editor.Text,[ref]$null, [ref]$null)
$functionsInFile = $AbstractSyntaxTree.FindAll(
{$args[0] -is[System.Management.Automation.Language.FunctionDefinitionAst]}, $true)
#Get the text of the line where we have the cursor
$str = $psISE.CurrentFile.Editor.CaretLineText
#Store them on the stack for later use
$global:__ISEGoToAddOnstackOfLine.Push($psISE.CurrentFile.Editor.CaretLine)
$global:__ISEGoToAddOnstackOfCol.Push($psISE.CurrentFile.Editor.CaretColumn)
$global:__ISEGoToAddOncurrLine = $global:__ISEGoToAddOnstackOfLine.Peek()
$global:__ISEGoToAddOncurrcol = $global:__ISEGoToAddOnstackOfCol.Peek()
#Get the selected text so that it can be used for searching existing functions
$selectedFunction = $psISE.CurrentFile.Editor.SelectedText
#Ensure that the cursor is somewhere between the word boundaries of the function
$functionsInFile | %{if(($str.Contains($_.name)) `
主站蜘蛛池模板:
水富县|
河津市|
德保县|
双城市|
栖霞市|
松潘县|
霍州市|
蕉岭县|
银川市|
景泰县|
昌吉市|
蕉岭县|
缙云县|
景东|
汉川市|
霍林郭勒市|
鹤壁市|
石嘴山市|
乡宁县|
沛县|
芦溪县|
漯河市|
大理市|
前郭尔|
天全县|
镇安县|
渭源县|
石河子市|
阿克陶县|
新丰县|
加查县|
秭归县|
鄄城县|
同仁县|
金堂县|
黎城县|
六安市|
夹江县|
从江县|
格尔木市|
湟中县|