Minor refactoring. Replaced some ifs with asserts, added @NotNull/@Nullable annotations.
This commit is contained in:
+14
-7
@@ -31,6 +31,7 @@ import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.messages.MessageBusConnection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.di.InjectorForJavaSemanticServices;
|
||||
import org.jetbrains.jet.lang.BuiltinsScopeExtensionMode;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||
@@ -99,9 +100,6 @@ public class AddKotlinSignatureAnnotation extends BaseIntentionAction implements
|
||||
public void invoke(@NotNull final Project project, final Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||
final PsiMethod method = findMethod(file, editor.getCaretModel().getOffset());
|
||||
String signature = getDefaultSignature(project, (PsiMethod) method.getOriginalElement());
|
||||
if (signature == null) {
|
||||
return;
|
||||
}
|
||||
final MessageBusConnection busConnection = project.getMessageBus().connect();
|
||||
busConnection.subscribe(ExternalAnnotationsManager.TOPIC, new ExternalAnnotationsListener.Adapter() {
|
||||
@Override
|
||||
@@ -128,26 +126,35 @@ public class AddKotlinSignatureAnnotation extends BaseIntentionAction implements
|
||||
return new AddAnnotationFix(KOTLIN_SIGNATURE_ANNOTATION, method, signatureToNameValuePairs(method.getProject(), signature));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String getDefaultSignature(@NotNull Project project, @NotNull PsiMethod method) {
|
||||
InjectorForJavaSemanticServices injector = new InjectorForJavaSemanticServices(BuiltinsScopeExtensionMode.ALL, project);
|
||||
JavaDescriptorResolver javaDescriptorResolver = injector.getJavaDescriptorResolver();
|
||||
FqName classFqName = new FqName(method.getContainingClass().getQualifiedName());
|
||||
|
||||
PsiClass containingClass = method.getContainingClass();
|
||||
assert containingClass != null;
|
||||
String qualifiedName = containingClass.getQualifiedName();
|
||||
assert qualifiedName != null;
|
||||
FqName classFqName = new FqName(qualifiedName);
|
||||
|
||||
if (method.getModifierList().hasModifierProperty(PsiModifier.STATIC)) {
|
||||
NamespaceDescriptor namespaceDescriptor = javaDescriptorResolver.resolveNamespace(classFqName);
|
||||
if (namespaceDescriptor == null) return null;
|
||||
assert namespaceDescriptor != null: "Couldn't resolve namespace descriptor for " + classFqName;
|
||||
namespaceDescriptor.getMemberScope().getFunctions(Name.identifier(method.getName()));
|
||||
}
|
||||
else {
|
||||
ClassDescriptor classDescriptor = javaDescriptorResolver.resolveClass(classFqName);
|
||||
if (classDescriptor == null) return null;
|
||||
assert classDescriptor != null: "Couldn't resolve class descriptor for " + classFqName;
|
||||
classDescriptor.getDefaultType().getMemberScope().getFunctions(Name.identifier(method.getName()));
|
||||
}
|
||||
|
||||
SimpleFunctionDescriptor functionDescriptor = injector.getBindingTrace().getBindingContext().get(BindingContext.FUNCTION, method);
|
||||
assert functionDescriptor != null: "Couldn't find function descriptor for " + method.getName() + " in " + classFqName;
|
||||
return RENDERER.render(functionDescriptor);
|
||||
}
|
||||
|
||||
private static PsiMethod findMethod(PsiFile file, int offset) {
|
||||
@Nullable
|
||||
private static PsiMethod findMethod(@NotNull PsiFile file, int offset) {
|
||||
PsiElement element = file.findElementAt(offset);
|
||||
PsiMethod res = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
|
||||
if (res == null) return null;
|
||||
|
||||
+17
-15
@@ -160,9 +160,8 @@ class EditSignatureBalloon {
|
||||
settings.setAdditionalPageAtBottom(false);
|
||||
settings.setAdditionalLinesCount(2);
|
||||
|
||||
if (editor instanceof EditorEx) {
|
||||
((EditorEx)editor).setEmbeddedIntoDialogWrapper(true);
|
||||
}
|
||||
assert editor instanceof EditorEx;
|
||||
((EditorEx)editor).setEmbeddedIntoDialogWrapper(true);
|
||||
|
||||
editor.getColorsScheme().setColor(EditorColors.CARET_ROW_COLOR, editor.getColorsScheme().getDefaultBackground());
|
||||
|
||||
@@ -208,18 +207,20 @@ class EditSignatureBalloon {
|
||||
}
|
||||
|
||||
private void saveAndHide() {
|
||||
balloon.hide();
|
||||
|
||||
final String newSignature = editor.getDocument().getText();
|
||||
if (!previousSignature.equals(newSignature)) {
|
||||
new WriteCommandAction(project) {
|
||||
@Override
|
||||
protected void run(Result result) throws Throwable {
|
||||
ExternalAnnotationsManager.getInstance(project).editExternalAnnotation(
|
||||
method, KOTLIN_SIGNATURE_ANNOTATION, signatureToNameValuePairs(project, newSignature));
|
||||
}
|
||||
}.execute();
|
||||
if (previousSignature.equals(newSignature)) {
|
||||
return;
|
||||
}
|
||||
|
||||
balloon.hide();
|
||||
new WriteCommandAction(project) {
|
||||
@Override
|
||||
protected void run(Result result) throws Throwable {
|
||||
ExternalAnnotationsManager.getInstance(project).editExternalAnnotation(
|
||||
method, KOTLIN_SIGNATURE_ANNOTATION, signatureToNameValuePairs(project, newSignature));
|
||||
}
|
||||
}.execute();
|
||||
}
|
||||
|
||||
static void invokeEditSignature(@NotNull PsiMethod element, @NotNull Editor editor, @Nullable Point point) {
|
||||
@@ -231,10 +232,11 @@ class EditSignatureBalloon {
|
||||
if (pair.getName() == null || "value".equals(pair.getName())) {
|
||||
PsiAnnotationMemberValue value = pair.getValue();
|
||||
if (value != null) {
|
||||
PsiElement firstChild = value.getFirstChild();
|
||||
VirtualFile virtualFile = value.getContainingFile().getVirtualFile();
|
||||
if (firstChild != null && firstChild.getNode().getElementType() == JavaTokenType.STRING_LITERAL
|
||||
&& virtualFile != null) {
|
||||
assert virtualFile != null;
|
||||
|
||||
PsiElement firstChild = value.getFirstChild();
|
||||
if (firstChild != null && firstChild.getNode().getElementType() == JavaTokenType.STRING_LITERAL) {
|
||||
new OpenFileDescriptor(value.getProject(), virtualFile, value.getTextOffset() + 1).navigate(true);
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -38,7 +38,8 @@ class KotlinSignatureUtil {
|
||||
private KotlinSignatureUtil() {
|
||||
}
|
||||
|
||||
static PsiMethod getAnnotationOwner(PsiElement element) {
|
||||
@NotNull
|
||||
static PsiMethod getAnnotationOwner(@NotNull PsiElement element) {
|
||||
PsiMethod annotationOwner = element.getOriginalElement() instanceof PsiMethod
|
||||
? (PsiMethod) element.getOriginalElement()
|
||||
: (PsiMethod) element;
|
||||
|
||||
Reference in New Issue
Block a user