From 04add5a83c8d07ee323a6893e8dfcb0dbc33b50c Mon Sep 17 00:00:00 2001 From: lempinen Date: Thu, 16 Sep 2010 06:49:17 +0000 Subject: [PATCH] All modelica keywords are highlighted. Fixed the bug where the end of the word is highlighted, if it is a keyword. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@17955 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../ui/properties/VariableNameValidator.java | 70 +++++- .../ExpressionFieldConfiguration.java | 220 ++++++------------ 2 files changed, 146 insertions(+), 144 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableNameValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableNameValidator.java index 56ae17ab..e4819708 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableNameValidator.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableNameValidator.java @@ -1,5 +1,7 @@ package org.simantics.sysdyn.ui.properties; +import java.util.HashSet; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -9,6 +11,72 @@ public class VariableNameValidator { String lowerCase = name.toLowerCase(); Pattern p = Pattern.compile("[a-zA-Z0-9]++"); Matcher m = p.matcher(lowerCase); - return m.matches(); + if (!m.matches() || keywords.contains(name)) + return false; + else + return true; + } + + public static final Set keywords = new HashSet(); + + static { + keywords.add("algorithm"); + keywords.add("discrete"); + keywords.add("false"); + keywords.add("model"); + keywords.add("redeclare"); + keywords.add("and"); + keywords.add("each"); + keywords.add("final"); + keywords.add("not"); + keywords.add("replaceable"); + keywords.add("annotation"); + keywords.add("else"); + keywords.add("flow"); + keywords.add("operator"); + keywords.add("return"); + keywords.add("assert"); + keywords.add("elseif"); + keywords.add("for"); + keywords.add("or"); + keywords.add("stream"); + keywords.add("block"); + keywords.add("elsewhen"); + keywords.add("function"); + keywords.add("outer"); + keywords.add("then"); + keywords.add("break"); + keywords.add("encapsulated"); + keywords.add("if"); + keywords.add("output"); + keywords.add("true"); + keywords.add("class"); + keywords.add("end"); + keywords.add("import"); + keywords.add("package"); + keywords.add("type"); + keywords.add("connect"); + keywords.add("enumeration"); + keywords.add("in"); + keywords.add("parameter"); + keywords.add("when"); + keywords.add("connector"); + keywords.add("equation"); + keywords.add("initial"); + keywords.add("partial"); + keywords.add("while"); + keywords.add("constant"); + keywords.add("expandable"); + keywords.add("inner"); + keywords.add("protected"); + keywords.add("within"); + keywords.add("constrainedby"); + keywords.add("extends"); + keywords.add("input"); + keywords.add("public"); + keywords.add("der"); + keywords.add("external"); + keywords.add("loop"); + keywords.add("record"); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionFieldConfiguration.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionFieldConfiguration.java index 68d46d86..08ecb22d 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionFieldConfiguration.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionFieldConfiguration.java @@ -11,9 +11,6 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.properties.widgets.expressions; -import java.util.HashSet; -import java.util.Set; - import org.eclipse.jface.text.DefaultTextHover; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextHover; @@ -24,6 +21,7 @@ import org.eclipse.jface.text.rules.DefaultDamagerRepairer; import org.eclipse.jface.text.rules.IRule; import org.eclipse.jface.text.rules.ITokenScanner; import org.eclipse.jface.text.rules.IWordDetector; +import org.eclipse.jface.text.rules.MultiLineRule; import org.eclipse.jface.text.rules.RuleBasedScanner; import org.eclipse.jface.text.rules.Token; import org.eclipse.jface.text.rules.WordRule; @@ -33,148 +31,84 @@ import org.eclipse.jface.text.source.ISourceViewer; import org.eclipse.jface.text.source.SourceViewerConfiguration; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.RGB; +import org.simantics.sysdyn.ui.properties.VariableNameValidator; public class ExpressionFieldConfiguration extends SourceViewerConfiguration { - ColorManager colorManager; - - public ExpressionFieldConfiguration(ColorManager colorManager) { - super(); - this.colorManager = colorManager; - } - - public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) { - return new String[] { - IDocument.DEFAULT_CONTENT_TYPE - }; - } - - /* - @Override - public IAutoEditStrategy[] getAutoEditStrategies( - ISourceViewer sourceViewer, String contentType) { - return new IAutoEditStrategy[] { - new ReplaceTabsBySpaces(), - new IndentLineEditStrategy(), - new MatchingBracketsEditStrategy() - }; - } - */ - - public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) { - PresentationReconciler reconciler = new PresentationReconciler(); - - DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getSclTokenScanner()); - - reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE); - reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE); - - return reconciler; - } - - /* - * - */ - ITokenScanner getSclTokenScanner() { - RuleBasedScanner scanner = new RuleBasedScanner(); - - final Token reserved = new Token( - new TextAttribute( - colorManager.getColor(new RGB(127, 0, 85)), - null, - SWT.BOLD - )); - - - WordRule reservedWord = new WordRule(new IWordDetector() { - @Override - public boolean isWordStart(char c) { - return Character.isLetter(c); - } - - @Override - public boolean isWordPart(char c) { - return Character.isLetter(c); - } - }); - - for(String s : keywords) { - reservedWord.addWord(s, reserved); - } - - IRule[] rules = new IRule[] { - reservedWord - }; - scanner.setRules(rules); - - return scanner; - } - - @Override - public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) { - return new DefaultTextHover(sourceViewer); - } - - @Override - public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) { - return new DefaultAnnotationHover(); - } - - static final Set keywords = new HashSet(); - - static { - keywords.add("within"); - keywords.add("final"); - keywords.add("public"); - keywords.add("protected"); - keywords.add("connect"); - keywords.add("when"); - keywords.add("then"); - keywords.add("elsewhen"); - keywords.add("if"); - keywords.add("end"); - keywords.add("elseif"); - keywords.add("else"); - keywords.add("for"); - keywords.add("while"); - keywords.add("loop"); - keywords.add("der"); - keywords.add("enumeration"); - keywords.add("extends"); - keywords.add("class"); - keywords.add("partial"); - keywords.add("encapsulated"); - keywords.add("model"); - keywords.add("record"); - keywords.add("block"); - keywords.add("expandable"); - keywords.add("connector"); - keywords.add("type"); - keywords.add("package"); - keywords.add("function"); - keywords.add("import"); - keywords.add("external"); - keywords.add("constrainedby"); - keywords.add("redeclare"); - keywords.add("replaceable"); - keywords.add("flow"); - keywords.add("discrete"); - keywords.add("parameter"); - keywords.add("constant"); - keywords.add("input"); - keywords.add("output"); - keywords.add("annotation"); - keywords.add("false"); - keywords.add("true"); - keywords.add("each"); - keywords.add("initial"); - keywords.add("algorithm"); - keywords.add("equation"); - keywords.add("or"); - keywords.add("and"); - keywords.add("not"); - keywords.add("break"); - keywords.add("return"); - } + ColorManager colorManager; + + public ExpressionFieldConfiguration(ColorManager colorManager) { + super(); + this.colorManager = colorManager; + } + + public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) { + return new String[] { + IDocument.DEFAULT_CONTENT_TYPE + }; + } + + public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) { + PresentationReconciler reconciler = new PresentationReconciler(); + + DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getSclTokenScanner()); + + reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE); + reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE); + + return reconciler; + } + + + ITokenScanner getSclTokenScanner() { + RuleBasedScanner scanner = new RuleBasedScanner(); + + final Token reserved = new Token( + new TextAttribute( + colorManager.getColor(new RGB(127, 0, 85)), + null, + SWT.BOLD + )); + final Token defaultToken = new Token(new TextAttribute(colorManager.getColor(new RGB(0, 0, 0)))); + + final Token comment = new Token(new TextAttribute(colorManager.getColor(new RGB(63, 127, 95)))); + + + WordRule reservedWord = new WordRule(new IWordDetector() { + @Override + public boolean isWordStart(char c) { + return Character.isLetter(c); + } + + @Override + public boolean isWordPart(char c) { + return Character.isLetter(c); + } + }, defaultToken); + + + for(String s : VariableNameValidator.keywords) { + reservedWord.addWord(s, reserved); + } + + IRule[] rules = new IRule[] { + reservedWord, + new MultiLineRule("/*", "*/", comment) + }; + scanner.setRules(rules); + + return scanner; + } + + @Override + public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) { + return new DefaultTextHover(sourceViewer); + } + + @Override + public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) { + return new DefaultAnnotationHover(); + } + } -- 2.47.1