如何实现区域/守则的崩溃。
-
20-09-2019 - |
题
你可以如何实施地区的一个。k.a.代码崩溃JavaScript在Visual Studio?
如果有数以百计的在线javascript,它将可以更容易理解的使用代码折叠与区域作为在vb/C#。
#region My Code
#endregion
解决方案
这里的博客条目对此进行了解释 和这个 MSDN问题.
您必须使用 Visual Studio 2003/2005/2008 宏。
为了保真起见,从博客条目中复制+粘贴:
- 打开宏资源管理器
- 创建一个新宏
- 命名它
OutlineRegions
- 单击编辑宏并粘贴以下 VB 代码:
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+E。我不知道为什么 - 我刚刚第一次输入并现在使用它:)
其他提示
微软现在有一个扩展的 VS 2010 强>提供此功能:
有关谁正在使用Visual Studio的最新版本的开发者的好消息
在的Web要点与此功能来了。
注意:对于VS 2017使用 JavaScript的区域:强> HTTPS: //marketplace.visualstudio.com/items?itemName=MadsKristensen.JavaScriptRegions
那容易!
标记要折叠和部分,
控制+ M + H
和对扩大使用 '+' 标记其左侧。
对于那些关于使用visual studio2012年存在的 网基础2012年
对于那些关于使用visual studio2015年存在的 网Essentials2015.3
使用是完全一样@普拉萨德要求
通过(无论任何逻辑块)标记的代码段和击球CTRL + M + H你定义选择作为是可折叠和可膨胀的区域。
在 JSEnhancements 插件的Visual Studio地址这个很好。
感谢 0A0D 的一个伟大的答案。我有好运气吧。 达林的季米特洛夫也使有关限制了你的JS文件的复杂性很好的理由。尽管如此,我还是觉得场合倒塌功能,它们的定义使得通过文件浏览变得更加容易。
关于在一般#地区,此 SO问题覆盖它得非常好。
我已经做了一些修改的宏以支持更高级的代码崩溃。这种方法,可以把描述的//#区域关键字ALA 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使用它。
在VS 2012和VS 2015年安装WebEssentials插件,你将能够做到这一点。
对于视觉工作室2017。
//#region Get Deactivation JS
.
.
//#endregion Get Deactivation JS
这是不早了工作,所以我下载的扩展名从这里
区域应在不改变设置的工作
//#region Optional Naming
var x = 5 -0; // Code runs inside #REGION
/* Unnecessary code must be commented out */
//#endregion
要启用折叠注释区域/ ** /
/* Collapse this
*/
设置 - >搜索 “折叠” - >编辑:折叠策略 - >从 “自动” 到 “压痕”
。标记:Node.js的节点的NodeJS JS ES5的JavaScript ECMAScript的评论折叠隐藏区 Visual Studio代码vscode 2018版1.2+ https://code.visualstudio.com/updates/v1_17#_folding-regions
不仅为VS但几乎所有的编辑器。
(function /* RegionName */ () { ... })();
警告:强>具有缺点,诸如范围