Syntax highlighting

help please?

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 5171 Views - Last Post: 12 February 2010 - 03:28 AM Rate Topic: -----

#1 Rickster0  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 236
  • Joined: 08-July 08

Syntax highlighting

Posted 09 February 2010 - 07:17 AM

Ok, i've been making my own syntax highlighter for my project, and i need to know how to make the colour of the text between these two text values green

=begin
something like this
if it can be done ?
=end


heres what i have so far.

Public Class ScriptEditor

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

        Dim words As New List(Of String)

        words.Add("Select")

        words.Add("Insert")

        Dim arrKeyWords() As String, strKW As String
        Dim arrOperators() As String, strOP As String
        Dim arrComments() As String, strCM As String
        Dim arrVariables() As String, strVAR As String

        arrKeyWords = New String() {"alias", "and", "begin", "break", "case", "catch", "class", "def", "do", "elsif", "else", "fail" _
                                    , "ensure", "for", "end", "if", "in", "module", "next", "not", "or", "raise" _
                                    , "redo", "rescue", "retry", "return", "then", "throw", "super", "unless", "undef", "until" _
                                    , "when", "while", "yield", "BEGIN", "END", "Defined?", "nil", "self", "True", "False" _
                                    , "__FILE__", "__LINE__"}

        arrOperators = New String() {"=", "==", "+", "-", "*", "/", ">", "<", "||", ":"}

        arrComments = New String() {"=begin", "=end", "#"}

        arrVariables = New String() {"@", "@@", "$"}

        If RichTextBox1.Text.Length > 0 Then

            Dim selectStart As Integer = RichTextBox1.Selectionstart

            RichTextBox1.Select(0, RichTextBox1.Text.Length)

            RichTextBox1.SelectionColor = Color.Black

            RichTextBox1.DeselectAll()

            'Variable Colouring
            For Each strVAR In arrVariables

                Dim pos As Integer = 0

                Do While RichTextBox1.Text.ToUpper.IndexOf(strVAR.ToUpper, pos) >= 0

                    pos = RichTextBox1.Text.ToUpper.IndexOf(strVAR.ToUpper, pos)

                    RichTextBox1.Select(pos, strVAR.Length)

                    RichTextBox1.SelectionColor = Color.Blue

                    pos += 1

                Loop
            Next

            'Keyword Colouring
            For Each strKW In arrKeyWords

                Dim pos As Integer = 0

                Do While RichTextBox1.Text.ToUpper.IndexOf(strKW.ToUpper, pos) >= 0

                    pos = RichTextBox1.Text.ToUpper.IndexOf(strKW.ToUpper, pos)

                    RichTextBox1.Select(pos, strKW.Length)

                    RichTextBox1.SelectionColor = Color.Blue

                    pos += 1

                Loop
            Next
            ' Operator Colouring
            For Each strOP In arrOperators
                Dim pos As Integer = 0
                Do While RichTextBox1.Text.ToUpper.IndexOf(strOP.ToUpper, pos) >= 0

                    pos = RichTextBox1.Text.ToUpper.IndexOf(strOP.ToUpper, pos)

                    RichTextBox1.Select(pos, strOP.Length)

                    RichTextBox1.SelectionColor = Color.LightBlue

                    pos += 1

                Loop

            Next
            ' Comment Colouring
            For Each strCM In arrComments

                Dim pos As Integer = 0

                Do While RichTextBox1.Text.ToUpper.IndexOf(strCM.ToUpper, pos) >= 0

                    pos = RichTextBox1.Text.ToUpper.IndexOf(strCM.ToUpper, pos)

                    RichTextBox1.Select(pos, strCM.Length)

                    RichTextBox1.SelectionColor = Color.Green

                    pos += 1


                Loop
            Next

            RichTextBox1.Selectionstart = selectStart

        End If

    End Sub
End Class




Is This A Good Question/Topic? 0
  • +

Replies To: Syntax highlighting

#2 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Syntax highlighting

Posted 09 February 2010 - 08:54 AM

