| 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/edit/assist/assist_core.dart'; | 9 import 'package:analysis_server/edit/assist/assist_core.dart'; |
| 10 import 'package:analysis_server/edit/fix/fix_core.dart'; | 10 import 'package:analysis_server/edit/fix/fix_core.dart'; |
| 11 import 'package:analysis_server/src/analysis_server.dart'; | 11 import 'package:analysis_server/src/analysis_server.dart'; |
| 12 import 'package:analysis_server/src/collections.dart'; | 12 import 'package:analysis_server/src/collections.dart'; |
| 13 import 'package:analysis_server/src/constants.dart'; | 13 import 'package:analysis_server/src/constants.dart'; |
| 14 import 'package:analysis_server/src/protocol_server.dart' hide Element; | 14 import 'package:analysis_server/src/protocol_server.dart' hide Element; |
| 15 import 'package:analysis_server/src/services/correction/assist.dart'; | 15 import 'package:analysis_server/src/services/correction/assist.dart'; |
| 16 import 'package:analysis_server/src/services/correction/fix.dart'; | 16 import 'package:analysis_server/src/services/correction/fix.dart'; |
| 17 import 'package:analysis_server/src/services/correction/organize_directives.dart
'; |
| 17 import 'package:analysis_server/src/services/correction/sort_members.dart'; | 18 import 'package:analysis_server/src/services/correction/sort_members.dart'; |
| 18 import 'package:analysis_server/src/services/correction/status.dart'; | 19 import 'package:analysis_server/src/services/correction/status.dart'; |
| 19 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; | 20 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; |
| 20 import 'package:analysis_server/src/services/search/search_engine.dart'; | 21 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 21 import 'package:analyzer/src/generated/ast.dart'; | 22 import 'package:analyzer/src/generated/ast.dart'; |
| 22 import 'package:analyzer/src/generated/element.dart'; | 23 import 'package:analyzer/src/generated/element.dart'; |
| 23 import 'package:analyzer/src/generated/engine.dart' as engine; | 24 import 'package:analyzer/src/generated/engine.dart' as engine; |
| 24 import 'package:analyzer/src/generated/error.dart' as engine; | 25 import 'package:analyzer/src/generated/error.dart' as engine; |
| 25 import 'package:analyzer/src/generated/parser.dart' as engine; | 26 import 'package:analyzer/src/generated/parser.dart' as engine; |
| 26 import 'package:analyzer/src/generated/scanner.dart' as engine; | 27 import 'package:analyzer/src/generated/scanner.dart' as engine; |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 if (requestName == EDIT_FORMAT) { | 184 if (requestName == EDIT_FORMAT) { |
| 184 return format(request); | 185 return format(request); |
| 185 } else if (requestName == EDIT_GET_ASSISTS) { | 186 } else if (requestName == EDIT_GET_ASSISTS) { |
| 186 return getAssists(request); | 187 return getAssists(request); |
| 187 } else if (requestName == EDIT_GET_AVAILABLE_REFACTORINGS) { | 188 } else if (requestName == EDIT_GET_AVAILABLE_REFACTORINGS) { |
| 188 return _getAvailableRefactorings(request); | 189 return _getAvailableRefactorings(request); |
| 189 } else if (requestName == EDIT_GET_FIXES) { | 190 } else if (requestName == EDIT_GET_FIXES) { |
| 190 return getFixes(request); | 191 return getFixes(request); |
| 191 } else if (requestName == EDIT_GET_REFACTORING) { | 192 } else if (requestName == EDIT_GET_REFACTORING) { |
| 192 return _getRefactoring(request); | 193 return _getRefactoring(request); |
| 194 } else if (requestName == EDIT_ORGANIZE_DIRECTIVES) { |
| 195 return organizeDirectives(request); |
| 193 } else if (requestName == EDIT_SORT_MEMBERS) { | 196 } else if (requestName == EDIT_SORT_MEMBERS) { |
| 194 return sortMembers(request); | 197 return sortMembers(request); |
| 195 } | 198 } |
| 196 } on RequestFailure catch (exception) { | 199 } on RequestFailure catch (exception) { |
| 197 return exception.response; | 200 return exception.response; |
| 198 } | 201 } |
| 199 return null; | 202 return null; |
| 200 } | 203 } |
| 201 | 204 |
| 205 Response organizeDirectives(Request request) { |
| 206 var params = new EditOrganizeDirectivesParams.fromRequest(request); |
| 207 // prepare file |
| 208 String file = params.file; |
| 209 if (!engine.AnalysisEngine.isDartFileName(file)) { |
| 210 return new Response.fileNotAnalyzed(request, file); |
| 211 } |
| 212 // prepare resolved units |
| 213 List<CompilationUnit> units = server.getResolvedCompilationUnits(file); |
| 214 if (units.isEmpty) { |
| 215 return new Response.fileNotAnalyzed(request, file); |
| 216 } |
| 217 // prepare context |
| 218 CompilationUnit unit = units.first; |
| 219 engine.AnalysisContext context = unit.element.context; |
| 220 Source source = unit.element.source; |
| 221 List<engine.AnalysisError> errors = context.computeErrors(source); |
| 222 // check if there are scan/parse errors in the file |
| 223 int numScanParseErrors = _getNumberOfScanParseErrors(errors); |
| 224 if (numScanParseErrors != 0) { |
| 225 return new Response.organizeDirectivesError( |
| 226 request, 'File has $numScanParseErrors scan/parse errors.'); |
| 227 } |
| 228 // do organize |
| 229 int fileStamp = context.getModificationStamp(source); |
| 230 String code = context.getContents(source).data; |
| 231 DirectiveOrganizer sorter = new DirectiveOrganizer(code, unit, errors); |
| 232 List<SourceEdit> edits = sorter.organize(); |
| 233 SourceFileEdit fileEdit = new SourceFileEdit(file, fileStamp, edits: edits); |
| 234 return new EditOrganizeDirectivesResult(fileEdit).toResponse(request.id); |
| 235 } |
| 236 |
| 202 Response sortMembers(Request request) { | 237 Response sortMembers(Request request) { |
| 203 var params = new EditSortMembersParams.fromRequest(request); | 238 var params = new EditSortMembersParams.fromRequest(request); |
| 204 // prepare file | 239 // prepare file |
| 205 String file = params.file; | 240 String file = params.file; |
| 206 if (!engine.AnalysisEngine.isDartFileName(file)) { | 241 if (!engine.AnalysisEngine.isDartFileName(file)) { |
| 207 return new Response.sortMembersInvalidFile(request); | 242 return new Response.sortMembersInvalidFile(request); |
| 208 } | 243 } |
| 209 // prepare resolved units | 244 // prepare resolved units |
| 210 List<CompilationUnit> units = server.getResolvedCompilationUnits(file); | 245 List<CompilationUnit> units = server.getResolvedCompilationUnits(file); |
| 211 if (units.isEmpty) { | 246 if (units.isEmpty) { |
| 212 return new Response.sortMembersInvalidFile(request); | 247 return new Response.sortMembersInvalidFile(request); |
| 213 } | 248 } |
| 214 // prepare context | 249 // prepare context |
| 215 CompilationUnit unit = units.first; | 250 CompilationUnit unit = units.first; |
| 216 engine.AnalysisContext context = unit.element.context; | 251 engine.AnalysisContext context = unit.element.context; |
| 217 Source source = unit.element.source; | 252 Source source = unit.element.source; |
| 218 // check if there are no scan/parse errors in the file | 253 // check if there are scan/parse errors in the file |
| 219 engine.AnalysisErrorInfo errors = context.getErrors(source); | 254 engine.AnalysisErrorInfo errors = context.getErrors(source); |
| 220 int numScanParseErrors = 0; | 255 int numScanParseErrors = _getNumberOfScanParseErrors(errors.errors); |
| 221 errors.errors.forEach((engine.AnalysisError error) { | |
| 222 if (error.errorCode is engine.ScannerErrorCode || | |
| 223 error.errorCode is engine.ParserErrorCode) { | |
| 224 numScanParseErrors++; | |
| 225 } | |
| 226 }); | |
| 227 if (numScanParseErrors != 0) { | 256 if (numScanParseErrors != 0) { |
| 228 return new Response.sortMembersParseErrors(request, numScanParseErrors); | 257 return new Response.sortMembersParseErrors(request, numScanParseErrors); |
| 229 } | 258 } |
| 230 // do sort | 259 // do sort |
| 231 int fileStamp = context.getModificationStamp(source); | 260 int fileStamp = context.getModificationStamp(source); |
| 232 String code = context.getContents(source).data; | 261 String code = context.getContents(source).data; |
| 233 MemberSorter sorter = new MemberSorter(code, unit); | 262 MemberSorter sorter = new MemberSorter(code, unit); |
| 234 List<SourceEdit> edits = sorter.sort(); | 263 List<SourceEdit> edits = sorter.sort(); |
| 235 SourceFileEdit fileEdit = new SourceFileEdit(file, fileStamp, edits: edits); | 264 SourceFileEdit fileEdit = new SourceFileEdit(file, fileStamp, edits: edits); |
| 236 return new EditSortMembersResult(fileEdit).toResponse(request.id); | 265 return new EditSortMembersResult(fileEdit).toResponse(request.id); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 refactoringManager.getRefactoring(request); | 326 refactoringManager.getRefactoring(request); |
| 298 return Response.DELAYED_RESPONSE; | 327 return Response.DELAYED_RESPONSE; |
| 299 } | 328 } |
| 300 | 329 |
| 301 /** | 330 /** |
| 302 * Initializes [refactoringManager] with a new instance. | 331 * Initializes [refactoringManager] with a new instance. |
| 303 */ | 332 */ |
| 304 void _newRefactoringManager() { | 333 void _newRefactoringManager() { |
| 305 refactoringManager = new _RefactoringManager(server, searchEngine); | 334 refactoringManager = new _RefactoringManager(server, searchEngine); |
| 306 } | 335 } |
| 336 |
| 337 static int _getNumberOfScanParseErrors(List<engine.AnalysisError> errors) { |
| 338 int numScanParseErrors = 0; |
| 339 for (engine.AnalysisError error in errors) { |
| 340 if (error.errorCode is engine.ScannerErrorCode || |
| 341 error.errorCode is engine.ParserErrorCode) { |
| 342 numScanParseErrors++; |
| 343 } |
| 344 } |
| 345 return numScanParseErrors; |
| 346 } |
| 307 } | 347 } |
| 308 | 348 |
| 309 /** | 349 /** |
| 310 * An object managing a single [Refactoring] instance. | 350 * An object managing a single [Refactoring] instance. |
| 311 * | 351 * |
| 312 * The instance is identified by its kind, file, offset and length. | 352 * The instance is identified by its kind, file, offset and length. |
| 313 * It is initialized when the a set of parameters is given for the first time. | 353 * It is initialized when the a set of parameters is given for the first time. |
| 314 * All subsequent requests are performed on this [Refactoring] instance. | 354 * All subsequent requests are performed on this [Refactoring] instance. |
| 315 * | 355 * |
| 316 * Once new set of parameters is received, the previous [Refactoring] instance | 356 * Once new set of parameters is received, the previous [Refactoring] instance |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 645 } | 685 } |
| 646 if (refactoring is RenameRefactoring) { | 686 if (refactoring is RenameRefactoring) { |
| 647 RenameRefactoring renameRefactoring = refactoring; | 687 RenameRefactoring renameRefactoring = refactoring; |
| 648 RenameOptions renameOptions = params.options; | 688 RenameOptions renameOptions = params.options; |
| 649 renameRefactoring.newName = renameOptions.newName; | 689 renameRefactoring.newName = renameOptions.newName; |
| 650 return renameRefactoring.checkNewName(); | 690 return renameRefactoring.checkNewName(); |
| 651 } | 691 } |
| 652 return new RefactoringStatus(); | 692 return new RefactoringStatus(); |
| 653 } | 693 } |
| 654 } | 694 } |
| OLD | NEW |