gpt4 book ai didi

c# - 在 C# 交互式窗口中跟踪更改

转载 作者:太空狗 更新时间:2023-10-29 17:47:38 25 4
gpt4 key购买 nike

VS 现在带有一个交互式窗口,但与运行原始 CSI.EXE Roslyn 进程不同,Visual Studio 添加了 IntelliSense 和一些其他功能,例如能够在当前项目中加载。

我想编写一个 VS 插件来跟踪此窗口中所有文本编辑器的更改。这可能吗?我正在寻找的是类似于 PreviewKeyDown/PreviewTextInput WPF 事件的东西。我能否在 C# 交互式窗口中获取这些内容?如果可以,如何获取?

这是我到目前为止的进展:

var dte = Shell.Instance.GetComponent<DTE>();
foreach (Window window in dte.MainWindow.Collection)
{
if (window.Kind.ToUpper().Contains("TOOL"))
{
if (window.Caption == "C# Interactive")
{
WpfWindow wpfWindow = (WpfWindow)HwndSource.FromHwnd((IntPtr) window.HWnd).RootVisual;
for (int i = 0; i < VTH.GetChildrenCount(wpfWindow); ++i)
{
// now what?
}
}
}
}

最佳答案

这里有一些代码会得到 IWpfTextViewHost关于 C# 交互式窗口的引用。从那里,您可以访问 Visual Studio 中的所有文本服务:文本行、文本缓冲区等(或者您可以直接 Hook WPF 的控件,我不推荐这样做)

// get global UI shell service from a service provider
var shell = (IVsUIShell)ServiceProvider.GetService(typeof(SVsUIShell));

// try to find the C# interactive Window frame from it's package Id
// with different Guids, it could also work for other interactive Windows (F#, VB, etc.)
var CSharpVsInteractiveWindowPackageId = new Guid("{ca8cc5c7-0231-406a-95cd-aa5ed6ac0190}");

// you can use a flag here to force open it
var flags = __VSFINDTOOLWIN.FTW_fFindFirst;
shell.FindToolWindow((uint)flags, ref CSharpVsInteractiveWindowPackageId, out IVsWindowFrame frame);

// available?
if (frame != null)
{
// get its view (it's a WindowPane)
frame.GetProperty((int)__VSFPROPID.VSFPROPID_DocView, out object dv);

// this pane implements IVsInteractiveWindow (you need to add the Microsoft.VisualStudio.VsInteractiveWindow nuget package)
var iw = (IVsInteractiveWindow)dv;

// now get the wpf view host
// using an extension method from Microsoft.VisualStudio.VsInteractiveWindowExtensions class
IWpfTextViewHost host = iw.InteractiveWindow.GetTextViewHost();

// you can get lines with this
var lines = host.TextView.TextViewLines;

// and subscribe to events in text with this
host.TextView.TextBuffer.Changed += TextBuffer_Changed;
}

private void TextBuffer_Changed(object sender, TextContentChangedEventArgs e)
{
// text has changed
}

请注意“Microsoft.VisualStudio.VsInteractiveWindow”程序集没有专门记录,但源是开放的:http://sourceroslyn.io/#Microsoft.VisualStudio.VsInteractiveWindow

关于c# - 在 C# 交互式窗口中跟踪更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47807399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com