Search in sources :

Example 1 with RegExpLexer

use of org.intellij.lang.regexp.RegExpLexer in project intellij-plugins by JetBrains.

the class CucumberStepRenameProcessor method prepareRegexAndGetStaticTexts.

/**
   * Wraps all special symbols of regexp with group and cut static text.
   * @param source regex to work with
   * @return List of strings. The first one is prepared regex, then static elements of the regex
   */
@NotNull
public static List<String> prepareRegexAndGetStaticTexts(@NotNull final String source) {
    final ArrayList<String> result = new ArrayList<>();
    final StringBuilder preparedRegexp = new StringBuilder();
    final RegExpLexer lexer = new RegExpLexer(EnumSet.noneOf(RegExpCapability.class));
    lexer.start(source);
    IElementType previous = null;
    final TokenSet toSkip = TokenSet.create(RegExpTT.CHARACTER, RegExpTT.CARET, RegExpTT.DOLLAR);
    StringBuilder currentStaticText = new StringBuilder();
    boolean insideAddedGroup = false;
    final Stack<IElementType> elementsWaitingToClose = new Stack<>();
    while (lexer.getTokenType() != null) {
        if (!toSkip.contains(lexer.getTokenType())) {
            if (!insideAddedGroup) {
                insideAddedGroup = true;
                preparedRegexp.append('(');
                result.add(currentStaticText.toString());
                currentStaticText = new StringBuilder();
            }
            if (lexer.getTokenType() == RegExpTT.GROUP_BEGIN || lexer.getTokenType() == RegExpTT.NON_CAPT_GROUP) {
                elementsWaitingToClose.push(RegExpTT.GROUP_END);
            } else if (lexer.getTokenType() == RegExpTT.CLASS_BEGIN) {
                elementsWaitingToClose.push(RegExpTT.CLASS_END);
            } else if (elementsWaitingToClose.size() > 0 && lexer.getTokenType() == elementsWaitingToClose.peek()) {
                elementsWaitingToClose.pop();
            }
        } else {
            if (elementsWaitingToClose.size() == 0) {
                if (previous != null && previous != RegExpTT.CHARACTER && insideAddedGroup) {
                    insideAddedGroup = false;
                    preparedRegexp.append(')');
                }
                if (lexer.getTokenType() == RegExpTT.CHARACTER) {
                    currentStaticText.append(lexer.getTokenText());
                }
            }
        }
        preparedRegexp.append(lexer.getTokenText());
        if (lexer.getTokenType() == RegExpTT.GROUP_BEGIN) {
            //Making all group in the regex non capturing
            preparedRegexp.append("?:");
        }
        previous = lexer.getTokenType();
        lexer.advance();
    }
    if (insideAddedGroup) {
        preparedRegexp.append(')');
    }
    result.add(currentStaticText.toString());
    result.add(0, preparedRegexp.toString());
    return result;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) TokenSet(com.intellij.psi.tree.TokenSet) RegExpCapability(org.intellij.lang.regexp.RegExpCapability) RegExpLexer(org.intellij.lang.regexp.RegExpLexer) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with RegExpLexer

use of org.intellij.lang.regexp.RegExpLexer in project intellij-plugins by JetBrains.

the class CucumberStepRenameDialog method guardRegexpSpecialSymbols.

private static void guardRegexpSpecialSymbols(@NotNull final Editor editor) {
    final String text = editor.getDocument().getText();
    final RegExpLexer lexer = new RegExpLexer(EnumSet.noneOf(RegExpCapability.class));
    lexer.start(text);
    while (lexer.getTokenType() != null) {
        if (lexer.getTokenType() != RegExpTT.CHARACTER) {
            editor.getDocument().createGuardedBlock(lexer.getTokenStart(), lexer.getTokenEnd());
        }
        lexer.advance();
    }
}
Also used : RegExpCapability(org.intellij.lang.regexp.RegExpCapability) RegExpLexer(org.intellij.lang.regexp.RegExpLexer)

Aggregations

RegExpCapability (org.intellij.lang.regexp.RegExpCapability)2 RegExpLexer (org.intellij.lang.regexp.RegExpLexer)2 IElementType (com.intellij.psi.tree.IElementType)1 TokenSet (com.intellij.psi.tree.TokenSet)1 NotNull (org.jetbrains.annotations.NotNull)1