]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Open module when pressing F3 on import or include declaration 82/82/3
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Wed, 21 Sep 2016 11:28:31 +0000 (14:28 +0300)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Wed, 21 Sep 2016 11:58:06 +0000 (14:58 +0300)
Change-Id: If53a968b8a29a7c6a18ae9f1e44bac06df13bbd4

bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/OpenDeclaration.java
bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/OpenSCLModule.java

index f8ea0c20d1b9960e4258cc53cf6898a77ffbc5f3..522b8d3ed22a4e097ad0e98722abad9e331e8612 100644 (file)
@@ -46,14 +46,27 @@ public class OpenDeclaration extends AbstractHandler {
         return text.substring(startPos, endPos);\r
     }\r
     \r
-    public static String extractAt(String text, int caretPos) {\r
+    public static String extractIdentifierOrSymbolAt(String text, int caretPos) {\r
         String result = extractIdentifierAt(text, caretPos);\r
         if(!result.isEmpty())\r
             return result;\r
         return extractSymbolAt(text, caretPos);\r
     }\r
-\r
     \r
+    private static String extractLineAt(String text, int caretPos) {\r
+        int startPos = caretPos;\r
+        while(startPos > 0 && !isNewline(text.charAt(startPos-1)))\r
+            --startPos;\r
+        int endPos = caretPos;\r
+        while(endPos < text.length() && !isNewline(text.charAt(endPos)))\r
+            ++endPos;\r
+        return text.substring(startPos, endPos);\r
+    }\r
+    \r
+    private static boolean isNewline(char c) {\r
+        return c=='\n' || c=='\r';\r
+    }\r
+\r
     @Override\r
     public Object execute(ExecutionEvent event) throws ExecutionException {\r
         IEditorPart editor = \r
@@ -62,14 +75,28 @@ public class OpenDeclaration extends AbstractHandler {
             return null;\r
         SCLModuleEditor2 moduleEditor = (SCLModuleEditor2)editor;\r
         StyledText styledText = (StyledText)moduleEditor.getAdapter(Control.class);\r
-        String identifierAtCaret = extractAt(styledText.getText(), styledText.getCaretOffset());\r
-        if(identifierAtCaret.isEmpty())\r
-            return null;\r
-        SCLTextEditorEnvironment editorEnvironment = moduleEditor.getSCLTextEditorEnvironment();\r
-        editorEnvironment.updateEnvironment(moduleEditor.getDocument());\r
-        SCLValue value = editorEnvironment.getValue(identifierAtCaret);\r
-        if(value != null)\r
-            OpenSCLDefinition.openDefinition(value);\r
+        String text = styledText.getText();\r
+        int caretOffset = styledText.getCaretOffset();\r
+        \r
+        // Find the line where the caret is\r
+        String lineAtCaret = extractLineAt(text, caretOffset);\r
+        if(lineAtCaret.startsWith("import ") || lineAtCaret.startsWith("include ")) {\r
+            int p1 = lineAtCaret.indexOf('"', 6);\r
+            int p2 = lineAtCaret.indexOf('"', p1+1);\r
+            String moduleName = lineAtCaret.substring(p1+1, p2);\r
+            OpenSCLModule.openModule(moduleName);\r
+        }\r
+        else {\r
+            // Try to find an identifier at caret\r
+            String identifierAtCaret = extractIdentifierOrSymbolAt(text, caretOffset);\r
+            if(identifierAtCaret.isEmpty())\r
+                return null;\r
+            SCLTextEditorEnvironment editorEnvironment = moduleEditor.getSCLTextEditorEnvironment();\r
+            editorEnvironment.updateEnvironment(moduleEditor.getDocument());\r
+            SCLValue value = editorEnvironment.getValue(identifierAtCaret);\r
+            if(value != null)\r
+                OpenSCLDefinition.openDefinition(value);\r
+        }\r
         return null;\r
     }\r
 \r
index a9792a4d03339b49cd887232fc0a68537619979c..a0208e5b24d28f2dc0696f512062920af53e281a 100644 (file)
@@ -1,31 +1,35 @@
-package org.simantics.scl.ui.editor2;\r
-\r
-import org.eclipse.core.commands.AbstractHandler;\r
-import org.eclipse.core.commands.ExecutionEvent;\r
-import org.eclipse.core.commands.ExecutionException;\r
-import org.eclipse.ui.IWorkbenchPage;\r
-import org.eclipse.ui.PartInitException;\r
-import org.eclipse.ui.PlatformUI;\r
-\r
-public class OpenSCLModule extends AbstractHandler {\r
-\r
-    @Override\r
-    public Object execute(ExecutionEvent event) throws ExecutionException {\r
-        SCLModuleSelectionDialog dialog = new SCLModuleSelectionDialog(\r
-                PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());\r
-        if(dialog.open() == SCLModuleSelectionDialog.OK) {\r
-            String moduleName = (String)dialog.getFirstResult();\r
-            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();\r
-            if(page == null)\r
-                return null;\r
-            SCLModuleEditorInput input = new StandardSCLModuleEditorInput(moduleName);\r
-            try {\r
-                page.openEditor(input, "org.simantics.scl.ui.editor2");\r
-            } catch (PartInitException e) {\r
-                e.printStackTrace();\r
-            }\r
-        }\r
-        return null;\r
-    }\r
-\r
-}\r
+package org.simantics.scl.ui.editor2;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+
+public class OpenSCLModule extends AbstractHandler {
+
+    @Override
+    public Object execute(ExecutionEvent event) throws ExecutionException {
+        SCLModuleSelectionDialog dialog = new SCLModuleSelectionDialog(
+                PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
+        if(dialog.open() == SCLModuleSelectionDialog.OK) {
+            String moduleName = (String)dialog.getFirstResult();
+            openModule(moduleName);
+        }
+        return null;
+    }
+
+    public static void openModule(String moduleName) {
+        IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+        if(page == null)
+            return;
+        SCLModuleEditorInput input = new StandardSCLModuleEditorInput(moduleName);
+        try {
+            page.openEditor(input, "org.simantics.scl.ui.editor2");
+        } catch (PartInitException e) {
+            e.printStackTrace();
+        }
+    }
+
+}