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.swt.custom.StyledText; import org.eclipse.swt.widgets.Control; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.PlatformUI; import org.simantics.scl.compiler.elaboration.modules.SCLValue; import org.simantics.scl.ui.editor.completion.SCLTextEditorEnvironment; public class OpenDeclaration extends AbstractHandler { private static boolean isIdentifierPart(char c) { return Character.isJavaIdentifierPart(c) || c=='.'; } private static String extractIdentifierAt(String text, int caretPos) { int startPos = caretPos; while(startPos > 0 && isIdentifierPart(text.charAt(startPos-1))) --startPos; int endPos = caretPos; while(endPos < text.length() && isIdentifierPart(text.charAt(endPos))) ++endPos; return text.substring(startPos, endPos); } private static final String SYMBOL_CHARS = "!$%&*+/<=>?@\\^|-:~."; private static boolean isSymbolPart(char c) { for(int i=0;i 0 && isSymbolPart(text.charAt(startPos-1))) --startPos; int endPos = caretPos; while(endPos < text.length() && isSymbolPart(text.charAt(endPos))) ++endPos; return text.substring(startPos, endPos); } public static String extractAt(String text, int caretPos) { String result = extractIdentifierAt(text, caretPos); if(!result.isEmpty()) return result; return extractSymbolAt(text, caretPos); } @Override public Object execute(ExecutionEvent event) throws ExecutionException { IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); if(!(editor instanceof SCLModuleEditor2)) 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); return null; } }