Search in sources :

Example 41 with Position

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

the class RoxygenHelper method insertRoxygenSkeletonS4Class.

private void insertRoxygenSkeletonS4Class(TokenCursor cursor) {
    final Position startPos = cursor.currentPosition();
    String setClassCall = extractCall(cursor);
    if (setClassCall == null)
        return;
    server_.getSetClassCall(setClassCall, new ServerRequestCallback<SetClassCall>() {

        @Override
        public void onResponseReceived(SetClassCall response) {
            if (hasRoxygenBlock(startPos)) {
                amendExistingRoxygenBlock(startPos.getRow() - 1, response.getClassName(), response.getSlots(), null, "slot", RE_ROXYGEN_SLOT);
            } else {
                insertRoxygenTemplate(response.getClassName(), response.getSlots(), response.getTypes(), "slot", "S4 class", startPos);
            }
        }

        @Override
        public void onError(ServerError error) {
            Debug.logError(error);
        }
    });
}
Also used : Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) ServerError(org.rstudio.studio.client.server.ServerError) JsArrayString(com.google.gwt.core.client.JsArrayString)

Example 42 with Position

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

the class MathJaxUtil method getLatexRange.

public static Range getLatexRange(DocDisplay docDisplay, Position pos) {
    if (pos == null)
        pos = docDisplay.getCursorPosition();
    // find start of latex block
    TokenIterator startIt = docDisplay.createTokenIterator();
    // avoid case where token iterator moves back across lines
    // to discover a latex block
    Token startToken = startIt.moveToPosition(pos);
    if (startToken != null && startToken.hasAllTypes("latex", "end") && startIt.getCurrentTokenRow() != pos.getRow()) {
        return null;
    }
    for (Token token = startIt.moveToPosition(pos); token != null; token = startIt.stepBackward()) {
        if (!token.hasType("latex"))
            return null;
        if (token.hasType("begin"))
            break;
    }
    // find end of latex block
    TokenIterator endIt = docDisplay.createTokenIterator();
    for (Token token = endIt.moveToPosition(pos); token != null; token = endIt.stepForward()) {
        if (!token.hasType("latex"))
            return null;
        if (token.hasType("end"))
            break;
    }
    Token lhsToken = startIt.getCurrentToken();
    if (lhsToken == null || !lhsToken.hasAllTypes("latex", "begin"))
        return null;
    Token rhsToken = endIt.getCurrentToken();
    if (rhsToken == null || !rhsToken.hasAllTypes("latex", "end"))
        return null;
    Position startPos = startIt.getCurrentTokenPosition();
    Position endPos = endIt.getCurrentTokenPosition();
    endPos.setColumn(endPos.getColumn() + endIt.getCurrentToken().getValue().length());
    return Range.fromPoints(startPos, endPos);
}
Also used : TokenIterator(org.rstudio.studio.client.workbench.views.source.editors.text.ace.TokenIterator) 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 43 with Position

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

the class YamlFrontMatter method getFrontMatterRange.

public static Range getFrontMatterRange(DocDisplay display) {
    // front matter can end with ... rather than ---; see spec:
    // http://www.yaml.org/spec/1.2/spec.html#id2760395
    RegExp frontMatterBegin = RegExp.compile("^---\\s*$", "gm");
    RegExp frontMatterEnd = RegExp.compile("^(---|\\.\\.\\.)\\s*$", "gm");
    Position begin = null;
    Position end = null;
    for (int i = 0; i < display.getRowCount(); i++) {
        String code = display.getLine(i);
        if (begin == null) {
            // haven't found front matter begin yet; test this line
            MatchResult beginMatch = frontMatterBegin.exec(code);
            if (beginMatch == null) {
                // Markdown package)
                if (!code.matches("\\s*"))
                    break;
            } else {
                begin = Position.create(i + 1, 0);
                continue;
            }
        } else if (end == null) {
            // haven't found front matter end yet; test this line
            MatchResult endMatch = frontMatterEnd.exec(code);
            if (endMatch != null) {
                end = Position.create(i, 0);
                break;
            }
        }
    }
    if (begin == null || end == null)
        return null;
    return Range.fromPoints(begin, end);
}
Also used : RegExp(com.google.gwt.regexp.shared.RegExp) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) MatchResult(com.google.gwt.regexp.shared.MatchResult)

Example 44 with Position

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

the class RCompletionManager method checkCanAutoPopup.

private boolean checkCanAutoPopup(char c, int lookbackLimit) {
    if (docDisplay_.isVimModeOn() && !docDisplay_.isVimInInsertMode())
        return false;
    String currentLine = docDisplay_.getCurrentLine();
    Position cursorPos = input_.getCursorPosition();
    int cursorColumn = cursorPos.getColumn();
    // Don't auto-popup when the cursor is within a string
    if (docDisplay_.isCursorInSingleLineString())
        return false;
    // be annoying)
    if (isValidForRIdentifier(docDisplay_.getCharacterAtCursor()))
        return false;
    boolean canAutoPopup = (currentLine.length() > lookbackLimit - 1 && isValidForRIdentifier(c));
    if (isConsole_ && !uiPrefs_.alwaysCompleteInConsole().getValue())
        canAutoPopup = false;
    if (canAutoPopup) {
        for (int i = 0; i < lookbackLimit; i++) {
            if (!isValidForRIdentifier(currentLine.charAt(cursorColumn - i - 1))) {
                canAutoPopup = false;
                break;
            }
        }
    }
    return canAutoPopup;
}
Also used : InputEditorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) SourcePosition(org.rstudio.studio.client.workbench.views.source.model.SourcePosition) InputEditorLineWithCursorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorLineWithCursorPosition)

Example 45 with Position

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

the class RCompletionManager method getCurrentCompletionToken.

String getCurrentCompletionToken() {
    AceEditor editor = (AceEditor) docDisplay_;
    if (editor == null)
        return "";
    // `r foo`
    if (DocumentMode.isCursorInMarkdownMode(docDisplay_))
        return token_;
    Position cursorPos = editor.getCursorPosition();
    Token currentToken = editor.getSession().getTokenAt(cursorPos);
    if (currentToken == null)
        return "";
    // If the user has inserted some spaces, the cursor might now lie
    // on a 'text' token. In that case, find the previous token and
    // use that for completion.
    String suffix = "";
    if (currentToken.getValue().trim().isEmpty()) {
        suffix = currentToken.getValue();
        TokenIterator it = editor.createTokenIterator();
        it.moveToPosition(cursorPos);
        Token token = it.stepBackward();
        if (token != null)
            currentToken = token;
    }
    // Exclude non-string and non-identifier tokens.
    if (currentToken.hasType("operator", "comment", "numeric", "text", "punctuation"))
        return "";
    String tokenValue = currentToken.getValue();
    String subsetted = tokenValue.substring(0, cursorPos.getColumn() - currentToken.getColumn());
    return subsetted + suffix;
}
Also used : TokenIterator(org.rstudio.studio.client.workbench.views.source.editors.text.ace.TokenIterator) InputEditorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) SourcePosition(org.rstudio.studio.client.workbench.views.source.model.SourcePosition) InputEditorLineWithCursorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorLineWithCursorPosition) AceEditor(org.rstudio.studio.client.workbench.views.source.editors.text.AceEditor) Token(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token)

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