Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

my vs2008 addin for textformatting is awfully slow

i wrote a little addin, which does some formatting of my C# code. in the addins Exec method i do the following

try {
    TextSelection selection = (EnvDTE.TextSelection)_applicationObject.ActiveDocument.Selection;
    String foo = String.Empty;                      
    if (!text.IsEmpty) {                            
    foo = someCoolObjectThatFormatsText.Format(selection.Text);
    selection.Text = foo;  // here everything gets painfully slow :-(
    }
}
catch (Exception) {
    throw;
}

when the line with the code "SelectedText.Text = foobar;" is call, VS rebuilds each line of the selection step by step. You can easily watch it doing this step. But i don't get, why it is that slow.

Any hints? TIA


1 Answers

JFTR: I had to use TextSelection.Insert(...), but to also get visual studios depth of indention, i also had to mess with the selected text to span the selection also over the full first and last line:

TextSelection text = (EnvDTE.TextSelection)_applicationObject.ActiveDocument.Selection;
text.SmartFormat(); //  sets the correct indention als studio
/* the following lines will expand the selection to whole lines: */
int lineSpan = text.BottomPoint.Line - text.TopPoint.Line;
text.MoveToPoint(text.TopPoint,false);                      
text.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn,false);                       
text.LineDown(true,lineSpan);                       
text.EndOfLine(true);
/* and now my custom textformatting */
text.Insert(someCoolObjectThatFormatsText.Format(text.Text),(int)vsInsertFlags.vsInsertFlagsContainNewText);                                                                                    
text.Collapse();

I don't really know wether this is a good way to alter textselections but it works fine and is way faster than the original addin code


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!