简体   繁体   English

Powershell_ise不会刷新外部修改

[英]Powershell_ise doesn't refresh modification done outside

How to refresh Powershell_ise for contents modified outside the IDE. 如何刷新Powershell_ise以获取IDE外部修改的内容。

Most of the time i would have opened both Powershell_ise and notepad++ 大多数时候我会打开Powershell_ise和notepad ++

If i does changes in Powershell_ise , notepad++ asks for reload but if i modify in notepad++ there is no way to refresh in Powershell_ise. 如果我在Powershell_ise中做了更改,notepad ++会要求重新加载,但如果我在notepad ++中修改,则无法在Powershell_ise中刷新。

Whether any way to refresh the content or am i overlooking any feature which provides this? 是否有任何方式来刷新内容或我是否忽略了提供此功能的任何功能?

This post is old, but I figured I'd post this as google brought me here with the same issue. 这篇文章很老了,但我想我会发布这个谷歌带来了我同样的问题。

I eventually just wrote this little function which doesn't do exactly what the OP wanted, but maybe other googlers will find it useful: 我最终写了这个小功能,它并没有完全符合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')

Its not perfect, but its good enough for me for now. 它并不完美,但它对我来说已经足够了。 All is does is create a key binding that closes,reopens, and then executes the current file. 一切都是创建一个关闭,重新打开,然后执行当前文件的键绑定。 Its a bit jarring though because when you run it you'll lose your current cursor position when the file is closed and reopened. 它有点刺耳,因为当你运行它时,你会在文件关闭并重新打开时丢失当前光标位置。 I'm sure you could store the column and line position of the cursor and restore it when reloading, but I'm too lazy to bother with that for the time being. 我确定你可以存储光标的列和行位置,并在重新加载时恢复它,但我暂时懒得打扰它。

Edit: I accidentally posted an older non-working version of my code. 编辑:我不小心发布了我的代码的旧版非工作版本。 Updated with working version. 更新了工作版本。

PowerShell ISE does not support refreshing the changed files automatically. PowerShell ISE不支持自动刷新已更改的文件。 It is not there even in ISE v3. 即使在ISE v3中也不存在。

There is connect suggestion on this topic: https://connect.microsoft.com/PowerShell/feedback/details/711915/open-ise-files-should-update-when-edited-externally 有关此主题的连接建议: https//connect.microsoft.com/PowerShell/feedback/details/711915/open-ise-files-should-update-when-edited-externally

However, this can be done using PowerShell ISE Object model and PowerShell eventing. 但是,这可以使用PowerShell ISE对象模型和PowerShell事件来完成。 Explore $psise.CurrentFile and $psise.CurrentPowerShellTab.Files collection. 探索$ psise.CurrentFile和$ psise.CurrentPowerShellTab.Files集合。 This must give you enough information to write your own simple addon. 这必须为您提供足够的信息来编写您自己的简单插件。

Here is a different spin on red888's script: 以下是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

It restores the position of the caret after reload. 它在重新加载后恢复了插入符号的位置。 I removed the line 我删除了该行

iex $PsISE.CurrentPowerShellTab.Files.Editor.Text

As I didn't need it and its also not the same as running the script (and so results in strange behavior of statements like $script:MyInvocation.MyCommand.Path ). 因为我不需要它,它也与运行脚本不同(因此会导致像$script:MyInvocation.MyCommand.Path这样的语句的奇怪行为)。

Incidentally, if you put this code in your ISE profile it will automatically run when you first load the ISE. 顺便提一下,如果您将此代码放在ISE配置文件中,它将在您第一次加载ISE时自动运行。 The ISE profile is just a powershell script whose location is given by the $profile variable. ISE配置文件只是一个powershell脚本,其位置由$profile变量给出。

Here are some commands that create the profile if it doesn't exist, and then opens it. 以下是一些创建配置文件的命令(如果它不存在),然后打开它。 Run it from inside the ISE: 从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