Visual Studio - 如何在运行宏时禁用autoformat / correct?

时间:2023-01-14 18:52:59

When running a macro that changes the selected text, tags are automatically closed and the text formatted. How can I prevent that from happening?

运行更改所选文本的宏时,标签会自动关闭并且文本格式化。我怎样才能防止这种情况发生?

For example, wrapping text in a tag:

例如,在标记中包装文本:

DTE.ActiveDocument.Selection.Text = String.Format("<tag>{0}</tag>", DTE.ActiveDocument.Selection.Text)

Ends up with two closing tags:

结束时有两个结束标记:

<tag>Text</tag></tag>

Even stranger, multiple lines fails:

更奇怪的是,多行失败:

<li>One</li>
<li>Two</li>
<li>Three</li>

An ends up as

最终成为

<ul>            <li>One</li>
            <li>Two</li>
                        <li>Three</li></li></ul>

How can I prevent that? As can be seen by the last example, the formatting is wrong and there is an extra </li>

我怎么能防止这种情况?从最后一个例子可以看出,格式化是错误的,还有一个额外的

2 个解决方案

#1


You'll need to insert the text rather than assigning it:

您需要插入文本而不是分配它:

Try
    DTE.UndoContext.Open("InsertSomeCode")
    Dim ts As TextSelection = CType(DTE.ActiveDocument.Selection, TextSelection)
    ts.Insert(String.Format("<tag>{0}</tag>", ts.Text))
Finally
    DTE.UndoContext.Close()
End Try

#2


I think the only way to do this is to open a undo transaction for your edit. This should suspend all formatting operations until the transaction is complete. This will allow you to do several edit operations before a format.

我认为执行此操作的唯一方法是为您的编辑打开撤消事务。这应该暂停所有格式化操作,直到事务完成。这将允许您在格式之前执行多个编辑操作。

Here is the documentation OpenLinkedUndo method

这是OpenLinkedUndo方法的文档

#1


You'll need to insert the text rather than assigning it:

您需要插入文本而不是分配它:

Try
    DTE.UndoContext.Open("InsertSomeCode")
    Dim ts As TextSelection = CType(DTE.ActiveDocument.Selection, TextSelection)
    ts.Insert(String.Format("<tag>{0}</tag>", ts.Text))
Finally
    DTE.UndoContext.Close()
End Try

#2


I think the only way to do this is to open a undo transaction for your edit. This should suspend all formatting operations until the transaction is complete. This will allow you to do several edit operations before a format.

我认为执行此操作的唯一方法是为您的编辑打开撤消事务。这应该暂停所有格式化操作,直到事务完成。这将允许您在格式之前执行多个编辑操作。

Here is the documentation OpenLinkedUndo method

这是OpenLinkedUndo方法的文档