VS宏,js文件之region折叠

参考:
http://blog.csdn.net/javaeecoding/archive/2009/04/01/4040995.aspx
http://www.cnblogs.com/over140/archive/2009/06/22/1507564.html

作用:
  在vs中折叠js之region段。

缺陷:

  ①设置了书签,但未能清除书签。

  ②目前只能处理掉“region两层嵌套”,超过两层将出错。

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Imports System.Collections

#Const WriteRegionComment = True
#Const UseBookmark = True

Public Class JsHelper

  ' You can delete the "#".
  Const CON_REGION_START As String = "//#region"
  Const CON_REGION_END As String = "//#endregion"
  Const CON_START_COMMENT As String = "//△Region:"
  Private Shared CON_LENGTH_OF_REGION_START As Integer = Len(CON_REGION_START)
  Private Shared CON_LENGTH_OF_REGION_END As Integer = Len(CON_REGION_END)
  Private Shared CON_LENGTH_OF_COMMENT_START As Integer = Len(CON_START_COMMENT)

 


  Private Shared Function CheckFileType() As Boolean
    Dim oDoc As Document
    Try
      oDoc = DTE.ActiveDocument
    Catch ex As Exception
      Exit Function
    End Try

    If Not IsJsFile(oDoc.Name) Then
      MsgBox("当前文档不属于js文件,无法插入相关代码", , "HtmlHelper.CheckFileType()")
      Return False
    End If

    Return True
  End Function

 

  Private Function IsJsFile( _
    ByRef i_sFileName As String _
  ) As Boolean
    'ByRef i_sExtendName As String _
    Dim sExtendName As String = GetExtendNameFromFullName(i_sFileName)
    If sExtendName.Length = 0 Then
      Return False
    End If
    Return sExtendName.EndsWith("js")
  End Function

 


  Public Shared Sub _ClearRegionComments()
    If Not CheckFileType() Then
      Exit Sub
    End If

    DTE.ActiveDocument.Selection.SetBookmark()

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

    Dim saLineText() As String
    saLineText = selection.Text.Split(vbCrLf)

    Dim sTrimedStartLineText As String
    Dim iCount As Integer = saLineText.Length
    For i = iCount - 1 To 0 Step -1
      sTrimedStartLineText = saLineText(i).TrimStart
      If sTrimedStartLineText.StartsWith(CON_START_COMMENT) Then
        selection.GotoLine(i)
        selection.EndOfLine()
        selection.LineDown(True, 1)
        selection.EndOfLine(True)
        selection.Delete()
      End If
    Next
  End Sub

  Public Shared Sub _OutlineRegions()
    OutlineRegionsSub()
  End Sub

  Public Shared Sub _OutlineRegionWithoutComments()
    OutlineRegionsSub(False)
  End Sub

  Private Shared Sub OutlineRegionsSub(Optional ByVal i_bCreateRegion As Boolean = True)
    If Not CheckFileType() Then
      Exit Sub
    End If


#If UseBookmark Then
    DTE.ActiveDocument.Selection.SetBookmark()
#End If

 

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

 

    '' greenxmcn: Stop outlining, if you use another addin or macro to set outline, please commit the next statement.
    'DTE.ExecuteCommand("Edit.StopOutlining")

 

    ' SEE: http://blog.csdn.net/javaeecoding/archive/2009/04/01/4040995.aspx
    ' javaeecoding:判定Collapse存在(为解决折叠后行号发生变化的Bug)
    selection.EndOfDocument()
    Dim iLineIndexBeforeAllOutlining As Integer = selection.CurrentLine()
    DTE.ExecuteCommand("Edit.ToggleAllOutlining")
    selection.EndOfDocument()
    Dim iLineIndexAfterAllOutlining As Integer = selection.CurrentLine()
    If iLineIndexBeforeAllOutlining > iLineIndexAfterAllOutlining Then
      DTE.ExecuteCommand("Edit.ToggleAllOutlining")
    End If

 

    Dim saLineText() As String
    saLineText = selection.Text.Split(vbCrLf)

    'Dim sEndLineIndexList As String = ""

#If WriteRegionComment Then
    Dim stackComments As Stack = New Stack()
    Dim saComment As String()
    Dim iCommentMaxIndex As Integer = -1
    Dim stackCommentsPos As Stack = New Stack()
    Dim iWriteCommentPos As Integer
#End If

    Dim intCollapseStart As Integer = 0
    Dim intCollapseNum As Integer = 0
    Dim sTrimedStartLineText As String
    Dim startRegions As Stack = New Stack()

    Dim iMaxIndex As Integer = saLineText.Length - 1

    '农民伯伯 --- 自动为"//#endregion"结束的代码添加最后一行,不然出错
    'If the current document ends with "//#endregion", append a new empty line.
    sTrimedStartLineText = saLineText(iMaxIndex).TrimStart
    If sTrimedStartLineText.StartsWith(CON_REGION_END) Then
      selection.EndOfDocument()
      selection.NewLine()
    End If

    For i = 0 To iMaxIndex
      sTrimedStartLineText = saLineText(i).TrimStart
      If sTrimedStartLineText.StartsWith(CON_REGION_START) Then
        startRegions.Push(i + 1)
#If WriteRegionComment Then
        'If (iCommentMaxIndex < -1) Then
        '  MsgBox(iCommentMaxIndex)
        'End If
        If i_bCreateRegion Then
          iCommentMaxIndex += 1
          If iCommentMaxIndex = 0 Then
            ReDim saComment(iCommentMaxIndex)
          Else
            ReDim Preserve saComment(iCommentMaxIndex)
          End If
          saComment(iCommentMaxIndex) = Trim(sTrimedStartLineText.Substring(CON_LENGTH_OF_COMMENT_START))
        End If
