use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrStringUtil method wrapGStringInto.
private static void wrapGStringInto(GrString grString, String quotes) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(grString.getProject());
final PsiElement firstChild = grString.getFirstChild();
final PsiElement lastChild = grString.getLastChild();
final GrExpression template = factory.createExpressionFromText(quotes + "$x" + quotes);
if (firstChild != null && firstChild.getNode().getElementType() == GroovyTokenTypes.mGSTRING_BEGIN && !quotes.equals(firstChild.getText())) {
grString.getNode().replaceChild(firstChild.getNode(), template.getFirstChild().getNode());
}
if (lastChild != null && lastChild.getNode().getElementType() == GroovyTokenTypes.mGSTRING_END && !quotes.equals(lastChild.getText())) {
grString.getNode().replaceChild(lastChild.getNode(), template.getLastChild().getNode());
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrStringUtil method createStringFromRegex.
public static GrLiteral createStringFromRegex(@NotNull GrLiteral regex) {
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(regex.getProject());
if (regex instanceof GrRegex) {
StringBuilder builder = new StringBuilder();
String quote = regex.getText().indexOf('\n') >= 0 ? TRIPLE_DOUBLE_QUOTES : DOUBLE_QUOTES;
builder.append(quote);
for (PsiElement child = regex.getFirstChild(); child != null; child = child.getNextSibling()) {
final IElementType type = child.getNode().getElementType();
if (type == GroovyTokenTypes.mREGEX_CONTENT || type == GroovyElementTypes.GSTRING_CONTENT) {
builder.append(escapeSymbolsForGString(unescapeSlashyString(child.getText()), quote.equals(DOUBLE_QUOTES), true));
} else if (type == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_CONTENT) {
builder.append(escapeSymbolsForGString(unescapeDollarSlashyString(child.getText()), quote.equals(DOUBLE_QUOTES), true));
} else if (type == GroovyElementTypes.GSTRING_INJECTION) {
builder.append(child.getText());
}
}
builder.append(quote);
return (GrLiteral) factory.createExpressionFromText(builder.toString());
} else {
Object value = regex.getValue();
LOG.assertTrue(value == null || value instanceof String);
if (value == null) {
value = removeQuotes(regex.getText());
}
return factory.createLiteralFromValue(value);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory 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.GroovyPsiElementFactory in project android by JetBrains.
the class GradleDslExpressionList method create.
@Override
@Nullable
public GroovyPsiElement create() {
GroovyPsiElement psiElement = getPsiElement();
if (psiElement == null) {
if (myParent instanceof GradleDslExpressionMap) {
// This is a list in the map element and we need to create a named argument for it.
return createNamedArgumentList();
}
psiElement = super.create();
}
if (psiElement == null) {
return null;
}
if (psiElement instanceof GrListOrMap) {
return psiElement;
}
if (psiElement instanceof GrArgumentList) {
if (!myToBeAddedExpressions.isEmpty() && ((GrArgumentList) psiElement).getAllArguments().length == 1 && !myAppendToArgumentListWithOneElement) {
// Sometimes it's not possible to append to the arguments list with one item. eg. proguardFile "xyz".
// Set the psiElement to null and create a new psiElement of an empty application statement.
setPsiElement(null);
psiElement = super.create();
} else {
return psiElement;
}
}
if (psiElement instanceof GrApplicationStatement) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(psiElement.getProject());
GrArgumentList argumentList = factory.createArgumentListFromText("xyz");
// Workaround to get an empty argument list.
argumentList.getFirstChild().delete();
PsiElement added = psiElement.addAfter(argumentList, psiElement.getLastChild());
if (added instanceof GrArgumentList) {
GrArgumentList addedArgumentList = (GrArgumentList) added;
setPsiElement(addedArgumentList);
return addedArgumentList;
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project android by JetBrains.
the class ArtifactDependencyModel method buildExcludesBlock.
private static GrClosableBlock buildExcludesBlock(@NotNull List<ArtifactDependencySpec> excludes, @NotNull Project project) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
GrClosableBlock block = factory.createClosureFromText("{\n}");
for (ArtifactDependencySpec spec : excludes) {
String text = String.format("exclude group: '%s', module: '%s'", spec.group, spec.name);
block.addBefore(factory.createStatementFromText(text), block.getLastChild());
PsiElement lineTerminator = factory.createLineTerminator(1);
block.addBefore(lineTerminator, block.getLastChild());
}
return block;
}
Aggregations