Search in sources :

Example 6 with Position

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

the class FindReplace method find.

private boolean find(FindType findType, boolean incremental) {
    String searchString = display_.getFindValue().getValue();
    if (searchString.length() == 0) {
        // out the searching string then return to the original position
        if (incremental && (incrementalSearchPosition_ != null))
            editor_.setSelectionRange(Range.fromPoints(incrementalSearchPosition_, incrementalSearchPosition_));
        return false;
    }
    boolean ignoreCase = !display_.getCaseSensitive().getValue();
    boolean regex = display_.getRegex().getValue();
    boolean wholeWord = display_.getWholeWord().getValue();
    boolean wrap = display_.getWrapSearch().getValue();
    // if we are searching in a selection then create a custom position
    // (based on the current selection) and range (based on the originally
    // saved selection range)
    Position position = null;
    Range range = null;
    if (display_.getInSelection().getValue() && (targetSelection_ != null)) {
        range = targetSelection_.getRange();
        if (findType == FindType.Forward) {
            Position selectionEnd = editor_.getSelectionEnd();
            if (selectionEnd.isBefore(range.getEnd()))
                position = selectionEnd;
        } else {
            Position selectionStart = editor_.getSelectionStart();
            if (selectionStart.isAfter(range.getStart()))
                position = selectionStart;
        }
    }
    // incremental start position then set it, otherwise clear it
    if (incremental) {
        if (incrementalSearchPosition_ == null) {
            if (position != null)
                incrementalSearchPosition_ = position;
            else
                incrementalSearchPosition_ = defaultForward_ ? editor_.getSelectionStart() : editor_.getSelectionEnd();
        }
        // incremental searches always continue searching from the
        // original search position
        position = incrementalSearchPosition_;
    } else {
        incrementalSearchPosition_ = null;
    }
    // do the search
    Search search = Search.create(searchString, findType != FindType.Forward, wrap, !ignoreCase, wholeWord, position, range, regex);
    try {
        Range resultRange = search.find(editor_.getSession());
        if (resultRange == null) {
            if (!incremental) {
                globalDisplay_.showMessage(GlobalDisplay.MSG_INFO, errorCaption_, "No more occurrences.");
            } else {
                editor_.collapseSelection(true);
            }
            return false;
        } else {
            editor_.revealRange(resultRange, false);
            return true;
        }
    } catch (Throwable e) {
        globalDisplay_.showMessage(GlobalDisplay.MSG_ERROR, errorCaption_, "Invalid search term.");
        return false;
    }
}
Also used : Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) Search(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Search) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)

Example 7 with Position

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

the class RCompletionToolTip method setCursorAnchor.

private void setCursorAnchor() {
    Position start = docDisplay_.getSelectionStart();
    start = Position.create(start.getRow(), start.getColumn() - 1);
    Position end = docDisplay_.getSelectionEnd();
    end = Position.create(end.getRow(), end.getColumn() + 1);
    if (anchor_ != null)
        anchor_.detach();
    anchor_ = docDisplay_.createAnchoredSelection(start, end);
}
Also used : Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)

Example 8 with Position

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

the class SignatureToolTipManager method setAnchor.

// Sets an anchored range for a cursor currently lying
// on an identifier before a '(' (a function call).
private void setAnchor(TokenCursor cursor) {
    if (anchor_ != null) {
        anchor_.detach();
        anchor_ = null;
    }
    TokenCursor endCursor = cursor.cloneCursor();
    if (!endCursor.moveToNextToken())
        return;
    if (!endCursor.valueEquals("("))
        return;
    // this function?
    if (!endCursor.fwdToMatchingToken())
        return;
    Position endPos = endCursor.currentPosition();
    TokenCursor startCursor = cursor.cloneCursor();
    Token lookbehind = startCursor.peekBwd(1);
    if (lookbehind.valueEquals("::") || lookbehind.valueEquals(":::")) {
        if (!startCursor.moveToPreviousToken())
            return;
        if (!startCursor.moveToPreviousToken())
            return;
    }
    Position startPos = startCursor.currentPosition();
    anchor_ = docDisplay_.createAnchoredSelection(startPos, endPos);
}
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) Token(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token)

Example 9 with Position

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

the class TextEditingTargetRenameHelper method renameInScopeR.

