Search in sources :

Example 51 with Position

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

the class TextEditingTargetLatexFormatMenu method createLatexCommand.

private Command createLatexCommand(final String prefix, final String suffix, final boolean isSectionCommand) {
    return new Command() {

        @Override
        public void execute() {
            String selection = editor_.getSelectionValue();
            // modify prefix based on prefs
            String insertPrefix = prefix;
            if (isSectionCommand && prefs_.insertNumberedLatexSections().getValue()) {
                insertPrefix = insertPrefix.replace("*", "");
            }
            editor_.insertCode(insertPrefix + selection + suffix, false);
            editor_.focus();
            // inside the braces
            if (selection.length() == 0) {
                Position pos = editor_.getCursorPosition();
                int row = pos.getRow();
                if (suffix.startsWith("\n"))
                    row = Math.max(0, row - 1);
                int col = Math.max(0, pos.getColumn() - suffix.length());
                editor_.setCursorPosition(Position.create(row, col));
            }
        }
    };
}
Also used : Command(com.google.gwt.user.client.Command) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)

Example 52 with Position

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

the class RCompletionToolTip method onLoad.

@Override
protected void onLoad() {
    super.onLoad();
    if (nativePreviewReg_ != null) {
        nativePreviewReg_.removeHandler();
        nativePreviewReg_ = null;
    }
    nativePreviewReg_ = Event.addNativePreviewHandler(new NativePreviewHandler() {

        public void onPreviewNativeEvent(NativePreviewEvent e) {
            int eventType = e.getTypeInt();
            if (eventType == Event.ONKEYDOWN || eventType == Event.ONMOUSEDOWN) {
                // dismiss if we've left our anchor zone
                // (defer this so the current key has a chance to 
                // enter the editor and affect the cursor)
                Scheduler.get().scheduleDeferred(new ScheduledCommand() {

                    @Override
                    public void execute() {
                        Position cursorPos = docDisplay_.getCursorPosition();
                        if (anchor_ != null) {
                            Range anchorRange = anchor_.getRange();
                            if (cursorPos.isBeforeOrEqualTo(anchorRange.getStart()) || cursorPos.isAfterOrEqualTo(anchorRange.getEnd())) {
                                anchor_.detach();
                                anchor_ = null;
                                hide();
                            }
                        }
                    }
                });
            }
        }
    });
}
Also used : NativePreviewHandler(com.google.gwt.user.client.Event.NativePreviewHandler) NativePreviewEvent(com.google.gwt.user.client.Event.NativePreviewEvent) ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand) 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 53 with Position

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

the class SignatureToolTipManager method resolveActiveFunctionAndDisplayToolTip.

