括号高亮显示不正确 - VB.NET

3

我正在制作一个用于我的编程语言的文本编辑器。它运行得非常好,但我有一个问题:当我输入类似以下内容时:

void Example() {
   for (int i = 0, 10; i++) {
    console.println(i);
   }
}

for循环的结束括号
for (int i = 0, 10; i++) {
    console.println(i);
} <== This one

没有加亮显示。有人能帮我吗? 提前感谢!

下面是我的代码:

Imports System.Drawing.Color
Imports System.Drawing.Font

Public Class Form1

Private Sub Highlight(ByRef Text As String(), ByRef Name As String)
    Dim Color As Color = Nothing
    Dim Font As Font = Nothing
    Select Case Name
        Case "Keywords"
            Color = Blue
            Font = NewFont(FontStyle.Regular)
        Case "Functions"
            Color = Black
            Font = NewFont(FontStyle.Italic)
        Case "Classes"
            Color = Cyan
            Font = NewFont(FontStyle.Regular)
        Case "Types"
            Color = Purple
            Font = NewFont(FontStyle.Regular)
        Case "Operators"
            Color = GreenYellow
            Font = NewFont(FontStyle.Regular)
        Case "Brackets"
            Color = Red
            Font = NewFont(FontStyle.Regular)
    End Select
    Dim CursorPos As Integer = tb.SelectionStart
    For i As Integer = 0 To Text.Length - 1
        FindAll(tb, Text(i))
        tb.SelectionColor = Color
        tb.SelectionFont = Font
        tb.DeselectAll()
    Next
    tb.SelectionStart = CursorPos
    tb.SelectionColor = Nothing
    tb.SelectionFont = NewFont(FontStyle.Regular)
End Sub

Private Function NewFont(ByVal Style As FontStyle)
    Return New Font(tb.Font, Style)
End Function

Private Sub FindAll(ByRef tb As RichTextBox, ByRef Find As String)
    Dim StartIndex As Integer = 0
    Dim Text As String = tb.Text
    Do
        Dim Index As Integer = Text.IndexOf(Find, StartIndex)
        If Index < 0 Then
            Exit Do
        End If
        tb.Select(Index, Find.Length)
        StartIndex = Index + 1
    Loop
End Sub

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tb.TextChanged
    Dim Keywords As String() = {"new", "using", "void", "function", "public", "protected", "private", "if", "else", "for", "loop", "while", "until", "true", "false", "null", "default"}
    Dim Types As String() = {"string", "int", "long", "byte", "char"}
    Dim Brackets As String() = {"(", ")", "[", "]", "{", "}"}
    Dim Operators As String() = {"+", "-", "=", "/", "*"}
    Dim Classes As String() = {"console", "color", "font"}
    Dim Functions As String() = {"print", "println"}
    Highlight(Keywords, "Keywords")
    Highlight(Types, "Types")
    Highlight(Brackets, "Brackets")
    Highlight(Operators, "Operators")
    Highlight(Classes, "Classes")
    Highlight(Functions, "Functions")
End Sub
End Class

1
你应该将数组移出“TextChanged”事件(或者至少将它们设置为“Static”)。每次声明新的数组都会浪费内存。 - Visual Vincent
谢谢,但它还是不起作用。括号仍然没有高亮显示。 - HighTechProgramming15
我并没有说这是解决方案。但它可以减少内存使用量,可能会稍微提高速度。 - Visual Vincent
1个回答

0

你的代码只突出显示每个括号、运算符等的最后一个出现。

在每次成功查找后应用字体和颜色将会正确地影响文本。

顺便说一句,你的代码没有取消关键字的高亮(例如void更改为vo id仍然保持高亮)。

结果 tadaa

enter image description here

修改 FindAll() 过程
    Private Sub FindAll(ByRef tb As RichTextBox, ByRef Find As String, ByRef StartIndex As Integer)

    Dim Text As String = tb.Text

    Dim Index As Integer = Text.IndexOf(Find, StartIndex)
    StartIndex = Index + 1
    If Index < 0 Then
        Exit Sub
    End If
    tb.Select(Index, Find.Length)
End Sub

Highlight过程的末尾和For循环

        Dim CursorPos As Integer = tb.SelectionStart

    For i As Integer = 0 To Text.Length - 1
        Dim StartIndex As Integer = 0
        Do
            FindAll(tb, Text(i), StartIndex)

            If StartIndex = 0 Then
                Exit Do
            End If

            tb.SelectionColor = Color
            tb.SelectionFont = Font
            tb.DeselectAll()
        Loop
        tb.SelectionStart = CursorPos
        tb.SelectionColor = Nothing
        tb.SelectionFont = NewFont(FontStyle.Regular)
    Next

非常感谢!它起作用了!但是,“vo id”仍然被突出显示。 - HighTechProgramming15
@HighTechProgramming15:加油,你做得很好。 - Yakov R.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接