Search in sources :

Example 11 with Range

use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.

the class AceEditorIdleCommands method onPreviewLatex.

private void onPreviewLatex(TextEditingTarget target, DocUpdateSentinel sentinel, IdleState state) {
    Position position = resolvePosition(target.getDocDisplay(), state);
    Range range = MathJaxUtil.getLatexRange(target.getDocDisplay(), position);
    if (range == null)
        return;
    String pref = prefs_.showLatexPreviewOnCursorIdle().getValue();
    // document
    if (sentinel.getBoolProperty(TextEditingTargetNotebook.CONTENT_PREVIEW_ENABLED, pref != UIPrefsAccessor.LATEX_PREVIEW_SHOW_NEVER)) {
        target.renderLatex(range, true);
    }
}
Also used : Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)

Example 12 with Range

use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.

the class AceEditorWidget method updateBreakpoints.

private void updateBreakpoints(AceDocumentChangeEventNative changeEvent) {
    // if there are no breakpoints, don't do any work to move them about
    if (breakpoints_.size() == 0) {
        return;
    }
    // see if we need to move any breakpoints around in response to 
    // this change to the document's text
    String action = changeEvent.getAction();
    Range range = changeEvent.getRange();
    Position start = range.getStart();
    Position end = range.getEnd();
    // in a way that could change lines, we can't have moved anything
    if (start.getRow() == end.getRow() || (!action.equals("insertText") && !action.equals("insertLines") && !action.equals("removeText") && !action.equals("removeLines"))) {
        return;
    }
    int shiftedBy = 0;
    int shiftStartRow = 0;
    // compute how many rows to shift
    if (action == "insertText" || action == "insertLines") {
        shiftedBy = end.getRow() - start.getRow();
    } else {
        shiftedBy = start.getRow() - end.getRow();
    }
    // compute where to start shifting
    shiftStartRow = start.getRow() + ((action == "insertText" && start.getColumn() > 0) ? 1 : 0);
    // make a pass through the breakpoints and move them as appropriate:
    // remove all the breakpoints after the row where the change
    // happened, and add them back at their new position if they were
    // not part of a deleted range. 
    ArrayList<Breakpoint> movedBreakpoints = new ArrayList<Breakpoint>();
    for (int idx = 0; idx < breakpoints_.size(); idx++) {
        Breakpoint breakpoint = breakpoints_.get(idx);
        int breakpointRow = rowFromLine(breakpoint.getEditorLineNumber());
        if (breakpointRow >= shiftStartRow) {
            // remove the breakpoint from its old position
            movedBreakpoints.add(breakpoint);
            removeBreakpointMarker(breakpoint);
        }
    }
    for (Breakpoint breakpoint : movedBreakpoints) {
        // calculate the new position of the breakpoint
        int oldBreakpointPosition = rowFromLine(breakpoint.getEditorLineNumber());
        int newBreakpointPosition = oldBreakpointPosition + shiftedBy;
        // breakpoint there
        if (oldBreakpointPosition >= end.getRow() && !(oldBreakpointPosition == end.getRow() && shiftedBy < 0) && getBreakpointIdxByLine(lineFromRow(newBreakpointPosition)) < 0) {
            breakpoint.moveToLineNumber(lineFromRow(newBreakpointPosition));
            placeBreakpointMarker(breakpoint);
            fireEvent(new BreakpointMoveEvent(breakpoint.getBreakpointId()));
        } else {
            breakpoints_.remove(breakpoint);
            fireEvent(new BreakpointSetEvent(breakpoint.getEditorLineNumber(), breakpoint.getBreakpointId(), false));
        }
    }
}
Also used : Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) ArrayList(java.util.ArrayList) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range) AnchoredRange(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AnchoredRange) Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint)

Example 13 with Range

use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.

the class AceEditorWidget method removeMarkersAtCursorPosition.

public void removeMarkersAtCursorPosition() {
    // Defer this so other event handling can update anchors etc.
    Scheduler.get().scheduleDeferred(new ScheduledCommand() {

        @Override
        public void execute() {
            Position cursor = editor_.getCursorPosition();
            JsArray<AceAnnotation> newAnnotations = JsArray.createArray().cast();
            for (int i = 0; i < annotations_.size(); i++) {
                AnchoredAceAnnotation annotation = annotations_.get(i);
                int markerId = annotation.getMarkerId();
                Marker marker = editor_.getSession().getMarker(markerId);
                // a previous action.
                if (marker == null)
                    continue;
                Range range = marker.getRange();
                if (!range.contains(cursor))
                    newAnnotations.push(annotation.asAceAnnotation());
                else
                    editor_.getSession().removeMarker(markerId);
            }
            editor_.getSession().setAnnotations(newAnnotations);
            editor_.getRenderer().renderMarkers();
        }
    });
}
Also used : JsArray(com.google.gwt.core.client.JsArray) ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) Marker(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Marker) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range) AnchoredRange(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AnchoredRange)

