Merge remote-tracking branch 'origin/master'

This commit is contained in:
Anna Kozlova
2014-06-04 16:48:55 +04:00
22 changed files with 156 additions and 299 deletions
@@ -260,4 +260,6 @@ public abstract class QuickFixFactory {
public abstract IntentionAction createAddMissingRequiredAnnotationParametersFix(@NotNull PsiAnnotation annotation,
@NotNull PsiMethod[] annotationMethods,
@NotNull Collection<String> missedElements);
@NotNull
public abstract IntentionAction createSurroundWithQuotesAnnotationParameterValueFix(@NotNull PsiAnnotationMemberValue value, @NotNull PsiType expectedType);
}
@@ -153,7 +153,10 @@ public class AnnotationsHighlightUtil {
String description = JavaErrorMessages.message("annotation.incompatible.types",
JavaHighlightUtil.formatType(type), JavaHighlightUtil.formatType(expectedType));
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(value).descriptionAndTooltip(description).create();
final HighlightInfo info =
HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(value).descriptionAndTooltip(description).create();
QuickFixAction.registerQuickFixAction(info, QuickFixFactory.getInstance().createSurroundWithQuotesAnnotationParameterValueFix(value, expectedType));
return info;
}
LOG.error("Unknown annotation member value: " + value);
@@ -595,4 +595,11 @@ public class EmptyQuickFixFactory extends QuickFixFactory {
public IntentionAction createAddMissingRequiredAnnotationParametersFix(@NotNull PsiAnnotation psiAnnotation, @NotNull PsiMethod[] psiMethods, @NotNull Collection<String> strings) {
return QuickFixes.EMPTY_FIX;
}
@NotNull
@Override
public IntentionAction createSurroundWithQuotesAnnotationParameterValueFix(@NotNull PsiAnnotationMemberValue value,
@NotNull PsiType expectedType) {
return QuickFixes.EMPTY_FIX;
}
}
@@ -0,0 +1,76 @@
/*
* 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.psi.util.PsiTreeUtil;
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;
}
}
@@ -15,13 +15,15 @@
*/
package com.intellij.codeInsight.intention.impl;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.intention.BaseElementAtCaretIntentionAction;
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -34,11 +36,15 @@ public class CreateSwitchIntention extends BaseElementAtCaretIntentionAction {
@Override
public void invoke(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) throws IncorrectOperationException {
if (!FileModificationService.getInstance().preparePsiElementsForWrite(element)) {
return;
}
final PsiExpressionStatement expressionStatement = resolveExpressionStatement(element);
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
PsiSwitchStatement switchStatement = (PsiSwitchStatement)elementFactory
.createStatementFromText(String.format("switch (%s) {\n$MARKER$\n}", expressionStatement.getExpression().getText()), null);
switchStatement = (PsiSwitchStatement)expressionStatement.replace(switchStatement);
CodeStyleManager.getInstance(project).reformat(switchStatement);
for (final PsiStatement psiStatement : switchStatement.getBody().getStatements()) {
if (psiStatement.getText().equals("$MARKER$")) {
@@ -55,7 +61,7 @@ public class CreateSwitchIntention extends BaseElementAtCaretIntentionAction {
@Override
public boolean isAvailable(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) {
final PsiExpressionStatement expressionStatement = resolveExpressionStatement(element);
return expressionStatement != null && isValidTypeForSwitch(expressionStatement.getExpression().getType());
return expressionStatement != null && isValidTypeForSwitch(expressionStatement.getExpression().getType(), expressionStatement);
}
private static PsiExpressionStatement resolveExpressionStatement(final PsiElement element) {
@@ -67,7 +73,7 @@ public class CreateSwitchIntention extends BaseElementAtCaretIntentionAction {
}
}
private static boolean isValidTypeForSwitch(@Nullable final PsiType type) {
private static boolean isValidTypeForSwitch(@Nullable final PsiType type, final PsiElement context) {
if (type == null) {
return false;
}
@@ -77,7 +83,8 @@ public class CreateSwitchIntention extends BaseElementAtCaretIntentionAction {
if (resolvedClass == null) {
return false;
}
return resolvedClass.isEnum() || (CommonClassNames.JAVA_LANG_STRING.equals(resolvedClass.getQualifiedName()));
return (PsiUtil.isLanguageLevel5OrHigher(context) && resolvedClass.isEnum())
|| (PsiUtil.isLanguageLevel7OrHigher(context) && CommonClassNames.JAVA_LANG_STRING.equals(resolvedClass.getQualifiedName()));
}
return type.equals(PsiType.INT) || type.equals(PsiType.BYTE) || type.equals(PsiType.SHORT) || type.equals(PsiType.CHAR);
}
@@ -1,105 +0,0 @@
/*
* 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.intention.impl;
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import java.util.Set;
/**
* @author Dmitry Batkovich
*/
public class SurroundWithQuotesStringAnnotationParameterValueIntention extends PsiElementBaseIntentionAction {
private static final Set<IElementType> SUITABLE_TYPES = ContainerUtil.newHashSet(JavaTokenType.LONG_LITERAL,
JavaTokenType.FLOAT_LITERAL,
JavaTokenType.INTEGER_LITERAL,
JavaTokenType.DOUBLE_LITERAL,
JavaTokenType.CHARACTER_LITERAL,
JavaTokenType.TRUE_KEYWORD,
JavaTokenType.FALSE_KEYWORD);
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
String newText = element.getText();
if (((PsiJavaToken)element).getTokenType().equals(JavaTokenType.CHARACTER_LITERAL)) {
newText = newText.substring(1, newText.length() - 1);
}
newText = "\"" + newText + "\"";
PsiElement newToken = JavaPsiFacade.getInstance(project).getElementFactory().createExpressionFromText(newText, null).getFirstChild();
final PsiElement newElement = element.replace(newToken);
editor.getCaretModel().moveToOffset(newElement.getTextOffset() + newElement.getTextLength());
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
if (!(element instanceof PsiJavaToken && SUITABLE_TYPES.contains(((PsiJavaToken)element).getTokenType()))) {
return false;
}
final PsiElement literalExpression = element.getParent();
if (literalExpression == null) {
return false;
}
final PsiElement nameValuePair = literalExpression.getParent();
if (!(nameValuePair instanceof PsiNameValuePair)) {
return false;
}
final PsiAnnotation annotation = PsiTreeUtil.getParentOfType(nameValuePair, PsiAnnotation.class);
if (annotation == null) {
return false;
}
final PsiJavaCodeReferenceElement nameRef = annotation.getNameReferenceElement();
if (nameRef == null) {
return false;
}
final PsiElement resolved = nameRef.resolve();
if (!(resolved instanceof PsiClass)) {
return false;
}
final String parameterName = ((PsiNameValuePair)nameValuePair).getName();
final PsiMethod[] methods =
((PsiClass)resolved).findMethodsByName(parameterName == null ? PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME : parameterName, false);
if (methods.length != 1) {
return false;
}
final PsiType methodReturnType = methods[0].getReturnType();
if (!(methodReturnType instanceof PsiClassType)) {
return false;
}
final PsiClass returnTypeClass = ((PsiClassType)methodReturnType).resolve();
return returnTypeClass != null && CommonClassNames.JAVA_LANG_STRING.equals(returnTypeClass.getQualifiedName());
}
@NotNull
@Override
public String getFamilyName() {
return "Surround annotation parameter value with quotes";
}
@NotNull
@Override
public String getText() {
return getFamilyName();
}
}
@@ -755,6 +755,13 @@ public class QuickFixFactoryImpl extends QuickFixFactory {
return new AddMissingRequiredAnnotationParametersFix(annotation, annotationMethods, missedElements);
}
@NotNull
@Override
public IntentionAction createSurroundWithQuotesAnnotationParameterValueFix(@NotNull PsiAnnotationMemberValue value,
@NotNull PsiType expectedType) {
return new SurroundWithQuotesAnnotationParameterValueFix(value, expectedType);
}
private static boolean timeToOptimizeImports(@NotNull PsiFile file) {
if (!CodeInsightSettings.getInstance().OPTIMIZE_IMPORTS_ON_THE_FLY) return false;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.intention;
package com.intellij.codeInsight.daemon.quickFix;
import com.intellij.codeInsight.daemon.LightIntentionActionTestCase;