作用:
在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