use of org.intellij.lang.regexp.psi.impl.RegExpElementImpl 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);
}
}
Aggregations