private int renameInScopeR() {
    Position startPosition = editor_.hasSelection() ? editor_.getSelectionStart() : editor_.getCursorPosition();
    TokenCursor cursor = editor_.getSession().getMode().getCodeModel().getTokenCursor();
    if (!cursor.moveToPosition(startPosition, true))
        return 0;
    if (cursor.isRightBracket())
        if (!cursor.moveToPreviousToken())
            return 0;
    editor_.setCursorPosition(cursor.currentPosition());
    // Ensure the scope tree is built, since we use that for determining
    // the scope for the current refactor.
    editor_.buildScopeTree();
    // Validate that we're looking at an R identifier
    String targetValue = cursor.currentValue();
    String targetType = cursor.currentType();
    boolean isRefactorable = cursor.hasType("identifier", "constant.language", "string") || cursor.typeEquals("keyword");
    if (!isRefactorable)
        return 0;
    // If we're refactoring a string, just do it through the whole document.
    if (cursor.typeEquals("string")) {
        editor_.selectAll(cursor.currentValue());
        return editor_.getNativeSelection().getAllRanges().length;
    }
    // form and rename just that argument.
    if (cursor.peekFwd(1).valueEquals("=") && cursor.isWithinFunctionCall()) {
        String argName = cursor.currentValue();
        TokenCursor clone = cursor.cloneCursor();
        while (clone.findOpeningBracket("(", false)) {
            if (!clone.moveToPreviousToken())
                break;
            String functionName = clone.currentValue();
            if (!functionName.equals("function"))
                return renameFunctionArgument(functionName, argName);
        }
    }
    // Determine the appropriate refactoring scope.
    //
    // Algorithm:
    //
    //    1. Get the function scope.
    //    2. If we have an argument of the same name, or function
    //       of the same name, rename in that scope.
    //    3. Otherwise, walk forward from the start of that scope,
    //       looking for assignments.
    //    4. If we discover an assignment, rename in that scope
    //       from that position.
    //    5. Repeat while not at top level.
    //
    // TODO: if renaming a function argument, we should also rename
    //       named usages of that function argument where possible.
    Scope scope = editor_.getScopeAtPosition(cursor.currentPosition());
    // of a scope begins with the function identifier.
    if (cursor.peekFwd(1).isLeftAssign() && cursor.peekFwd(2).valueEquals("function") && !scope.isTopLevel()) {
        scope = scope.getParentScope();
    }
    while (!scope.isTopLevel()) {
        if (scope.isFunction()) {
            ScopeFunction scopeFn = (ScopeFunction) scope;
            String fnName = scopeFn.getFunctionName();
            if (fnName.equals(targetValue))
                return renameVariablesInScope(scope, targetValue, targetType);
            JsArrayString fnArgs = scopeFn.getFunctionArgs();
            for (int i = 0; i < fnArgs.length(); i++) if (fnArgs.get(i).equals(targetValue))
                return renameVariablesInScope(scope, targetValue, targetType);
        }
        if (scope.isChunk())
            return renameVariablesInScope(scope, targetValue, targetType);
        if (!cursor.moveToPosition(scope.getBodyStart(), true))
            continue;
        while (cursor.moveToNextToken()) {
            if (cursor.fwdToMatchingToken())
                continue;
            if (cursor.currentPosition().isAfterOrEqualTo(scope.getEnd()) || cursor.currentPosition().isAfter(startPosition)) {
                break;
            }
            if (cursor.peekFwd(1).isLeftAssign() && !cursor.peekBwd(1).isExtractionOperator()) {
                return renameVariablesInScope(scope, cursor.currentPosition(), targetValue, targetType);
            }
        }
        scope = scope.getParentScope();
    }
    return renameVariablesInScope(scope, targetValue, targetType);
}
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) JsArrayString(com.google.gwt.core.client.JsArrayString)

Example 10 with Position

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

the class TextEditingTargetScopeHelper method getSweaveChunks.

public Scope[] getSweaveChunks(Position startPosition, final int which) {
    // provide default position based on selection if necessary
    final Position position = startPosition != null ? startPosition : docDisplay_.getSelectionStart();
    ScopeList scopeList = new ScopeList(docDisplay_);
    scopeList.selectAll(ScopeList.CHUNK);
    scopeList.selectAll(new ScopePredicate() {

        @Override
        public boolean test(Scope scope) {
            if (!scope.isChunk())
                return false;
            int dir = scope.getEnd().compareTo(position);
            if (which == PREVIOUS_CHUNKS)
                return dir < 0;
            else
                return dir > 0;
        }
    });
    return scopeList.getScopes();
}
Also used : Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) ScopePredicate(org.rstudio.studio.client.workbench.views.source.editors.text.ScopeList.ScopePredicate)

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