OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library computer.occurrences; |
| 6 |
| 7 import 'dart:collection'; |
| 8 |
| 9 import 'package:analysis_server/src/computer/element.dart'; |
| 10 import 'package:analysis_server/src/constants.dart'; |
| 11 import 'package:analyzer/src/generated/ast.dart'; |
| 12 import 'package:analyzer/src/generated/element.dart' as engine; |
| 13 |
| 14 |
| 15 /** |
| 16 * A computer for elements occurrences in a Dart [CompilationUnit]. |
| 17 */ |
| 18 class DartUnitOccurrencesComputer { |
| 19 final CompilationUnit _unit; |
| 20 |
| 21 final Map<engine.Element, List<int>> _elementsOffsets = |
| 22 new HashMap<engine.Element, List<int>>(); |
| 23 |
| 24 DartUnitOccurrencesComputer(this._unit); |
| 25 |
| 26 /** |
| 27 * Returns the computed occurrences, not `null`. |
| 28 */ |
| 29 List<Map<String, Object>> compute() { |
| 30 _unit.accept(new _DartUnitOccurrencesComputerVisitor(this)); |
| 31 List<Occurrences> occurrences = <Occurrences>[]; |
| 32 _elementsOffsets.forEach((engineElement, offsets) { |
| 33 Element serverElement = new Element.fromEngine(engineElement); |
| 34 int length = engineElement.displayName.length; |
| 35 occurrences.add(new Occurrences(serverElement, offsets, length)); |
| 36 }); |
| 37 return occurrences.map((occurrences) => occurrences.toJson()).toList(); |
| 38 } |
| 39 |
| 40 void _addOccurrence(engine.Element element, int offset) { |
| 41 List<int> offsets = _elementsOffsets[element]; |
| 42 if (offsets == null) { |
| 43 offsets = <int>[]; |
| 44 _elementsOffsets[element] = offsets; |
| 45 } |
| 46 offsets.add(offset); |
| 47 } |
| 48 } |
| 49 |
| 50 |
| 51 class Occurrences { |
| 52 final Element element; |
| 53 final List<int> offsets; |
| 54 final int length; |
| 55 |
| 56 Occurrences(this.element, this.offsets, this.length); |
| 57 |
| 58 factory Occurrences.fromJson(Map<String, Object> map) { |
| 59 Element element = new Element.fromJson(map[ELEMENT]); |
| 60 List<int> offsets = map[OFFSETS]; |
| 61 int length = map[LENGTH]; |
| 62 return new Occurrences(element, offsets, length); |
| 63 } |
| 64 |
| 65 Map<String, Object> toJson() { |
| 66 Map<String, Object> json = new HashMap<String, Object>(); |
| 67 json[ELEMENT] = element.toJson(); |
| 68 json[OFFSETS] = offsets; |
| 69 json[LENGTH] = length; |
| 70 return json; |
| 71 } |
| 72 |
| 73 @override |
| 74 String toString() => toJson().toString(); |
| 75 } |
| 76 |
| 77 |
| 78 class _DartUnitOccurrencesComputerVisitor extends RecursiveAstVisitor { |
| 79 final DartUnitOccurrencesComputer computer; |
| 80 |
| 81 _DartUnitOccurrencesComputerVisitor(this.computer); |
| 82 |
| 83 @override |
| 84 visitSimpleIdentifier(SimpleIdentifier node) { |
| 85 engine.Element element = node.bestElement; |
| 86 if (element != null) { |
| 87 computer._addOccurrence(element, node.offset); |
| 88 } |
| 89 return super.visitSimpleIdentifier(node); |
| 90 } |
| 91 } |
OLD | NEW |