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
<! --
Code highlighting produced by actipro codehighlighter (freeware)
Http://www.CodeHighlighter.com/
--> 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:
<! --
Code highlighting produced by actipro codehighlighter (freeware)
Http://www.CodeHighlighter.com/
-->
// 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