What you need to do is if the start of the =begin and =end tags. You could store the position of the begin tag between calls to TextChanged or check for the end tag after finding a begin tag. Below I have sketched out one method that looks for the begin tag after finding the end tag. I've also changed the single line comment to color the entire line.
            For Each strCM As String In arrComments

                Dim pos As Integer = 0

                Do While RichTextBox1.Text.ToUpper.IndexOf(strCM.ToUpper, pos) >= 0

                    pos = RichTextBox1.Text.ToUpper.IndexOf(strCM.ToUpper, pos)

                    If strCM.ToUpper = "=END" Then
                        Dim iSPos As Integer = RichTextBox1.Text.ToUpper.LastIndexOf("="c, pos - 1, pos)
                        If RichTextBox1.Text.ToUpper()(iSPos + 1) = "B"c Then
                            RichTextBox1.Select(iSPos, pos + strCM.Length)
                            RichTextBox1.SelectionColor = Color.Green
                        End If
                    ElseIf strCM = "#" Then
                        RichTextBox1.Select(pos, RichTextBox1.Text.IndexOf(Chr(10)))
                        RichTextBox1.SelectionColor = Color.Green
                    End If
                    pos += 1
                Loop
            Next

This post has been edited by T3hC13h: 09 February 2010 - 09:01 AM

Was This Post Helpful? 0
  • +
  • -

#3 Rickster0  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 236
  • Joined: 08-July 08

Re: Syntax highlighting

Posted 09 February 2010 - 09:00 AM

ah thank you, the =begin and =end tags work perfectly, but if i do

#hello im a comment line

only the hello is in green the rest is black.

#hello im a comment line

any ideas?

This post has been edited by Rickster0: 09 February 2010 - 09:01 AM

Was This Post Helpful? 0
  • +
  • -

#4 Rickster0  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 236
  • Joined: 08-July 08

Re: Syntax highlighting

Posted 09 February 2010 - 09:16 AM

Also, can you help me with the same kind of thing that you did for the =begin to =end.

but i would like it to be done for "hello" and 'Test'

thanks again.
Was This Post Helpful? 0
  • +
  • -

#5 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Syntax highlighting

Posted 09 February 2010 - 09:17 AM

Check my edit. This block
    ElseIf strCM = "#" Then
        RichTextBox1.Select(pos, RichTextBox1.Text.IndexOf(Chr(10)))
        RichTextBox1.SelectionColor = Color.Green
    End If


in my first post should be coloring the entire line after the comment token.

This post has been edited by T3hC13h: 09 February 2010 - 09:18 AM

Was This Post Helpful? 0
  • +
  • -

#6 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Syntax highlighting

Posted 09 February 2010 - 09:18 AM

Hi,

You can try something like this:

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        Dim cl As Long
        On Error Resume Next
        If RichTextBox1.TextLength = 0 Then Exit Sub
        cl = RichTextBox1.Selectionstart
        Dim StartPos As Long
        Dim EndPos As Long
        Dim CurrentPos As Long = RichTextBox1.Selectionstart
        StartPos = 1 : EndPos = 1
        StartPos = InStrRev(RichTextBox1.Text, Chr(10), CurrentPos)
        If StartPos = 0 Then StartPos = 1
        EndPos = InStr(CurrentPos, RichTextBox1.Text, Chr(10), CompareMethod.Binary)
        If EndPos = 0 Then EndPos = RichTextBox1.TextLength
        RichTextBox1.Selectionstart = StartPos
        RichTextBox1.SelectionLength = EndPos - StartPos + 1
        RichTextBox1.SelectionColor = Color.Black
        'RichTextBox1.Text
        Dim Lowerbounds As Long = StartPos
        Dim Upperbounds As Long = EndPos
        StartPos = Lowerbounds : EndPos = Lowerbounds

        Do
            ' Char = =
            StartPos = InStr(EndPos, RichTextBox1.Text, Chr(61), CompareMethod.Binary)
            If StartPos = 0 Then Exit Do
            If StartPos > Upperbounds Then Exit Do
            If StartPos = Len(RichTextBox1.Text) Then Exit Do
            EndPos = InStr(StartPos + 1, RichTextBox1.Text, Chr(61), CompareMethod.Binary)
            If EndPos = 0 Then Exit Do
            If EndPos > Upperbounds Then Exit Do
            EndPos = EndPos + 1
            RichTextBox1.Selectionstart = StartPos - 1
            RichTextBox1.SelectionLength = EndPos - StartPos
            RichTextBox1.SelectionColor = Color.Green
        Loop

        RichTextBox1.SelectionLength = 0
        RichTextBox1.Selectionstart = cl
        RichTextBox1.SelectionColor = Color.Black

    End Sub

