| 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' show SourceChange, | 9 import 'package:analysis_server/src/protocol.dart' show SourceChange, |
| 10 SourceEdit; | 10 SourceEdit; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 for (List list in lists) { | 26 for (List list in lists) { |
| 27 if (list[position] != element) { | 27 if (list[position] != element) { |
| 28 return false; | 28 return false; |
| 29 } | 29 } |
| 30 } | 30 } |
| 31 return true; | 31 return true; |
| 32 } | 32 } |
| 33 | 33 |
| 34 | 34 |
| 35 /** | 35 /** |
| 36 * Climbs up [PrefixedIdentifier] and [ProperyAccess] nodes that include [node]. |
| 37 */ |
| 38 Expression climbPropertyAccess(AstNode node) { |
| 39 while (true) { |
| 40 AstNode parent = node.parent; |
| 41 if (parent is PrefixedIdentifier && parent.identifier == node) { |
| 42 node = parent; |
| 43 continue; |
| 44 } |
| 45 if (parent is PropertyAccess && parent.propertyName == node) { |
| 46 node = parent; |
| 47 continue; |
| 48 } |
| 49 return node; |
| 50 } |
| 51 } |
| 52 |
| 53 |
| 54 /** |
| 36 * TODO(scheglov) replace with nodes once there will be [CompilationUnit#getComm
ents]. | 55 * TODO(scheglov) replace with nodes once there will be [CompilationUnit#getComm
ents]. |
| 37 * | 56 * |
| 38 * Returns [SourceRange]s of all comments in [unit]. | 57 * Returns [SourceRange]s of all comments in [unit]. |
| 39 */ | 58 */ |
| 40 List<SourceRange> getCommentRanges(CompilationUnit unit) { | 59 List<SourceRange> getCommentRanges(CompilationUnit unit) { |
| 41 List<SourceRange> ranges = <SourceRange>[]; | 60 List<SourceRange> ranges = <SourceRange>[]; |
| 42 Token token = unit.beginToken; | 61 Token token = unit.beginToken; |
| 43 while (token != null && token.type != TokenType.EOF) { | 62 while (token != null && token.type != TokenType.EOF) { |
| 44 Token commentToken = token.precedingComments; | 63 Token commentToken = token.precedingComments; |
| 45 while (commentToken != null) { | 64 while (commentToken != null) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 66 } | 85 } |
| 67 if (typeName == "String") { | 86 if (typeName == "String") { |
| 68 return "''"; | 87 return "''"; |
| 69 } | 88 } |
| 70 } | 89 } |
| 71 // no better guess | 90 // no better guess |
| 72 return "null"; | 91 return "null"; |
| 73 } | 92 } |
| 74 | 93 |
| 75 | 94 |
| 95 |
| 76 /** | 96 /** |
| 77 * Return the name of the [Element] kind. | 97 * Return the name of the [Element] kind. |
| 78 */ | 98 */ |
| 79 String getElementKindName(Element element) { | 99 String getElementKindName(Element element) { |
| 80 return element.kind.displayName; | 100 return element.kind.displayName; |
| 81 } | 101 } |
| 82 | 102 |
| 83 | |
| 84 | |
| 85 /** | 103 /** |
| 86 * Returns the name to display in the UI for the given [Element]. | 104 * Returns the name to display in the UI for the given [Element]. |
| 87 */ | 105 */ |
| 88 String getElementQualifiedName(Element element) { | 106 String getElementQualifiedName(Element element) { |
| 89 ElementKind kind = element.kind; | 107 ElementKind kind = element.kind; |
| 90 if (kind == ElementKind.FIELD || kind == ElementKind.METHOD) { | 108 if (kind == ElementKind.FIELD || kind == ElementKind.METHOD) { |
| 91 return '${element.enclosingElement.displayName}.${element.displayName}'; | 109 return '${element.enclosingElement.displayName}.${element.displayName}'; |
| 92 } else { | 110 } else { |
| 93 return element.displayName; | 111 return element.displayName; |
| 94 } | 112 } |
| 95 } | 113 } |
| 96 | 114 |
| 115 |
| 97 /** | 116 /** |
| 98 * If the given [AstNode] is in a [ClassDeclaration], returns the | 117 * If the given [AstNode] is in a [ClassDeclaration], returns the |
| 99 * [ClassElement]. Otherwise returns `null`. | 118 * [ClassElement]. Otherwise returns `null`. |
| 100 */ | 119 */ |
| 101 ClassElement getEnclosingClassElement(AstNode node) { | 120 ClassElement getEnclosingClassElement(AstNode node) { |
| 102 ClassDeclaration enclosingClassNode = | 121 ClassDeclaration enclosingClassNode = |
| 103 node.getAncestor((node) => node is ClassDeclaration); | 122 node.getAncestor((node) => node is ClassDeclaration); |
| 104 if (enclosingClassNode != null) { | 123 if (enclosingClassNode != null) { |
| 105 return enclosingClassNode.element; | 124 return enclosingClassNode.element; |
| 106 } | 125 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 } | 179 } |
| 161 if (node is MethodDeclaration) { | 180 if (node is MethodDeclaration) { |
| 162 return node; | 181 return node; |
| 163 } | 182 } |
| 164 node = node.parent; | 183 node = node.parent; |
| 165 } | 184 } |
| 166 return null; | 185 return null; |
| 167 } | 186 } |
| 168 | 187 |
| 169 | 188 |
| 170 | |
| 171 /** | 189 /** |
| 172 * Returns [getExpressionPrecedence] for the parent of [node], | 190 * Returns [getExpressionPrecedence] for the parent of [node], |
| 173 * or `0` if the parent node is [ParenthesizedExpression]. | 191 * or `0` if the parent node is [ParenthesizedExpression]. |
| 174 * | 192 * |
| 175 * The reason is that `(expr)` is always executed after `expr`. | 193 * The reason is that `(expr)` is always executed after `expr`. |
| 176 */ | 194 */ |
| 177 int getExpressionParentPrecedence(AstNode node) { | 195 int getExpressionParentPrecedence(AstNode node) { |
| 178 AstNode parent = node.parent; | 196 AstNode parent = node.parent; |
| 179 if (parent is ParenthesizedExpression) { | 197 if (parent is ParenthesizedExpression) { |
| 180 return 0; | 198 return 0; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 196 | 214 |
| 197 /** | 215 /** |
| 198 * Returns the namespace of the given [ImportElement]. | 216 * Returns the namespace of the given [ImportElement]. |
| 199 */ | 217 */ |
| 200 Map<String, Element> getImportNamespace(ImportElement imp) { | 218 Map<String, Element> getImportNamespace(ImportElement imp) { |
| 201 NamespaceBuilder builder = new NamespaceBuilder(); | 219 NamespaceBuilder builder = new NamespaceBuilder(); |
| 202 Namespace namespace = builder.createImportNamespaceForDirective(imp); | 220 Namespace namespace = builder.createImportNamespaceForDirective(imp); |
| 203 return namespace.definedNames; | 221 return namespace.definedNames; |
| 204 } | 222 } |
| 205 | 223 |
| 206 | |
| 207 /** | 224 /** |
| 208 * Returns the line prefix from the given source, i.e. basically just a | 225 * Returns the line prefix from the given source, i.e. basically just a |
| 209 * whitespace prefix of the given [String]. | 226 * whitespace prefix of the given [String]. |
| 210 */ | 227 */ |
| 211 String getLinePrefix(String line) { | 228 String getLinePrefix(String line) { |
| 212 int index = 0; | 229 int index = 0; |
| 213 while (index < line.length) { | 230 while (index < line.length) { |
| 214 int c = line.codeUnitAt(index); | 231 int c = line.codeUnitAt(index); |
| 215 if (!isWhitespace(c)) { | 232 if (!isWhitespace(c)) { |
| 216 break; | 233 break; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 229 Element element = node.staticElement; | 246 Element element = node.staticElement; |
| 230 if (element is LocalVariableElement) { | 247 if (element is LocalVariableElement) { |
| 231 return element; | 248 return element; |
| 232 } | 249 } |
| 233 if (element is ParameterElement) { | 250 if (element is ParameterElement) { |
| 234 return element; | 251 return element; |
| 235 } | 252 } |
| 236 return null; | 253 return null; |
| 237 } | 254 } |
| 238 | 255 |
| 256 |
| 239 /** | 257 /** |
| 240 * @return the [LocalVariableElement] if given [SimpleIdentifier] is the referen
ce to | 258 * @return the [LocalVariableElement] if given [SimpleIdentifier] is the referen
ce to |
| 241 * local variable, or <code>null</code> in the other case. | 259 * local variable, or <code>null</code> in the other case. |
| 242 */ | 260 */ |
| 243 LocalVariableElement getLocalVariableElement(SimpleIdentifier node) { | 261 LocalVariableElement getLocalVariableElement(SimpleIdentifier node) { |
| 244 Element element = node.staticElement; | 262 Element element = node.staticElement; |
| 245 if (element is LocalVariableElement) { | 263 if (element is LocalVariableElement) { |
| 246 return element; | 264 return element; |
| 247 } | 265 } |
| 248 return null; | 266 return null; |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 if (statement is Block) { | 399 if (statement is Block) { |
| 382 List<Statement> blockStatements = statement.statements; | 400 List<Statement> blockStatements = statement.statements; |
| 383 if (blockStatements.length != 1) { | 401 if (blockStatements.length != 1) { |
| 384 return null; | 402 return null; |
| 385 } | 403 } |
| 386 return blockStatements[0]; | 404 return blockStatements[0]; |
| 387 } | 405 } |
| 388 return statement; | 406 return statement; |
| 389 } | 407 } |
| 390 | 408 |
| 391 | |
| 392 /** | 409 /** |
| 393 * Returns the [String] content of the given [Source]. | 410 * Returns the [String] content of the given [Source]. |
| 394 */ | 411 */ |
| 395 String getSourceContent(AnalysisContext context, Source source) { | 412 String getSourceContent(AnalysisContext context, Source source) { |
| 396 return context.getContents(source).data; | 413 return context.getContents(source).data; |
| 397 } | 414 } |
| 398 | 415 |
| 416 |
| 399 /** | 417 /** |
| 400 * Returns the given [Statement] if not a [Block], or all the children | 418 * Returns the given [Statement] if not a [Block], or all the children |
| 401 * [Statement]s if a [Block]. | 419 * [Statement]s if a [Block]. |
| 402 */ | 420 */ |
| 403 List<Statement> getStatements(Statement statement) { | 421 List<Statement> getStatements(Statement statement) { |
| 404 if (statement is Block) { | 422 if (statement is Block) { |
| 405 return statement.statements; | 423 return statement.statements; |
| 406 } | 424 } |
| 407 return [statement]; | 425 return [statement]; |
| 408 } | 426 } |
| (...skipping 974 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1383 | 1401 |
| 1384 @override | 1402 @override |
| 1385 Object visitExpression(Expression node) { | 1403 Object visitExpression(Expression node) { |
| 1386 if (node is BinaryExpression && node.operator.type == groupOperatorType) { | 1404 if (node is BinaryExpression && node.operator.type == groupOperatorType) { |
| 1387 return super.visitNode(node); | 1405 return super.visitNode(node); |
| 1388 } | 1406 } |
| 1389 operands.add(node); | 1407 operands.add(node); |
| 1390 return null; | 1408 return null; |
| 1391 } | 1409 } |
| 1392 } | 1410 } |
| OLD | NEW |