Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(298)

Unified Diff: pkg/analysis_server/lib/src/utilities/change_builder_core.dart

Issue 2790173003: Bug fixes and added support for creating source edits (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_server/lib/src/utilities/change_builder_core.dart
diff --git a/pkg/analysis_server/lib/src/utilities/change_builder_core.dart b/pkg/analysis_server/lib/src/utilities/change_builder_core.dart
index 919388ea7a50aebe798523d8263a84a896f4814b..c999300055d66e3977f6b7736165cf83cc10717a 100644
--- a/pkg/analysis_server/lib/src/utilities/change_builder_core.dart
+++ b/pkg/analysis_server/lib/src/utilities/change_builder_core.dart
@@ -46,11 +46,9 @@ class ChangeBuilderImpl implements ChangeBuilder {
Future<Null> addFileEdit(String path, int fileStamp,
void buildFileEdit(FileEditBuilder builder)) async {
FileEditBuilderImpl builder = await createFileEditBuilder(path, fileStamp);
- try {
- buildFileEdit(builder);
- } finally {
- _change.addFileEdit(builder.fileEdit);
- }
+ buildFileEdit(builder);
+ _change.addFileEdit(builder.fileEdit);
+ builder.finalize();
}
/**
@@ -199,7 +197,8 @@ class FileEditBuilderImpl implements FileEditBuilder {
@override
void addLinkedPosition(int offset, int length, String groupName) {
LinkedEditGroup group = changeBuilder.getLinkedEditGroup(groupName);
- Position position = new Position(fileEdit.file, offset);
+ Position position =
+ new Position(fileEdit.file, offset + _deltaToOffset(offset));
group.addPosition(position, length);
}
@@ -237,6 +236,29 @@ class FileEditBuilderImpl implements FileEditBuilder {
EditBuilderImpl createEditBuilder(int offset, int length) {
return new EditBuilderImpl(this, offset, length);
}
+
+ /**
+ * Finalize the source file edit that is being built.
+ */
+ void finalize() {
+ // Nothing to do.
+ }
+
+ /**
+ * Return the current delta caused by edits that will be applied before the
+ * given [offset]. In other words, if all of the edits that have so far been
+ * added were to be applied, then the text at the given `offset` before the
+ * edits will be at `offset + deltaToOffset(offset)` after the edits.
+ */
+ int _deltaToOffset(int targetOffset) {
+ int offset = 0;
+ for (var edit in fileEdit.edits) {
+ if (edit.offset <= targetOffset) {
+ offset += edit.replacement.length - edit.length;
+ }
+ }
+ return offset;
+ }
}
/**

Powered by Google App Engine
This is Rietveld 408576698