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