0

我正在开发一个支持脚本的 C# 应用程序。是否有FoldingStrategy用于 AvalonEdit 代码折叠的 Visual Basic 代码示例?

4

2 回答 2

1

我在这段代码中发现了两个错误。有两个超出范围的例外:

Do Until text(k - 1) = vbLf OrElse text(k - 1) = vbCr

如果 k 运行到 0。我通过一行修复了它:

k -= 1
If k <= 0 Then Exit Do

并在:

Dim startOffset As Integer = startOffsets.Pop()

如果 startOffset 为空,则此选项通过以下方式固定:

If startOffsets.Count > 0 Then
    Dim startOffset As Integer = startOffsets.Pop()
    .....
End If

但是折叠生成在 InlineSubs 上失败了。试试这个例子:

Public Class Window
    Public Sub New()
        InitializeComponent()

        AddHandler TextBox.TextChanged, Sub(TBsender As Object, TBe As EventArgs)
                                             CheckCode()
                                        End Sub
    End Sub
End Class

我没有设法解决最后一个问题。

于 2014-06-06T15:21:47.670 回答
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
于 2012-08-31T03:12:09.170 回答