3

我写了一个小插件,它对我的​​ C# 代码进行了一些格式化。在插件 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

4

2 回答 2

2

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

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

于 2009-07-08T08:09:10.150 回答
0

我没有使用过插件,但由于您只要求“提示”,所以这是我的。

在分配之前尝试禁用屏幕更新。

帮助文件还说,

“设置 Text 属性时,Text 的值会插入到所选文本的前面,然后折叠起来,类似于将文本粘贴到文档中时发生的情况。请注意,此属性的行为就像在编辑器打开时键入时一样在插入(即非改写)模式下。第 128 个字符之后的任何文本都将被截断。”

这似乎意味着该变量没有按预期覆盖,而是附加,然后删除先前的文本。尝试先清空变量,看看它是否改变了任何东西。

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

于 2009-07-03T19:52:48.400 回答