Syntax Highlight
Syntax Highlight (syntax highlighting) refers to the different content of the text in different colors, fonts and other rendering, in this way, users can quickly find a certain content, in a short time to the full text of the structure of a general understanding. This paper explores how to implement syntax highlighting in JTF.
Premise
Syntax highlighting is a feature that uses different styles to render different content in a piece of text, as shown in the following illustration:
Figure 1. Syntax highlighting for the Java editor
The different parts of the Java source code in Figure 1 are rendered in different colors by the Java editor. For example, the keyword is red and bold, the variable is blue, the annotation is green, and so on. Different colors can let the user quickly find the content, can be said to be a very convenient accessibility.
In part of this series, I mentioned the ANTLR and the use of ANTLR to produce lexical and parser. Syntax highlighting is a very dependent function of the lexical analyzer, because the lexical parser will parse the character stream into symbolic streams, symbols containing various attributes, such as type, starting position and end position, and so on, syntax highlighting precisely needs this information to render text. We've got some basic classes that can help us manage a list of symbols, like tokenlist, which can get the corresponding symbols based on character offsets. This provides a good basis for implementing syntax highlighting.
Itokenscanner
JTF is to understand symbolic information through a Itokenscanner interface, let's take a look at the definition of this interface:
Listing 1. Itokenscanner interface
public interface ITokenScanner {
void setRange(IDocument document, int offset, int length);
IToken nextToken();
int getTokenOffset();
int getTokenLength();
}