Visual StudioでJavaScriptのコード折りたたみとも呼ばれる領域をどのように実装できますか?
javascriptに数百行ある場合は、vb / C#のように領域を使用してコードを折りたたむと理解しやすくなります。
#region My Code
#endregion
Visual StudioでJavaScriptのコード折りたたみとも呼ばれる領域をどのように実装できますか?
javascriptに数百行ある場合は、vb / C#のように領域を使用してコードを折りたたむと理解しやすくなります。
#region My Code
#endregion
VisualStudioの最新バージョンを使用している開発者にとって朗報です
WebEssentialsにはこの機能が付属しています。
注:VS 2017の場合は、JavaScriptリージョンを使用してください: https ://marketplace.visualstudio.com/items?itemName = MadsKristensen.JavaScriptRegions
Microsoftには、この機能を提供するVS2010の拡張機能があります。
簡単だ!
折りたたむ部分に印を付けて、
Ctrl + M + H
展開するには、左側の「+」マークを使用します。
Visual Studio 2012を使用しようとしている人のために、WebEssentials2012が存在します
Visual Studio 2015を使用しようとしている人のために、WebEssentials2015.3があります。
使用法は@prasadが尋ねたのとまったく同じです
ここのブログエントリはそれとこのMSDNの質問を説明しています。
VisualStudio2003/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用のJSEnhancementsプラグインは、これにうまく対処します。
素晴らしい答えをくれた0A0Dに感謝します。私はそれで幸運に恵まれました。 Darin Dimitrovは、JSファイルの複雑さを制限することについても良い議論をしています。それでも、関数を定義に折りたたむと、ファイルの参照がはるかに簡単になる場合があります。
一般的な#regionに関して、このSO質問はそれを非常によくカバーしています。
より高度なコードの折りたたみをサポートするために、マクロにいくつかの変更を加えました。このメソッドを使用すると、//#regionキーワード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
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
折りたたむと下のようになります
これは現在VS2017でネイティブになっています:
//#region fold this up
//#endregion
//と#の間の空白は重要ではありません。
変更ログにそれについての言及が見つからないため、これがどのバージョンで追加されたかはわかりません。v15.7.3で使用できます。
VS2012およびVS2015では、WebEssentialsプラグインをインストールすると、それを実行できるようになります。
//#region Get Deactivation JS
.
.
//#endregion Get Deactivation JS
これは以前は機能していなかったので、ここから拡張機能をダウンロードしました
拡張子名(JavaScriptリージョン)Mads Kristensen
Resharperを使用している場合
この写真のステップを休耕する
//#region $name$
$END$$SELECTION$
//#endregion $name$
これがお役に立てば幸いです
リージョンは設定を変更せずに機能するはずです
//#region Optional Naming
var x = 5 -0; // Code runs inside #REGION
/* Unnecessary code must be commented out */
//#endregion
コメント領域の折りたたみを有効にするには/**/
/* Collapse this
*/
設定->「折りたたみ」を検索->エディタ:折りたたみ戦略->「自動」から「インデント」へ。
タグ:Node.js Nodejs Node js JavascriptES5ECMAScriptコメント折りたたみ非表示領域VisualStudiocodevscode2018バージョン1.2+ https: //code.visualstudio.com/updates/v1_17#_folding-regions
VSだけでなく、ほぼすべてのエディターが対象です。
(function /* RegionName */ () { ... })();
警告:スコープなどの欠点があります。