| 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.navigation; | 5 library computer.navigation; |
| 6 | 6 |
| 7 import 'dart:collection'; |
| 8 |
| 7 import 'package:analysis_server/src/protocol_server.dart' as protocol; | 9 import 'package:analysis_server/src/protocol_server.dart' as protocol; |
| 8 import 'package:analyzer/src/generated/ast.dart'; | 10 import 'package:analyzer/src/generated/ast.dart'; |
| 9 import 'package:analyzer/src/generated/element.dart'; | 11 import 'package:analyzer/src/generated/element.dart'; |
| 10 import 'package:analyzer/src/generated/scanner.dart'; | 12 import 'package:analyzer/src/generated/scanner.dart'; |
| 11 | 13 |
| 12 | 14 |
| 13 /** | 15 /** |
| 14 * A computer for navigation regions in a Dart [CompilationUnit]. | 16 * A computer for navigation regions in a Dart [CompilationUnit]. |
| 15 */ | 17 */ |
| 16 class DartUnitNavigationComputer { | 18 class DartUnitNavigationComputer { |
| 17 final CompilationUnit _unit; | 19 final CompilationUnit _unit; |
| 18 | 20 |
| 19 final List<protocol.NavigationRegion> _regions = <protocol.NavigationRegion>[ | 21 final List<String> files = <String>[]; |
| 20 ]; | 22 final Map<String, int> fileMap = new HashMap<String, int>(); |
| 23 final List<protocol.NavigationTarget> targets = <protocol.NavigationTarget>[]; |
| 24 final Map<Element, int> targetMap = new HashMap<Element, int>(); |
| 25 final List<protocol.NavigationRegion> regions = <protocol.NavigationRegion>[]; |
| 21 | 26 |
| 22 DartUnitNavigationComputer(this._unit); | 27 DartUnitNavigationComputer(this._unit); |
| 23 | 28 |
| 24 /** | 29 /** |
| 25 * Returns the computed navigation regions, not `null`. | 30 * Computes [regions], [targets] and [files]. |
| 26 */ | 31 */ |
| 27 List<protocol.NavigationRegion> compute() { | 32 void compute() { |
| 28 _unit.accept(new _DartUnitNavigationComputerVisitor(this)); | 33 _unit.accept(new _DartUnitNavigationComputerVisitor(this)); |
| 29 return new List.from(_regions); | |
| 30 } | 34 } |
| 31 | 35 |
| 32 void _addRegion(int offset, int length, Element element) { | 36 void _addRegion(int offset, int length, Element element) { |
| 33 if (element is FieldFormalParameterElement) { | 37 if (element is FieldFormalParameterElement) { |
| 34 element = (element as FieldFormalParameterElement).field; | 38 element = (element as FieldFormalParameterElement).field; |
| 35 } | 39 } |
| 36 if (element == null || element == DynamicElementImpl.instance) { | 40 if (element == null || element == DynamicElementImpl.instance) { |
| 37 return; | 41 return; |
| 38 } | 42 } |
| 39 if (element.location == null) { | 43 if (element.location == null) { |
| 40 return; | 44 return; |
| 41 } | 45 } |
| 42 protocol.Element target = protocol.newElement_fromEngine(element); | 46 int targetIndex = _addTarget(element); |
| 43 _regions.add(new protocol.NavigationRegion(offset, length, [target])); | 47 regions.add( |
| 48 new protocol.NavigationRegion(offset, length, <int>[targetIndex])); |
| 49 } |
| 50 |
| 51 int _addTarget(Element element) { |
| 52 int index = targetMap[element]; |
| 53 if (index == null) { |
| 54 index = targets.length; |
| 55 protocol.NavigationTarget target = |
| 56 protocol.newNavigationTarget_fromElement(element, _addFile); |
| 57 targets.add(target); |
| 58 targetMap[element] = index; |
| 59 } |
| 60 return index; |
| 61 } |
| 62 |
| 63 int _addFile(String file) { |
| 64 int index = fileMap[file]; |
| 65 if (index == null) { |
| 66 index = files.length; |
| 67 files.add(file); |
| 68 fileMap[file] = index; |
| 69 } |
| 70 return index; |
| 44 } | 71 } |
| 45 | 72 |
| 46 void _addRegion_nodeStart_nodeEnd(AstNode a, AstNode b, Element element) { | 73 void _addRegion_nodeStart_nodeEnd(AstNode a, AstNode b, Element element) { |
| 47 int offset = a.offset; | 74 int offset = a.offset; |
| 48 int length = b.end - offset; | 75 int length = b.end - offset; |
| 49 _addRegion(offset, length, element); | 76 _addRegion(offset, length, element); |
| 50 } | 77 } |
| 51 | 78 |
| 52 void _addRegion_tokenStart_nodeEnd(Token a, AstNode b, Element element) { | 79 void _addRegion_tokenStart_nodeEnd(Token a, AstNode b, Element element) { |
| 53 int offset = a.offset; | 80 int offset = a.offset; |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 Element element = node.bestElement; | 244 Element element = node.bestElement; |
| 218 computer._addRegionForNode(node, element); | 245 computer._addRegionForNode(node, element); |
| 219 } | 246 } |
| 220 | 247 |
| 221 void _safelyVisit(AstNode node) { | 248 void _safelyVisit(AstNode node) { |
| 222 if (node != null) { | 249 if (node != null) { |
| 223 node.accept(this); | 250 node.accept(this); |
| 224 } | 251 } |
| 225 } | 252 } |
| 226 } | 253 } |
| OLD | NEW |