Syntax highlighting

help please?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 9104 Views - Last Post: 18 December 2017 - 02:01 AM Rate Topic: -----

#16 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:30 AM

Ah i see, thanks. this will be great for a refrence. im trying to learn how to do this on my own, but i never knew about that. and i've never once used chr(int) ever..
Was This Post Helpful? 0
  • +
  • -

#17 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:39 AM

ok, so i changed the code for the # to display the " as a coment line.

but now i want to make it like the =begin =end function

but on one line only.

how can i edit this

  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


to do just that? And also can you explain how that code works ^^;
Was This Post Helpful? 0
  • +
  • -

#18 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 - 02:30 PM

Ok so now your comment array is now going to look like this:
arrComments = New String() {"=begin", "=end", "#", Chr(34).ToString}


We cant use any of the past methods because they were using either a block of text or a single line. Since what we need to do is highlight part of a line we need to check for token starts and ends within the string and account for multiple starts and stops on a single line.

ElseIf strCM = "#" Then
   RichTextBox1.Select(pos, RichTextBox1.Text.IndexOf(Chr(10), pos))
   RichTextBox1.SelectionColor = Color.Green
ElseIf strCM(0) = Chr(34) Then
   With RichTextBox1
       Dim iStartPos As Integer = .Text.IndexOf(Chr(34))
       Dim iEndPos As Integer = .Text.IndexOf(Chr(34), iStartPos + 1) 'find closing token'
       Do Until iEndPos = -1 'until no closing token found'
           .Select(iStartPos, iEndPos - iStartPos + 1)
           .SelectionColor = Color.Green
           iStartPos = .Text.IndexOf(Chr(34), iEndPos + 1) 'find next start'
           If iStartPos = -1 Then 'no more tokens to parse'
               Exit Do
           End If
           iEndPos = .Text.IndexOf(Chr(34), iStartPos + 1) 'find next end'
       Loop
       If iStartPos > -1 And iEndPos = -1 Then 'this condition will be true if there is not closing token'
           .Select(iStartPos, .Text.Length - iStartPos)
           .SelectionColor = Color.Green
       End If
    End With
    Exit Do 'the do loop checks the entire .Text so no need to continue'
End If


Was This Post Helpful? 1
  • +
  • -

#19 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 10 February 2010 - 04:44 AM

sweet ok, now i got a small issue XD

After using the "hello"

then =begin hello = end

