Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit d83efc3

Browse files
[nnbd_migration] Apply the changes to disk on apply-migration request
This integrates fairly well with the new dartdev ux. We may be able to improve the wording, etc., but given the fact that it has to be rerun with --apply-edits, there is no need to notify anyone that the edits occurred (currently). I'll take another look at potential wording improvements in a follow-up CL. In the dartfix ux, the dartfix driver doesn't get notified of the changes and will ask if you want to apply, and this is not as good :) If you say yes, you will apply the edits twice. Given that there is already a CL to delete that code, I didn't change this in dartfix and don't plan to. Change-Id: Ie822a5761d05b3dd08b5a793f812a331d4657598 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/138203 Reviewed-by: Paul Berry <[email protected]>
1 parent 9eb655e commit d83efc3

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

pkg/analysis_server/lib/src/edit/preview/preview_site.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import 'package:analysis_server/src/edit/preview/index_file_page.dart';
1717
import 'package:analysis_server/src/edit/preview/navigation_tree_page.dart';
1818
import 'package:analysis_server/src/edit/preview/not_found_page.dart';
1919
import 'package:analysis_server/src/edit/preview/region_page.dart';
20+
import 'package:analysis_server/src/protocol_server.dart';
2021
import 'package:analysis_server/src/status/pages.dart';
2122
import 'package:analyzer/file_system/file_system.dart';
2223

@@ -205,7 +206,16 @@ class PreviewSite extends Site implements AbstractGetHandler {
205206
throw StateError('Cannot reapply migration.');
206207
}
207208

209+
final edits = migrationState.listener.sourceChange.edits;
210+
211+
// Eagerly mark the migration applied. If this throws, we cannot go back.
208212
migrationState.markApplied();
213+
for (final fileEdit in edits) {
214+
final file = pathMapper.provider.getFile(fileEdit.file);
215+
String code = file.exists ? file.readAsStringSync() : '';
216+
code = SourceEdit.applySequence(code, fileEdit.edits);
217+
file.writeAsStringSync(code);
218+
}
209219
}
210220

211221
@override

pkg/analysis_server/test/src/edit/preview/preview_site_test.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:analysis_server/src/edit/nnbd_migration/path_mapper.dart';
99
import 'package:analysis_server/src/edit/preview/preview_site.dart';
1010
import 'package:analyzer/file_system/file_system.dart';
1111
import 'package:analyzer/file_system/memory_file_system.dart';
12+
import 'package:analyzer_plugin/protocol/protocol_common.dart';
1213
import 'package:test/test.dart';
1314
import 'package:test_reflective_loader/test_reflective_loader.dart';
1415

@@ -35,8 +36,50 @@ class PreviewSiteTest {
3536
site = PreviewSite(state);
3637
}
3738

39+
void test_applyChangesEmpty() {
40+
resourceProvider.getFile('/test.dart').writeAsStringSync('void main() {}');
41+
site.performApply();
42+
expect(resourceProvider.getFile('/test.dart').readAsStringSync(),
43+
'void main() {}');
44+
expect(state.hasBeenApplied, true);
45+
}
46+
3847
void test_applyChangesTwiceThrows() {
3948
site.performApply();
4049
expect(site.performApply, throwsA(isA<StateError>()));
4150
}
51+
52+
void test_applyMultipleChanges() {
53+
resourceProvider.getFile('/test.dart').writeAsStringSync('void main() {}');
54+
dartfixListener.addSourceChange(
55+
"test change",
56+
Location('/test.dart', 10, 0, 1, 10),
57+
SourceChange("test change", edits: [
58+
SourceFileEdit('/test.dart', 0, edits: [
59+
SourceEdit(10, 0, 'List args'),
60+
SourceEdit(13, 0, '\n print(args);\n')
61+
])
62+
]));
63+
site.performApply();
64+
expect(resourceProvider.getFile('/test.dart').readAsStringSync(), '''
65+
void main(List args) {
66+
print(args);
67+
}''');
68+
expect(state.hasBeenApplied, true);
69+
}
70+
71+
void test_applySingleChange() {
72+
resourceProvider.getFile('/test.dart').writeAsStringSync('void main() {}');
73+
dartfixListener.addSourceChange(
74+
"test change",
75+
Location('/test.dart', 10, 0, 1, 10),
76+
SourceChange("test change", edits: [
77+
SourceFileEdit('/test.dart', 0,
78+
edits: [SourceEdit(10, 0, 'List args')])
79+
]));
80+
site.performApply();
81+
expect(resourceProvider.getFile('/test.dart').readAsStringSync(),
82+
'void main(List args) {}');
83+
expect(state.hasBeenApplied, true);
84+
}
4285
}

0 commit comments

Comments
 (0)