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
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
text1.setToolTipText("Stop");\r
text1 = new Text(c, SWT.SINGLE | SWT.BORDER);\r
text1.setToolTipText("Step");\r
- */\r
+ */\r
\r
} \r
\r
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
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
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
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
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
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