IDEA-76599 Quickfix to change a double literal to a float literal

This commit is contained in:
anna
2012-02-10 12:14:32 +01:00
parent 992515d681
commit cb0442de53
16 changed files with 225 additions and 1 deletions

View File

@@ -498,6 +498,7 @@ public class HighlightMethodUtil {
AddTypeArgumentsFix.REGISTRAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
registerMethodAccessLevelIntentions(methodCandidates, methodCall, list, highlightInfo);
ChangeMethodSignatureFromUsageFix.registerIntentions(methodCandidates, list, highlightInfo, fixRange);
ConvertDoubleToFloatFix.registerIntentions(methodCandidates, list, highlightInfo, fixRange);
WrapExpressionFix.registerWrapAction(methodCandidates, list.getExpressions(), highlightInfo);
ChangeParameterClassFix.registerQuickFixActions(methodCall, list, highlightInfo);
if (methodCandidates.length == 0) {
@@ -1273,6 +1274,7 @@ public class HighlightMethodUtil {
if (classReference != null) {
ConstructorParametersFixer.registerFixActions(classReference, constructorCall, info, getFixRange(infoElement));
ChangeMethodSignatureFromUsageFix.registerIntentions(results, list, info, null);
ConvertDoubleToFloatFix.registerIntentions(results, list, info, null);
PermuteArgumentsFix.registerFix(info, constructorCall, toMethodCandidates(results), getFixRange(list));
ChangeParameterClassFix.registerQuickFixActions(constructorCall, list, info);
QuickFixAction.registerQuickFixAction(info, getFixRange(list), new SurroundWithArrayFix(constructorCall), null);

View File

@@ -1014,7 +1014,7 @@ public class HighlightUtil {
}
// true if floating point literal consists of zeros only
private static boolean isFPZero(final String text) {
public static boolean isFPZero(final String text) {
for (int i = 0; i < text.length(); i++) {
final char c = text.charAt(i);
if (Character.isDigit(c) && c != '0') return false;

View File

@@ -0,0 +1,109 @@
/*
* Copyright 2000-2012 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.impl.HighlightInfo;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightUtil;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
/**
* User: anna
* Date: 2/10/12
*/
public class ConvertDoubleToFloatFix implements IntentionAction {
private final PsiExpression myExpression;
public ConvertDoubleToFloatFix(PsiExpression expression) {
myExpression = expression;
}
@NotNull
@Override
public String getText() {
return "Convert '" + myExpression.getText() + "' to float";
}
@NotNull
@Override
public String getFamilyName() {
return getText();
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (myExpression.isValid()) {
if (!StringUtil.endsWithIgnoreCase(myExpression.getText(), "d")) {
final PsiLiteralExpression expression = (PsiLiteralExpression)createFloatingPointExpression(project);
final Object value = expression.getValue();
return value instanceof Float && !((Float)value).isInfinite() && !(((Float)value).floatValue() == 0 && !HighlightUtil.isFPZero(expression.getText()));
}
}
return false;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
myExpression.replace(createFloatingPointExpression(project));
}
private PsiExpression createFloatingPointExpression(Project project) {
return JavaPsiFacade.getElementFactory(project).createExpressionFromText(myExpression.getText() + "f", myExpression);
}
@Override
public boolean startInWriteAction() {
return true;
}
public static void registerIntentions(@NotNull JavaResolveResult[] candidates,
@NotNull PsiExpressionList list,
@NotNull HighlightInfo highlightInfo,
TextRange fixRange) {
if (candidates.length == 0) return;
PsiExpression[] expressions = list.getExpressions();
for (JavaResolveResult candidate : candidates) {
registerIntention(expressions, highlightInfo, fixRange, candidate, list);
}
}
private static void registerIntention(@NotNull PsiExpression[] expressions,
@NotNull HighlightInfo highlightInfo,
TextRange fixRange,
@NotNull JavaResolveResult candidate,
@NotNull PsiElement context) {
if (!candidate.isStaticsScopeCorrect()) return;
PsiMethod method = (PsiMethod)candidate.getElement();
if (method != null && context.getManager().isInProject(method)) {
final PsiParameter[] parameters = method.getParameterList().getParameters();
if (parameters.length == expressions.length) {
for (int i = 0, length = parameters.length; i < length; i++) {
PsiParameter parameter = parameters[i];
final PsiExpression expression = expressions[i];
if (expression instanceof PsiLiteralExpression && PsiType.FLOAT.equals(parameter.getType()) && PsiType.DOUBLE.equals(expression.getType())) {
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new ConvertDoubleToFloatFix(expression), null);
}
}
}
}
}
}

View File

@@ -0,0 +1,7 @@
// "Convert '1e1' to float" "true"
class Test {
void bar() {
foo(1e1f);
}
void foo(float f){}
}

View File

@@ -0,0 +1,7 @@
// "Convert '2.' to float" "true"
class Test {
void bar() {
foo(2.f);
}
void foo(float f){}
}

View File

@@ -0,0 +1,7 @@
// "Convert '.3' to float" "true"
class Test {
void bar() {
foo(.3f);
}
void foo(float f){}
}

View File

@@ -0,0 +1,7 @@
// "Convert '0.0' to float" "true"
class Test {
void bar() {
foo(0.0f);
}
void foo(float f){}
}

View File

@@ -0,0 +1,7 @@
// "Convert '3.14' to float" "true"
class Test {
void bar() {
foo(3.14f);
}
void foo(float f){}
}

View File

@@ -0,0 +1,7 @@
// "Convert '1e1' to float" "true"
class Test {
void bar() {
foo(1e<caret>1);
}
void foo(float f){}
}

View File

@@ -0,0 +1,7 @@
// "Convert '2.' to float" "true"
class Test {
void bar() {
foo(2<caret>.);
}
void foo(float f){}
}

View File

@@ -0,0 +1,7 @@
// "Convert '.3' to float" "true"
class Test {
void bar() {
foo(.<caret>3);
}
void foo(float f){}
}

View File

@@ -0,0 +1,7 @@
// "Convert '0.0' to float" "true"
class Test {
void bar() {
foo(0<caret>.0);
}
void foo(float f){}
}

View File

@@ -0,0 +1,7 @@
// "Convert '3.14' to float" "true"
class Test {
void bar() {
foo(3<caret>.14);
}
void foo(float f){}
}

View File

@@ -0,0 +1,7 @@
// "Convert '1e-9d' to float" "false"
class Test {
void bar() {
foo(1e-9<caret>d);
}
void foo(float f){}
}

View File

@@ -0,0 +1,7 @@
// "Convert '1e137' to float" "false"
class Test {
void bar() {
foo(1e1<caret>37);
}
void foo(float f){}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2000-2012 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.quickFix;
/**
* @author cdr
*/
public class ConvertDoubleToFloatFixTest extends LightQuickFix15TestCase {
public void test() throws Exception { doAllTests(); }
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/convertDoubleToFloat";
}
}