| 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 domains.analysis.navigation; |
| 6 |
| 7 import 'dart:collection'; |
| 8 |
| 9 import 'package:analysis_server/analysis/navigation/navigation_core.dart'; |
| 10 import 'package:analysis_server/src/analysis_server.dart'; |
| 11 import 'package:analysis_server/src/collections.dart'; |
| 12 import 'package:analysis_server/src/protocol_server.dart' as protocol; |
| 13 import 'package:analyzer/src/generated/engine.dart' |
| 14 show AnalysisContext, AnalysisEngine; |
| 15 import 'package:analyzer/src/generated/java_engine.dart' show CaughtException; |
| 16 import 'package:analyzer/src/generated/source.dart' show Source; |
| 17 |
| 18 /** |
| 19 * Compute all known navigation information for the given part of [source]. |
| 20 */ |
| 21 NavigationHolderImpl computeNavigation(AnalysisServer server, |
| 22 AnalysisContext context, Source source, int offset, int length) { |
| 23 NavigationHolderImpl holder = new NavigationHolderImpl(); |
| 24 List<NavigationContributor> contributors = |
| 25 server.serverPlugin.navigationContributors; |
| 26 for (NavigationContributor contributor in contributors) { |
| 27 try { |
| 28 contributor.computeNavigation(holder, context, source, offset, length); |
| 29 } catch (exception, stackTrace) { |
| 30 AnalysisEngine.instance.logger.logError( |
| 31 'Exception from navigation contributor: ${contributor.runtimeType}', |
| 32 new CaughtException(exception, stackTrace)); |
| 33 } |
| 34 } |
| 35 return holder; |
| 36 } |
| 37 |
| 38 /** |
| 39 * A concrete implementation of [NavigationHolder]. |
| 40 */ |
| 41 class NavigationHolderImpl implements NavigationHolder { |
| 42 /** |
| 43 * A list of navigation regions. |
| 44 */ |
| 45 final List<protocol.NavigationRegion> regions = <protocol.NavigationRegion>[]; |
| 46 |
| 47 /** |
| 48 * All the unique targets referenced by [regions]. |
| 49 */ |
| 50 final List<protocol.NavigationTarget> targets = <protocol.NavigationTarget>[]; |
| 51 final Map<Pair<protocol.ElementKind, protocol.Location>, int> targetMap = |
| 52 new HashMap<Pair<protocol.ElementKind, protocol.Location>, int>(); |
| 53 |
| 54 /** |
| 55 * All the unique files referenced by [targets]. |
| 56 */ |
| 57 final List<String> files = <String>[]; |
| 58 final Map<String, int> fileMap = new HashMap<String, int>(); |
| 59 |
| 60 @override |
| 61 void addRegion(int offset, int length, protocol.ElementKind targetKind, |
| 62 protocol.Location targetLocation) { |
| 63 int targetIndex = _addTarget(targetKind, targetLocation); |
| 64 protocol.NavigationRegion region = |
| 65 new protocol.NavigationRegion(offset, length, <int>[targetIndex]); |
| 66 regions.add(region); |
| 67 } |
| 68 |
| 69 int _addFile(String file) { |
| 70 int index = fileMap[file]; |
| 71 if (index == null) { |
| 72 index = files.length; |
| 73 files.add(file); |
| 74 fileMap[file] = index; |
| 75 } |
| 76 return index; |
| 77 } |
| 78 |
| 79 int _addTarget(protocol.ElementKind kind, protocol.Location location) { |
| 80 var pair = |
| 81 new Pair<protocol.ElementKind, protocol.Location>(kind, location); |
| 82 int index = targetMap[pair]; |
| 83 if (index == null) { |
| 84 String file = location.file; |
| 85 int fileIndex = _addFile(file); |
| 86 index = targets.length; |
| 87 protocol.NavigationTarget target = new protocol.NavigationTarget( |
| 88 kind, |
| 89 fileIndex, |
| 90 location.offset, |
| 91 location.length, |
| 92 location.startLine, |
| 93 location.startColumn); |
| 94 targets.add(target); |
| 95 targetMap[pair] = index; |
| 96 } |
| 97 return index; |
| 98 } |
| 99 } |
| OLD | NEW |