How to disable autoformat / fix while running a macro? Visual Studio

When you run a macro that changes the selected text, the tags are automatically closed and the text is formatted. How can I prevent this?

For example, wrapping text in a tag:

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

Ends with two closing tags:

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

Even a stranger, a few lines fail:

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

An ends as

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

How can I prevent this? As you can see from the last example, formatting is incorrect and there is additional</li>

+3
source share
2 answers

You will need to insert the text, and not assign 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
+4
source

Source: https://habr.com/ru/post/1713670/


All Articles