anything after end is green :(
Was This Post Helpful? 0
  • +
  • -

#20 T3hC13h  Icon User is offline

  • D.I.C Regular

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

Re: Syntax highlighting

Posted 10 February 2010 - 06:54 AM

View PostRickster0, on 10 February 2010 - 03:44 AM, said:

sweet ok, now i got a small issue XD

After using the "hello"

then =begin hello = end

anything after end is green :(



Wow my substring skills are taking a beating on this one. I found the issue and its simple, heres a hint. Its not highlighting EVERYTHING after "=end" it will stop after a number of characters (highlighting too many characters).
Was This Post Helpful? 0
  • +
  • -

#21 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 10 February 2010 - 07:10 AM

you've lost me with that one :x
Was This Post Helpful? 0
  • +
  • -

#22 T3hC13h  Icon User is offline

  • D.I.C Regular

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

Re: Syntax highlighting

Posted 10 February 2010 - 08:55 AM

The length value we are passing to Select is too long if our begin block doesn't start at the beginning of the text.
Was This Post Helpful? 0
  • +
  • -

#23 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 11 February 2010 - 02:36 AM

Ah, ok ill have a look :)
Was This Post Helpful? 0
  • +
  • -

#24 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 11 February 2010 - 06:12 AM

unfortunatly, i cant seem to get it to work, i dont even know what im supposed to do. i dont really understand this part much :/
Was This Post Helpful? 0
  • +
  • -

#25 T3hC13h  Icon User is offline

  • D.I.C Regular

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

Re: Syntax highlighting

Posted 11 February 2010 - 08:23 AM

Ok heres the code we are working with:

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


Once we've reached this line RichTextBox1.Select(iSPos, pos + strCM.Length) we have two integers we are passing to .Select.
iSPos is the number of characters the =BEGIN is from the start of the script text.
pos is the number of characters the =END is from the start of the script text.
.Select takes two parameters, the first is the start of the selection. The second is the number of characters to select.
In the above code instead of telling Select to highlight the difference between iSPos and pos I'm telling Select to include the sum of iSpos and pos.

To correct this we simply subtract the value of the starting tag from the value of the ending tag.

RichTextBox1.Select(iSPos, (pos -iSPos) + strCM.Length)

This post has been edited by T3hC13h: 11 February 2010 - 08:23 AM

Was This Post Helpful? 0
  • +
  • -

#26 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 11 February 2010 - 11:09 AM

Ah, i see. i got confused with that :S

Thanks for clearing that one up :D

EDIT:

Thank you it's working perfectly.

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

Was This Post Helpful? 0
  • +
  • -

#27 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 11 February 2010 - 11:39 AM

ok, i have another question/problem.

When you want to create a variable, say Gort, that variable includes a keyword, wich highlights or as blue, how can i stop that from happening unless the keywords are on their own?

example:

Gort
going

, also another question at the same time, to create a variable i would like to use these identifiers, "$" "@" "@@".

so for example.

@hello
@@good
$forever

so this is just a word that follows the specific symbol highlighting.

So far here's the code.

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


        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", "#", Chr(34).ToString, Chr(39).ToString}

        If RichTextBox1.Text.Length > 0 Then

            Dim selectStart As Integer = RichTextBox1.Selectionstart

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

            RichTextBox1.SelectionColor = Color.Black

            RichTextBox1.DeselectAll()


            '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)

                    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 - iSPos) + strCM.Length)
                            RichTextBox1.SelectionColor = Color.Green
                        End If

                    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
                        Exit Do 'the do loop checks the entire .Text so no need to continue'

                    ElseIf strCM(0) = Chr(34) Then
                        With RichTextBox1
                            Dim iStartPos As Integer = .Text.IndexOf(Chr(34))
                            Dim iEndPos As Integer = .Text.IndexOf(Chr(34), iStartPos + 1) 'find closing token'
                            Do Until iEndPos = -1 'until no closing token found'
                                .Select(iStartPos, iEndPos - iStartPos + 1)
                                .SelectionColor = Color.DarkRed
                                iStartPos = .Text.IndexOf(Chr(34), iEndPos + 1) 'find next start'
                                If iStartPos = -1 Then 'no more tokens to parse'
                                    Exit Do
                                End If
                                iEndPos = .Text.IndexOf(Chr(34), iStartPos + 1) 'find next end'
                            Loop
                            If iStartPos > -1 And iEndPos = -1 Then 'this condition will be true if there is not closing token'
                                .Select(iStartPos, .Text.Length - iStartPos)
                                .SelectionColor = Color.DarkRed
                            End If
                        End With
                        Exit Do 'the do loop checks the entire .Text so no need to continue'

                    ElseIf strCM(0) = Chr(39) Then
                        With RichTextBox1
                            Dim iStartPos As Integer = .Text.IndexOf(Chr(39))
                            Dim iEndPos As Integer = .Text.IndexOf(Chr(39), iStartPos + 1) 'find closing token'
                            Do Until iEndPos = -1 'until no closing token found'
                                .Select(iStartPos, iEndPos - iStartPos + 1)
                                .SelectionColor = Color.Purple
                                iStartPos = .Text.IndexOf(Chr(39), iEndPos + 1) 'find next start'
                                If iStartPos = -1 Then 'no more tokens to parse'
                                    Exit Do
                                End If
                                iEndPos = .Text.IndexOf(Chr(39), iStartPos + 1) 'find next end'
                            Loop
                            If iStartPos > -1 And iEndPos = -1 Then 'this condition will be true if there is not closing token'
                                .Select(iStartPos, .Text.Length - iStartPos)
                                .SelectionColor = Color.Purple
                            End If
                        End With
                        Exit Do 'the do loop checks the entire .Text so no need to continue'

                    End If

                    pos += 1
                Loop
            Next

            RichTextBox1.Selectionstart = selectStart

        End If
    End Sub
