use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition in project intellij-community by JetBrains.
the class CreateClassActionBase method createClassByType.
@Nullable
public static GrTypeDefinition createClassByType(@NotNull final PsiDirectory directory, @NotNull final String name, @NotNull final PsiManager manager, @Nullable final PsiElement contextElement, @NotNull final String templateName, boolean allowReformatting) {
return WriteAction.compute(() -> {
try {
GrTypeDefinition targetClass = null;
try {
PsiFile file = GroovyTemplatesFactory.createFromTemplate(directory, name, name + ".groovy", templateName, allowReformatting);
for (PsiElement element : file.getChildren()) {
if (element instanceof GrTypeDefinition) {
targetClass = ((GrTypeDefinition) element);
break;
}
}
if (targetClass == null) {
throw new IncorrectOperationException(GroovyBundle.message("no.class.in.file.template"));
}
} catch (final IncorrectOperationException e) {
ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog(GroovyBundle.message("cannot.create.class.error.text", name, e.getLocalizedMessage()), GroovyBundle.message("cannot.create.class.error.title")));
return null;
}
PsiModifierList modifiers = targetClass.getModifierList();
if (contextElement != null && !JavaPsiFacade.getInstance(manager.getProject()).getResolveHelper().isAccessible(targetClass, contextElement, null) && modifiers != null) {
modifiers.setModifierProperty(PsiModifier.PUBLIC, true);
}
return targetClass;
} catch (IncorrectOperationException e) {
LOG.error(e);
return null;
}
});
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition in project intellij-community by JetBrains.
the class GroovyStatementMover method checkAvailable.
@Override
public boolean checkAvailable(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) {
final Project project = file.getProject();
if (!HandlerUtils.canBeInvoked(editor, project) || !(file instanceof GroovyFileBase))
return false;
LineRange range = getLineRangeFromSelection(editor);
final Document document = editor.getDocument();
final int offset = document.getLineStartOffset(range.startLine);
final GrLiteral literal = PsiTreeUtil.findElementOfClassAtOffset(file, offset, GrLiteral.class, false);
//multiline string
if (literal != null && literal.textContains('\n'))
return false;
final GroovyPsiElement pivot = getElementToMove((GroovyFileBase) file, offset);
if (pivot == null)
return false;
final LineRange pivotRange = getLineRange(pivot);
range = new LineRange(Math.min(range.startLine, pivotRange.startLine), Math.max(range.endLine, pivotRange.endLine));
final GroovyPsiElement scope = PsiTreeUtil.getParentOfType(pivot, GrMethod.class, GrTypeDefinitionBody.class, GroovyFileBase.class);
final boolean stmtLevel = isStatement(pivot);
boolean topLevel = pivot instanceof GrTypeDefinition && pivot.getParent() instanceof GroovyFileBase;
final List<LineRange> allRanges = allRanges(scope, stmtLevel, topLevel);
LineRange prev = null;
LineRange next = null;
for (LineRange each : allRanges) {
if (each.endLine <= range.startLine) {
prev = each;
}
if (each.containsLine(range.startLine)) {
range = new LineRange(each.startLine, range.endLine);
}
if (each.startLine < range.endLine && each.endLine > range.endLine) {
range = new LineRange(range.startLine, each.endLine);
}
if (each.startLine >= range.endLine && next == null) {
next = each;
}
}
info.toMove = range;
info.toMove2 = down ? next : prev;
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition in project intellij-community by JetBrains.
the class CreateParameterForFieldIntention method findFieldCandidates.
@Nullable
private static List<GrField> findFieldCandidates(PsiElement element) {
final GrMethod constructor = PsiTreeUtil.getParentOfType(element, GrMethod.class);
if (constructor == null || !constructor.isConstructor())
return null;
if (constructor.getBlock() == null)
return null;
if (PsiTreeUtil.isAncestor(constructor.getBlock(), element, false)) {
return null;
}
final PsiClass clazz = constructor.getContainingClass();
if (!(clazz instanceof GrTypeDefinition))
return null;
return findCandidatesCached(constructor, (GrTypeDefinition) clazz);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition in project intellij-community by JetBrains.
the class GroovyDocumentationProvider method generateDocumentationContentStub.
@Override
public String generateDocumentationContentStub(PsiComment contextComment) {
if (!(contextComment instanceof GrDocComment)) {
return null;
}
final GrDocCommentOwner owner = GrDocCommentUtil.findDocOwner((GrDocComment) contextComment);
if (owner == null)
return null;
Project project = contextComment.getProject();
final CodeDocumentationAwareCommenter commenter = (CodeDocumentationAwareCommenter) LanguageCommenters.INSTANCE.forLanguage(owner.getLanguage());
StringBuilder builder = StringBuilderSpinAllocator.alloc();
try {
if (owner instanceof GrMethod) {
final GrMethod method = (GrMethod) owner;
JavaDocumentationProvider.generateParametersTakingDocFromSuperMethods(project, builder, commenter, method);
final PsiType returnType = method.getInferredReturnType();
if ((returnType != null || method.getModifierList().hasModifierProperty(GrModifier.DEF)) && !PsiType.VOID.equals(returnType)) {
builder.append(CodeDocumentationUtil.createDocCommentLine(RETURN_TAG, project, commenter));
builder.append(LINE_SEPARATOR);
}
final PsiClassType[] references = method.getThrowsList().getReferencedTypes();
for (PsiClassType reference : references) {
builder.append(CodeDocumentationUtil.createDocCommentLine(THROWS_TAG, project, commenter));
builder.append(reference.getClassName());
builder.append(LINE_SEPARATOR);
}
} else if (owner instanceof GrTypeDefinition) {
final PsiTypeParameterList typeParameterList = ((PsiClass) owner).getTypeParameterList();
if (typeParameterList != null) {
JavaDocumentationProvider.createTypeParamsListComment(builder, project, commenter, typeParameterList);
}
}
return builder.length() > 0 ? builder.toString() : null;
} finally {
StringBuilderSpinAllocator.dispose(builder);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition in project intellij-community by JetBrains.
the class GroovyRefactoringUtil method getExpressionOccurrences.
public static PsiElement[] getExpressionOccurrences(@NotNull PsiElement expr, @NotNull PsiElement scope) {
ArrayList<PsiElement> occurrences = new ArrayList<>();
Comparator<PsiElement> comparator = (element1, element2) -> {
if (element1 != null && element1.equals(element2))
return 0;
if (element1 instanceof GrParameter && element2 instanceof GrParameter) {
final String name1 = ((GrParameter) element1).getName();
final String name2 = ((GrParameter) element2).getName();
return name1.compareTo(name2);
}
return 1;
};
if (scope instanceof GrLoopStatement) {
PsiElement son = expr;
while (son.getParent() != null && !(son.getParent() instanceof GrLoopStatement)) {
son = son.getParent();
}
assert scope.equals(son.getParent());
collectOccurrences(expr, son, occurrences, comparator, false);
} else {
collectOccurrences(expr, scope, occurrences, comparator, scope instanceof GrTypeDefinition || scope instanceof GroovyFileBase);
}
return PsiUtilCore.toPsiElementArray(occurrences);
}
Aggregations