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

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

Issue 2267893002: Fix for 'Extract Local' in an expression function body. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 4 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library services.src.refactoring.extract_local; 5 library services.src.refactoring.extract_local;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:collection'; 8 import 'dart:collection';
9 9
10 import 'package:analysis_server/src/protocol_server.dart' hide Element; 10 import 'package:analysis_server/src/protocol_server.dart' hide Element;
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 new SourceEdit(singleExpression.offset, 0, declarationSource); 132 new SourceEdit(singleExpression.offset, 0, declarationSource);
133 doSourceChange_addElementEdit(change, unitElement, edit); 133 doSourceChange_addElementEdit(change, unitElement, edit);
134 return new Future.value(change); 134 return new Future.value(change);
135 } 135 }
136 // prepare positions 136 // prepare positions
137 List<Position> positions = <Position>[]; 137 List<Position> positions = <Position>[];
138 int occurrencesShift = 0; 138 int occurrencesShift = 0;
139 void addPosition(int offset) { 139 void addPosition(int offset) {
140 positions.add(new Position(file, offset)); 140 positions.add(new Position(file, offset));
141 } 141 }
142
142 // add variable declaration 143 // add variable declaration
143 { 144 {
144 String declarationCode; 145 String declarationCode;
145 int nameOffsetInDeclarationCode; 146 int nameOffsetInDeclarationCode;
146 if (stringLiteralPart != null) { 147 if (stringLiteralPart != null) {
147 declarationCode = 'var '; 148 declarationCode = 'var ';
148 nameOffsetInDeclarationCode = declarationCode.length; 149 nameOffsetInDeclarationCode = declarationCode.length;
149 declarationCode += "$name = '$stringLiteralPart';"; 150 declarationCode += "$name = '$stringLiteralPart';";
150 } else { 151 } else {
151 String keyword = _declarationKeyword; 152 String keyword = _declarationKeyword;
(...skipping 21 matching lines...) Expand all
173 String code = '{' + eol + prefix + indent; 174 String code = '{' + eol + prefix + indent;
174 addPosition( 175 addPosition(
175 target.offset + code.length + nameOffsetInDeclarationCode); 176 target.offset + code.length + nameOffsetInDeclarationCode);
176 code += declarationCode + eol; 177 code += declarationCode + eol;
177 code += prefix + indent + 'return '; 178 code += prefix + indent + 'return ';
178 SourceEdit edit = 179 SourceEdit edit =
179 new SourceEdit(target.offset, expr.offset - target.offset, code); 180 new SourceEdit(target.offset, expr.offset - target.offset, code);
180 occurrencesShift = target.offset + code.length - expr.offset; 181 occurrencesShift = target.offset + code.length - expr.offset;
181 doSourceChange_addElementEdit(change, unitElement, edit); 182 doSourceChange_addElementEdit(change, unitElement, edit);
182 } 183 }
183 doSourceChange_addElementEdit(change, unitElement, 184 doSourceChange_addElementEdit(
184 new SourceEdit(expr.end, 0, ';' + eol + prefix + '}')); 185 change,
186 unitElement,
187 new SourceEdit(
188 expr.end, target.end - expr.end, ';' + eol + prefix + '}'));
185 } 189 }
186 } 190 }
187 // prepare replacement 191 // prepare replacement
188 String occurrenceReplacement = name; 192 String occurrenceReplacement = name;
189 if (stringLiteralPart != null) { 193 if (stringLiteralPart != null) {
190 occurrenceReplacement = "\${$name}"; 194 occurrenceReplacement = "\${$name}";
191 occurrencesShift += 2; 195 occurrencesShift += 2;
192 } 196 }
193 // replace occurrences with variable reference 197 // replace occurrences with variable reference
194 for (SourceRange range in occurrences) { 198 for (SourceRange range in occurrences) {
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 } 295 }
292 } 296 }
293 // set selected expression 297 // set selected expression
294 if (coveringExpressionOffsets.isEmpty) { 298 if (coveringExpressionOffsets.isEmpty) {
295 rootExpression = node; 299 rootExpression = node;
296 } 300 }
297 // add the expression range 301 // add the expression range
298 coveringExpressionOffsets.add(node.offset); 302 coveringExpressionOffsets.add(node.offset);
299 coveringExpressionLengths.add(node.length); 303 coveringExpressionLengths.add(node.length);
300 } 304 }
301 // we need enclosing block to add variable declaration statement 305 // We need an enclosing function.
306 // If it has a block body, we can add a new variable declaration statement
307 // into this block. If it has an expression body, we can convert it into
308 // the block body first.
302 if (coveringNode == null || 309 if (coveringNode == null ||
303 coveringNode.getAncestor((node) => node is Block) == null) { 310 coveringNode.getAncestor((node) => node is FunctionBody) == null) {
304 return new RefactoringStatus.fatal( 311 return new RefactoringStatus.fatal(
305 'Expression inside of function must be selected ' 312 'Expression inside a function must be selected '
Brian Wilkerson 2016/08/22 18:54:38 "Expression" --> "An expression"?
scheglov 2016/08/22 18:57:44 Done.
306 'to activate this refactoring.'); 313 'to activate this refactoring.');
307 } 314 }
308 // part of string literal 315 // part of string literal
309 if (coveringNode is StringLiteral) { 316 if (coveringNode is StringLiteral) {
310 if (selectionRange.length != 0 && 317 if (selectionRange.length != 0 &&
311 selectionRange.offset > coveringNode.offset && 318 selectionRange.offset > coveringNode.offset &&
312 selectionRange.end < coveringNode.end) { 319 selectionRange.end < coveringNode.end) {
313 stringLiteralPart = selectionStr; 320 stringLiteralPart = selectionStr;
314 return new RefactoringStatus(); 321 return new RefactoringStatus();
315 } 322 }
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 694
688 _TokenLocalElementVisitor(this.map); 695 _TokenLocalElementVisitor(this.map);
689 696
690 visitSimpleIdentifier(SimpleIdentifier node) { 697 visitSimpleIdentifier(SimpleIdentifier node) {
691 Element element = node.staticElement; 698 Element element = node.staticElement;
692 if (element is LocalVariableElement) { 699 if (element is LocalVariableElement) {
693 map[node.token] = element; 700 map[node.token] = element;
694 } 701 }
695 } 702 }
696 } 703 }
OLDNEW
« 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