intentions preview: create annotation method from usage

GitOrigin-RevId: 202cf795eba05296027ab41e034220a9c1a6b678
This commit is contained in:
Anna Kozlova
2022-09-07 13:35:44 +02:00
committed by intellij-monorepo-bot
parent fc2f8af34d
commit c59dfcedab
4 changed files with 22 additions and 17 deletions

View File

@@ -1,18 +1,4 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.ExpectedTypeInfo;
@@ -20,6 +6,8 @@ import com.intellij.codeInsight.ExpectedTypesProvider;
import com.intellij.codeInsight.TailType;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.analysis.AnnotationsHighlightUtil;
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo;
import com.intellij.ide.highlighter.JavaFileType;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
@@ -32,6 +20,9 @@ import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Objects;
public class CreateAnnotationMethodFromUsageFix extends CreateFromUsageBaseFix {
private static final Logger LOG = Logger.getInstance(CreateAnnotationMethodFromUsageFix.class);
@@ -65,6 +56,19 @@ public class CreateAnnotationMethodFromUsageFix extends CreateFromUsageBaseFix {
chooseTargetClass(project, editor, this::invokeImpl);
}
@Override
public @NotNull IntentionPreviewInfo generatePreview(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
PsiNameValuePair nameValuePair = getNameValuePair();
List<PsiClass> classes = filterTargetClasses(nameValuePair, project);
if (nameValuePair == null || classes.isEmpty()) return IntentionPreviewInfo.EMPTY;
final PsiType type = getAnnotationValueType(nameValuePair.getValue());
PsiClass targetClass = classes.get(0);
String methodName = Objects.requireNonNull(nameValuePair.getName());
return new IntentionPreviewInfo.CustomDiff(JavaFileType.INSTANCE, targetClass.getName(),
"",
Objects.requireNonNull(type).getPresentableText() + " " + methodName + "()");
}
private void invokeImpl(@NotNull PsiClass targetClass) {
PsiNameValuePair nameValuePair = getNameValuePair();
if (nameValuePair == null || isValidElement(nameValuePair)) return;

View File

@@ -1,4 +1,4 @@
// "Create method 'test'" "true"
// "Create method 'test'" "true-preview"
public class Test {
@Attr(test= "")
public Test() {

View File

@@ -1,4 +1,4 @@
// "Create method 'test'" "true"
// "Create method 'test'" "true-preview"
public class Test {
@Attr(te<caret>st= "")
public Test() {