| /*=============================================================================# |
| # Copyright (c) 2008, 2021 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.internal.r.debug.ui.launcher; |
| |
| import java.util.List; |
| |
| import org.eclipse.core.commands.AbstractHandler; |
| import org.eclipse.core.commands.ExecutionEvent; |
| import org.eclipse.core.commands.ExecutionException; |
| import org.eclipse.core.resources.IFile; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.jface.text.ITextSelection; |
| import org.eclipse.jface.viewers.ISelection; |
| import org.eclipse.jface.viewers.IStructuredSelection; |
| |
| import org.eclipse.statet.ecommons.text.TextUtil; |
| import org.eclipse.statet.ecommons.ui.workbench.WorkbenchUIUtils; |
| |
| import org.eclipse.statet.internal.r.debug.ui.RLaunchingMessages; |
| import org.eclipse.statet.ltk.model.core.element.SourceStructElement; |
| import org.eclipse.statet.ltk.ui.util.LTKSelectionUtils; |
| import org.eclipse.statet.r.core.refactoring.RRefactoringAdapter; |
| import org.eclipse.statet.r.launching.RCodeLaunching; |
| |
| |
| /** |
| * Launch shortcut, which submits |
| * - the current line/selection directly to R (text editor) |
| * - code of selected model element (outline etc.) |
| * and does not change the focus by default. |
| * |
| * Low requirements: ITextSelection is sufficient |
| */ |
| public class SubmitSelectionHandler extends AbstractHandler { |
| |
| |
| public static class AndGotoConsole extends SubmitSelectionHandler { |
| |
| |
| public AndGotoConsole() { |
| super(true); |
| } |
| |
| } |
| |
| |
| private final boolean fGotoConsole; |
| private RRefactoringAdapter fModelUtil; |
| |
| |
| public SubmitSelectionHandler() { |
| this(false); |
| } |
| |
| protected SubmitSelectionHandler(final boolean gotoConsole) { |
| fGotoConsole = gotoConsole; |
| } |
| |
| |
| @Override |
| public Object execute(final ExecutionEvent event) throws ExecutionException { |
| final ISelection selection = WorkbenchUIUtils.getCurrentSelection(event.getApplicationContext()); |
| |
| try { |
| // text selection |
| if (selection instanceof ITextSelection) { |
| final List<String> lines = LaunchShortcutUtil.getSelectedCodeLines(event); |
| if (lines != null) { |
| RCodeLaunching.runRCodeDirect(lines, fGotoConsole, null); |
| return null; |
| } |
| } |
| else |
| // selection of model elements |
| if (selection instanceof IStructuredSelection) { |
| final SourceStructElement[] elements = LTKSelectionUtils.getSelectedSourceStructElements(selection); |
| if (elements != null) { |
| if (fModelUtil == null) { |
| fModelUtil = new RSourceCodeAdapter(); |
| } |
| final List<String> lines = TextUtil.toLines( |
| fModelUtil.getSourceCodeStringedTogether(elements, null) ); |
| |
| RCodeLaunching.runRCodeDirect(lines, fGotoConsole, null); |
| return null; |
| } |
| |
| final IFile[] files = LTKSelectionUtils.getSelectedFiles(selection); |
| if (files != null) { |
| final int last = files.length-1; |
| for (int i = 0; i <= last; i++) { |
| final List<String> lines = LaunchShortcutUtil.getCodeLines(files[i]); |
| |
| RCodeLaunching.runRCodeDirect(lines, (i == last) && fGotoConsole, null); |
| } |
| return null; |
| } |
| } |
| } |
| catch (final CoreException e) { |
| LaunchShortcutUtil.handleRLaunchException(e, |
| RLaunchingMessages.RSelectionLaunch_error_message, event); |
| return null; |
| } |
| |
| LaunchShortcutUtil.handleUnsupportedExecution(event); |
| return null; |
| } |
| |
| } |