| 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 computer.occurrences; | 5 library computer.occurrences; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/collections.dart'; |
| 9 import 'package:analysis_server/src/computer/element.dart'; | 10 import 'package:analysis_server/src/computer/element.dart'; |
| 10 import 'package:analysis_server/src/constants.dart'; | 11 import 'package:analysis_server/src/constants.dart'; |
| 11 import 'package:analyzer/src/generated/ast.dart'; | 12 import 'package:analyzer/src/generated/ast.dart'; |
| 12 import 'package:analyzer/src/generated/element.dart' as engine; | 13 import 'package:analyzer/src/generated/element.dart' as engine; |
| 13 | 14 |
| 14 | 15 |
| 15 /** | 16 /** |
| 16 * A computer for elements occurrences in a Dart [CompilationUnit]. | 17 * A computer for elements occurrences in a Dart [CompilationUnit]. |
| 17 */ | 18 */ |
| 18 class DartUnitOccurrencesComputer { | 19 class DartUnitOccurrencesComputer { |
| 19 final CompilationUnit _unit; | 20 final CompilationUnit _unit; |
| 20 | 21 |
| 21 final Map<engine.Element, List<int>> _elementsOffsets = | 22 final Map<engine.Element, List<int>> _elementsOffsets = |
| 22 new HashMap<engine.Element, List<int>>(); | 23 new HashMap<engine.Element, List<int>>(); |
| 23 | 24 |
| 24 DartUnitOccurrencesComputer(this._unit); | 25 DartUnitOccurrencesComputer(this._unit); |
| 25 | 26 |
| 26 /** | 27 /** |
| 27 * Returns the computed occurrences, not `null`. | 28 * Returns the computed occurrences, not `null`. |
| 28 */ | 29 */ |
| 29 List<Map<String, Object>> compute() { | 30 List<Occurrences> compute() { |
| 30 _unit.accept(new _DartUnitOccurrencesComputerVisitor(this)); | 31 _unit.accept(new _DartUnitOccurrencesComputerVisitor(this)); |
| 31 List<Occurrences> occurrences = <Occurrences>[]; | 32 List<Occurrences> occurrences = <Occurrences>[]; |
| 32 _elementsOffsets.forEach((engineElement, offsets) { | 33 _elementsOffsets.forEach((engineElement, offsets) { |
| 33 Element serverElement = new Element.fromEngine(engineElement); | 34 Element serverElement = new Element.fromEngine(engineElement); |
| 34 int length = engineElement.displayName.length; | 35 int length = engineElement.displayName.length; |
| 35 occurrences.add(new Occurrences(serverElement, offsets, length)); | 36 occurrences.add(new Occurrences(serverElement, offsets, length)); |
| 36 }); | 37 }); |
| 37 return occurrences.map((occurrences) => occurrences.toJson()).toList(); | 38 return occurrences; |
| 38 } | 39 } |
| 39 | 40 |
| 40 void _addOccurrence(engine.Element element, int offset) { | 41 void _addOccurrence(engine.Element element, int offset) { |
| 41 List<int> offsets = _elementsOffsets[element]; | 42 List<int> offsets = _elementsOffsets[element]; |
| 42 if (offsets == null) { | 43 if (offsets == null) { |
| 43 offsets = <int>[]; | 44 offsets = <int>[]; |
| 44 _elementsOffsets[element] = offsets; | 45 _elementsOffsets[element] = offsets; |
| 45 } | 46 } |
| 46 offsets.add(offset); | 47 offsets.add(offset); |
| 47 } | 48 } |
| 48 } | 49 } |
| 49 | 50 |
| 50 | 51 |
| 51 class Occurrences { | 52 class Occurrences implements HasToJson { |
| 52 final Element element; | 53 final Element element; |
| 53 final List<int> offsets; | 54 final List<int> offsets; |
| 54 final int length; | 55 final int length; |
| 55 | 56 |
| 56 Occurrences(this.element, this.offsets, this.length); | 57 Occurrences(this.element, this.offsets, this.length); |
| 57 | 58 |
| 58 factory Occurrences.fromJson(Map<String, Object> map) { | 59 factory Occurrences.fromJson(Map<String, Object> map) { |
| 59 Element element = new Element.fromJson(map[ELEMENT]); | 60 Element element = new Element.fromJson(map[ELEMENT]); |
| 60 List<int> offsets = map[OFFSETS]; | 61 List<int> offsets = map[OFFSETS]; |
| 61 int length = map[LENGTH]; | 62 int length = map[LENGTH]; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 82 | 83 |
| 83 @override | 84 @override |
| 84 visitSimpleIdentifier(SimpleIdentifier node) { | 85 visitSimpleIdentifier(SimpleIdentifier node) { |
| 85 engine.Element element = node.bestElement; | 86 engine.Element element = node.bestElement; |
| 86 if (element != null) { | 87 if (element != null) { |
| 87 computer._addOccurrence(element, node.offset); | 88 computer._addOccurrence(element, node.offset); |
| 88 } | 89 } |
| 89 return super.visitSimpleIdentifier(node); | 90 return super.visitSimpleIdentifier(node); |
| 90 } | 91 } |
| 91 } | 92 } |
| OLD | NEW |