| 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'; |
| 11 import 'package:analysis_server/src/constants.dart'; | 11 import 'package:analysis_server/src/constants.dart'; |
| 12 import 'package:analysis_server/src/protocol_server.dart' hide Element; | 12 import 'package:analysis_server/src/protocol_server.dart' hide Element; |
| 13 import 'package:analysis_server/src/services/correction/assist.dart'; | 13 import 'package:analysis_server/src/services/correction/assist.dart'; |
| 14 import 'package:analysis_server/src/services/correction/fix.dart'; | 14 import 'package:analysis_server/src/services/correction/fix.dart'; |
| 15 import 'package:analysis_server/src/services/correction/sort_members.dart'; | 15 import 'package:analysis_server/src/services/correction/sort_members.dart'; |
| 16 import 'package:analysis_server/src/services/correction/status.dart'; | 16 import 'package:analysis_server/src/services/correction/status.dart'; |
| 17 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; | 17 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; |
| 18 import 'package:analysis_server/src/services/search/search_engine.dart'; | 18 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 19 import 'package:analyzer/src/generated/ast.dart'; | 19 import 'package:analyzer/src/generated/ast.dart'; |
| 20 import 'package:analyzer/src/generated/element.dart'; | 20 import 'package:analyzer/src/generated/element.dart'; |
| 21 import 'package:analyzer/src/generated/engine.dart' as engine; | 21 import 'package:analyzer/src/generated/engine.dart' as engine; |
| 22 import 'package:analyzer/src/generated/error.dart' as engine; | 22 import 'package:analyzer/src/generated/error.dart' as engine; |
| 23 import 'package:analyzer/src/generated/parser.dart' as engine; | 23 import 'package:analyzer/src/generated/parser.dart' as engine; |
| 24 import 'package:analyzer/src/generated/scanner.dart' as engine; | 24 import 'package:analyzer/src/generated/scanner.dart' as engine; |
| 25 import 'package:analyzer/src/generated/source.dart'; | 25 import 'package:analyzer/src/generated/source.dart'; |
| 26 | 26 |
| 27 | 27 |
| 28 /** | 28 bool test_simulateRefactoringException_change = false; |
| 29 * This flag is used in tests to check behavior when an exception happens | 29 bool test_simulateRefactoringException_final = false; |
| 30 * inside a refactoring. | 30 bool test_simulateRefactoringException_init = false; |
| 31 */ | |
| 32 bool test_simulateRefactoringException = false; | |
| 33 | 31 |
| 34 | 32 |
| 35 /** | 33 /** |
| 36 * Instances of the class [EditDomainHandler] implement a [RequestHandler] | 34 * Instances of the class [EditDomainHandler] implement a [RequestHandler] |
| 37 * that handles requests in the edit domain. | 35 * that handles requests in the edit domain. |
| 38 */ | 36 */ |
| 39 class EditDomainHandler implements RequestHandler { | 37 class EditDomainHandler implements RequestHandler { |
| 40 /** | 38 /** |
| 41 * The analysis server that is using this handler to process requests. | 39 * The analysis server that is using this handler to process requests. |
| 42 */ | 40 */ |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 | 250 |
| 253 void getRefactoring(Request request) { | 251 void getRefactoring(Request request) { |
| 254 // prepare for processing the request | 252 // prepare for processing the request |
| 255 requestId = request.id; | 253 requestId = request.id; |
| 256 result = new EditGetRefactoringResult( | 254 result = new EditGetRefactoringResult( |
| 257 EMPTY_PROBLEM_LIST, | 255 EMPTY_PROBLEM_LIST, |
| 258 EMPTY_PROBLEM_LIST, | 256 EMPTY_PROBLEM_LIST, |
| 259 EMPTY_PROBLEM_LIST); | 257 EMPTY_PROBLEM_LIST); |
| 260 // process the request | 258 // process the request |
| 261 var params = new EditGetRefactoringParams.fromRequest(request); | 259 var params = new EditGetRefactoringParams.fromRequest(request); |
| 262 _init(params.kind, params.file, params.offset, params.length).then((_) { | 260 runZoned(() { |
| 263 if (initStatus.hasFatalError) { | 261 _init(params.kind, params.file, params.offset, params.length).then((_) { |
| 264 feedback = null; | 262 if (initStatus.hasFatalError) { |
| 265 return _sendResultResponse(); | 263 feedback = null; |
| 266 } | |
| 267 // set options | |
| 268 if (_requiresOptions) { | |
| 269 if (params.options == null) { | |
| 270 optionsStatus = new RefactoringStatus(); | |
| 271 return _sendResultResponse(); | 264 return _sendResultResponse(); |
| 272 } | 265 } |
| 273 optionsStatus = _setOptions(params); | 266 // set options |
| 274 if (_hasFatalError) { | 267 if (_requiresOptions) { |
| 268 if (params.options == null) { |
| 269 optionsStatus = new RefactoringStatus(); |
| 270 return _sendResultResponse(); |
| 271 } |
| 272 optionsStatus = _setOptions(params); |
| 273 if (_hasFatalError) { |
| 274 return _sendResultResponse(); |
| 275 } |
| 276 } |
| 277 // done if just validation |
| 278 if (params.validateOnly) { |
| 279 finalStatus = new RefactoringStatus(); |
| 275 return _sendResultResponse(); | 280 return _sendResultResponse(); |
| 276 } | 281 } |
| 277 } | 282 // simulate an exception |
| 278 // done if just validation | 283 if (test_simulateRefactoringException_final) { |
| 279 if (params.validateOnly) { | 284 throw 'A simulated refactoring exception - final.'; |
| 280 finalStatus = new RefactoringStatus(); | |
| 281 return _sendResultResponse(); | |
| 282 } | |
| 283 // validation and create change | |
| 284 refactoring.checkFinalConditions().then((_finalStatus) { | |
| 285 finalStatus = _finalStatus; | |
| 286 if (_hasFatalError) { | |
| 287 return _sendResultResponse(); | |
| 288 } | 285 } |
| 289 // create change | 286 // validation and create change |
| 290 return refactoring.createChange().then((change) { | 287 return refactoring.checkFinalConditions().then((_finalStatus) { |
| 291 result.change = change; | 288 finalStatus = _finalStatus; |
| 292 result.potentialEdits = nullIfEmpty(refactoring.potentialEditIds); | 289 if (_hasFatalError) { |
| 293 return _sendResultResponse(); | 290 return _sendResultResponse(); |
| 291 } |
| 292 // simulate an exception |
| 293 if (test_simulateRefactoringException_change) { |
| 294 throw 'A simulated refactoring exception - change.'; |
| 295 } |
| 296 // create change |
| 297 return refactoring.createChange().then((change) { |
| 298 result.change = change; |
| 299 result.potentialEdits = nullIfEmpty(refactoring.potentialEditIds); |
| 300 return _sendResultResponse(); |
| 301 }); |
| 294 }); | 302 }); |
| 295 }); | 303 }); |
| 296 }).catchError((exception, stackTrace) { | 304 }, onError: (exception, stackTrace) { |
| 297 server.instrumentationService.logException(exception, stackTrace); | 305 server.instrumentationService.logException(exception, stackTrace); |
| 298 server.sendResponse( | 306 server.sendResponse( |
| 299 new Response.serverError(request, exception, stackTrace)); | 307 new Response.serverError(request, exception, stackTrace)); |
| 300 _reset(); | 308 _reset(); |
| 301 }); | 309 }); |
| 302 } | 310 } |
| 303 | 311 |
| 304 /** | 312 /** |
| 305 * Initializes this context to perform a refactoring with the specified | 313 * Initializes this context to perform a refactoring with the specified |
| 306 * parameters. The existing [Refactoring] is reused or created as needed. | 314 * parameters. The existing [Refactoring] is reused or created as needed. |
| 307 */ | 315 */ |
| 308 Future<RefactoringStatus> _init(RefactoringKind kind, String file, int offset, | 316 Future<RefactoringStatus> _init(RefactoringKind kind, String file, int offset, |
| 309 int length) { | 317 int length) { |
| 310 // check if we can continue with the existing Refactoring instance | 318 // check if we can continue with the existing Refactoring instance |
| 311 if (this.kind == kind && | 319 if (this.kind == kind && |
| 312 this.file == file && | 320 this.file == file && |
| 313 this.offset == offset && | 321 this.offset == offset && |
| 314 this.length == length) { | 322 this.length == length) { |
| 315 return new Future.value(initStatus); | 323 return new Future.value(initStatus); |
| 316 } | 324 } |
| 317 _reset(); | 325 _reset(); |
| 318 this.kind = kind; | 326 this.kind = kind; |
| 319 this.file = file; | 327 this.file = file; |
| 320 this.offset = offset; | 328 this.offset = offset; |
| 321 this.length = length; | 329 this.length = length; |
| 322 // simulate an exception | 330 // simulate an exception |
| 323 if (test_simulateRefactoringException) { | 331 if (test_simulateRefactoringException_init) { |
| 324 throw 'A simulated refactoring exception.'; | 332 throw 'A simulated refactoring exception - init.'; |
| 325 } | 333 } |
| 326 // create a new Refactoring instance | 334 // create a new Refactoring instance |
| 327 if (kind == RefactoringKind.CONVERT_GETTER_TO_METHOD) { | 335 if (kind == RefactoringKind.CONVERT_GETTER_TO_METHOD) { |
| 328 List<Element> elements = server.getElementsAtOffset(file, offset); | 336 List<Element> elements = server.getElementsAtOffset(file, offset); |
| 329 if (elements.isNotEmpty) { | 337 if (elements.isNotEmpty) { |
| 330 Element element = elements[0]; | 338 Element element = elements[0]; |
| 331 if (element is ExecutableElement) { | 339 if (element is ExecutableElement) { |
| 332 refactoring = | 340 refactoring = |
| 333 new ConvertGetterToMethodRefactoring(searchEngine, element); | 341 new ConvertGetterToMethodRefactoring(searchEngine, element); |
| 334 } | 342 } |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 } | 525 } |
| 518 if (refactoring is RenameRefactoring) { | 526 if (refactoring is RenameRefactoring) { |
| 519 RenameRefactoring renameRefactoring = refactoring; | 527 RenameRefactoring renameRefactoring = refactoring; |
| 520 RenameOptions renameOptions = params.options; | 528 RenameOptions renameOptions = params.options; |
| 521 renameRefactoring.newName = renameOptions.newName; | 529 renameRefactoring.newName = renameOptions.newName; |
| 522 return renameRefactoring.checkNewName(); | 530 return renameRefactoring.checkNewName(); |
| 523 } | 531 } |
| 524 return new RefactoringStatus(); | 532 return new RefactoringStatus(); |
| 525 } | 533 } |
| 526 } | 534 } |
| OLD | NEW |