use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class GroovyPsiElementImpl method acceptGroovyChildren.
public static void acceptGroovyChildren(PsiElement parent, GroovyElementVisitor visitor) {
PsiElement child = parent.getFirstChild();
while (child != null) {
ProgressManager.checkCanceled();
if (child instanceof GroovyPsiElement) {
((GroovyPsiElement) child).accept(visitor);
}
child = child.getNextSibling();
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class GrReassignedLocalVarsChecker method isReassignedVarImpl.
private static boolean isReassignedVarImpl(@NotNull final GrVariable resolved) {
final GrControlFlowOwner variableScope = PsiTreeUtil.getParentOfType(resolved, GrCodeBlock.class, GroovyFile.class);
if (variableScope == null)
return false;
final String name = resolved.getName();
final Ref<Boolean> isReassigned = Ref.create(false);
for (PsiElement scope = resolved.getParent().getNextSibling(); scope != null; scope = scope.getNextSibling()) {
if (scope instanceof GroovyPsiElement) {
((GroovyPsiElement) scope).accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitClosure(@NotNull GrClosableBlock closure) {
if (getUsedVarsInsideBlock(closure).contains(name)) {
isReassigned.set(true);
}
}
@Override
public void visitElement(@NotNull GroovyPsiElement element) {
if (isReassigned.get())
return;
super.visitElement(element);
}
});
if (isReassigned.get())
break;
}
}
return isReassigned.get();
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class ConvertConcatenationToGstringIntention method getOperandText.
private static void getOperandText(@Nullable GrExpression operand, StringBuilder builder, boolean multiline) {
if (operand instanceof GrRegex) {
for (GroovyPsiElement element : ((GrRegex) operand).getAllContentParts()) {
if (element instanceof GrStringInjection) {
builder.append(element.getText());
} else if (element instanceof GrStringContent) {
if (GrStringUtil.isDollarSlashyString((GrLiteral) operand)) {
processDollarSlashyContent(builder, multiline, element.getText());
} else {
processSlashyContent(builder, multiline, element.getText());
}
}
}
} else if (operand instanceof GrString) {
boolean isMultiline = GrStringUtil.isMultilineStringLiteral((GrLiteral) operand);
for (GroovyPsiElement element : ((GrString) operand).getAllContentParts()) {
if (element instanceof GrStringInjection) {
builder.append(element.getText());
} else if (element instanceof GrStringContent) {
if (isMultiline) {
processMultilineGString(builder, element.getText());
} else {
processSinglelineGString(builder, element.getText());
}
}
}
} else if (operand instanceof GrLiteral) {
String text = GrStringUtil.removeQuotes(operand.getText());
GrLiteral literal = (GrLiteral) operand;
if (GrStringUtil.isSingleQuoteString(literal)) {
processSinglelineString(builder, text);
} else if (GrStringUtil.isTripleQuoteString(literal)) {
processMultilineString(builder, text);
} else if (GrStringUtil.isDoubleQuoteString(literal)) {
processSinglelineGString(builder, text);
} else if (GrStringUtil.isTripleDoubleQuoteString(literal)) {
processMultilineGString(builder, text);
} else if (GrStringUtil.isSlashyString(literal)) {
processSlashyContent(builder, multiline, text);
} else if (GrStringUtil.isDollarSlashyString(literal)) {
processDollarSlashyContent(builder, multiline, text);
}
} else if (MyPredicate.satisfied(operand)) {
performIntention((GrBinaryExpression) operand, builder, multiline);
} else if (isToStringMethod(operand, builder)) {
//nothing to do
} else {
builder.append(START_BRACE).append(operand == null ? "" : operand.getText()).append(END_BRACE);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class ConvertMultilineStringToSingleLineIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
String quote = element.getText().substring(0, 1);
StringBuilder buffer = new StringBuilder();
buffer.append(quote);
GrExpression old;
if (element instanceof GrLiteralImpl) {
appendSimpleStringValue(element, buffer, quote);
old = (GrExpression) element;
} else {
final GrStringImpl gstring = (GrStringImpl) element;
for (GroovyPsiElement child : gstring.getAllContentParts()) {
if (child instanceof GrStringContent) {
appendSimpleStringValue(child, buffer, "\"");
} else if (child instanceof GrStringInjection) {
buffer.append(child.getText());
}
}
old = gstring;
}
buffer.append(quote);
try {
final int offset = editor.getCaretModel().getOffset();
final TextRange range = old.getTextRange();
int shift;
if (range.getStartOffset() == offset) {
shift = 0;
} else if (range.getStartOffset() == offset - 1) {
shift = -1;
} else if (range.getEndOffset() == offset) {
shift = -4;
} else if (range.getEndOffset() == offset + 1) {
shift = -3;
} else {
shift = -2;
}
final GrExpression newLiteral = GroovyPsiElementFactory.getInstance(project).createExpressionFromText(buffer.toString());
old.replaceWithExpression(newLiteral, true);
if (shift != 0) {
editor.getCaretModel().moveToOffset(offset + shift);
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class RemoveParenthesesFromMethodPredicate method satisfiedBy.
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof GrMethodCallExpression))
return false;
if (!PsiUtil.isExpressionStatement(element))
return false;
final GrMethodCallExpression expression = (GrMethodCallExpression) element;
if (PsiImplUtil.hasClosureArguments(expression))
return false;
final StringBuilder newStatementText = new StringBuilder();
newStatementText.append(expression.getInvokedExpression().getText()).append(' ');
GrArgumentList argumentList = expression.getArgumentList();
final GroovyPsiElement[] allArguments = argumentList.getAllArguments();
argumentList = (GrArgumentList) argumentList.copy();
final PsiElement leftParen = argumentList.getLeftParen();
final PsiElement rightParen = argumentList.getRightParen();
if (leftParen != null)
leftParen.delete();
if (rightParen != null)
rightParen.delete();
newStatementText.append(argumentList.getText());
final GrStatement newStatement;
try {
newStatement = GroovyPsiElementFactory.getInstance(element.getProject()).createStatementFromText(newStatementText.toString());
} catch (IncorrectOperationException e) {
return false;
}
if (newStatement instanceof GrApplicationStatement) {
final GrCommandArgumentList newArgList = ((GrApplicationStatement) newStatement).getArgumentList();
if (newArgList.getAllArguments().length == allArguments.length) {
return true;
}
}
return false;
}
Aggregations