| Index: pkg/analysis_server/lib/src/services/refactoring/extract_local.dart
|
| diff --git a/pkg/analysis_server/lib/src/services/refactoring/extract_local.dart b/pkg/analysis_server/lib/src/services/refactoring/extract_local.dart
|
| index 48390a20820b046944bb23a4cfc3b949151dc9c0..0648eb3bcdc47332a7e5b5ab36f561a995ab92ca 100644
|
| --- a/pkg/analysis_server/lib/src/services/refactoring/extract_local.dart
|
| +++ b/pkg/analysis_server/lib/src/services/refactoring/extract_local.dart
|
| @@ -140,22 +140,7 @@ class ExtractLocalRefactoringImpl extends RefactoringImpl implements
|
| }
|
| String eol = utils.endOfLine;
|
| // prepare location for declaration
|
| - AstNode target_;
|
| - {
|
| - List<AstNode> nodes = _findNodes(occurrences);
|
| - AstNode commonParent = getNearestCommonAncestor(nodes);
|
| - if (commonParent is Block) {
|
| - List<AstNode> firstParents = getParents(nodes[0]);
|
| - int commonIndex = firstParents.indexOf(commonParent);
|
| - target_ = firstParents[commonIndex + 1];
|
| - } else {
|
| - target_ = _getEnclosingExpressionBody(commonParent);
|
| - if (target_ == null) {
|
| - target_ = commonParent.getAncestor((node) => node is Statement);
|
| - }
|
| - }
|
| - }
|
| - AstNode target = target_;
|
| + AstNode target = _findDeclarationTarget(occurrences);
|
| // insert variable declaration
|
| if (target is Statement) {
|
| String prefix = utils.getNodePrefix(target);
|
| @@ -314,6 +299,32 @@ class ExtractLocalRefactoringImpl extends RefactoringImpl implements
|
| }
|
|
|
| /**
|
| + * Return the [AstNode] to defined the variable before.
|
| + * It should be accessible by all the given [occurrences].
|
| + */
|
| + AstNode _findDeclarationTarget(List<SourceRange> occurrences) {
|
| + List<AstNode> nodes = _findNodes(occurrences);
|
| + AstNode commonParent = getNearestCommonAncestor(nodes);
|
| + // Block
|
| + if (commonParent is Block) {
|
| + List<AstNode> firstParents = getParents(nodes[0]);
|
| + int commonIndex = firstParents.indexOf(commonParent);
|
| + return firstParents[commonIndex + 1];
|
| + }
|
| + // ExpressionFunctionBody
|
| + AstNode expressionBody = _getEnclosingExpressionBody(commonParent);
|
| + if (expressionBody != null) {
|
| + return expressionBody;
|
| + }
|
| + // single Statement
|
| + AstNode target = commonParent.getAncestor((node) => node is Statement);
|
| + while (target.parent is! Block) {
|
| + target = target.parent;
|
| + }
|
| + return target;
|
| + }
|
| +
|
| + /**
|
| * Returns [AstNode]s at the offsets of the given [SourceRange]s.
|
| */
|
| List<AstNode> _findNodes(List<SourceRange> ranges) {
|
|
|