Allow annotate java fields in GUI

This commit is contained in:
Nikolay Krasko
2012-10-05 13:11:35 +04:00
parent 896e78eb04
commit cc4be6ad67
3 changed files with 66 additions and 13 deletions
@@ -91,16 +91,36 @@ public class AddKotlinSignatureAnnotation extends BaseIntentionAction implements
@Override @Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
PsiMethod method = findMethod(file, editor.getCaretModel().getOffset()); PsiMethod method = findMethod(file, editor.getCaretModel().getOffset());
if (method == null) return false; if (method != null) {
if (findKotlinSignatureAnnotation(method) != null) return false; if (findKotlinSignatureAnnotation(method) != null) return false;
if (method.getModifierList().hasExplicitModifier(PsiModifier.PRIVATE)) return false; if (method.getModifierList().hasExplicitModifier(PsiModifier.PRIVATE)) return false;
return createFix(method, "").isAvailable(project, editor, file); return createFix(method, "").isAvailable(project, editor, file);
}
PsiField field = findField(file, editor.getCaretModel().getOffset());
if (field != null) {
if (findKotlinSignatureAnnotation(field) != null) return false;
PsiModifierList modifierList = field.getModifierList();
if (modifierList == null || modifierList.hasExplicitModifier(PsiModifier.PRIVATE)) return false;
return createFix(field, "").isAvailable(project, editor, file);
}
return false;
} }
@Override @Override
public void invoke(@NotNull final Project project, final Editor editor, PsiFile file) throws IncorrectOperationException { public void invoke(@NotNull final Project project, final Editor editor, PsiFile file) throws IncorrectOperationException {
final PsiMethod method = findMethod(file, editor.getCaretModel().getOffset()); PsiMethod method = findMethod(file, editor.getCaretModel().getOffset());
String signature = getDefaultSignature(project, (PsiMethod) method.getOriginalElement()); PsiField field = findField(file, editor.getCaretModel().getOffset());
assert (method != null || field != null);
String signature = method != null ?
getDefaultSignature(project, (PsiMethod) method.getOriginalElement()) :
getDefaultSignature(project, (PsiField) field.getOriginalElement());
final PsiModifierListOwner annotatedElement = method != null ? method : field;
final MessageBusConnection busConnection = project.getMessageBus().connect(); final MessageBusConnection busConnection = project.getMessageBus().connect();
busConnection.subscribe(ExternalAnnotationsManager.TOPIC, new ExternalAnnotationsListener.Adapter() { busConnection.subscribe(ExternalAnnotationsManager.TOPIC, new ExternalAnnotationsListener.Adapter() {
@Override @Override
@@ -108,23 +128,23 @@ public class AddKotlinSignatureAnnotation extends BaseIntentionAction implements
boolean successful) { boolean successful) {
busConnection.disconnect(); busConnection.disconnect();
if (successful && owner == method && KOTLIN_SIGNATURE_ANNOTATION.equals(annotationFQName)) { if (successful && owner == annotatedElement && KOTLIN_SIGNATURE_ANNOTATION.equals(annotationFQName)) {
ApplicationManager.getApplication().invokeLater(new Runnable() { ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override @Override
public void run() { public void run() {
refreshMarkers(project); refreshMarkers(project);
EditSignatureAction.invokeEditSignature(method, editor, null); EditSignatureAction.invokeEditSignature(annotatedElement, editor, null);
} }
}, ModalityState.NON_MODAL); }, ModalityState.NON_MODAL);
} }
} }
}); });
createFix(method, signature).invoke(project, editor, file); createFix(annotatedElement, signature).invoke(project, editor, file);
} }
@NotNull @NotNull
private static AddAnnotationFix createFix(@NotNull PsiMethod method, @NotNull String signature) { private static AddAnnotationFix createFix(@NotNull PsiModifierListOwner annotatedElement, @NotNull String signature) {
return new AddAnnotationFix(KOTLIN_SIGNATURE_ANNOTATION, method, signatureToNameValuePairs(method.getProject(), signature)); return new AddAnnotationFix(KOTLIN_SIGNATURE_ANNOTATION, annotatedElement, signatureToNameValuePairs(annotatedElement.getProject(), signature));
} }
@NotNull @NotNull
@@ -169,6 +189,33 @@ public class AddKotlinSignatureAnnotation extends BaseIntentionAction implements
return String.format("fun %s%s", classFqName.shortName(), RENDERER.renderFunctionParameters(constructorDescriptor)); return String.format("fun %s%s", classFqName.shortName(), RENDERER.renderFunctionParameters(constructorDescriptor));
} }
@NotNull
private static String getDefaultSignature(@NotNull Project project, @NotNull PsiField field) {
InjectorForJavaSemanticServices injector = new InjectorForJavaSemanticServices(BuiltinsScopeExtensionMode.ALL, project);
JavaDescriptorResolver javaDescriptorResolver = injector.getJavaDescriptorResolver();
PsiClass containingClass = field.getContainingClass();
assert containingClass != null;
String qualifiedName = containingClass.getQualifiedName();
assert qualifiedName != null;
FqName classFqName = new FqName(qualifiedName);
PsiModifierList modifierList = field.getModifierList();
if (modifierList != null && modifierList.hasModifierProperty(PsiModifier.STATIC)) {
NamespaceDescriptor namespaceDescriptor = javaDescriptorResolver.resolveNamespace(classFqName);
assert namespaceDescriptor != null: "Couldn't resolve namespace descriptor for " + classFqName;
namespaceDescriptor.getMemberScope().getProperties(Name.identifier(field.getName()));
}
else {
ClassDescriptor classDescriptor = javaDescriptorResolver.resolveClass(classFqName);
assert classDescriptor != null: "Couldn't resolve class descriptor for " + classFqName;
classDescriptor.getDefaultType().getMemberScope().getProperties(Name.identifier(field.getName()));
}
// TODO: Generate default string for the field
return "";
}
@Nullable @Nullable
private static PsiMethod findMethod(@NotNull PsiFile file, int offset) { private static PsiMethod findMethod(@NotNull PsiFile file, int offset) {
PsiElement element = file.findElementAt(offset); PsiElement element = file.findElementAt(offset);
@@ -185,4 +232,9 @@ public class AddKotlinSignatureAnnotation extends BaseIntentionAction implements
} }
return res; return res;
} }
@Nullable
private static PsiField findField(@NotNull PsiFile file, int offset) {
return PsiTreeUtil.getParentOfType(file.findElementAt(offset), PsiField.class);
}
} }
@@ -50,6 +50,7 @@ public class KotlinSignatureInJavaMarkerProvider implements LineMarkerProvider {
private static final String SHOW_MARKERS_PROPERTY = "kotlin.signature.markers.enabled"; private static final String SHOW_MARKERS_PROPERTY = "kotlin.signature.markers.enabled";
private static final Function<PsiElement, String> TOOLTIP_PROVIDER = new Function<PsiElement, String>() { private static final Function<PsiElement, String> TOOLTIP_PROVIDER = new Function<PsiElement, String>() {
@Nullable
@Override @Override
public String fun(PsiElement element) { public String fun(PsiElement element) {
PsiAnnotation annotation = findKotlinSignatureAnnotation(element); PsiAnnotation annotation = findKotlinSignatureAnnotation(element);
@@ -49,8 +49,8 @@ class KotlinSignatureUtil {
ASTNode node = SourceTreeToPsiMap.psiElementToTree(element); ASTNode node = SourceTreeToPsiMap.psiElementToTree(element);
if (node != null) { if (node != null) {
PsiCompiledElement compiledElement = node.getUserData(ClsElementImpl.COMPILED_ELEMENT); PsiCompiledElement compiledElement = node.getUserData(ClsElementImpl.COMPILED_ELEMENT);
if (compiledElement instanceof PsiMethod) { if (compiledElement instanceof PsiModifierListOwner) {
return (PsiMethod) compiledElement; return (PsiModifierListOwner) compiledElement;
} }
} }
} }