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

标签 vb.net syntax-highlighting

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

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

最佳答案

您的代码仅突出显示每个括号、运算符等的最后一次出现...

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

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

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

关于vb.net - 括号突出显示无法正常工作 - VB.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36982720/

相关文章:

mysql - 我不知道这段代码的错误在哪里

javascript - 如何对来自 JSON 的代码进行语法突出显示

syntax-highlighting - Sublime Text 2 中的自定义语法高亮

xcode - 修改 Xcode 语法突出显示以灰显 NSAssert 行

vb.net - VB.NET 有匿名函数吗?

javascript - 如何识别搜索模式

vb.net - Convert.ToInt32 格式不正确

vb.net - 通过选定的文本获取DropDownList选定的值

vim - vi - "pop"保留字和函数的解释

css - 删除在命名的 CSS 颜色上突出显示的激烈 Sublime Text 语法