]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Expression field no such variable -annotations and syntax highlighting
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 1 Mar 2010 15:57:30 +0000 (15:57 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 1 Mar 2010 15:57:30 +0000 (15:57 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@14492 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionField.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldConfiguration.java

index 1e83532880af8c5858a6dc369d568f5855694992..ad4630e0908c65ce95fff4474d698ee465a60796 100644 (file)
@@ -33,9 +33,12 @@ import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Table;\r
 import org.eclipse.swt.widgets.TableItem;\r
 import org.eclipse.swt.widgets.Text;\r
+import org.eclipse.ui.IEditorInput;\r
+import org.eclipse.ui.IEditorPart;\r
 import org.eclipse.ui.ISelectionListener;\r
 import org.eclipse.ui.IWorkbenchPart;\r
 import org.eclipse.ui.IWorkbenchPartReference;\r
+import org.eclipse.ui.PlatformUI;\r
 import org.eclipse.ui.part.ViewPart;\r
 import org.simantics.db.Builtins;\r
 import org.simantics.db.ReadGraph;\r
@@ -46,8 +49,15 @@ import org.simantics.db.request.Read;
 import org.simantics.sysdyn.expressionParser.ExpressionParser;\r
 import org.simantics.sysdyn.expressionParser.ParseException;\r
 import org.simantics.sysdyn.expressionParser.Token;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+import org.simantics.sysdyn.manager.SysdynModelManager;\r
+import org.simantics.sysdyn.representation.Configuration;\r
+import org.simantics.sysdyn.representation.IElement;\r
+import org.simantics.sysdyn.representation.Variable;\r
 import org.simantics.sysdyn.ui.equation.expressions.ExpressionField;\r
+import org.simantics.sysdyn.ui.equation.expressions.StockExpressionViewFactor;\r
 import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.workbench.ResourceEditorInput;\r
 import org.eclipse.jface.text.Position;\r
 \r
 public class EquationView extends ViewPart implements ISelectionListener {\r
@@ -160,7 +170,7 @@ public class EquationView extends ViewPart implements ISelectionListener {
         text1.setToolTipText("Stop");\r
         text1 = new Text(c, SWT.SINGLE | SWT.BORDER);\r
         text1.setToolTipText("Step");\r
-        */\r
+         */\r
 \r
     }   \r
 \r
@@ -363,7 +373,7 @@ public class EquationView extends ViewPart implements ISelectionListener {
         HashMap<ExpressionField, HashMap<String, List<Token>>> references = new HashMap<ExpressionField, HashMap<String, List<Token>>>();\r
 \r
 \r
-\r
+        // Build references and variable array\r
         if(expressionComposite.getExpressionViewFactor() != null)\r
             for(ExpressionField ef : expressionComposite.getExpressionViewFactor().getExpressionFields()) {\r
                 ef.resetAnnotations();\r
@@ -379,35 +389,92 @@ public class EquationView extends ViewPart implements ISelectionListener {
                     ef.setSyntaxError(e1.currentToken);\r
                 }\r
             }\r
+        \r
+        // Remove variables from variable array that don't exist in the model. Create annotations\r
+        if(!variables.isEmpty()) {\r
+            ArrayList<String> modelVariables = new ArrayList<String>();\r
+            Set<String> noSuchVariables = new HashSet<String>();\r
+            IEditorPart editor = \r
+                PlatformUI.getWorkbench()\r
+                .getActiveWorkbenchWindow().getActivePage().getActiveEditor();\r
+            IEditorInput input =  editor.getEditorInput();\r
+            ResourceEditorInput rei = (ResourceEditorInput) input;\r
+            SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession());\r
+            SysdynModel model = sdm.getModel(rei.getResource());\r
+            Configuration conf = model.getConfiguration();\r
+            ArrayList<IElement> elements = conf.elements;\r
+            for(IElement e : elements) {\r
+                if(e instanceof Variable) {\r
+                    Variable v = (Variable) e;\r
+                    modelVariables.add(v.getName());\r
+                }\r
+            }\r
+            \r
 \r
-        for(TableItem ti : this.shortcutTabs.getVariableTable().getItems()) {\r
-            if(!variables.contains(ti.getText())) {\r
-                ti.setForeground(new Color(ti.getDisplay(), 255, 0, 0));\r
-            } else {\r
-                ti.setForeground(new Color(ti.getDisplay(), 0, 0, 0));\r
-                variables.remove(ti.getText());\r
+            for(String v : variables) {\r
+                if(!modelVariables.contains(v)) {\r
+                    noSuchVariables.add(v);\r
+                }\r
             }\r
+            \r
+            if(!noSuchVariables.isEmpty()) {\r
+                // remove no such variables from variable list\r
+                for(String s : noSuchVariables)\r
+                    variables.remove(s);\r
+                // create annotations\r
+                HashMap<ExpressionField ,ArrayList<Position>> positions = getPositionsForVariables(references, noSuchVariables);\r
+                for(ExpressionField ef : positions.keySet()) {\r
+                    ef.setNoSuchVariableAnnotations(positions.get(ef));\r
+                }\r
+            }      \r
         }\r
+        \r
+        // Check that the variables that exist have connections and the connected variables have references in the expressions\r
+        if(!(expressionComposite.getExpressionViewFactor() instanceof StockExpressionViewFactor)) { \r
+            for(TableItem ti : this.shortcutTabs.getVariableTable().getItems()) {\r
+                if(!variables.contains(ti.getText())) {\r
+                    ti.setForeground(new Color(ti.getDisplay(), 255, 0, 0));\r
+                } else {\r
+                    ti.setForeground(new Color(ti.getDisplay(), 0, 0, 0));\r
+                    variables.remove(ti.getText());\r
+                }\r
+            }\r
 \r
-        if(!variables.isEmpty()) {\r
-            ArrayList<Position> positions = new ArrayList<Position>();\r
-            for(String s : variables) {\r
-                List<Token> tlist = new ArrayList<Token>();\r
-                for(ExpressionField ef : references.keySet()) {\r
-                    tlist = references.get(ef).get(s);\r
-                    if(tlist != null)\r
-                        for(Token t : tlist) {\r
-                            StyledText st = ef.getSourceViewer().getTextWidget();\r
-                            int start =  st.getOffsetAtLine(t.beginLine - 1) + t.beginColumn - 1;\r
-                            int offset = st.getOffsetAtLine(t.endLine - 1) + t.endColumn - start;\r
-                            positions.add(new Position(\r
-                                    start,\r
-                                    offset));\r
-                        }\r
-                    ef.setMissingLinkAnnotations(positions);\r
+            if(!variables.isEmpty()) {\r
+                HashMap<ExpressionField ,ArrayList<Position>> positions = getPositionsForVariables(references, variables);\r
+                for(ExpressionField ef : positions.keySet()) {\r
+                    ef.setMissingLinkAnnotations(positions.get(ef));\r
                 }\r
+\r
             }\r
+        } \r
+\r
+    }\r
 \r
+    @SuppressWarnings("unchecked")\r
+    private HashMap<ExpressionField ,ArrayList<Position>> getPositionsForVariables(HashMap<ExpressionField, HashMap<String, List<Token>>> references, Set<String> variables) {\r
+        HashMap<ExpressionField ,ArrayList<Position>> result = new HashMap<ExpressionField ,ArrayList<Position>>();\r
+        for(String s : variables) {\r
+            List<Token> tlist = new ArrayList<Token>();\r
+            for(ExpressionField ef : references.keySet()) {\r
+                ArrayList<Position> positions = new ArrayList<Position>();\r
+                tlist = references.get(ef).get(s);\r
+                if(tlist != null)\r
+                    for(Token t : tlist) {\r
+                        StyledText st = ef.getSourceViewer().getTextWidget();\r
+                        int start =  st.getOffsetAtLine(t.beginLine - 1) + t.beginColumn - 1;\r
+                        int offset = st.getOffsetAtLine(t.endLine - 1) + t.endColumn - start;\r
+                        positions.add(new Position(\r
+                                start,\r
+                                offset));\r
+                    }\r
+                if(result.keySet().contains(ef)) {\r
+                    result.get(ef).addAll((ArrayList<Position>)positions.clone());\r
+                } else {\r
+                    result.put(ef, (ArrayList<Position>)positions.clone());\r
+                }\r
+            }\r
         }\r
+        return result;\r
     }\r
 }
\ No newline at end of file
index 3c26c4f5c84432e7ad525f44000290104c7f7228..bbbb0fa0df186b4c4b424190a46595c05ac290f8 100644 (file)
@@ -63,7 +63,9 @@ public class ExpressionField extends Composite {
 \r
         painter.addAnnotationType("MissingLink");\r
         painter.setAnnotationTypeColor("MissingLink", new Color(this.getDisplay(), 255,0,0));\r
-\r
+        painter.addAnnotationType("NoSuchVariable");\r
+        painter.setAnnotationTypeColor("NoSuchVariable", new Color(this.getDisplay(), 255,0,0));\r
+        \r
         _sourceViewer.setDocument(_document, _annotationModel);\r
 \r
         _sourceViewer.getControl().setLayoutData(new GridData(SWT.FILL,\r
@@ -92,6 +94,15 @@ public class ExpressionField extends Composite {
             _annotationModel.addAnnotation(annotation, p);        \r
         }\r
     }\r
+    \r
+    public void setNoSuchVariableAnnotations(List<Position> positions){\r
+        for(Position p : positions) {\r
+            Annotation annotation = new Annotation(false);\r
+            annotation.setType("NoSuchVariable");\r
+            annotation.setText("No such variable in model");\r
+            _annotationModel.addAnnotation(annotation, p);        \r
+        }\r
+    }\r
 \r
     public void setSyntaxError(Token token){\r
         int start = 0;\r
index 6c274f30b54d0402cbb23a4865496f557a79753d..ac3627e03884bc5b9edb16dff7d6856add77e0ea 100644 (file)
@@ -1,5 +1,8 @@
 package org.simantics.sysdyn.ui.equation.expressions;\r
 \r
+import java.util.HashSet;\r
+import java.util.Set;\r
+\r
 import org.eclipse.jface.text.DefaultTextHover;\r
 import org.eclipse.jface.text.IDocument;\r
 import org.eclipse.jface.text.ITextHover;\r
@@ -10,7 +13,6 @@ import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
 import org.eclipse.jface.text.rules.IRule;\r
 import org.eclipse.jface.text.rules.ITokenScanner;\r
 import org.eclipse.jface.text.rules.IWordDetector;\r
-import org.eclipse.jface.text.rules.PatternRule;\r
 import org.eclipse.jface.text.rules.RuleBasedScanner;\r
 import org.eclipse.jface.text.rules.Token;\r
 import org.eclipse.jface.text.rules.WordRule;\r
@@ -66,42 +68,31 @@ public class ExpressionFieldConfiguration extends SourceViewerConfiguration {
      ITokenScanner getSclTokenScanner() {\r
         RuleBasedScanner scanner = new RuleBasedScanner();\r
 \r
-        final Token string = new Token(new TextAttribute(colorManager.getColor(new RGB(42, 0, 255))));  \r
         final Token reserved = new Token(\r
                 new TextAttribute(\r
                         colorManager.getColor(new RGB(127, 0, 85)),\r
                         null,\r
                         SWT.BOLD\r
                 ));\r
-        final Token comment = new Token(new TextAttribute(colorManager.getColor(new RGB(63, 127, 95))));\r
+        \r
 \r
         WordRule reservedWord = new WordRule(new IWordDetector() {   \r
             @Override\r
             public boolean isWordStart(char c) {\r
-                return c=='@';\r
+                return Character.isLetter(c);\r
             }\r
 \r
             @Override\r
             public boolean isWordPart(char c) {\r
-                return Character.isJavaIdentifierPart(c);\r
+                return Character.isLetter(c);\r
             }\r
         });\r
 \r
-        reservedWord.addWord("@a", reserved);\r
-        reservedWord.addWord("@keywords", reserved);\r
-        reservedWord.addWord("@forAll", reserved);\r
-        reservedWord.addWord("@forSome", reserved);\r
-        reservedWord.addWord("@true", reserved);\r
-        reservedWord.addWord("@false", reserved);\r
-        reservedWord.addWord("@base", reserved);\r
-        reservedWord.addWord("@prefix", reserved);\r
+        for(String s : keywords) {\r
+            reservedWord.addWord(s, reserved);\r
+        }\r
 \r
         IRule[] rules = new IRule[] {\r
-                //new MultiLineRule("\"\"\"", "\"\"\"", string),\r
-                new PatternRule("\"", "\"", string, '\\', true),   \r
-                new PatternRule("\"\"\"", "\"\"\"", string, '\\', false),\r
-                new PatternRule("<", ">", string, '\\', false),\r
-                new PatternRule("#", null, comment, '\0', true),\r
                 reservedWord\r
         };\r
         scanner.setRules(rules);\r
@@ -119,4 +110,60 @@ public class ExpressionFieldConfiguration extends SourceViewerConfiguration {
          return new DefaultAnnotationHover();\r
      }\r
 \r
+     static final Set<String> keywords = new HashSet<String>();\r
+     \r
+     static {\r
+         keywords.add("within");\r
+         keywords.add("final");\r
+         keywords.add("public");\r
+         keywords.add("protected");\r
+         keywords.add("connect");\r
+         keywords.add("when");\r
+         keywords.add("then");\r
+         keywords.add("elsewhen");\r
+         keywords.add("if");\r
+         keywords.add("end");\r
+         keywords.add("elseif");\r
+         keywords.add("else");\r
+         keywords.add("for");\r
+         keywords.add("while");\r
+         keywords.add("loop");\r
+         keywords.add("der");\r
+         keywords.add("enumeration");\r
+         keywords.add("extends");\r
+         keywords.add("class");\r
+         keywords.add("partial");\r
+         keywords.add("encapsulated");\r
+         keywords.add("model");\r
+         keywords.add("record");\r
+         keywords.add("block");\r
+         keywords.add("expandable");\r
+         keywords.add("connector");\r
+         keywords.add("type");\r
+         keywords.add("package");\r
+         keywords.add("function");\r
+         keywords.add("import");\r
+         keywords.add("external");\r
+         keywords.add("constrainedby");\r
+         keywords.add("redeclare");\r
+         keywords.add("replaceable");\r
+         keywords.add("flow");\r
+         keywords.add("discrete");\r
+         keywords.add("parameter");\r
+         keywords.add("constant");\r
+         keywords.add("input");\r
+         keywords.add("output");\r
+         keywords.add("annotation");\r
+         keywords.add("false");\r
+         keywords.add("true");\r
+         keywords.add("each");\r
+         keywords.add("initial");\r
+         keywords.add("algorithm");\r
+         keywords.add("equation");\r
+         keywords.add("or");\r
+         keywords.add("and");\r
+         keywords.add("not");\r
+         keywords.add("break");\r
+         keywords.add("return");     \r
+     }\r
 }\r