天天看點

讓Visual Studio 也支援JS代碼折疊 —— 續 [ Visual Studio | Js | ScriptOutline | SmallOutline ]

    

    1.2  宏代碼,修改上文使用的宏即可。

Option Strict Off

Option Explicit Off

Imports System

Imports EnvDTE

Imports EnvDTE80

Imports System.Diagnostics

Imports System.Collections

Public Module JsMacros

    Sub OutlineRegions()

        Const REGION_START As String = "//region"

        Const REGION_END As String = "//endregion"

        Dim selection As EnvDTE.TextSelection = DTE.ActiveDocument.Selection

        Dim startRegions As Stack = New Stack()         '堆棧

        Dim intCollapseStart As Integer = 0

        Dim intCollapseNum As Integer = 0

        Dim strLines() As String

        selection.StartOfDocument(True)

        selection.SelectAll()

        strLines = selection.Text.Split(vbCrLf)         '擷取所有行

        For i = 0 To strLines.Length - 1

            If strLines(i).TrimStart.StartsWith(REGION_START) Then

                startRegions.Push(i + 1)                            '儲存行号   

            End If

            If strLines(i).TrimStart.StartsWith(REGION_END) Then

                intCollapseStart = startRegions.Pop() + 1           '傳回行号   

                intCollapseNum = (i + 1) - intCollapseStart + 1     '傳回要折疊的行數

                selection.GotoLine(intCollapseStart)

                selection.LineDown(True, intCollapseNum)

                selection.SwapAnchor()

                selection.OutlineSection()

        Next

        selection.StartOfDocument()

    End Sub

End Module

     1.3  注意

      1.3.1.  由上文的"//#region" 、"//#endregion"修改成了本文的"//region"和"//endregion" 。

      1.3.2   如果想把"//region"這一行也隐藏掉隻剩下"...",隻需要将宏代碼"intCollapseStart = startRegions.Pop() + 1"後面的"+1"去掉即可。遺憾的是沒能弄出C# 折疊的那種效果出來。

      1.3.3  如果還想支援if for 等關鍵字的折疊,強烈推薦文章1,本文也是在此文的基礎上修改的,改正了"//region"後面不能接注釋的缺陷。

二、支援JS的Visual Studio插件

            2.1      ScriptOutline      從試用的情況看來并沒有折疊,但是他顯示了方法大綱,且無需設定快捷鍵,作為插件和VS內建,同樣能達到快速找到方法的目的。參照文章3。

                  2.1.2      拷貝壓縮檔案中的ScriptOutline.AddIn、ScriptOutline.dll到目錄 C:\Documents and Settings\<username>\My Documents\Visual Studio 2005\Addins

                        如果Addins目錄沒有的話自己建一個就行。

                  2.1.3      工具(Tools) -> 外部程式管理器(Add-in Manager...),勾上ScriptOutline插件,确定即可顯示Script Outline視窗。

                  2.1.4      編寫測試代碼,效果如圖:

                  藉此我們可以在方法間快速切換!注意這裡使用的環境是Microsoft Visual Studio 2005。             

            2.2      SmartOutline

                  2.2.2      安裝插件 SmartOutline_v1.1.msi ,下一步下一步就行。工具欄會出現SmallOutline,可能需要重新開機VS。

                  2.2.3      編寫測試代碼,依次按步驟:

                       2.2.3.1      選中要折疊的函數,出現如下提示

        

                        2.2.3.2      點選提示或輸入組合快捷鍵 Alt+S、Alt+C ,彈出如下對話框,輸入JS代碼折疊後顯示的注釋名

         

                        2.2.3.3      最終效果

      2.2.4  總結

        比較之下還是這個最好用,如下優點:

        a).  不污染源代碼,和C#裡面寫#region的效果一樣。

        b).  折疊效果好,能顯示折疊後代碼塊的注釋,不需要像宏那樣關掉之後重新激活。

        c).  此插件同時支援VS2005和VS2008,不僅如此,還支援C#、HTML、CSS等,可以從SmallOutline -> General -> Enable SmallOutline for the following files下面的清單裡看到支援的其他檔案。

繼續閱讀