use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyBraceEnforcer method replaceWithBlock.
private void replaceWithBlock(@NotNull GrStatement statement, GrStatement blockCandidate) {
if (!statement.isValid()) {
LOG.assertTrue(false);
}
if (!checkRangeContainsElement(blockCandidate))
return;
final PsiManager manager = statement.getManager();
LOG.assertTrue(manager != null);
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(manager.getProject());
String oldText = blockCandidate.getText();
// There is a possible case that target block to wrap ends with single-line comment. Example:
// if (true) i = 1; // Cool assignment
// We can't just surround target block of code with curly braces because the closing one will be treated as comment as well.
// Hence, we perform a check if we have such situation at the moment and insert new line before the closing brace.
StringBuilder buf = new StringBuilder(oldText.length() + 5);
buf.append("{\n").append(oldText);
buf.append("\n}");
final int oldTextLength = statement.getTextLength();
try {
ASTNode newChild = SourceTreeToPsiMap.psiElementToTree(factory.createBlockStatementFromText(buf.toString(), null));
ASTNode parent = SourceTreeToPsiMap.psiElementToTree(statement);
ASTNode childToReplace = SourceTreeToPsiMap.psiElementToTree(blockCandidate);
CodeEditUtil.replaceChild(parent, childToReplace, newChild);
removeTailSemicolon(newChild, parent);
CodeStyleManager.getInstance(statement.getProject()).reformat(statement, true);
} catch (IncorrectOperationException e) {
LOG.error(e);
} finally {
updateResultRange(oldTextLength, statement.getTextLength());
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrCreateMissingSwitchBranchesIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
if (!(element instanceof GrSwitchStatement))
return;
final List<PsiEnumConstant> constants = findUnusedConstants((GrSwitchStatement) element);
if (constants.isEmpty())
return;
final PsiEnumConstant first = constants.get(0);
final PsiClass aClass = first.getContainingClass();
if (aClass == null)
return;
String qName = aClass.getQualifiedName();
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
PsiElement anchor = findAnchor(element);
for (PsiEnumConstant constant : constants) {
final GrCaseSection section = factory.createSwitchSection("case " + qName + "." + constant.getName() + ":\n break");
final PsiElement added = element.addBefore(section, anchor);
element.addBefore(factory.createLineTerminator(1), anchor);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(added);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrSplitDeclarationIntention method processSingleVar.
private static void processSingleVar(Project project, GrVariableDeclaration declaration, GrVariable variable) {
GrExpression initializer = variable.getInitializerGroovy();
if (initializer != null) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
GrExpression assignment = factory.createExpressionFromText(variable.getName() + " = " + initializer.getText());
initializer.delete();
declaration = GroovyRefactoringUtil.addBlockIntoParent(declaration);
declaration.getParent().addAfter(assignment, declaration);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyExtractMethodHandler method renameParameterOccurrences.
private static void renameParameterOccurrences(GrMethod method, ExtractMethodInfoHelper helper) throws IncorrectOperationException {
GrOpenBlock block = method.getBlock();
if (block == null)
return;
GrStatement[] statements = block.getStatements();
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(helper.getProject());
for (ParameterInfo info : helper.getParameterInfos()) {
final String oldName = info.getOriginalName();
final String newName = info.getName();
final ArrayList<GrExpression> result = new ArrayList<>();
if (!oldName.equals(newName)) {
for (final GrStatement statement : statements) {
statement.accept(new PsiRecursiveElementVisitor() {
@Override
public void visitElement(final PsiElement element) {
super.visitElement(element);
if (element instanceof GrReferenceExpression) {
GrReferenceExpression expr = (GrReferenceExpression) element;
if (!expr.isQualified() && oldName.equals(expr.getReferenceName())) {
result.add(expr);
}
}
}
});
for (GrExpression expr : result) {
expr.replaceWithExpression(factory.createExpressionFromText(newName), false);
}
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyMethodInliner method prepareNewMethod.
/**
* Prepare temporary method with non-conflicting local names
*/
@NotNull
private static GrMethod prepareNewMethod(@NotNull GrCallExpression call, @NotNull GrMethod method, @Nullable GrExpression qualifier) throws IncorrectOperationException {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
if (method instanceof GrReflectedMethod) {
method = ((GrReflectedMethod) method).getBaseMethod();
}
GrMethod newMethod = factory.createMethodFromText(method.getText(), call);
if (qualifier != null) {
Collection<GroovyInlineMethodUtil.ReferenceExpressionInfo> infos = GroovyInlineMethodUtil.collectReferenceInfo(method);
GroovyInlineMethodUtil.addQualifiersToInnerReferences(newMethod, infos, qualifier);
}
ArrayList<PsiNamedElement> innerDefinitions = new ArrayList<>();
collectInnerDefinitions(newMethod.getBlock(), innerDefinitions);
// there are only local variables and parameters (possible of inner closures)
for (PsiNamedElement namedElement : innerDefinitions) {
String name = namedElement.getName();
if (name != null) {
String newName = qualifier instanceof GrReferenceExpression ? InlineMethodConflictSolver.suggestNewName(name, method, call, ((GrReferenceExpression) qualifier).getReferenceName()) : InlineMethodConflictSolver.suggestNewName(name, method, call);
if (!newName.equals(namedElement.getName())) {
final Collection<PsiReference> refs = ReferencesSearch.search(namedElement).findAll();
for (PsiReference ref : refs) {
PsiElement element = ref.getElement();
if (element instanceof GrReferenceExpression) {
GrExpression newExpr = factory.createExpressionFromText(newName);
((GrReferenceExpression) element).replaceWithExpression(newExpr, false);
}
}
namedElement.setName(newName);
}
}
}
GroovyInlineMethodUtil.replaceParametersWithArguments(call, newMethod);
return newMethod;
}
Aggregations