| 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 services.src.correction.util; | 5 library services.src.correction.util; |
| 6 | 6 |
| 7 import 'dart:math'; | 7 import 'dart:math'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/protocol.dart' | 9 import 'package:analysis_server/src/protocol.dart' |
| 10 show SourceChange, SourceEdit; | 10 show SourceChange, SourceEdit; |
| 11 import 'package:analysis_server/src/protocol_server.dart' | 11 import 'package:analysis_server/src/protocol_server.dart' |
| 12 show doSourceChange_addElementEdit; | 12 show doSourceChange_addElementEdit; |
| 13 import 'package:analysis_server/src/services/correction/source_range.dart'; | 13 import 'package:analysis_server/src/services/correction/source_range.dart'; |
| 14 import 'package:analysis_server/src/services/correction/strings.dart'; | 14 import 'package:analysis_server/src/services/correction/strings.dart'; |
| 15 import 'package:analysis_server/src/services/search/element_visitors.dart'; | 15 import 'package:analysis_server/src/services/search/element_visitors.dart'; |
| 16 import 'package:analyzer/src/generated/ast.dart'; | 16 import 'package:analyzer/src/generated/ast.dart'; |
| 17 import 'package:analyzer/src/generated/element.dart'; | 17 import 'package:analyzer/src/generated/element.dart'; |
| 18 import 'package:analyzer/src/generated/engine.dart'; | 18 import 'package:analyzer/src/generated/engine.dart'; |
| 19 import 'package:analyzer/src/generated/resolver.dart'; | 19 import 'package:analyzer/src/generated/resolver.dart'; |
| 20 import 'package:analyzer/src/generated/scanner.dart'; | 20 import 'package:analyzer/src/generated/scanner.dart'; |
| 21 import 'package:analyzer/src/generated/source.dart'; | 21 import 'package:analyzer/src/generated/source.dart'; |
| 22 import 'package:path/path.dart'; | 22 import 'package:path/path.dart'; |
| 23 | 23 |
| 24 /** | 24 /** |
| 25 * Adds edits to the given [change] that ensure that all the [libraries] are | 25 * Adds edits to the given [change] that ensure that all the [libraries] are |
| 26 * imported into the given [targetLibrary]. | 26 * imported into the given [targetLibrary]. |
| 27 */ | 27 */ |
| 28 void addLibraryImports(SourceChange change, LibraryElement targetLibrary, | 28 void addLibraryImports(SourceChange change, LibraryElement targetLibrary, |
| 29 Set<LibraryElement> libraries) { | 29 Set<LibraryElement> libraries) { |
| 30 CompilationUnit libUnit = targetLibrary.definingCompilationUnit.node; | 30 CompilationUnitElement libUnitElement = targetLibrary.definingCompilationUnit; |
| 31 CompilationUnit libUnit = getParsedUnit(libUnitElement); |
| 31 // prepare new import location | 32 // prepare new import location |
| 32 int offset = 0; | 33 int offset = 0; |
| 33 String prefix; | 34 String prefix; |
| 34 String suffix; | 35 String suffix; |
| 35 { | 36 { |
| 36 // if no directives | 37 // if no directives |
| 37 prefix = ''; | 38 prefix = ''; |
| 38 CorrectionUtils libraryUtils = new CorrectionUtils(libUnit); | 39 CorrectionUtils libraryUtils = new CorrectionUtils(libUnit); |
| 39 String eol = libraryUtils.endOfLine; | 40 String eol = libraryUtils.endOfLine; |
| 40 suffix = eol; | 41 suffix = eol; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 String findAbsoluteUri(AnalysisContext context, String path) { | 107 String findAbsoluteUri(AnalysisContext context, String path) { |
| 107 Source fileSource = new NonExistingSource(path, UriKind.FILE_URI); | 108 Source fileSource = new NonExistingSource(path, UriKind.FILE_URI); |
| 108 Uri uri = context.sourceFactory.restoreUri(fileSource); | 109 Uri uri = context.sourceFactory.restoreUri(fileSource); |
| 109 if (uri == null) { | 110 if (uri == null) { |
| 110 return null; | 111 return null; |
| 111 } | 112 } |
| 112 return uri.toString(); | 113 return uri.toString(); |
| 113 } | 114 } |
| 114 | 115 |
| 115 /** | 116 /** |
| 117 * Returns the EOL to use for the given [code]. |
| 118 */ |
| 119 String getCodeEndOfLine(String code) { |
| 120 if (code.contains('\r\n')) { |
| 121 return '\r\n'; |
| 122 } |
| 123 return '\n'; |
| 124 } |
| 125 |
| 126 /** |
| 116 * TODO(scheglov) replace with nodes once there will be [CompilationUnit.getComm
ents]. | 127 * TODO(scheglov) replace with nodes once there will be [CompilationUnit.getComm
ents]. |
| 117 * | 128 * |
| 118 * Returns [SourceRange]s of all comments in [unit]. | 129 * Returns [SourceRange]s of all comments in [unit]. |
| 119 */ | 130 */ |
| 120 List<SourceRange> getCommentRanges(CompilationUnit unit) { | 131 List<SourceRange> getCommentRanges(CompilationUnit unit) { |
| 121 List<SourceRange> ranges = <SourceRange>[]; | 132 List<SourceRange> ranges = <SourceRange>[]; |
| 122 Token token = unit.beginToken; | 133 Token token = unit.beginToken; |
| 123 while (token != null && token.type != TokenType.EOF) { | 134 while (token != null && token.type != TokenType.EOF) { |
| 124 Token commentToken = token.precedingComments; | 135 Token commentToken = token.precedingComments; |
| 125 while (commentToken != null) { | 136 while (commentToken != null) { |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 AstNode current = node.parent; | 423 AstNode current = node.parent; |
| 413 int index = numParents; | 424 int index = numParents; |
| 414 while (current != null) { | 425 while (current != null) { |
| 415 parents[--index] = current; | 426 parents[--index] = current; |
| 416 current = current.parent; | 427 current = current.parent; |
| 417 } | 428 } |
| 418 return parents; | 429 return parents; |
| 419 } | 430 } |
| 420 | 431 |
| 421 /** | 432 /** |
| 433 * Returns a parsed [AstNode] for the given [classElement]. |
| 434 * |
| 435 * The resulting AST structure may or may not be resolved. |
| 436 */ |
| 437 AstNode getParsedClassElementNode(ClassElement classElement) { |
| 438 CompilationUnitElement unitElement = |
| 439 classElement.getAncestor((e) => e is CompilationUnitElement); |
| 440 CompilationUnit unit = getParsedUnit(unitElement); |
| 441 int offset = classElement.nameOffset; |
| 442 AstNode classNameNode = new NodeLocator.con1(offset).searchWithin(unit); |
| 443 if (classElement.isEnum) { |
| 444 return classNameNode.getAncestor((node) => node is EnumDeclaration); |
| 445 } else { |
| 446 return classNameNode.getAncestor( |
| 447 (node) => node is ClassDeclaration || node is ClassTypeAlias); |
| 448 } |
| 449 } |
| 450 |
| 451 /** |
| 452 * Returns a parsed [CompilationUnit] for the given [unitElement]. |
| 453 * |
| 454 * The resulting AST structure may or may not be resolved. |
| 455 * If it is not resolved, then at least the given [unitElement] will be set. |
| 456 */ |
| 457 CompilationUnit getParsedUnit(CompilationUnitElement unitElement) { |
| 458 AnalysisContext context = unitElement.context; |
| 459 Source source = unitElement.source; |
| 460 CompilationUnit unit = context.parseCompilationUnit(source); |
| 461 if (unit.element == null) { |
| 462 unit.element = unitElement; |
| 463 } |
| 464 return unit; |
| 465 } |
| 466 |
| 467 /** |
| 422 * Returns a [PropertyAccessorElement] if the given [SimpleIdentifier] is a | 468 * Returns a [PropertyAccessorElement] if the given [SimpleIdentifier] is a |
| 423 * reference to a property, or `null` in the other case. | 469 * reference to a property, or `null` in the other case. |
| 424 */ | 470 */ |
| 425 PropertyAccessorElement getPropertyAccessorElement(SimpleIdentifier node) { | 471 PropertyAccessorElement getPropertyAccessorElement(SimpleIdentifier node) { |
| 426 Element element = node.staticElement; | 472 Element element = node.staticElement; |
| 427 if (element is PropertyAccessorElement) { | 473 if (element is PropertyAccessorElement) { |
| 428 return element; | 474 return element; |
| 429 } | 475 } |
| 430 return null; | 476 return null; |
| 431 } | 477 } |
| (...skipping 1078 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1510 | 1556 |
| 1511 @override | 1557 @override |
| 1512 Object visitExpression(Expression node) { | 1558 Object visitExpression(Expression node) { |
| 1513 if (node is BinaryExpression && node.operator.type == groupOperatorType) { | 1559 if (node is BinaryExpression && node.operator.type == groupOperatorType) { |
| 1514 return super.visitNode(node); | 1560 return super.visitNode(node); |
| 1515 } | 1561 } |
| 1516 operands.add(node); | 1562 operands.add(node); |
| 1517 return null; | 1563 return null; |
| 1518 } | 1564 } |
| 1519 } | 1565 } |
| OLD | NEW |