use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class GrAliasImportIntention method doRefactoring.
private static void doRefactoring(@NotNull Project project, @NotNull GrImportStatement importStatement, @NotNull PsiMember member) {
if (member instanceof GrAccessorMethod && !importStatement.isOnDemand() && !importStatement.getImportedName().equals(member.getName())) {
member = ((GrAccessorMethod) member).getProperty();
}
final GroovyFileBase file = (GroovyFileBase) importStatement.getContainingFile();
final List<UsageInfo> usages = findUsages(member, file);
GrImportStatement templateImport = createTemplateImport(project, importStatement, member, file);
if (ApplicationManager.getApplication().isUnitTestMode()) {
if (!importStatement.isOnDemand()) {
importStatement.delete();
}
updateRefs(usages, member.getName(), templateImport);
} else {
runTemplate(project, importStatement, member, file, usages, templateImport);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class MyPredicate method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull final Project project, Editor editor) throws IncorrectOperationException {
final GrListOrMap map = (GrListOrMap) element;
final GrNamedArgument[] namedArguments = map.getNamedArguments();
LOG.assertTrue(map.getInitializers().length == 0);
final PsiFile file = map.getContainingFile();
final String packageName = file instanceof GroovyFileBase ? ((GroovyFileBase) file).getPackageName() : "";
final CreateClassDialog dialog = new CreateClassDialog(project, GroovyBundle.message("create.class.family.name"), "", packageName, GrCreateClassKind.CLASS, true, ModuleUtilCore.findModuleForPsiElement(element));
dialog.show();
if (dialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
return;
boolean replaceReturnType = checkForReturnFromMethod(map);
boolean variableDeclaration = checkForVariableDeclaration(map);
final GrParameter methodParameter = checkForMethodParameter(map);
final String qualifiedClassName = dialog.getClassName();
final String selectedPackageName = StringUtil.getPackageName(qualifiedClassName);
final String shortName = StringUtil.getShortName(qualifiedClassName);
final GrTypeDefinition typeDefinition = createClass(project, namedArguments, selectedPackageName, shortName);
final PsiClass generatedClass = CreateClassActionBase.createClassByType(dialog.getTargetDirectory(), typeDefinition.getName(), PsiManager.getInstance(project), map, GroovyTemplates.GROOVY_CLASS, true);
final PsiClass replaced = (PsiClass) generatedClass.replace(typeDefinition);
replaceMapWithClass(project, map, replaced, replaceReturnType, variableDeclaration, methodParameter);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class GroovyBlockGenerator method getGroovyChildren.
private static ASTNode[] getGroovyChildren(final ASTNode node) {
PsiElement psi = node.getPsi();
if (psi instanceof OuterLanguageElement) {
TextRange range = node.getTextRange();
ArrayList<ASTNode> childList = new ArrayList<>();
PsiFile groovyFile = psi.getContainingFile().getViewProvider().getPsi(GroovyLanguage.INSTANCE);
if (groovyFile instanceof GroovyFileBase) {
addChildNodes(groovyFile, childList, range, psi);
}
return childList.toArray(new ASTNode[childList.size()]);
}
return node.getChildren(null);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class ControlFlowBuilderUtil method isCertainlyReturnStatement.
/**
* check whether statement is return (the statement which provides return value) statement of method or closure.
*
* @param st
* @return
*/
public static boolean isCertainlyReturnStatement(GrStatement st) {
final PsiElement parent = st.getParent();
if (parent instanceof GrOpenBlock) {
if (st != ArrayUtil.getLastElement(((GrOpenBlock) parent).getStatements()))
return false;
PsiElement pparent = parent.getParent();
if (pparent instanceof GrMethod) {
return true;
}
if (pparent instanceof GrBlockStatement || pparent instanceof GrCatchClause || pparent instanceof GrLabeledStatement) {
pparent = pparent.getParent();
}
if (pparent instanceof GrIfStatement || pparent instanceof GrControlStatement || pparent instanceof GrTryCatchStatement) {
return isCertainlyReturnStatement((GrStatement) pparent);
}
} else if (parent instanceof GrClosableBlock) {
return st == ArrayUtil.getLastElement(((GrClosableBlock) parent).getStatements());
} else if (parent instanceof GroovyFileBase) {
return st == ArrayUtil.getLastElement(((GroovyFileBase) parent).getStatements());
} else if (parent instanceof GrForStatement || parent instanceof GrIfStatement && st != ((GrIfStatement) parent).getCondition() || parent instanceof GrSynchronizedStatement && st != ((GrSynchronizedStatement) parent).getMonitor() || parent instanceof GrWhileStatement && st != ((GrWhileStatement) parent).getCondition() || parent instanceof GrConditionalExpression && st != ((GrConditionalExpression) parent).getCondition() || parent instanceof GrElvisExpression) {
return isCertainlyReturnStatement((GrStatement) parent);
} else if (parent instanceof GrCaseSection) {
final GrStatement[] statements = ((GrCaseSection) parent).getStatements();
final GrStatement last = ArrayUtil.getLastElement(statements);
final GrSwitchStatement switchStatement = (GrSwitchStatement) parent.getParent();
if (last instanceof GrBreakStatement && statements.length > 1 && statements[statements.length - 2] == st) {
return isCertainlyReturnStatement(switchStatement);
} else if (st == last) {
if (st instanceof GrBreakStatement || isLastStatementInCaseSection((GrCaseSection) parent, switchStatement)) {
return isCertainlyReturnStatement(switchStatement);
}
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class GroovyClassNameInsertHandler method handleInsert.
@Override
public void handleInsert(InsertionContext context, JavaPsiClassReferenceElement item) {
PsiFile file = context.getFile();
Editor editor = context.getEditor();
int endOffset = editor.getCaretModel().getOffset();
if (PsiTreeUtil.findElementOfClassAtOffset(file, endOffset - 1, GrImportStatement.class, false) != null || !(file instanceof GroovyFileBase)) {
AllClassesGetter.INSERT_FQN.handleInsert(context, item);
return;
}
PsiDocumentManager.getInstance(context.getProject()).commitDocument(editor.getDocument());
PsiElement position = file.findElementAt(endOffset - 1);
boolean parens = shouldInsertParentheses(position);
final PsiClass psiClass = item.getObject();
if (isInVariable(position) || GroovyCompletionUtil.isInPossibleClosureParameter(position)) {
Project project = context.getProject();
String qname = psiClass.getQualifiedName();
String shortName = psiClass.getName();
if (qname == null)
return;
PsiClass aClass = JavaPsiFacade.getInstance(project).getResolveHelper().resolveReferencedClass(shortName, position);
if (aClass == null) {
((GroovyFileBase) file).addImportForClass(psiClass);
return;
} else if (aClass == CompletionUtil.getOriginalOrSelf(psiClass)) {
return;
}
}
AllClassesGetter.TRY_SHORTENING.handleInsert(context, item);
if (parens && context.getCompletionChar() != '[') {
int identifierEnd = context.getTailOffset();
GroovyPsiElement place = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), GroovyPsiElement.class, false);
JavaCompletionUtil.insertParentheses(context, item, false, place != null && GroovyCompletionUtil.hasConstructorParameters(psiClass, place));
if (context.getCompletionChar() == '<' || psiClass.hasTypeParameters() && context.getCompletionChar() != '(') {
context.setAddCompletionChar(false);
JavaCompletionUtil.promptTypeArgs(context, identifierEnd);
}
}
}
Aggregations