| 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 search.search_result; | 5 library search.search_result; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/computer/element.dart'; | 7 import 'package:analysis_server/src/protocol2.dart'; |
| 8 import 'package:analysis_server/src/constants.dart'; | |
| 9 import 'package:analysis_server/src/services/json.dart'; | |
| 10 import 'package:analysis_server/src/services/search/search_engine.dart'; | 8 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 11 import 'package:analyzer/src/generated/element.dart' as engine; | 9 import 'package:analyzer/src/generated/element.dart' as engine; |
| 12 | 10 |
| 13 | 11 |
| 14 /** | 12 SearchResult searchResultFromMatch(SearchMatch match) { |
| 15 * A single result from a search request. | 13 SearchResultKind kind = new SearchResultKind.fromEngine(match.kind); |
| 16 */ | 14 Location location = |
| 17 class SearchResult implements HasToJson { | 15 new Location.fromOffset( |
| 18 /** | 16 match.element, |
| 19 * The kind of element that was found or the kind of reference that was found. | 17 match.sourceRange.offset, |
| 20 */ | 18 match.sourceRange.length); |
| 21 final SearchResultKind kind; | 19 List<Element> path = _computePath(match.element); |
| 22 | 20 return new SearchResult(location, kind, !match.isResolved, path); |
| 23 /** | |
| 24 * Is `true` if the result is a potential match but cannot be confirmed to be | |
| 25 * a match. | |
| 26 * | |
| 27 * For example, if all references to a method `m` defined in some class were | |
| 28 * requested, and a reference to a method `m` from an unknown class were | |
| 29 * found, it would be marked as being a potential match. | |
| 30 */ | |
| 31 final bool isPotential; | |
| 32 | |
| 33 /** | |
| 34 * The location of the code that matched the search criteria. | |
| 35 */ | |
| 36 final Location location; | |
| 37 | |
| 38 /** | |
| 39 * The elements that contain the result, starting with the most immediately | |
| 40 * enclosing ancestor and ending with the library. | |
| 41 */ | |
| 42 final List<Element> path; | |
| 43 | |
| 44 SearchResult(this.kind, this.isPotential, this.location, this.path); | |
| 45 | |
| 46 factory SearchResult.fromJson(Map<String, Object> map) { | |
| 47 SearchResultKind kind = new SearchResultKind.fromName(map[KIND]); | |
| 48 bool isPotential = map[IS_POTENTIAL]; | |
| 49 Location location = new Location.fromJson(map[LOCATION]); | |
| 50 List<Map<String, Object>> pathJson = map[PATH]; | |
| 51 List<Element> path = pathJson.map((json) { | |
| 52 return new Element.fromJson(json); | |
| 53 }).toList(); | |
| 54 return new SearchResult(kind, isPotential, location, path); | |
| 55 } | |
| 56 | |
| 57 factory SearchResult.fromMatch(SearchMatch match) { | |
| 58 SearchResultKind kind = new SearchResultKind.fromEngine(match.kind); | |
| 59 Location location = | |
| 60 new Location.fromOffset( | |
| 61 match.element, | |
| 62 match.sourceRange.offset, | |
| 63 match.sourceRange.length); | |
| 64 List<Element> path = _computePath(match.element); | |
| 65 return new SearchResult(kind, !match.isResolved, location, path); | |
| 66 } | |
| 67 | |
| 68 Map<String, Object> toJson() { | |
| 69 return { | |
| 70 KIND: kind.name, | |
| 71 IS_POTENTIAL: isPotential, | |
| 72 LOCATION: location.toJson(), | |
| 73 PATH: path.map(Element.asJson).toList() | |
| 74 }; | |
| 75 } | |
| 76 | |
| 77 @override | |
| 78 String toString() => toJson().toString(); | |
| 79 | |
| 80 static Map<String, Object> asJson(SearchResult result) { | |
| 81 return result.toJson(); | |
| 82 } | |
| 83 | |
| 84 static List<Element> _computePath(engine.Element element) { | |
| 85 List<Element> path = <Element>[]; | |
| 86 while (element != null) { | |
| 87 path.add(new Element.fromEngine(element)); | |
| 88 element = element.enclosingElement; | |
| 89 } | |
| 90 return path; | |
| 91 } | |
| 92 } | 21 } |
| 93 | 22 |
| 94 | 23 List<Element> _computePath(engine.Element element) { |
| 95 /** | 24 List<Element> path = <Element>[]; |
| 96 * An enumeration of the kinds of search results returned by the search domain. | 25 while (element != null) { |
| 97 */ | 26 path.add(new Element.fromEngine(element)); |
| 98 class SearchResultKind { | 27 element = element.enclosingElement; |
| 99 static const DECLARATION = const SearchResultKind('DECLARATION'); | |
| 100 static const READ = const SearchResultKind('READ'); | |
| 101 static const READ_WRITE = const SearchResultKind('READ_WRITE'); | |
| 102 static const WRITE = const SearchResultKind('WRITE'); | |
| 103 static const INVOCATION = const SearchResultKind('INVOCATION'); | |
| 104 static const REFERENCE = const SearchResultKind('REFERENCE'); | |
| 105 static const UNKNOWN = const SearchResultKind('UNKNOWN'); | |
| 106 | |
| 107 final String name; | |
| 108 | |
| 109 const SearchResultKind(this.name); | |
| 110 | |
| 111 factory SearchResultKind.fromEngine(MatchKind kind) { | |
| 112 if (kind == MatchKind.DECLARATION) { | |
| 113 return DECLARATION; | |
| 114 } | |
| 115 if (kind == MatchKind.READ) { | |
| 116 return READ; | |
| 117 } | |
| 118 if (kind == MatchKind.READ_WRITE) { | |
| 119 return READ_WRITE; | |
| 120 } | |
| 121 if (kind == MatchKind.WRITE) { | |
| 122 return WRITE; | |
| 123 } | |
| 124 if (kind == MatchKind.INVOCATION) { | |
| 125 return INVOCATION; | |
| 126 } | |
| 127 if (kind == MatchKind.REFERENCE) { | |
| 128 return REFERENCE; | |
| 129 } | |
| 130 return UNKNOWN; | |
| 131 } | 28 } |
| 132 | 29 return path; |
| 133 factory SearchResultKind.fromName(String name) { | |
| 134 if (name == DECLARATION.name) { | |
| 135 return DECLARATION; | |
| 136 } | |
| 137 if (name == READ.name) { | |
| 138 return READ; | |
| 139 } | |
| 140 if (name == READ_WRITE.name) { | |
| 141 return READ_WRITE; | |
| 142 } | |
| 143 if (name == WRITE.name) { | |
| 144 return WRITE; | |
| 145 } | |
| 146 if (name == INVOCATION.name) { | |
| 147 return INVOCATION; | |
| 148 } | |
| 149 if (name == REFERENCE.name) { | |
| 150 return REFERENCE; | |
| 151 } | |
| 152 return UNKNOWN; | |
| 153 } | |
| 154 | |
| 155 @override | |
| 156 String toString() => name; | |
| 157 } | 30 } |
| OLD | NEW |