Search in sources :

Example 1 with TokenIterator

use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.TokenIterator 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 2 with TokenIterator

use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.TokenIterator 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)

Example 3 with TokenIterator

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

the class SetupChunkOptionsPopupPanel method findOptsChunk.

private Range findOptsChunk() {
    TokenIterator iterator = display_.createTokenIterator(position_);
    while (true) {
        Token token = iterator.stepForward();
        if (token == null)
            break;
        if (token.hasType("codeend"))
            break;
        Position startPos = iterator.getCurrentTokenPosition();
        if (!token.getValue().equals("opts_chunk"))
            continue;
        Position knitrPrefixPos = findKnitrPrefix(iterator.clone());
        if (knitrPrefixPos != null)
            startPos = knitrPrefixPos;
        token = iterator.stepForward();
        if (!token.getValue().equals("$"))
            continue;
        token = iterator.stepForward();
        if (!token.getValue().equals("set"))
            continue;
        token = iterator.stepForward();
        if (!token.getValue().equals("("))
            continue;
        if (!iterator.fwdToMatchingToken())
            continue;
        token = iterator.stepForward();
        Position endPos = iterator.getCurrentTokenPosition();
        return Range.fromPoints(startPos, endPos);
    }
    return null;
}
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)

Aggregations

Position (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)3 Token (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token)3 TokenIterator (org.rstudio.studio.client.workbench.views.source.editors.text.ace.TokenIterator)3 InputEditorLineWithCursorPosition (org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorLineWithCursorPosition)1 InputEditorPosition (org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition)1 AceEditor (org.rstudio.studio.client.workbench.views.source.editors.text.AceEditor)1 SourcePosition (org.rstudio.studio.client.workbench.views.source.model.SourcePosition)1