如何在javascript中实现区域/代码崩溃 [英] how to implement regions/code collapse in javascript

查看:177
本文介绍了如何在javascript中实现区域/代码崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Visual Studio中实现JavaScript代码崩溃的区域?

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

如果javascript中有数百行,使用代码折叠会更容易理解与vb / C#中的区域。

If there are hundreds of lines in javascript, it'll be more understandable using code folding with regions as in vb/C#.

#region My Code

#endregion


推荐答案

博客条目在这里解释了它和这个 MSDN问题

你必须使用Visual Studio 2003/2005 / 2008宏。

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

复制+粘贴来自博客条目,保真度:

Copy + Paste from Blog entry for fidelity sake:


  1. 打开Macro Explorer

  2. 创建新宏

  3. 将其命名为 OutlineRegions

  4. 点击编辑宏并粘贴以下VB代码:

  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. 保存宏并关闭编辑器

  2. 现在让我们为宏分配快捷方式。转到工具 - >选项 - >环境 - >键盘,然后在显示命令包含文本框

  3. 中搜索宏,现在在按快捷键下的文本框中输入所需的捷径。我使用Ctrl + M + E。我不知道为什么 - 我刚刚进入它并立即使用它:)

这篇关于如何在javascript中实现区域/代码崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