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