Visual Studio实现JS代码折叠功能

开发
Visual Studio的代码折叠功能非常好用,#region #endregion 这个词连搜狗的词库里面都出现了(不含'#'号),可见使用频率很高,但是他不支持js的代码折叠。最近Ext用得比较多,一写就是上百行JS代码,非常不方便,想着自己写个扩展或插件什么的,已经用宏来实现了,本文可以理解为该文的简单译本,注意宏代码部分我有所改动。

环境

Microsoft Visual Studio 2008

正文

1. 打开宏资源管理器:视图 -> 其他窗口 -> 宏资源管理器 

打开宏资源管理器

2.      创建一个新模块

创建新模块

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

  1. Option Strict Off  
  2. Option Explicit Off  
  3. Imports System  
  4. Imports EnvDTE  
  5. Imports EnvDTE80  
  6. Imports System.Diagnostics  
  7. Imports System.Collections  
  8. Public Module JsMacros  
  9.     Sub OutlineRegions()  
  10.         Dim selection As EnvDTE.TextSelection = DTE.ActiveDocument.Selection  
  11.         Const REGION_START As String = "//#region" 
  12.         Const REGION_END As String = "//#endregion" 
  13.         selection.SelectAll()  
  14.         '农民伯伯 --- 自动为"//#endregion"结束的代码添加***一行,不然出错  
  15.         If selection.Text.EndsWith(REGION_END) Then  
  16.             selection.EndOfLine()  
  17.             selection.NewLine()  
  18.             selection.SelectAll()  
  19.         End If  
  20.         Dim text As String = selection.Text  
  21.         selection.StartOfDocument(True)  
  22.         Dim startIndex As Integer  
  23.         Dim endIndex As Integer  
  24.         Dim lastIndex As Integer = 0 
  25.         Dim startRegions As Stack = New Stack()  
  26.         Do  
  27.             startIndex = text.IndexOf(REGION_START, lastIndex)  
  28.             endIndex = text.IndexOf(REGION_END, lastIndex)  
  29.             If startIndex = -1 AndAlso endIndex = -1 Then  
  30.                 Exit Do  
  31.             End If  
  32.             If startIndex <> -1 AndAlso startIndex < endIndex Then  
  33.                 startRegions.Push(startIndex)  
  34.                 lastIndex = startIndex + 1  
  35.             Else  
  36.                 ' Outline region   
  37.                 selection.MoveToLineAndOffset(CalcLineNumber(text, CInt(startRegions.Pop())), 1)  
  38.                 selection.MoveToLineAndOffset(CalcLineNumber(text, endIndex) + 1, 1, True)  
  39.                 selection.OutlineSection()  
  40.                 lastIndex = endIndex + 1  
  41.             End If  
  42.         Loop  
  43.         selection.StartOfDocument()  
  44.     End Sub  
  45.     Private Function CalcLineNumber(ByVal text As String, ByVal index As Integer)  
  46.         Dim lineNumber As Integer = 1 
  47.         Dim i As Integer = 0 
  48.         While i < index 
  49.             If text.Chars(i) = vbCr Then  
  50.                 lineNumber += 1  
  51.                 i += 1  
  52.             End If  
  53.             i += 1  
  54.         End While  
  55.         Return lineNumber  
  56.     End Function  
  57. End Module 

保存即可。这里可以省去新建宏的步骤,他会根据代码自动给你生成一个宏的。

注意我加的代码段,如果不加,并且你的JS***一行为#endregion,宏将报错,显示“值不在预期的范围内”。

4.设置快捷键

设置快捷键

 

4.1工具 -> 选项 - > 环境 -> 键盘

4.2在显示命令包含下面的文本框中输入宏名outli,不用输全,下面能显示你新建的宏

4.3点一下 按快捷键 下面的文本框, 然后自定义快捷键组合,我定义的是Ctrl+M,Ctrl+J,点分配(别忘了!),点确定。

5.效果

5.1输入代码:

  1. //aasdsadsad  
  2. //#region  
  3. //#endregion 

5.2快捷键Ctrl+M,Ctrl+J启动宏,能看到系统的右下角显示可爱的小方块在转动,js编辑框显示效果如下:

js编辑框显示效果

5.3之后就可以用快捷键Ctrl+M,Ctrl+L来[展开/折叠]代码了,注意关闭之后重新打开需要再启动一次宏,展开效果如下:

效果

 

结束

想到不如做到,但做之前要是能先Google一下也许能事半功倍。

【编辑推荐】

  1. Visual Studio 2010 Beta 1安装和调试
  2. Visual Studio 2010的微软云平台扩展发布
  3. Visual Studio 2010 Beta1试用手记
  4. Visual Studio 2010重要新功能一览
  5. 使用Visual Studio 2008调试器
责任编辑:彭凡 来源: CSDN博客
相关推荐

2011-02-28 10:27:41

Visual Stud

2012-04-25 11:04:13

Visual Stud

2010-12-14 09:15:50

Visual Stud

2012-09-19 10:14:12

Visual Stud

2013-11-13 10:07:26

Visual Stud微软

2010-04-12 08:43:45

Visual Stud

2009-12-18 09:49:28

Visual Stud

2013-06-04 17:08:19

Visual Stud

2009-10-22 09:47:33

Visual Stud

2009-07-01 17:11:32

标记导航Visual Stud

2009-12-04 16:57:52

Visual Stud

2009-04-23 14:05:28

Visual Stud历史调试功能

2013-08-01 15:12:03

Visual Stud

2009-12-14 17:44:39

Visual Stud

2023-09-26 00:24:44

VisualStudio视图

2009-11-05 09:42:42

Visual Stud

2009-11-19 10:55:33

Visual Stud

2009-08-21 13:29:20

Visual Stud

2010-07-05 09:46:42

Visual Stud

2012-09-17 13:49:31

点赞
收藏

51CTO技术栈公众号