#End If
      ElseIf sTrimedStartLineText.StartsWith(CON_REGION_END) Then
        intCollapseStart = startRegions.Pop() '+ 1
        intCollapseNum = (i + 1) - intCollapseStart + 1

        selection.GotoLine(intCollapseStart)
        selection.LineDown(True, intCollapseNum)
        selection.SwapAnchor()
        selection.OutlineSection()
        DTE.ExecuteCommand("Edit.ToggleOutliningExpansion")

        'sEndLineIndexList &= IIf(Len(sEndLineIndexList) = 0, "", vbCrLf) & CStr(intCollapseStart) & "-" & CStr(i + 1)

#If WriteRegionComment Then
        If i_bCreateRegion Then
          iWriteCommentPos = i + 2
          If iWriteCommentPos > iMaxIndex + 1 OrElse _
            (Not (saLineText(iWriteCommentPos - 1).TrimStart.StartsWith(CON_START_COMMENT))) Then
            Dim sComment As String = Trim(saComment(iCommentMaxIndex))
            stackCommentsPos.Push(iWriteCommentPos)
            stackComments.Push(sComment)
          End If

          iCommentMaxIndex -= 1
          Select Case iCommentMaxIndex
            Case -1
              Erase saComment
            Case Else
              ReDim Preserve saComment(iCommentMaxIndex)
          End Select
      End If
      End If
#End If
    Next
    'MsgBox("sEndLineIndexList:" & VbCrLf & sEndLineIndexList)

#If WriteRegionComment Then
    If i_bCreateRegion Then
      'Dim sCommentLineIndexList As String
      Do While (stackCommentsPos.Count > 0)
        iWriteCommentPos = stackCommentsPos.Pop()
        Dim sComment As String = Trim(stackComments.Pop())
        selection.GotoLine(iWriteCommentPos - 1)
        selection.EndOfLine()
        selection.NewLine()
        selection.Insert(CON_START_COMMENT & sComment)
        'sCommentLineIndexList &= CStr(iWriteCommentPos) & vbCrLf
      Loop
      'MsgBox("sCommentLineIndexList:" & VbCrLf & sCommentLineIndexList)
    End If
#End If

    ' Format the document using vs method.
    DTE.ExecuteCommand("Edit.FormatDocument")

 

    DTE.ExecuteCommand("Edit.ToggleAllOutlining")

 

#If UseBookmark Then
    selection.PreviousBookmark()
    selection.NextBookmark()
    ' Try to clear the temp bookmark, but failed. I don't know why.
    DTE.ActiveDocument.Selection.ClearBookmark()
#Else
    selection.StartOfDocument()
#End If

 

    startRegions = Nothing

#If WriteRegionComment Then
    If i_bCreateRegion Then
      stackComments = Nothing
      stackCommentsPos = Nothing

      Erase saComment
    End If
#End If
  End Sub
End Class

 参考:
http://blog.csdn.net/javaeecoding/archive/2009/04/01/4040995.aspx
http://www.cnblogs.com/over140/archive/2009/06/22/1507564.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
'******vs实现js格式化和代码折叠****** '* 版权所有(C) sksmks 2009 '* 文件名称 : JsFormat '* 当前版本 : 1.0.0.1 '* 作 者 : 黄会攀 (@@:272302281) '* 设计日期 : 2009年10月31日 '* 内容摘要 : VS2008 JsFormat Macro '*******************************************************************************/ 一. 目的 在程序的开发中,javascript脚本的扩大,代码折叠,格式化是必不可少的一部分。对于大多数注释,代码折叠,格式化,都有着相似的样子,另外,在团队开发的过程,脚本太大,太长。为了提高在开发过程中,代码的可读性和开发效率,可以利用VS提供的Macro扩展功能,实现一套Macro模板。在使用的过程中,只需要使用相应的快捷键,便可格式化脚本和查看代码结构。 二. 实现 (打开资源管理器:视图 -> 其他窗口 -> 资源管理器 或者按 Alt+F8) 1. 新建Macro工程 打开 菜单 --> 工具 --> (Macro) --> 新建(New Macro Project...),或者(资源管理器Alt+F8 )根据向导提示建立工程。 2. 编辑Macro工程 打开 菜单 --> 工具 --> (Macro) --> 资源管理器(Macro Explorer) ,在Explorer中选择新建的工程,修改Module名(右键),在Module名上双击,在打开的Macro IDE中进行编辑。 3. 在Module下,添加要实现的MacroForJs,并实现(内容为向Active Document中添加注释)。 4. 保存 5.在VS2008中,双击添加的Macro,将执行对应的Macro代码(使用VBScript在Macro IDE中实现的)。 6. 建立快捷键 打开 菜单 --> 工具 --> 选项 --> 键盘 ,在列表框中选择刚才添加的MacroForJsPro里的OutlineFun (作用:注释),然后在 Press shortcuts keys中输入快捷键,点击Assign 。 打开 菜单 --> 工具 --> 选项 --> 键盘 ,在列表框中选择刚才添加的MacroForJsPro里的FormatForJsChar (作用:格式化),然后在 Press shortcuts keys中输入快捷键,点击Assign 。 7. 这时候,就可以直接使用快捷键来执行MacroForJs 。 三. 使用附件 1. 加载 打开 菜单 --> 工具(Tools) --> (Macro) --> 加载项目(Load Macro Project... ),选择附件里的工程。 2. 配置快捷键 3. 完成 具体的实现代码见附件工程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值