locked
SDK Package : How to detect changes in a TextDocument? RRS feed

  • Question

  • Hello!

    I'm building by first extension for Visual Studio which is a tool allows me to create "regions" in css files. For this to work I need to be able to detect when the text changes. So far I have used EnvDTE80.Events2.TextDocumentKeyPressEvents to detect when a key is pressed. That works fine but ofcourse undo/redo won't be caught.

    I could certainly listen to the commands too but if someone adds a custom command that alters the text it will break my extension. I have a feeling that there is a better way to do this. I found this thread http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/0be5b641-153d-4421-beb3-941624a7f882  but the link to the solution is unfortuanly dead.

    To be clear :
    - I'm using the default editor
    - This only needs to work for CSS files
    - I need to know when the text changes
    - It's fine if only works in VS2010

    If anyone has any good resources or advices I would appreciate it.

    /Mikael Eliasson
    Friday, February 12, 2010 8:24 AM

Answers

  • Hello,

    If you need this only in VS 2010, you can use later approach.
    Please note, the TextViewCreated method is called if view is created(not the buffer). This means f.e., if you split your window, a second call to this method occurs, which (if you are not aware of this) results in registering the same event twice-using the simple code below.

     

    [Export(typeof(IWpfTextViewCreationListener))] 
    [ContentType("css")] 
    [TextViewRole(PredefinedTextViewRoles.Interactive)] 
    public class WpfTVCreationListener:IWpfTextViewCreationListener { 
    public void TextViewCreated(IWpfTextView textView) { 
    ITextBuffer buff=textView.TextBuffer; 
    buff.Changed+=new EventHandler<TextContentChangedEventArgs>(buff_Changed); 
    }
      
    void buff_Changed(object sender, TextContentChangedEventArgs e) { 
     //any code you need here 
    }
    }

     

     

     

    Friday, February 12, 2010 7:17 PM

All replies

  • Hello,

    If you need this only in VS 2010, you can use later approach.
    Please note, the TextViewCreated method is called if view is created(not the buffer). This means f.e., if you split your window, a second call to this method occurs, which (if you are not aware of this) results in registering the same event twice-using the simple code below.

     

    [Export(typeof(IWpfTextViewCreationListener))] 
    [ContentType("css")] 
    [TextViewRole(PredefinedTextViewRoles.Interactive)] 
    public class WpfTVCreationListener:IWpfTextViewCreationListener { 
    public void TextViewCreated(IWpfTextView textView) { 
    ITextBuffer buff=textView.TextBuffer; 
    buff.Changed+=new EventHandler<TextContentChangedEventArgs>(buff_Changed); 
    }
      
    void buff_Changed(object sender, TextContentChangedEventArgs e) { 
     //any code you need here 
    }
    }

     

     

     

    Friday, February 12, 2010 7:17 PM
  • Thank you for the answer!

    The problem is that I don't get exports to work since I'm in a VSIP package(Need to be able to add a custom tool window). I however managed to get a hold of the TextBuffer by using the IVsTextManager to get the IVsTextView and then convert it to a IWpfTextView by using the IVsEditorAdaptersFactoryService which you can get like this:

    IComponentModel componentModelService = Microsoft.VisualStudio.Shell.ServiceProvider.GlobalProvider.GetService(typeof(SComponentModel)) as IComponentModel;
    IEnumerable<IVsEditorAdaptersFactoryService> adapterFactories = componentModelService.DefaultExportProvider.GetExportedValues<IVsEditorAdaptersFactoryService>();

    Thanks for pointing me in the right direction.




    • Proposed as answer by fedi24 Tuesday, February 16, 2010 5:06 PM
    Saturday, February 13, 2010 10:19 AM
  • I'm glad I could help a bit.
    You helped me in turn - some time ago I was looking for default component model, but somehow could not find the right way. And here it is, the first line :-). So easy, so obvious.

    Thanks.
    Tuesday, February 16, 2010 5:06 PM