From: Hannu Niemistö Date: Wed, 21 Sep 2016 11:28:31 +0000 (+0300) Subject: Open module when pressing F3 on import or include declaration X-Git-Tag: v1.25.0~101^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=8c3b0e2c8984ce7c9d61cdb1a47f199376702c86;p=simantics%2Fplatform.git Open module when pressing F3 on import or include declaration Change-Id: If53a968b8a29a7c6a18ae9f1e44bac06df13bbd4 --- diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/OpenDeclaration.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/OpenDeclaration.java index f8ea0c20d..522b8d3ed 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/OpenDeclaration.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/OpenDeclaration.java @@ -46,14 +46,27 @@ public class OpenDeclaration extends AbstractHandler { return text.substring(startPos, endPos); } - public static String extractAt(String text, int caretPos) { + public static String extractIdentifierOrSymbolAt(String text, int caretPos) { String result = extractIdentifierAt(text, caretPos); if(!result.isEmpty()) return result; return extractSymbolAt(text, caretPos); } - + private static String extractLineAt(String text, int caretPos) { + int startPos = caretPos; + while(startPos > 0 && !isNewline(text.charAt(startPos-1))) + --startPos; + int endPos = caretPos; + while(endPos < text.length() && !isNewline(text.charAt(endPos))) + ++endPos; + return text.substring(startPos, endPos); + } + + private static boolean isNewline(char c) { + return c=='\n' || c=='\r'; + } + @Override public Object execute(ExecutionEvent event) throws ExecutionException { IEditorPart editor = @@ -62,14 +75,28 @@ public class OpenDeclaration extends AbstractHandler { return null; SCLModuleEditor2 moduleEditor = (SCLModuleEditor2)editor; StyledText styledText = (StyledText)moduleEditor.getAdapter(Control.class); - String identifierAtCaret = extractAt(styledText.getText(), styledText.getCaretOffset()); - if(identifierAtCaret.isEmpty()) - return null; - SCLTextEditorEnvironment editorEnvironment = moduleEditor.getSCLTextEditorEnvironment(); - editorEnvironment.updateEnvironment(moduleEditor.getDocument()); - SCLValue value = editorEnvironment.getValue(identifierAtCaret); - if(value != null) - OpenSCLDefinition.openDefinition(value); + String text = styledText.getText(); + int caretOffset = styledText.getCaretOffset(); + + // Find the line where the caret is + String lineAtCaret = extractLineAt(text, caretOffset); + if(lineAtCaret.startsWith("import ") || lineAtCaret.startsWith("include ")) { + int p1 = lineAtCaret.indexOf('"', 6); + int p2 = lineAtCaret.indexOf('"', p1+1); + String moduleName = lineAtCaret.substring(p1+1, p2); + OpenSCLModule.openModule(moduleName); + } + else { + // Try to find an identifier at caret + String identifierAtCaret = extractIdentifierOrSymbolAt(text, caretOffset); + if(identifierAtCaret.isEmpty()) + return null; + SCLTextEditorEnvironment editorEnvironment = moduleEditor.getSCLTextEditorEnvironment(); + editorEnvironment.updateEnvironment(moduleEditor.getDocument()); + SCLValue value = editorEnvironment.getValue(identifierAtCaret); + if(value != null) + OpenSCLDefinition.openDefinition(value); + } return null; } diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/OpenSCLModule.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/OpenSCLModule.java index a9792a4d0..a0208e5b2 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/OpenSCLModule.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/OpenSCLModule.java @@ -1,31 +1,35 @@ -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(); - IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); - if(page == null) - return null; - SCLModuleEditorInput input = new StandardSCLModuleEditorInput(moduleName); - try { - page.openEditor(input, "org.simantics.scl.ui.editor2"); - } catch (PartInitException e) { - e.printStackTrace(); - } - } - return null; - } - -} +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(); + } + } + +}