Skip to content

Commit 1ae6f1b

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Support for creating and displaying edits in the preview tool
Change-Id: I2284ceeb0229d7111022eb3759e39752264f1bc6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/125492 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 8fbb053 commit 1ae6f1b

3 files changed

Lines changed: 36 additions & 5 deletions

File tree

pkg/analysis_server/lib/src/edit/nnbd_migration/info_builder.dart

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,19 @@ class InfoBuilder {
324324
return details;
325325
}
326326

327+
/// Return a list of edits that can be applied.
328+
List<EditDetail> _computeEdits(FixInfo fixInfo) {
329+
// TODO(brianwilkerson) Add other kinds of edits, such as adding an assert.
330+
List<EditDetail> edits = [];
331+
SingleNullabilityFix fix = fixInfo.fix;
332+
if (fix.description.kind == NullabilityFixKind.makeTypeNullable) {
333+
Location location = fix.location;
334+
edits.add(EditDetail(
335+
'Force type to be non-nullable.', location.offset, 0, '/*!*/'));
336+
}
337+
return edits;
338+
}
339+
327340
/// Return the navigation sources for the unit associated with the [result].
328341
List<NavigationSource> _computeNavigationSources(ResolvedUnitResult result) {
329342
NavigationCollectorImpl collector = new NavigationCollectorImpl();
@@ -443,12 +456,15 @@ class InfoBuilder {
443456
if (fixInfo != null) {
444457
String explanation = '${fixInfo.fix.description.appliedMessage}.';
445458
List<RegionDetail> details = _computeDetails(fixInfo);
459+
List<EditDetail> edits = _computeEdits(fixInfo);
446460
if (length > 0) {
447-
regions.add(RegionInfo(RegionType.fix, mapper.map(offset), length,
448-
explanation, details));
461+
regions.add(RegionInfo(
462+
RegionType.fix, mapper.map(offset), length, explanation, details,
463+
edits: edits));
449464
}
450465
regions.add(RegionInfo(RegionType.fix, mapper.map(end),
451-
replacement.length, explanation, details));
466+
replacement.length, explanation, details,
467+
edits: edits));
452468
}
453469
}
454470
if (explainNonNullableTypes) {

pkg/analysis_server/lib/src/edit/nnbd_migration/instrumentation_renderer.dart

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,22 @@ class InstrumentationRenderer {
401401
// Write out any edits.
402402
//
403403
if (supportsIncrementalWorkflow && region.edits.isNotEmpty) {
404-
// TODO(brianwilkerson) Implement this.
404+
for (EditDetail edit in region.edits) {
405+
int offset = edit.offset;
406+
String targetUri = Uri(
407+
scheme: 'http',
408+
path: pathContext.basename(unitInfo.path),
409+
queryParameters: {
410+
'offset': offset.toString(),
411+
'end': (offset + edit.length).toString(),
412+
'replacement': edit.replacement
413+
}).toString();
414+
regions.write('<p>');
415+
regions.write('<a href="$targetUri">');
416+
regions.write(edit.description);
417+
regions.write('</a>');
418+
regions.write('</p>');
419+
}
405420
}
406421
regions.write('</span></span>');
407422
}

pkg/analysis_server/lib/src/edit/nnbd_migration/migration_info.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class RegionInfo {
116116
/// Initialize a newly created region.
117117
RegionInfo(
118118
this.regionType, this.offset, this.length, this.explanation, this.details,
119-
{this.edits});
119+
{this.edits = const []});
120120
}
121121

122122
/// Different types of regions that are called out.

0 commit comments

Comments
 (0)