PDT/Dev2Dev/Semantic

From Eclipsepedia

< PDT
Revision as of 05:50, 26 August 2009 by Wcandillon.gmail.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Semantic Highlighting

Overview

Following the improvements done by the Web tools project (232752) on semantic highlighting, the PHP Source Editor can now set semantic rules for better coloring techniques that will improve its performance and functionality. For example, currently, all variables are highlighted in the same color. Although members / static / constants variables should be colored in separate colors just like the Java editor does.

The following screenshot demonstrate the power of the semantic coloring:

Pdt semantic editor.png

The latest version of the patch is available at 271430.

API

The PHP semantic highlighting API is used to define semantic rules for PHP coloring. It's also useful for plugin developpers who would like to extends PHP highlighting with specific semantic rules.

The first step is to use the org.eclipse.wst.sse.ui.semanticHighlighting extension point with the org.eclipse.php.core.phpsource target. By using the PHP source target, PHP UI plugin is automatically registering this contributor as a semantic highlighter.
<extension point="org.eclipse.wst.sse.ui.semanticHighlighting">
  <highlighting
    class="org.eclipse.php.internal.ui.editor.highlighting.ParameterVariableHighlighting"
    target="org.eclipse.php.core.phpsource">
  </highlighting>
</extension>
The given class must extend org.eclipse.php.internal.ui.editor.highlighter.AbstractSemanticHighlighting.
public class ParameterVariableHighlighting extends AbstractSemanticHighlighting {
 
  @Override
  public AbstractSemanticApply getSemanticApply() {
    return new ParameterVariableApply();
  }
 
  @Override
  public void initDefaultPreferences() {
    getStyle().setUnderlineByDefault(true)
              .setDefaultTextColor(new RGB(102, 0, 0));
  }
 
  public String getDisplayName() {
    return "Local variables";
  }
}
Since some rules might be conflicting between each other, you can define the priority rules. For instance, the InternalClassHighlighting rule has to override the ClassHighlighting rule. You can use the @HighlightingPriority annotation to define the application order of the semantic rules.
@HighlightingPriority(Priority.HIGH)
public class InternalClassHighlighting extends AbstractSemanticHighlighting {
The AbstractSemanticApply is simple visitor of PHP AST model. Once a node to be colored is matched, it can be added to the semantic coloring using the highlight method.
protected class InterfaceApply extends AbstractSemanticApply {
 
  @Override
  public boolean visit(ClassDeclaration classDecl) {
    for (Identifier type: classDecl.interfaces()) {
      highlight(type);
    }
    return true;
  }
 
  @Override
  public boolean visit(InterfaceDeclaration interfaceDecl) {
    highlight(interfaceDecl.getName());
    for (Identifier type: classDecl.interfaces()) {
      highlight(type);
    }
    return true;
  }
}


The following table describes the semantic rules that have been implemented so far and their default style.


Rule
Default Style
Enabled by default
Constants
Bold
True
Field

False
Function

False
Internal Function
Blue
True
Global Variable
Blue
True
Local variable

False
Method

False
Static Field
Italic
True
Static Method

False
Classes
Blue
True
Internal Classes
Blue
True
Internal Interfaces
Blue
True
Interfaces

False
Parameter Variables
Underline
True
Super Globales
Light blue
True


UI

The rules are automatically added to the Syntax Coloring preference page:

Pdt semantic preferences.png

Performances

Work in progress...