Search in sources :

Example 1 with InputEditorSelection

use of org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorSelection in project rstudio by rstudio.

the class TextEditingTarget method doReflowComment.

void doReflowComment(String commentPrefix, boolean multiParagraphIndent) {
    docDisplay_.focus();
    InputEditorSelection originalSelection = docDisplay_.getSelection();
    InputEditorSelection selection = originalSelection;
    if (selection.isEmpty()) {
        selection = selection.growToIncludeLines("^\\s*" + commentPrefix + ".*$");
    } else {
        selection = selection.shrinkToNonEmptyLines();
        selection = selection.extendToLineStart();
        selection = selection.extendToLineEnd();
    }
    if (selection.isEmpty())
        return;
    reflowComments(commentPrefix, multiParagraphIndent, selection, originalSelection.isEmpty() ? originalSelection.getStart() : null);
}
Also used : InputEditorSelection(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorSelection)

Example 2 with InputEditorSelection

use of org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorSelection 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)

Example 3 with InputEditorSelection

use of org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorSelection in project rstudio by rstudio.

the class RCompletionManager method previewKeyDown.

public boolean previewKeyDown(NativeEvent event) {
    suggestTimer_.cancel();
    if (sigTipManager_.previewKeyDown(event))
        return true;
    if (isDisabled())
        return false;
    /**
       * KEYS THAT MATTER
       *
       * When popup not showing:
       * Tab - attempt completion (handled in Console.java)
       * 
       * When popup showing:
       * Esc - dismiss popup
       * Enter/Tab - accept current selection
       * Up-arrow/Down-arrow - change selected item
       * [identifier] - narrow suggestions--or if we're lame, just dismiss
       * All others - dismiss popup
       */
    nativeEvent_ = event;
    int keycode = event.getKeyCode();
    int modifier = KeyboardShortcut.getModifierValue(event);
    if (!popup_.isShowing()) {
        // don't allow ctrl + space for completions in Emacs mode
        if (docDisplay_.isEmacsModeOn() && event.getKeyCode() == KeyCodes.KEY_SPACE)
            return false;
        if (CompletionUtils.isCompletionRequest(event, modifier)) {
            if (initFilter_ == null || initFilter_.shouldComplete(event)) {
                // '[](', or '`r |' contexts
                if (DocumentMode.isCursorInMarkdownMode(docDisplay_)) {
                    String currentLine = docDisplay_.getCurrentLineUpToCursor();
                    if (!(Pattern.create("^```{[rR]").test(currentLine) || Pattern.create(".*\\[.*\\]\\(").test(currentLine) || (Pattern.create(".*`r").test(currentLine) && StringUtil.countMatches(currentLine, '`') % 2 == 1)))
                        return false;
                }
                // If we're in tex mode, only provide completions in chunks
                if (DocumentMode.isCursorInTexMode(docDisplay_)) {
                    String currentLine = docDisplay_.getCurrentLineUpToCursor();
                    if (!Pattern.create("^<<").test(currentLine))
                        return false;
                }
                return beginSuggest(true, false, true);
            }
        } else if (event.getKeyCode() == KeyCodes.KEY_TAB && modifier == KeyboardShortcut.SHIFT) {
            return snippets_.attemptSnippetInsertion(true);
        } else if (// F1
        keycode == 112 && modifier == KeyboardShortcut.NONE) {
            goToHelp();
            return true;
        } else if (// F2
        keycode == 113 && modifier == KeyboardShortcut.NONE) {
            goToFunctionDefinition();
            return true;
        }
    } else {
        // bail on modifier keys
        if (KeyboardHelper.isModifierKey(keycode))
            return false;
        // allow emacs-style navigation of popup entries
        if (modifier == KeyboardShortcut.CTRL) {
            switch(keycode) {
                case KeyCodes.KEY_P:
                    return popup_.selectPrev();
                case KeyCodes.KEY_N:
                    return popup_.selectNext();
            }
        } else if (modifier == KeyboardShortcut.NONE) {
            if (keycode == KeyCodes.KEY_ESCAPE) {
                invalidatePendingRequests();
                return true;
            }
            // had originally requested completions at e.g. "stats::".
            if (popup_.hasCompletions() && !popup_.isOffscreen()) {
                if (keycode == KeyCodes.KEY_ENTER) {
                    QualifiedName value = popup_.getSelectedValue();
                    if (value != null) {
                        context_.onSelection(value);
                        return true;
                    }
                } else if (keycode == KeyCodes.KEY_TAB) {
                    QualifiedName value = popup_.getSelectedValue();
                    if (value != null) {
                        if (value.type == RCompletionType.DIRECTORY)
                            context_.suggestOnAccept_ = true;
                        context_.onSelection(value);
                        return true;
                    }
                } else if (keycode == KeyCodes.KEY_UP)
                    return popup_.selectPrev();
                else if (keycode == KeyCodes.KEY_DOWN)
                    return popup_.selectNext();
                else if (keycode == KeyCodes.KEY_PAGEUP)
                    return popup_.selectPrevPage();
                else if (keycode == KeyCodes.KEY_PAGEDOWN)
                    return popup_.selectNextPage();
                else if (keycode == KeyCodes.KEY_HOME)
                    return popup_.selectFirst();
                else if (keycode == KeyCodes.KEY_END)
                    return popup_.selectLast();
                if (// F1
                keycode == 112) {
                    context_.showHelpTopic();
                    return true;
                } else if (// F2
                keycode == 113) {
                    goToFunctionDefinition();
                    return true;
                }
            }
        }
        if (canContinueCompletions(event))
            return false;
        // pop up completions
        if (keycode == 191 && modifier == KeyboardShortcut.NONE) {
            input_.insertCode("/");
            return beginSuggest(true, true, false);
        }
        // continue showing completions on backspace
        if (keycode == KeyCodes.KEY_BACKSPACE && modifier == KeyboardShortcut.NONE && !docDisplay_.inMultiSelectMode()) {
            int cursorColumn = input_.getCursorPosition().getColumn();
            String currentLine = docDisplay_.getCurrentLine();
            // also halt suggestions if we're about to remove the only character on the line
            if (cursorColumn > 0) {
                char ch = currentLine.charAt(cursorColumn - 2);
                char prevCh = currentLine.charAt(cursorColumn - 3);
                boolean isAcceptableCharSequence = isValidForRIdentifier(ch) || (ch == ':' && prevCh == ':') || ch == '$' || ch == '@' || // for file completions
                ch == '/';
                if (currentLine.length() > 0 && cursorColumn > 0 && isAcceptableCharSequence) {
                    // manually remove the previous character
                    InputEditorSelection selection = input_.getSelection();
                    InputEditorPosition start = selection.getStart().movePosition(-1, true);
                    InputEditorPosition end = selection.getStart();
                    if (currentLine.charAt(cursorColumn) == ')' && currentLine.charAt(cursorColumn - 1) == '(') {
                        // flush cache as old completions no longer relevant
                        requester_.flushCache();
                        end = selection.getStart().movePosition(1, true);
                    }
                    input_.setSelection(new InputEditorSelection(start, end));
                    input_.replaceSelection("", false);
                    return beginSuggest(false, false, false);
                }
            } else {
                invalidatePendingRequests();
                return true;
            }
        }
        invalidatePendingRequests();
        return false;
    }
    return false;
}
Also used : InputEditorSelection(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorSelection) QualifiedName(org.rstudio.studio.client.workbench.views.console.shell.assist.CompletionRequester.QualifiedName) InputEditorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition)

