use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.
the class ConvertGStringToStringIntention method convertGStringLiteralToStringLiteral.
public static String convertGStringLiteralToStringLiteral(GrLiteral literal) {
PsiElement child = literal.getFirstChild();
if (child == null)
return literal.getText();
String text;
ArrayList<String> list = new ArrayList<>();
PsiElement prevSibling = null;
PsiElement nextSibling;
do {
text = child.getText();
nextSibling = child.getNextSibling();
if (child instanceof GrStringInjection) {
if (((GrStringInjection) child).getClosableBlock() != null) {
text = prepareClosableBlock(((GrStringInjection) child).getClosableBlock());
} else if (((GrStringInjection) child).getExpression() != null) {
text = prepareExpression(((GrStringInjection) child).getExpression());
} else {
text = child.getText();
}
} else {
text = prepareText(text, prevSibling == null, nextSibling == null, nextSibling instanceof GrClosableBlock || nextSibling instanceof GrReferenceExpression);
}
if (text != null) {
list.add(text);
}
prevSibling = child;
child = child.getNextSibling();
} while (child != null);
StringBuilder builder = new StringBuilder(literal.getTextLength() * 2);
if (list.isEmpty())
return "''";
builder.append(list.get(0));
for (int i = 1; i < list.size(); i++) {
builder.append(" + ").append(list.get(i));
}
return builder.toString();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.
the class MyPredicate method getParameterByArgument.
@Nullable
private static GrParameter getParameterByArgument(GrExpression arg) {
PsiElement parent = PsiUtil.skipParentheses(arg.getParent(), true);
if (!(parent instanceof GrArgumentList))
return null;
final GrArgumentList argList = (GrArgumentList) parent;
parent = parent.getParent();
if (!(parent instanceof GrMethodCall))
return null;
final GrMethodCall methodCall = (GrMethodCall) parent;
final GrExpression expression = methodCall.getInvokedExpression();
if (!(expression instanceof GrReferenceExpression))
return null;
final GroovyResolveResult resolveResult = ((GrReferenceExpression) expression).advancedResolve();
if (resolveResult == null)
return null;
GrClosableBlock[] closures = methodCall.getClosureArguments();
final Map<GrExpression, Pair<PsiParameter, PsiType>> mapToParams = GrClosureSignatureUtil.mapArgumentsToParameters(resolveResult, arg, false, false, argList.getNamedArguments(), argList.getExpressionArguments(), closures);
if (mapToParams == null)
return null;
final Pair<PsiParameter, PsiType> parameterPair = mapToParams.get(arg);
final PsiParameter parameter = parameterPair == null ? null : parameterPair.getFirst();
return parameter instanceof GrParameter ? ((GrParameter) parameter) : null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.
the class ConvertJavaStyleArrayIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrClosableBlock block = ((GrMethodCallExpression) element).getClosureArguments()[0];
final String text = block.getText();
int start = block.getLBrace().getStartOffsetInParent() + 1;
int finish = block.getRBrace().getStartOffsetInParent();
String newText = "[" + text.substring(start, finish) + "]";
final GrExpression newExpr = GroovyPsiElementFactory.getInstance(element.getProject()).createExpressionFromText(newText);
((GrMethodCallExpression) element).replaceWithStatement(newExpr);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.
the class ExtractClosureHelperImpl method getSelectedType.
@Override
public PsiType getSelectedType() {
if (myForceDef)
return null;
if (myType == null) {
final GrClosableBlock closure = ExtractClosureProcessorBase.generateClosure(this);
PsiType type = closure.getType();
if (type instanceof PsiClassType) {
final PsiType[] parameters = ((PsiClassType) type).getParameters();
if (parameters.length == 1 && parameters[0] != null) {
if (parameters[0].equalsToText(PsiType.VOID.getBoxedTypeName())) {
type = ((PsiClassType) type).rawType();
}
}
}
myType = type;
}
return myType;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.
the class GroovyInlineLocalHandler method createSettings.
/**
* Returns Settings object for referenced definition in case of local variable
*/
@Nullable
private static InlineLocalVarSettings createSettings(final GrVariable variable, Editor editor, boolean invokedOnReference) {
final String localName = variable.getName();
final Project project = variable.getProject();
GrExpression initializer = null;
Instruction writeInstr = null;
Instruction[] flow = null;
//search for initializer to inline
if (invokedOnReference) {
LOG.assertTrue(editor != null, "null editor but invokedOnReference==true");
final PsiReference ref = TargetElementUtil.findReference(editor);
LOG.assertTrue(ref != null);
PsiElement cur = ref.getElement();
if (cur instanceof GrReferenceExpression) {
GrControlFlowOwner controlFlowOwner;
do {
controlFlowOwner = ControlFlowUtils.findControlFlowOwner(cur);
if (controlFlowOwner == null)
break;
flow = controlFlowOwner.getControlFlow();
final List<BitSet> writes = ControlFlowUtils.inferWriteAccessMap(flow, variable);
final PsiElement finalCur = cur;
Instruction instruction = ControlFlowUtils.findInstruction(finalCur, flow);
LOG.assertTrue(instruction != null);
final BitSet prev = writes.get(instruction.num());
if (prev.cardinality() == 1) {
writeInstr = flow[prev.nextSetBit(0)];
final PsiElement element = writeInstr.getElement();
if (element instanceof GrVariable) {
initializer = ((GrVariable) element).getInitializerGroovy();
} else if (element instanceof GrReferenceExpression) {
initializer = TypeInferenceHelper.getInitializerFor((GrReferenceExpression) element);
}
}
PsiElement old_cur = cur;
if (controlFlowOwner instanceof GrClosableBlock) {
cur = controlFlowOwner;
} else {
PsiElement parent = controlFlowOwner.getParent();
if (parent instanceof GrMember)
cur = ((GrMember) parent).getContainingClass();
}
if (cur == old_cur)
break;
} while (initializer == null);
}
} else {
flow = ControlFlowUtils.findControlFlowOwner(variable).getControlFlow();
initializer = variable.getInitializerGroovy();
writeInstr = ContainerUtil.find(flow, instruction -> instruction.getElement() == variable);
}
if (initializer == null || writeInstr == null) {
String message = GroovyRefactoringBundle.message("cannot.find.a.single.definition.to.inline.local.var");
CommonRefactoringUtil.showErrorHint(variable.getProject(), editor, message, INLINE_VARIABLE, HelpID.INLINE_VARIABLE);
return null;
}
int writeInstructionNumber = writeInstr.num();
if (ApplicationManager.getApplication().isUnitTestMode()) {
return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
}
final String question = GroovyRefactoringBundle.message("inline.local.variable.prompt.0.1", localName);
RefactoringMessageDialog dialog = new RefactoringMessageDialog(INLINE_VARIABLE, question, HelpID.INLINE_VARIABLE, "OptionPane.questionIcon", true, project);
if (dialog.showAndGet()) {
return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
}
return null;
}
Aggregations