diff options
Diffstat (limited to 'java/java-impl/src/com/intellij/codeInsight/daemon/impl')
10 files changed, 267 insertions, 317 deletions
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/GutterIconTooltipHelper.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/GutterIconTooltipHelper.java deleted file mode 100644 index d539559efc25..000000000000 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/GutterIconTooltipHelper.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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. - */ - -/* - * @author max - */ -package com.intellij.codeInsight.daemon.impl; - -import com.intellij.psi.PsiClass; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiFile; -import com.intellij.psi.PsiMethod; -import com.intellij.psi.presentation.java.ClassPresentationUtil; -import org.jetbrains.annotations.NonNls; -import org.jetbrains.annotations.NotNull; - -import java.text.MessageFormat; -import java.util.Arrays; -import java.util.LinkedHashSet; -import java.util.Set; - -public class GutterIconTooltipHelper { - private GutterIconTooltipHelper() { - } - - public static String composeText(@NotNull PsiElement[] elements, String start, final String pattern) { - return composeText(Arrays.asList(elements), start, pattern); - } - - public static String composeText(@NotNull Iterable<? extends PsiElement> elements, String start, final String pattern) { - @NonNls StringBuilder result = new StringBuilder(); - result.append("<html><body>"); - result.append(start); - Set<String> names = new LinkedHashSet<String>(); - for (PsiElement element : elements) { - String descr = ""; - if (element instanceof PsiClass) { - String className = ClassPresentationUtil.getNameForClass((PsiClass)element, true); - descr = MessageFormat.format(pattern, className); - } - else if (element instanceof PsiMethod) { - String methodName = ((PsiMethod)element).getName(); - PsiClass aClass = ((PsiMethod)element).getContainingClass(); - String className = aClass == null ? "" : ClassPresentationUtil.getNameForClass(aClass, true); - descr = MessageFormat.format(pattern, methodName, className); - } - else if (element instanceof PsiFile) { - descr = MessageFormat.format(pattern, ((PsiFile)element).getName()); - } - names.add(descr); - } - - @NonNls String sep = ""; - for (String name : names) { - result.append(sep); - sep = "<br>"; - result.append(name); - } - - result.append("</body></html>"); - return result.toString(); - } -} diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/JavaChangeLocalityDetector.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/JavaChangeLocalityDetector.java deleted file mode 100644 index 4f5557486e2b..000000000000 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/JavaChangeLocalityDetector.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2000-2009 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. - */ - -/* - * @author max - */ -package com.intellij.codeInsight.daemon.impl; - -import com.intellij.codeInsight.daemon.ChangeLocalityDetector; -import com.intellij.psi.*; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -public class JavaChangeLocalityDetector implements ChangeLocalityDetector { - @Override - @Nullable - public PsiElement getChangeHighlightingDirtyScopeFor(@NotNull final PsiElement element) { - // optimization - PsiElement parent = element.getParent(); - PsiElement grand; - if (element instanceof PsiCodeBlock - && parent instanceof PsiMethod - && !((PsiMethod)parent).isConstructor() - && (grand = parent.getParent()) instanceof PsiClass - && !(grand instanceof PsiAnonymousClass)) { - // for changes inside method, rehighlight codeblock only - // do not use this optimization for constructors and class initializers - to update non-initialized fields - return parent; - } - return null; - } -}
\ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/JavaDocAnnotator.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/JavaDocAnnotator.java deleted file mode 100644 index 82c9080b9b9b..000000000000 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/JavaDocAnnotator.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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. - */ -package com.intellij.codeInsight.daemon.impl; - -import com.intellij.lang.annotation.Annotation; -import com.intellij.lang.annotation.AnnotationHolder; -import com.intellij.lang.annotation.Annotator; -import com.intellij.openapi.editor.colors.CodeInsightColors; -import com.intellij.psi.PsiElement; -import com.intellij.psi.javadoc.PsiDocTag; -import com.intellij.psi.javadoc.PsiDocTagValue; -import org.jetbrains.annotations.NotNull; - -/** - * @author Dmitry Avdeev - * Date: 11/8/11 - */ -public class JavaDocAnnotator implements Annotator { - @Override - public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) { - if (element instanceof PsiDocTag) { - String name = ((PsiDocTag)element).getName(); - if ("param".equals(name)) { - PsiDocTagValue tagValue = ((PsiDocTag)element).getValueElement(); - if (tagValue != null) { - Annotation annotation = holder.createInfoAnnotation(tagValue, null); - annotation.setTextAttributes(CodeInsightColors.DOC_COMMENT_TAG_VALUE); - } - } - } - } -} diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/actions/SuppressWarningsFoldingBuilder.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/actions/SuppressWarningsFoldingBuilder.java deleted file mode 100644 index f99abde5f685..000000000000 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/actions/SuppressWarningsFoldingBuilder.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright 2000-2010 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. - */ - -/* - * User: anna - * Date: 25-May-2010 - */ -package com.intellij.codeInsight.daemon.impl.actions; - -import com.intellij.codeInsight.folding.JavaCodeFoldingSettings; -import com.intellij.lang.ASTNode; -import com.intellij.lang.folding.FoldingBuilderEx; -import com.intellij.lang.folding.FoldingDescriptor; -import com.intellij.openapi.editor.Document; -import com.intellij.openapi.util.Comparing; -import com.intellij.openapi.util.text.StringUtil; -import com.intellij.psi.*; -import com.intellij.psi.util.PsiUtil; -import com.intellij.util.Function; -import org.jetbrains.annotations.NotNull; - -import java.util.ArrayList; -import java.util.List; - -public class SuppressWarningsFoldingBuilder extends FoldingBuilderEx { - @NotNull - @Override - public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) { - if (!(root instanceof PsiJavaFile) || quick || !JavaCodeFoldingSettings.getInstance().isCollapseSuppressWarnings()) { - return FoldingDescriptor.EMPTY; - } - if (!PsiUtil.isLanguageLevel5OrHigher(root)) { - return FoldingDescriptor.EMPTY; - } - final List<FoldingDescriptor> result = new ArrayList<FoldingDescriptor>(); - root.accept(new JavaRecursiveElementWalkingVisitor(){ - @Override - public void visitAnnotation(PsiAnnotation annotation) { - if (Comparing.strEqual(annotation.getQualifiedName(), SuppressWarnings.class.getName())) { - result.add(new FoldingDescriptor(annotation, annotation.getTextRange())); - } - super.visitAnnotation(annotation); - } - }); - return result.toArray(new FoldingDescriptor[result.size()]); - } - - @Override - public String getPlaceholderText(@NotNull ASTNode node) { - final PsiElement element = node.getPsi(); - if (element instanceof PsiAnnotation) { - return "/" + StringUtil.join(((PsiAnnotation)element).getParameterList().getAttributes(), new Function<PsiNameValuePair, String>() { - @Override - public String fun(PsiNameValuePair value) { - return getMemberValueText(value.getValue()); - } - }, ", ") + "/"; - } - return element.getText(); - } - - private static String getMemberValueText(PsiAnnotationMemberValue memberValue) { - if (memberValue instanceof PsiArrayInitializerMemberValue) { - final PsiAnnotationMemberValue[] initializers = ((PsiArrayInitializerMemberValue)memberValue).getInitializers(); - return StringUtil.join(initializers, new Function<PsiAnnotationMemberValue, String>() { - @Override - public String fun(PsiAnnotationMemberValue psiAnnotationMemberValue) { - return getMemberValueText(psiAnnotationMemberValue); - } - }, ", "); - } - if (memberValue instanceof PsiLiteral) { - final Object o = ((PsiLiteral)memberValue).getValue(); - if (o != null) { - return o.toString(); - } - } - return memberValue != null ? memberValue.getText() : ""; - } - - - @Override - public boolean isCollapsedByDefault(@NotNull ASTNode node) { - return JavaCodeFoldingSettings.getInstance().isCollapseSuppressWarnings(); - } -} diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/JavadocErrorFilter.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/JavadocErrorFilter.java deleted file mode 100644 index 660b241d5b7f..000000000000 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/JavadocErrorFilter.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2000-2009 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. - */ -package com.intellij.codeInsight.daemon.impl.analysis; - -import com.intellij.codeInsight.highlighting.HighlightErrorFilter; -import com.intellij.psi.PsiErrorElement; -import com.intellij.psi.javadoc.PsiDocComment; -import com.intellij.psi.util.PsiTreeUtil; -import org.jetbrains.annotations.NotNull; - -/** - * @author yole - */ -public class JavadocErrorFilter extends HighlightErrorFilter { - - @Override - public boolean shouldHighlightErrorElement(@NotNull final PsiErrorElement element) { - return !value(element); - } - - public static boolean value(final PsiErrorElement element) { - return PsiTreeUtil.getParentOfType(element, PsiDocComment.class) != null; - } -} diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddMissingRequiredAnnotationParametersFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddMissingRequiredAnnotationParametersFix.java new file mode 100644 index 000000000000..348fcdeda8e5 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddMissingRequiredAnnotationParametersFix.java @@ -0,0 +1,163 @@ +/* + * Copyright 2000-2014 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. + */ +package com.intellij.codeInsight.daemon.impl.quickfix; + +import com.intellij.codeInsight.daemon.QuickFixBundle; +import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.codeInsight.template.TemplateBuilderImpl; +import com.intellij.codeInsight.template.TemplateManager; +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Pair; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.psi.*; +import com.intellij.util.IncorrectOperationException; +import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.containers.HashSet; +import gnu.trove.TObjectIntHashMap; +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; +import java.util.Comparator; +import java.util.SortedSet; +import java.util.TreeSet; + +/** +* @author Dmitry Batkovich +*/ +public class AddMissingRequiredAnnotationParametersFix implements IntentionAction { + private static final Logger LOG = Logger.getInstance(AddMissingRequiredAnnotationParametersFix.class); + + private final PsiAnnotation myAnnotation; + private final PsiMethod[] myAnnotationMethods; + private final Collection<String> myMissedElements; + + public AddMissingRequiredAnnotationParametersFix(final PsiAnnotation annotation, + final PsiMethod[] annotationMethods, + final Collection<String> missedElements) { + if (missedElements.isEmpty()) { + throw new IllegalArgumentException("missedElements can't be empty"); + } + myAnnotation = annotation; + myAnnotationMethods = annotationMethods; + myMissedElements = missedElements; + } + + @NotNull + @Override + public String getText() { + return myMissedElements.size() == 1 + ? QuickFixBundle.message("add.missing.annotation.single.parameter.fix", ContainerUtil.getFirstItem(myMissedElements)) + : QuickFixBundle.message("add.missing.annotation.parameters.fix", StringUtil.join(myMissedElements, ", ")); + } + + @NotNull + @Override + public String getFamilyName() { + return QuickFixBundle.message("annotations.fix"); + } + + @Override + public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) { + return true; + } + + @Override + public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException { + final PsiNameValuePair[] addedParameters = myAnnotation.getParameterList().getAttributes(); + + final TObjectIntHashMap<String> annotationsOrderMap = getAnnotationsOrderMap(); + final SortedSet<Pair<String, PsiAnnotationMemberValue>> + newParameters = new TreeSet<Pair<String, PsiAnnotationMemberValue>>(new Comparator<Pair<String, PsiAnnotationMemberValue>>() { + @Override + public int compare(final Pair<String, PsiAnnotationMemberValue> o1, final Pair<String, PsiAnnotationMemberValue> o2) { + return annotationsOrderMap.get(o1.getFirst()) - annotationsOrderMap.get(o2.getFirst()); + } + }); + final boolean order = isAlreadyAddedOrdered(annotationsOrderMap, addedParameters); + if (order) { + if (addedParameters.length != 0) { + final PsiAnnotationParameterList parameterList = myAnnotation.getParameterList(); + parameterList.deleteChildRange(addedParameters[0], addedParameters[addedParameters.length - 1]); + for (final PsiNameValuePair addedParameter : addedParameters) { + final String name = addedParameter.getName(); + final PsiAnnotationMemberValue value = addedParameter.getValue(); + if (name == null || value == null) { + LOG.error(String.format("Invalid annotation parameter name = %s, value = %s", name, value)); + continue; + } + newParameters.add(Pair.create(name, value)); + } + } + } + + final PsiExpression nullValue = JavaPsiFacade.getElementFactory(myAnnotation.getProject()).createExpressionFromText(PsiKeyword.NULL, null); + for (final String misssedParameter : myMissedElements) { + newParameters.add(Pair.<String, PsiAnnotationMemberValue>create(misssedParameter, nullValue)); + } + + TemplateBuilderImpl builder = null; + for (final Pair<String, PsiAnnotationMemberValue> newParameter : newParameters) { + final PsiAnnotationMemberValue value = + myAnnotation.setDeclaredAttributeValue(newParameter.getFirst(), newParameter.getSecond()); + if (myMissedElements.contains(newParameter.getFirst())) { + if (builder == null) { + builder = new TemplateBuilderImpl(myAnnotation.getParameterList()); + } + builder.replaceElement(value, new EmptyExpression(), true); + } + } + + editor.getCaretModel().moveToOffset(myAnnotation.getParameterList().getTextRange().getStartOffset()); + final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); + final Document document = documentManager.getDocument(file); + if (document == null) { + throw new IllegalStateException(); + } + documentManager.doPostponedOperationsAndUnblockDocument(document); + TemplateManager.getInstance(project).startTemplate(editor, builder.buildInlineTemplate(), null); + } + + @Override + public boolean startInWriteAction() { + return true; + } + + private TObjectIntHashMap<String> getAnnotationsOrderMap() { + final TObjectIntHashMap<String> map = new TObjectIntHashMap<String>(); + for (int i = 0; i < myAnnotationMethods.length; i++) { + map.put(myAnnotationMethods[i].getName(), i); + } + return map; + } + + private static boolean isAlreadyAddedOrdered(final TObjectIntHashMap<String> orderMap, final PsiNameValuePair[] addedParameters) { + if (addedParameters.length <= 1) { + return true; + } + int previousOrder = orderMap.get(addedParameters[0].getName()); + for (int i = 1; i < addedParameters.length; i++) { + final int currentOrder = orderMap.get(addedParameters[i].getName()); + if (currentOrder < previousOrder) { + return false; + } + previousOrder = currentOrder; + } + return true; + } +} diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorParameterFromFieldFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorParameterFromFieldFix.java index f47a780b599a..14e7478ad025 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorParameterFromFieldFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorParameterFromFieldFix.java @@ -25,6 +25,7 @@ import com.intellij.codeInsight.generation.PsiMethodMember; import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.codeInsight.intention.impl.AssignFieldFromParameterAction; import com.intellij.codeInsight.intention.impl.FieldFromParameterUtils; +import com.intellij.codeInspection.ex.GlobalInspectionContextBase; import com.intellij.ide.util.MemberChooser; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.editor.Editor; @@ -123,6 +124,7 @@ public class CreateConstructorParameterFromFieldFix implements IntentionAction { } } }); + final List<PsiElement> cleanupElements = new ArrayList<PsiElement>(); final ArrayList<PsiMethod> constrs = filterConstructorsIfFieldAlreadyAssigned(constructors, getField()); if (constrs.size() > 1) { final PsiMethodMember[] members = new PsiMethodMember[constrs.size()]; @@ -142,7 +144,7 @@ public class CreateConstructorParameterFromFieldFix implements IntentionAction { } for (PsiMethodMember member : elements) { - if (!addParameterToConstructor(project, file, editor, member.getElement(), new PsiField[] {getField()})) break; + if (!addParameterToConstructor(project, file, editor, member.getElement(), new PsiField[] {getField()}, cleanupElements)) break; } } else if (!constrs.isEmpty()) { @@ -177,12 +179,13 @@ public class CreateConstructorParameterFromFieldFix implements IntentionAction { addParameterToConstructor(project, file, editor, constructor, constrs.size() == constructors.length ? fields.toArray(new PsiField[fields.size()]) - : new PsiField[]{getField()}); + : new PsiField[]{getField()}, cleanupElements); } finally { fieldsToFix.clear(); } } + GlobalInspectionContextBase.cleanupElements(project, null, cleanupElements.toArray(new PsiElement[cleanupElements.size()])); } @NotNull @@ -231,7 +234,7 @@ public class CreateConstructorParameterFromFieldFix implements IntentionAction { final PsiFile file, final Editor editor, final PsiMethod constructor, - final PsiField[] fields) throws IncorrectOperationException { + final PsiField[] fields, final List<PsiElement> cleanupElements) throws IncorrectOperationException { final PsiParameterList parameterList = constructor.getParameterList(); final PsiParameter[] parameters = parameterList.getParameters(); ParameterInfoImpl[] newParamInfos = new ParameterInfoImpl[parameters.length + fields.length]; @@ -267,7 +270,7 @@ public class CreateConstructorParameterFromFieldFix implements IntentionAction { return ApplicationManager.getApplication().runWriteAction(new Computable<Boolean>() { @Override public Boolean compute() { - return doCreate(project, editor, parameters, constructorPointer, resultParams, usedFields); + return doCreate(project, editor, parameters, constructorPointer, resultParams, usedFields, cleanupElements); } }); } @@ -309,7 +312,7 @@ public class CreateConstructorParameterFromFieldFix implements IntentionAction { } private static boolean doCreate(Project project, Editor editor, PsiParameter[] parameters, SmartPsiElementPointer constructorPointer, - ParameterInfoImpl[] parameterInfos, HashMap<PsiField, String> fields) { + ParameterInfoImpl[] parameterInfos, HashMap<PsiField, String> fields, List<PsiElement> cleanupElements) { PsiMethod constructor = (PsiMethod)constructorPointer.getElement(); assert constructor != null; PsiParameter[] newParameters = constructor.getParameterList().getParameters(); @@ -324,7 +327,11 @@ public class CreateConstructorParameterFromFieldFix implements IntentionAction { continue; } notNull(project, field, parameter); - AssignFieldFromParameterAction.addFieldAssignmentStatement(project, field, parameter, editor); + cleanupElements.add(parameter); + final PsiElement assignmentStatement = AssignFieldFromParameterAction.addFieldAssignmentStatement(project, field, parameter, editor); + if (assignmentStatement != null) { + cleanupElements.add(assignmentStatement); + } created = true; } } diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateFromUsageUtils.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateFromUsageUtils.java index a66a75855a23..c5763cc2691e 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateFromUsageUtils.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateFromUsageUtils.java @@ -28,6 +28,7 @@ import com.intellij.ide.fileTemplates.FileTemplate; import com.intellij.ide.fileTemplates.FileTemplateManager; import com.intellij.ide.fileTemplates.FileTemplateUtil; import com.intellij.ide.fileTemplates.JavaTemplateUtil; +import com.intellij.lang.java.JavaLanguage; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Editor; @@ -135,7 +136,10 @@ public class CreateFromUsageUtils { JVMElementFactory factory = JVMElementFactories.getFactory(aClass.getLanguage(), aClass.getProject()); - LOG.assertTrue(!aClass.isInterface() || method.hasModifierProperty(PsiModifier.DEFAULT) || method.hasModifierProperty(PsiModifier.STATIC), "Interface bodies should be already set up"); + LOG.assertTrue(!aClass.isInterface() || + method.hasModifierProperty(PsiModifier.DEFAULT) || + method.hasModifierProperty(PsiModifier.STATIC) || + method.getLanguage() != JavaLanguage.INSTANCE, "Interface bodies should be already set up"); FileType fileType = FileTypeManager.getInstance().getFileTypeByExtension(template.getExtension()); Properties properties = new Properties(); diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ShowModulePropertiesFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ShowModulePropertiesFix.java index 203612ee1cec..16b2c2db4831 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ShowModulePropertiesFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ShowModulePropertiesFix.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -15,7 +15,7 @@ */ package com.intellij.codeInsight.daemon.impl.quickfix; -import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.codeInspection.IntentionAndQuickFixAction; import com.intellij.openapi.actionSystem.ActionManager; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.IdeActions; @@ -26,20 +26,23 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.ui.configuration.ProjectSettingsService; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; -import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -public class ShowModulePropertiesFix implements IntentionAction { +public class ShowModulePropertiesFix extends IntentionAndQuickFixAction { private final String myModuleName; public ShowModulePropertiesFix(@NotNull PsiElement context) { - Module module = ModuleUtilCore.findModuleForPsiElement(context); + this(ModuleUtilCore.findModuleForPsiElement(context)); + } + + public ShowModulePropertiesFix(@Nullable Module module) { myModuleName = module == null ? null : module.getName(); } - @Override @NotNull - public String getText() { + @Override + public String getName() { AnAction action = ActionManager.getInstance().getAction(IdeActions.MODULE_SETTINGS); return action.getTemplatePresentation().getText(); } @@ -56,7 +59,7 @@ public class ShowModulePropertiesFix implements IntentionAction { } @Override - public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException { + public void applyFix(@NotNull Project project, PsiFile file, @Nullable Editor editor) { ProjectSettingsService.getInstance(project).showModuleConfigurationDialog(myModuleName, null); } diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SurroundWithQuotesAnnotationParameterValueFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SurroundWithQuotesAnnotationParameterValueFix.java new file mode 100644 index 000000000000..0468ec844a1a --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SurroundWithQuotesAnnotationParameterValueFix.java @@ -0,0 +1,75 @@ +/* + * Copyright 2000-2014 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. + */ +package com.intellij.codeInsight.daemon.impl.quickfix; + +import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.psi.*; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; + +/** + * @author Dmitry Batkovich + */ +public class SurroundWithQuotesAnnotationParameterValueFix implements IntentionAction { + private final PsiAnnotationMemberValue myValue; + private final PsiType myExpectedType; + + public SurroundWithQuotesAnnotationParameterValueFix(final PsiAnnotationMemberValue value, final PsiType expectedType) { + myValue = value; + myExpectedType = expectedType; + } + + @Override + public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { + if (!myValue.isValid() || !(myExpectedType instanceof PsiClassType)) { + return false; + } + final PsiClass resolvedType = ((PsiClassType)myExpectedType).resolve(); + return resolvedType != null && CommonClassNames.JAVA_LANG_STRING.equals(resolvedType.getQualifiedName()) && myValue instanceof PsiLiteralExpression; + } + + @Override + public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { + String newText = myValue.getText(); + newText = StringUtil.stripQuotesAroundValue(newText); + newText = "\"" + newText + "\""; + PsiElement newToken = JavaPsiFacade.getInstance(project).getElementFactory().createExpressionFromText(newText, null); + final PsiElement newElement = myValue.replace(newToken); + editor.getCaretModel().moveToOffset(newElement.getTextOffset() + newElement.getTextLength()); + } + + @NotNull + @Override + public String getFamilyName() { + return "Surround annotation parameter value with quotes"; + } + + @NotNull + @Override + public String getText() { + return getFamilyName(); + } + + @Override + public boolean startInWriteAction() { + return true; + } + + +} |