Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move autocompleted closing tag in Visual Studio

I want to make Visual Studio move the autocompleted closing tag a word (or more) to the right. For example, given the following HTML:

<p>I need to emphasize some text.</p>

If I type <em> before the word "emphasize", Visual Studio autocompletes like so:

<p>I need to <em></em>emphasize some text.</p>

Then I need to move the closing </em> to get what I want:

<p>I need to <em>emphasize</em> some text.</p>

Is there a way to make Visual Studio do that last step automatically?

like image 308
Hugh Avatar asked Jan 02 '09 15:01

Hugh


People also ask

How do you move a closing tag in VS code?

It is possible to do, but either using Ctrl + Shift + P -> "Emmet: Go to Matching Pair" or by manually setting a shortcut for it ( Ctrl + K Ctrl + S ).

How do I stop VSCode from auto closing tags?

Set "typescript. autoClosingTags" to false to disable JSX tag closing.

What is enable auto closing tag in VS code?

Go to File > Preferences > Settings, search for html. autoClosingTags and set it to false . This will make it so when you type <div> , it won't insert </div> automatically, but if you start typing </ , it won't close the tag automatically. You can press ENTER to make it autocomplete for you.


3 Answers

Your question got me thinking how cool it would be if this functionality existed. Luckily, it was pretty simple to implement as a macro in VS. Below is the code for the macro. You can easily bind this to CTRL+ALT+Right using the customization tool in VS.

(Note: I just threw this together quickly being that it's Friday evening)

Sub MoveClosingTag()
    Dim ts As EnvDTE.TextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
    Dim start As EditPoint = ts.ActivePoint.CreateEditPoint()
    Dim tag As String

    ts.WordRight(True)
    If ts.Text = "</" Then
        Do Until ts.ActivePoint.AtEndOfLine
            ts.CharRight(True)
            If ts.Text.EndsWith(">") Then Exit Do
        Loop
        tag = ts.Text
        If tag.EndsWith(">") Then
            ts.Delete()
            ts.WordRight(False)
            ts.Insert(tag, EnvDTE.vsInsertFlags.vsInsertFlagsCollapseToStart)
        Else
            ts.MoveToPoint(start)
        End If
    Else
        ts.MoveToPoint(start)
    End If
End Sub
like image 78
w4g3n3r Avatar answered Oct 17 '22 03:10

w4g3n3r


I don't think this is possible. However, you can configure which HTML tags will be closed automatically:

Tools -> Options -> Text Editor -> HTML -> Format -> "Tag Specific Options" button -> Client HTML Tags -> em -> Closing Tag -> No Closing Tag

Please also consider that automatically moving the closing tagis not trivial (what should be the Word boundary?) and it would cover only a very special use case (only one Word should be e.g. highlighted).

like image 42
Dirk Vollmar Avatar answered Oct 17 '22 03:10

Dirk Vollmar


Props to @w4g3n3r for doing the hard work. I've modified the macro a bit to play nicer with spaces.

Note: I find that CTRL+. works nicely as a shortcut key for this; your right ring finger's already on the . key in the use case I originally described.

Sub MoveClosingTag()
    Dim ts As EnvDTE.TextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
    Dim start As EditPoint = ts.ActivePoint.CreateEditPoint()
    Dim tag As String

    ts.WordRight(True)
    If ts.Text = "</" Then
        Do Until ts.ActivePoint.AtEndOfLine
            ts.CharRight(True)
            If ts.Text.EndsWith(">") Then Exit Do
        Loop
        tag = ts.Text
        If tag.EndsWith(">") Then
            ts.Delete()
            Dim pos As Integer
            pos = ts.CurrentColumn
            ts.FindPattern(">", vsFindOptions.vsFindOptionsRegularExpression)
            If ts.CurrentColumn = pos Then
                ts.WordRight(False)
                ts.FindPattern(">", vsFindOptions.vsFindOptionsRegularExpression)
            End If
            ts.Insert(tag, EnvDTE.vsInsertFlags.vsInsertFlagsCollapseToStart)
        Else
            ts.MoveToPoint(start)
        End If
    Else
        ts.MoveToPoint(start)
    End If
End Sub
like image 28
Hugh Avatar answered Oct 17 '22 03:10

Hugh