use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class GroovyElseSelectioner method select.
@Nullable
@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
if (!(e instanceof GrIfStatement))
return null;
GrIfStatement ifSt = (GrIfStatement) e;
GrStatement branch = ifSt.getElseBranch();
PsiElement elseKeyword = ifSt.getElseKeyword();
if (branch == null || elseKeyword == null)
return null;
return expandToWholeLine(editorText, new TextRange(elseKeyword.getTextRange().getStartOffset(), branch.getTextRange().getEndOffset()));
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class GroovyStatementSelectioner method select.
@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
TextRange originalRange;
PsiElement first;
PsiElement last;
if (e instanceof GrStatement) {
first = e;
PsiElement next = e.getNextSibling();
next = skipWhitespacesForward(next);
if (next != null && next.getNode().getElementType() == GroovyTokenTypes.mSEMI) {
originalRange = new TextRange(e.getTextRange().getStartOffset(), next.getTextRange().getEndOffset());
last = next;
} else {
originalRange = e.getTextRange();
last = e;
}
} else {
last = e;
PsiElement prev = e.getPrevSibling();
prev = skipWhitespaceBack(prev);
if (prev instanceof GrStatement) {
originalRange = new TextRange(prev.getTextRange().getStartOffset(), e.getTextRange().getEndOffset());
first = prev;
} else {
originalRange = e.getTextRange();
first = e;
}
}
final List<TextRange> ranges = ExtendWordSelectionHandlerBase.expandToWholeLine(editorText, originalRange);
final TextRange blockRange = inferBlockRange(first, last);
if (!blockRange.equals(originalRange)) {
ranges.addAll(ExtendWordSelectionHandlerBase.expandToWholeLine(editorText, blockRange, true));
}
return ranges;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class GroovyExtractMethodHandler method findConflicts.
private static boolean findConflicts(InitialInfo info) {
//new ConflictsDialog()
final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
final PsiElement declarationOwner = info.getContext().getParent();
GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
super.visitReferenceExpression(referenceExpression);
GroovyResolveResult resolveResult = referenceExpression.advancedResolve();
PsiElement resolveContext = resolveResult.getCurrentFileResolveContext();
if (resolveContext != null && !(resolveContext instanceof GrImportStatement) && !PsiTreeUtil.isAncestor(declarationOwner, resolveContext, true) && !skipResult(resolveResult)) {
conflicts.putValue(referenceExpression, GroovyRefactoringBundle.message("ref.0.will.not.be.resolved.outside.of.current.context", referenceExpression.getText()));
}
}
//skip 'print' and 'println'
private boolean skipResult(GroovyResolveResult result) {
PsiElement element = result.getElement();
if (element instanceof PsiMethod) {
String name = ((PsiMethod) element).getName();
if (!name.startsWith("print"))
return false;
if (element instanceof GrGdkMethod)
element = ((GrGdkMethod) element).getStaticMethod();
PsiClass aClass = ((PsiMethod) element).getContainingClass();
if (aClass != null) {
String qname = aClass.getQualifiedName();
return GroovyCommonClassNames.DEFAULT_GROOVY_METHODS.equals(qname);
}
}
return false;
}
};
GrStatement[] statements = info.getStatements();
for (GrStatement statement : statements) {
statement.accept(visitor);
}
if (conflicts.isEmpty())
return false;
if (ApplicationManager.getApplication().isUnitTestMode()) {
throw new BaseRefactoringProcessor.ConflictsInTestsException(conflicts.values());
}
ConflictsDialog dialog = new ConflictsDialog(info.getProject(), conflicts);
dialog.show();
return !dialog.isOK();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class GroovyExtractMethodHandler method performRefactoring.
private void performRefactoring(@NotNull final InitialInfo initialInfo, @Nullable final Editor editor) {
final PsiClass owner = PsiUtil.getContextClass(initialInfo.getContext());
LOG.assertTrue(owner != null);
final ExtractMethodInfoHelper helper = getSettings(initialInfo, owner);
if (helper == null)
return;
CommandProcessor.getInstance().executeCommand(helper.getProject(), () -> {
WriteAction.run(() -> {
createMethod(helper, owner);
GrStatementOwner declarationOwner = helper.getStringPartInfo() == null ? GroovyRefactoringUtil.getDeclarationOwner(helper.getStatements()[0]) : null;
GrStatement realStatement = ExtractUtil.replaceStatement(declarationOwner, helper);
// move to offset
if (editor != null) {
PsiDocumentManager.getInstance(helper.getProject()).commitDocument(editor.getDocument());
editor.getSelectionModel().removeSelection();
editor.getCaretModel().moveToOffset(ExtractUtil.getCaretOffset(realStatement));
}
});
}, REFACTORING_NAME, null);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class GroovyIntroduceParameterMethodUsagesProcessor method processAddSuperCall.
@Override
public boolean processAddSuperCall(IntroduceParameterData data, UsageInfo usage, UsageInfo[] usages) throws IncorrectOperationException {
if (!(usage.getElement() instanceof GrMethod) || !isGroovyUsage(usage))
return true;
GrMethod constructor = (GrMethod) usage.getElement();
if (!constructor.isConstructor())
return true;
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(data.getProject());
GrConstructorInvocation superCall = factory.createConstructorInvocation("super();");
GrOpenBlock body = constructor.getBlock();
final GrStatement[] statements = body.getStatements();
if (statements.length > 0) {
superCall = (GrConstructorInvocation) body.addStatementBefore(superCall, statements[0]);
} else {
superCall = (GrConstructorInvocation) body.addStatementBefore(superCall, null);
}
processChangeMethodUsage(data, new UsageInfo(superCall), usages);
return false;
}
Aggregations