Skip to content

Commit

Permalink
Enhance the syntax highlighter of the javascript source viewer (eclip…
Browse files Browse the repository at this point in the history
  • Loading branch information
speckyspooky authored Feb 28, 2024
1 parent 1366eb2 commit bd0178b
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4967,6 +4967,8 @@ ExpressionSyntaxColoringPage.Element.Comment=Comment
ExpressionSyntaxColoringPage.Element.Default=Default
ExpressionSyntaxColoringPage.Element.Keyword=Keyword
ExpressionSyntaxColoringPage.Element.String=String
ExpressionSyntaxColoringPage.Element.Method=JavaScript Common Methods & Functions
ExpressionSyntaxColoringPage.Element.Object=JavaScript Common Objects
ExpressionSyntaxColoringPage.Label.Background=Backgr&ound:
ExpressionSyntaxColoringPage.Label.Foreground=&Foreground:
ExpressionSyntaxColoringPage.Label.SampleText=&Sample text:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*******************************************************************************
* Copyright (c) 2012, 2014 Actuate Corporation.
*
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
*
* SPDX-License-Identifier: EPL-2.0
*
*
*
* Contributors:
* Actuate Corporation - initial API and implementation
Expand Down Expand Up @@ -93,13 +93,19 @@

import com.ibm.icu.text.Collator;

/**
* Class to represents the syntax coloring page
*
* @since 3.3
*
*/
public final class ExpressionSyntaxColoringPage extends AbstractSyntaxColoringPage implements IWorkbenchPreferencePage {

private Button fBold;
private Label fForegroundLabel;
private Label fBackgroundLabel;
private Button fClearStyle;
private Map fContextToStyleMap;
private Map<String, String> fContextToStyleMap;
private Color fDefaultForeground = null;
private Color fDefaultBackground = null;
private IDocument fDocument;
Expand All @@ -108,9 +114,9 @@ public final class ExpressionSyntaxColoringPage extends AbstractSyntaxColoringPa
private Button fItalic;
private OverlayPreferenceStore fOverlayStore;
private Button fStrike;
private Collection fStylePreferenceKeys;
private Collection<String> fStylePreferenceKeys;
private StructuredViewer fStylesViewer = null;
private Map fStyleToDescriptionMap;
private Map<String, String> fStyleToDescriptionMap;
private StyledText fText;
private Button fUnderline;

Expand Down Expand Up @@ -176,7 +182,7 @@ void applyStyles() {
if (regions.length > 0) {
for (int i = 0; i < regions.length; i++) {
ITypedRegion region = regions[i];
String namedStyle = (String) fContextToStyleMap.get(region.getType());
String namedStyle = fContextToStyleMap.get(region.getType());
if (namedStyle == null) {
continue;
}
Expand Down Expand Up @@ -264,7 +270,7 @@ public void widgetSelected(SelectionEvent e) {
fStylesViewer = createStylesViewer(styleEditor);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
gridData.horizontalIndent = 0;
Iterator iterator = fStyleToDescriptionMap.values().iterator();
Iterator<String> iterator = fStyleToDescriptionMap.values().iterator();
while (iterator.hasNext()) {
gridData.widthHint = Math.max(gridData.widthHint,
convertWidthInCharsToPixels(iterator.next().toString().length()));
Expand Down Expand Up @@ -615,11 +621,11 @@ protected ISourceViewer getSourcePreviewViewer() {
* Set up all the style preference keys in the overlay store
*/
private OverlayKey[] createOverlayStoreKeys() {
List overlayKeys = new ArrayList();
List<OverlayKey> overlayKeys = new ArrayList<OverlayKey>();

Iterator i = getStylePreferenceKeys().iterator();
Iterator<String> i = getStylePreferenceKeys().iterator();
while (i.hasNext()) {
overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, (String) i.next()));
overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, i.next()));
}

OverlayPreferenceStore.OverlayKey[] keys = new OverlayPreferenceStore.OverlayKey[overlayKeys.size()];
Expand Down Expand Up @@ -744,7 +750,7 @@ private String getNamedStyleAtOffset(int offset) {
try {
String regionContext = fDocument.getPartition(offset).getType();

String namedStyle = (String) fContextToStyleMap.get(regionContext);
String namedStyle = fContextToStyleMap.get(regionContext);
if (namedStyle != null) {
return namedStyle;
}
Expand All @@ -757,13 +763,15 @@ private OverlayPreferenceStore getOverlayStore() {
return fOverlayStore;
}

private Collection getStylePreferenceKeys() {
private Collection<String> getStylePreferenceKeys() {
if (fStylePreferenceKeys == null) {
List styles = new ArrayList();
List<String> styles = new ArrayList<String>();
styles.add(ReportPlugin.EXPRESSION_CONTENT_COLOR_PREFERENCE);
styles.add(ReportPlugin.EXPRESSION_COMMENT_COLOR_PREFERENCE);
styles.add(ReportPlugin.EXPRESSION_KEYWORD_COLOR_PREFERENCE);
styles.add(ReportPlugin.EXPRESSION_STRING_COLOR_PREFERENCE);
styles.add(ReportPlugin.EXPRESSION_METHOD_COLOR_PREFERENCE);
styles.add(ReportPlugin.EXPRESSION_OBJECT_COLOR_PREFERENCE);
fStylePreferenceKeys = styles;
}
return fStylePreferenceKeys;
Expand Down Expand Up @@ -853,8 +861,8 @@ public void keyTraversed(TraverseEvent e) {
public void init(IWorkbench workbench) {
setDescription(Messages.getString("ExpressionSyntaxColoringPage.Decscription")); //$NON-NLS-1$

fStyleToDescriptionMap = new HashMap();
fContextToStyleMap = new HashMap();
fStyleToDescriptionMap = new HashMap<String, String>();
fContextToStyleMap = new HashMap<String, String>();

initStyleToDescriptionMap();
initRegionContextToStyleMap();
Expand All @@ -870,7 +878,8 @@ private void initRegionContextToStyleMap() {
fContextToStyleMap.put(JSPartitionScanner.JS_COMMENT, ReportPlugin.EXPRESSION_COMMENT_COLOR_PREFERENCE);
fContextToStyleMap.put(JSPartitionScanner.JS_KEYWORD, ReportPlugin.EXPRESSION_KEYWORD_COLOR_PREFERENCE);
fContextToStyleMap.put(JSPartitionScanner.JS_STRING, ReportPlugin.EXPRESSION_STRING_COLOR_PREFERENCE);

fContextToStyleMap.put(JSPartitionScanner.JS_METHOD, ReportPlugin.EXPRESSION_METHOD_COLOR_PREFERENCE);
fContextToStyleMap.put(JSPartitionScanner.JS_OBJECT, ReportPlugin.EXPRESSION_OBJECT_COLOR_PREFERENCE);
}

private void initStyleToDescriptionMap() {
Expand All @@ -882,6 +891,10 @@ private void initStyleToDescriptionMap() {
Messages.getString("ExpressionSyntaxColoringPage.Element.Keyword")); //$NON-NLS-1$
fStyleToDescriptionMap.put(ReportPlugin.EXPRESSION_STRING_COLOR_PREFERENCE,
Messages.getString("ExpressionSyntaxColoringPage.Element.String")); //$NON-NLS-1$
fStyleToDescriptionMap.put(ReportPlugin.EXPRESSION_METHOD_COLOR_PREFERENCE,
Messages.getString("ExpressionSyntaxColoringPage.Element.Method")); //$NON-NLS-1$
fStyleToDescriptionMap.put(ReportPlugin.EXPRESSION_OBJECT_COLOR_PREFERENCE,
Messages.getString("ExpressionSyntaxColoringPage.Element.Object")); //$NON-NLS-1$
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public interface ReportColorConstants {

Color TableGuideTextColor = ColorManager.getColor("org.eclipse.birt.report.designer.ui.TableGuideTextColor", //$NON-NLS-1$
new RGB(147, 137, 145));

Color TableGuideFillColor = ColorManager.getColor("org.eclipse.birt.report.designer.ui.GuideFillColor", //$NON-NLS-1$
new RGB(239, 239, 247));

Expand All @@ -73,13 +74,22 @@ public interface ReportColorConstants {

Color JSCOMMENTCOLOR = ColorManager.getColor("org.eclipse.birt.report.designer.ui.JSCOMMENTCOLOR", //$NON-NLS-1$
new RGB(63, 127, 95));

Color JSSTRINGCOLOR = ColorManager.getColor("org.eclipse.birt.report.designer.ui.JSSTRINGCOLOR", //$NON-NLS-1$
new RGB(42, 0, 255));

Color JSKEYWORDCOLOR = ColorManager.getColor("org.eclipse.birt.report.designer.ui.JSKEYWORDCOLOR", //$NON-NLS-1$
new RGB(127, 0, 85));

Color JSLINENUMBERCOLOR = ColorManager.getColor("org.eclipse.birt.report.designer.ui.JSLINENUMBERCOLOR", //$NON-NLS-1$
new RGB(127, 127, 127));

Color JSMETHODCOLOR = ColorManager.getColor("org.eclipse.birt.report.designer.ui.JSMETHODCOLOR", //$NON-NLS-1$
new RGB(81, 97, 122));

Color JSOBJECTCOLOR = ColorManager.getColor("org.eclipse.birt.report.designer.ui.JSOBJECTCOLOR", //$NON-NLS-1$
new RGB(181, 87, 50));

Color[] ShadowColors = {

ColorManager.getColor(92, 114, 143), ColorManager.getColor(97, 118, 147),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class JSDocumentProvider extends StorageDocumentProvider {
* Array of token types
*/
private static String[] colorTokens = { JSPartitionScanner.JS_COMMENT, JSPartitionScanner.JS_STRING,
JSPartitionScanner.JS_KEYWORD };
JSPartitionScanner.JS_KEYWORD, JSPartitionScanner.JS_METHOD, JSPartitionScanner.JS_OBJECT };

public JSDocumentProvider() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@
package org.eclipse.birt.report.designer.internal.ui.script;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.script.functionservice.IScriptFunction;
import org.eclipse.birt.core.script.functionservice.IScriptFunctionCategory;
import org.eclipse.birt.core.script.functionservice.impl.FunctionProvider;
import org.eclipse.birt.report.designer.internal.ui.util.ExceptionHandler;
import org.eclipse.birt.report.designer.util.DEUtil;
import org.eclipse.birt.report.model.api.metadata.IClassInfo;
import org.eclipse.birt.report.model.api.metadata.IMethodInfo;
import org.eclipse.jface.text.rules.EndOfLineRule;
import org.eclipse.jface.text.rules.IPredicateRule;
import org.eclipse.jface.text.rules.IToken;
Expand All @@ -33,15 +42,31 @@
*/
public class JSPartitionScanner extends RuleBasedPartitionScanner {

/** property: javascript default key */
public final static String JS_DEFAULT = "__js_default"; //$NON-NLS-1$
/** property: javascript comment key */
public final static String JS_COMMENT = "__js_comment"; //$NON-NLS-1$
/** property: javascript keyword key */
public final static String JS_KEYWORD = "__js_keyword"; //$NON-NLS-1$
/** property: javascript string key */
public final static String JS_STRING = "__js_string"; //$NON-NLS-1$
/** property: javascript method key */
public final static String JS_METHOD = "__js_method"; //$NON-NLS-1$
/** property: javascript object key */
public final static String JS_OBJECT = "__js_object"; //$NON-NLS-1$

/** token: javascript string */
public final static IToken TOKEN_STRING = new Token(JS_STRING);
/** token: javascript comment */
public final static IToken TOKEN_COMMENT = new Token(JS_COMMENT);
/** token: javascript default */
public final static IToken TOKEN_DEFAULT = new Token(JS_DEFAULT);
/** token: javascript keyword */
public final static IToken TOKEN_KEYWORD = new Token(JS_KEYWORD);
/** token: javascript method */
public final static IToken TOKEN_METHOD = new Token(JS_METHOD);
/** token: javascript object */
public final static IToken TOKEN_OBJECT = new Token(JS_OBJECT);

/**
* Array of keyword token strings.
Expand All @@ -67,7 +92,9 @@ public class JSPartitionScanner extends RuleBasedPartitionScanner {
"var", //$NON-NLS-1$
"void", //$NON-NLS-1$
"while", //$NON-NLS-1$
"with" //$NON-NLS-1$
"with", //$NON-NLS-1$
"const", //$NON-NLS-1$
"let", //$NON-NLS-1$
};

/**
Expand All @@ -76,11 +103,23 @@ public class JSPartitionScanner extends RuleBasedPartitionScanner {
private static String[] constantTokens = { "false", "null", "true" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
};

/**
* Array of global object strings
*/
private ArrayList<String> globalObjectTokens = new ArrayList<String>();

/**
* Array of method names
*/
private ArrayList<String> keywordMethods = new ArrayList<String>();

/**
* Creates a new JSPartitionScanner object.
*/
public JSPartitionScanner() {
List rules = new ArrayList();
List<IPredicateRule> rules = new ArrayList<IPredicateRule>();

fetchJSCommonObjectsMethods();

rules.add(new MultiLineRule("/*", "*/", TOKEN_COMMENT)); //$NON-NLS-1$ //$NON-NLS-2$
rules.add(new EndOfLineRule("//", TOKEN_COMMENT)); //$NON-NLS-1$
Expand All @@ -102,12 +141,20 @@ public boolean isWordPart(char c) {
}, TOKEN_DEFAULT);
keywordRule.addWords(keywordTokens, TOKEN_KEYWORD);
keywordRule.addWords(constantTokens, TOKEN_KEYWORD);

String[] keywordArray = new String[keywordMethods.size()];
keywordArray = keywordMethods.toArray(keywordArray);
keywordRule.addWords(keywordArray, TOKEN_METHOD);

String[] globalObjectArray = new String[globalObjectTokens.size()];
globalObjectArray = globalObjectTokens.toArray(globalObjectArray);
keywordRule.addWords(globalObjectArray, TOKEN_OBJECT);
rules.add(keywordRule);

setRuleList(rules);
}

private void setRuleList(List rules) {
private void setRuleList(List<IPredicateRule> rules) {
IPredicateRule[] result = new IPredicateRule[rules.size()];
rules.toArray(result);
setPredicateRules(result);
Expand All @@ -120,4 +167,45 @@ protected void addWords(WordRule rule, String[] tokens, IToken token) {

}

/*
* parse all classes and method from JS libraries and the according methods for
* syntax highlights
*/
private void fetchJSCommonObjectsMethods() {
this.globalObjectTokens.add("reportContext"); //$NON-NLS-1$
this.globalObjectTokens.add("params"); //$NON-NLS-1$
this.globalObjectTokens.add("value"); //$NON-NLS-1$
this.globalObjectTokens.add("vars"); //$NON-NLS-1$

try {
// analysis of static javascript classes and methods
List<IClassInfo> list = DEUtil.getClasses();
for (Iterator<IClassInfo> cIter = list.iterator(); cIter.hasNext();) {
IClassInfo classInfo = cIter.next();
if (classInfo.isNative() == true && !classInfo.getName().equals("Total")) {
this.globalObjectTokens.add(classInfo.getName());

List<IMethodInfo> resultMethodList = classInfo.getMethods();
for (Iterator<IMethodInfo> mIter = resultMethodList.iterator(); mIter.hasNext();) {
IMethodInfo methodInfo = mIter.next();
keywordMethods.add(methodInfo.getName());
}
}
}

// analysis of project implemented javascript classes and methods
IScriptFunctionCategory[] classInfo = FunctionProvider.getCategories();
for (int i = 0; i < classInfo.length; i++) {
this.globalObjectTokens.add(classInfo[i].getName());

IScriptFunction[] methodInfo = classInfo[i].getFunctions();
for (int j = 0; j < methodInfo.length; j++) {
keywordMethods.add(methodInfo[j].getName());
}
}
} catch (BirtException e) {
ExceptionHandler.handle(e);
}

}
}
Loading

0 comments on commit bd0178b

Please sign in to comment.