Search in sources :

Example 1 with RegExpPattern

use of org.intellij.lang.regexp.psi.RegExpPattern in project intellij-community by JetBrains.

the class RegExpFactory method createBranchFromText.

@NotNull
public static RegExpBranch createBranchFromText(@NotNull CharSequence text, @NotNull PsiElement context) {
    final RegExpPattern pattern = createPatternFromText(text, context);
    final RegExpBranch branch = PsiTreeUtil.getChildOfType(pattern, RegExpBranch.class);
    assert branch != null;
    return branch;
}
Also used : RegExpPattern(org.intellij.lang.regexp.psi.RegExpPattern) RegExpBranch(org.intellij.lang.regexp.psi.RegExpBranch) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with RegExpPattern

use of org.intellij.lang.regexp.psi.RegExpPattern in project intellij-community by JetBrains.

the class RegExpUtil method getEnumValues.

@Nullable
public static Set<String> getEnumValues(Project project, @NotNull String regExp) {
    final PsiFileFactory factory = PsiFileFactory.getInstance(project);
    final PsiFile file = factory.createFileFromText("dummy.regexp", RegExpFileType.INSTANCE, regExp);
    final RegExpPattern pattern = (RegExpPattern) file.getFirstChild();
    if (pattern == null) {
        return null;
    }
    final RegExpBranch[] branches = pattern.getBranches();
    final Set<String> values = new HashSet<>();
    for (RegExpBranch branch : branches) {
        if (analyzeBranch(branch)) {
            values.add(branch.getUnescapedText());
        }
    }
    return values;
}
Also used : PsiFileFactory(com.intellij.psi.PsiFileFactory) RegExpPattern(org.intellij.lang.regexp.psi.RegExpPattern) RegExpBranch(org.intellij.lang.regexp.psi.RegExpBranch) PsiFile(com.intellij.psi.PsiFile) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with RegExpPattern

use of org.intellij.lang.regexp.psi.RegExpPattern in project intellij-community by JetBrains.

the class RegExpFactory method createPatternFromText.

@NotNull
public static RegExpPattern createPatternFromText(@NotNull CharSequence text, @NotNull PsiElement context) {
    final PsiFileFactory factory = PsiFileFactory.getInstance(context.getProject());
    final PsiFile file = factory.createFileFromText("dummy.regexp", RegExpFileType.INSTANCE, text);
    final RegExpPattern pattern = PsiTreeUtil.getChildOfType(file, RegExpPattern.class);
    assert pattern != null;
    return pattern;
}
Also used : PsiFileFactory(com.intellij.psi.PsiFileFactory) RegExpPattern(org.intellij.lang.regexp.psi.RegExpPattern) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with RegExpPattern

use of org.intellij.lang.regexp.psi.RegExpPattern in project intellij-community by JetBrains.

the class GroupSurrounder method surroundElements.

@Nullable
public TextRange surroundElements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement[] elements) throws IncorrectOperationException {
    assert elements.length == 1 || PsiTreeUtil.findCommonParent(elements) == elements[0].getParent();
    final PsiElement e = elements[0];
    final ASTNode node = e.getNode();
    assert node != null;
    final ASTNode parent = node.getTreeParent();
    final StringBuilder s = new StringBuilder();
    for (int i = 0; i < elements.length; i++) {
        final PsiElement element = elements[i];
        if (element instanceof RegExpElementImpl) {
            s.append(((RegExpElementImpl) element).getUnescapedText());
        } else {
            s.append(element.getText());
        }
        if (i > 0) {
            final ASTNode child = element.getNode();
            assert child != null;
            parent.removeChild(child);
        }
    }
    final PsiFileFactory factory = PsiFileFactory.getInstance(project);
    final PsiFile f = factory.createFileFromText("dummy.regexp", RegExpFileType.INSTANCE, makeReplacement(s));
    final RegExpPattern pattern = PsiTreeUtil.getChildOfType(f, RegExpPattern.class);
    assert pattern != null;
    final RegExpAtom element = pattern.getBranches()[0].getAtoms()[0];
    if (isInsideStringLiteral(e)) {
        final Document doc = editor.getDocument();
        PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(doc);
        final TextRange tr = e.getTextRange();
        doc.replaceString(tr.getStartOffset(), tr.getEndOffset(), StringUtil.escapeStringCharacters(element.getText()));
        return TextRange.from(e.getTextRange().getEndOffset(), 0);
    } else {
        final PsiElement n = e.replace(element);
        return TextRange.from(n.getTextRange().getEndOffset(), 0);
    }
}
Also used : PsiFileFactory(com.intellij.psi.PsiFileFactory) RegExpPattern(org.intellij.lang.regexp.psi.RegExpPattern) RegExpElementImpl(org.intellij.lang.regexp.psi.impl.RegExpElementImpl) RegExpAtom(org.intellij.lang.regexp.psi.RegExpAtom) ASTNode(com.intellij.lang.ASTNode) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with RegExpPattern

use of org.intellij.lang.regexp.psi.RegExpPattern in project intellij-community by JetBrains.

the class SimplifyQuantifierAction method invoke.

public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    if (myReplacement == null) {
        myQuantifier.delete();
    } else {
        final PsiFileFactory factory = PsiFileFactory.getInstance(project);
        final ASTNode modifier = myQuantifier.getModifier();
        final PsiFile f = factory.createFileFromText("dummy.regexp", RegExpFileType.INSTANCE, "a" + myReplacement + (modifier != null ? modifier.getText() : ""));
        final RegExpPattern pattern = PsiTreeUtil.getChildOfType(f, RegExpPattern.class);
        assert pattern != null;
        final RegExpClosure closure = (RegExpClosure) pattern.getBranches()[0].getAtoms()[0];
        myQuantifier.replace(closure.getQuantifier());
    }
}
Also used : PsiFileFactory(com.intellij.psi.PsiFileFactory) RegExpPattern(org.intellij.lang.regexp.psi.RegExpPattern) RegExpClosure(org.intellij.lang.regexp.psi.RegExpClosure) ASTNode(com.intellij.lang.ASTNode) PsiFile(com.intellij.psi.PsiFile)

Aggregations

RegExpPattern (org.intellij.lang.regexp.psi.RegExpPattern)6 PsiFile (com.intellij.psi.PsiFile)4 PsiFileFactory (com.intellij.psi.PsiFileFactory)4 RegExpBranch (org.intellij.lang.regexp.psi.RegExpBranch)3 ASTNode (com.intellij.lang.ASTNode)2 PsiElement (com.intellij.psi.PsiElement)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 Document (com.intellij.openapi.editor.Document)1 TextRange (com.intellij.openapi.util.TextRange)1 HashSet (java.util.HashSet)1 RegExpFile (org.intellij.lang.regexp.RegExpFile)1 RegExpAtom (org.intellij.lang.regexp.psi.RegExpAtom)1 RegExpClosure (org.intellij.lang.regexp.psi.RegExpClosure)1 RegExpGroup (org.intellij.lang.regexp.psi.RegExpGroup)1 RegExpRecursiveElementVisitor (org.intellij.lang.regexp.psi.RegExpRecursiveElementVisitor)1 RegExpElementImpl (org.intellij.lang.regexp.psi.impl.RegExpElementImpl)1