Update AssistProcessor to use ChangeBuilder

R=scheglov@google.com

Review-Url: https://codereview.chromium.org/2928313002 .
This commit is contained in:
Brian Wilkerson 2017-06-12 07:28:01 -07:00
parent 44854543ea
commit d824acbf71
3 changed files with 1113 additions and 1016 deletions

View file

@ -229,8 +229,9 @@ class FileEditBuilderImpl implements FileEditBuilder {
try {
buildEdit(builder);
} finally {
fileEdit.add(builder.sourceEdit);
_captureSelection(builder);
SourceEdit edit = builder.sourceEdit;
fileEdit.add(edit);
_captureSelection(builder, edit);
}
}
@ -248,8 +249,9 @@ class FileEditBuilderImpl implements FileEditBuilder {
try {
buildEdit(builder);
} finally {
fileEdit.add(builder.sourceEdit);
_captureSelection(builder);
SourceEdit edit = builder.sourceEdit;
fileEdit.add(edit);
_captureSelection(builder, edit);
}
}
@ -259,8 +261,9 @@ class FileEditBuilderImpl implements FileEditBuilder {
try {
builder.write(text);
} finally {
fileEdit.add(builder.sourceEdit);
_captureSelection(builder);
SourceEdit edit = builder.sourceEdit;
fileEdit.add(edit);
_captureSelection(builder, edit);
}
}
@ -270,8 +273,9 @@ class FileEditBuilderImpl implements FileEditBuilder {
try {
builder.write(text);
} finally {
fileEdit.add(builder.sourceEdit);
_captureSelection(builder);
SourceEdit edit = builder.sourceEdit;
fileEdit.add(edit);
_captureSelection(builder, edit);
}
}
@ -289,29 +293,46 @@ class FileEditBuilderImpl implements FileEditBuilder {
/**
* Capture the selection offset if one was set.
*/
void _captureSelection(EditBuilderImpl builder) {
void _captureSelection(EditBuilderImpl builder, SourceEdit edit) {
int offset = builder._selectionOffset;
if (offset >= 0) {
Position position =
new Position(fileEdit.file, offset + _deltaToOffset(offset));
new Position(fileEdit.file, offset + _deltaToEdit(edit));
changeBuilder.setSelection(position);
}
}
/**
* 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.
* [targetEdit]. In other words, if all of the edits that occur before the
* target edit were to be applied, then the text at the offset of the target
* edit before the applied 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;
int _deltaToEdit(SourceEdit targetEdit) {
int delta = 0;
for (SourceEdit edit in fileEdit.edits) {
if (edit.offset < targetEdit.offset) {
delta += edit.replacement.length - edit.length;
}
}
return offset;
return delta;
}
/**
* 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
* applied edits will be at `offset + _deltaToOffset(offset)` after the edits.
*/
int _deltaToOffset(int offset) {
int delta = 0;
for (SourceEdit edit in fileEdit.edits) {
if (edit.offset <= offset) {
delta += edit.replacement.length - edit.length;
}
}
return delta;
}
}

View file

@ -70,8 +70,13 @@ abstract class EditBuilder {
/**
* Set the selection to the given location within the edit being built.
*
* This method only works correctly if all of the edits that will applied to
* text before the current edit have already been created. Those edits are
* needed in order to convert the current offset (as of the time this method
* is invoked) into an offset relative to the text resulting from applying all
* of the edits.
*/
@experimental
void selectHere();
/**
@ -115,6 +120,11 @@ abstract class FileEditBuilder {
* source. This is typically used to include pre-existing regions of text in a
* group. If the region to be included is part of newly generated text, then
* the method [EditBuilder.addLinkedEdit] should be used instead.
*
* This method only works correctly if all of the edits that will applied to
* text before the given range have already been created. Those edits are
* needed in order to convert the range into a range relative to the text
* resulting from applying all of the edits.
*/
void addLinkedPosition(SourceRange range, String groupName);