Visual Studio also supports JS Code folding [Visual Studio | # region | # endregion]

Source: Internet
Author: User

Preface

The Code folding function of Visual Studio is very easy to use. The word # region # endregion appears in the sogou Dictionary (not including the '#' number), which is frequently used, however, he does not support code folding of js: (recently, Ext has been used more often. Writing hundreds of lines of JS code is very inconvenient. He wants to write an extension or plug-in on his own, I accidentally found the following article, which has been implemented using macros. This article can be understood as a simple translation of this Article. Note that I have changed the macro code :)
 

Article

1. Using # region Directive With JavaScript Files in Visual Studio

 

Environment

Microsoft Visual maxcompute 2008

 

Body

1. Open macro Resource Manager: View-> other Windows-> macro Resource Manager

2. Create a new module

  

3. Edit macro: select a module and right-click to edit

Option Strict Off
Option Explicit Off

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System. Diagnostics
Imports System. Collections

Public Module JsMacros

Sub OutlineRegions ()
Dim selection As EnvDTE. TextSelection = DTE. ActiveDocument. Selection

Const REGION_START As String = "// # region"
Const REGION_END As String = "// # endregion"

Selection. SelectAll ()
'Farmer's uncle --- automatically add the last line to the Code ending with "// # endregion". Otherwise, an error occurs.
If selection. Text. EndsWith (REGION_END) Then
Selection. EndOfLine ()
Selection. NewLine ()
Selection. SelectAll ()
End If

Dim text As String = selection. Text
Selection. StartOfDocument (True)

Dim startIndex As Integer
Dim endIndex As Integer
Dim lastIndex As Integer = 0
Dim startRegions As Stack = New Stack ()

Do
StartIndex = text. IndexOf (REGION_START, lastIndex)
EndIndex = text. IndexOf (REGION_END, lastIndex)

If startIndex =-1 AndAlso endIndex =-1 Then
Exit Do
End If

If startIndex <>-1 AndAlso startIndex <endIndex Then
StartRegions. Push (startIndex)
LastIndex = startIndex + 1
Else
'Outline region
Selection. MoveToLineAndOffset (CalcLineNumber (text, CInt (startRegions. Pop (), 1)
Selection. MoveToLineAndOffset (CalcLineNumber (text, endIndex) + 1, 1, True)
Selection. OutlineSection ()

LastIndex = endIndex + 1
End If
Loop

Selection. StartOfDocument ()
End Sub

Private Function CalcLineNumber (ByVal text As String, ByVal index As Integer)
Dim lineNumber As Integer = 1
Dim I As Integer = 0

While I <index
If text. Chars (I) = vbCr Then
LineNumber + = 1
I + = 1
End If

I + = 1
End While

Return lineNumber
End Function

End Module

Save it. You can skip the process of creating a new macro here. It will automatically generate a macro for you based on the code.

Note that if the code segment I added is not added and the last line of your JS is # endregion, the macro will report an error, showing "the value is not within the expected range ".

 

4. Set shortcuts

4.1 tools-> options-> environment-> keyboard

4.2 enter the macro name outli in the text box that contains the display command. You do not need to enter the full value. The new macro is displayed below.

4.3 click the text box below the shortcut key, and then customize the shortcut key combination. I define Ctrl + M, Ctrl + J, and clickAllocate(Don't forget !), Click OK.

 

5. Results

5.1 enter the code:


// Aasdsadsad

// # Region
// # Endregion

5.2 press Ctrl + M, Ctrl + J to start the macro. You can see that the cute small square is rotating in the lower right corner of the system. The js editing box shows the following effect:

    

After 5.3, you can use the shortcut keys Ctrl + M, Ctrl + L to [Expand/collapse] the code. Note that you need to start the macro again after closing the code and re-open it. The expansion effect is as follows:

 

End

I think it is better to do it, but before doing so, Google may get twice the result with half the effort :)

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.