| 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/collections.dart'; |
| 10 import 'package:analysis_server/src/computer/element.dart'; | 10 import 'package:analysis_server/src/computer/element.dart' as server; |
| 11 import 'package:analysis_server/src/constants.dart'; | 11 import 'package:analysis_server/src/constants.dart'; |
| 12 import 'package:analyzer/src/generated/ast.dart'; | 12 import 'package:analyzer/src/generated/ast.dart'; |
| 13 import 'package:analyzer/src/generated/element.dart' as engine; | 13 import 'package:analyzer/src/generated/element.dart'; |
| 14 | 14 |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * A computer for elements occurrences in a Dart [CompilationUnit]. | 17 * A computer for elements occurrences in a Dart [CompilationUnit]. |
| 18 */ | 18 */ |
| 19 class DartUnitOccurrencesComputer { | 19 class DartUnitOccurrencesComputer { |
| 20 final CompilationUnit _unit; | 20 final CompilationUnit _unit; |
| 21 | 21 |
| 22 final Map<engine.Element, List<int>> _elementsOffsets = | 22 final Map<Element, List<int>> _elementsOffsets = |
| 23 new HashMap<engine.Element, List<int>>(); | 23 new HashMap<Element, List<int>>(); |
| 24 | 24 |
| 25 DartUnitOccurrencesComputer(this._unit); | 25 DartUnitOccurrencesComputer(this._unit); |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * Returns the computed occurrences, not `null`. | 28 * Returns the computed occurrences, not `null`. |
| 29 */ | 29 */ |
| 30 List<Occurrences> compute() { | 30 List<Occurrences> compute() { |
| 31 _unit.accept(new _DartUnitOccurrencesComputerVisitor(this)); | 31 _unit.accept(new _DartUnitOccurrencesComputerVisitor(this)); |
| 32 List<Occurrences> occurrences = <Occurrences>[]; | 32 List<Occurrences> occurrences = <Occurrences>[]; |
| 33 _elementsOffsets.forEach((engineElement, offsets) { | 33 _elementsOffsets.forEach((engineElement, offsets) { |
| 34 Element serverElement = new Element.fromEngine(engineElement); | 34 var serverElement = new server.Element.fromEngine(engineElement); |
| 35 int length = engineElement.displayName.length; | 35 var length = engineElement.displayName.length; |
| 36 occurrences.add(new Occurrences(serverElement, offsets, length)); | 36 occurrences.add(new Occurrences(serverElement, offsets, length)); |
| 37 }); | 37 }); |
| 38 return occurrences; | 38 return occurrences; |
| 39 } | 39 } |
| 40 | 40 |
| 41 void _addOccurrence(engine.Element element, int offset) { | 41 void _addOccurrence(Element element, int offset) { |
| 42 element = _canonicalizeElement(element); |
| 42 List<int> offsets = _elementsOffsets[element]; | 43 List<int> offsets = _elementsOffsets[element]; |
| 43 if (offsets == null) { | 44 if (offsets == null) { |
| 44 offsets = <int>[]; | 45 offsets = <int>[]; |
| 45 _elementsOffsets[element] = offsets; | 46 _elementsOffsets[element] = offsets; |
| 46 } | 47 } |
| 47 offsets.add(offset); | 48 offsets.add(offset); |
| 48 } | 49 } |
| 50 |
| 51 Element _canonicalizeElement(Element element) { |
| 52 if (element is PropertyAccessorElement) { |
| 53 element = (element as PropertyAccessorElement).variable; |
| 54 } |
| 55 if (element is Member) { |
| 56 element = (element as Member).baseElement; |
| 57 } |
| 58 if (element is FieldFormalParameterElement) { |
| 59 element = (element as FieldFormalParameterElement).field; |
| 60 } |
| 61 return element; |
| 62 } |
| 49 } | 63 } |
| 50 | 64 |
| 51 | 65 |
| 52 class Occurrences implements HasToJson { | 66 class Occurrences implements HasToJson { |
| 53 final Element element; | 67 final server.Element element; |
| 54 final List<int> offsets; | 68 final List<int> offsets; |
| 55 final int length; | 69 final int length; |
| 56 | 70 |
| 57 Occurrences(this.element, this.offsets, this.length); | 71 Occurrences(this.element, this.offsets, this.length); |
| 58 | 72 |
| 59 factory Occurrences.fromJson(Map<String, Object> map) { | 73 factory Occurrences.fromJson(Map<String, Object> map) { |
| 60 Element element = new Element.fromJson(map[ELEMENT]); | 74 server.Element element = new server.Element.fromJson(map[ELEMENT]); |
| 61 List<int> offsets = map[OFFSETS]; | 75 List<int> offsets = map[OFFSETS]; |
| 62 int length = map[LENGTH]; | 76 int length = map[LENGTH]; |
| 63 return new Occurrences(element, offsets, length); | 77 return new Occurrences(element, offsets, length); |
| 64 } | 78 } |
| 65 | 79 |
| 66 Map<String, Object> toJson() { | 80 Map<String, Object> toJson() { |
| 67 Map<String, Object> json = new HashMap<String, Object>(); | 81 Map<String, Object> json = new HashMap<String, Object>(); |
| 68 json[ELEMENT] = element.toJson(); | 82 json[ELEMENT] = element.toJson(); |
| 69 json[OFFSETS] = offsets; | 83 json[OFFSETS] = offsets; |
| 70 json[LENGTH] = length; | 84 json[LENGTH] = length; |
| 71 return json; | 85 return json; |
| 72 } | 86 } |
| 73 | 87 |
| 74 @override | 88 @override |
| 75 String toString() => toJson().toString(); | 89 String toString() => toJson().toString(); |
| 76 } | 90 } |
| 77 | 91 |
| 78 | 92 |
| 79 class _DartUnitOccurrencesComputerVisitor extends RecursiveAstVisitor { | 93 class _DartUnitOccurrencesComputerVisitor extends RecursiveAstVisitor { |
| 80 final DartUnitOccurrencesComputer computer; | 94 final DartUnitOccurrencesComputer computer; |
| 81 | 95 |
| 82 _DartUnitOccurrencesComputerVisitor(this.computer); | 96 _DartUnitOccurrencesComputerVisitor(this.computer); |
| 83 | 97 |
| 84 @override | 98 @override |
| 85 visitSimpleIdentifier(SimpleIdentifier node) { | 99 visitSimpleIdentifier(SimpleIdentifier node) { |
| 86 engine.Element element = node.bestElement; | 100 Element element = node.bestElement; |
| 87 if (element != null) { | 101 if (element != null) { |
| 88 computer._addOccurrence(element, node.offset); | 102 computer._addOccurrence(element, node.offset); |
| 89 } | 103 } |
| 90 return super.visitSimpleIdentifier(node); | 104 return super.visitSimpleIdentifier(node); |
| 91 } | 105 } |
| 92 } | 106 } |
| OLD | NEW |