use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class Intention method replaceExpressionWithNegatedExpressionString.
protected static void replaceExpressionWithNegatedExpressionString(@NotNull String newExpression, @NotNull GrExpression expression) throws IncorrectOperationException {
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(expression.getProject());
GrExpression expressionToReplace = expression;
final String expString;
if (BoolUtils.isNegated(expression)) {
expressionToReplace = BoolUtils.findNegation(expression);
expString = newExpression;
} else {
expString = "!(" + newExpression + ')';
}
final GrExpression newCall = factory.createExpressionFromText(expString);
assert expressionToReplace != null;
expressionToReplace.replaceWithExpression(newCall, true);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrDocInlinedTagImpl method setName.
@Override
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
final PsiElement nameElement = getNameElement();
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
final GrDocComment comment = factory.createDocCommentFromText("/** {@" + name + "}*/");
nameElement.replace(comment.getTags()[0].getNameElement());
return this;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyFileImpl method setPackageName.
@Override
public void setPackageName(String packageName) {
final ASTNode fileNode = getNode();
final GrPackageDefinition currentPackage = getPackageDefinition();
if (packageName == null || packageName.isEmpty()) {
if (currentPackage != null) {
final ASTNode currNode = currentPackage.getNode();
fileNode.removeChild(currNode);
}
return;
}
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
final GrPackageDefinition newPackage = (GrPackageDefinition) factory.createTopElementFromText("package " + packageName);
if (currentPackage != null) {
final GrCodeReferenceElement packageReference = currentPackage.getPackageReference();
if (packageReference != null) {
GrCodeReferenceElement ref = newPackage.getPackageReference();
if (ref != null) {
packageReference.replace(ref);
}
return;
}
}
final ASTNode newNode = newPackage.getNode();
if (currentPackage != null) {
final ASTNode currNode = currentPackage.getNode();
fileNode.replaceChild(currNode, newNode);
} else {
ASTNode anchor = fileNode.getFirstChildNode();
if (anchor != null && anchor.getElementType() == GroovyTokenTypes.mSH_COMMENT) {
anchor = anchor.getTreeNext();
fileNode.addLeaf(GroovyTokenTypes.mNLS, "\n", anchor);
}
fileNode.addChild(newNode, anchor);
if (anchor != null && !anchor.getText().startsWith("\n\n")) {
fileNode.addLeaf(GroovyTokenTypes.mNLS, "\n", anchor);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrReferenceElementImpl method handleElementRename.
@Override
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
PsiElement nameElement = getReferenceNameElement();
if (nameElement != null) {
ASTNode node = nameElement.getNode();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
ASTNode newNameNode;
try {
newNameNode = factory.createReferenceNameFromText(newElementName).getNode();
} catch (IncorrectOperationException e) {
newNameNode = factory.createLiteralFromValue(newElementName).getFirstChild().getNode();
}
assert newNameNode != null && node != null;
getNode().replaceChild(node, newNameNode);
}
return this;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrModifierListImpl method addAnnotation.
@Override
@NotNull
public GrAnnotation addAnnotation(@NotNull @NonNls String qualifiedName) {
final PsiClass psiClass = JavaPsiFacade.getInstance(getProject()).findClass(qualifiedName, getResolveScope());
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
GrAnnotation annotation;
if (psiClass != null && psiClass.isAnnotationType()) {
annotation = (GrAnnotation) addAfter(factory.createModifierFromText("@xxx"), null);
annotation.getClassReference().bindToElement(psiClass);
} else {
annotation = (GrAnnotation) addAfter(factory.createModifierFromText("@" + qualifiedName), null);
}
final PsiElement parent = getParent();
if (!(parent instanceof GrParameter)) {
final ASTNode node = annotation.getNode();
final ASTNode treeNext = node.getTreeNext();
if (treeNext != null) {
getNode().addLeaf(TokenType.WHITE_SPACE, "\n", treeNext);
} else {
parent.getNode().addLeaf(TokenType.WHITE_SPACE, "\n", getNode().getTreeNext());
}
}
return annotation;
}
Aggregations