use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GroovyLineMarkerProvider method getLineMarkerInfo.
@Override
public LineMarkerInfo getLineMarkerInfo(@NotNull final PsiElement element) {
final PsiElement parent = element.getParent();
if (parent instanceof PsiNameIdentifierOwner) {
if (parent instanceof GrField && element == ((GrField) parent).getNameIdentifierGroovy()) {
for (GrAccessorMethod method : GroovyPropertyUtils.getFieldAccessors((GrField) parent)) {
MethodSignatureBackedByPsiMethod superSignature = SuperMethodsSearch.search(method, null, true, false).findFirst();
if (superSignature != null) {
PsiMethod superMethod = superSignature.getMethod();
boolean overrides = method.hasModifierProperty(PsiModifier.ABSTRACT) == superMethod.hasModifierProperty(PsiModifier.ABSTRACT) || superMethod.getBody() != null && GrTraitUtil.isTrait(superMethod.getContainingClass());
final Icon icon = overrides ? AllIcons.Gutter.OverridingMethod : AllIcons.Gutter.ImplementingMethod;
final MarkerType type = GroovyMarkerTypes.OVERRIDING_PROPERTY_TYPE;
return new LineMarkerInfo<>(element, element.getTextRange(), icon, Pass.LINE_MARKERS, type.getTooltip(), type.getNavigationHandler(), GutterIconRenderer.Alignment.LEFT);
}
}
} else if (parent instanceof GrMethod && element == ((GrMethod) parent).getNameIdentifierGroovy() && hasSuperMethods((GrMethod) element.getParent())) {
final Icon icon = AllIcons.Gutter.OverridingMethod;
final MarkerType type = GroovyMarkerTypes.GR_OVERRIDING_METHOD;
return new LineMarkerInfo<>(element, element.getTextRange(), icon, Pass.LINE_MARKERS, type.getTooltip(), type.getNavigationHandler(), GutterIconRenderer.Alignment.LEFT);
}
}
//need to draw method separator above docComment
if (myDaemonSettings.SHOW_METHOD_SEPARATORS && element.getFirstChild() == null) {
PsiElement element1 = element;
boolean isMember = false;
while (element1 != null && !(element1 instanceof PsiFile) && element1.getPrevSibling() == null) {
element1 = element1.getParent();
if (element1 instanceof PsiMember || element1 instanceof GrVariableDeclarationImpl) {
isMember = true;
break;
}
}
if (isMember && !(element1 instanceof PsiAnonymousClass || element1.getParent() instanceof PsiAnonymousClass)) {
PsiFile file = element1.getContainingFile();
Document document = file == null ? null : PsiDocumentManager.getInstance(file.getProject()).getLastCommittedDocument(file);
boolean drawSeparator = false;
if (document != null) {
CharSequence documentChars = document.getCharsSequence();
int category = getGroovyCategory(element1, documentChars);
for (PsiElement child = element1.getPrevSibling(); child != null; child = child.getPrevSibling()) {
int category1 = getGroovyCategory(child, documentChars);
if (category1 == 0)
continue;
drawSeparator = category != 1 || category1 != 1;
break;
}
}
if (drawSeparator) {
GrDocComment comment = null;
if (element1 instanceof GrDocCommentOwner) {
comment = ((GrDocCommentOwner) element1).getDocComment();
}
LineMarkerInfo info = new LineMarkerInfo<>(element, comment != null ? comment.getTextRange() : element.getTextRange(), null, Pass.LINE_MARKERS, FunctionUtil.<Object, String>nullConstant(), null, GutterIconRenderer.Alignment.RIGHT);
EditorColorsScheme scheme = myColorsManager.getGlobalScheme();
info.separatorColor = scheme.getColor(CodeInsightColors.METHOD_SEPARATORS_COLOR);
info.separatorPlacement = SeparatorPlacement.TOP;
return info;
}
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class AddMethodFix method doFix.
@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
if (myClass.isInterface()) {
final GrMethod method = GroovyPsiElementFactory.getInstance(project).createMethodFromText("def " + myClass.getName() + " " + myMethodName + "();");
myClass.add(method);
} else {
String templName = JavaTemplateUtil.TEMPLATE_IMPLEMENTED_METHOD_BODY;
final FileTemplate template = FileTemplateManager.getInstance(project).getCodeTemplate(templName);
Properties properties = new Properties();
String returnType = generateTypeText(myClass);
properties.setProperty(FileTemplate.ATTRIBUTE_RETURN_TYPE, returnType);
properties.setProperty(FileTemplate.ATTRIBUTE_DEFAULT_RETURN_VALUE, PsiTypesUtil.getDefaultValueOfType(JavaPsiFacade.getElementFactory(project).createType(myClass)));
properties.setProperty(FileTemplate.ATTRIBUTE_CALL_SUPER, "");
properties.setProperty(FileTemplate.ATTRIBUTE_CLASS_NAME, myClass.getQualifiedName());
properties.setProperty(FileTemplate.ATTRIBUTE_SIMPLE_CLASS_NAME, myClass.getName());
properties.setProperty(FileTemplate.ATTRIBUTE_METHOD_NAME, myMethodName);
try {
String bodyText = StringUtil.replace(template.getText(properties), ";", "");
final GrCodeBlock newBody = GroovyPsiElementFactory.getInstance(project).createMethodBodyFromText("\n" + bodyText + "\n");
final GrMethod method = GroovyPsiElementFactory.getInstance(project).createMethodFromText("", myMethodName, returnType, ArrayUtil.EMPTY_STRING_ARRAY, myClass);
method.setBlock(newBody);
myClass.add(method);
} catch (IOException e) {
LOG.error(e);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GroovyGenerationInfo method positionCaret.
@Override
public void positionCaret(@NotNull Editor editor, boolean toEditMethodBody) {
final T firstMember = getPsiMember();
LOG.assertTrue(firstMember.isValid());
if (toEditMethodBody) {
GrMethod method = (GrMethod) firstMember;
GrOpenBlock body = method.getBlock();
if (body != null) {
PsiElement l = body.getLBrace();
if (l != null)
l = l.getNextSibling();
while (PsiImplUtil.isWhiteSpaceOrNls(l)) l = l.getNextSibling();
if (l == null)
l = body;
PsiElement r = body.getRBrace();
if (r != null)
r = r.getPrevSibling();
while (PsiImplUtil.isWhiteSpaceOrNls(r)) r = r.getPrevSibling();
if (r == null)
r = body;
int start = l.getTextRange().getStartOffset();
int end = r.getTextRange().getEndOffset();
editor.getCaretModel().moveToOffset(Math.min(start, end));
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
if (start < end) {
//Not an empty body
editor.getSelectionModel().setSelection(start, end);
}
return;
}
}
int offset;
if (firstMember instanceof GrMethod) {
GrMethod method = (GrMethod) firstMember;
GrCodeBlock body = method.getBlock();
if (body == null) {
offset = method.getTextRange().getStartOffset();
} else {
offset = body.getLBrace().getTextRange().getEndOffset();
}
} else {
offset = firstMember.getTextRange().getStartOffset();
}
editor.getCaretModel().moveToOffset(offset);
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
editor.getSelectionModel().removeSelection();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GroovyGenerationInfo method insert.
@Override
public void insert(@NotNull PsiClass aClass, @Nullable PsiElement anchor, boolean before) throws IncorrectOperationException {
final T proto = getPsiMember();
if (proto instanceof GrMethod) {
GroovyChangeContextUtil.encodeContextInfo(((GrMethod) proto).getParameterList());
}
super.insert(aClass, anchor, before);
final T member = getPsiMember();
if (member == null)
return;
LOG.assertTrue(member instanceof GroovyPsiElement, member);
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(member.getProject());
final PsiElement prev = member.getPrevSibling();
if (prev != null && GroovyTokenTypes.mNLS == prev.getNode().getElementType()) {
prev.replace(factory.createLineTerminator(1));
} else if (prev instanceof PsiMember) {
member.getParent().getNode().addLeaf(GroovyTokenTypes.mNLS, "\n", member.getNode());
}
final PsiElement next = member.getNextSibling();
if (next != null && GroovyTokenTypes.mNLS == next.getNode().getElementType()) {
next.replace(factory.createLineTerminator(1));
} else if (next instanceof PsiMember) {
member.getParent().getNode().addLeaf(GroovyTokenTypes.mNLS, "\n", next.getNode());
}
if (member instanceof GrMethod) {
GroovyChangeContextUtil.decodeContextInfo(((GrMethod) member).getParameterList(), null, null);
}
JavaCodeStyleManager.getInstance(member.getProject()).shortenClassReferences(member);
adjustDocCommentIfExists(member);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GrFieldMember method generateSetter.
@Override
@Nullable
public GroovyGenerationInfo<GrMethod> generateSetter() {
PsiField field = getElement();
if (field.hasModifierProperty(PsiModifier.FINAL)) {
return null;
}
final GrMethod method = createMethodIfNotExists(field, GroovyPropertyUtils.generateSetterPrototype(field));
return method == null ? null : new GroovyGenerationInfo<>(method);
}
Aggregations