Building a fully functional code editor based on the JFace Text framework: Part 8th

Source: Internet
Author: User
Tags range

Hyperlink

The Hyperlink (hyperlink) is used for fast code positioning in the Java editor, and when you hold down the CTRL key and point the mouse to a function name, the function name appears as a hyperlink, and the code jumps to the declaration of the function when you click it. This feature makes it easy for Eclipse to browse the code, and this time I'll explain how to add a hyperlink feature to my editor.

Positioning of hyperlinks

The editor will not know which area should be displayed as a hyperlink, which is implemented through the Ihyperlinkdetector interface. This involves semantic content, because you have to know what's underneath the mouse. This support needs to be implemented on the parser level.

Rendering of hyperlinks

How does JTF display hyperlinks? You may think of annotations, which is not the default implementation. Here to introduce another interface: Ihyperlinkpresenter. JTF The default is implemented with Styledtext Stylerange, which is to put the hyperlink text in blue and underlined. But because of this interface, you can make the hyperlinks look anything.

To implement a hyperlink

The hyperlink feature to implement in this article is: Click on a variable name and the editor will select the statement that declares the variable.

Bottom support

The bottom layer needs to support two functions: to judge a position as a variable and to get the statement scope of the variable declaration. Because my example is simple, it's very simple to judge whether a variable is the same as long as the symbol type is an ID type. The statement scope that gets the variable declaration needs to check the syntax tree, because the subtree of the variable declaration takes the equal sign as the root node, so it's OK to find the corresponding root node. The left and right nodes of the subtree are then obtained from the equals sign, thus calculating the range of characters for the entire subtree. This code has been added to the Treehelper, see the Getvariabledeclaration and Gettreerange methods.

Implement Ihyperlink

I implemented a variablehyperlink to encapsulate the hyperlink information, the most important way is open (), because it will be called after you click the hyperlink:

Listing 1. The Open method of Variablehyperlink

public void open() {
  // get doc
  IDocument doc = viewer.getDocument();

  // get tree
  Tree tree = TreeManager.getTree(doc);

  // get variable declaration range
  Point range = TreeHelper.getVariableDeclaration(tree, variable);

  // select text
  if(range != null) {
  viewer.setSelectedRange(range.x, range.y);
  viewer.revealRange(range.x, range.y);
  }
}

I used the method in the Treehelper just mentioned to get the scope of the statement, the rest is more straightforward, select the scope and make sure it is visible in the editor.

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.