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

PDT/Dev2Dev/Semantic

< PDT
Revision as of 18:38, 25 August 2009 by Wcandillon.gmail.com (Talk | contribs) (UI)

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 latest version of the patch is available at [1].

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

UI

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

Pdt semantic preferences.png

The following screenshot demonstrate the power of the semantic coloring:

Pdt semantic editor.png

References

jdt semantic highlighting

wtp semantic highlighting

Component Requirements, Constraints, and Acceptance Criteria

i did some fundamental work and added some basic classes.

so add a semantic highlighting can be done by the following steps:

1. add a SemanticHighlightingType type constant in class SemanticHighlightings. for example:

public static final SemanticHighlightingType StaticFieldHighlightingType = new SemanticHighlightingType(STATIC_FIELD);

SemanticHighlightingType have another constructor

public SemanticHighlightingType(String preferenceKey,boolean boldByDefault,
			boolean italicByDefault,boolean strikethroughByDefault,boolean underlineByDefault,RGB defaultTextColor)

so default values can be set using this constructor . if useing the first constructor.all the boolean value are setting to false.and the defaultTextColor depands on the findRGB mothed:

	private static RGB findRGB(String key, RGB defaultRGB) {
		if (!PlatformUI.isWorkbenchRunning())
			return defaultRGB;

		ColorRegistry registry = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
		RGB rgb = registry.getRGB(key);
		if (rgb != null)
			return rgb;
		return defaultRGB;
	}

	private String getThemeColorKey() {
		return PHPUiPlugin.ID + "." + getPreferenceKey() + "Highlighting"; //$NON-NLS-1$//$NON-NLS-2$
	}

	protected RGB getDefaultDefaultTextColor() {
		return new RGB(0, 0, 0);
	}

of course,you can subclass SemanticHighlightingType class to get more flexible default values.

2. in the method getSemanticHighlightingMap() of SemanticHighlightings,add the following statement.

semanticHighlightingMap.put(StaticFieldHighlightingType.getPreferenceKey(), StaticFieldHighlightingType);

3. of course you have to extend the extension "org.eclipse.wst.sse.ui.semanticHighlighting" in your plugin.xml

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

4. the StaticFieldHighlighting class in plugin.xml, StaticFieldHighlighting is implemented as following:

package org.eclipse.php.internal.ui.editor.highlighting;

import org.eclipse.php.internal.core.ast.nodes.*;
import org.eclipse.php.internal.ui.editor.SemanticHighlightingType;
import org.eclipse.php.internal.ui.editor.SemanticHighlightings;

public class StaticFieldHighlighting extends AbstractSemanticHighlighting {

	protected class StaticFieldApply extends AbstractSemanticApply{
		@Override
		public boolean visit(Variable variable) {
			// 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();
					if((fieldsDeclaration.getModifier() & Modifier.STATIC) != 0){
						addASTNode(variable);
					}
				}
			}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 AbstractSemanticApply getSemanticApply() {
		return new StaticFieldApply();
	}

	@Override
	public SemanticHighlightingType getSemanticHighlightingType() {
		// TODO Auto-generated method stub
		return SemanticHighlightings.StaticFieldHighlightingType;
	}
}

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 .


so that is all.


i will talk about the fundamental work and the basic classes i added soon.

and i have finished staticField and staticMethodInvocation highlighting.

the following semantic elements highlighting will be finished soon!

ConstantHighlighting

FieldHighlighting

MethodDeclarationHighlighting

AbstractMethodInvocationHighlighting

InheritedMethodInvocationHighlighting

ParameterVariableHighlighting

ClassHighlighting

InterfaceHighlighting

Index Classes

API

Back to the top