我的vs2008插件用于textformatting非常慢

我写了一个小插件,它对我的​​C#代码进行了一些格式化。 在addins Exec方法中,我执行以下操作

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

当代码行为“SelectedText.Text = foobar”时 是调用,VS逐步重建选择的每一行。 您可以轻松地观察它执行此步骤。 但我不明白,为什么这么慢。

任何提示? TIA

JFTR:我不得不使用TextSelection.Insert(…),但是为了获得视觉工作室深度的缩进,我还必须弄乱选定的文本以跨越整个第一行和最后一行的选择:

 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(); 

我真的不知道这是改变文本选择的好方法,但它工作正常并且比原始插件代码更快

我没有使用插件,但由于你只是要求’提示’,这是我的。

在进行分配之前,请尝试禁用屏幕更新。

帮助文件也说,

“当设置Text属性时,Text的值将插入所选文本的前面,然后折叠,类似于将文本粘贴到文档中时所发生的情况。请注意,此属性的行为与编辑时的类型相同in insert(即非泛音)模式。截断第128个字符后的任何文本。“

这似乎意味着变量没有像预期的那样被覆盖,而是被附加,然后删除前一个文本。 首先尝试清空变量,然后查看它是否发生了变化。

另外,请考虑使用PasteMethod替换文本而不是分配。