This post has been edited by Luc001: 09 February 2010 - 09:20 AM

Was This Post Helpful? 0
  • +
  • -

#7 Rickster0  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 236
  • Joined: 08-July 08

Re: Syntax highlighting

Posted 09 February 2010 - 09:23 AM

@T3hC13h

that is the code in the project, yet it doesnt colour the whole line.


@Luc001

that is extremely hard for me to read, and extremly complicated.

can you comment some of the stuff :/
Was This Post Helpful? 0
  • +
  • -

#8 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Syntax highlighting

Posted 09 February 2010 - 09:25 AM

Hi,

I think I was a little late to post but anyway.
With my code you can add as much Characters as you like for example, I've added a few more:

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        Dim cl As Long
        On Error Resume Next
        If RichTextBox1.TextLength = 0 Then Exit Sub
        cl = RichTextBox1.Selectionstart
        Dim StartPos As Long
        Dim EndPos As Long
        Dim CurrentPos As Long = RichTextBox1.Selectionstart
        StartPos = 1 : EndPos = 1
        StartPos = InStrRev(RichTextBox1.Text, Chr(10), CurrentPos)
        If StartPos = 0 Then StartPos = 1
        EndPos = InStr(CurrentPos, RichTextBox1.Text, Chr(10), CompareMethod.Binary)
        If EndPos = 0 Then EndPos = RichTextBox1.TextLength
        RichTextBox1.Selectionstart = StartPos
        RichTextBox1.SelectionLength = EndPos - StartPos + 1
        RichTextBox1.SelectionColor = Color.Black
        'RichTextBox1.Text
        Dim Lowerbounds As Long = StartPos
        Dim Upperbounds As Long = EndPos
        StartPos = Lowerbounds : EndPos = Lowerbounds

        Do
            ' Char = =
            StartPos = InStr(EndPos, RichTextBox1.Text, Chr(61), CompareMethod.Binary)
            If StartPos = 0 Then Exit Do
            If StartPos > Upperbounds Then Exit Do
            If StartPos = Len(RichTextBox1.Text) Then Exit Do
            EndPos = InStr(StartPos + 1, RichTextBox1.Text, Chr(61), CompareMethod.Binary)
            If EndPos = 0 Then Exit Do
            If EndPos > Upperbounds Then Exit Do
            EndPos = EndPos + 1
            RichTextBox1.Selectionstart = StartPos - 1
            RichTextBox1.SelectionLength = EndPos - StartPos
            RichTextBox1.SelectionColor = Color.Green
        Loop

        Do
            ' Char = "
            StartPos = InStr(EndPos, RichTextBox1.Text, Chr(34), CompareMethod.Binary)
            If StartPos = 0 Then Exit Do
            If StartPos > Upperbounds Then Exit Do
            If StartPos = Len(RichTextBox1.Text) Then Exit Do
            EndPos = InStr(StartPos + 1, RichTextBox1.Text, Chr(34), CompareMethod.Binary)
            If EndPos = 0 Then Exit Do
            If EndPos > Upperbounds Then Exit Do
            EndPos = EndPos + 1
            RichTextBox1.Selectionstart = StartPos - 1
            RichTextBox1.SelectionLength = EndPos - StartPos
            RichTextBox1.SelectionColor = Color.Blue
        Loop
        Do
            ' Chars 91 = [ and 93 = ]
            StartPos = InStr(EndPos, RichTextBox1.Text, Chr(91), CompareMethod.Binary)
            If StartPos = 0 Then Exit Do
            If StartPos > Upperbounds Then Exit Do
            If StartPos = Len(RichTextBox1.Text) Then Exit Do
            EndPos = InStr(StartPos + 1, RichTextBox1.Text, Chr(93), CompareMethod.Binary)
            If EndPos = 0 Then Exit Do
            If EndPos > Upperbounds Then Exit Do
            EndPos = EndPos + 1
            RichTextBox1.Selectionstart = StartPos - 1
            RichTextBox1.SelectionLength = EndPos - StartPos
            RichTextBox1.SelectionColor = Color.Red
        Loop

        RichTextBox1.SelectionLength = 0
        RichTextBox1.Selectionstart = cl
        RichTextBox1.SelectionColor = Color.Black

    End Sub

