diff --git a/pkg/analysis_server/lib/src/services/correction/assist.dart b/pkg/analysis_server/lib/src/services/correction/assist.dart index 1b98a13028f70..772299c3dbf9c 100644 --- a/pkg/analysis_server/lib/src/services/correction/assist.dart +++ b/pkg/analysis_server/lib/src/services/correction/assist.dart @@ -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, diff --git a/pkg/analysis_server/lib/src/services/correction/assist_internal.dart b/pkg/analysis_server/lib/src/services/correction/assist_internal.dart index d0f2695fe10e6..dffd207c95d2a 100644 --- a/pkg/analysis_server/lib/src/services/correction/assist_internal.dart +++ b/pkg/analysis_server/lib/src/services/correction/assist_internal.dart @@ -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(); @@ -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; } @@ -382,6 +385,33 @@ class AssistProcessor { } } + Future _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 _addProposal_assignToLocalVariable() async { // TODO(brianwilkerson) Determine whether this await is necessary. await null; diff --git a/pkg/analysis_server/test/services/correction/assist_test.dart b/pkg/analysis_server/test/services/correction/assist_test.dart index a4430d1dc533d..b9e542f5b389e 100644 --- a/pkg/analysis_server/test/services/correction/assist_test.dart +++ b/pkg/analysis_server/test/services/correction/assist_test.dart @@ -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) {