Search in sources :

Example 21 with Range

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

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

Example 23 with 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);
    }
}
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)

Example 24 with Range

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());
}
Also used : GetEditorContextEvent(org.rstudio.studio.client.events.GetEditorContextEvent) DocumentSelection(org.rstudio.studio.client.events.GetEditorContextEvent.DocumentSelection) Selection(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Selection) VoidServerRequestCallback(org.rstudio.studio.client.server.VoidServerRequestCallback) AceEditor(org.rstudio.studio.client.workbench.views.source.editors.text.AceEditor) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range) DocumentSelection(org.rstudio.studio.client.events.GetEditorContextEvent.DocumentSelection)

Example 25 with Range

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);
    }
}
Also used : AceFold(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceFold) InputEditorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range) Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint) 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)

Aggregations

Range (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)37 Position (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)11 JsArrayString (com.google.gwt.core.client.JsArrayString)7 ScheduledCommand (com.google.gwt.core.client.Scheduler.ScheduledCommand)7 Breakpoint (org.rstudio.studio.client.common.debugging.model.Breakpoint)5 Token (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token)5 ArrayList (java.util.ArrayList)4 AnchoredRange (org.rstudio.studio.client.workbench.views.source.editors.text.ace.AnchoredRange)4 Scope (org.rstudio.studio.client.workbench.views.source.editors.text.Scope)3 JsArray (com.google.gwt.core.client.JsArray)2 Command (com.google.gwt.user.client.Command)2 NativePreviewEvent (com.google.gwt.user.client.Event.NativePreviewEvent)2 NativePreviewHandler (com.google.gwt.user.client.Event.NativePreviewHandler)2 Handler (org.rstudio.core.client.command.Handler)2 EnsureHeightHandler (org.rstudio.core.client.events.EnsureHeightHandler)2 EnsureVisibleHandler (org.rstudio.core.client.events.EnsureVisibleHandler)2 Match (org.rstudio.core.client.regex.Match)2 ChangeFontSizeHandler (org.rstudio.studio.client.application.events.ChangeFontSizeHandler)2 FileChangeHandler (org.rstudio.studio.client.workbench.views.files.events.FileChangeHandler)2 HideMessageHandler (org.rstudio.studio.client.workbench.views.source.editors.text.status.StatusBar.HideMessageHandler)2