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;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class CompleteReferencesWithSameQualifier method addVariantsWithSameQualifier.
private void addVariantsWithSameQualifier(@NotNull PsiElement element, @NotNull Set<String> result) {
if (element instanceof GrReferenceExpression && element != myRefExpr && !PsiUtil.isLValue((GroovyPsiElement) element)) {
final GrReferenceExpression refExpr = (GrReferenceExpression) element;
final String refName = refExpr.getReferenceName();
if (refName != null && !result.contains(refName) && myMatcher.prefixMatches(refName)) {
final GrExpression hisQualifier = refExpr.getQualifierExpression();
if (hisQualifier != null && myQualifier != null) {
if (PsiEquivalenceUtil.areElementsEquivalent(hisQualifier, myQualifier)) {
if (refExpr.resolve() == null) {
result.add(refName);
}
}
} else if (hisQualifier == null && myQualifier == null) {
if (refExpr.resolve() == null) {
result.add(refName);
}
}
}
}
for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
addVariantsWithSameQualifier(child, result);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class GroovyPositionManager method getAllClasses.
@Override
@NotNull
public List<ReferenceType> getAllClasses(@NotNull final SourcePosition position) throws NoDataException {
if (LOG.isDebugEnabled()) {
LOG.debug("getAllClasses: " + position);
}
checkGroovyFile(position);
List<ReferenceType> result = ApplicationManager.getApplication().runReadAction(new Computable<List<ReferenceType>>() {
@Override
public List<ReferenceType> compute() {
GroovyPsiElement sourceImage = findReferenceTypeSourceImage(position);
if (sourceImage instanceof GrTypeDefinition && !((GrTypeDefinition) sourceImage).isAnonymous()) {
String qName = getClassNameForJvm((GrTypeDefinition) sourceImage);
if (qName != null)
return myDebugProcess.getVirtualMachineProxy().classesByName(qName);
} else if (sourceImage == null) {
final String scriptName = getScriptQualifiedName(position);
if (scriptName != null)
return myDebugProcess.getVirtualMachineProxy().classesByName(scriptName);
} else {
String enclosingName = findEnclosingName(position);
if (enclosingName == null)
return null;
final List<ReferenceType> outers = myDebugProcess.getVirtualMachineProxy().classesByName(enclosingName);
final List<ReferenceType> result = new ArrayList<>(outers.size());
for (ReferenceType outer : outers) {
final ReferenceType nested = findNested(outer, sourceImage, position);
if (nested != null) {
result.add(nested);
}
}
return result;
}
return null;
}
});
if (LOG.isDebugEnabled()) {
LOG.debug("getAllClasses = " + result);
}
if (result == null)
throw NoDataException.INSTANCE;
return result;
}
Aggregations