use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.
the class EditingTargetCodeExecution method getRangeFromBehavior.
private Range getRangeFromBehavior(String executionBehavior) {
Range range;
// by default the range can encompass the whole document
int startRowLimit = 0;
int endRowLimit = docDisplay_.getRowCount();
// limit range to chunk if we're inside one
Scope scope = docDisplay_.getCurrentChunk();
if (scope != null) {
startRowLimit = scope.getBodyStart().getRow();
endRowLimit = scope.getEnd().getRow() - 1;
}
if (executionBehavior == UIPrefsAccessor.EXECUTE_STATEMENT) {
// no scope to guard region, check the document itself to find
// the region to execute
range = docDisplay_.getMultiLineExpr(docDisplay_.getCursorPosition(), startRowLimit, endRowLimit);
} else if (executionBehavior == UIPrefsAccessor.EXECUTE_PARAGRAPH) {
range = docDisplay_.getParagraph(docDisplay_.getCursorPosition(), startRowLimit, endRowLimit);
} else {
// single-line execution
int row = docDisplay_.getCursorPosition().getRow();
range = Range.fromPoints(Position.create(row, 0), Position.create(row, docDisplay_.getLength(row)));
}
return range;
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.
the class EditingTargetCodeExecution method executeBehavior.
public void executeBehavior(String executionBehavior) {
Range range = getRangeFromBehavior(executionBehavior);
executeRange(range, null, false);
moveCursorAfterExecution(range);
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.
the class EditingTargetCodeExecution method executeSelection.
public void executeSelection(boolean consoleExecuteWhenNotFocused, boolean moveCursorAfter, String functionWrapper, boolean onlyUseConsole) {
// when executing LaTeX in R Markdown, show a popup preview
if (executeLatex(false))
return;
// when executing inline R code, show a popup preview
if (executeInlineChunk())
return;
Range selectionRange = docDisplay_.getSelectionRange();
boolean noSelection = selectionRange.isEmpty();
if (noSelection) {
// don't do multiline execution within Roxygen examples
int row = docDisplay_.getSelectionStart().getRow();
if (isRoxygenExampleRow(row)) {
selectionRange = Range.fromPoints(Position.create(row, 0), Position.create(row, docDisplay_.getLength(row)));
} else {
// if no selection, follow UI pref to see what to execute
selectionRange = getRangeFromBehavior(prefs_.executionBehavior().getValue());
}
// if we failed to discover a range, bail
if (selectionRange == null)
return;
// make it harder to step off the end of a chunk
InsertChunkInfo insert = docDisplay_.getInsertChunkInfo();
if (insert != null && !StringUtil.isNullOrEmpty(insert.getValue())) {
// get the selection we're about to execute; if it's the same as
// the last line of the chunk template, don't run it
String code = codeExtractor_.extractCode(docDisplay_, selectionRange);
String[] chunkLines = insert.getValue().split("\n");
if (!StringUtil.isNullOrEmpty(code) && chunkLines.length > 0 && code.trim() == chunkLines[chunkLines.length - 1].trim())
return;
}
}
executeRange(selectionRange, functionWrapper, onlyUseConsole);
// advance if there is no current selection
if (noSelection && moveCursorAfter) {
moveCursorAfterExecution(selectionRange);
}
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.
the class Source method getEditorContext.
private void getEditorContext(String id, String path, DocDisplay docDisplay) {
AceEditor editor = (AceEditor) docDisplay;
Selection selection = editor.getNativeSelection();
Range[] ranges = selection.getAllRanges();
JsArray<DocumentSelection> docSelections = JavaScriptObject.createArray().cast();
for (int i = 0; i < ranges.length; i++) {
docSelections.push(DocumentSelection.create(ranges[i], editor.getTextForRange(ranges[i])));
}
id = StringUtil.notNull(id);
path = StringUtil.notNull(path);
GetEditorContextEvent.SelectionData data = GetEditorContextEvent.SelectionData.create(id, path, editor.getCode(), docSelections);
server_.getEditorContextCompleted(data, new VoidServerRequestCallback());
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.
the class TextEditingTarget method onUnfold.
@Handler
void onUnfold() {
if (useScopeTreeFolding()) {
Range range = Range.fromPoints(docDisplay_.getSelectionStart(), docDisplay_.getSelectionEnd());
if (range.isEmpty()) {
// If no selection, either:
//
// 1) Unfold a fold containing the cursor, or
// 2) Unfold the closest fold on the current row.
Position pos = docDisplay_.getCursorPosition();
AceFold containingCandidate = null;
AceFold startCandidate = null;
AceFold endCandidate = null;
for (AceFold f : JsUtil.asIterable(docDisplay_.getFolds())) {
// Check to see whether this fold contains the cursor position.
if (f.getRange().contains(pos)) {
if (containingCandidate == null || containingCandidate.getRange().contains(f.getRange())) {
containingCandidate = f;
}
}
if (startCandidate == null && f.getStart().getRow() == pos.getRow() && f.getStart().getColumn() >= pos.getColumn()) {
startCandidate = f;
}
if (startCandidate == null && f.getEnd().getRow() == pos.getRow() && f.getEnd().getColumn() <= pos.getColumn()) {
endCandidate = f;
}
}
if (containingCandidate != null) {
docDisplay_.unfold(containingCandidate);
} else if (startCandidate == null ^ endCandidate == null) {
docDisplay_.unfold(startCandidate != null ? startCandidate : endCandidate);
} else if (startCandidate != null && endCandidate != null) {
// Both are candidates; see which is closer
int startDelta = startCandidate.getStart().getColumn() - pos.getColumn();
int endDelta = pos.getColumn() - endCandidate.getEnd().getColumn();
docDisplay_.unfold(startDelta <= endDelta ? startCandidate : endCandidate);
}
} else {
// If selection, unfold the selection
docDisplay_.unfold(range);
}
} else {
int row = docDisplay_.getSelectionStart().getRow();
docDisplay_.unfold(row);
}
}
Aggregations