use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project android by JetBrains.
the class GradleDslMethodCall method create.
@Override
@Nullable
public GroovyPsiElement create() {
GroovyPsiElement psiElement = getPsiElement();
if (psiElement != null) {
return psiElement;
}
if (myParent == null) {
return null;
}
GroovyPsiElement parentPsiElement = myParent.create();
if (parentPsiElement == null) {
return null;
}
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentPsiElement.getProject());
String statementText = (myStatementName != null ? myStatementName + " " : "") + myName + "()";
GrStatement statement = factory.createStatementFromText(statementText);
PsiElement addedElement = parentPsiElement.addBefore(statement, parentPsiElement.getLastChild());
if (addedElement instanceof GrApplicationStatement) {
GrExpression[] expressionArguments = ((GrApplicationStatement) addedElement).getArgumentList().getExpressionArguments();
if (expressionArguments.length == 1 && expressionArguments[0] instanceof GrMethodCallExpression) {
setPsiElement(expressionArguments[0]);
return getPsiElement();
}
}
if (addedElement instanceof GrMethodCallExpression) {
setPsiElement((GrMethodCallExpression) addedElement);
return getPsiElement();
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project android by JetBrains.
the class GradleDslLiteral method create.
@Override
@Nullable
public GroovyPsiElement create() {
if (!(myParent instanceof GradleDslExpressionMap)) {
return super.create();
}
// This is a value in the map element we need to create a named argument for it.
GroovyPsiElement parentPsiElement = myParent.create();
if (parentPsiElement == null) {
return null;
}
setPsiElement(parentPsiElement);
GrLiteral newLiteral = createLiteral();
if (newLiteral == null) {
return null;
}
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(newLiteral.getProject());
GrNamedArgument namedArgument = factory.createNamedArgument(myName, newLiteral);
PsiElement added;
if (parentPsiElement instanceof GrArgumentList) {
added = ((GrArgumentList) parentPsiElement).addNamedArgument(namedArgument);
} else {
added = parentPsiElement.addAfter(namedArgument, parentPsiElement.getLastChild());
}
if (added instanceof GrNamedArgument) {
GrNamedArgument addedNameArgument = (GrNamedArgument) added;
GrLiteral literal = getChildOfType(addedNameArgument, GrLiteral.class);
if (literal != null) {
myExpression = literal;
setModified(false);
return getPsiElement();
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project freeline by alibaba.
the class GradleUtil method applyPlugin.
/**
* 插入插件的表达式
* apply plugin: 'com.antfortune.freeline'
*
* @param project
* @param psiFile
* @param pluginId
*/
public static void applyPlugin(Project project, GroovyFile psiFile, String pluginId) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
GrStatement grStatement = factory.createExpressionFromText(String.format("apply plugin: \'%s\'", new Object[] { pluginId }), null);
GrExpression expression = GroovyFileUil.getLastPlugin(psiFile);
if (expression != null && expression.getParent() != null) {
psiFile.addAfter(grStatement, expression.getParent());
// 换行
psiFile.addAfter(factory.createLineTerminator("\n"), expression.getParent());
}
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
Document document = documentManager.getDocument(psiFile);
if (document != null) {
documentManager.commitDocument(document);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project android by JetBrains.
the class GradleGroovyFile method createNewValue.
/**
* Creates a new, blank-valued property at the given path.
*/
@Nullable
static GrMethodCall createNewValue(@NotNull GrStatementOwner root, @NotNull BuildFileKey key, @Nullable Object value, boolean reformatClosure) {
// First iterate through the components of the path and make sure all of the nested closures are in place.
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(root.getProject());
String path = key.getPath();
String[] parts = path.split("/");
GrStatementOwner parent = root;
for (int i = 0; i < parts.length - 1; i++) {
String part = parts[i];
GrStatementOwner closure = getMethodClosureArgument(parent, part);
if (closure == null) {
parent.addStatementBefore(factory.createStatementFromText(part + " {}"), null);
closure = getMethodClosureArgument(parent, part);
if (closure == null) {
return null;
}
}
parent = closure;
}
String name = parts[parts.length - 1];
String text = name + " " + key.getType().convertValueToExpression(value);
GrStatement statementBefore = null;
if (key.shouldInsertAtBeginning()) {
GrStatement[] parentStatements = parent.getStatements();
if (parentStatements.length > 0) {
statementBefore = parentStatements[0];
}
}
parent.addStatementBefore(factory.createStatementFromText(text), statementBefore);
if (reformatClosure) {
reformatClosure(parent);
}
return getMethodCall(parent, name);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project android by JetBrains.
the class BuildFileStatementFactory method setValues.
@Override
public void setValues(@NotNull GrStatementOwner closure, @NotNull List<BuildFileStatement> statements, @Nullable KeyFilter filter) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(closure.getProject());
closure = (GrStatementOwner) closure.replace(factory.createClosureFromText("{\n}"));
for (BuildFileStatement statement : statements) {
for (PsiElement element : statement.getGroovyElements(factory)) {
closure.addBefore(element, closure.getLastChild());
}
}
GradleGroovyFile.reformatClosure(closure);
}
Aggregations