Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(961)

Side by Side Diff: pkg/analysis_server/lib/src/search/search_result.dart

Issue 392693002: Initial implementation for 'search.findElementReferences'. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 class SearchResult {
14 final SearchResultKind kind;
15 final bool isPotential;
16 final Location location;
17 final List<Element> path;
18
19 SearchResult(this.kind, this.isPotential, this.location, this.path);
20
21 factory SearchResult.fromJson(Map<String, Object> map) {
22 SearchResultKind kind = new SearchResultKind.fromName(map[KIND]);
23 bool isPotential = map[IS_POTENTIAL];
24 Location location = new Location.fromJson(map[LOCATION]);
25 List<Map<String, Object>> pathJson = map[PATH];
26 List<Element> path = pathJson.map((json) {
27 return new Element.fromJson(json);
28 }).toList();
29 return new SearchResult(kind, isPotential, location, path);
30 }
31
32 factory SearchResult.fromMatch(SearchMatch match) {
33 SearchResultKind kind = new SearchResultKind.fromEngine(match.kind);
34 Location location =
35 new Location.fromOffset(
36 match.element,
37 match.sourceRange.offset,
38 match.sourceRange.length);
39 List<Element> path = _computePath(match.element);
40 return new SearchResult(kind, !match.isResolved, location, path);
41 }
42
43 Map<String, Object> toJson() {
44 return {
45 KIND: kind.name,
46 IS_POTENTIAL: isPotential,
47 LOCATION: location.toJson(),
48 PATH: path.map(Element.asJson).toList()
49 };
50 }
51
52 @override
53 String toString() => toJson().toString();
54
55 static Map<String, Object> asJson(SearchResult result) {
56 return result.toJson();
57 }
58
59 static List<Element> _computePath(engine.Element element) {
60 List<Element> path = <Element>[];
61 while (element != null) {
62 path.add(new Element.fromEngine(element));
63 element = element.enclosingElement;
64 }
65 return path;
66 }
67 }
68
69
70 class SearchResultKind {
71 static const DECLARATION = const SearchResultKind('DECLARATION');
72 static const READ = const SearchResultKind('READ');
73 static const READ_WRITE = const SearchResultKind('READ_WRITE');
74 static const WRITE = const SearchResultKind('WRITE');
75 static const INVOCATION = const SearchResultKind('INVOCATION');
76 static const REFERENCE = const SearchResultKind('REFERENCE');
77 static const UNKNOWN = const SearchResultKind('UNKNOWN');
78
79 final String name;
80
81 const SearchResultKind(this.name);
82
83 factory SearchResultKind.fromEngine(MatchKind kind) {
84 if (kind == MatchKind.DECLARATION) {
85 return DECLARATION;
86 }
87 if (kind == MatchKind.READ) {
88 return READ;
89 }
90 if (kind == MatchKind.READ_WRITE) {
91 return READ_WRITE;
92 }
93 if (kind == MatchKind.WRITE) {
94 return WRITE;
95 }
96 if (kind == MatchKind.INVOCATION) {
97 return INVOCATION;
98 }
99 if (kind == MatchKind.REFERENCE) {
100 return REFERENCE;
101 }
102 return UNKNOWN;
103 }
104
105 factory SearchResultKind.fromName(String name) {
106 if (name == DECLARATION.name) {
107 return DECLARATION;
108 }
109 if (name == READ.name) {
110 return READ;
111 }
112 if (name == READ_WRITE.name) {
113 return READ_WRITE;
114 }
115 if (name == WRITE.name) {
116 return WRITE;
117 }
118 if (name == INVOCATION.name) {
119 return INVOCATION;
120 }
121 if (name == REFERENCE.name) {
122 return REFERENCE;
123 }
124 return UNKNOWN;
125 }
126
127 @override
128 String toString() => name;
129 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698