]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/OpenDeclaration.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / editor2 / OpenDeclaration.java
1 package org.simantics.scl.ui.editor2;
2
3
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;
13
14 public class OpenDeclaration extends AbstractHandler {
15
16     private static boolean isIdentifierPart(char c) {
17         return Character.isJavaIdentifierPart(c) || c=='.';
18     }
19     
20     private static String extractIdentifierAt(String text, int caretPos) {
21         int startPos = caretPos;
22         while(startPos > 0 && isIdentifierPart(text.charAt(startPos-1)))
23             --startPos;
24         int endPos = caretPos;
25         while(endPos < text.length() && isIdentifierPart(text.charAt(endPos)))
26             ++endPos;
27         return text.substring(startPos, endPos);
28     }
29     
30     private static final String SYMBOL_CHARS = "!$%&*+/<=>?@\\^|-:~.";
31     
32     private static boolean isSymbolPart(char c) {
33         for(int i=0;i<SYMBOL_CHARS.length();++i)
34             if(SYMBOL_CHARS.charAt(i) == c)
35                 return true;
36         return false;
37     }
38     
39     private static String extractSymbolAt(String text, int caretPos) {
40         int startPos = caretPos;
41         while(startPos > 0 && isSymbolPart(text.charAt(startPos-1)))
42             --startPos;
43         int endPos = caretPos;
44         while(endPos < text.length() && isSymbolPart(text.charAt(endPos)))
45             ++endPos;
46         return text.substring(startPos, endPos);
47     }
48     
49     public static String extractIdentifierOrSymbolAt(String text, int caretPos) {
50         String result = extractIdentifierAt(text, caretPos);
51         if(!result.isEmpty())
52             return result;
53         return extractSymbolAt(text, caretPos);
54     }
55     
56     private static String extractLineAt(String text, int caretPos) {
57         int startPos = caretPos;
58         while(startPos > 0 && !isNewline(text.charAt(startPos-1)))
59             --startPos;
60         int endPos = caretPos;
61         while(endPos < text.length() && !isNewline(text.charAt(endPos)))
62             ++endPos;
63         return text.substring(startPos, endPos);
64     }
65     
66     private static boolean isNewline(char c) {
67         return c=='\n' || c=='\r';
68     }
69
70     @Override
71     public Object execute(ExecutionEvent event) throws ExecutionException {
72         IEditorPart editor = 
73                 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
74         if(!(editor instanceof SCLModuleEditor2))
75             return null;
76         SCLModuleEditor2 moduleEditor = (SCLModuleEditor2)editor;
77         StyledText styledText = (StyledText)moduleEditor.getAdapter(Control.class);
78         String text = styledText.getText();
79         int caretOffset = styledText.getCaretOffset();
80         
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);
88         }
89         else {
90             // Try to find an identifier at caret
91             String identifierAtCaret = extractIdentifierOrSymbolAt(text, caretOffset);
92             if(identifierAtCaret.isEmpty())
93                 return null;
94             SCLTextEditorEnvironment editorEnvironment = moduleEditor.getSCLTextEditorEnvironment();
95             editorEnvironment.updateEnvironment(moduleEditor.getDocument());
96             SCLValue value = editorEnvironment.getValue(identifierAtCaret);
97             if(value != null)
98                 OpenSCLDefinition.openDefinition(value);
99         }
100         return null;
101     }
102
103 }