Search in sources :

Example 1 with InsertChunkInfo

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";
    }
}
Also used : InputEditorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) InsertChunkInfo(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Mode.InsertChunkInfo) JsArrayString(com.google.gwt.core.client.JsArrayString) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)

Example 2 with InsertChunkInfo

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);
}
Also used : InsertChunkInfo(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Mode.InsertChunkInfo) Handler(org.rstudio.core.client.command.Handler) ChangeFontSizeHandler(org.rstudio.studio.client.application.events.ChangeFontSizeHandler) RecordNavigationPositionHandler(org.rstudio.studio.client.workbench.views.source.events.RecordNavigationPositionHandler) EnsureHeightHandler(org.rstudio.core.client.events.EnsureHeightHandler) EnsureVisibleHandler(org.rstudio.core.client.events.EnsureVisibleHandler) HideMessageHandler(org.rstudio.studio.client.workbench.views.source.editors.text.status.StatusBar.HideMessageHandler) FileChangeHandler(org.rstudio.studio.client.workbench.views.files.events.FileChangeHandler)

Example 3 with InsertChunkInfo

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);
    }
}
Also used : InsertChunkInfo(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Mode.InsertChunkInfo) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)

Aggregations

InsertChunkInfo (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Mode.InsertChunkInfo)3 Range (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)2 JsArrayString (com.google.gwt.core.client.JsArrayString)1 Handler (org.rstudio.core.client.command.Handler)1 EnsureHeightHandler (org.rstudio.core.client.events.EnsureHeightHandler)1 EnsureVisibleHandler (org.rstudio.core.client.events.EnsureVisibleHandler)1 ChangeFontSizeHandler (org.rstudio.studio.client.application.events.ChangeFontSizeHandler)1 InputEditorPosition (org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition)1 FileChangeHandler (org.rstudio.studio.client.workbench.views.files.events.FileChangeHandler)1 Position (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)1 HideMessageHandler (org.rstudio.studio.client.workbench.views.source.editors.text.status.StatusBar.HideMessageHandler)1 RecordNavigationPositionHandler (org.rstudio.studio.client.workbench.views.source.events.RecordNavigationPositionHandler)1