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

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

Issue 392693002: Initial implementation for 'search.findElementReferences'. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add comments 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.element_references;
6
7 import 'dart:async';
8
9 import 'package:analysis_server/src/search/search_result.dart';
10 import 'package:analysis_services/search/search_engine.dart';
11 import 'package:analyzer/src/generated/element.dart';
12 import 'package:analyzer/src/generated/source.dart';
13
14
15 /**
16 * A computer for `search.findElementReferences` request results.
17 */
18 class ElementReferencesComputer {
19 final SearchEngine searchEngine;
20
21 ElementReferencesComputer(this.searchEngine);
22
23 /**
24 * Computes [SearchResult]s for [element] references.
25 */
26 Future<List<SearchResult>> compute(Element element, bool withPotential) {
27 var futures = <Future<List<SearchResult>>>[];
28 // tweak element
29 if (element is FieldFormalParameterElement) {
30 element = (element as FieldFormalParameterElement).field;
31 }
32 if (element is PropertyAccessorElement) {
33 element = (element as PropertyAccessorElement).variable;
34 }
35 // prepare Element(s) to find references to
36 List<Element> refElements = <Element>[];
37 if (element != null) {
38 // TODO(scheglov) find all hierarchy members
39 // if (element is ClassMemberElement) {
40 // refElements = HierarchyUtils.getHierarchyMembers(searchEngine, element );
41 // } else {
42 // refElements = <Element>[element];
43 // }
44 refElements = <Element>[element];
45 }
46 // process each 'refElement'
47 for (Element refElement in refElements) {
48 // add variable declaration
49 if (_isVariableLikeElement(refElement)) {
50 int nameOffset = refElement.nameOffset;
51 int nameLength = refElement.name.length;
52 SearchMatch searchMatch =
53 new SearchMatch(
54 MatchKind.DECLARATION,
55 refElement,
56 new SourceRange(nameOffset, nameLength),
57 true,
58 false);
59 SearchResult searchResult = new SearchResult.fromMatch(searchMatch);
60 futures.add(new Future.value(<SearchResult>[searchResult]));
61 }
62 // do search
63 Future<List<SearchMatch>> matchesFuture =
64 searchEngine.searchReferences(refElement);
65 Future<List<SearchResult>> resultsFuture =
66 matchesFuture.then((List<SearchMatch> matches) {
67 return matches.map(toResult).toList();
68 });
69 futures.add(resultsFuture);
70 }
71 // report potential references
72 if (withPotential) {
73 var matchesFuture = searchEngine.searchMemberReferences(element.name);
74 var resultsFuture = matchesFuture.then((List<SearchMatch> matches) {
75 return matches.where(
76 (match) => !match.isResolved).map(toResult).toList();
77 });
78 futures.add(resultsFuture);
79 }
80 // merge results
81 var futuresFuture = Future.wait(futures);
82 return futuresFuture.then((List<List<SearchResult>> lists) {
83 // TODO(scheglov) extract?
84 return lists.expand((List<SearchResult> matches) => matches).toList();
85 });
86 }
87
88 static SearchResult toResult(SearchMatch match) {
89 return new SearchResult.fromMatch(match);
90 }
91
92 static bool _isVariableLikeElement(Element element) {
93 if (element is LocalVariableElement) {
94 return true;
95 }
96 if (element is ParameterElement) {
97 return true;
98 }
99 if (element is PropertyInducingElement) {
100 return !element.isSynthetic;
101 }
102 return false;
103 }
104 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/lib/src/domain_search.dart ('k') | pkg/analysis_server/lib/src/search/search_domain.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698