-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge 45efccb into dev
- Loading branch information
Showing
58 changed files
with
291 additions
and
564 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
pkg/analysis_server/lib/src/services/correction/dart/convert_to_if_case_statement_chain.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:analysis_server/src/services/correction/assist.dart'; | ||
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart'; | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer_plugin/utilities/assist/assist.dart'; | ||
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; | ||
import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart'; | ||
import 'package:analyzer_plugin/utilities/range_factory.dart'; | ||
|
||
class ConvertToIfCaseStatementChain extends CorrectionProducer { | ||
@override | ||
AssistKind get assistKind => | ||
DartAssistKind.CONVERT_TO_IF_CASE_STATEMENT_CHAIN; | ||
|
||
@override | ||
Future<void> compute(ChangeBuilder builder) async { | ||
final switchStatement = node; | ||
if (switchStatement is! SwitchStatement) { | ||
return; | ||
} | ||
|
||
final ifIndent = utils.getLinePrefix(switchStatement.offset); | ||
final expressionCode = utils.getNodeText(switchStatement.expression); | ||
|
||
final switchPatternCases = <SwitchPatternCase>[]; | ||
SwitchDefault? defaultCase; | ||
for (final member in switchStatement.members) { | ||
switch (member) { | ||
case SwitchPatternCase(): | ||
switchPatternCases.add(member); | ||
case SwitchDefault(): | ||
defaultCase = member; | ||
default: | ||
return; | ||
} | ||
} | ||
|
||
await builder.addDartFileEdit(file, (builder) { | ||
builder.addReplacement(range.node(switchStatement), (builder) { | ||
var isFirst = true; | ||
for (final case_ in switchPatternCases) { | ||
if (isFirst) { | ||
isFirst = false; | ||
} else { | ||
builder.write(' else '); | ||
} | ||
final patternCode = utils.getNodeText(case_.guardedPattern); | ||
builder.writeln('if ($expressionCode case $patternCode) {'); | ||
_writeStatements( | ||
builder: builder, | ||
blockIndent: ifIndent, | ||
statements: case_.statements, | ||
); | ||
builder.write('$ifIndent}'); | ||
} | ||
if (defaultCase case final defaultCase?) { | ||
builder.writeln(' else {'); | ||
_writeStatements( | ||
builder: builder, | ||
blockIndent: ifIndent, | ||
statements: defaultCase.statements, | ||
); | ||
builder.write('$ifIndent}'); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
void _writeStatements({ | ||
required DartEditBuilder builder, | ||
required List<Statement> statements, | ||
required String blockIndent, | ||
}) { | ||
final range = utils.getLinesRangeStatements(statements); | ||
|
||
final firstIndent = utils.getLinePrefix(statements.first.offset); | ||
final singleIndent = utils.getIndent(1); | ||
|
||
final code = utils.replaceSourceRangeIndent( | ||
range, | ||
firstIndent, | ||
blockIndent + singleIndent, | ||
); | ||
builder.write(code); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...s_server/test/src/services/correction/assist/convert_to_if_case_statement_chain_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:analysis_server/src/services/correction/assist.dart'; | ||
import 'package:analyzer_plugin/utilities/assist/assist.dart'; | ||
import 'package:test_reflective_loader/test_reflective_loader.dart'; | ||
|
||
import 'assist_processor.dart'; | ||
|
||
void main() { | ||
defineReflectiveSuite(() { | ||
defineReflectiveTests(ConvertToIfCaseStatementChainTest); | ||
}); | ||
} | ||
|
||
@reflectiveTest | ||
class ConvertToIfCaseStatementChainTest extends AssistProcessorTest { | ||
@override | ||
AssistKind get kind => DartAssistKind.CONVERT_TO_IF_CASE_STATEMENT_CHAIN; | ||
|
||
Future<void> test_noDefault() async { | ||
await resolveTestCode(''' | ||
void f(Object? x) { | ||
switch (x) { | ||
case int(): | ||
0; | ||
case double(): | ||
1; | ||
} | ||
} | ||
'''); | ||
await assertHasAssistAt('switch', ''' | ||
void f(Object? x) { | ||
if (x case int()) { | ||
0; | ||
} else if (x case double()) { | ||
1; | ||
} | ||
} | ||
'''); | ||
} | ||
|
||
Future<void> test_withDefault() async { | ||
await resolveTestCode(''' | ||
void f(Object? x) { | ||
switch (x) { | ||
case int(): | ||
0; | ||
case double(): | ||
1; | ||
default: | ||
2; | ||
} | ||
} | ||
'''); | ||
await assertHasAssistAt('switch', ''' | ||
void f(Object? x) { | ||
if (x case int()) { | ||
0; | ||
} else if (x case double()) { | ||
1; | ||
} else { | ||
2; | ||
} | ||
} | ||
'''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.