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

Unified Diff: pkg/analysis_server/lib/src/services/refactoring/extract_local.dart

Issue 879463004: Fix for extracting local variable in if-else-if statements. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 10 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
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/refactoring/extract_local_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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) {
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/refactoring/extract_local_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698