如何在Visual Studio中为JavaScript实现区域(即代码折叠)?
如果JavaScript有数百行代码,使用类似于VB / C#中的代码折叠区域将更易于理解。
#region My Code
#endregion
如何在Visual Studio中为JavaScript实现区域(即代码折叠)?
如果JavaScript有数百行代码,使用类似于VB / C#中的代码折叠区域将更易于理解。
#region My Code
#endregion
对于正在使用最新版本Visual Studio的开发人员来说,有一个好消息。
Web Essentials将带来这个功能。
注意:对于 VS 2017,请使用JavaScript Regions:https://marketplace.visualstudio.com/items?itemName=MadsKristensen.JavaScriptRegions。微软现在推出了一个适用于VS 2010的扩展,该扩展提供了以下功能:
这很简单!
标记您想要折叠的部分,然后
Ctrl+M+H
要展开,请在其左侧使用“+”标记。
对于即将使用Visual Studio 2012的用户,可以使用Web Essentials 2012
对于即将使用Visual Studio 2015的用户,可以使用Web Essentials 2015.3
使用方法与@prasad提问的完全一样
您必须使用Visual Studio 2003/2005/2008宏。
为了保持一致性,请从博客文章中复制+粘贴以下内容:
OutlineRegions
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
通过标记代码片段 (不考虑任何逻辑块) 并按下 CTRL + M + H,您将定义所选内容为可折叠和展开的区域。
对于那些来到这里寻找Visual Studio Code的人,相同的语法也适用。
// #region MongoDB Client
const MongoClient = require('mongodb').MongoClient;
const url = constants.credentials["uat"].mongo.url
MongoClient.connect(url, { useUnifiedTopology: true }, function (err, client) {
if (err) {
console.log(err);
}
else {
docDB = client.db("middlewareDB");
}
});
// #endregion
折叠时,它看起来像下面这样
感谢0A0D提供的出色答案。我已经用它获得了好运气。Darin Dimitrov也提出了限制JS文件复杂性的好观点。然而,我确实发现折叠函数到其定义中使浏览文件变得更加容易的情况。
关于#region总体而言,这个SO问题解释得很好。
我对宏进行了一些修改,以支持更高级的代码折叠。这种方法允许您在//#region关键字之后放置描述,类似于C#,并将其显示在代码中,如下所示:
示例代码:
//#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 InputHandler
更新的宏:
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 Module
这个功能现在已经内置于VS2017中:
//#region fold this up
//#endregion
// 和 # 之间的空格不影响代码。
我不知道这个功能是在哪个版本添加的,因为在版本更改日志中没有找到任何提及。但我能在 v15.7.3 版本中使用它。