Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "PDT/Dev2Dev/Semantic"

< PDT
(Component Requirements, Constraints, and Acceptance Criteria)
 
(23 intermediate revisions by 3 users not shown)
Line 1: Line 1:
== Semantic Highlighting ==
+
== Semantic Highlighting ==
  
=== Overview ===
+
=== Overview ===
Following the improvements done by the Web tools project ([https://bugs.eclipse.org/232752 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.
+
  
Each semantic rule should have a preference page which describes its rule color and functionality.
+
Following the improvements done by the Web tools project ([https://bugs.eclipse.org/232752 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.  
  
=== References ===
+
The following screenshot demonstrate the power of the semantic coloring:
jdt semantic highlighting
+
  
wtp semantic highlighting
+
[[Image:Pdt semantic editor.png]]
  
=== Component Requirements, Constraints, and Acceptance Criteria ===
+
The latest version of the patch is available at [https://bugs.eclipse.org/271430 271430].
  
i did some fundamental work and added some basic classes.
+
=== API  ===
  
so add a semantic highlighting can be done by the following steps:
+
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.  
1. add a SemanticHighlightingType type constant in class SemanticHighlightings. for example:
+
<pre>
+
public static final SemanticHighlightingType StaticFieldHighlightingType = new SemanticHighlightingType(STATIC_FIELD);
+
</pre>
+
2. in the method getSemanticHighlightingStrings() of SemanticHighlightings,add the following statement.
+
<pre>
+
fgSemanticHighlightingStrings.put(StaticFieldHighlightingType.getPreferenceKey(), StaticFieldHighlightingType);
+
</pre>
+
3. of course you have to extend the extension "org.eclipse.wst.sse.ui.semanticHighlighting" in your plugin.xml
+
<pre>
+
<extension point="org.eclipse.wst.sse.ui.semanticHighlighting">
+
<highlighting
+
        class="org.eclipse.php.internal.ui.editor.highlighting.StaticFieldHighlighting"
+
        target="org.eclipse.php.core.phpsource">
+
</highlighting>
+
</extension>
+
</pre>
+
4. the StaticFieldHighlighting class in plugin.xml, StaticFieldHighlighting is implemented as following:
+
<pre>
+
package org.eclipse.php.internal.ui.editor.highlighting;
+
  
import org.eclipse.php.internal.core.ast.nodes.*;
+
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. <source lang="xml">
import org.eclipse.php.internal.ui.editor.SemanticHighlightingType;
+
<extension point="org.eclipse.wst.sse.ui.semanticHighlighting">
import org.eclipse.php.internal.ui.editor.SemanticHighlightings;
+
  <highlighting
 +
    class="org.eclipse.php.internal.ui.editor.highlighting.ParameterVariableHighlighting"
 +
    target="org.eclipse.php.core.phpsource">
 +
  </highlighting>
 +
</extension>
 +
</source>
  
public class StaticFieldHighlighting extends AbstractSemanticHighlighting {
+
The given class must extend ''org.eclipse.php.internal.ui.editor.highlighter.AbstractSemanticHighlighting''. <source lang="java">
 +
public class ParameterVariableHighlighting extends AbstractSemanticHighlighting {
  
protected class StaticFieldApply extends AbstractSemanticApply{
+
  @Override
@Override
+
  public AbstractSemanticApply getSemanticApply() {
public boolean visit(Variable variable) {
+
    return new ParameterVariableApply();
// TODO Auto-generated method stub
+
  }
if(variable.getParent() instanceof SingleFieldDeclaration){
+
SingleFieldDeclaration singleFieldDeclaration = (SingleFieldDeclaration)variable.getParent();
+
if(singleFieldDeclaration.getParent() instanceof FieldsDeclaration){
+
FieldsDeclaration fieldsDeclaration = (FieldsDeclaration)
+
  
singleFieldDeclaration.getParent();
+
  @Override
if((fieldsDeclaration.getModifier() & Modifier.STATIC) != 0){
+
  public void initDefaultPreferences() {
addASTNode(variable);
+
    getStyle().setUnderlineByDefault(true)
}
+
              .setDefaultTextColor(new RGB(102, 0, 0));
}
+
  }
}else if(variable.getParent() instanceof StaticFieldAccess){
+
addASTNode(variable);
+
}else if(variable.getParent() instanceof ArrayAccess){
+
if(((ArrayAccess)variable.getParent()).getName() == variable){
+
addASTNode(variable);
+
}
+
}
+
return super.visit(variable);
+
}
+
}
+
  
@Override
+
  public String getDisplayName() {
public AbstractSemanticApply getSemanticApply() {
+
    return "Local variables";
return new StaticFieldApply();
+
  }
}
+
}
 +
</source>
 +
 
 +
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. <source lang="java">
 +
@HighlightingPriority(Priority.HIGH)
 +
public class InternalClassHighlighting extends AbstractSemanticHighlighting {
 +
</source>
 +
 
 +
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. <source lang="java">
 +
protected class InterfaceApply extends AbstractSemanticApply {
 +
 
 +
  @Override
 +
  public boolean visit(ClassDeclaration classDecl) {
 +
    for (Identifier type: classDecl.interfaces()) {
 +
      highlight(type);
 +
    }
 +
    return true;
 +
  }
  
@Override
+
  @Override
public SemanticHighlightingType getSemanticHighlightingType() {
+
  public boolean visit(InterfaceDeclaration interfaceDecl) {
// TODO Auto-generated method stub
+
    highlight(interfaceDecl.getName());
return SemanticHighlightings.StaticFieldHighlightingType;
+
    for (Identifier type: classDecl.interfaces()) {
}
+
      highlight(type);
 +
    }
 +
    return true;
 +
  }
 
}
 
}
 +
</source>
  
</pre>
+
<br> The following table describes the semantic rules that have been implemented so far and their default style.  
in StaticFieldHighlighting ,you have to implement two methods,getSemanticHighlightingType() and getSemanticApply().
+
getSemanticHighlightingType() returns what you just added in
+
SemanticHighlightings class.
+
getSemanticApply() return a subclass of AbstractSemanticApply .here is StaticFieldApply which you must implement yourself.and the stuff is done in StaticFieldApply ,override the corresponding visit methods to find out the ASTNode which will be highlighted,and call the method addASTNode(ASTNode node) defined in AbstractSemanticApply .
+
  
 +
<br>
  
so that is all.
+
{| cellspacing="1" cellpadding="1" border="1" style="width: 622px; height: 338px;"
 +
|-
 +
| '''Rule<br>'''
 +
| '''Default Style<br>'''
 +
| '''Enabled by default'''<br>
 +
|-
 +
| Constants<br>
 +
| Bold<br>
 +
| True<br>
 +
|-
 +
| Field<br>
 +
| <br>
 +
| False<br>
 +
|-
 +
| Function<br>
 +
| <br>
 +
| False<br>
 +
|-
 +
| Internal Function<br>
 +
| Blue<br>
 +
| True<br>
 +
|-
 +
| Global Variable<br>
 +
| Blue<br>
 +
| True<br>
 +
|-
 +
| Local variable<br>
 +
| <br>
 +
| False<br>
 +
|-
 +
| Method<br>
 +
| <br>
 +
| False<br>
 +
|-
 +
| Static Field<br>
 +
| Italic<br>
 +
| True<br>
 +
|-
 +
| Static Method<br>
 +
| <br>
 +
| False<br>
 +
|-
 +
| Classes<br>
 +
| Blue<br>
 +
| True<br>
 +
|-
 +
| Internal Classes<br>
 +
| Blue<br>
 +
| True<br>
 +
|-
 +
| Internal Interfaces<br>
 +
| Blue<br>
 +
| True<br>
 +
|-
 +
| Interfaces<br>
 +
| <br>
 +
| False<br>
 +
|-
 +
| Parameter Variables<br>
 +
| Underline<br>
 +
| True<br>
 +
|-
 +
| Super Globales<br>
 +
| Light blue<br>
 +
| True<br>
 +
|}
  
 +
<br>
  
i will talk about the fundamental work and the basic classes i added soon.
+
=== UI  ===
  
and i have finished staticField and staticMethodInvocation highlighting.
+
The rules are automatically added to the Syntax Coloring preference page:
  
=== Index Classes ===
+
[[Image:Pdt semantic preferences.png]]
  
 +
=== Performances  ===
  
=== API ===
+
Work in progress...

Latest revision as of 05:50, 26 August 2009

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...

Back to the top