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

Side by Side Diff: pkg/analysis_services/lib/src/correction/assist.dart

Issue 438453002: Port and test more assists. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 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 | Annotate | Revision Log
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 // This code was auto-generated, is not intended to be edited, and is subject to 5 // This code was auto-generated, is not intended to be edited, and is subject to
6 // significant change. Please see the README file for more information. 6 // significant change. Please see the README file for more information.
7 7
8 library services.src.correction.assist; 8 library services.src.correction.assist;
9 9
10 import 'package:analysis_services/correction/assist.dart'; 10 import 'package:analysis_services/correction/assist.dart';
11 import 'package:analysis_services/correction/change.dart'; 11 import 'package:analysis_services/correction/change.dart';
12 import 'package:analysis_services/search/hierarchy.dart';
12 import 'package:analysis_services/search/search_engine.dart'; 13 import 'package:analysis_services/search/search_engine.dart';
13 import 'package:analysis_services/src/correction/name_suggestion.dart'; 14 import 'package:analysis_services/src/correction/name_suggestion.dart';
14 import 'package:analysis_services/src/correction/source_buffer.dart'; 15 import 'package:analysis_services/src/correction/source_buffer.dart';
15 import 'package:analysis_services/src/correction/source_range.dart'; 16 import 'package:analysis_services/src/correction/source_range.dart';
16 import 'package:analysis_services/src/correction/util.dart'; 17 import 'package:analysis_services/src/correction/util.dart';
17 import 'package:analyzer/src/generated/ast.dart'; 18 import 'package:analyzer/src/generated/ast.dart';
18 import 'package:analyzer/src/generated/element.dart'; 19 import 'package:analyzer/src/generated/element.dart';
19 import 'package:analyzer/src/generated/java_core.dart'; 20 import 'package:analyzer/src/generated/java_core.dart';
20 import 'package:analyzer/src/generated/scanner.dart'; 21 import 'package:analyzer/src/generated/scanner.dart';
21 import 'package:analyzer/src/generated/source.dart'; 22 import 'package:analyzer/src/generated/source.dart';
(...skipping 17 matching lines...) Expand all
39 40
40 final List<Edit> edits = <Edit>[]; 41 final List<Edit> edits = <Edit>[];
41 final Map<String, LinkedPositionGroup> linkedPositionGroups = <String, 42 final Map<String, LinkedPositionGroup> linkedPositionGroups = <String,
42 LinkedPositionGroup>{}; 43 LinkedPositionGroup>{};
43 Position endPosition = null; 44 Position endPosition = null;
44 final List<Assist> assists = <Assist>[]; 45 final List<Assist> assists = <Assist>[];
45 46
46 int selectionEnd; 47 int selectionEnd;
47 CorrectionUtils utils; 48 CorrectionUtils utils;
48 AstNode node; 49 AstNode node;
49 AstNode coveredNode;
50
51 50
52 AssistProcessor(this.searchEngine, this.source, this.file, this.unit, 51 AssistProcessor(this.searchEngine, this.source, this.file, this.unit,
53 this.selectionOffset, this.selectionLength) { 52 this.selectionOffset, this.selectionLength) {
54 unitElement = unit.element; 53 unitElement = unit.element;
55 unitLibraryElement = unitElement.library; 54 unitLibraryElement = unitElement.library;
56 unitLibraryFile = unitLibraryElement.source.fullName; 55 unitLibraryFile = unitLibraryElement.source.fullName;
57 unitLibraryFolder = dirname(unitLibraryFile); 56 unitLibraryFolder = dirname(unitLibraryFile);
58 selectionEnd = selectionOffset + selectionLength; 57 selectionEnd = selectionOffset + selectionLength;
59 } 58 }
60 59
61 /** 60 /**
62 * Returns the EOL to use for this [CompilationUnit]. 61 * Returns the EOL to use for this [CompilationUnit].
63 */ 62 */
64 String get eol => utils.endOfLine; 63 String get eol => utils.endOfLine;
65 64
66 List<Assist> compute() { 65 List<Assist> compute() {
67 utils = new CorrectionUtils(unit); 66 utils = new CorrectionUtils(unit);
68 node = new NodeLocator.con1(selectionOffset).searchWithin(unit); 67 node = new NodeLocator.con2(
69 coveredNode = new NodeLocator.con2(
70 selectionOffset, 68 selectionOffset,
71 selectionEnd).searchWithin(unit); 69 selectionEnd).searchWithin(unit);
72 // try to add proposals 70 // try to add proposals
73 _addProposal_addTypeAnnotation(); 71 _addProposal_addTypeAnnotation();
74 _addProposal_assignToLocalVariable(); 72 _addProposal_assignToLocalVariable();
75 _addProposal_convertToBlockFunctionBody(); 73 _addProposal_convertToBlockFunctionBody();
76 _addProposal_convertToExpressionFunctionBody(); 74 _addProposal_convertToExpressionFunctionBody();
77 _addProposal_convertToIsNot_onIs(); 75 _addProposal_convertToIsNot_onIs();
78 _addProposal_convertToIsNot_onNot(); 76 _addProposal_convertToIsNot_onNot();
79 _addProposal_convertToIsNotEmpty(); 77 _addProposal_convertToIsNotEmpty();
80 _addProposal_exchangeOperands(); 78 _addProposal_exchangeOperands();
81 _addProposal_extractClassIntoPart(); 79 _addProposal_extractClassIntoPart();
82 _addProposal_importAddShow(); 80 _addProposal_importAddShow();
83 _addProposal_invertIf(); 81 _addProposal_invertIf();
84 _addProposal_joinIfStatementOuter(); 82 _addProposal_joinIfStatementOuter();
85 _addProposal_joinVariableDeclaration_onAssignment(); 83 _addProposal_joinVariableDeclaration_onAssignment();
86 _addProposal_joinVariableDeclaration_onDeclaration(); 84 _addProposal_joinVariableDeclaration_onDeclaration();
87 _addProposal_removeTypeAnnotation(); 85 _addProposal_removeTypeAnnotation();
88 _addProposal_replaceConditionalWithIfElse(); 86 _addProposal_replaceConditionalWithIfElse();
89 _addProposal_replaceIfElseWithConditional(); 87 _addProposal_replaceIfElseWithConditional();
90 _addProposal_splitAndCondition(); 88 _addProposal_splitAndCondition();
91 _addProposal_splitVariableDeclaration(); 89 _addProposal_splitVariableDeclaration();
92 _addProposal_surroundWith(); 90 _addProposal_surroundWith();
93 // done 91 // done
94 return assists; 92 return assists;
95 } 93 }
96 94
95 FunctionBody getEnclosingFunctionBody() {
96 {
97 FunctionExpression function =
98 node.getAncestor((node) => node is FunctionExpression);
99 if (function != null) {
100 return function.body;
101 }
102 }
103 {
104 FunctionDeclaration function =
105 node.getAncestor((node) => node is FunctionDeclaration);
106 if (function != null) {
107 return function.functionExpression.body;
108 }
109 }
110 {
111 MethodDeclaration method =
112 node.getAncestor((node) => node is MethodDeclaration);
113 if (method != null) {
114 return method.body;
115 }
116 }
117 return null;
118 }
119
97 void _addAssist(AssistKind kind, List args, {String assistFile}) { 120 void _addAssist(AssistKind kind, List args, {String assistFile}) {
98 if (assistFile == null) { 121 if (assistFile == null) {
99 assistFile = file; 122 assistFile = file;
100 } 123 }
101 FileEdit fileEdit = new FileEdit(file); 124 FileEdit fileEdit = new FileEdit(file);
102 edits.forEach((edit) => fileEdit.add(edit)); 125 edits.forEach((edit) => fileEdit.add(edit));
103 // prepare Change 126 // prepare Change
104 String message = JavaString.format(kind.message, args); 127 String message = JavaString.format(kind.message, args);
105 Change change = new Change(message); 128 Change change = new Change(message);
106 change.add(fileEdit); 129 change.add(fileEdit);
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 } 245 }
223 builder.endPosition(); 246 builder.endPosition();
224 } 247 }
225 builder.append(" = "); 248 builder.append(" = ");
226 // add proposal 249 // add proposal
227 _insertBuilder(builder); 250 _insertBuilder(builder);
228 _addAssist(AssistKind.ASSIGN_TO_LOCAL_VARIABLE, []); 251 _addAssist(AssistKind.ASSIGN_TO_LOCAL_VARIABLE, []);
229 } 252 }
230 253
231 void _addProposal_convertToBlockFunctionBody() { 254 void _addProposal_convertToBlockFunctionBody() {
232 // TODO(scheglov) implement 255 FunctionBody body = getEnclosingFunctionBody();
233 // FunctionBody body = enclosingFunctionBody; 256 // prepare expression body
234 // // prepare expression body 257 if (body is! ExpressionFunctionBody) {
235 // if (body is! ExpressionFunctionBody) { 258 _coverageMarker();
236 // return; 259 return;
237 // } 260 }
238 // Expression returnValue = (body as ExpressionFunctionBody).expression; 261 Expression returnValue = (body as ExpressionFunctionBody).expression;
239 // // prepare prefix 262 // prepare prefix
240 // String prefix; 263 String prefix;
241 // { 264 {
242 // AstNode bodyParent = body.parent; 265 AstNode bodyParent = body.parent;
Brian Wilkerson 2014/07/31 13:24:58 You know I don't like nested blocks in general, bu
scheglov 2014/07/31 16:09:03 Done.
243 // prefix = utils.getNodePrefix(bodyParent); 266 prefix = utils.getNodePrefix(bodyParent);
244 // } 267 }
245 // // add change 268 // add change
246 // String eol = utils.endOfLine; 269 String indent = utils.getIndent(1);
247 // String indent = utils.getIndent(1); 270 String returnSource = 'return ' + _getSource(returnValue);
248 // String newBodySource = 271 String newBodySource = "{$eol$prefix${indent}$returnSource;$eol$prefix}";
249 // "{${eol}${prefix}${indent}return ${_getSource(returnValue)};${eol}${pr efix}}"; 272 _addReplaceEdit(rangeNode(body), newBodySource);
250 // _addReplaceEdit(rangeNode(body), newBodySource); 273 // add proposal
251 // // add proposal 274 _addAssist(AssistKind.CONVERT_INTO_BLOCK_BODY, []);
252 // _addAssist(AssistKind.CONVERT_INTO_BLOCK_BODY, []);
253 } 275 }
254 276
255 void _addProposal_convertToExpressionFunctionBody() { 277 void _addProposal_convertToExpressionFunctionBody() {
256 // TODO(scheglov) implement 278 // prepare current body
257 // // prepare current body 279 FunctionBody body = getEnclosingFunctionBody();
258 // FunctionBody body = enclosingFunctionBody; 280 if (body is! BlockFunctionBody) {
259 // if (body is! BlockFunctionBody) { 281 _coverageMarker();
260 // return; 282 return;
261 // } 283 }
262 // // prepare return statement 284 // prepare return statement
263 // List<Statement> statements = (body as BlockFunctionBody).block.statements; 285 List<Statement> statements = (body as BlockFunctionBody).block.statements;
264 // if (statements.length != 1) { 286 if (statements.length != 1) {
265 // return; 287 _coverageMarker();
266 // } 288 return;
267 // if (statements[0] is! ReturnStatement) { 289 }
268 // return; 290 if (statements[0] is! ReturnStatement) {
269 // } 291 _coverageMarker();
270 // ReturnStatement returnStatement = statements[0] as ReturnStatement; 292 return;
271 // // prepare returned expression 293 }
272 // Expression returnExpression = returnStatement.expression; 294 ReturnStatement returnStatement = statements[0] as ReturnStatement;
273 // if (returnExpression == null) { 295 // prepare returned expression
274 // return; 296 Expression returnExpression = returnStatement.expression;
275 // } 297 if (returnExpression == null) {
276 // // add change 298 _coverageMarker();
277 // String newBodySource = "=> ${_getSource(returnExpression)}"; 299 return;
278 // if (body.parent is! FunctionExpression || 300 }
279 // body.parent.parent is FunctionDeclaration) { 301 // add change
280 // newBodySource += ";"; 302 String newBodySource = "=> ${_getSource(returnExpression)}";
281 // } 303 if (body.parent is! FunctionExpression ||
282 // _addReplaceEdit(rangeNode(body), newBodySource); 304 body.parent.parent is FunctionDeclaration) {
283 // // add proposal 305 newBodySource += ";";
284 // _addAssist( 306 }
285 // AssistKind.CONVERT_INTO_EXPRESSION_BODY, 307 _addReplaceEdit(rangeNode(body), newBodySource);
286 // []); 308 // add proposal
309 _addAssist(AssistKind.CONVERT_INTO_EXPRESSION_BODY, []);
287 } 310 }
288 311
289 /** 312 /**
290 * Converts "!isEmpty" -> "isNotEmpty" if possible. 313 * Converts "!isEmpty" -> "isNotEmpty" if possible.
291 */ 314 */
292 void _addProposal_convertToIsNotEmpty() { 315 void _addProposal_convertToIsNotEmpty() {
293 // TODO(scheglov) implement 316 // prepare "expr.isEmpty"
294 // // prepare "expr.isEmpty" 317 AstNode isEmptyAccess = null;
295 // AstNode isEmptyAccess = null; 318 SimpleIdentifier isEmptyIdentifier = null;
296 // SimpleIdentifier isEmptyIdentifier = null; 319 if (node is SimpleIdentifier) {
297 // if (node is SimpleIdentifier) { 320 SimpleIdentifier identifier = node as SimpleIdentifier;
298 // SimpleIdentifier identifier = node as SimpleIdentifier; 321 AstNode parent = identifier.parent;
299 // AstNode parent = identifier.parent; 322 // normal case (but rare)
300 // // normal case (but rare) 323 if (parent is PropertyAccess) {
301 // if (parent is PropertyAccess) { 324 isEmptyIdentifier = parent.propertyName;
302 // PropertyAccess propertyAccess = parent; 325 isEmptyAccess = parent;
303 // isEmptyIdentifier = propertyAccess.propertyName; 326 }
304 // isEmptyAccess = propertyAccess; 327 // usual case
305 // } 328 if (parent is PrefixedIdentifier) {
306 // // usual case 329 isEmptyIdentifier = parent.identifier;
307 // if (parent is PrefixedIdentifier) { 330 isEmptyAccess = parent;
308 // PrefixedIdentifier prefixedIdentifier = parent; 331 }
309 // isEmptyIdentifier = prefixedIdentifier.identifier; 332 }
310 // isEmptyAccess = prefixedIdentifier; 333 if (isEmptyIdentifier == null) {
311 // } 334 _coverageMarker();
312 // } 335 return;
313 // if (isEmptyIdentifier == null) { 336 }
314 // return; 337 // should be "isEmpty"
315 // } 338 Element propertyElement = isEmptyIdentifier.bestElement;
316 // // should be "isEmpty" 339 if (propertyElement == null || "isEmpty" != propertyElement.name) {
317 // Element propertyElement = isEmptyIdentifier.bestElement; 340 _coverageMarker();
318 // if (propertyElement == null || "isEmpty" != propertyElement.name) { 341 return;
319 // return; 342 }
320 // } 343 // should have "isNotEmpty"
321 // // should have "isNotEmpty" 344 Element propertyTarget = propertyElement.enclosingElement;
322 // Element propertyTarget = propertyElement.enclosingElement; 345 if (propertyTarget == null ||
323 // if (propertyTarget == null || 346 getChildren(propertyTarget, "isNotEmpty").isEmpty) {
324 // CorrectionUtils.getChildren2(propertyTarget, "isNotEmpty").isEmpty) { 347 _coverageMarker();
325 // return; 348 return;
326 // } 349 }
327 // // should be in PrefixExpression 350 // should be in PrefixExpression
328 // if (isEmptyAccess.parent is! PrefixExpression) { 351 if (isEmptyAccess.parent is! PrefixExpression) {
329 // return; 352 _coverageMarker();
330 // } 353 return;
331 // PrefixExpression prefixExpression = 354 }
332 // isEmptyAccess.parent as PrefixExpression; 355 PrefixExpression prefixExpression =
333 // // should be ! 356 isEmptyAccess.parent as PrefixExpression;
334 // if (prefixExpression.operator.type != TokenType.BANG) { 357 // should be !
335 // return; 358 if (prefixExpression.operator.type != TokenType.BANG) {
336 // } 359 return;
337 // // do replace 360 }
338 // _addRemoveEdit( 361 // do replace
339 // rangeStartStart(prefixExpression, prefixExpression.operand)); 362 _addRemoveEdit(rangeStartStart(prefixExpression, prefixExpression.operand));
340 // _addReplaceEdit( 363 _addReplaceEdit(rangeNode(isEmptyIdentifier), "isNotEmpty");
341 // rangeNode(isEmptyIdentifier), 364 // add proposal
342 // "isNotEmpty"); 365 _addAssist(AssistKind.CONVERT_INTO_IS_NOT_EMPTY, []);
343 // // add proposal
344 // _addAssist(AssistKind.CONVERT_INTO_IS_NOT_EMPTY, []);
345 } 366 }
346 367
347 void _addProposal_convertToIsNot_onIs() { 368 void _addProposal_convertToIsNot_onIs() {
348 // TODO(scheglov) implement 369 // may be child of "is"
349 // // may be child of "is" 370 AstNode node = this.node;
350 // AstNode node = this.node; 371 while (node != null && node is! IsExpression) {
351 // while (node != null && node is! IsExpression) { 372 node = node.parent;
352 // node = node.parent; 373 }
353 // } 374 // prepare "is"
354 // // prepare "is" 375 if (node is! IsExpression) {
355 // if (node is! IsExpression) { 376 _coverageMarker();
356 // return; 377 return;
357 // } 378 }
358 // IsExpression isExpression = node as IsExpression; 379 IsExpression isExpression = node as IsExpression;
359 // if (isExpression.notOperator != null) { 380 if (isExpression.notOperator != null) {
360 // return; 381 _coverageMarker();
361 // } 382 return;
362 // // prepare enclosing () 383 }
363 // AstNode parent = isExpression.parent; 384 // prepare enclosing ()
364 // if (parent is! ParenthesizedExpression) { 385 AstNode parent = isExpression.parent;
365 // return; 386 if (parent is! ParenthesizedExpression) {
366 // } 387 _coverageMarker();
367 // ParenthesizedExpression parExpression = parent as ParenthesizedExpression; 388 return;
368 // // prepare enclosing !() 389 }
369 // AstNode parent2 = parent.parent; 390 ParenthesizedExpression parExpression = parent as ParenthesizedExpression;
370 // if (parent2 is! PrefixExpression) { 391 // prepare enclosing !()
371 // return; 392 AstNode parent2 = parent.parent;
372 // } 393 if (parent2 is! PrefixExpression) {
373 // PrefixExpression prefExpression = parent2 as PrefixExpression; 394 _coverageMarker();
374 // if (prefExpression.operator.type != TokenType.BANG) { 395 return;
375 // return; 396 }
376 // } 397 PrefixExpression prefExpression = parent2 as PrefixExpression;
377 // // strip !() 398 if (prefExpression.operator.type != TokenType.BANG) {
378 // if (CorrectionUtils.getParentPrecedence(prefExpression) >= 399 _coverageMarker();
379 // TokenType.IS.precedence) { 400 return;
380 // _addRemoveEdit(rangeToken(prefExpression.operator)); 401 }
381 // } else { 402 // strip !()
382 // _addRemoveEdit( 403 if (getExpressionParentPrecedence(prefExpression) >=
383 // rangeStartEnd( 404 TokenType.IS.precedence) {
384 // prefExpression, 405 _addRemoveEdit(rangeToken(prefExpression.operator));
385 // parExpression.leftParenthesis)); 406 } else {
386 // _addRemoveEdit( 407 _addRemoveEdit(
387 // rangeStartEnd( 408 rangeStartEnd(prefExpression, parExpression.leftParenthesis));
388 // parExpression.rightParenthesis, 409 _addRemoveEdit(
389 // prefExpression)); 410 rangeStartEnd(parExpression.rightParenthesis, prefExpression));
390 // } 411 }
391 // _addInsertEdit(isExpression.isOperator.end, "!"); 412 _addInsertEdit(isExpression.isOperator.end, "!");
392 // // add proposal 413 // add proposal
393 // _addAssist(AssistKind.CONVERT_INTO_IS_NOT, []); 414 _addAssist(AssistKind.CONVERT_INTO_IS_NOT, []);
394 } 415 }
395 416
396 void _addProposal_convertToIsNot_onNot() { 417 void _addProposal_convertToIsNot_onNot() {
397 // TODO(scheglov) implement 418 // may be () in prefix expression
398 // // may be () in prefix expression 419 if (node is ParenthesizedExpression && node.parent is PrefixExpression) {
399 // if (node is ParenthesizedExpression && node.parent is PrefixExpression) { 420 node = node.parent;
400 // node = node.parent; 421 }
401 // } 422 // prepare !()
402 // // prepare !() 423 if (node is! PrefixExpression) {
403 // if (node is! PrefixExpression) { 424 _coverageMarker();
404 // return; 425 return;
405 // } 426 }
406 // PrefixExpression prefExpression = node as PrefixExpression; 427 PrefixExpression prefExpression = node as PrefixExpression;
407 // // should be ! operator 428 // should be ! operator
408 // if (prefExpression.operator.type != TokenType.BANG) { 429 if (prefExpression.operator.type != TokenType.BANG) {
409 // return; 430 _coverageMarker();
410 // } 431 return;
411 // // prepare !() 432 }
412 // Expression operand = prefExpression.operand; 433 // prepare !()
413 // if (operand is! ParenthesizedExpression) { 434 Expression operand = prefExpression.operand;
414 // return; 435 if (operand is! ParenthesizedExpression) {
415 // } 436 _coverageMarker();
416 // ParenthesizedExpression parExpression = operand as ParenthesizedExpression ; 437 return;
417 // operand = parExpression.expression; 438 }
418 // // prepare "is" 439 ParenthesizedExpression parExpression = operand as ParenthesizedExpression;
419 // if (operand is! IsExpression) { 440 operand = parExpression.expression;
420 // return; 441 // prepare "is"
421 // } 442 if (operand is! IsExpression) {
422 // IsExpression isExpression = operand as IsExpression; 443 _coverageMarker();
423 // if (isExpression.notOperator != null) { 444 return;
424 // return; 445 }
425 // } 446 IsExpression isExpression = operand as IsExpression;
426 // // strip !() 447 if (isExpression.notOperator != null) {
427 // if (getExpressionParentPrecedence(prefExpression) >= 448 _coverageMarker();
428 // TokenType.IS.precedence) { 449 return;
429 // _addRemoveEdit(rangeToken(prefExpression.operator)); 450 }
430 // } else { 451 // strip !()
431 // _addRemoveEdit( 452 if (getExpressionParentPrecedence(prefExpression) >=
432 // rangeStartEnd( 453 TokenType.IS.precedence) {
433 // prefExpression, 454 _addRemoveEdit(rangeToken(prefExpression.operator));
434 // parExpression.leftParenthesis)); 455 } else {
435 // _addRemoveEdit( 456 _addRemoveEdit(
436 // rangeStartEnd( 457 rangeStartEnd(prefExpression, parExpression.leftParenthesis));
437 // parExpression.rightParenthesis, 458 _addRemoveEdit(
438 // prefExpression)); 459 rangeStartEnd(parExpression.rightParenthesis, prefExpression));
439 // } 460 }
440 // _addInsertEdit(isExpression.isOperator.end, "!"); 461 _addInsertEdit(isExpression.isOperator.end, "!");
441 // // add proposal 462 // add proposal
442 // _addAssist(AssistKind.CONVERT_INTO_IS_NOT, []); 463 _addAssist(AssistKind.CONVERT_INTO_IS_NOT, []);
443 } 464 }
444 465
445 void _addProposal_exchangeOperands() { 466 void _addProposal_exchangeOperands() {
446 // TODO(scheglov) implement 467 // check that user invokes quick assist on binary expression
447 // // check that user invokes quick assist on binary expression 468 if (node is! BinaryExpression) {
448 // if (node is! BinaryExpression) { 469 _coverageMarker();
449 // return; 470 return;
450 // } 471 }
451 // BinaryExpression binaryExpression = node as BinaryExpression; 472 BinaryExpression binaryExpression = node as BinaryExpression;
452 // // prepare operator position 473 // prepare operator position
453 // int offset = 474 if (!_isOperatorSelected(
454 // _isOperatorSelected(binaryExpression, _selectionOffset, _selectionLeng th); 475 binaryExpression,
455 // if (offset == -1) { 476 selectionOffset,
456 // return; 477 selectionLength)) {
457 // } 478 _coverageMarker();
458 // // add edits 479 return;
459 // { 480 }
460 // Expression leftOperand = binaryExpression.leftOperand; 481 // add edits
461 // Expression rightOperand = binaryExpression.rightOperand; 482 {
462 // // find "wide" enclosing binary expression with same operator 483 Expression leftOperand = binaryExpression.leftOperand;
463 // while (binaryExpression.parent is BinaryExpression) { 484 Expression rightOperand = binaryExpression.rightOperand;
464 // BinaryExpression newBinaryExpression = 485 // find "wide" enclosing binary expression with same operator
465 // binaryExpression.parent as BinaryExpression; 486 while (binaryExpression.parent is BinaryExpression) {
466 // if (newBinaryExpression.operator.type != 487 BinaryExpression newBinaryExpression =
467 // binaryExpression.operator.type) { 488 binaryExpression.parent as BinaryExpression;
468 // break; 489 if (newBinaryExpression.operator.type !=
469 // } 490 binaryExpression.operator.type) {
470 // binaryExpression = newBinaryExpression; 491 _coverageMarker();
471 // } 492 break;
472 // // exchange parts of "wide" expression parts 493 }
473 // SourceRange leftRange = 494 binaryExpression = newBinaryExpression;
474 // rangeStartEnd(binaryExpression, leftOperand); 495 }
475 // SourceRange rightRange = 496 // exchange parts of "wide" expression parts
476 // rangeStartEnd(rightOperand, binaryExpression); 497 SourceRange leftRange = rangeStartEnd(binaryExpression, leftOperand);
477 // _addReplaceEdit(leftRange, _getSource2(rightRange)); 498 SourceRange rightRange = rangeStartEnd(rightOperand, binaryExpression);
478 // _addReplaceEdit(rightRange, _getSource2(leftRange)); 499 _addReplaceEdit(leftRange, _getSource2(rightRange));
479 // } 500 _addReplaceEdit(rightRange, _getSource2(leftRange));
480 // // add proposal 501 }
481 // _addAssist(AssistKind.EXCHANGE_OPERANDS, []); 502 // add proposal
503 _addAssist(AssistKind.EXCHANGE_OPERANDS, []);
482 } 504 }
483 505
484 void _addProposal_extractClassIntoPart() { 506 void _addProposal_extractClassIntoPart() {
485 // TODO(scheglov) implement 507 // TODO(scheglov) implement
486 // // should be on the name 508 // // should be on the name
487 // if (node is! SimpleIdentifier) { 509 // if (node is! SimpleIdentifier) {
488 // return; 510 // return;
489 // } 511 // }
490 // if (node.parent is! ClassDeclaration) { 512 // if (node.parent is! ClassDeclaration) {
491 // return; 513 // return;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 // if (importDirective == null) { 556 // if (importDirective == null) {
535 // return; 557 // return;
536 // } 558 // }
537 // // there should be no existing combinators 559 // // there should be no existing combinators
538 // if (!importDirective.combinators.isEmpty) { 560 // if (!importDirective.combinators.isEmpty) {
539 // return; 561 // return;
540 // } 562 // }
541 // // prepare whole import namespace 563 // // prepare whole import namespace
542 // ImportElement importElement = importDirective.element; 564 // ImportElement importElement = importDirective.element;
543 // Map<String, Element> namespace = 565 // Map<String, Element> namespace =
544 // CorrectionUtils.getImportNamespace(importElement); 566 // getImportNamespace(importElement);
545 // // prepare names of referenced elements (from this import) 567 // // prepare names of referenced elements (from this import)
546 // Set<String> referencedNames = new Set(); 568 // Set<String> referencedNames = new Set();
547 // // TODO(scheglov) 569 // for (Element element in namespace.values) {
548 //// SearchEngine searchEngine = _assistContext.searchEngine; 570 // List<SearchMatch> references =
549 //// for (Element element in namespace.values) { 571 // searchEngine.searchReferences(element, null, null);
550 //// List<SearchMatch> references = 572 // for (SearchMatch match in references) {
551 //// searchEngine.searchReferences(element, null, null); 573 // LibraryElement library = match.element.library;
552 //// for (SearchMatch match in references) { 574 // if (unitLibraryElement == library) {
553 //// LibraryElement library = match.element.library; 575 // referencedNames.add(element.displayName);
554 //// if (_unitLibraryElement == library) { 576 // break;
555 //// referencedNames.add(element.displayName); 577 // }
556 //// break; 578 // }
557 //// } 579 // }
558 //// }
559 //// }
560 // // ignore if unused 580 // // ignore if unused
561 // if (referencedNames.isEmpty) { 581 // if (referencedNames.isEmpty) {
562 // return; 582 // return;
563 // } 583 // }
564 // // prepare change 584 // // prepare change
565 // String sb = " show ${StringUtils.join(referencedNames, ", ")}"; 585 // String sb = " show ${StringUtils.join(referencedNames, ", ")}";
566 // _addInsertEdit(importDirective.end - 1, sb.toString()); 586 // _addInsertEdit(importDirective.end - 1, sb.toString());
567 // // add proposal 587 // // add proposal
568 // _addAssist(AssistKind.IMPORT_ADD_SHOW, []); 588 // _addAssist(AssistKind.IMPORT_ADD_SHOW, []);
569 } 589 }
(...skipping 14 matching lines...) Expand all
584 // // prepare source 604 // // prepare source
585 // String invertedCondition = utils.invertCondition(condition); 605 // String invertedCondition = utils.invertCondition(condition);
586 // String thenSource = _getSource(thenStatement); 606 // String thenSource = _getSource(thenStatement);
587 // String elseSource = _getSource(elseStatement); 607 // String elseSource = _getSource(elseStatement);
588 // // do replacements 608 // // do replacements
589 // _addReplaceEdit(rangeNode(condition), invertedCondition); 609 // _addReplaceEdit(rangeNode(condition), invertedCondition);
590 // _addReplaceEdit(rangeNode(thenStatement), elseSource); 610 // _addReplaceEdit(rangeNode(thenStatement), elseSource);
591 // _addReplaceEdit(rangeNode(elseStatement), thenSource); 611 // _addReplaceEdit(rangeNode(elseStatement), thenSource);
592 // // add proposal 612 // // add proposal
593 // _addAssist(AssistKind.INVERT_IF_STATEMENT, []); 613 // _addAssist(AssistKind.INVERT_IF_STATEMENT, []);
594 // } 614 }
595 // 615
596 // void _addProposal_joinIfStatementInner() { 616 void _addProposal_joinIfStatementInner() {
617 // TODO(scheglov) implement
597 // // climb up condition to the (supposedly) "if" statement 618 // // climb up condition to the (supposedly) "if" statement
598 // AstNode node = this.node; 619 // AstNode node = this.node;
599 // while (node is Expression) { 620 // while (node is Expression) {
600 // node = node.parent; 621 // node = node.parent;
601 // } 622 // }
602 // // prepare target "if" statement 623 // // prepare target "if" statement
603 // if (node is! IfStatement) { 624 // if (node is! IfStatement) {
604 // return; 625 // return;
605 // } 626 // }
606 // IfStatement targetIfStatement = node as IfStatement; 627 // IfStatement targetIfStatement = node as IfStatement;
607 // if (targetIfStatement.elseStatement != null) { 628 // if (targetIfStatement.elseStatement != null) {
608 // return; 629 // return;
609 // } 630 // }
610 // // prepare inner "if" statement 631 // // prepare inner "if" statement
611 // Statement targetThenStatement = targetIfStatement.thenStatement; 632 // Statement targetThenStatement = targetIfStatement.thenStatement;
612 // Statement innerStatement = 633 // Statement innerStatement =
613 // CorrectionUtils.getSingleStatement(targetThenStatement); 634 // CorrectionUtils.getSingleStatement(targetThenStatement);
614 // if (innerStatement is! IfStatement) { 635 // if (innerStatement is! IfStatement) {
615 // return; 636 // return;
616 // } 637 // }
617 // IfStatement innerIfStatement = innerStatement as IfStatement; 638 // IfStatement innerIfStatement = innerStatement as IfStatement;
618 // if (innerIfStatement.elseStatement != null) { 639 // if (innerIfStatement.elseStatement != null) {
619 // return; 640 // return;
620 // } 641 // }
621 // // prepare environment 642 // // prepare environment
622 // String prefix = utils.getNodePrefix(targetIfStatement); 643 // String prefix = utils.getNodePrefix(targetIfStatement);
623 // String eol = utils.endOfLine;
624 // // merge conditions 644 // // merge conditions
625 // String condition; 645 // String condition;
626 // { 646 // {
627 // Expression targetCondition = targetIfStatement.condition; 647 // Expression targetCondition = targetIfStatement.condition;
628 // Expression innerCondition = innerIfStatement.condition; 648 // Expression innerCondition = innerIfStatement.condition;
629 // String targetConditionSource = _getSource(targetCondition); 649 // String targetConditionSource = _getSource(targetCondition);
630 // String innerConditionSource = _getSource(innerCondition); 650 // String innerConditionSource = _getSource(innerCondition);
631 // if (_shouldWrapParenthesisBeforeAnd(targetCondition)) { 651 // if (_shouldWrapParenthesisBeforeAnd(targetCondition)) {
632 // targetConditionSource = "(${targetConditionSource})"; 652 // targetConditionSource = "(${targetConditionSource})";
633 // } 653 // }
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
677 // } 697 // }
678 // if (parent is! IfStatement) { 698 // if (parent is! IfStatement) {
679 // return; 699 // return;
680 // } 700 // }
681 // IfStatement outerIfStatement = parent as IfStatement; 701 // IfStatement outerIfStatement = parent as IfStatement;
682 // if (outerIfStatement.elseStatement != null) { 702 // if (outerIfStatement.elseStatement != null) {
683 // return; 703 // return;
684 // } 704 // }
685 // // prepare environment 705 // // prepare environment
686 // String prefix = utils.getNodePrefix(outerIfStatement); 706 // String prefix = utils.getNodePrefix(outerIfStatement);
687 // String eol = utils.endOfLine;
688 // // merge conditions 707 // // merge conditions
689 // String condition; 708 // String condition;
690 // { 709 // {
691 // Expression targetCondition = targetIfStatement.condition; 710 // Expression targetCondition = targetIfStatement.condition;
692 // Expression outerCondition = outerIfStatement.condition; 711 // Expression outerCondition = outerIfStatement.condition;
693 // String targetConditionSource = _getSource(targetCondition); 712 // String targetConditionSource = _getSource(targetCondition);
694 // String outerConditionSource = _getSource(outerCondition); 713 // String outerConditionSource = _getSource(outerCondition);
695 // if (_shouldWrapParenthesisBeforeAnd(targetCondition)) { 714 // if (_shouldWrapParenthesisBeforeAnd(targetCondition)) {
696 // targetConditionSource = "(${targetConditionSource})"; 715 // targetConditionSource = "(${targetConditionSource})";
697 // } 716 // }
(...skipping 15 matching lines...) Expand all
713 //// rangeNode(outerIfStatement), 732 //// rangeNode(outerIfStatement),
714 //// MessageFormat.format( 733 //// MessageFormat.format(
715 //// "if ({0}) '{'{1}{2}{3}'}'", 734 //// "if ({0}) '{'{1}{2}{3}'}'",
716 //// [condition, eol, newSource, prefix])); 735 //// [condition, eol, newSource, prefix]));
717 // } 736 // }
718 // // done 737 // // done
719 // _addAssist(AssistKind.JOIN_IF_WITH_OUTER, []); 738 // _addAssist(AssistKind.JOIN_IF_WITH_OUTER, []);
720 } 739 }
721 740
722 void _addProposal_joinVariableDeclaration_onAssignment() { 741 void _addProposal_joinVariableDeclaration_onAssignment() {
723 // TODO(scheglov) implement 742 // check that node is LHS in assignment
724 // // check that node is LHS in assignment 743 if (node is SimpleIdentifier &&
725 // if (node is SimpleIdentifier && 744 node.parent is AssignmentExpression &&
726 // node.parent is AssignmentExpression && 745 identical((node.parent as AssignmentExpression).leftHandSide, node) &&
727 // identical((node.parent as AssignmentExpression).leftHandSide, node) && 746 node.parent.parent is ExpressionStatement) {
728 // node.parent.parent is ExpressionStatement) { 747 } else {
729 // } else { 748 _coverageMarker();
730 // return; 749 return;
731 // } 750 }
732 // AssignmentExpression assignExpression = 751 AssignmentExpression assignExpression = node.parent as AssignmentExpression;
733 // node.parent as AssignmentExpression; 752 // check that binary expression is assignment
734 // // check that binary expression is assignment 753 if (assignExpression.operator.type != TokenType.EQ) {
735 // if (assignExpression.operator.type != TokenType.EQ) { 754 _coverageMarker();
736 // return; 755 return;
737 // } 756 }
738 // // prepare "declaration" statement 757 // prepare "declaration" statement
739 // Element element = (node as SimpleIdentifier).staticElement; 758 Element element = (node as SimpleIdentifier).staticElement;
740 // if (element == null) { 759 if (element == null) {
741 // return; 760 _coverageMarker();
742 // } 761 return;
743 // int declOffset = element.nameOffset; 762 }
744 // AstNode declNode = new NodeLocator.con1(declOffset).searchWithin(_unit); 763 int declOffset = element.nameOffset;
745 // if (declNode != null && 764 AstNode declNode = new NodeLocator.con1(declOffset).searchWithin(unit);
746 // declNode.parent is VariableDeclaration && 765 if (declNode != null &&
747 // identical((declNode.parent as VariableDeclaration).name, declNode) && 766 declNode.parent is VariableDeclaration &&
748 // declNode.parent.parent is VariableDeclarationList && 767 identical((declNode.parent as VariableDeclaration).name, declNode) &&
749 // declNode.parent.parent.parent is VariableDeclarationStatement) { 768 declNode.parent.parent is VariableDeclarationList &&
750 // } else { 769 declNode.parent.parent.parent is VariableDeclarationStatement) {
751 // return; 770 } else {
752 // } 771 _coverageMarker();
753 // VariableDeclaration decl = declNode.parent as VariableDeclaration; 772 return;
754 // VariableDeclarationStatement declStatement = 773 }
755 // decl.parent.parent as VariableDeclarationStatement; 774 VariableDeclaration decl = declNode.parent as VariableDeclaration;
756 // // may be has initializer 775 VariableDeclarationStatement declStatement =
757 // if (decl.initializer != null) { 776 decl.parent.parent as VariableDeclarationStatement;
758 // return; 777 // may be has initializer
759 // } 778 if (decl.initializer != null) {
760 // // check that "declaration" statement declared only one variable 779 _coverageMarker();
761 // if (declStatement.variables.variables.length != 1) { 780 return;
762 // return; 781 }
763 // } 782 // check that "declaration" statement declared only one variable
764 // 783 if (declStatement.variables.variables.length != 1) {
765 // // check that "declaration" and "assignment" statements are part of sa me Block 784 _coverageMarker();
766 // ExpressionStatement assignStatement = 785 return;
767 // node.parent.parent as ExpressionStatement; 786 }
768 // if (assignStatement.parent is Block && 787 // check that the "declaration" and "assignment" statements are
769 // identical(assignStatement.parent, declStatement.parent)) { 788 // parts of the same Block
770 // } else { 789 ExpressionStatement assignStatement =
771 // return; 790 node.parent.parent as ExpressionStatement;
772 // } 791 if (assignStatement.parent is Block &&
773 // Block block = assignStatement.parent as Block; 792 assignStatement.parent == declStatement.parent) {
774 // // check that "declaration" and "assignment" statements are adjacent 793 } else {
775 // List<Statement> statements = block.statements; 794 _coverageMarker();
776 // if (statements.indexOf(assignStatement) == 795 return;
777 // statements.indexOf(declStatement) + 1) { 796 }
778 // } else { 797 Block block = assignStatement.parent as Block;
779 // return; 798 // check that "declaration" and "assignment" statements are adjacent
780 // } 799 List<Statement> statements = block.statements;
781 // // add edits 800 if (statements.indexOf(assignStatement) ==
782 // { 801 statements.indexOf(declStatement) + 1) {
783 // int assignOffset = assignExpression.operator.offset; 802 } else {
784 // _addReplaceEdit( 803 _coverageMarker();
785 // rangeEndStart(declNode, assignOffset), 804 return;
786 // " "); 805 }
787 // } 806 // add edits
788 // // add proposal 807 {
789 // _addAssist(AssistKind.JOIN_VARIABLE_DECLARATION, []); 808 int assignOffset = assignExpression.operator.offset;
809 _addReplaceEdit(rangeEndStart(declNode, assignOffset), " ");
810 }
811 // add proposal
812 _addAssist(AssistKind.JOIN_VARIABLE_DECLARATION, []);
790 } 813 }
791 814
792 void _addProposal_joinVariableDeclaration_onDeclaration() { 815 void _addProposal_joinVariableDeclaration_onDeclaration() {
793 // TODO(scheglov) implement 816 // prepare enclosing VariableDeclarationList
794 // // prepare enclosing VariableDeclarationList 817 VariableDeclarationList declList =
795 // VariableDeclarationList declList = 818 node.getAncestor((node) => node is VariableDeclarationList);
796 // node.getAncestor((node) => node is VariableDeclarationList); 819 if (declList != null && declList.variables.length == 1) {
797 // if (declList != null && declList.variables.length == 1) { 820 } else {
798 // } else { 821 _coverageMarker();
799 // return; 822 return;
800 // } 823 }
801 // VariableDeclaration decl = declList.variables[0]; 824 VariableDeclaration decl = declList.variables[0];
802 // // already initialized 825 // already initialized
803 // if (decl.initializer != null) { 826 if (decl.initializer != null) {
804 // return; 827 _coverageMarker();
805 // } 828 return;
806 // // prepare VariableDeclarationStatement in Block 829 }
807 // if (declList.parent is VariableDeclarationStatement && 830 // prepare VariableDeclarationStatement in Block
808 // declList.parent.parent is Block) { 831 if (declList.parent is VariableDeclarationStatement &&
809 // } else { 832 declList.parent.parent is Block) {
810 // return; 833 } else {
811 // } 834 _coverageMarker();
812 // VariableDeclarationStatement declStatement = 835 return;
813 // declList.parent as VariableDeclarationStatement; 836 }
814 // Block block = declStatement.parent as Block; 837 VariableDeclarationStatement declStatement =
815 // List<Statement> statements = block.statements; 838 declList.parent as VariableDeclarationStatement;
816 // // prepare assignment 839 Block block = declStatement.parent as Block;
817 // AssignmentExpression assignExpression; 840 List<Statement> statements = block.statements;
818 // { 841 // prepare assignment
819 // // declaration should not be last Statement 842 AssignmentExpression assignExpression;
820 // int declIndex = statements.indexOf(declStatement); 843 {
821 // if (declIndex < statements.length - 1) { 844 // declaration should not be last Statement
822 // } else { 845 int declIndex = statements.indexOf(declStatement);
823 // return; 846 if (declIndex < statements.length - 1) {
824 // } 847 } else {
825 // // next Statement should be assignment 848 _coverageMarker();
826 // Statement assignStatement = statements[declIndex + 1]; 849 return;
827 // if (assignStatement is ExpressionStatement) { 850 }
828 // } else { 851 // next Statement should be assignment
829 // return; 852 Statement assignStatement = statements[declIndex + 1];
830 // } 853 if (assignStatement is ExpressionStatement) {
831 // ExpressionStatement expressionStatement = 854 } else {
832 // assignStatement as ExpressionStatement; 855 _coverageMarker();
833 // // expression should be assignment 856 return;
834 // if (expressionStatement.expression is AssignmentExpression) { 857 }
835 // } else { 858 ExpressionStatement expressionStatement =
836 // return; 859 assignStatement as ExpressionStatement;
837 // } 860 // expression should be assignment
838 // assignExpression = expressionStatement.expression as AssignmentExpressio n; 861 if (expressionStatement.expression is AssignmentExpression) {
839 // } 862 } else {
840 // // check that pure assignment 863 _coverageMarker();
841 // if (assignExpression.operator.type != TokenType.EQ) { 864 return;
842 // return; 865 }
843 // } 866 assignExpression = expressionStatement.expression as AssignmentExpression;
844 // // add edits 867 }
845 // { 868 // check that pure assignment
846 // int assignOffset = assignExpression.operator.offset; 869 if (assignExpression.operator.type != TokenType.EQ) {
847 // _addReplaceEdit( 870 _coverageMarker();
848 // rangeEndStart(decl.name, assignOffset), 871 return;
849 // " "); 872 }
850 // } 873 // add edits
851 // // add proposal 874 {
852 // _addAssist(AssistKind.JOIN_VARIABLE_DECLARATION, []); 875 int assignOffset = assignExpression.operator.offset;
876 _addReplaceEdit(rangeEndStart(decl.name, assignOffset), " ");
877 }
878 // add proposal
879 _addAssist(AssistKind.JOIN_VARIABLE_DECLARATION, []);
853 } 880 }
854 881
855 void _addProposal_removeTypeAnnotation() { 882 void _addProposal_removeTypeAnnotation() {
856 // TODO(scheglov) implement 883 AstNode typeStart = null;
857 // AstNode typeStart = null; 884 AstNode typeEnd = null;
858 // AstNode typeEnd = null; 885 // try top-level variable
859 // // try top-level variable 886 {
860 // { 887 TopLevelVariableDeclaration declaration =
861 // TopLevelVariableDeclaration declaration = 888 node.getAncestor((node) => node is TopLevelVariableDeclaration);
862 // node.getAncestor((node) => node is TopLevelVariableDeclaration); 889 if (declaration != null) {
863 // if (declaration != null) { 890 TypeName typeNode = declaration.variables.type;
864 // TypeName typeNode = declaration.variables.type; 891 if (typeNode != null) {
865 // if (typeNode != null) { 892 VariableDeclaration field = declaration.variables.variables[0];
866 // VariableDeclaration field = declaration.variables.variables[0]; 893 typeStart = declaration;
867 // typeStart = declaration; 894 typeEnd = field;
868 // typeEnd = field; 895 }
869 // } 896 }
870 // } 897 }
871 // } 898 // try class field
872 // // try class field 899 {
873 // { 900 FieldDeclaration fieldDeclaration =
874 // FieldDeclaration fieldDeclaration = 901 node.getAncestor((node) => node is FieldDeclaration);
875 // node.getAncestor((node) => node is FieldDeclaration); 902 if (fieldDeclaration != null) {
876 // if (fieldDeclaration != null) { 903 TypeName typeNode = fieldDeclaration.fields.type;
877 // TypeName typeNode = fieldDeclaration.fields.type; 904 if (typeNode != null) {
878 // if (typeNode != null) { 905 VariableDeclaration field = fieldDeclaration.fields.variables[0];
879 // VariableDeclaration field = fieldDeclaration.fields.variables[0]; 906 typeStart = fieldDeclaration;
880 // typeStart = fieldDeclaration; 907 typeEnd = field;
881 // typeEnd = field; 908 }
882 // } 909 }
883 // } 910 }
884 // } 911 // try local variable
885 // // try local variable 912 {
886 // { 913 VariableDeclarationStatement statement =
887 // VariableDeclarationStatement statement = 914 node.getAncestor((node) => node is VariableDeclarationStatement);
888 // node.getAncestor((node) => node is VariableDeclarationStatement); 915 if (statement != null) {
889 // if (statement != null) { 916 TypeName typeNode = statement.variables.type;
890 // TypeName typeNode = statement.variables.type; 917 if (typeNode != null) {
891 // if (typeNode != null) { 918 VariableDeclaration variable = statement.variables.variables[0];
892 // VariableDeclaration variable = statement.variables.variables[0]; 919 typeStart = typeNode;
893 // typeStart = typeNode; 920 typeEnd = variable;
894 // typeEnd = variable; 921 }
895 // } 922 }
896 // } 923 }
897 // } 924 // add edit
898 // // add edit 925 if (typeStart != null && typeEnd != null) {
899 // if (typeStart != null && typeEnd != null) { 926 SourceRange typeRange = rangeStartStart(typeStart, typeEnd);
900 // SourceRange typeRange = 927 _addReplaceEdit(typeRange, "var ");
901 // rangeStartStart(typeStart, typeEnd); 928 }
902 // _addReplaceEdit(typeRange, "var "); 929 // add proposal
903 // } 930 _addAssist(AssistKind.REMOVE_TYPE_ANNOTATION, []);
904 // // add proposal
905 // _addAssist(AssistKind.REMOVE_TYPE_ANNOTATION, []);
906 } 931 }
907 932
908 void _addProposal_replaceConditionalWithIfElse() { 933 void _addProposal_replaceConditionalWithIfElse() {
909 // TODO(scheglov) implement 934 ConditionalExpression conditional = null;
910 // ConditionalExpression conditional = null; 935 // may be on Statement with Conditional
911 // // may be on Statement with Conditional 936 Statement statement = node.getAncestor((node) => node is Statement);
912 // Statement statement = node.getAncestor((node) => node is Statement); 937 if (statement == null) {
913 // if (statement == null) { 938 _coverageMarker();
914 // return; 939 return;
915 // } 940 }
916 // // variable declaration 941 // variable declaration
917 // bool inVariable = false; 942 bool inVariable = false;
918 // if (statement is VariableDeclarationStatement) { 943 if (statement is VariableDeclarationStatement) {
919 // VariableDeclarationStatement variableStatement = statement; 944 VariableDeclarationStatement variableStatement = statement;
920 // for (VariableDeclaration variable in 945 for (VariableDeclaration variable in
921 // variableStatement.variables.variables) { 946 variableStatement.variables.variables) {
922 // if (variable.initializer is ConditionalExpression) { 947 if (variable.initializer is ConditionalExpression) {
923 // conditional = variable.initializer as ConditionalExpression; 948 conditional = variable.initializer as ConditionalExpression;
924 // inVariable = true; 949 inVariable = true;
925 // break; 950 break;
926 // } 951 }
927 // } 952 }
928 // } 953 }
929 // // assignment 954 // assignment
930 // bool inAssignment = false; 955 bool inAssignment = false;
931 // if (statement is ExpressionStatement) { 956 if (statement is ExpressionStatement) {
932 // ExpressionStatement exprStmt = statement; 957 ExpressionStatement exprStmt = statement;
933 // if (exprStmt.expression is AssignmentExpression) { 958 if (exprStmt.expression is AssignmentExpression) {
934 // AssignmentExpression assignment = 959 AssignmentExpression assignment =
935 // exprStmt.expression as AssignmentExpression; 960 exprStmt.expression as AssignmentExpression;
936 // if (assignment.operator.type == TokenType.EQ && 961 if (assignment.operator.type == TokenType.EQ &&
937 // assignment.rightHandSide is ConditionalExpression) { 962 assignment.rightHandSide is ConditionalExpression) {
938 // conditional = assignment.rightHandSide as ConditionalExpression; 963 conditional = assignment.rightHandSide as ConditionalExpression;
939 // inAssignment = true; 964 inAssignment = true;
940 // } 965 }
941 // } 966 }
942 // } 967 }
943 // // return 968 // return
944 // bool inReturn = false; 969 bool inReturn = false;
945 // if (statement is ReturnStatement) { 970 if (statement is ReturnStatement) {
946 // ReturnStatement returnStatement = statement; 971 ReturnStatement returnStatement = statement;
947 // if (returnStatement.expression is ConditionalExpression) { 972 if (returnStatement.expression is ConditionalExpression) {
948 // conditional = returnStatement.expression as ConditionalExpression; 973 conditional = returnStatement.expression as ConditionalExpression;
949 // inReturn = true; 974 inReturn = true;
950 // } 975 }
951 // } 976 }
952 // // prepare environment 977 // prepare environment
953 // String eol = utils.endOfLine; 978 String indent = utils.getIndent(1);
954 // String indent = utils.getIndent(1); 979 String prefix = utils.getNodePrefix(statement);
955 // String prefix = utils.getNodePrefix(statement); 980 // Type v = Conditional;
956 // // Type v = Conditional; 981 if (inVariable) {
957 // if (inVariable) { 982 VariableDeclaration variable = conditional.parent as VariableDeclaration;
958 // VariableDeclaration variable = conditional.parent as VariableDeclaration ; 983 _addRemoveEdit(rangeEndEnd(variable.name, conditional));
959 // _addRemoveEdit( 984 String conditionSrc = _getSource(conditional.condition);
960 // rangeEndEnd(variable.name, conditional)); 985 String thenSrc = _getSource(conditional.thenExpression);
961 // // TODO(scheglov) 986 String elseSrc = _getSource(conditional.elseExpression);
962 //// _addReplaceEdit( 987 String name = variable.name.name;
963 //// rangeEndLength(statement, 0), 988 String src = eol;
964 //// MessageFormat.format( 989 src += prefix + 'if ($conditionSrc) {' + eol;
965 //// "{3}{4}if ({0}) '{'{3}{4}{5}{6} = {1};{3}{4}'} else {'{3}{4}{5 }{6} = {2};{3}{4}'}'", 990 src += prefix + indent + '$name = $thenSrc;' + eol;
966 //// [ 991 src += prefix + '} else {' + eol;
967 //// _getSource(conditional.condition), 992 src += prefix + indent + '$name = $elseSrc;' + eol;
968 //// _getSource(conditional.thenExpression), 993 src += prefix + '}';
969 //// _getSource(conditional.elseExpression), 994 _addReplaceEdit(rangeEndLength(statement, 0), src);
970 //// eol, 995 }
971 //// prefix, 996 // v = Conditional;
972 //// indent, 997 if (inAssignment) {
973 //// variable.name])); 998 AssignmentExpression assignment =
974 // } 999 conditional.parent as AssignmentExpression;
975 // // v = Conditional; 1000 Expression leftSide = assignment.leftHandSide;
976 // if (inAssignment) { 1001 String conditionSrc = _getSource(conditional.condition);
977 // AssignmentExpression assignment = 1002 String thenSrc = _getSource(conditional.thenExpression);
978 // conditional.parent as AssignmentExpression; 1003 String elseSrc = _getSource(conditional.elseExpression);
979 // Expression leftSide = assignment.leftHandSide; 1004 String name = _getSource(leftSide);
980 // // TODO(scheglov) 1005 String src = '';
981 //// _addReplaceEdit( 1006 src += 'if ($conditionSrc) {' + eol;
982 //// rangeNode(statement), 1007 src += prefix + indent + '$name = $thenSrc;' + eol;
983 //// MessageFormat.format( 1008 src += prefix + '} else {' + eol;
984 //// "if ({0}) '{'{3}{4}{5}{6} = {1};{3}{4}'} else {'{3}{4}{5}{6} = {2};{3}{4}'}'", 1009 src += prefix + indent + '$name = $elseSrc;' + eol;
985 //// [ 1010 src += prefix + '}';
986 //// _getSource(conditional.condition), 1011 _addReplaceEdit(rangeNode(statement), src);
987 //// _getSource(conditional.thenExpression), 1012 }
988 //// _getSource(conditional.elseExpression), 1013 // return Conditional;
989 //// eol, 1014 if (inReturn) {
990 //// prefix, 1015 String conditionSrc = _getSource(conditional.condition);
991 //// indent, 1016 String thenSrc = _getSource(conditional.thenExpression);
992 //// _getSource(leftSide)])); 1017 String elseSrc = _getSource(conditional.elseExpression);
993 // } 1018 String src = '';
994 // // return Conditional; 1019 src += 'if ($conditionSrc) {' + eol;
995 // if (inReturn) { 1020 src += prefix + indent + 'return $thenSrc;' + eol;
996 // // TODO(scheglov) 1021 src += prefix + '} else {' + eol;
997 //// _addReplaceEdit( 1022 src += prefix + indent + 'return $elseSrc;' + eol;
998 //// rangeNode(statement), 1023 src += prefix + '}';
999 //// MessageFormat.format( 1024 _addReplaceEdit(rangeNode(statement), src);
1000 //// "if ({0}) '{'{3}{4}{5}return {1};{3}{4}'} else {'{3}{4}{5}retu rn {2};{3}{4}'}'", 1025 }
1001 //// [ 1026 // add proposal
1002 //// _getSource(conditional.condition), 1027 _addAssist(AssistKind.REPLACE_CONDITIONAL_WITH_IF_ELSE, []);
1003 //// _getSource(conditional.thenExpression),
1004 //// _getSource(conditional.elseExpression),
1005 //// eol,
1006 //// prefix,
1007 //// indent]));
1008 // }
1009 // // add proposal
1010 // _addAssist(
1011 // AssistKind.REPLACE_CONDITIONAL_WITH_IF_ELSE,
1012 // []);
1013 } 1028 }
1014 1029
1015 void _addProposal_replaceIfElseWithConditional() { 1030 void _addProposal_replaceIfElseWithConditional() {
1016 // TODO(scheglov) implement 1031 // TODO(scheglov) implement
1017 // // should be "if" 1032 // // should be "if"
1018 // if (node is! IfStatement) { 1033 // if (node is! IfStatement) {
1019 // return; 1034 // return;
1020 // } 1035 // }
1021 // IfStatement ifStatement = node as IfStatement; 1036 // IfStatement ifStatement = node as IfStatement;
1022 // // single then/else statements 1037 // // single then/else statements
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1102 // while (condition.parent is BinaryExpression && 1117 // while (condition.parent is BinaryExpression &&
1103 // (condition.parent as BinaryExpression).operator.type == 1118 // (condition.parent as BinaryExpression).operator.type ==
1104 // TokenType.AMPERSAND_AMPERSAND) { 1119 // TokenType.AMPERSAND_AMPERSAND) {
1105 // condition = condition.parent as BinaryExpression; 1120 // condition = condition.parent as BinaryExpression;
1106 // } 1121 // }
1107 // if (!identical(ifStatement.condition, condition)) { 1122 // if (!identical(ifStatement.condition, condition)) {
1108 // return; 1123 // return;
1109 // } 1124 // }
1110 // // prepare environment 1125 // // prepare environment
1111 // String prefix = utils.getNodePrefix(ifStatement); 1126 // String prefix = utils.getNodePrefix(ifStatement);
1112 // String eol = utils.endOfLine;
1113 // String indent = utils.getIndent(1); 1127 // String indent = utils.getIndent(1);
1114 // // prepare "rightCondition" 1128 // // prepare "rightCondition"
1115 // String rightConditionSource; 1129 // String rightConditionSource;
1116 // { 1130 // {
1117 // SourceRange rightConditionRange = 1131 // SourceRange rightConditionRange =
1118 // rangeStartEnd(binaryExpression.rightOperand, condition); 1132 // rangeStartEnd(binaryExpression.rightOperand, condition);
1119 // rightConditionSource = _getSource2(rightConditionRange); 1133 // rightConditionSource = _getSource2(rightConditionRange);
1120 // } 1134 // }
1121 // // remove "&& rightCondition" 1135 // // remove "&& rightCondition"
1122 // _addRemoveEdit( 1136 // _addRemoveEdit(
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
1203 // List<VariableDeclaration> variables = statement.variables.variables; 1217 // List<VariableDeclaration> variables = statement.variables.variables;
1204 // if (variables.length != 1) { 1218 // if (variables.length != 1) {
1205 // return; 1219 // return;
1206 // } 1220 // }
1207 // VariableDeclaration variable = variables[0]; 1221 // VariableDeclaration variable = variables[0];
1208 // // remove initializer value 1222 // // remove initializer value
1209 // _addRemoveEdit( 1223 // _addRemoveEdit(
1210 // rangeEndStart(variable.name, statement.semicolon)); 1224 // rangeEndStart(variable.name, statement.semicolon));
1211 // // TODO(scheglov) 1225 // // TODO(scheglov)
1212 //// // add assignment statement 1226 //// // add assignment statement
1213 //// String eol = _utils.endOfLine;
1214 //// String indent = _utils.getNodePrefix(statement); 1227 //// String indent = _utils.getNodePrefix(statement);
1215 //// String assignSource = 1228 //// String assignSource =
1216 //// MessageFormat.format( 1229 //// MessageFormat.format(
1217 //// "{0} = {1};", 1230 //// "{0} = {1};",
1218 //// [variable.name.name, _getSource(variable.initializer)]); 1231 //// [variable.name.name, _getSource(variable.initializer)]);
1219 //// SourceRange assignRange = rangeEndLength(statement, 0); 1232 //// SourceRange assignRange = rangeEndLength(statement, 0);
1220 //// _addReplaceEdit(assignRange, "${eol}${indent}${assignSource}"); 1233 //// _addReplaceEdit(assignRange, "${eol}${indent}${assignSource}");
1221 //// // add proposal 1234 //// // add proposal
1222 //// _addUnitCorrectionProposal( 1235 //// _addUnitCorrectionProposal(
1223 //// AssistKind.SPLIT_VARIABLE_DECLARATION, 1236 //// AssistKind.SPLIT_VARIABLE_DECLARATION,
(...skipping 22 matching lines...) Expand all
1246 // if (selectedStatements.isEmpty || 1259 // if (selectedStatements.isEmpty ||
1247 // selectedStatements.length != selectedNodes.length) { 1260 // selectedStatements.length != selectedNodes.length) {
1248 // return; 1261 // return;
1249 // } 1262 // }
1250 // } 1263 // }
1251 // // prepare statement information 1264 // // prepare statement information
1252 // Statement firstStatement = selectedStatements[0]; 1265 // Statement firstStatement = selectedStatements[0];
1253 // Statement lastStatement = selectedStatements[selectedStatements.length - 1 ]; 1266 // Statement lastStatement = selectedStatements[selectedStatements.length - 1 ];
1254 // SourceRange statementsRange = utils.getLinesRange(selectedStatements); 1267 // SourceRange statementsRange = utils.getLinesRange(selectedStatements);
1255 // // prepare environment 1268 // // prepare environment
1256 // String eol = utils.endOfLine;
1257 // String indentOld = utils.getNodePrefix(firstStatement); 1269 // String indentOld = utils.getNodePrefix(firstStatement);
1258 // String indentNew = "${indentOld}${utils.getIndent(1)}"; 1270 // String indentNew = "${indentOld}${utils.getIndent(1)}";
1259 // // "block" 1271 // // "block"
1260 // { 1272 // {
1261 // _addInsertEdit(statementsRange.offset, "${indentOld}{${eol}"); 1273 // _addInsertEdit(statementsRange.offset, "${indentOld}{${eol}");
1262 // { 1274 // {
1263 // Edit edit = 1275 // Edit edit =
1264 // utils.createIndentEdit(statementsRange, indentOld, indentNew); 1276 // utils.createIndentEdit(statementsRange, indentOld, indentNew);
1265 // edits.add(edit); 1277 // edits.add(edit);
1266 // } 1278 // }
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
1515 1527
1516 /** 1528 /**
1517 * Adds a new [Edit] to [edits]. 1529 * Adds a new [Edit] to [edits].
1518 */ 1530 */
1519 void _addReplaceEdit(SourceRange range, String text) { 1531 void _addReplaceEdit(SourceRange range, String text) {
1520 Edit edit = new Edit(range.offset, range.length, text); 1532 Edit edit = new Edit(range.offset, range.length, text);
1521 edits.add(edit); 1533 edits.add(edit);
1522 } 1534 }
1523 1535
1524 /** 1536 /**
1525 * This method does nothing, but we invoke it in places where Dart VM
1526 * coverage agent fails to provide coverage information - such as almost
1527 * all "return" statements.
1528 *
1529 * https://code.google.com/p/dart/issues/detail?id=19912
1530 */
1531 void _coverageMarker() {
1532 }
1533
1534 /**
1535 * Returns an existing or just added [LinkedPositionGroup] with [groupId]. 1537 * Returns an existing or just added [LinkedPositionGroup] with [groupId].
1536 */ 1538 */
1537 LinkedPositionGroup _getLinkedPosition(String groupId) { 1539 LinkedPositionGroup _getLinkedPosition(String groupId) {
1538 LinkedPositionGroup group = linkedPositionGroups[groupId]; 1540 LinkedPositionGroup group = linkedPositionGroups[groupId];
1539 if (group == null) { 1541 if (group == null) {
1540 group = new LinkedPositionGroup(groupId); 1542 group = new LinkedPositionGroup(groupId);
1541 linkedPositionGroups[groupId] = group; 1543 linkedPositionGroups[groupId] = group;
1542 } 1544 }
1543 return group; 1545 return group;
1544 } 1546 }
(...skipping 24 matching lines...) Expand all
1569 builder.linkedPositionGroups.forEach((LinkedPositionGroup group) { 1571 builder.linkedPositionGroups.forEach((LinkedPositionGroup group) {
1570 LinkedPositionGroup fixGroup = _getLinkedPosition(group.id); 1572 LinkedPositionGroup fixGroup = _getLinkedPosition(group.id);
1571 group.positions.forEach((Position position) { 1573 group.positions.forEach((Position position) {
1572 fixGroup.addPosition(position); 1574 fixGroup.addPosition(position);
1573 }); 1575 });
1574 group.proposals.forEach((String proposal) { 1576 group.proposals.forEach((String proposal) {
1575 fixGroup.addProposal(proposal); 1577 fixGroup.addProposal(proposal);
1576 }); 1578 });
1577 }); 1579 });
1578 } 1580 }
1581
1582 /**
1583 * This method does nothing, but we invoke it in places where Dart VM
1584 * coverage agent fails to provide coverage information - such as almost
1585 * all "return" statements.
1586 *
1587 * https://code.google.com/p/dart/issues/detail?id=19912
1588 */
1589 static void _coverageMarker() {
1590 }
1591
1592 /**
1593 * Returns `true` if the selection covers an operator of the given
1594 * [BinaryExpression].
1595 */
1596 static bool _isOperatorSelected(BinaryExpression binaryExpression, int offset,
1597 int length) {
1598 AstNode left = binaryExpression.leftOperand;
1599 AstNode right = binaryExpression.rightOperand;
1600 // between the nodes
1601 if (offset >= left.endToken.end && offset + length <= right.offset) {
1602 _coverageMarker();
1603 return true;
1604 }
1605 // or exactly select the node (but not with infix expressions)
1606 if (offset == left.offset && offset + length == right.endToken.end) {
1607 if (left is BinaryExpression || right is BinaryExpression) {
1608 _coverageMarker();
1609 return false;
1610 }
1611 _coverageMarker();
1612 return true;
1613 }
1614 // invalid selection (part of node, etc)
1615 _coverageMarker();
1616 return false;
1617 }
1579 } 1618 }
OLDNEW
« no previous file with comments | « pkg/analysis_services/lib/correction/assist.dart ('k') | pkg/analysis_services/lib/src/correction/source_range.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698