| /*=============================================================================# |
| # Copyright (c) 2005, 2020 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.io.BufferedReader; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.io.InputStreamReader; |
| import java.net.URI; |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| import org.eclipse.core.commands.ExecutionEvent; |
| import org.eclipse.core.resources.IFile; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.core.runtime.Platform; |
| import org.eclipse.core.runtime.Status; |
| import org.eclipse.core.runtime.URIUtil; |
| import org.eclipse.core.runtime.content.IContentType; |
| import org.eclipse.jface.text.BadLocationException; |
| import org.eclipse.jface.text.Document; |
| import org.eclipse.jface.text.IDocument; |
| import org.eclipse.jface.text.IRegion; |
| import org.eclipse.jface.text.ITextSelection; |
| import org.eclipse.jface.viewers.ISelection; |
| import org.eclipse.ui.IEditorInput; |
| import org.eclipse.ui.IFileEditorInput; |
| import org.eclipse.ui.IURIEditorInput; |
| import org.eclipse.ui.IWorkbenchPart; |
| import org.eclipse.ui.handlers.HandlerUtil; |
| import org.eclipse.ui.ide.IDE; |
| import org.eclipse.ui.statushandlers.StatusManager; |
| import org.eclipse.ui.texteditor.IDocumentProvider; |
| import org.eclipse.ui.texteditor.ITextEditor; |
| |
| 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.ui.sourceediting.ISourceEditor; |
| import org.eclipse.statet.ltk.ui.util.LTKWorkbenchUIUtil; |
| import org.eclipse.statet.r.launching.ICodeSubmitContentHandler; |
| import org.eclipse.statet.r.launching.RCodeLaunching; |
| import org.eclipse.statet.r.ui.RUI; |
| |
| |
| public class LaunchShortcutUtil { |
| |
| |
| public static final String TOGGLE_ECHO_COMMAND_ID = "org.eclipse.statet.r.commands.ToggleRunEcho"; //$NON-NLS-1$ |
| |
| public static String getContentTypeId(final IFile file) { |
| final IContentType contentType = IDE.guessContentType(file); |
| return (contentType != null) ? contentType.getId() : null; |
| } |
| |
| public static String getContentTypeId(final URI uri) { |
| final String fileName = URIUtil.lastSegment(uri); |
| if (fileName != null) { |
| final IContentType contentType = Platform.getContentTypeManager().findContentTypeFor(fileName); |
| return (contentType != null) ? contentType.getId() : null; |
| } |
| return null; |
| } |
| |
| public static List<String> getCodeLines(final IFile file) throws CoreException { |
| InputStream input = null; |
| try { |
| final String charset; |
| try { |
| input = file.getContents(); |
| charset = file.getCharset(); |
| } |
| catch (final CoreException e) { |
| throw new CoreException(new Status(IStatus.ERROR, RUI.BUNDLE_ID, 0, |
| RLaunchingMessages.SubmitCode_error_WhenAnalyzingAndCollecting_message, |
| e )); |
| } |
| try { |
| final BufferedReader reader = new BufferedReader(new InputStreamReader(input, charset)); |
| final StringBuilder buffer = new StringBuilder(); |
| final char[] readBuffer = new char[2048]; |
| int n; |
| while ((n = reader.read(readBuffer)) > 0) { |
| buffer.append(readBuffer, 0, n); |
| } |
| |
| final IContentType contentType= IDE.getContentType(file); |
| final ICodeSubmitContentHandler handler = RCodeLaunching.getCodeSubmitContentHandler( |
| (contentType != null) ? contentType.getId() : null ); |
| |
| final Document document= new Document(buffer.toString()); |
| handler.setup(document); |
| return handler.getCodeLines(document); |
| } |
| catch (final CoreException e) { |
| throw new CoreException(new Status(IStatus.ERROR, RUI.BUNDLE_ID, 0, |
| RLaunchingMessages.SubmitCode_error_WhenAnalyzingAndCollecting_message, |
| e )); |
| } |
| catch (final IOException e) { |
| throw new CoreException(new Status(IStatus.ERROR, RUI.BUNDLE_ID, 0, |
| RLaunchingMessages.SubmitCode_error_WhenAnalyzingAndCollecting_message, |
| e )); |
| } |
| catch (final BadLocationException e) { |
| throw new CoreException(new Status(IStatus.ERROR, RUI.BUNDLE_ID, 0, |
| RLaunchingMessages.SubmitCode_error_WhenAnalyzingAndCollecting_message, |
| e )); |
| } |
| } |
| finally { |
| if (input != null) { |
| try { |
| input.close(); |
| } catch (final IOException ignore) {} |
| } |
| } |
| } |
| |
| public static List<String> getSelectedCodeLines(final ExecutionEvent event) throws CoreException { |
| try { |
| final ISelection selection = WorkbenchUIUtils.getCurrentSelection(event.getApplicationContext()); |
| final IWorkbenchPart workbenchPart = HandlerUtil.getActivePart(event); |
| if (selection instanceof ITextSelection) { |
| final ITextSelection textSelection = (ITextSelection) selection; |
| if (textSelection.getLength() > 0) { |
| final String code = textSelection.getText(); |
| if (code != null) { |
| final ArrayList<String> lines= new ArrayList<>(2 + code.length()/30); |
| TextUtil.addLines(code, lines); |
| return lines; |
| } |
| } |
| IDocument document = null; |
| if (workbenchPart instanceof ITextEditor) { |
| final ITextEditor editor = (ITextEditor) workbenchPart; |
| final IDocumentProvider documentProvider = editor.getDocumentProvider(); |
| if (documentProvider != null) { |
| document = documentProvider.getDocument(editor.getEditorInput()); |
| } |
| } |
| if (document == null) { |
| final ISourceEditor editor = workbenchPart.getAdapter(ISourceEditor.class); |
| if (editor != null) { |
| document = editor.getViewer().getDocument(); |
| } |
| } |
| if (document != null) { |
| final ArrayList<String> lines= new ArrayList<>( |
| document.getNumberOfLines(textSelection.getOffset(), textSelection.getLength()) ); |
| if (textSelection.getLength() > 0) { |
| TextUtil.addLines(document, textSelection.getOffset(), textSelection.getLength(), lines); |
| } |
| else { |
| final int line = document.getLineOfOffset(textSelection.getOffset()); |
| final IRegion lineInformation = document.getLineInformation(line); |
| lines.add(document.get(lineInformation.getOffset(), lineInformation.getLength())); |
| } |
| return lines; |
| } |
| } |
| return null; |
| } |
| catch (final BadLocationException e) { |
| throw new CoreException(new Status(IStatus.ERROR, RUI.BUNDLE_ID, -1, |
| RLaunchingMessages.SubmitCode_error_WhenAnalyzingAndCollecting_message, e)); |
| } |
| } |
| |
| public static Object getFile(final IEditorInput editorInput) { |
| if (editorInput instanceof IFileEditorInput) { |
| return ((IFileEditorInput) editorInput).getFile(); |
| } |
| if (editorInput instanceof IURIEditorInput) { |
| return ((IURIEditorInput) editorInput).getURI(); |
| } |
| return null; |
| } |
| |
| public static IStatus createUnsupported() { |
| return new Status(IStatus.ERROR, RUI.BUNDLE_ID, |
| RLaunchingMessages.SubmitCode_info_NotSupported_message ); |
| } |
| |
| public static void handleUnsupportedExecution(final ExecutionEvent executionEvent) { |
| LTKWorkbenchUIUtil.indicateStatus(createUnsupported(), executionEvent); |
| } |
| |
| public static void handleRLaunchException(final Throwable e, final String defaultMessage, final ExecutionEvent executionEvent) { |
| final Status status= new Status(IStatus.ERROR, RUI.BUNDLE_ID, 0, defaultMessage, e); |
| StatusManager.getManager().handle(status); |
| if (e instanceof CoreException) { |
| LTKWorkbenchUIUtil.indicateStatus(((CoreException) e).getStatus(), executionEvent); |
| } |
| else { |
| LTKWorkbenchUIUtil.indicateStatus(status, executionEvent); |
| } |
| } |
| |
| |
| private LaunchShortcutUtil() {} |
| |
| } |