挠溃症 发表于 2025-5-29 20:21:24

Visual Studio 让JS也支持代码折叠 [ Visual Studio | #region

 前言
      Visual Studio的代码折叠功能非常好用,#region #endregion 这个词连搜狗的词库里面都出现了(不含'#'号),可见使用频率很高,但是他不支持js的代码折叠 : ( 最近Ext用得比较多,一写就是上百行JS代码,非常不方便,想着自己写个扩展或插件什么的,意外搜到了下面的文章,已经用宏来实现了,本文可以理解为该文的简单译本,注意宏代码部分我有所改动 : )
 
文章
      1.      Using #region Directive With JavaScript Files in Visual Studio
 
环境
      Microsoft Visual Studio 2008
 
正文
      1.      打开宏资源管理器:视图 -> 其他窗口 -> 宏资源管理器
   
      2.      创建一个新模块

  
  3.  编辑宏:  选中模块 -> 右键编辑

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()
        '农民伯伯 --- 自动为"//#endregion"结束的代码添加最后一行,不然出错
        If selection.Text.EndsWith(REGION_END) Then
            selection.EndOfLine()
            selection.NewLine()
            selection.SelectAll()
        End If


        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 
页: [1]
查看完整版本: Visual Studio 让JS也支持代码折叠 [ Visual Studio | #region