Search in sources :

Example 1 with Position

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

the class CppCompletionRequest method applyValue.

private void applyValue(CppCompletion completion) {
    if (invalidationToken_.isInvalid())
        return;
    terminate();
    if (completion.getType() == CppCompletion.SNIPPET) {
        snippets_.applySnippet(getUserTypedText(), completion.getTypedText());
        return;
    }
    String insertText = completion.getTypedText();
    if (completion.getType() == CppCompletion.FUNCTION && uiPrefs_.insertParensAfterFunctionCompletion().getValue()) {
        if (uiPrefs_.insertMatching().getValue())
            insertText = insertText + "()";
        else
            insertText = insertText + "(";
    } else if (completion.getType() == CppCompletion.DIRECTORY) {
        insertText += "/";
        Scheduler.get().scheduleDeferred(new ScheduledCommand() {

            @Override
            public void execute() {
                manager_.codeCompletion();
            }
        });
    }
    docDisplay_.setFocus(true);
    docDisplay_.setSelection(getReplacementSelection());
    docDisplay_.replaceSelection(insertText, true);
    if (completion.hasParameters() && uiPrefs_.insertParensAfterFunctionCompletion().getValue() && uiPrefs_.insertMatching().getValue()) {
        Position pos = docDisplay_.getCursorPosition();
        pos = Position.create(pos.getRow(), pos.getColumn() - 1);
        docDisplay_.setSelectionRange(Range.fromPoints(pos, pos));
    } else if (completion.getType() == CppCompletion.FILE) {
        char ch = docDisplay_.getCharacterAtCursor();
        // if there is a '>' or '"' following the cursor, move over it
        if (ch == '>' || ch == '"') {
            docDisplay_.moveCursorForward();
        } else // otherwise, insert the corresponding closing character
        {
            String line = docDisplay_.getCurrentLine();
            if (line.contains("<"))
                docDisplay_.insertCode(">");
            else if (line.contains("\""))
                docDisplay_.insertCode("\"");
        }
    }
    if (completion.hasParameters() && uiPrefs_.showSignatureTooltips().getValue()) {
        new CppCompletionSignatureTip(completion, docDisplay_);
    }
}
Also used : ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)

Example 2 with Position

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

the class CppCompletionSignatureTip method onLoad.

