Iterate through Tokens on current line behind cursor

SyntaxEditor for Windows Forms Forum

Posted 10 years ago by Ernesto Obregon
Avatar
Actipro

Is there an easy way to iterate backwards from the current cursor position on the current line to see if there is any other tokens on the line besides the last immediate token before the cursor ?

Example: I want to find what the next valid token is besides the WhiteSpaceToken between the / and the word 'text' below if 'text' was a KeyWordToken or if there are no tokens then I should read a LineTerminatorToken as this is what is returned by default at the beginning of a line.

Current line of text       /

Comments (5)

Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Ernesto,

Yes, we have a TextStream class that lets you iterate through text via character, token, and other options. To get a TextStream, do this:
TextStream stream = editor.Document.GetTextStream(editor.Caret.Offset);
Then use its methods to go through the text. You can have it tell you which token you are on and skip backwards by token. Just keep an eye out for your line terminator token or document start as you iterate back.

Hope that helps!


Actipro Software Support

Posted 10 years ago by Ernesto Obregon
Avatar
Is this what you are saying, that I can loop backwards in the text stream by iterating one space backwards at a time and then compare if the current token is a "LineTerminatorToken" then this should indicate I am now at the beginning of the current line? Do you see any problems with the below code sample?.

Dim stream As TextStream = editor.Document.GetTextStream(editor.Caret.Offset)
Dim index As Integer = 1
 If stream.GoToPreviousNonWhitespaceToken Then
   Do Until stream.Token.Key = "LineTerminatorToken"
      stream = stream.Document.GetTextStream(editor.Caret.Offset - index)
      index += 1
   Loop
 End If
Posted 10 years ago by Ernesto Obregon
Avatar
This is proably more appropriate, yea ?

Dim stream As TextStream = editor.Document.GetTextStream(editor.Caret.Offset)
Dim index As Integer = 1
While Not stream.IsAtDocumentLineStart
 stream = stream.Document.GetTextStream(editor.Caret.Offset - index)
 index += 1
End While
Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Ernesto,

Yes your last post idea would probably be the best idea where you loop while not stream.IsAtDocumentLineStart. However instead of creating a new TextStream each loop iteration (not a good idea), instead call Stream.ReadTokenReverse(). Then you can examine the token that is read.


Actipro Software Support

Posted 10 years ago by Ernesto Obregon
Avatar
Cool, thanks. I did not realize that the ReadTokenReverse() method would iterate in reverse on the line each time the method was called. I thought it only read one token in reverse from the current offset and I had to change the offset manaully. This method works nicely.
The latest build of this product (v2020.1 build 0400) was released 24 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.