Skip to content

Commit

Permalink
new convert-to-int-literal assist
Browse files Browse the repository at this point in the history
Change-Id: I25f08d82fd4afaba485059e558536b37855bb6c9
Reviewed-on: https://dart-review.googlesource.com/c/81301
Commit-Queue: Dan Rubel <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
  • Loading branch information
Dan Rubel authored and [email protected] committed Oct 23, 2018
1 parent 58d73e0 commit 4f2a4e6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/assist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class DartAssistKind {
'dart.assist.convert.toConstructorFieldParameter',
30,
"Convert to field formal parameter");
static const CONVERT_TO_INT_LITERAL = const AssistKind(
'dart.assist.convert.toIntLiteral', 30, "Convert to an int literal");
static const CONVERT_TO_NORMAL_PARAMETER = const AssistKind(
'dart.assist.convert.toConstructorNormalParameter',
30,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class AssistProcessor {
await _addProposal_convertPartOfToUri();
await _addProposal_convertToForIndexLoop();
await _addProposal_convertToGenericFunctionSyntax();
await _addProposal_convertToIntLiteral();
await _addProposal_convertToIsNot_onIs();
await _addProposal_convertToIsNot_onNot();
await _addProposal_convertToIsNotEmpty();
Expand Down Expand Up @@ -176,6 +177,8 @@ class AssistProcessor {
// Calculate only specific assists for edit.dartFix
if (assistKind == DartAssistKind.CONVERT_CLASS_TO_MIXIN) {
await _addProposal_convertClassToMixin();
} else if (assistKind == DartAssistKind.CONVERT_TO_INT_LITERAL) {
await _addProposal_convertToIntLiteral();
}
return assists;
}
Expand Down Expand Up @@ -382,6 +385,33 @@ class AssistProcessor {
}
}

Future<void> _addProposal_convertToIntLiteral() async {
if (node is! DoubleLiteral) {
_coverageMarker();
return;
}
DoubleLiteral literal = node;
int intValue;
try {
intValue = literal.value?.truncate();
} catch (e) {
// Double cannot be converted to int
}
if (intValue == null || intValue != literal.value) {
_coverageMarker();
return;
}

DartChangeBuilder changeBuilder = new DartChangeBuilder(session);
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
builder.addReplacement(new SourceRange(literal.offset, literal.length),
(DartEditBuilder builder) {
builder.write('${intValue}');
});
});
_addAssistFromBuilder(changeBuilder, DartAssistKind.CONVERT_TO_INT_LITERAL);
}

Future<void> _addProposal_assignToLocalVariable() async {
// TODO(brianwilkerson) Determine whether this await is necessary.
await null;
Expand Down
32 changes: 32 additions & 0 deletions pkg/analysis_server/test/services/correction/assist_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2504,6 +2504,38 @@ class A {
''');
}

test_convertToIntLiteral() async {
await resolveTestUnit('''
const double myDouble = 42.0;
''');
await assertHasAssistAt('42.0', DartAssistKind.CONVERT_TO_INT_LITERAL, '''
const double myDouble = 42;
''');
}

test_convertToIntLiteral_e() async {
await resolveTestUnit('''
const double myDouble = 4.2e1;
''');
await assertHasAssistAt('4.2e1', DartAssistKind.CONVERT_TO_INT_LITERAL, '''
const double myDouble = 42;
''');
}

test_convertToIntLiteral_eBig() async {
await resolveTestUnit('''
const double myDouble = 4.2e99999;
''');
await assertNoAssistAt('4.2e99999', DartAssistKind.CONVERT_TO_INT_LITERAL);
}

test_convertToIntLiteral_notDouble() async {
await resolveTestUnit('''
const double myDouble = 42;
''');
await assertNoAssistAt('42', DartAssistKind.CONVERT_TO_INT_LITERAL);
}

test_convertToIsNot_BAD_is_alreadyIsNot() async {
await resolveTestUnit('''
main(p) {
Expand Down

0 comments on commit 4f2a4e6

Please sign in to comment.