我正在开发一个支持脚本编写的C#应用程序。是否有任何样本FoldingStrategy
的Visual Basic代码用于AvalonEdit的代码折叠?
答案 0 :(得分:1)
这是我创建的一个似乎有用的...相当好。 不完美,因为它没有考虑到许多可能的情况,但它是一个开始。
Imports ICSharpCode.AvalonEdit.Folding
Imports ICSharpCode.AvalonEdit.Document
Public Class VBNETFoldingStrategy
Inherits AbstractFoldingStrategy
Private foldableKeywords() As String = {"namespace", "class", "sub", "function", "structure", "enum"}
Public Overrides Function CreateNewFoldings(document As ICSharpCode.AvalonEdit.Document.TextDocument, ByRef firstErrorOffset As Integer) As System.Collections.Generic.IEnumerable(Of ICSharpCode.AvalonEdit.Folding.NewFolding)
firstErrorOffset = -1
Dim foldings As New List(Of NewFolding)
Dim text As String = document.Text
Dim lowerCaseText As String = text.ToLower()
For Each foldableKeyword In foldableKeywords
foldings.AddRange(GetFoldings(text, lowerCaseText, foldableKeyword))
Next
Return foldings.OrderBy(Function(f) f.StartOffset)
End Function
Public Function GetFoldings(text As String, lowerCaseText As String, keyword As String) As IEnumerable(Of NewFolding)
Dim foldings As New List(Of NewFolding)
Dim closingKeyword As String = "end " + keyword
Dim closingKeywordLength As String = closingKeyword.Length
keyword += " "
Dim keywordLength As String = keyword.Length
Dim startOffsets As New Stack(Of Integer)
For i As Integer = 0 To text.Length - closingKeywordLength
If lowerCaseText.Substring(i, keywordLength) = keyword Then
Dim k As Integer = i
If k <> 0 Then
Dim lastLetterPos As Integer = i
Do Until text(k - 1) = vbLf OrElse text(k - 1) = vbCr
If Char.IsLetter(text(k)) Then lastLetterPos = k
k -= 1
Loop
If lastLetterPos > k Then k = lastLetterPos
End If
startOffsets.Push(k)
ElseIf lowerCaseText.Substring(i, closingKeywordLength) = closingKeyword Then
Dim startOffset As Integer = startOffsets.Pop()
Dim newFolding As NewFolding = New NewFolding(startOffset, i + closingKeywordLength)
Dim p As Integer = text.IndexOf(vbLf, startOffset)
If p = -1 Then p = text.IndexOf(vbCr, startOffset)
If p = -1 Then p = text.Length - 1
newFolding.Name = text.Substring(startOffset, p - startOffset)
foldings.Add(newFolding)
End If
Next
Return foldings
End Function
End Class
答案 1 :(得分:1)
我在这段代码中发现了两个错误。有两个超出范围的出现在:
Do Until text(k - 1) = vbLf OrElse text(k - 1) = vbCr
如果k跑到0.我将其固定为一行:
k -= 1
If k <= 0 Then Exit Do
和at:
Dim startOffset As Integer = startOffsets.Pop()
如果startOffset为空,则如果由:
修复,则为空If startOffsets.Count > 0 Then
Dim startOffset As Integer = startOffsets.Pop()
.....
End If
但是IndingSubs上的折叠生成失败了。试试这个例子:
Public Class Window
Public Sub New()
InitializeComponent()
AddHandler TextBox.TextChanged, Sub(TBsender As Object, TBe As EventArgs)
CheckCode()
End Sub
End Sub
End Class
我没有设法解决最后一个问题。