From 09e59a7139fe89b26ca17a4978c0b582738d1d1c Mon Sep 17 00:00:00 2001 From: Sebastian Bergemann <bergemann@fortiss.org> Date: Fri, 23 Jun 2023 17:51:23 +0200 Subject: [PATCH] Add better highlighter for code comments and autocompletion Issue-Ref: 4299 Issue-Url: https://git.fortiss.org/af3/af3/-/issues/4299 Signed-off-by: Sebastian Bergemann <bergemann@fortiss.org> --- .../editor/SourceEditorConfigurationBase.java | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/editor/SourceEditorConfigurationBase.java b/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/editor/SourceEditorConfigurationBase.java index 434b88c9a..451ff8c07 100644 --- a/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/editor/SourceEditorConfigurationBase.java +++ b/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/editor/SourceEditorConfigurationBase.java @@ -19,8 +19,10 @@ import static java.lang.Character.isLetterOrDigit; import static org.eclipse.jface.text.IDocument.DEFAULT_CONTENT_TYPE; import static org.eclipse.wb.swt.SWTResourceManager.getColor; +import java.util.ArrayList; import java.util.List; +import org.apache.commons.lang3.ArrayUtils; import org.eclipse.emf.ecore.EObject; import org.eclipse.jface.text.presentation.IPresentationReconciler; import org.eclipse.jface.text.presentation.PresentationReconciler; @@ -29,6 +31,7 @@ import org.eclipse.jface.text.rules.IRule; import org.eclipse.jface.text.rules.IToken; import org.eclipse.jface.text.rules.ITokenScanner; import org.eclipse.jface.text.rules.IWordDetector; +import org.eclipse.jface.text.rules.PatternRule; import org.eclipse.jface.text.rules.RuleBasedScanner; import org.eclipse.jface.text.rules.WordRule; import org.eclipse.jface.text.source.IAnnotationHover; @@ -59,8 +62,8 @@ public abstract class SourceEditorConfigurationBase<T extends EObject> /** Color constant used to display code in dark blue. */ protected Color DARK_BLUE = getColor(0, 0, 128); - /** Color constant used to display code in green. */ - protected Color GREEN = getColor(0, 178, 24); + /** Color constant used to display comments (green). */ + protected Color COMMENT_COLOR = getColor(0, 148, 0); /** Identifier for the start of a comment. */ public static final String COMMENT_START_IDENTIFIER = "/*"; @@ -73,15 +76,13 @@ public abstract class SourceEditorConfigurationBase<T extends EObject> /** {@inheritDoc} */ @Override public boolean isWordStart(char c) { - return isLetterOrDigit(c) || COMMENT_START_IDENTIFIER.indexOf(c) != -1 || - COMMENT_END_IDENTIFIER.indexOf(c) != -1; + return isLetterOrDigit(c); } /** {@inheritDoc} */ @Override public boolean isWordPart(char c) { - return isLetterOrDigit(c) || c == '_' || COMMENT_START_IDENTIFIER.indexOf(c) != -1 || - COMMENT_END_IDENTIFIER.indexOf(c) != -1; + return isLetterOrDigit(c) || c == '_'; } }; @@ -110,10 +111,20 @@ public abstract class SourceEditorConfigurationBase<T extends EObject> /** Returns the rule based scanner. */ private RuleBasedScanner getScannerForSyntaxHighlighting() { - List<WordRule> rules = getCommonRules(); - rules.add(getRuleSpecificToEditor()); + // word rules + List<WordRule> wordRules = getCommonRules(); + wordRules.add(getRuleSpecificToEditor()); + + // pattern rules + List<PatternRule> patternRules = new ArrayList<>(); + patternRules.add(getCommentRuleSpecificToEditor()); + + IRule[] wordRuleArray = wordRules.toArray(new IRule[0]); + IRule[] patternRuleArray = patternRules.toArray(new IRule[0]); + IRule[] rules = ArrayUtils.addAll(wordRuleArray, patternRuleArray); + RuleBasedScanner scanner = new RuleBasedScanner(); - scanner.setRules(rules.toArray(new IRule[0])); + scanner.setRules(rules); return scanner; } @@ -123,6 +134,9 @@ public abstract class SourceEditorConfigurationBase<T extends EObject> /** Returns a rule specific to the editor. */ abstract protected WordRule getRuleSpecificToEditor(); + /** Returns the comment rule specific to the editor. */ + abstract protected PatternRule getCommentRuleSpecificToEditor(); + /** Adds the words to the rule with the given token. */ protected void addWordsToRule(List<String> words, WordRule rule, IToken token) { for(String word : words) { -- GitLab