Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add "Run current line" functionality to the pop-up menu
"Run selected R snippet".
If nothing is selected right-click and "Run current line/selected R snippet" executes the content of the current line.
This way one can run the code in the line without the need to select.
  • Loading branch information
Valentin Georgiev committed Feb 29, 2012
1 parent d43edbd commit fdf24ea
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 14 deletions.
1 change: 1 addition & 0 deletions plugins/net.bioclipse.r.ui/META-INF/MANIFEST.MF
Expand Up @@ -21,6 +21,7 @@ Bundle-ActivationPolicy: lazy
Export-Package: net.bioclipse.r.ui,
net.bioclipse.r.ui.views
Import-Package: org.apache.log4j,
org.eclipse.core.expressions,
org.slf4j
Bundle-Vendor: The Bioclipse Team
Bundle-ClassPath: .
2 changes: 1 addition & 1 deletion plugins/net.bioclipse.r.ui/plugin.xml
Expand Up @@ -63,7 +63,7 @@
locationURI="popup:org.eclipse.ui.popup.any?after=additions">
<command
commandId="net.bioclipse.r.ui.runRSnippet"
label="Run selected R snippet">
label="Run current line/selected R snippet">
<visibleWhen>
<iterate operator="and" ifEmpty="false">
<adapt type="org.eclipse.jface.text.ITextSelection" />
Expand Down
@@ -1,15 +1,14 @@
package net.bioclipse.r.ui.handlers;

import net.bioclipse.r.ui.util.RunUtil;
import net.bioclipse.r.ui.views.RConsoleView;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;

/**
* Handler to execute a Text Selection in R
Expand All @@ -21,16 +20,13 @@ public class RunRSnippetHandler extends AbstractHandler implements IHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {

ISelection selection = HandlerUtil.getCurrentSelection(event);
if (!(selection instanceof ITextSelection)) return null;

ITextSelection textsel = (ITextSelection) selection;
System.out.println("You selected text: \n" + textsel.getText());

RConsoleView rView = (RConsoleView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("net.bioclipse.r.ui.views.RConsoleView");
rView.execEditorInpit(textsel.getText());
//We are done
try {
RConsoleView rView = (RConsoleView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("net.bioclipse.r.ui.views.RConsoleView");
String code = RunUtil.getSelectedCode(event);
rView.execEditorInpit(code);
} catch (CoreException e) {
e.printStackTrace();
}
return null;
}

Expand Down
@@ -0,0 +1,97 @@
/**
* Util methods for running code from the R editor
*
* @author valyo
*
*/

package net.bioclipse.r.ui.util;

import net.bioclipse.r.ui.editors.REditor;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.BadLocationException;
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.IEditorPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.texteditor.ITextEditor;

public class RunUtil {

public static final String fileseparator = java.io.File.separator;

public static String getSelectedCode(final ExecutionEvent event) throws CoreException {
try {
final ISelection selection = WorkbenchUIUtil.getCurrentSelection(event.getApplicationContext());
final IWorkbenchPart workbenchPart = HandlerUtil.getActivePart(event);
if (selection instanceof ITextSelection) {
final ITextSelection textsel = (ITextSelection) selection;
if (textsel.getLength() > 0) {
System.out.println("You selected text: \n" + textsel.getText());
final String code = textsel.getText();
if (code != null) {
return code;
}
}
else {
IDocument document = null;
if (workbenchPart instanceof ITextEditor) {
final ITextEditor editor = (ITextEditor) workbenchPart;
if (!(editor instanceof REditor))
return null;
REditor reditor = (REditor) editor;
document = reditor.getDocumentProvider().getDocument(reditor.getEditorInput());
}
int line = document.getLineOfOffset(textsel.getOffset());
final IRegion lineInformation = document.getLineInformation(line);
String code = document.get(lineInformation.getOffset(),lineInformation.getLength());
return code;
}
}
}catch (BadLocationException e) {
// TODO: handle exception
throw new CoreException(new Status(IStatus.ERROR, "net.bioclipse.r.ui", "An error occured when collecting the code to be run", e));
}
return null;
}

public static String getContent(ExecutionEvent event) {
IEditorPart editor = HandlerUtil.getActiveEditor(event);
if (!(editor instanceof REditor)) return null;
REditor reditor = (REditor)editor;

IDocument doc = reditor.getDocumentProvider().getDocument(reditor.getEditorInput());
String contents = doc.get();

System.out.println("Editor content: \n" + contents);
return contents;
}

public static String getFilePath() {
IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (!(editor instanceof REditor)) return null;
REditor reditor = (REditor) editor;

//Check the editor state and get the file path
String filepath = reditor.getFilePath();
System.out.println("File path is: " + filepath);

//Get the file path with correct file separator
filepath = RunUtil.fixFilepath(filepath);
return filepath;
}

public static String fixFilepath(String filepath) {
filepath = filepath.replace(fileseparator, "/");
return filepath;
}

}
@@ -0,0 +1,40 @@
/**
* Util methods for Bioclipse workbench
*
* @author valyo
*
*/

package net.bioclipse.r.ui.util;

import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.ui.ISources;
import org.eclipse.ui.IWorkbenchSite;

public class WorkbenchUIUtil {

public static ISelection getCurrentSelection(final Object context) {
if (context instanceof IEvaluationContext) {
final IEvaluationContext evaluationContext = (IEvaluationContext) context;
Object object = evaluationContext.getVariable(ISources.ACTIVE_SITE_NAME);
if (object instanceof IWorkbenchSite) {
final IWorkbenchSite site = (IWorkbenchSite) object;
final ISelectionProvider selectionProvider = site.getSelectionProvider();
if (selectionProvider != null) {
return selectionProvider.getSelection();
}
return null;
}
else {
object = evaluationContext.getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);
if (object instanceof ISelection) {
return (ISelection) object;
}
}
}
return null;
}

}

0 comments on commit fdf24ea

Please sign in to comment.