Example 14 with Range

use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range 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 15 with Range

use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.

the class TextEditingTarget method doCommentUncomment.

// GWT emulation only provides isSpace
@SuppressWarnings("deprecation")
private void doCommentUncomment(String commentStart, String commentEnd) {
    Range initialRange = docDisplay_.getSelectionRange();
    int rowStart = initialRange.getStart().getRow();
    int rowEnd = initialRange.getEnd().getRow();
    boolean isSingleLineAction = rowStart == rowEnd;
    boolean commentWhitespace = commentEnd == null;
    // Also figure out if we're commenting an Roxygen block.
    boolean looksLikeRoxygen = false;
    // Skip commenting the last line if the selection is
    // multiline and ends on the first column of the end row.
    boolean dontCommentLastLine = false;
    if (rowStart != rowEnd && initialRange.getEnd().getColumn() == 0)
        dontCommentLastLine = true;
    Range expanded = Range.create(rowStart, 0, rowEnd, dontCommentLastLine ? 0 : docDisplay_.getLine(rowEnd).length());
    docDisplay_.setSelectionRange(expanded);
    String[] lines = JsUtil.toStringArray(docDisplay_.getLines(rowStart, rowEnd - (dontCommentLastLine ? 1 : 0)));
    String commonPrefix = StringUtil.getCommonPrefix(lines, true, true);
    String commonIndent = StringUtil.getIndent(commonPrefix);
    // First, figure out whether we're commenting or uncommenting.
    // If we discover any line that doesn't start with the comment sequence,
    // then we'll comment the whole selection.
    boolean isCommentAction = false;
    for (String line : lines) {
        String trimmed = line.trim();
        // Ignore lines that are just whitespace.
        if (!commentWhitespace && trimmed.isEmpty())
            continue;
        if (!isCommentAction) {
            if (!trimmed.startsWith(commentStart))
                isCommentAction = true;
        }
        if (docDisplay_.getFileType().isR()) {
            if (!looksLikeRoxygen) {
                if (trimmed.startsWith("@"))
                    looksLikeRoxygen = true;
                else if (trimmed.startsWith("#'"))
                    looksLikeRoxygen = true;
            }
        }
    }
    if (looksLikeRoxygen)
        commentStart += "'";
    // Now, construct a new, commented selection to replace with.
    StringBuilder builder = new StringBuilder();
    if (isCommentAction) {
        for (String line : lines) {
            String trimmed = line.trim();
            if (!commentWhitespace && trimmed.isEmpty()) {
                builder.append("\n");
                continue;
            }
            builder.append(commonIndent);
            builder.append(commentStart);
            builder.append(" ");
            builder.append(line.substring(commonIndent.length()));
            if (commentEnd != null) {
                builder.append(" ");
                builder.append(commentEnd);
            }
            builder.append("\n");
        }
    } else {
        for (String line : lines) {
            String trimmed = line.trim();
            if (trimmed.isEmpty()) {
                builder.append("\n");
                continue;
            }
            boolean isCommentedLine = true;
            int commentStartIdx = line.indexOf(commentStart);
            if (commentStartIdx == -1)
                isCommentedLine = false;
            int commentEndIdx = line.length();
            if (commentEnd != null) {
                commentEndIdx = line.lastIndexOf(commentEnd);
                if (commentEndIdx == -1)
                    isCommentedLine = false;
            }
            if (!isCommentedLine) {
                builder.append(line);
                continue;
            }
            // We want to strip out the leading comment prefix,
            // but preserve the indent.
            int startIdx = commentStartIdx + commentStart.length();
            if (Character.isSpace(line.charAt(startIdx)))
                startIdx++;
            int endIdx = commentEndIdx;
            String afterComment = line.substring(startIdx, endIdx);
            builder.append(StringUtil.trimRight(commonIndent + afterComment));
            builder.append("\n");
        }
    }
    String newSelection = dontCommentLastLine ? builder.toString() : builder.substring(0, builder.length() - 1);
    docDisplay_.replaceSelection(newSelection);
    // Nudge the selection to match the commented action.
    if (isSingleLineAction) {
        int diff = newSelection.length() - lines[0].length();
        if (commentEnd != null)
            diff = diff < 0 ? diff + commentEnd.length() + 1 : diff - commentEnd.length() - 1;
        int colStart = initialRange.getStart().getColumn();
        int colEnd = initialRange.getEnd().getColumn();
        Range newRange = Range.create(rowStart, colStart + diff, rowStart, colEnd + diff);
        docDisplay_.setSelectionRange(newRange);
    }
}
Also used : JsArrayString(com.google.gwt.core.client.JsArrayString) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range) Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint)

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