how to implement regions/code collapse in javascript

后端 未结 17 1592
灰色年华
灰色年华 2020-11-28 21:01

How can you implement regions a.k.a. code collapse for JavaScript in Visual Studio?

If there are hundreds of lines in javascript, it\'ll be more understandable using

相关标签:
17条回答
  • 2020-11-28 21:57

    Microsoft now has an extension for VS 2010 that provides this functionality:

    JScript Editor Extensions

    0 讨论(0)
  • 2020-11-28 22:00

    Thats easy!

    Mark the section you want to collapse and,

    Ctrl+M+H

    And to expand use '+' mark on its left.

    0 讨论(0)
  • 2020-11-28 22:00

    The JSEnhancements plugin for Visual Studio addresses this nicely.

    0 讨论(0)
  • 2020-11-28 22:01

    Blog entry here explains it and this MSDN question.

    You have to use Visual Studio 2003/2005/2008 Macros.

    Copy + Paste from Blog entry for fidelity sake:

    1. Open Macro Explorer
    2. Create a New Macro
    3. Name it OutlineRegions
    4. Click Edit macro and paste the following VB code:
    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()
            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
    
    1. Save the Macro and Close the Editor
    2. Now let's assign shortcut to the macro. Go to Tools->Options->Environment->Keyboard and search for your macro in "show commands containing" textbox
    3. now in textbox under the "Press shortcut keys" you can enter the desired shortcut. I use Ctrl+M+E. I don't know why - I just entered it first time and use it now :)
    0 讨论(0)
  • 2020-11-28 22:01

    For those about to use the visual studio 2012, exists the Web Essentials 2012

    For those about to use the visual studio 2015, exists the Web Essentials 2015.3

    The usage is exactly like @prasad asked

    0 讨论(0)
提交回复
热议问题