End Class



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

Was This Post Helpful? 0
  • +
  • -

#28 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Syntax highlighting

Posted 11 February 2010 - 02:00 PM

You might want to put a Space after each of the arrKeyWords ("alias ", "and ", "in " this way you won't end up with text that looks like this:

Dim something as String
Was This Post Helpful? 0
  • +
  • -

#29 T3hC13h  Icon User is offline

  • D.I.C Regular

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

Re: Syntax highlighting

Posted 11 February 2010 - 03:14 PM

View PostCharlieMay, on 11 February 2010 - 01:00 PM, said:

You might want to put a Space after each of the arrKeyWords ("alias ", "and ", "in " this way you won't end up with text that looks like this:

Dim something as String


Damn thats simple and it almost works. The only issue is that you'll have to add a space on lines that end with a keyword.

Heres my not so simple solution.

            '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)
                    If (pos + strKW.Length = RichTextBox1.TextLength) And (pos - 1) <= 0 Then 'if keyword is only text'
                        RichTextBox1.Select(pos, strKW.Length)
                    ElseIf (pos - 1) <= 0 Then 'if keyword is at start of text'
                        RichTextBox1.Select(pos, strKW.Length)
                    ElseIf RichTextBox1.Text(pos - 1) = Chr(32) Then 'if space precedes keyword'
                        If pos + strKW.Length = RichTextBox1.TextLength Then 'if keyword is at end of text'
                            RichTextBox1.Select(pos, strKW.Length)
                        ElseIf pos + strKW.Length < RichTextBox1.TextLength Then 'if keyword is within text'
                            If RichTextBox1.Text(pos + strKW.Length) = Chr(32) Or RichTextBox1.Text(pos + strKW.Length) = Chr(10) Then 'followed by space or newline'
                                RichTextBox1.Select(pos, strKW.Length)
                            End If
                        End If
                    End If
                    RichTextBox1.SelectionColor = Color.Blue
                    pos += 1
                Loop
            Next




Heres what I came up with for variable/symbol coloring.

            'Variable Colouring'
            For Each strVar In arrVarTeal
                Dim pos As Integer = 0
                Do While RichTextBox1.Text.ToUpper.IndexOf(strVar.ToUpper, pos) >= 0
                    pos = RichTextBox1.Text.ToUpper.IndexOf(strVar.ToUpper, pos)
                    Dim iVLength As Integer = strVar.Length 'store the length since we may need to find the end of the name'
                    If pos + iVLength = RichTextBox1.TextLength Then 'colors the symbol character as we type it'
                        RichTextBox1.Select(pos, iVLength)
                    Else
                        Do
                            iVLength += 1
                            If iVLength + pos = RichTextBox1.TextLength Then
                                RichTextBox1.Select(pos, iVLength)
                                Exit Do
                            ElseIf RichTextBox1.Text(pos + iVLength) = Chr(32) Then 'found space'
                                RichTextBox1.Select(pos, iVLength)
                                Exit Do
                            ElseIf RichTextBox1.Text(pos + iVLength) = Chr(10) Then 'end of line'
                                RichTextBox1.Select(pos, iVLength)
                                Exit Do
                            End If
                        Loop
                        RichTextBox1.Select(pos, iVLength)
                    End If
                    RichTextBox1.SelectionColor = Color.LightBlue
                    pos += 1
                Loop
            Next


Was This Post Helpful? 1
  • +
  • -

#30 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 12 February 2010 - 03:28 AM

Thank you, it seems to work alright at the moment, i will let you know if i find any problems. Thank you all for your help :)
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3