| 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/constants.dart'; | 10 import 'package:analysis_server/src/constants.dart'; |
| 11 import 'package:analysis_server/src/protocol.dart'; | 11 import 'package:analysis_server/src/protocol.dart'; |
| 12 import 'package:analysis_server/src/protocol2.dart' show AnalysisError, | 12 import 'package:analysis_server/src/protocol2.dart' show AnalysisError, |
| 13 EditGetAssistsParams, EditGetAssistsResult, | 13 EditGetAssistsParams, EditGetAssistsResult, EditGetAvailableRefactoringsPara
ms, |
| 14 EditGetAvailableRefactoringsParams, EditGetAvailableRefactoringsResult, | 14 EditGetAvailableRefactoringsResult, EditGetFixesParams, EditGetFixesResult, |
| 15 EditGetFixesParams, EditGetFixesResult, EditGetRefactoringParams, | 15 EditGetRefactoringParams, EditGetRefactoringResult, ErrorFixes, LinkedEditGr
oup, |
| 16 EditGetRefactoringResult, ErrorFixes, LinkedEditGroup, Location, | 16 Location, RefactoringKind, RefactoringProblem, RefactoringProblemSeverity, |
| 17 RefactoringKind, RefactoringProblem, RefactoringProblemSeverity, | 17 RenameOptions, RenameFeedback, SourceChange; |
| 18 RenameOptions, SourceChange; | |
| 19 import 'package:analysis_server/src/services/correction/assist.dart'; | 18 import 'package:analysis_server/src/services/correction/assist.dart'; |
| 20 import 'package:analysis_server/src/services/correction/fix.dart'; | 19 import 'package:analysis_server/src/services/correction/fix.dart'; |
| 21 import 'package:analysis_server/src/services/correction/status.dart'; | 20 import 'package:analysis_server/src/services/correction/status.dart'; |
| 22 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; | 21 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; |
| 23 import 'package:analysis_server/src/services/search/search_engine.dart'; | 22 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 24 import 'package:analyzer/src/generated/ast.dart'; | 23 import 'package:analyzer/src/generated/ast.dart'; |
| 25 import 'package:analyzer/src/generated/element.dart'; | 24 import 'package:analyzer/src/generated/element.dart'; |
| 26 import 'package:analyzer/src/generated/engine.dart' as engine; | 25 import 'package:analyzer/src/generated/engine.dart' as engine; |
| 27 import 'package:analyzer/src/generated/error.dart' as engine; | 26 import 'package:analyzer/src/generated/error.dart' as engine; |
| 28 import 'package:analyzer/src/generated/source.dart'; | 27 import 'package:analyzer/src/generated/source.dart'; |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 */ | 164 */ |
| 166 class _RefactoringManager { | 165 class _RefactoringManager { |
| 167 final AnalysisServer server; | 166 final AnalysisServer server; |
| 168 final SearchEngine searchEngine; | 167 final SearchEngine searchEngine; |
| 169 | 168 |
| 170 RefactoringKind kind; | 169 RefactoringKind kind; |
| 171 String file; | 170 String file; |
| 172 int offset; | 171 int offset; |
| 173 int length; | 172 int length; |
| 174 Refactoring refactoring; | 173 Refactoring refactoring; |
| 174 Object feedback; |
| 175 RefactoringStatus initStatus; | 175 RefactoringStatus initStatus; |
| 176 RefactoringStatus optionsStatus; | 176 RefactoringStatus optionsStatus; |
| 177 RefactoringStatus finalStatus; | 177 RefactoringStatus finalStatus; |
| 178 | 178 |
| 179 String requestId; | 179 String requestId; |
| 180 EditGetRefactoringResult result; | 180 EditGetRefactoringResult result; |
| 181 | 181 |
| 182 _RefactoringManager(this.server, this.searchEngine) { | 182 _RefactoringManager(this.server, this.searchEngine) { |
| 183 _reset(); | 183 _reset(); |
| 184 } | 184 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 211 if (params.validateOnly) { | 211 if (params.validateOnly) { |
| 212 return _sendResultResponse(); | 212 return _sendResultResponse(); |
| 213 } | 213 } |
| 214 // validation and create change | 214 // validation and create change |
| 215 refactoring.checkFinalConditions().then((_finalStatus) { | 215 refactoring.checkFinalConditions().then((_finalStatus) { |
| 216 finalStatus = _finalStatus; | 216 finalStatus = _finalStatus; |
| 217 if (_hasFatalError) { | 217 if (_hasFatalError) { |
| 218 return _sendResultResponse(); | 218 return _sendResultResponse(); |
| 219 } | 219 } |
| 220 return refactoring.createChange().then((change) { | 220 return refactoring.createChange().then((change) { |
| 221 result.change = new SourceChange( | 221 result.change = new SourceChange(change.message, edits: change.edits); |
| 222 change.message, | |
| 223 edits: change.edits); | |
| 224 return _sendResultResponse(); | 222 return _sendResultResponse(); |
| 225 }); | 223 }); |
| 226 }); | 224 }); |
| 227 }); | 225 }); |
| 228 } | 226 } |
| 229 | 227 |
| 230 /** | 228 /** |
| 231 * Initializes this context to perform a refactoring with the specified | 229 * Initializes this context to perform a refactoring with the specified |
| 232 * parameters. The existing [Refactoring] is reused or created as needed. | 230 * parameters. The existing [Refactoring] is reused or created as needed. |
| 233 */ | 231 */ |
| 234 Future<RefactoringStatus> _init(RefactoringKind kind, String file, int offset, | 232 Future<RefactoringStatus> _init(RefactoringKind kind, String file, int offset, |
| 235 int length) { | 233 int length) { |
| 236 List<RefactoringProblem> problems = <RefactoringProblem>[]; | 234 List<RefactoringProblem> problems = <RefactoringProblem>[]; |
| 237 // check if we can continue with the existing Refactoring instance | 235 // check if we can continue with the existing Refactoring instance |
| 238 if (this.kind == kind && | 236 if (this.kind == kind && |
| 239 this.file == file && | 237 this.file == file && |
| 240 this.offset == offset && | 238 this.offset == offset && |
| 241 this.length == length) { | 239 this.length == length) { |
| 242 return new Future.value(initStatus); | 240 return new Future.value(initStatus); |
| 243 } | 241 } |
| 244 _reset(); | 242 _reset(); |
| 245 this.kind = kind; | 243 this.kind = kind; |
| 246 this.file = file; | 244 this.file = file; |
| 247 this.offset = offset; | 245 this.offset = offset; |
| 248 this.length = length; | 246 this.length = length; |
| 249 // create a new Refactoring instance | 247 // create a new Refactoring instance |
| 250 if (kind == RefactoringKind.RENAME) { | 248 if (kind == RefactoringKind.RENAME) { |
| 249 List<AstNode> nodes = server.getNodesAtOffset(file, offset); |
| 251 List<Element> elements = server.getElementsAtOffset(file, offset); | 250 List<Element> elements = server.getElementsAtOffset(file, offset); |
| 252 if (elements.isNotEmpty) { | 251 if (nodes.isNotEmpty && elements.isNotEmpty) { |
| 252 AstNode node = nodes[0]; |
| 253 Element element = elements[0]; | 253 Element element = elements[0]; |
| 254 refactoring = new RenameRefactoring(searchEngine, element); | 254 refactoring = new RenameRefactoring(searchEngine, element); |
| 255 feedback = new RenameFeedback(node.offset, node.length); |
| 255 } | 256 } |
| 256 } | 257 } |
| 257 if (refactoring == null) { | 258 if (refactoring == null) { |
| 258 initStatus = | 259 initStatus = |
| 259 new RefactoringStatus.fatal('Unable to create a refactoring'); | 260 new RefactoringStatus.fatal('Unable to create a refactoring'); |
| 260 return new Future.value(initStatus); | 261 return new Future.value(initStatus); |
| 261 } | 262 } |
| 262 // check initial conditions | 263 // check initial conditions |
| 263 return refactoring.checkInitialConditions().then((status) { | 264 return refactoring.checkInitialConditions().then((status) { |
| 264 initStatus = status; | 265 initStatus = status; |
| 265 return initStatus; | 266 return initStatus; |
| 266 }); | 267 }); |
| 267 } | 268 } |
| 268 | 269 |
| 269 void _reset() { | 270 void _reset() { |
| 270 refactoring = null; | 271 refactoring = null; |
| 272 feedback = null; |
| 271 initStatus = new RefactoringStatus(); | 273 initStatus = new RefactoringStatus(); |
| 272 optionsStatus = new RefactoringStatus(); | 274 optionsStatus = new RefactoringStatus(); |
| 273 finalStatus = new RefactoringStatus(); | 275 finalStatus = new RefactoringStatus(); |
| 274 } | 276 } |
| 275 | 277 |
| 276 void _sendResultResponse() { | 278 void _sendResultResponse() { |
| 279 result.feedback = feedback; |
| 277 // set problems | 280 // set problems |
| 278 { | 281 { |
| 279 RefactoringStatus status = new RefactoringStatus(); | 282 RefactoringStatus status = new RefactoringStatus(); |
| 280 status.addStatus(initStatus); | 283 status.addStatus(initStatus); |
| 281 status.addStatus(optionsStatus); | 284 status.addStatus(optionsStatus); |
| 282 status.addStatus(finalStatus); | 285 status.addStatus(finalStatus); |
| 283 result.problems = status.problems; | 286 result.problems = status.problems; |
| 284 } | 287 } |
| 285 // send the response | 288 // send the response |
| 286 server.sendResponse(result.toResponse(requestId)); | 289 server.sendResponse(result.toResponse(requestId)); |
| 287 // done with this request | 290 // done with this request |
| 288 requestId = null; | 291 requestId = null; |
| 289 result = null; | 292 result = null; |
| 290 } | 293 } |
| 291 | 294 |
| 292 RefactoringStatus _setOptions(Object options) { | 295 RefactoringStatus _setOptions(Object options) { |
| 293 if (refactoring is RenameRefactoring) { | 296 if (refactoring is RenameRefactoring) { |
| 294 RenameRefactoring renameRefactoring = refactoring; | 297 RenameRefactoring renameRefactoring = refactoring; |
| 295 RenameOptions renameOptions = options; | 298 RenameOptions renameOptions = options; |
| 296 String newName = renameOptions.newName; | 299 String newName = renameOptions.newName; |
| 297 renameRefactoring.newName = newName; | 300 renameRefactoring.newName = newName; |
| 298 return renameRefactoring.checkNewName(); | 301 return renameRefactoring.checkNewName(); |
| 299 } | 302 } |
| 300 return new RefactoringStatus(); | 303 return new RefactoringStatus(); |
| 301 } | 304 } |
| 302 } | 305 } |
| OLD | NEW |