Building a fully functional code editor based on the JFace Text framework: Part 2nd

Source: Internet
Author: User

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();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.