Search in sources :

Example 1 with RegExpAtom

use of org.intellij.lang.regexp.psi.RegExpAtom 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 2 with RegExpAtom

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

the class SimpleSurroundDescriptor method getElementsToSurround.

@NotNull
public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) {
    // adjust start/end
    PsiElement element1 = file.findElementAt(startOffset);
    PsiElement element2 = file.findElementAt(endOffset - 1);
    if (element1 instanceof PsiWhiteSpace) {
        startOffset = element1.getTextRange().getEndOffset();
    }
    if (element2 instanceof PsiWhiteSpace) {
        endOffset = element2.getTextRange().getStartOffset();
    }
    RegExpElement pattern = findElementAtStrict(file, startOffset, endOffset, RegExpPattern.class);
    if (pattern != null)
        return new RegExpElement[] { pattern };
    RegExpElement branch = findElementAtStrict(file, startOffset, endOffset, RegExpBranch.class);
    if (branch != null)
        return new RegExpElement[] { branch };
    List<PsiElement> atoms = new ArrayList<>();
    RegExpAtom atom = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, RegExpAtom.class);
    for (; atom != null; atom = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, RegExpAtom.class)) {
        atoms.add(atom);
        startOffset = atom.getTextRange().getEndOffset();
        // handle embedded whitespace
        if ((element1 = file.findElementAt(startOffset)) instanceof PsiWhiteSpace) {
            startOffset = element1.getTextRange().getEndOffset();
            atoms.add(element1);
        }
    }
    if (startOffset == endOffset && atoms.size() > 0) {
        PsiElement[] elements = PsiUtilCore.toPsiElementArray(atoms);
        if ((atoms.size() == 1 || PsiTreeUtil.findCommonParent(elements) == elements[0].getParent())) {
            return elements;
        }
    }
    return PsiElement.EMPTY_ARRAY;
}
Also used : RegExpElement(org.intellij.lang.regexp.psi.RegExpElement) RegExpAtom(org.intellij.lang.regexp.psi.RegExpAtom) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElement (com.intellij.psi.PsiElement)2 RegExpAtom (org.intellij.lang.regexp.psi.RegExpAtom)2 ASTNode (com.intellij.lang.ASTNode)1 Document (com.intellij.openapi.editor.Document)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiFile (com.intellij.psi.PsiFile)1 PsiFileFactory (com.intellij.psi.PsiFileFactory)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 ArrayList (java.util.ArrayList)1 RegExpElement (org.intellij.lang.regexp.psi.RegExpElement)1 RegExpPattern (org.intellij.lang.regexp.psi.RegExpPattern)1 RegExpElementImpl (org.intellij.lang.regexp.psi.impl.RegExpElementImpl)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1