Example 4 with InputEditorSelection

use of org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorSelection in project rstudio by rstudio.

the class AceEditor method getEnd.

public InputEditorSelection getEnd() {
    EditSession session = getSession();
    int rows = session.getLength();
    Position end = Position.create(rows, session.getLine(rows).length());
    return new InputEditorSelection(new AceInputEditorPosition(session, end));
}
Also used : InputEditorSelection(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorSelection) InputEditorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition) SourcePosition(org.rstudio.studio.client.workbench.views.source.model.SourcePosition) Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint)

Example 5 with InputEditorSelection

use of org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorSelection in project rstudio by rstudio.

the class TextEditingTargetCompilePdfHelper method ensureRnwConcordance.

public void ensureRnwConcordance() {
    RnwWeave rnwWeave = getActiveRnwWeave();
    if ((rnwWeave != null) && rnwWeave.getInjectConcordance()) {
        if (!hasConcordanceDirective(docDisplay_.getCode())) {
            InputEditorSelection doc = docDisplay_.search("\\\\begin{document}", // backwards
            false, // wrap
            true, // case sensitive
            false, // whole word
            false, // from selection
            null, // range (search all)
            null, // regexp mode
            true);
            if (doc != null) {
                InputEditorPosition pos = doc.getEnd().moveToNextLine();
                docDisplay_.insertCode(pos, "\\SweaveOpts{concordance=TRUE}\n");
            }
        }
    }
}
Also used : RnwWeave(org.rstudio.studio.client.common.rnw.RnwWeave) InputEditorSelection(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorSelection) InputEditorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition)

Aggregations

InputEditorSelection (org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorSelection)8 InputEditorPosition (org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition)4 SourcePosition (org.rstudio.studio.client.workbench.views.source.model.SourcePosition)2 Pair (org.rstudio.core.client.Pair)1 Breakpoint (org.rstudio.studio.client.common.debugging.model.Breakpoint)1 RnwWeave (org.rstudio.studio.client.common.rnw.RnwWeave)1 QualifiedName (org.rstudio.studio.client.workbench.views.console.shell.assist.CompletionRequester.QualifiedName)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 Position (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)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