OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, 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 import 'package:analyzer/src/generated/source.dart' show SourceRange; |
| 6 import 'package:analyzer_plugin/protocol/protocol_common.dart'; |
| 7 import 'package:analyzer_plugin/utilities/navigation.dart'; |
| 8 import 'package:analyzer_plugin/utilities/pair.dart'; |
| 9 |
| 10 /** |
| 11 * A concrete implementation of [NavigationCollector]. |
| 12 */ |
| 13 class NavigationCollectorImpl implements NavigationCollector { |
| 14 /** |
| 15 * A list of navigation regions. |
| 16 */ |
| 17 final List<NavigationRegion> regions = <NavigationRegion>[]; |
| 18 final Map<SourceRange, List<int>> regionMap = <SourceRange, List<int>>{}; |
| 19 |
| 20 /** |
| 21 * All the unique targets referenced by [regions]. |
| 22 */ |
| 23 final List<NavigationTarget> targets = <NavigationTarget>[]; |
| 24 final Map<Pair<ElementKind, Location>, int> targetMap = |
| 25 <Pair<ElementKind, Location>, int>{}; |
| 26 |
| 27 /** |
| 28 * All the unique files referenced by [targets]. |
| 29 */ |
| 30 final List<String> files = <String>[]; |
| 31 final Map<String, int> fileMap = <String, int>{}; |
| 32 |
| 33 @override |
| 34 void addRegion( |
| 35 int offset, int length, ElementKind targetKind, Location targetLocation) { |
| 36 SourceRange range = new SourceRange(offset, length); |
| 37 // add new target |
| 38 List<int> targets = regionMap.putIfAbsent(range, () => <int>[]); |
| 39 int targetIndex = _addTarget(targetKind, targetLocation); |
| 40 targets.add(targetIndex); |
| 41 } |
| 42 |
| 43 void createRegions() { |
| 44 regionMap.forEach((range, targets) { |
| 45 NavigationRegion region = |
| 46 new NavigationRegion(range.offset, range.length, targets); |
| 47 regions.add(region); |
| 48 }); |
| 49 regions.sort((NavigationRegion first, NavigationRegion second) { |
| 50 return first.offset - second.offset; |
| 51 }); |
| 52 } |
| 53 |
| 54 int _addFile(String file) { |
| 55 int index = fileMap[file]; |
| 56 if (index == null) { |
| 57 index = files.length; |
| 58 files.add(file); |
| 59 fileMap[file] = index; |
| 60 } |
| 61 return index; |
| 62 } |
| 63 |
| 64 int _addTarget(ElementKind kind, Location location) { |
| 65 var pair = new Pair<ElementKind, Location>(kind, location); |
| 66 int index = targetMap[pair]; |
| 67 if (index == null) { |
| 68 String file = location.file; |
| 69 int fileIndex = _addFile(file); |
| 70 index = targets.length; |
| 71 NavigationTarget target = new NavigationTarget( |
| 72 kind, |
| 73 fileIndex, |
| 74 location.offset, |
| 75 location.length, |
| 76 location.startLine, |
| 77 location.startColumn); |
| 78 targets.add(target); |
| 79 targetMap[pair] = index; |
| 80 } |
| 81 return index; |
| 82 } |
| 83 } |
OLD | NEW |