簡體   English   中英

Powershell_ise不會刷新外部修改

[英]Powershell_ise doesn't refresh modification done outside

如何刷新Powershell_ise以獲取IDE外部修改的內容。

大多數時候我會打開Powershell_ise和notepad ++

如果我在Powershell_ise中做了更改,notepad ++會要求重新加載,但如果我在notepad ++中修改,則無法在Powershell_ise中刷新。

是否有任何方式來刷新內容或我是否忽略了提供此功能的任何功能?

這篇文章很老了,但我想我會發布這個谷歌帶來了我同樣的問題。

我最終寫了這個小功能,它並沒有完全符合OP的要求,但也許其他googlers會發現它很有用:

function Build {
    #Reload file
    $CurrentFile = $psise.CurrentFile
    $FilePath = $CurrentFile.FullPath
    $PsISE.CurrentPowerShellTab.Files.remove($CurrentFile)
    $PsISE.CurrentPowerShellTab.Files.add($FilePath)

    iex $PsISE.CurrentPowerShellTab.Files.Editor.Text
}

$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Clear()
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Reload file and run",{Build},'f4')

它並不完美,但它對我來說已經足夠了。 一切都是創建一個關閉,重新打開,然后執行當前文件的鍵綁定。 它有點刺耳,因為當你運行它時,你會在文件關閉並重新打開時丟失當前光標位置。 我確定你可以存儲光標的列和行位置,並在重新加載時恢復它,但我暫時懶得打擾它。

編輯:我不小心發布了我的代碼的舊版非工作版本。 更新了工作版本。

PowerShell ISE不支持自動刷新已更改的文件。 即使在ISE v3中也不存在。

有關此主題的連接建議: https//connect.microsoft.com/PowerShell/feedback/details/711915/open-ise-files-should-update-when-edited-externally

但是,這可以使用PowerShell ISE對象模型和PowerShell事件來完成。 探索$ psise.CurrentFile和$ psise.CurrentPowerShellTab.Files集合。 這必須為您提供足夠的信息來編寫您自己的簡單插件。

以下是red888腳本的不同內容:

function Reload {

    $CurrentFile = $psise.CurrentFile
    $FilePath = $CurrentFile.FullPath

    $lineNum = $psise.CurrentFile.Editor.CaretLine
    $colNum = $psise.CurrentFile.Editor.CaretColumn

    $PsISE.CurrentPowerShellTab.Files.remove($CurrentFile) > $null

    $newFile = $PsISE.CurrentPowerShellTab.Files.add($FilePath)

    $newfile.Editor.SetCaretPosition($lineNum,$colNum)
}

$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Clear()
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Reload File",{Reload},'f4') > $null

它在重新加載后恢復了插入符號的位置。 我刪除了該行

iex $PsISE.CurrentPowerShellTab.Files.Editor.Text

因為我不需要它,它也與運行腳本不同(因此會導致像$script:MyInvocation.MyCommand.Path這樣的語句的奇怪行為)。

順便提一下,如果您將此代碼放在ISE配置文件中,它將在您第一次加載ISE時自動運行。 ISE配置文件只是一個powershell腳本,其位置由$profile變量給出。

以下是一些創建配置文件的命令(如果它不存在),然后打開它。 從ISE內部運行:

if (!(Test-Path (Split-Path $profile))) { mkdir (Split-Path $profile) } ;
if (!(Test-Path $profile)) { New-Item $profile -ItemType file } ;
notepad $profile

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM