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

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: 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 class ElementReferencesComputer {
16 final SearchEngine searchEngine;
17
18 ElementReferencesComputer(this.searchEngine);
19
20 /**
21 * Computes [SearchResult]s for [element] references.
22 */
23 Future<List<SearchResult>> compute(Element element, bool withPotential) {
24 var futures = <Future<List<SearchResult>>>[];
25 // tweak element
26 if (element is FieldFormalParameterElement) {
27 element = (element as FieldFormalParameterElement).field;
28 }
29 if (element is PropertyAccessorElement) {
30 element = (element as PropertyAccessorElement).variable;
31 }
32 // prepare Element(s) to find references to
33 List<Element> refElements = <Element>[];
34 if (element != null) {
35 // TODO(scheglov) find all hierarchy members
36 // if (element is ClassMemberElement) {
37 // refElements = HierarchyUtils.getHierarchyMembers(searchEngine, element );
38 // } else {
39 // refElements = <Element>[element];
40 // }
41 refElements = <Element>[element];
42 }
43 // process each 'refElement'
44 for (Element refElement in refElements) {
45 // add variable declaration
46 if (_isVariableLikeElement(refElement)) {
47 int nameOffset = refElement.nameOffset;
48 int nameLength = refElement.name.length;
49 SearchMatch searchMatch =
50 new SearchMatch(
51 MatchKind.DECLARATION,
52 refElement,
53 new SourceRange(nameOffset, nameLength),
54 true,
55 false);
56 SearchResult searchResult = new SearchResult.fromMatch(searchMatch);
57 futures.add(new Future.value(<SearchResult>[searchResult]));
58 }
59 // do search
60 Future<List<SearchMatch>> matchesFuture =
61 searchEngine.searchReferences(refElement);
62 Future<List<SearchResult>> resultsFuture =
63 matchesFuture.then((List<SearchMatch> matches) {
64 return matches.map(toResult).toList();
65 });
66 futures.add(resultsFuture);
67 }
68 // report potential references
69 if (withPotential) {
70 var matchesFuture = searchEngine.searchMemberReferences(element.name);
71 var resultsFuture = matchesFuture.then((List<SearchMatch> matches) {
72 return matches.where(
73 (match) => !match.isResolved).map(toResult).toList();
74 });
75 futures.add(resultsFuture);
76 }
77 // merge results
78 var futuresFuture = Future.wait(futures);
79 return futuresFuture.then((List<List<SearchResult>> lists) {
80 // TODO(scheglov) extract?
81 return lists.expand((List<SearchResult> matches) => matches).toList();
82 });
83 }
84
85 static SearchResult toResult(SearchMatch match) {
86 return new SearchResult.fromMatch(match);
87 }
88
89 static bool _isVariableLikeElement(Element element) {
90 if (element is LocalVariableElement) {
91 return true;
92 }
93 if (element is ParameterElement) {
94 return true;
95 }
96 if (element is PropertyInducingElement) {
97 return !element.isSynthetic;
98 }
99 return false;
100 }
101 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698