public void resolveActiveFunctionAndDisplayToolTip() {
    if (suppressed_) {
        suppressed_ = false;
        return;
    }
    if (docDisplay_.isPopupVisible())
        return;
    AceEditor editor = (AceEditor) docDisplay_;
    if (editor == null)
        return;
    final Position position = getLookupPosition();
    final boolean isMouseEvent = isMouseDrivenEvent();
    // Ensure that the mouse target is actually the active editor
    if (isMouseEvent) {
        Element el = DomUtils.elementFromPoint(coordinates_.getMouseX(), coordinates_.getMouseY());
        AceEditorNative nativeEditor = AceEditorNative.getEditor(el);
        if (nativeEditor != null && nativeEditor != editor.getWidget().getEditor())
            return;
    }
    // and then later moves the mouse away)
    if (isMouseEvent)
        toolTip_.hide();
    TokenCursor cursor = editor.getSession().getMode().getRCodeModel().getTokenCursor();
    if (!cursor.moveToPosition(position, true))
        return;
    // position is not changed.
    if (!isMouseEvent && uiPrefs_.showFunctionTooltipOnIdle().getGlobalValue() && !cursor.valueEquals("(")) {
        cursor.findOpeningBracket("(", false);
    }
    Token lookahead = cursor.peekFwd(1);
    if (lookahead.valueEquals("::") || lookahead.valueEquals(":::"))
        if (!cursor.moveToNextToken())
            return;
    if (cursor.valueEquals("::") || cursor.valueEquals(":::"))
        if (!cursor.moveToNextToken())
            return;
    if (cursor.valueEquals("("))
        if (!cursor.moveToPreviousToken())
            return;
    // then bail.
    if (toolTip_.isShowing() && cursor.currentPosition().isEqualTo(completionPosition_)) {
        return;
    }
    completionPosition_ = cursor.currentPosition();
    // be an opening paren.
    if (!cursor.hasType("identifier"))
        return;
    if (!cursor.nextValue().equals("("))
        return;
    String callString = cursor.currentValue();
    if (isBoringFunction(callString))
        return;
    // If this is a namespaced function call, then append that context.
    Token lookbehind = cursor.peekBwd(1);
    if (lookbehind.valueEquals("::") || lookbehind.valueEquals(":::")) {
        // Do-while loop just to allow 'break' for control flow
        do {
            TokenCursor clone = cursor.cloneCursor();
            if (!clone.moveToPreviousToken())
                break;
            if (!clone.moveToPreviousToken())
                break;
            if (!clone.hasType("identifier"))
                break;
            callString = clone.currentValue() + "::" + callString;
        } while (false);
    }
    // Set anchor (so we can dismiss popup when cursor moves outside
    // of anchored region)
    setAnchor(cursor.cloneCursor());
    final String fnString = callString;
    server_.getArgs(fnString, "", "", new ServerRequestCallback<String>() {

        @Override
        public void onResponseReceived(String arguments) {
            if (StringUtil.isNullOrEmpty(arguments))
                return;
            final String signature = fnString + arguments;
            resolvePositionAndShow(signature, position);
        }

        @Override
        public void onError(ServerError error) {
            Debug.logError(error);
        }
    });
}
Also used : AceEditorNative(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceEditorNative) TokenCursor(org.rstudio.studio.client.workbench.views.source.editors.text.ace.TokenCursor) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) ServerError(org.rstudio.studio.client.server.ServerError) Element(com.google.gwt.dom.client.Element) AceEditor(org.rstudio.studio.client.workbench.views.source.editors.text.AceEditor) Token(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token)

Example 54 with Position

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

the class TextEditingTargetRenameHelper method renameVariablesInScope.

