Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 edit.domain; | 5 library edit.domain; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/analysis_server.dart'; | 9 import 'package:analysis_server/src/analysis_server.dart'; |
| 10 import 'package:analysis_server/src/collections.dart'; | 10 import 'package:analysis_server/src/collections.dart'; |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 297 | 297 |
| 298 void getRefactoring(Request request) { | 298 void getRefactoring(Request request) { |
| 299 // prepare for processing the request | 299 // prepare for processing the request |
| 300 requestId = request.id; | 300 requestId = request.id; |
| 301 result = new EditGetRefactoringResult( | 301 result = new EditGetRefactoringResult( |
| 302 EMPTY_PROBLEM_LIST, | 302 EMPTY_PROBLEM_LIST, |
| 303 EMPTY_PROBLEM_LIST, | 303 EMPTY_PROBLEM_LIST, |
| 304 EMPTY_PROBLEM_LIST); | 304 EMPTY_PROBLEM_LIST); |
| 305 // process the request | 305 // process the request |
| 306 var params = new EditGetRefactoringParams.fromRequest(request); | 306 var params = new EditGetRefactoringParams.fromRequest(request); |
| 307 runZoned(() { | 307 runZoned(() async { |
| 308 _init(params.kind, params.file, params.offset, params.length).then((_) { | 308 await _init(params.kind, params.file, params.offset, params.length); |
| 309 if (initStatus.hasFatalError) { | 309 if (initStatus.hasFatalError) { |
| 310 feedback = null; | 310 feedback = null; |
| 311 return _sendResultResponse(); | |
|
Paul Berry
2015/01/26 23:14:21
This looks strange because _sendResultResponse() r
| |
| 312 } | |
| 313 // set options | |
| 314 if (_requiresOptions) { | |
| 315 if (params.options == null) { | |
| 316 optionsStatus = new RefactoringStatus(); | |
| 311 return _sendResultResponse(); | 317 return _sendResultResponse(); |
| 312 } | 318 } |
| 313 // set options | 319 optionsStatus = _setOptions(params); |
| 314 if (_requiresOptions) { | 320 if (_hasFatalError) { |
| 315 if (params.options == null) { | |
| 316 optionsStatus = new RefactoringStatus(); | |
| 317 return _sendResultResponse(); | |
| 318 } | |
| 319 optionsStatus = _setOptions(params); | |
| 320 if (_hasFatalError) { | |
| 321 return _sendResultResponse(); | |
| 322 } | |
| 323 } | |
| 324 // done if just validation | |
| 325 if (params.validateOnly) { | |
| 326 finalStatus = new RefactoringStatus(); | |
| 327 return _sendResultResponse(); | 321 return _sendResultResponse(); |
| 328 } | 322 } |
| 329 // simulate an exception | 323 } |
| 330 if (test_simulateRefactoringException_final) { | 324 // done if just validation |
| 331 throw 'A simulated refactoring exception - final.'; | 325 if (params.validateOnly) { |
| 332 } | 326 finalStatus = new RefactoringStatus(); |
| 333 // validation and create change | 327 return _sendResultResponse(); |
| 334 return refactoring.checkFinalConditions().then((_finalStatus) { | 328 } |
| 335 finalStatus = _finalStatus; | 329 // simulate an exception |
| 336 if (_hasFatalError) { | 330 if (test_simulateRefactoringException_final) { |
| 337 return _sendResultResponse(); | 331 throw 'A simulated refactoring exception - final.'; |
| 338 } | 332 } |
| 339 // simulate an exception | 333 // validation and create change |
| 340 if (test_simulateRefactoringException_change) { | 334 finalStatus = await refactoring.checkFinalConditions(); |
| 341 throw 'A simulated refactoring exception - change.'; | 335 if (_hasFatalError) { |
| 342 } | 336 return _sendResultResponse(); |
| 343 // create change | 337 } |
| 344 return refactoring.createChange().then((change) { | 338 // simulate an exception |
| 345 result.change = change; | 339 if (test_simulateRefactoringException_change) { |
| 346 result.potentialEdits = nullIfEmpty(refactoring.potentialEditIds); | 340 throw 'A simulated refactoring exception - change.'; |
| 347 return _sendResultResponse(); | 341 } |
| 348 }); | 342 // create change |
| 349 }); | 343 result.change = await refactoring.createChange(); |
| 350 }); | 344 result.potentialEdits = nullIfEmpty(refactoring.potentialEditIds); |
| 345 _sendResultResponse(); | |
| 351 }, onError: (exception, stackTrace) { | 346 }, onError: (exception, stackTrace) { |
| 352 server.instrumentationService.logException(exception, stackTrace); | 347 server.instrumentationService.logException(exception, stackTrace); |
| 353 server.sendResponse( | 348 server.sendResponse( |
| 354 new Response.serverError(request, exception, stackTrace)); | 349 new Response.serverError(request, exception, stackTrace)); |
| 355 _reset(); | 350 _reset(); |
| 356 }); | 351 }); |
| 357 } | 352 } |
| 358 | 353 |
| 359 /** | 354 /** |
| 360 * Awaits for analysis to complete and then calls [_init2] to actually | |
| 361 * initialize the resfactoring. | |
| 362 */ | |
| 363 Future<RefactoringStatus> _init(RefactoringKind kind, String file, int offset, | |
| 364 int length) { | |
| 365 return server.onAnalysisComplete.then((_) { | |
| 366 return _init2(kind, file, offset, length); | |
| 367 }); | |
| 368 } | |
| 369 | |
| 370 /** | |
| 371 * Initializes this context to perform a refactoring with the specified | 355 * Initializes this context to perform a refactoring with the specified |
| 372 * parameters. The existing [Refactoring] is reused or created as needed. | 356 * parameters. The existing [Refactoring] is reused or created as needed. |
| 373 */ | 357 */ |
| 374 Future<RefactoringStatus> _init2(RefactoringKind kind, String file, | 358 _init(RefactoringKind kind, String file, |
|
Paul Berry
2015/01/26 23:14:21
Nit: annotate that the return type of the function
| |
| 375 int offset, int length) { | 359 int offset, int length) async { |
| 360 await server.onAnalysisComplete; | |
| 376 // check if we can continue with the existing Refactoring instance | 361 // check if we can continue with the existing Refactoring instance |
| 377 if (this.kind == kind && | 362 if (this.kind == kind && |
| 378 this.file == file && | 363 this.file == file && |
| 379 this.offset == offset && | 364 this.offset == offset && |
| 380 this.length == length) { | 365 this.length == length) { |
| 381 return new Future.value(initStatus); | 366 return; |
| 382 } | 367 } |
| 383 _reset(); | 368 _reset(); |
| 384 this.kind = kind; | 369 this.kind = kind; |
| 385 this.file = file; | 370 this.file = file; |
| 386 this.offset = offset; | 371 this.offset = offset; |
| 387 this.length = length; | 372 this.length = length; |
| 388 // simulate an exception | 373 // simulate an exception |
| 389 if (test_simulateRefactoringException_init) { | 374 if (test_simulateRefactoringException_init) { |
| 390 throw 'A simulated refactoring exception - init.'; | 375 throw 'A simulated refactoring exception - init.'; |
| 391 } | 376 } |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 468 } | 453 } |
| 469 // do create the refactoring | 454 // do create the refactoring |
| 470 refactoring = new RenameRefactoring(searchEngine, element); | 455 refactoring = new RenameRefactoring(searchEngine, element); |
| 471 feedback = | 456 feedback = |
| 472 new RenameFeedback(node.offset, node.length, 'kind', 'oldName'); | 457 new RenameFeedback(node.offset, node.length, 'kind', 'oldName'); |
| 473 } | 458 } |
| 474 } | 459 } |
| 475 if (refactoring == null) { | 460 if (refactoring == null) { |
| 476 initStatus = | 461 initStatus = |
| 477 new RefactoringStatus.fatal('Unable to create a refactoring'); | 462 new RefactoringStatus.fatal('Unable to create a refactoring'); |
| 478 return new Future.value(initStatus); | 463 return; |
| 479 } | 464 } |
| 480 // check initial conditions | 465 // check initial conditions |
| 481 return refactoring.checkInitialConditions().then((status) { | 466 initStatus = await refactoring.checkInitialConditions(); |
| 482 initStatus = status; | 467 if (refactoring is ExtractLocalRefactoring) { |
| 483 if (refactoring is ExtractLocalRefactoring) { | 468 ExtractLocalRefactoring refactoring = this.refactoring; |
| 484 ExtractLocalRefactoring refactoring = this.refactoring; | 469 ExtractLocalVariableFeedback feedback = this.feedback; |
| 485 ExtractLocalVariableFeedback feedback = this.feedback; | 470 feedback.names = refactoring.names; |
| 486 feedback.names = refactoring.names; | 471 feedback.offsets = refactoring.offsets; |
| 487 feedback.offsets = refactoring.offsets; | 472 feedback.lengths = refactoring.lengths; |
| 488 feedback.lengths = refactoring.lengths; | 473 } |
| 474 if (refactoring is ExtractMethodRefactoring) { | |
| 475 ExtractMethodRefactoring refactoring = this.refactoring; | |
| 476 ExtractMethodFeedback feedback = this.feedback; | |
| 477 feedback.canCreateGetter = refactoring.canCreateGetter; | |
| 478 feedback.returnType = refactoring.returnType; | |
| 479 feedback.names = refactoring.names; | |
| 480 feedback.parameters = refactoring.parameters; | |
| 481 feedback.offsets = refactoring.offsets; | |
| 482 feedback.lengths = refactoring.lengths; | |
| 483 } | |
| 484 if (refactoring is InlineLocalRefactoring) { | |
| 485 InlineLocalRefactoring refactoring = this.refactoring; | |
| 486 if (!initStatus.hasFatalError) { | |
| 487 feedback = new InlineLocalVariableFeedback( | |
| 488 refactoring.variableName, | |
| 489 refactoring.referenceCount); | |
| 489 } | 490 } |
| 490 if (refactoring is ExtractMethodRefactoring) { | 491 } |
| 491 ExtractMethodRefactoring refactoring = this.refactoring; | 492 if (refactoring is InlineMethodRefactoring) { |
| 492 ExtractMethodFeedback feedback = this.feedback; | 493 InlineMethodRefactoring refactoring = this.refactoring; |
| 493 feedback.canCreateGetter = refactoring.canCreateGetter; | 494 if (!initStatus.hasFatalError) { |
| 494 feedback.returnType = refactoring.returnType; | 495 feedback = new InlineMethodFeedback( |
| 495 feedback.names = refactoring.names; | 496 refactoring.methodName, |
| 496 feedback.parameters = refactoring.parameters; | 497 refactoring.isDeclaration, |
| 497 feedback.offsets = refactoring.offsets; | 498 className: refactoring.className); |
| 498 feedback.lengths = refactoring.lengths; | |
| 499 } | 499 } |
| 500 if (refactoring is InlineLocalRefactoring) { | 500 } |
| 501 InlineLocalRefactoring refactoring = this.refactoring; | 501 if (refactoring is RenameRefactoring) { |
| 502 if (!status.hasFatalError) { | 502 RenameRefactoring refactoring = this.refactoring; |
| 503 feedback = new InlineLocalVariableFeedback( | 503 RenameFeedback feedback = this.feedback; |
| 504 refactoring.variableName, | 504 feedback.elementKindName = refactoring.elementKindName; |
| 505 refactoring.referenceCount); | 505 feedback.oldName = refactoring.oldName; |
| 506 } | 506 } |
| 507 } | |
| 508 if (refactoring is InlineMethodRefactoring) { | |
| 509 InlineMethodRefactoring refactoring = this.refactoring; | |
| 510 if (!status.hasFatalError) { | |
| 511 feedback = new InlineMethodFeedback( | |
| 512 refactoring.methodName, | |
| 513 refactoring.isDeclaration, | |
| 514 className: refactoring.className); | |
| 515 } | |
| 516 } | |
| 517 if (refactoring is RenameRefactoring) { | |
| 518 RenameRefactoring refactoring = this.refactoring; | |
| 519 RenameFeedback feedback = this.feedback; | |
| 520 feedback.elementKindName = refactoring.elementKindName; | |
| 521 feedback.oldName = refactoring.oldName; | |
| 522 } | |
| 523 return initStatus; | |
| 524 }); | |
| 525 } | 507 } |
| 526 | 508 |
| 527 void _reset([engine.AnalysisContext context]) { | 509 void _reset([engine.AnalysisContext context]) { |
| 528 kind = null; | 510 kind = null; |
| 529 offset = null; | 511 offset = null; |
| 530 length = null; | 512 length = null; |
| 531 refactoring = null; | 513 refactoring = null; |
| 532 feedback = null; | 514 feedback = null; |
| 533 initStatus = new RefactoringStatus(); | 515 initStatus = new RefactoringStatus(); |
| 534 optionsStatus = new RefactoringStatus(); | 516 optionsStatus = new RefactoringStatus(); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 583 } | 565 } |
| 584 if (refactoring is RenameRefactoring) { | 566 if (refactoring is RenameRefactoring) { |
| 585 RenameRefactoring renameRefactoring = refactoring; | 567 RenameRefactoring renameRefactoring = refactoring; |
| 586 RenameOptions renameOptions = params.options; | 568 RenameOptions renameOptions = params.options; |
| 587 renameRefactoring.newName = renameOptions.newName; | 569 renameRefactoring.newName = renameOptions.newName; |
| 588 return renameRefactoring.checkNewName(); | 570 return renameRefactoring.checkNewName(); |
| 589 } | 571 } |
| 590 return new RefactoringStatus(); | 572 return new RefactoringStatus(); |
| 591 } | 573 } |
| 592 } | 574 } |
| OLD | NEW |