Building a fully functional code editor based on the JFace Text framework: part 10th

Source: Internet
Author: User

Text formatting

Text formatting can quickly make the source code in accordance with the scheduled rules, JTF just defined the framework, to play to the extreme is JDT. Open the Eclipse Settings page and find General->java->code Style->formatter, and you'll find that you can edit the myriad formatting options here. JDT provides a powerful and dazzling code format, but the purpose of this article is to introduce the infrastructure under this powerful feature.

Iformattingstrategy

Iformattingstrategy (formatting policy) is where the formatting is really done, and it represents a format. Formatting policies are associated with text types, so you can create a formatting policy for each type of text, which is a basic practice of JTF, and can you recall what other features are bound to the text type? This interface has an extended interface Iformattingstrategyextension, which introduces the concept of the formatting context (formatting contexts), as described below.

IContentFormatter

IContentFormatter is the most basic interface, and it represents a formatter. Since the actual formatting work should be put into the iformattingstrategy, what IContentFormatter do? It can be said that it basically corresponds to a scheduler that determines what type of text to format and then invokes a different iformattingstrategy implementation. However, this is only recommended behavior, you can do anything in the implementation of IContentFormatter, but do not recommend deliberately beyond the constraints of this architecture. JTF has the default implementation of this interface: ContentFormatter, generally we can use it directly.

Iformattingcontext

Iformattingcontext provides additional information for formatting, which holds information in a key-value pair, so you can save any formatting you need to know. But this is a relatively new interface, starting with Eclipse 3.0, and you need to implement Iformattingstrategyextension. Remember, don't forget to check to see if each interface has an extension interface, and you may have a lot of new discoveries, because JTF are everywhere, or Eclipse is full of extension interfaces.

Implementing text formatting

Implement Iformattingstrategy

First I use Exprformattingstrategy to implement the Iformattingstrategy interface, I did not implement Iformattingstrategyextension, because the language in the example is too simple, I don't need a lot of contextual information to do the formatting work. Another reason: text formatting algorithms are different, and have little to do with the architecture of the JTF, and there is no need for a detailed demo. My formatting is simple: there is a space between each symbol (except for semicolons), and only one statement is allowed per line.

Configuration

Modify the Exprconfiguration and overwrite the Getcontentformatter method. I use the default implementation and register the Exprformattingstrategy with the Setformattingstrategy method. Because of the simplicity of the principle, I did not achieve icontentformatter, if your software requirements must be implemented IContentFormatter, please do not hesitate.

Shortcut key Processing

As with content hints, quick help, you need a shortcut key to trigger the formatting of the text. The ordinary text editor does not have the formatting function, if I need to use the formatted command ID, need to rely on the Org.eclipse.jdt.ui plug-in, I do not want to do so. Of course, this doesn't matter to me, but if you're developing a real product, I don't think you want to include the entire JDT in order to use a constant, so I've defined a format command by extension and bound to ctrl+shift+f. Then, follow the quick help approach by adding a handler to the Exprviewer Createhandlers method.

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.