突出显示的括号无法正常工作 - VB.NET

时间:2016-05-02 12:41:06

标签: vb.net syntax-highlighting

我正在为我自己的编程语言创建一个文本编辑器。它运作得很好,但我有一个问题;当我输入这样的东西时:

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

for-loop的右括号

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 个答案:

答案 0 :(得分:0)

您的代码只是高举每个括号,运算符等的最后一次出现......

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

BTW-您的代码没有取消关键字(例如 void 更改为 vo id 保持hilited)

结果 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

以及突出显示过程结束时的 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