<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Bertan's Blog : VSIP</title><link>http://blogs.msdn.com/bertan/archive/tags/VSIP/default.aspx</link><description>Tags: VSIP</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>VSIP: Adding a command filter to any text editor</title><link>http://blogs.msdn.com/bertan/archive/2007/10/26/vsip-adding-a-command-filter-to-any-text-editor.aspx</link><pubDate>Sat, 27 Oct 2007 00:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5696022</guid><dc:creator>bertaygu</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/bertan/comments/5696022.aspx</comments><wfw:commentRss>http://blogs.msdn.com/bertan/commentrss.aspx?PostID=5696022</wfw:commentRss><description>&lt;P&gt;I have been trying to add a command filter to all text editors in Visual Studio for a project, since I had to collect information from several number of sources I wanted to collect them in one place in the hope that somebody else might find it useful.&lt;/P&gt;
&lt;P&gt;In order to get notification when a text view is registered in Visual Studio, we first have to register an implementation of &lt;EM&gt;IVsTextManagerEvents&lt;/EM&gt; as an event sink. In order to do this you can use the following code block which uses IVsTextManager service.&lt;/P&gt;&lt;FONT color=#2b91af size=2&gt;
&lt;P&gt;IVsTextManager&lt;/FONT&gt;&lt;FONT size=2&gt; textManager = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;this&lt;/FONT&gt;&lt;FONT size=2&gt;.GetService(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;typeof&lt;/FONT&gt;&lt;FONT size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;SVsTextManager&lt;/FONT&gt;&lt;FONT size=2&gt;)) &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;as&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;IVsTextManager&lt;/FONT&gt;&lt;FONT size=2&gt;;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;IConnectionPointContainer&lt;/FONT&gt;&lt;FONT size=2&gt; container = textManager &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;as&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;IConnectionPointContainer&lt;/FONT&gt;&lt;FONT size=2&gt;;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;IConnectionPoint&lt;/FONT&gt;&lt;FONT size=2&gt; textManagerEventsConnection = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;null&lt;/FONT&gt;&lt;FONT size=2&gt;;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;Guid&lt;/FONT&gt;&lt;FONT size=2&gt; eventGuid = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;typeof&lt;/FONT&gt;&lt;FONT size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;IVsTextManagerEvents&lt;/FONT&gt;&lt;FONT size=2&gt;).GUID;&lt;BR&gt;container.FindConnectionPoint(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;ref&lt;/FONT&gt;&lt;FONT size=2&gt; eventGuid, &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;out&lt;/FONT&gt;&lt;FONT size=2&gt; textManagerEventsConnection);&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;uint&lt;/FONT&gt;&lt;FONT size=2&gt; cookie = 0; &lt;BR&gt;textManagerEventsConnection.Advise(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;this&lt;/FONT&gt;&lt;FONT size=2&gt;, &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;out&lt;/FONT&gt;&lt;FONT size=2&gt; cookie);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;Now &lt;EM&gt;IVsTextManagerEvents.OnRegisterView &lt;/EM&gt;method is going to be called for each new view created. In that method you can use passed &lt;EM&gt;IVsTextView.AddCommandFilter&amp;nbsp;&lt;/EM&gt;method to add a command filter. However, when the event is raised the window is actually not initialized fully so any command filter you add will be below the language service filters thus you won't see all of the commands. My solution to this problem was to check the text buffer each time &lt;EM&gt;Exec&lt;/EM&gt; method was called on the filter and remove/add the filter once the buffer wasn't empty anymore. I am still trying to find a better solution and I will update this entry if I find anything better than relying on the buffer. One apparent problem with this solution is that as long as the text view remains empty, the filter will not be re-registered thus causing you to lose some events on empty files.&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5696022" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/bertan/archive/tags/Tips/default.aspx">Tips</category><category domain="http://blogs.msdn.com/bertan/archive/tags/VisualStudio/default.aspx">VisualStudio</category><category domain="http://blogs.msdn.com/bertan/archive/tags/VSIP/default.aspx">VSIP</category></item></channel></rss>