10

如何为在 IDE 之外修改的内容刷新 Powershell_ise。

大多数时候我会同时打开 Powershell_ise 和 notepad++

如果我在 Powershell_ise 中进行了更改,notepad++ 会要求重新加载,但如果我在 notepad++ 中进行修改,则无法在 Powershell_ise 中刷新。

无论是刷新内容的任何方式还是我忽略了提供此内容的任何功能?

4

3 回答 3

5

这篇文章很旧,但我想我会发布这个,因为谷歌把我带到了这里,遇到了同样的问题。

我最终只是写了这个小函数,它并不能完全满足 OP 的要求,但也许其他谷歌人会发现它很有用:

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')

它并不完美,但对我来说已经足够了。所做的只是创建一个关闭、重新打开然后执行当前文件的键绑定。虽然它有点不和谐,因为当你运行它时,当文件关闭并重新打开时,你会丢失当前的光标位置。我确定您可以存储光标的列和行位置并在重新加载时恢复它,但我暂时懒得理会。

编辑:我不小心发布了我的代码的较旧的非工作版本。更新为工作版本。

于 2014-05-30T17:03:18.323 回答
4

下面是对 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
于 2015-09-03T13:00:50.433 回答
3

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 集合。这必须为您提供足够的信息来编写您自己的简单插件。

于 2012-01-09T14:48:01.600 回答