mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-67549 (convert to plain/engineering to respect literal underscores)
This commit is contained in:
@@ -57,9 +57,9 @@ convert.integer.to.hex.intention.name=Convert to hex
|
||||
convert.integer.to.hex.intention.family.name=Convert to Hexadecimal
|
||||
convert.integer.to.binary.intention.name=Convert to binary
|
||||
convert.integer.to.binary.intention.family.name=Convert to Binary
|
||||
convert.to.engineering.notation.intention.name=Convert to Engineering notation
|
||||
convert.to.engineering.notation.intention.family.name=Convert to Engineering notation
|
||||
convert.to.plain.intention.name=Convert to Plain
|
||||
convert.to.engineering.notation.intention.name=Convert to engineering notation
|
||||
convert.to.engineering.notation.intention.family.name=Convert to Engineering Notation
|
||||
convert.to.plain.intention.name=Convert to plain
|
||||
convert.to.plain.intention.family.name=Convert to Plain
|
||||
string.to.char.intention.name=Replace string literal with character
|
||||
string.to.char.intention.family.name=Replace String with Char
|
||||
|
||||
+17
-34
@@ -15,48 +15,31 @@
|
||||
*/
|
||||
package com.siyeh.ipp.integer;
|
||||
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.ipp.base.Intention;
|
||||
import com.intellij.psi.PsiType;
|
||||
import com.siyeh.ipp.base.PsiElementPredicate;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* @author Konstantin Bulenkov
|
||||
* todo: actually it's a scientific notation, not an engineering one.
|
||||
*/
|
||||
public class ConvertToEngineeringNotationIntention extends Intention {
|
||||
public class ConvertToEngineeringNotationIntention extends ConvertNumberIntentionBase {
|
||||
private static final DecimalFormat FORMAT = new DecimalFormat("0.0#############E00", new DecimalFormatSymbols(Locale.US));
|
||||
|
||||
private static final DecimalFormat FORMAT =
|
||||
new DecimalFormat("0.00000000000000E00");
|
||||
private static final ConvertToEngineeringNotationPredicate PREDICATE =
|
||||
new ConvertToEngineeringNotationPredicate();
|
||||
@Override
|
||||
protected String convertValue(final Number value, final PsiType type, final boolean negated) {
|
||||
final double doubleValue = Double.parseDouble(value.toString()); // convert to double w/o adding parasitic digits
|
||||
final String text = FORMAT.format(negated ? -doubleValue : doubleValue);
|
||||
return PsiType.FLOAT.equals(type) ? text + "f" : text;
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected PsiElementPredicate getElementPredicate() {
|
||||
return PREDICATE;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
protected PsiElementPredicate getElementPredicate() {
|
||||
return new ConvertToEngineeringNotationPredicate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,10 +15,7 @@
|
||||
*/
|
||||
package com.siyeh.ipp.integer;
|
||||
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.ipp.base.Intention;
|
||||
import com.intellij.psi.PsiType;
|
||||
import com.siyeh.ipp.base.PsiElementPredicate;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -27,38 +24,18 @@ import java.math.BigDecimal;
|
||||
/**
|
||||
* @author Konstantin Bulenkov
|
||||
*/
|
||||
public class ConvertToPlainIntention extends Intention {
|
||||
public class ConvertToPlainIntention extends ConvertNumberIntentionBase {
|
||||
@Override
|
||||
protected String convertValue(final Number value, final PsiType type, final boolean negated) {
|
||||
String text = new BigDecimal(value.toString()).toPlainString();
|
||||
if (negated) text = "-" + text;
|
||||
if (PsiType.FLOAT.equals(type)) text += "f";
|
||||
return text;
|
||||
}
|
||||
|
||||
private static final ConvertToPlainPredicate PREDICATE =
|
||||
new ConvertToPlainPredicate();
|
||||
|
||||
@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;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
protected PsiElementPredicate getElementPredicate() {
|
||||
return new ConvertToPlainPredicate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
double x = <caret>1.23456789E03;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
double x = 1234.56789;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
float x = -<caret>12345.6789f;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
float x = -1.2345679E04f;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
float x = -<caret>1.2345679E04f;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
float x = -12345.679f;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
double x = <caret>625347615293854987235496527432424234625347615293854987235496527432424234625347615293854987235496527432424234.42;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
double x = 6.25347615293855E107;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
double x = <caret>1234.567_89;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
double x = 1.23456789E03;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
double x = <caret>1.234_567_890E03;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
double x = 1234.56789;
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.siyeh.ipp.integer;
|
||||
|
||||
import com.siyeh.IntentionPowerPackBundle;
|
||||
import com.siyeh.ipp.IPPTestCase;
|
||||
|
||||
public class ConvertToEngineeringNotationTest extends IPPTestCase {
|
||||
public void testPlainToEng() { doTest(); }
|
||||
public void testNegatedFloatToEng() { doTest(); }
|
||||
public void testToEngWithUnderscores() { doTest(); }
|
||||
|
||||
@Override
|
||||
protected String getIntentionName() {
|
||||
return IntentionPowerPackBundle.message("convert.to.engineering.notation.intention.name");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getRelativePath() {
|
||||
return "float";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.siyeh.ipp.integer;
|
||||
|
||||
import com.siyeh.IntentionPowerPackBundle;
|
||||
import com.siyeh.ipp.IPPTestCase;
|
||||
|
||||
public class ConvertToPlainTest extends IPPTestCase {
|
||||
public void testEngToPlain() { doTest(); }
|
||||
public void testNegatedFloatToPlain() { doTest(); }
|
||||
public void testToPlainWithUnderscores() { doTest(); }
|
||||
|
||||
@Override
|
||||
protected String getIntentionName() {
|
||||
return IntentionPowerPackBundle.message("convert.to.plain.intention.name");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getRelativePath() {
|
||||
return "float";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user