From 4336c764668fbc71f41a71843fd5d57c8bf65bfe Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 8 Oct 2010 12:30:19 +0200 Subject: [PATCH] IDEA-59582 (intention Convert to Plain for double literal does not work) --- ...ConvertToEngineeringNotationIntention.java | 42 +++++++++++------ .../ipp/integer/ConvertToPlainIntention.java | 47 +++++++++++++------ .../ipp/integer/ConvertToPlainPredicate.java | 2 +- 3 files changed, 62 insertions(+), 29 deletions(-) diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/integer/ConvertToEngineeringNotationIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/integer/ConvertToEngineeringNotationIntention.java index 22d725470c76..508f57e6b629 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/integer/ConvertToEngineeringNotationIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/integer/ConvertToEngineeringNotationIntention.java @@ -28,21 +28,35 @@ import java.text.DecimalFormat; * @author Konstantin Bulenkov */ public class ConvertToEngineeringNotationIntention extends Intention { - private static final DecimalFormat FORMAT = new DecimalFormat("0.00000000000000E00"); - private static final ConvertToEngineeringNotationPredicate PREDICATE = new ConvertToEngineeringNotationPredicate(); - @Override - protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException { - String text = FORMAT.format(Double.parseDouble(element.getText())).replace(',', '.'); - while (text.contains("0E") && !text.contains(".0E")) { - text = text.replace("0E", "E"); + private static final DecimalFormat FORMAT = + new DecimalFormat("0.00000000000000E00"); + private static final ConvertToEngineeringNotationPredicate PREDICATE = + new ConvertToEngineeringNotationPredicate(); + + @Override + protected void processIntention(@NotNull PsiElement element) + throws IncorrectOperationException { + final String elementText = element.getText(); + if (elementText.length() == 0) { + return; + } + final int lastIndex = elementText.length() - 1; + final char lastChar = elementText.charAt(lastIndex); + String text = FORMAT.format(Double.parseDouble(elementText)).replace(',', '.'); + while (text.contains("0E") && !text.contains(".0E")) { + text = text.replace("0E", "E"); + } + if (lastChar == 'f' || lastChar == 'F') { + replaceExpression(text + lastChar, (PsiExpression)element); + } else { + replaceExpression(text, (PsiExpression)element); + } } - replaceExpression(text, (PsiExpression)element); - } - @NotNull - @Override - protected PsiElementPredicate getElementPredicate() { - return PREDICATE; - } + @NotNull + @Override + protected PsiElementPredicate getElementPredicate() { + return PREDICATE; + } } diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/integer/ConvertToPlainIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/integer/ConvertToPlainIntention.java index 9d31b0b6f3e2..ea0d42785008 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/integer/ConvertToPlainIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/integer/ConvertToPlainIntention.java @@ -22,24 +22,43 @@ import com.siyeh.ipp.base.Intention; import com.siyeh.ipp.base.PsiElementPredicate; import org.jetbrains.annotations.NotNull; +import java.math.BigDecimal; + /** * @author Konstantin Bulenkov */ public class ConvertToPlainIntention extends Intention { - private static final ConvertToPlainPredicate PREDICATE = new ConvertToPlainPredicate(); - @Override - protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException { - try { - replaceExpression(Double.toString(Double.parseDouble(element.getText())), (PsiExpression)element); - } - catch (Exception e) {// - } - } + private static final ConvertToPlainPredicate PREDICATE = + new ConvertToPlainPredicate(); - @NotNull - @Override - protected PsiElementPredicate getElementPredicate() { - return PREDICATE; - } + @Override + protected void processIntention(@NotNull PsiElement element) + throws IncorrectOperationException { + try { + final String elementText = element.getText(); + if (elementText.length() == 0) { + return; + } + final int lastIndex = elementText.length() - 1; + final char lastChar = elementText.charAt(lastIndex); + if (lastChar == 'f' || lastChar == 'F') { + final BigDecimal bigDecimal = + new BigDecimal(elementText.substring(0, lastIndex)); + replaceExpression(bigDecimal.toPlainString() + lastChar, + (PsiExpression) element); + } else { + final BigDecimal bigDecimal = new BigDecimal(elementText); + replaceExpression(bigDecimal.toPlainString(), + (PsiExpression) element); + } + } catch (Exception e) {// + } + } + + @NotNull + @Override + protected PsiElementPredicate getElementPredicate() { + return PREDICATE; + } } diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/integer/ConvertToPlainPredicate.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/integer/ConvertToPlainPredicate.java index a12df598692e..25060ba0c2e9 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/integer/ConvertToPlainPredicate.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/integer/ConvertToPlainPredicate.java @@ -24,7 +24,7 @@ import com.siyeh.ipp.base.PsiElementPredicate; * @author Konstantin Bulenkov */ public class ConvertToPlainPredicate implements PsiElementPredicate { - @Override + public boolean satisfiedBy(PsiElement element) { if (element instanceof PsiLiteralExpression) { final PsiLiteralExpression expression = (PsiLiteralExpression)element;