Was This Post Helpful? 0
  • +
  • -

#9 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Syntax highlighting

Posted 09 February 2010 - 09:29 AM

View PostRickster0, on 09 February 2010 - 08:23 AM, said:

@T3hC13h

that is the code in the project, yet it doesnt colour the whole line.


There is a small drawback to using IndexOf and the new line char, you have to press enter before it will apply the color.

View PostRickster0, on 09 February 2010 - 08:23 AM, said:

Also, can you help me with the same kind of thing that you did for the =begin to =end.

but i would like it to be done for "hello" and 'Test'

thanks again.


I gave you an example of start-end highlight, give it a shot and see if you can't get it to work with different chars/string. If you get stuck post your code and I'll see if I can't get you back on track.
Was This Post Helpful? 0
  • +
  • -

#10 Rickster0  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 236
  • Joined: 08-July 08

Re: Syntax highlighting

Posted 09 February 2010 - 10:55 AM

Ok thanks T3, yeah it works when i press enter.

but if i do a new #comment. it cuts out to the same length of the first comment ....

EDIT:

i cant seem to get " or ' as a string :/

This post has been edited by Rickster0: 09 February 2010 - 11:08 AM

Was This Post Helpful? 0
  • +
  • -

#11 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Syntax highlighting

Posted 09 February 2010 - 11:04 AM

View PostRickster0, on 09 February 2010 - 09:55 AM, said:

Ok thanks T3, yeah it works when i press enter.

but if i do a new #comment. it cuts out to the same length of the first comment ....


Lol thats pretty funny. I found the bug, can you see where I went wrong with my code?
Was This Post Helpful? 0
  • +
  • -

#12 Rickster0  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 236
  • Joined: 08-July 08

Re: Syntax highlighting

Posted 09 February 2010 - 11:12 AM

No i cant lol, i havent got much experience in this area of VB.

also i found the number codes for " and ' but im not sure how to use them
Was This Post Helpful? 0
  • +
  • -

#13 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Syntax highlighting

Posted 09 February 2010 - 11:14 AM

Here is what I came up with. This time I'm not looking for the new line char at all. I just get the length of the line current line and pass that to our Select call.
ElseIf strCM = "#" Then
    With RichTextBox1
        Dim iLine As Integer = .GetLineFromCharIndex(pos)
        Dim sLine As String = .Lines(iLine)
        .Select(pos, sLine.Length)
        .SelectionColor = Color.Green
     End With
End If


This post has been edited by T3hC13h: 09 February 2010 - 11:15 AM

Was This Post Helpful? 1
  • +
  • -

#14 Rickster0  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 236
  • Joined: 08-July 08

Re: Syntax highlighting

Posted 09 February 2010 - 11:19 AM

Yes thats perfect, thanks.

Now for those strings XD.

i found this site for all the codes. but um, im not sure what i need to do to make them work as i said above.

http://yorktown.cbe....ASCIICodes.aspx

ok i found it out, Chr(34)

hmm now how to use that XD

This post has been edited by Rickster0: 09 February 2010 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

#15 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Syntax highlighting

Posted 09 February 2010 - 11:26 AM

View PostRickster0, on 09 February 2010 - 10:12 AM, said:

No i cant lol, i havent got much experience in this area of VB.

also i found the number codes for " and ' but im not sure how to use them


Ok thats fine. Here is what it was doing.

The below code looks for the first newline starting from the beginning of the document. Which works great if we only have a comment on the first line. If we want comment other lines then we need to look for the newline that comes AFTER our token "#" (or whatever).
RichTextBox1.Text.IndexOf(Chr(10))


So we simply tell IndexOf to start looking after our token by calling this:
RichTextBox1.Select(pos, RichTextBox1.Text.IndexOf(Chr(10), pos))


This code has the advantage of highlighting while you type instead of after you press enter.
ElseIf strCM = "#" Then
    With RichTextBox1
        Dim iLine As Integer = .GetLineFromCharIndex(pos)
        Dim sLine As String = .Lines(iLine)
        .Select(pos, sLine.Length)
        .SelectionColor = Color.Green
     End With
End If


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2