use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class NotAndParenthesesSurrounder method surroundExpression.
@Override
protected TextRange surroundExpression(@NotNull GrExpression expression, @Nullable PsiElement context) {
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(expression.getProject());
final GrUnaryExpression template = (GrUnaryExpression) factory.createExpressionFromText("!(a)", context);
assert template.getOperand() != null;
GroovyExpressionSurrounder.replaceToOldExpression(((GrParenthesizedExpression) template.getOperand()).getOperand(), expression);
final GrExpression result = expression.replaceWithExpression(template, true);
final int endOffset = result.getTextRange().getEndOffset();
return new TextRange(endOffset, endOffset);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project android by JetBrains.
the class JavaVersionDslElement method create.
@Override
@Nullable
public GroovyPsiElement create() {
GroovyPsiElement psiElement = getPsiElement();
if (psiElement != null) {
return psiElement;
}
GradlePropertiesDslElement parent = (GradlePropertiesDslElement) myParent;
assert parent != null;
GroovyPsiElement javaPsiElement = parent.create();
assert javaPsiElement != null;
ApplicationManager.getApplication().assertWriteAccessAllowed();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(javaPsiElement.getProject());
// Tries to create the new element close to targetCompatibility or sourceCompatibility, if neither of them exists, create it at the
// end of the file.
// Also, tries to copy the value from targetCompatibility or sourceCompatibility if possible to keep consistency.
JavaVersionDslElement anchor = null;
if (SOURCE_COMPATIBILITY_ATTRIBUTE_NAME.equals(myName)) {
anchor = parent.getPropertyElement(TARGET_COMPATIBILITY_ATTRIBUTE_NAME, JavaVersionDslElement.class);
} else if (TARGET_COMPATIBILITY_ATTRIBUTE_NAME.equals(myName)) {
anchor = parent.getPropertyElement(SOURCE_COMPATIBILITY_ATTRIBUTE_NAME, JavaVersionDslElement.class);
}
PsiElement anchorPsiElement = null;
String anchorText = null;
if (anchor != null) {
anchorPsiElement = anchor.getPsiElement();
anchorText = anchor.getVersionText();
}
if (anchorPsiElement == null) {
anchorPsiElement = javaPsiElement.getLastChild();
}
if (anchorText == null) {
anchorText = "1.6";
}
GrExpression newExpressionPsi;
GroovyPsiElement valuePsi;
if (myUseAssignment) {
GrExpression expression = factory.createExpressionFromText(myName + " = " + anchorText);
newExpressionPsi = (GrExpression) javaPsiElement.addBefore(expression, anchorPsiElement);
valuePsi = ((GrAssignmentExpression) newExpressionPsi).getRValue();
} else {
GrExpression expression = factory.createExpressionFromText(myName + " " + anchorText);
newExpressionPsi = (GrExpression) javaPsiElement.addBefore(expression, anchorPsiElement);
valuePsi = ((GrApplicationStatement) newExpressionPsi).getExpressionArguments()[0];
}
if (valuePsi instanceof GrLiteral) {
myInternalVersionElement = new GradleDslLiteral(parent, newExpressionPsi, myName, (GrLiteral) valuePsi);
} else if (valuePsi instanceof GrReferenceExpression) {
myInternalVersionElement = new GradleDslReference(parent, newExpressionPsi, myName, (GrReferenceExpression) valuePsi);
}
return getPsiElement();
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project android by JetBrains.
the class GradleFilePsiMerger method mergeDependencies.
private static void mergeDependencies(@NotNull PsiElement fromRoot, @NotNull PsiElement toRoot, @NotNull Project project, @Nullable String supportLibVersionFilter) {
Map<String, Multimap<String, GradleCoordinate>> dependencies = Maps.newHashMap();
final List<String> unparsedDependencies = Lists.newArrayList();
// Load existing dependencies into the map for the existing build.gradle
pullDependenciesIntoMap(toRoot, dependencies, null);
// Load dependencies into the map for the new build.gradle
pullDependenciesIntoMap(fromRoot, dependencies, unparsedDependencies);
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
RepositoryUrlManager urlManager = RepositoryUrlManager.get();
ImmutableList<String> configurations = CONFIGURATION_ORDERING.immutableSortedCopy(dependencies.keySet());
AndroidSdkData sdk = AndroidSdks.getInstance().tryToChooseAndroidSdk();
if (sdk != null) {
for (String configurationName : configurations) {
List<GradleCoordinate> resolved = urlManager.resolveDynamicSdkDependencies(dependencies.get(configurationName), supportLibVersionFilter, sdk, FileOpUtils.create());
for (GradleCoordinate dependency : resolved) {
PsiElement dependencyElement = factory.createStatementFromText(String.format("%s '%s'\n", configurationName, dependency.toString()));
toRoot.addBefore(dependencyElement, toRoot.getLastChild());
}
}
}
for (String dependency : unparsedDependencies) {
PsiElement dependencyElement = factory.createStatementFromText(dependency);
toRoot.addBefore(dependencyElement, toRoot.getLastChild());
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class MultipleRepositoryUrlsFix method doFix.
@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
List<GrCallExpression> statements = MultipleRepositoryUrlsInspection.findUrlCallExpressions(myClosure);
if (statements.size() <= 1)
return;
statements.remove(0);
List<PsiElement> elements = new ArrayList<>(statements);
for (GrCallExpression statement : statements) {
PsiElement newLineCandidate = statement.getNextSibling();
if (PsiUtil.isNewLine(newLineCandidate)) {
elements.add(newLineCandidate);
}
}
myClosure.removeElements(elements.toArray(new PsiElement[elements.size()]));
GrClosableBlock closableBlock = PsiTreeUtil.getParentOfType(myClosure, GrClosableBlock.class);
if (closableBlock == null)
return;
GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(project);
for (GrCallExpression statement : statements) {
closableBlock.addStatementBefore(elementFactory.createStatementFromText(myRepoType + '{' + statement.getText() + '}'), null);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrInspectionUtil method replaceExpression.
public static void replaceExpression(GrExpression expression, String newExpression) {
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(expression.getProject());
final GrExpression newCall = factory.createExpressionFromText(newExpression);
expression.replaceWithExpression(newCall, true);
}
Aggregations