Skip to content
Snippets Groups Projects
Commit 09e59a71 authored by Sebastian Bergemann's avatar Sebastian Bergemann
Browse files

Add better highlighter for code comments and autocompletion

Issue-Ref: 4299
Issue-Url: af3#4299



Signed-off-by: default avatarSebastian Bergemann <bergemann@fortiss.org>
parent 7b9329ed
No related branches found
No related tags found
1 merge request!2034299: Add comment handling to source editor (for highlighting)
Pipeline #38325 failed
......@@ -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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment