Fix for surround-with linked edit positions.

R=brianwilkerson@google.com

Bug: https://github.com/dart-lang/sdk/issues/32106
Change-Id: Ia899171aa38dd1283b72cdd100a0f5b678e9b565
Reviewed-on: https://dart-review.googlesource.com/41420
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2018-02-14 18:55:33 +00:00 committed by commit-bot@chromium.org
parent c01d59bc7e
commit 4232ac99e8
3 changed files with 37 additions and 9 deletions

View file

@ -62,8 +62,6 @@ class AssistProcessor {
final List<Assist> assists = <Assist>[];
Position exitPosition = null;
CorrectionUtils utils;
AstNode node;
@ -2575,7 +2573,6 @@ class AssistProcessor {
}
// prepare statement information
Statement firstStatement = selectedStatements[0];
Statement lastStatement = selectedStatements[selectedStatements.length - 1];
SourceRange statementsRange =
utils.getLinesRangeStatements(selectedStatements);
// prepare environment
@ -2593,7 +2590,6 @@ class AssistProcessor {
utils.replaceSourceRangeIndent(
statementsRange, indentOld, indentNew));
builder.addSimpleInsertion(statementsRange.end, '$indentOld}$eol');
exitPosition = _newPosition(lastStatement.end);
});
_addAssistFromBuilder(changeBuilder, DartAssistKind.SURROUND_WITH_BLOCK);
}
@ -2950,10 +2946,6 @@ class AssistProcessor {
return utils.getRangeText(range);
}
Position _newPosition(int offset) {
return new Position(file, offset);
}
Future<Null> _swapFlutterWidgets(
InstanceCreationExpression exprGoingDown,
InstanceCreationExpression exprGoingUp,

View file

@ -5050,6 +5050,8 @@ main() {
// end
}
''');
_assertLinkedGroup(change.linkedEditGroups[0], ['condition);']);
_assertExitPosition('condition);');
}
test_surroundWith_for() async {
@ -5072,6 +5074,11 @@ main() {
// end
}
''');
_assertLinkedGroup(change.linkedEditGroups[0], ['v =']);
_assertLinkedGroup(change.linkedEditGroups[1], ['init;']);
_assertLinkedGroup(change.linkedEditGroups[2], ['condition;']);
_assertLinkedGroup(change.linkedEditGroups[3], ['increment']);
_assertExitPosition(' }');
}
test_surroundWith_forIn() async {
@ -5094,6 +5101,9 @@ main() {
// end
}
''');
_assertLinkedGroup(change.linkedEditGroups[0], ['item']);
_assertLinkedGroup(change.linkedEditGroups[1], ['iterable']);
_assertExitPosition(' }');
}
test_surroundWith_if() async {
@ -5116,6 +5126,8 @@ main() {
// end
}
''');
_assertLinkedGroup(change.linkedEditGroups[0], ['condition']);
_assertExitPosition(' }');
}
test_surroundWith_tryCatch() async {
@ -5140,6 +5152,10 @@ main() {
// end
}
''');
_assertLinkedGroup(change.linkedEditGroups[0], ['Exception']);
_assertLinkedGroup(change.linkedEditGroups[1], ['e) {']);
_assertLinkedGroup(change.linkedEditGroups[2], ['// TODO']);
_assertExitPosition('// TODO');
}
test_surroundWith_tryFinally() async {
@ -5164,6 +5180,8 @@ main() {
// end
}
''');
_assertLinkedGroup(change.linkedEditGroups[0], ['// TODO']);
_assertExitPosition('// TODO');
}
test_surroundWith_while() async {
@ -5186,6 +5204,15 @@ main() {
// end
}
''');
_assertLinkedGroup(change.linkedEditGroups[0], ['condition']);
_assertExitPosition(' }');
}
void _assertExitPosition(String after) {
Position exitPosition = change.selection;
expect(exitPosition, isNotNull);
expect(exitPosition.file, testFile);
expect(exitPosition.offset, resultCode.indexOf(after) + after.length);
}
/**

View file

@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'dart:collection';
import 'dart:math' as math;
import 'package:analyzer/src/generated/source.dart';
@ -30,6 +31,12 @@ class ChangeBuilderImpl implements ChangeBuilder {
final Map<String, LinkedEditGroup> _linkedEditGroups =
<String, LinkedEditGroup>{};
/**
* The set of [Position]s that belong to the current [EditBuilderImpl] and
* should not be updated in result of inserting this builder.
*/
final Set<Position> _lockedPositions = new HashSet<Position>.identity();
/**
* Initialize a newly created change builder.
*/
@ -86,7 +93,7 @@ class ChangeBuilderImpl implements ChangeBuilder {
*/
void _updatePositions(int offset, int delta) {
void _updatePosition(Position position) {
if (position.offset >= offset) {
if (position.offset >= offset && !_lockedPositions.contains(position)) {
position.offset = position.offset + delta;
}
}
@ -165,6 +172,7 @@ class EditBuilderImpl implements EditBuilder {
int end = offset + _buffer.length;
int length = end - start;
Position position = new Position(fileEditBuilder.fileEdit.file, start);
fileEditBuilder.changeBuilder._lockedPositions.add(position);
LinkedEditGroup group =
fileEditBuilder.changeBuilder.getLinkedEditGroup(groupName);
group.addPosition(position, length);
@ -315,6 +323,7 @@ class FileEditBuilderImpl implements FileEditBuilder {
fileEdit.add(edit);
int delta = _editDelta(edit);
changeBuilder._updatePositions(edit.offset + math.max(0, delta), delta);
changeBuilder._lockedPositions.clear();
_captureSelection(builder, edit);
}