Joseph Michael Pesch
VP Programming

JavaScript/CSS Collapsable Regions

by 20. August 2010 15:13

VisualStudio 2010 JavaScript Outlining: Visual Studio 2010 editor extension for JavaScript code blocks and custom regions outlining
http://jsoutlining.codeplex.com/

Alternate method is to use Macro Code:

OptionExplicitOn

OptionStrictOn

 

Imports System

Imports EnvDTE

Imports EnvDTE80

Imports EnvDTE90

Imports System.Diagnostics

Imports System.Collections.Generic

 

PublicModule JsModules

 

  Sub OutlineRegions()

 

    Dim selection As EnvDTE.TextSelection = CType(DTE.ActiveDocument.Selection, EnvDTE.TextSelection)

 

    Const REGION_START AsString = "//#region"

    Const REGION_END AsString = "//#endregion"

 

    selection.SelectAll()

    Dim text AsString = selection.Text

    selection.StartOfDocument(True)

 

    Dim startIndex AsInteger

    Dim endIndex AsInteger

    Dim lastIndex AsInteger = 0

    Dim startRegions AsNew Stack(OfInteger)

 

    Do

      startIndex = text.IndexOf(REGION_START, lastIndex)

      endIndex = text.IndexOf(REGION_END, lastIndex)

 

      If startIndex = -1 AndAlso endIndex = -1 Then

        ExitDo

      EndIf

 

      If startIndex <> -1 AndAlso startIndex < endIndex Then

        startRegions.Push(startIndex)

        lastIndex = startIndex + 1

      Else

        ' Outline region ...

        Dim tempStartIndex AsInteger = CInt(startRegions.Pop())

        selection.MoveToLineAndOffset(CalcLineNumber(text, tempStartIndex), CalcLineOffset(text, tempStartIndex))

        selection.MoveToLineAndOffset(CalcLineNumber(text, endIndex) + 1, 1, True)

        selection.OutlineSection()

 

        lastIndex = endIndex + 1

      EndIf

    Loop

 

    selection.StartOfDocument()

  EndSub

 

  PrivateFunction CalcLineNumber(ByVal text AsString, ByVal index AsInteger) AsInteger

    Dim lineNumber AsInteger = 1

    Dim i AsInteger = 0

 

    While i < index

      If text.Chars(i) = vbLf Then

        lineNumber += 1

        i += 1

      EndIf

 

      If text.Chars(i) = vbCr Then

        lineNumber += 1

        i += 1

        If text.Chars(i) = vbLf Then

          i += 1 'Swallow the next vbLf

        EndIf

      EndIf

 

      i += 1

    EndWhile

 

    Return lineNumber

  EndFunction

 

  PrivateFunction CalcLineOffset(ByVal text AsString, ByVal index AsInteger) AsInteger

    Dim offset AsInteger = 1

    Dim i AsInteger = index - 1

 

    'Count backwards from //#region to the previous line counting the white spaces

    Dim whiteSpaces = 1

    While i >= 0

      Dim chr AsChar = text.Chars(i)

      If chr = vbCr Or chr = vbLf Then

        whiteSpaces = offset

        ExitWhile

      EndIf

      i -= 1

      offset += 1

    EndWhile

 

    'Count forwards from //#region to the end of the region line

    i = index

    offset = 0

    Do

      Dim chr AsChar = text.Chars(i)

      If chr = vbCr Or chr = vbLf Then

        Return whiteSpaces + offset

      EndIf

      offset += 1

      i += 1

    Loop

 

    Return whiteSpaces

  EndFunction

 

EndModule

 

 

See this URL for full posting on Macro code: http://stackoverflow.com/questions/1921628/how-to-implement-regions-code-collapse-in-javascript

Tags:

[None]

Comments are closed