Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
148 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
plugins/net.bioclipse.r.ui/src/net/bioclipse/r/ui/util/RunUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
plugins/net.bioclipse.r.ui/src/net/bioclipse/r/ui/util/WorkbenchUIUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |