Làm cách nào bạn có thể triển khai các vùng aka mã thu gọn cho JavaScript trong Visual Studio?
Nếu có hàng trăm dòng trong javascript, sẽ dễ hiểu hơn khi sử dụng mã gấp với các vùng như trong vb / C #.
#region My Code
#endregion
Làm cách nào bạn có thể triển khai các vùng aka mã thu gọn cho JavaScript trong Visual Studio?
Nếu có hàng trăm dòng trong javascript, sẽ dễ hiểu hơn khi sử dụng mã gấp với các vùng như trong vb / C #.
#region My Code
#endregion
Câu trả lời:
Mục blog ở đây giải thích nó và câu hỏi MSDN này .
Bạn phải sử dụng Macro Studio 2003/2005/2008.
Sao chép + Dán từ mục nhập Blog vì lợi ích trung thực:
OutlineRegionsOption 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
Microsoft hiện có một phần mở rộng cho VS 2010 cung cấp chức năng này:
Tin vui cho các nhà phát triển đang làm việc với phiên bản mới nhất của visual studio
Các Web Essentials đang đến với tính năng này.
Lưu ý: Đối với VS 2017, hãy sử dụng Vùng JavaScript: https://marketplace.visualstudio.com/items?itemName=MadsKristensen.JavaScriptRegions
Đó là dễ dàng!
Đánh dấu phần bạn muốn thu gọn và,
Ctrl + M + H
Và để mở rộng sử dụng dấu '+' bên trái.
Đối với những người sắp sử dụng visual studio 2012, hãy tồn tại Web Essentials 2012
Đối với những người sắp sử dụng visual studio 2015, hãy tồn tại Web Essentials 2015.3
Cách sử dụng giống hệt như @prasad đã hỏi
Bằng cách đánh dấu một phần mã (bất kể khối logic nào) và nhấn CTRL + M + H, bạn sẽ xác định lựa chọn là một khu vực có thể thu gọn và có thể mở rộng.
Các JSEnhancements plugin cho địa chỉ Visual Studio này độc đáo.
Cảm ơn 0A0D cho một câu trả lời tuyệt vời. Tôi đã có may mắn với nó. Darin Dimitrov cũng đưa ra một lập luận tốt về việc hạn chế sự phức tạp của các tệp JS của bạn. Tuy nhiên, tôi vẫn thấy các trường hợp thu gọn các hàm theo định nghĩa của chúng làm cho việc duyệt qua tệp dễ dàng hơn nhiều.
Về #region nói chung, Câu hỏi SO này bao gồm nó khá tốt.
Tôi đã thực hiện một vài sửa đổi cho Macro để hỗ trợ thu gọn mã nâng cao hơn. Phương pháp này cho phép bạn đặt một mô tả sau từ khóa // # ala C # và hiển thị nó trong mã như được hiển thị:
Mã ví dụ:
//#region InputHandler
var InputHandler = {
    inputMode: 'simple', //simple or advanced
    //#region filterKeys
    filterKeys: function(e) {
        var doSomething = true;
        if (doSomething) {
            alert('something');
        }
    },
    //#endregion filterKeys
    //#region handleInput
    handleInput: function(input, specialKeys) {
        //blah blah blah
    }
    //#endregion handleInput
};
//#endregion InputHandlerCập nhật Macro:
Option Explicit On
Option Strict On
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Imports System.Collections.Generic
Public Module JsMacros
    Sub OutlineRegions()
        Dim selection As EnvDTE.TextSelection = CType(DTE.ActiveDocument.Selection, EnvDTE.TextSelection)
        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 New Stack(Of Integer)
        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 ...
                Dim tempStartIndex As Integer = CInt(startRegions.Pop())
                selection.MoveToLineAndOffset(CalcLineNumber(text, tempStartIndex), CalcLineOffset(text, tempStartIndex))
                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) As Integer
        Dim lineNumber As Integer = 1
        Dim i As Integer = 0
        While i < index
            If text.Chars(i) = vbLf Then
                lineNumber += 1
                i += 1
            End If
            If text.Chars(i) = vbCr Then
                lineNumber += 1
                i += 1
                If text.Chars(i) = vbLf Then
                    i += 1 'Swallow the next vbLf
                End If
            End If
            i += 1
        End While
        Return lineNumber
    End Function
    Private Function CalcLineOffset(ByVal text As String, ByVal index As Integer) As Integer
        Dim offset As Integer = 1
        Dim i As Integer = index - 1
        'Count backwards from //#region to the previous line counting the white spaces
        Dim whiteSpaces = 1
        While i >= 0
            Dim chr As Char = text.Chars(i)
            If chr = vbCr Or chr = vbLf Then
                whiteSpaces = offset
                Exit While
            End If
            i -= 1
            offset += 1
        End While
        'Count forwards from //#region to the end of the region line
        i = index
        offset = 0
        Do
            Dim chr As Char = text.Chars(i)
            If chr = vbCr Or chr = vbLf Then
                Return whiteSpaces + offset
            End If
            offset += 1
            i += 1
        Loop
        Return whiteSpaces
    End Function
End ModuleTrên VS 2012 và VS 2015 cài đặt plugin WebEssentials và bạn sẽ có thể làm như vậy.
nếu bạn đang sử dụng Resharper
bỏ các bước trong bức ảnh này
 sau đó viết cái này trong trình soạn thảo mẫu
sau đó viết cái này trong trình soạn thảo mẫu
  //#region $name$
$END$$SELECTION$
  //#endregion $name$và đặt tên #regionnhư trong bức tranh này

hy vọng điều này sẽ giúp bạn
Không có câu trả lời nào trong số này không phù hợp với tôi với visual studio 2017.
Plugin tốt nhất cho VS 2017: Khu vực JavaScript
Ví dụ 1:
Ví dụ 2:
Đã kiểm tra và phê duyệt:
Vùng sẽ hoạt động mà không thay đổi cài đặt
//#region Optional Naming
    var x = 5 -0; // Code runs inside #REGION
    /* Unnecessary code must be commented out */
//#endregionĐể cho phép thu gọn khu vực bình luận / ** /
/* Collapse this
*/Cài đặt -> Tìm kiếm "gấp" -> Trình chỉnh sửa: Chiến lược gấp -> Từ "tự động" đến "thụt lề".
TAGS: Node.js Nodejs Node js Javascript ES5 ECMAScript comment gấp khu vực ẩn Visual studio đang vscode phiên bản 2018 1.2+ https://code.visualstudio.com/updates/v1_17#_folding-regions
Không chỉ cho VS mà gần như cho tất cả các biên tập viên.
(function /* RegionName */ () { ... })();
Cảnh báo: có nhược điểm như phạm vi.