I am developing a C# application which supports scripting. Does any have a sample FoldingStrategy
for Visual Basic code to be used for code folding for AvalonEdit?
Asked
Active
Viewed 1,563 times
0

noway
- 2,585
- 7
- 41
- 61
-
There is one supplied with the source code in C#. This is a bracket matching strategy that should be easily extended. – MoonKnight Jun 13 '12 at 22:19
2 Answers
1
Here's one I created which appears to work... rather well. Not perfect, as it doesn't take into consideration many possible situations but it's a start.
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

xfx
- 1,329
- 8
- 18
1
i found two errors on this code. there are two out of range exeptions at:
Do Until text(k - 1) = vbLf OrElse text(k - 1) = vbCr
if k runns to 0. i fixed that by one line:
k -= 1
If k <= 0 Then Exit Do
and at:
Dim startOffset As Integer = startOffsets.Pop()
if startOffset is empty this one if fixed by:
If startOffsets.Count > 0 Then
Dim startOffset As Integer = startOffsets.Pop()
.....
End If
But the Folding generation Fails on InlineSubs. Try this Example:
Public Class Window
Public Sub New()
InitializeComponent()
AddHandler TextBox.TextChanged, Sub(TBsender As Object, TBe As EventArgs)
CheckCode()
End Sub
End Sub
End Class
I didn't manage to fix this last problem.

derDere
- 11
- 1