1 package org.simantics.scl.ui.editor2;
4 import org.eclipse.core.commands.AbstractHandler;
5 import org.eclipse.core.commands.ExecutionEvent;
6 import org.eclipse.core.commands.ExecutionException;
7 import org.eclipse.swt.custom.StyledText;
8 import org.eclipse.swt.widgets.Control;
9 import org.eclipse.ui.IEditorPart;
10 import org.eclipse.ui.PlatformUI;
11 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
12 import org.simantics.scl.ui.editor.completion.SCLTextEditorEnvironment;
14 public class OpenDeclaration extends AbstractHandler {
16 private static boolean isIdentifierPart(char c) {
17 return Character.isJavaIdentifierPart(c) || c=='.';
20 private static String extractIdentifierAt(String text, int caretPos) {
21 int startPos = caretPos;
22 while(startPos > 0 && isIdentifierPart(text.charAt(startPos-1)))
24 int endPos = caretPos;
25 while(endPos < text.length() && isIdentifierPart(text.charAt(endPos)))
27 return text.substring(startPos, endPos);
30 private static final String SYMBOL_CHARS = "!$%&*+/<=>?@\\^|-:~.";
32 private static boolean isSymbolPart(char c) {
33 for(int i=0;i<SYMBOL_CHARS.length();++i)
34 if(SYMBOL_CHARS.charAt(i) == c)
39 private static String extractSymbolAt(String text, int caretPos) {
40 int startPos = caretPos;
41 while(startPos > 0 && isSymbolPart(text.charAt(startPos-1)))
43 int endPos = caretPos;
44 while(endPos < text.length() && isSymbolPart(text.charAt(endPos)))
46 return text.substring(startPos, endPos);
49 public static String extractIdentifierOrSymbolAt(String text, int caretPos) {
50 String result = extractIdentifierAt(text, caretPos);
53 return extractSymbolAt(text, caretPos);
56 private static String extractLineAt(String text, int caretPos) {
57 int startPos = caretPos;
58 while(startPos > 0 && !isNewline(text.charAt(startPos-1)))
60 int endPos = caretPos;
61 while(endPos < text.length() && !isNewline(text.charAt(endPos)))
63 return text.substring(startPos, endPos);
66 private static boolean isNewline(char c) {
67 return c=='\n' || c=='\r';
71 public Object execute(ExecutionEvent event) throws ExecutionException {
73 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
74 if(!(editor instanceof SCLModuleEditor2))
76 SCLModuleEditor2 moduleEditor = (SCLModuleEditor2)editor;
77 StyledText styledText = (StyledText)moduleEditor.getAdapter(Control.class);
78 String text = styledText.getText();
79 int caretOffset = styledText.getCaretOffset();
81 // Find the line where the caret is
82 String lineAtCaret = extractLineAt(text, caretOffset);
83 if(lineAtCaret.startsWith("import ") || lineAtCaret.startsWith("include ")) {
84 int p1 = lineAtCaret.indexOf('"', 6);
85 int p2 = lineAtCaret.indexOf('"', p1+1);
86 String moduleName = lineAtCaret.substring(p1+1, p2);
87 OpenSCLModule.openModule(moduleName);
90 // Try to find an identifier at caret
91 String identifierAtCaret = extractIdentifierOrSymbolAt(text, caretOffset);
92 if(identifierAtCaret.isEmpty())
94 SCLTextEditorEnvironment editorEnvironment = moduleEditor.getSCLTextEditorEnvironment();
95 editorEnvironment.updateEnvironment(moduleEditor.getDocument());
96 SCLValue value = editorEnvironment.getValue(identifierAtCaret);
98 OpenSCLDefinition.openDefinition(value);