Search in sources :

Example 1 with Range

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

the class MathJaxUtil method findLatexChunks.

public static List<Range> findLatexChunks(DocDisplay docDisplay) {
    docDisplay.tokenizeDocument();
    List<Range> ranges = new ArrayList<Range>();
    Position startPos = null;
    for (int i = 0, n = docDisplay.getRowCount(); i < n; i++) {
        Position pos = Position.create(i, 0);
        Token token = docDisplay.getTokenAt(Position.create(i, 0));
        if (token == null)
            continue;
        if (token.hasAllTypes("latex", "begin") && token.getValue().equals("$$")) {
            startPos = pos;
            // get the length of this line to see if it could be an inline
            // LaTeX chunk (e.g. $$ x = y $$)
            int length = docDisplay.getLength(i);
            if (length < 5)
                continue;
            // get the last token on the row; if it's a LaTeX end token then
            // consider the row to be an inline LaTeX chunk
            Token endLineToken = docDisplay.getTokenAt(Position.create(i, docDisplay.getLength(i)));
            if (endLineToken != null && endLineToken.hasAllTypes("latex", "end")) {
                ranges.add(Range.fromPoints(startPos, Position.create(i, length)));
            }
            continue;
        }
        if (token.hasAllTypes("latex", "end") && token.getValue().equals("$$")) {
            ranges.add(Range.fromPoints(startPos, Position.create(i, 2)));
            continue;
        }
    }
    return ranges;
}
Also used : Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) ArrayList(java.util.ArrayList) Token(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)

Example 2 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 3 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 4 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 5 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)

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