@Override
protected void onLoad() {
    super.onLoad();
    allTips_.add(this);
    nativePreviewReg_ = Event.addNativePreviewHandler(new NativePreviewHandler() {

        public void onPreviewNativeEvent(NativePreviewEvent e) {
            if (e.getTypeInt() == Event.ONKEYDOWN) {
                // handle keys
                switch(e.getNativeEvent().getKeyCode()) {
                    case KeyCodes.KEY_ESCAPE:
                        e.cancel();
                        hide();
                        break;
                    case KeyCodes.KEY_DOWN:
                        e.cancel();
                        setTextIndex(currentTextIndex_ + 1);
                        break;
                    case KeyCodes.KEY_UP:
                        e.cancel();
                        setTextIndex(currentTextIndex_ - 1);
                        break;
                }
                // 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();
                        Range anchorRange = anchor_.getRange();
                        if (cursorPos.isBeforeOrEqualTo(anchorRange.getStart()) || cursorPos.isAfterOrEqualTo(anchorRange.getEnd())) {
                            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 3 with Position

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

the class CppCompletionUtils method getCompletionPosition.

public static CompletionPosition getCompletionPosition(DocDisplay docDisplay, boolean explicit, boolean alwaysComplete, int autoChars) {
    // get the current line of code
    String line = docDisplay.getCurrentLine();
    // is this an '#include' line?
    Pattern reInclude = Pattern.create("^\\s*#+\\s*include", "");
    boolean isInclude = reInclude.test(line);
    // get the cursor position
    Position position = docDisplay.getCursorPosition();
    // if so then bail
    if ((position.getColumn() < line.length()) && CppCompletionUtils.isCppIdentifierChar(line.charAt(position.getColumn()))) {
        return null;
    }
    // determine the column right before this one
    int inputCol = position.getColumn() - 1;
    // walk backwards across C++ identifer symbols 
    int col = inputCol;
    if (isInclude) {
        while (col >= 0 && !StringUtil.isOneOf(line.charAt(col), '/', '<', '"')) {
            col--;
        }
    } else {
        while ((col >= 0) && CppCompletionUtils.isCppIdentifierChar(line.charAt(col))) {
            col--;
        }
    }
    // record source position
    Position startPos = Position.create(position.getRow(), col + 1);
    // check for a completion triggering sequence
    char ch = line.charAt(col);
    char prefixCh = line.charAt(col - 1);
    // member
    if (ch == '.' || (prefixCh == '-' && ch == '>')) {
        return new CompletionPosition(startPos, // no user text (get all completions)
        "", CompletionPosition.Scope.Member);
    } else // scope
    if (prefixCh == ':' && ch == ':') {
        return new CompletionPosition(startPos, // no user text (get all completions)
        "", CompletionPosition.Scope.Namespace);
    } else // directory
    if (isInclude && ch == '/') {
        return new CompletionPosition(startPos, // no user test (get all completions)
        "", CompletionPosition.Scope.File);
    } else // minimum character threshold
    if (// either always completing or explicit
    (alwaysComplete || explicit) && // meets the character threshold
    ((inputCol - col) >= (explicit ? 1 : autoChars)) && // not a quote character
    (isInclude || ch != '"')) {
        // calculate user text (up to two characters of additional
        // server side filter)
        String userText = line.substring(col + 1, Math.min(col + 3, position.getColumn()));
        CompletionPosition.Scope scope = isInclude ? CompletionPosition.Scope.File : CompletionPosition.Scope.Global;
        return new CompletionPosition(startPos, userText, scope);
    } else {
        return null;
    }
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)

Example 4 with Position

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

the class ChunkContextUi method switchChunk.

@Override
public void switchChunk(String chunkType) {
    if (chunk_ != null) {
        DocDisplay docDisplay = target_.getDocDisplay();
        Position start = chunk_.getPreamble();
        Position end = chunk_.getEnd();
        String chunkText = docDisplay.getTextForRange(Range.fromPoints(start, end));
        JsArrayString chunkLines = StringUtil.split(chunkText, "\n");
        if (chunkLines.length() > 0) {
            String firstLine = chunkLines.get(0);
            Position linedEnd = Position.create(start.getRow(), firstLine.length());
            String newFirstLine = firstLine.replaceFirst("[, ]*engine='[a-zA-Z]+'", "");
            newFirstLine = newFirstLine.replaceFirst("{[a-zA-Z]+", "{" + chunkType);
            docDisplay.replaceRange(Range.fromPoints(start, linedEnd), newFirstLine);
            target_.getNotebook().clearChunkOutput(chunk_);
        }
    }
}
Also used : Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) DocDisplay(org.rstudio.studio.client.workbench.views.source.editors.text.DocDisplay) JsArrayString(com.google.gwt.core.client.JsArrayString) JsArrayString(com.google.gwt.core.client.JsArrayString)

Example 5 with Position

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

the class TextEditingTargetPresentationHelper method navigateToSlide.

public static void navigateToSlide(final EditingTarget editor, int slideIndex) {
    // scan for the specified slide
    int currentSlide = 0;
    Position navPos = null;
    Position pos = Position.create(0, 0);
    while ((pos = editor.search(pos, "^\\={3,}\\s*$")) != null) {
        if (currentSlide++ == slideIndex) {
            navPos = Position.create(pos.getRow() - 1, 0);
            break;
        }
        pos = Position.create(pos.getRow() + 1, 0);
    }
    // navigate to the slide
    if (navPos != null) {
        final Position navPosAlias = navPos;
        Scheduler.get().scheduleDeferred(new ScheduledCommand() {

            @Override
            public void execute() {
                editor.navigateToPosition(SourcePosition.create(navPosAlias.getRow(), 0), false);
            }
        });
    }
}
Also used : ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) InputEditorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition) SourcePosition(org.rstudio.studio.client.workbench.views.source.model.SourcePosition)

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