Skip to content

Commit 2048190

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Makes encapsulate field work with final
[email protected] Fixes: #60154 Change-Id: I45c887169672e3cdbce4aea95000996c09e8123b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/410460 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Phil Quitslund <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Auto-Submit: Felipe Morschel <[email protected]>
1 parent 2647712 commit 2048190

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

pkg/analysis_server/lib/src/services/correction/dart/encapsulate_field.dart

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analysis_server/src/services/correction/assist.dart';
6+
import 'package:analysis_server/src/utilities/extensions/ast.dart';
67
import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
78
import 'package:analyzer/dart/ast/ast.dart';
89
import 'package:analyzer/dart/element/element2.dart';
@@ -17,9 +18,8 @@ class EncapsulateField extends ResolvedCorrectionProducer {
1718

1819
@override
1920
CorrectionApplicability get applicability =>
20-
// TODO(applicability): comment on why.
21-
CorrectionApplicability
22-
.singleLocation;
21+
// TODO(applicability): comment on why.
22+
CorrectionApplicability.singleLocation;
2323

2424
@override
2525
AssistKind get assistKind => DartAssistKind.ENCAPSULATE_FIELD;
@@ -40,10 +40,6 @@ class EncapsulateField extends ResolvedCorrectionProducer {
4040
if (variableList.keyword == null && variableList.type == null) {
4141
return;
4242
}
43-
// not interesting for final
44-
if (variableList.isFinal) {
45-
return;
46-
}
4743
// should have exactly one field
4844
var fields = variableList.variables;
4945
if (fields.length != 1) {
@@ -88,6 +84,16 @@ class EncapsulateField extends ResolvedCorrectionProducer {
8884
}
8985
// rename field
9086
builder.addSimpleReplacement(range.token(nameToken), '_$name');
87+
// Write setter.
88+
if (variableList.finalKeyword case var finalKeyword?) {
89+
builder.addSimpleReplacement(
90+
range.startStart(
91+
finalKeyword,
92+
variableList.type ?? variableList.variables.first,
93+
),
94+
'',
95+
);
96+
}
9197

9298
String fieldTypeCode;
9399
var type = fieldDeclaration.fields.type;
@@ -147,6 +153,9 @@ class EncapsulateField extends ResolvedCorrectionProducer {
147153
builder.write(' ${typeCode}get $name => _$name;');
148154

149155
// Write setter.
156+
if (variableList.isFinal) {
157+
return;
158+
}
150159
var overriddenSetters = inheritanceManager.getOverridden4(
151160
parentElement,
152161
Name(null, '$name='),

pkg/analysis_server/test/src/services/correction/assist/encapsulate_field_test.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,7 @@ enum E {
279279
final int test = 42;
280280
}
281281
''');
282-
// Enums can have only final fields, and final fields cannot be encapsulated
283-
// right now.
282+
// Enums can have only final fields.
284283
await assertNoAssistAt('test = 42');
285284
}
286285

@@ -300,7 +299,13 @@ class A {
300299
final int test = 42;
301300
}
302301
''');
303-
await assertNoAssistAt('test =');
302+
await assertHasAssistAt('test =', '''
303+
class A {
304+
int _test = 42;
305+
306+
int get test => _test;
307+
}
308+
''');
304309
}
305310

306311
Future<void> test_hasType() async {

0 commit comments

Comments
 (0)