Search in sources :

Example 11 with TokenCursor

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

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

Aggregations

TokenCursor (org.rstudio.studio.client.workbench.views.source.editors.text.ace.TokenCursor)12 Position (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)7 AceEditor (org.rstudio.studio.client.workbench.views.source.editors.text.AceEditor)6 JsArrayString (com.google.gwt.core.client.JsArrayString)5 CodeModel (org.rstudio.studio.client.workbench.views.source.editors.text.ace.CodeModel)4 ServerError (org.rstudio.studio.client.server.ServerError)2 InputEditorLineWithCursorPosition (org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorLineWithCursorPosition)2 ScopeFunction (org.rstudio.studio.client.workbench.views.source.editors.text.ScopeFunction)2 Token (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token)2 SourcePosition (org.rstudio.studio.client.workbench.views.source.model.SourcePosition)2 Element (com.google.gwt.dom.client.Element)1 Stack (java.util.Stack)1 FileSystemItem (org.rstudio.core.client.files.FileSystemItem)1 GlobalProgressDelayer (org.rstudio.studio.client.common.GlobalProgressDelayer)1 FileFunctionDefinition (org.rstudio.studio.client.workbench.codesearch.model.FileFunctionDefinition)1 ObjectDefinition (org.rstudio.studio.client.workbench.codesearch.model.ObjectDefinition)1 SearchPathFunctionDefinition (org.rstudio.studio.client.workbench.codesearch.model.SearchPathFunctionDefinition)1 SendToConsoleEvent (org.rstudio.studio.client.workbench.views.console.events.SendToConsoleEvent)1 InputEditorPosition (org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition)1 InputEditorSelection (org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorSelection)1