Search in sources :

Example 1 with DplyrJoinContext

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

the class RCompletionManager method beginSuggest.

/**
    * If false, the suggest operation was aborted
    */
private boolean beginSuggest(boolean flushCache, boolean implicit, boolean canAutoInsert) {
    suggestTimer_.cancel();
    if (!input_.isSelectionCollapsed())
        return false;
    invalidatePendingRequests(flushCache, false);
    InputEditorSelection selection = input_.getSelection();
    if (selection == null)
        return false;
    int cursorCol = selection.getStart().getPosition();
    String firstLine = input_.getText().substring(0, cursorCol);
    // of roxygen comments (e.g. at "#' |")
    if (isLineInComment(firstLine) && !isLineInRoxygenComment(firstLine))
        return false;
    // if the insertion character was a tab (unless the user has opted in)
    if (!uiPrefs_.allowTabMultilineCompletion().getValue()) {
        if (nativeEvent_ != null && nativeEvent_.getKeyCode() == KeyCodes.KEY_TAB)
            if (firstLine.matches("^\\s*$"))
                return false;
    }
    AutocompletionContext context = getAutocompletionContext();
    // but is effectively a bandaid until the autocompletion revamp.
    if (context.getToken().startsWith("-"))
        context.setToken(context.getToken().substring(1));
    // the comment marker
    if (context.getToken().equals("'@"))
        context.setToken(context.getToken().substring(1));
    context_ = new CompletionRequestContext(invalidation_.getInvalidationToken(), selection, canAutoInsert);
    RInfixData infixData = RInfixData.create();
    AceEditor editor = (AceEditor) docDisplay_;
    if (editor != null) {
        CodeModel codeModel = editor.getSession().getMode().getRCodeModel();
        TokenCursor cursor = codeModel.getTokenCursor();
        if (cursor.moveToPosition(input_.getCursorPosition())) {
            String token = "";
            if (cursor.hasType("identifier"))
                token = cursor.currentValue();
            String cursorPos = "left";
            if (cursor.currentValue() == "=")
                cursorPos = "right";
            TokenCursor clone = cursor.cloneCursor();
            if (clone.moveToPreviousToken())
                if (clone.currentValue() == "=")
                    cursorPos = "right";
            // Try to get a dplyr join completion
            DplyrJoinContext joinContext = codeModel.getDplyrJoinContextFromInfixChain(cursor);
            // If that failed, try a non-infix lookup
            if (joinContext == null) {
                String joinString = getDplyrJoinString(editor, cursor);
                if (!StringUtil.isNullOrEmpty(joinString)) {
                    requester_.getDplyrJoinCompletionsString(token, joinString, cursorPos, implicit, context_);
                    return true;
                }
            } else {
                requester_.getDplyrJoinCompletions(joinContext, implicit, context_);
                return true;
            }
            // completions
            if (cursor.moveToPosition(input_.getCursorPosition()))
                infixData = codeModel.getDataFromInfixChain(cursor);
        }
    }
    String filePath = getSourceDocumentPath();
    String docId = getSourceDocumentId();
    requester_.getCompletions(context.getToken(), context.getAssocData(), context.getDataType(), context.getNumCommas(), context.getFunctionCallString(), infixData.getDataName(), infixData.getAdditionalArgs(), infixData.getExcludeArgs(), infixData.getExcludeArgsFromObject(), filePath, docId, implicit, context_);
    return true;
}
Also used : InputEditorSelection(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorSelection) DplyrJoinContext(org.rstudio.studio.client.workbench.views.source.editors.text.ace.DplyrJoinContext) TokenCursor(org.rstudio.studio.client.workbench.views.source.editors.text.ace.TokenCursor) RInfixData(org.rstudio.studio.client.workbench.views.source.editors.text.ace.RInfixData) CodeModel(org.rstudio.studio.client.workbench.views.source.editors.text.ace.CodeModel) AceEditor(org.rstudio.studio.client.workbench.views.source.editors.text.AceEditor)

Aggregations

InputEditorSelection (org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorSelection)1 AceEditor (org.rstudio.studio.client.workbench.views.source.editors.text.AceEditor)1 CodeModel (org.rstudio.studio.client.workbench.views.source.editors.text.ace.CodeModel)1 DplyrJoinContext (org.rstudio.studio.client.workbench.views.source.editors.text.ace.DplyrJoinContext)1 RInfixData (org.rstudio.studio.client.workbench.views.source.editors.text.ace.RInfixData)1 TokenCursor (org.rstudio.studio.client.workbench.views.source.editors.text.ace.TokenCursor)1