From d4f6dffcb0461779ae7a7576521b595cbc6569b6 Mon Sep 17 00:00:00 2001 From: lempinen Date: Mon, 1 Mar 2010 15:57:30 +0000 Subject: [PATCH] Expression field no such variable -annotations and syntax highlighting git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@14492 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/equation/EquationView.java | 115 ++++++++++++++---- .../equation/expressions/ExpressionField.java | 13 +- .../ExpressionFieldConfiguration.java | 83 ++++++++++--- 3 files changed, 168 insertions(+), 43 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java index 1e835328..ad4630e0 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java @@ -33,9 +33,12 @@ import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableItem; import org.eclipse.swt.widgets.Text; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorPart; import org.eclipse.ui.ISelectionListener; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.IWorkbenchPartReference; +import org.eclipse.ui.PlatformUI; import org.eclipse.ui.part.ViewPart; import org.simantics.db.Builtins; import org.simantics.db.ReadGraph; @@ -46,8 +49,15 @@ import org.simantics.db.request.Read; import org.simantics.sysdyn.expressionParser.ExpressionParser; import org.simantics.sysdyn.expressionParser.ParseException; import org.simantics.sysdyn.expressionParser.Token; +import org.simantics.sysdyn.manager.SysdynModel; +import org.simantics.sysdyn.manager.SysdynModelManager; +import org.simantics.sysdyn.representation.Configuration; +import org.simantics.sysdyn.representation.IElement; +import org.simantics.sysdyn.representation.Variable; import org.simantics.sysdyn.ui.equation.expressions.ExpressionField; +import org.simantics.sysdyn.ui.equation.expressions.StockExpressionViewFactor; import org.simantics.ui.SimanticsUI; +import org.simantics.ui.workbench.ResourceEditorInput; import org.eclipse.jface.text.Position; public class EquationView extends ViewPart implements ISelectionListener { @@ -160,7 +170,7 @@ public class EquationView extends ViewPart implements ISelectionListener { text1.setToolTipText("Stop"); text1 = new Text(c, SWT.SINGLE | SWT.BORDER); text1.setToolTipText("Step"); - */ + */ } @@ -363,7 +373,7 @@ public class EquationView extends ViewPart implements ISelectionListener { HashMap>> references = new HashMap>>(); - + // Build references and variable array if(expressionComposite.getExpressionViewFactor() != null) for(ExpressionField ef : expressionComposite.getExpressionViewFactor().getExpressionFields()) { ef.resetAnnotations(); @@ -379,35 +389,92 @@ public class EquationView extends ViewPart implements ISelectionListener { ef.setSyntaxError(e1.currentToken); } } + + // Remove variables from variable array that don't exist in the model. Create annotations + if(!variables.isEmpty()) { + ArrayList modelVariables = new ArrayList(); + Set noSuchVariables = new HashSet(); + IEditorPart editor = + PlatformUI.getWorkbench() + .getActiveWorkbenchWindow().getActivePage().getActiveEditor(); + IEditorInput input = editor.getEditorInput(); + ResourceEditorInput rei = (ResourceEditorInput) input; + SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession()); + SysdynModel model = sdm.getModel(rei.getResource()); + Configuration conf = model.getConfiguration(); + ArrayList elements = conf.elements; + for(IElement e : elements) { + if(e instanceof Variable) { + Variable v = (Variable) e; + modelVariables.add(v.getName()); + } + } + - for(TableItem ti : this.shortcutTabs.getVariableTable().getItems()) { - if(!variables.contains(ti.getText())) { - ti.setForeground(new Color(ti.getDisplay(), 255, 0, 0)); - } else { - ti.setForeground(new Color(ti.getDisplay(), 0, 0, 0)); - variables.remove(ti.getText()); + for(String v : variables) { + if(!modelVariables.contains(v)) { + noSuchVariables.add(v); + } } + + if(!noSuchVariables.isEmpty()) { + // remove no such variables from variable list + for(String s : noSuchVariables) + variables.remove(s); + // create annotations + HashMap> positions = getPositionsForVariables(references, noSuchVariables); + for(ExpressionField ef : positions.keySet()) { + ef.setNoSuchVariableAnnotations(positions.get(ef)); + } + } } + + // Check that the variables that exist have connections and the connected variables have references in the expressions + if(!(expressionComposite.getExpressionViewFactor() instanceof StockExpressionViewFactor)) { + for(TableItem ti : this.shortcutTabs.getVariableTable().getItems()) { + if(!variables.contains(ti.getText())) { + ti.setForeground(new Color(ti.getDisplay(), 255, 0, 0)); + } else { + ti.setForeground(new Color(ti.getDisplay(), 0, 0, 0)); + variables.remove(ti.getText()); + } + } - if(!variables.isEmpty()) { - ArrayList positions = new ArrayList(); - for(String s : variables) { - List tlist = new ArrayList(); - for(ExpressionField ef : references.keySet()) { - tlist = references.get(ef).get(s); - if(tlist != null) - for(Token t : tlist) { - StyledText st = ef.getSourceViewer().getTextWidget(); - int start = st.getOffsetAtLine(t.beginLine - 1) + t.beginColumn - 1; - int offset = st.getOffsetAtLine(t.endLine - 1) + t.endColumn - start; - positions.add(new Position( - start, - offset)); - } - ef.setMissingLinkAnnotations(positions); + if(!variables.isEmpty()) { + HashMap> positions = getPositionsForVariables(references, variables); + for(ExpressionField ef : positions.keySet()) { + ef.setMissingLinkAnnotations(positions.get(ef)); } + } + } + + } + @SuppressWarnings("unchecked") + private HashMap> getPositionsForVariables(HashMap>> references, Set variables) { + HashMap> result = new HashMap>(); + for(String s : variables) { + List tlist = new ArrayList(); + for(ExpressionField ef : references.keySet()) { + ArrayList positions = new ArrayList(); + tlist = references.get(ef).get(s); + if(tlist != null) + for(Token t : tlist) { + StyledText st = ef.getSourceViewer().getTextWidget(); + int start = st.getOffsetAtLine(t.beginLine - 1) + t.beginColumn - 1; + int offset = st.getOffsetAtLine(t.endLine - 1) + t.endColumn - start; + positions.add(new Position( + start, + offset)); + } + if(result.keySet().contains(ef)) { + result.get(ef).addAll((ArrayList)positions.clone()); + } else { + result.put(ef, (ArrayList)positions.clone()); + } + } } + return result; } } \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionField.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionField.java index 3c26c4f5..bbbb0fa0 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionField.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionField.java @@ -63,7 +63,9 @@ public class ExpressionField extends Composite { painter.addAnnotationType("MissingLink"); painter.setAnnotationTypeColor("MissingLink", new Color(this.getDisplay(), 255,0,0)); - + painter.addAnnotationType("NoSuchVariable"); + painter.setAnnotationTypeColor("NoSuchVariable", new Color(this.getDisplay(), 255,0,0)); + _sourceViewer.setDocument(_document, _annotationModel); _sourceViewer.getControl().setLayoutData(new GridData(SWT.FILL, @@ -92,6 +94,15 @@ public class ExpressionField extends Composite { _annotationModel.addAnnotation(annotation, p); } } + + public void setNoSuchVariableAnnotations(List positions){ + for(Position p : positions) { + Annotation annotation = new Annotation(false); + annotation.setType("NoSuchVariable"); + annotation.setText("No such variable in model"); + _annotationModel.addAnnotation(annotation, p); + } + } public void setSyntaxError(Token token){ int start = 0; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldConfiguration.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldConfiguration.java index 6c274f30..ac3627e0 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldConfiguration.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldConfiguration.java @@ -1,5 +1,8 @@ package org.simantics.sysdyn.ui.equation.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; @@ -10,7 +13,6 @@ 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.PatternRule; import org.eclipse.jface.text.rules.RuleBasedScanner; import org.eclipse.jface.text.rules.Token; import org.eclipse.jface.text.rules.WordRule; @@ -66,42 +68,31 @@ public class ExpressionFieldConfiguration extends SourceViewerConfiguration { ITokenScanner getSclTokenScanner() { RuleBasedScanner scanner = new RuleBasedScanner(); - final Token string = new Token(new TextAttribute(colorManager.getColor(new RGB(42, 0, 255)))); final Token reserved = new Token( new TextAttribute( colorManager.getColor(new RGB(127, 0, 85)), null, SWT.BOLD )); - 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 c=='@'; + return Character.isLetter(c); } @Override public boolean isWordPart(char c) { - return Character.isJavaIdentifierPart(c); + return Character.isLetter(c); } }); - reservedWord.addWord("@a", reserved); - reservedWord.addWord("@keywords", reserved); - reservedWord.addWord("@forAll", reserved); - reservedWord.addWord("@forSome", reserved); - reservedWord.addWord("@true", reserved); - reservedWord.addWord("@false", reserved); - reservedWord.addWord("@base", reserved); - reservedWord.addWord("@prefix", reserved); + for(String s : keywords) { + reservedWord.addWord(s, reserved); + } IRule[] rules = new IRule[] { - //new MultiLineRule("\"\"\"", "\"\"\"", string), - new PatternRule("\"", "\"", string, '\\', true), - new PatternRule("\"\"\"", "\"\"\"", string, '\\', false), - new PatternRule("<", ">", string, '\\', false), - new PatternRule("#", null, comment, '\0', true), reservedWord }; scanner.setRules(rules); @@ -119,4 +110,60 @@ public class ExpressionFieldConfiguration extends SourceViewerConfiguration { 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"); + } } -- 2.47.1