use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Mode.InsertChunkInfo in project rstudio by rstudio.
the class TextEditingTarget method onInsertChunk.
private void onInsertChunk(String chunkPlaceholder, int rowOffset, int colOffset) {
String sel = null;
Range selRange = null;
// if currently in a chunk, add a blank line (for padding) and insert
// beneath it
Scope currentChunk = docDisplay_.getCurrentChunk();
if (currentChunk != null) {
// record current selection before manipulating text
sel = docDisplay_.getSelectionValue();
selRange = docDisplay_.getSelectionRange();
docDisplay_.setCursorPosition(currentChunk.getEnd());
docDisplay_.insertCode("\n");
docDisplay_.moveCursorForward(1);
}
Position pos = moveCursorToNextInsertLocation();
InsertChunkInfo insertChunkInfo = docDisplay_.getInsertChunkInfo();
if (insertChunkInfo != null) {
// inject the chunk skeleton
docDisplay_.insertCode(chunkPlaceholder, false);
// if we had text selected, inject it into the chunk
if (!StringUtil.isNullOrEmpty(sel)) {
Position contentPosition = insertChunkInfo.getContentPosition();
Position docContentPos = Position.create(pos.getRow() + contentPosition.getRow(), contentPosition.getColumn());
Position endPos = Position.create(docContentPos.getRow(), docContentPos.getColumn());
// move over newline if selected
if (sel.endsWith("\n"))
endPos.setRow(endPos.getRow() + 1);
docDisplay_.replaceRange(Range.fromPoints(docContentPos, endPos), sel);
docDisplay_.replaceRange(selRange, "");
}
Position cursorPosition = insertChunkInfo.getCursorPosition();
docDisplay_.setCursorPosition(Position.create(pos.getRow() + cursorPosition.getRow() + rowOffset, colOffset));
docDisplay_.focus();
} else {
assert false : "Mode did not have insertChunkInfo available";
}
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Mode.InsertChunkInfo in project rstudio by rstudio.
the class TextEditingTarget method onInsertChunk.
@Handler
void onInsertChunk() {
InsertChunkInfo info = docDisplay_.getInsertChunkInfo();
if (info == null)
return;
onInsertChunk(info.getValue(), 1, 0);
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Mode.InsertChunkInfo 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);
}
}
Aggregations