private int renameVariablesInScope(Scope scope, Position startPos, String targetValue, String targetType) {
    Position endPos = scope.getEnd();
    if (endPos == null)
        endPos = Position.create(editor_.getSession().getLength(), 0);
    TokenCursor cursor = editor_.getSession().getMode().getCodeModel().getTokenCursor();
    cursor.moveToPosition(startPos, true);
    // Workaround 'moveToPosition' not handling forward searches (yet)
    if (cursor.getRow() < startPos.getRow())
        if (!cursor.moveToNextToken())
            return 0;
    // NOTE: The token associated with the current cursor position
    // is added last, to ensure that after the 'multi-select' session has
    // ended the cursor remains where it started.
    Position cursorPos = editor_.getCursorPosition();
    do {
        // Left brackets push on the stack.
        if (cursor.isLeftBracket()) {
            // Update state.
            if (cursor.valueEquals("(")) {
                if (cursor.peekBwd(1).valueEquals("function"))
                    pushState(STATE_FUNCTION_DEFINITION);
                else
                    pushState(STATE_FUNCTION_CALL);
            } else {
                pushState(STATE_DEFAULT);
            }
            // Update protected names for braces.
            if (cursor.valueEquals("{"))
                pushProtectedNames(cursor.currentPosition(), scope);
            continue;
        }
        // Right brackets pop the stack.
        if (cursor.isRightBracket()) {
            popState();
            if (cursor.valueEquals("}"))
                popProtectedNames(cursor.currentPosition());
            continue;
        }
        // Protect a name if it's the target of an assignment in a child scope.
        if (cursor.hasType("identifier") && cursor.peekFwd(1).isLeftAssign() && !cursor.peekBwd(1).isExtractionOperator()) {
            Scope candidate = editor_.getScopeAtPosition(cursor.currentPosition());
            // Skip default arguments for nested functions
            if (peekState() == STATE_FUNCTION_DEFINITION && scope != candidate)
                continue;
            if (cursor.peekFwd(2).valueEquals("function") && !candidate.isTopLevel())
                candidate = candidate.getParentScope();
            if (candidate != scope) {
                addProtectedName(cursor.currentValue());
                continue;
            }
        }
        // Bail if we've reached the end of the scope.
        if (cursor.currentPosition().isAfterOrEqualTo(endPos))
            break;
        if (cursor.currentValue().equals(targetValue)) {
            // either as assignments, or exist as names to newly defined functions.
            if (isProtectedName(cursor.currentValue()))
                continue;
            // Skip variables following an 'extraction' operator.
            if (cursor.peekBwd(1).isExtractionOperator())
                continue;
            // Skip default arguments for nested functions
            Scope candidate = editor_.getScopeAtPosition(cursor.currentPosition());
            if (peekState() == STATE_FUNCTION_DEFINITION && scope != candidate)
                continue;
            //    ~~~    ~~~                ~~~
            if (peekState() == STATE_FUNCTION_CALL && cursor.nextValue().equals("="))
                continue;
            //                    ~~~    ~~~    ~~~
            if (peekState() == STATE_FUNCTION_DEFINITION && editor_.getScopeAtPosition(cursor.currentPosition()) != scope) {
                String prevValue = cursor.peekBwd(1).getValue();
                if (prevValue.equals("(") || prevValue.equals(",") || prevValue.equals("=")) {
                    continue;
                }
            }
            Range tokenRange = getTokenRange(cursor);
            if (!tokenRange.contains(cursorPos))
                ranges_.add(tokenRange);
        }
    } while (cursor.moveToNextToken());
    // after exiting 'multi-select' mode)
    if (cursor.moveToPosition(cursorPos, true))
        ranges_.add(getTokenRange(cursor));
    return applyRanges();
}
Also used : TokenCursor(org.rstudio.studio.client.workbench.views.source.editors.text.ace.TokenCursor) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) JsArrayString(com.google.gwt.core.client.JsArrayString) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)

Example 55 with Position

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

the class TextEditingTargetRenameHelper method getTokenRange.

private Range getTokenRange(TokenCursor cursor) {
    Position startPos = cursor.currentPosition();
    Position endPos = Position.create(startPos.getRow(), startPos.getColumn() + cursor.currentValue().length());
    return Range.fromPoints(startPos, endPos);
}
Also used : Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)

Aggregations

Position (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)64 InputEditorPosition (org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition)20 JsArrayString (com.google.gwt.core.client.JsArrayString)16 Breakpoint (org.rstudio.studio.client.common.debugging.model.Breakpoint)11 Range (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)11 ScheduledCommand (com.google.gwt.core.client.Scheduler.ScheduledCommand)7 FileChangeHandler (org.rstudio.studio.client.workbench.views.files.events.FileChangeHandler)7 TokenCursor (org.rstudio.studio.client.workbench.views.source.editors.text.ace.TokenCursor)7 SourcePosition (org.rstudio.studio.client.workbench.views.source.model.SourcePosition)7 Handler (org.rstudio.core.client.command.Handler)6 EnsureHeightHandler (org.rstudio.core.client.events.EnsureHeightHandler)6 EnsureVisibleHandler (org.rstudio.core.client.events.EnsureVisibleHandler)6 ChangeFontSizeHandler (org.rstudio.studio.client.application.events.ChangeFontSizeHandler)6 ServerError (org.rstudio.studio.client.server.ServerError)6 AceEditor (org.rstudio.studio.client.workbench.views.source.editors.text.AceEditor)6 Token (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token)6 HideMessageHandler (org.rstudio.studio.client.workbench.views.source.editors.text.status.StatusBar.HideMessageHandler)6 RecordNavigationPositionHandler (org.rstudio.studio.client.workbench.views.source.events.RecordNavigationPositionHandler)6 InputEditorLineWithCursorPosition (org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorLineWithCursorPosition)4 CodeModel (org.rstudio.studio.client.workbench.views.source.editors.text.ace.CodeModel)4