use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project android by JetBrains.
the class GradleDslElement method create.
/**
* Creates the {@link GroovyPsiElement} by adding this element to the .gradle file.
*
* <p>It creates a new {@link GroovyPsiElement} only when {@link #getPsiElement()} return {@code null}.
*
* <p>Returns the final {@link GroovyPsiElement} corresponds to this element or {@code null} when failed to create the
* {@link GroovyPsiElement}.
*/
@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;
}
Project project = parentPsiElement.getProject();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
if (isNewEmptyBlockElement()) {
// Avoid creation of an empty block statement.
return null;
}
String statementText = myName + (isBlockElement() ? " {\n}\n" : " \"abc\", \"xyz\"");
GrStatement statement = factory.createStatementFromText(statementText);
if (statement instanceof GrApplicationStatement) {
// Workaround to create an application statement.
((GrApplicationStatement) statement).getArgumentList().delete();
}
PsiElement lineTerminator = factory.createLineTerminator(1);
PsiElement addedElement;
if (parentPsiElement instanceof GroovyFile) {
addedElement = parentPsiElement.addAfter(statement, parentPsiElement.getLastChild());
parentPsiElement.addBefore(lineTerminator, addedElement);
} else {
addedElement = parentPsiElement.addBefore(statement, parentPsiElement.getLastChild());
parentPsiElement.addAfter(lineTerminator, addedElement);
}
if (isBlockElement()) {
GrClosableBlock closableBlock = getClosableBlock(addedElement);
if (closableBlock != null) {
setPsiElement(closableBlock);
}
} else {
if (addedElement instanceof GrApplicationStatement) {
setPsiElement((GrApplicationStatement) addedElement);
}
}
return getPsiElement();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement 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.api.statements.GrStatement 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.api.statements.GrStatement 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.api.statements.GrStatement in project android by JetBrains.
the class GradleSettingsFile method addModule.
/**
* Adds a reference to the module to the settings file, if there is not already one. The module path must be colon separated, with a
* leading colon, e.g. ":project:subproject". Must be run inside a write action.
*
* If the file does not match the default module location, this method will override the location.
*/
public void addModule(@NotNull String modulePath, @NotNull File location) {
checkInitialized();
commitDocumentChanges();
for (GrMethodCall includeStatement : getMethodCalls(myGroovyFile, INCLUDE_METHOD)) {
for (GrLiteral lit : getLiteralArguments(includeStatement)) {
if (modulePath.equals(lit.getValue())) {
return;
}
}
}
GrMethodCall includeStatement = getMethodCall(myGroovyFile, INCLUDE_METHOD);
if (includeStatement != null) {
GrArgumentList argList = includeStatement.getArgumentList();
GrLiteral literal = GroovyPsiElementFactory.getInstance(myProject).createLiteralFromValue(modulePath);
argList.addAfter(literal, argList.getLastChild());
} else {
GrStatement statement = GroovyPsiElementFactory.getInstance(myProject).createStatementFromText(INCLUDE_METHOD + " '" + modulePath + "'");
myGroovyFile.add(statement);
}
// We get location relative to this file parent
VirtualFile parent = getFile().getParent();
File defaultLocation = GradleUtil.getModuleDefaultPath(parent, modulePath);
if (!FileUtil.filesEqual(defaultLocation, location)) {
final String path;
File parentFile = VfsUtilCore.virtualToIoFile(parent);
if (FileUtil.isAncestor(parentFile, location, true)) {
path = PathUtil.toSystemIndependentName(FileUtil.getRelativePath(parentFile, location));
} else {
path = PathUtil.toSystemIndependentName(location.getAbsolutePath());
}
String locationAssignment = String.format(CUSTOM_LOCATION_FORMAT, modulePath, path);
GrStatement locationStatement = GroovyPsiElementFactory.getInstance(myProject).createStatementFromText(locationAssignment);
myGroovyFile.add(locationStatement);
}
}
Aggregations