2017-12-14 8 views
9

VSに対話型ウィンドウが追加されました。しかし、Visual Studioでは、CSI.EXE Roslynプロセスの実行とは異なり、IntelliSenseと現在のプロジェクトで読み込めるようないくつかの機能が追加されています。C#インタラクティブウィンドウでの変更の追跡

このウィンドウですべてのテキストエディタの変更を追跡する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? 
     } 
    } 
    } 
} 
+0

MEH、その文書化されていない* *別の場合、文字通り 'EnvDTE Interactive'を使用のためのドキュメントはありません。 com/en-us/dotnet/api/envdte.outputwindow.dte?view = visualstudiosdk-2017 –

+0

C#対話型ウィンドウのIWpfTextViewHostを探していますか?それはあなたのために大丈夫でしょうか? –

+0

@SimonMourierはわかりません。具体的なコントロールが必要なのは、1)私にバッファ全体を与え、2)変更を通知することができる。基本的に私はインタラクティブウィンドウのすべての変更を追跡する必要があります。 –

答えて

10

はC#インタラクティブウィンドウにIWpfTextViewHostの参照を取得しますいくつかのコードです。そこから、あなたは、Visual Studioからすべてのテキストサービスにアクセスすることができます。テキストライン、テキストバッファなどを

// 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 
} 

注意「、マイクロソフト(またはあなたが、私はお勧めしませんWPFのコントロール、上で直接フックすることができます) .VisualStudio.VsInteractiveWindow」アセンブリは、具体的に文書化されますが、ソースが開いていません。https://docs.microsoft:http://source.roslyn.io/#Microsoft.VisualStudio.VsInteractiveWindow

+0

これはうまくいくようです!クイック質問:基本的なVSコントロールにどのようにフックするつもりですか?理由は私はキャレットと選択情報が欲しいということです。 –

+0

すべては、IWpfTextViewHost以下から入手可能でなければなりません。最も興味深いものは、ITextView:Caret、Selectionなどです。https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.text.editor.itextview?view=visualstudiosdk-2017 –

+0

ソースはオープン、素敵な答え! –

関連する問題