| 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 services.search_engine; | 5 library services.search_engine; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analyzer/dart/element/element.dart'; | 9 import 'package:analyzer/dart/element/element.dart'; |
| 10 import 'package:analyzer/dart/element/visitor.dart'; | 10 import 'package:analyzer/dart/element/visitor.dart'; |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 | 148 |
| 149 Source _librarySource; | 149 Source _librarySource; |
| 150 Source _unitSource; | 150 Source _unitSource; |
| 151 LibraryElement _libraryElement; | 151 LibraryElement _libraryElement; |
| 152 Element _element; | 152 Element _element; |
| 153 | 153 |
| 154 SearchMatch(this.context, this.libraryUri, this.unitUri, this.kind, | 154 SearchMatch(this.context, this.libraryUri, this.unitUri, this.kind, |
| 155 this.sourceRange, this.isResolved, this.isQualified); | 155 this.sourceRange, this.isResolved, this.isQualified); |
| 156 | 156 |
| 157 /** | 157 /** |
| 158 * Return the [Element] containing the match. | 158 * Return the [Element] containing the match. Can return `null` if the unit |
| 159 * does not exist, or its element was invalidated, or the element cannot be |
| 160 * found, etc. |
| 159 */ | 161 */ |
| 160 Element get element { | 162 Element get element { |
| 161 if (_element == null) { | 163 if (_element == null) { |
| 162 CompilationUnitElement unitElement = | 164 CompilationUnitElement unitElement = |
| 163 context.getCompilationUnitElement(unitSource, librarySource); | 165 context.getCompilationUnitElement(unitSource, librarySource); |
| 164 _ContainingElementFinder finder = | 166 if (unitElement != null) { |
| 165 new _ContainingElementFinder(sourceRange.offset); | 167 _ContainingElementFinder finder = |
| 166 unitElement.accept(finder); | 168 new _ContainingElementFinder(sourceRange.offset); |
| 167 _element = finder.containingElement; | 169 unitElement.accept(finder); |
| 170 _element = finder.containingElement; |
| 171 } |
| 168 } | 172 } |
| 169 return _element; | 173 return _element; |
| 170 } | 174 } |
| 171 | 175 |
| 172 /** | 176 /** |
| 173 * The absolute path of the file containing the match. | 177 * The absolute path of the file containing the match. |
| 174 */ | 178 */ |
| 175 String get file => unitSource.fullName; | 179 String get file => unitSource.fullName; |
| 176 | 180 |
| 177 @override | 181 @override |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 buffer.write(unitUri); | 234 buffer.write(unitUri); |
| 231 buffer.write(", range="); | 235 buffer.write(", range="); |
| 232 buffer.write(sourceRange); | 236 buffer.write(sourceRange); |
| 233 buffer.write(", isResolved="); | 237 buffer.write(", isResolved="); |
| 234 buffer.write(isResolved); | 238 buffer.write(isResolved); |
| 235 buffer.write(", isQualified="); | 239 buffer.write(", isQualified="); |
| 236 buffer.write(isQualified); | 240 buffer.write(isQualified); |
| 237 buffer.write(")"); | 241 buffer.write(")"); |
| 238 return buffer.toString(); | 242 return buffer.toString(); |
| 239 } | 243 } |
| 244 |
| 245 /** |
| 246 * Return elements of [matches] which has not-null elements. |
| 247 * |
| 248 * When [SearchMatch.element] is not `null` we cache its value, so it cannot |
| 249 * become `null` later. |
| 250 */ |
| 251 static List<SearchMatch> withNotNullElement(List<SearchMatch> matches) { |
| 252 return matches.where((match) => match.element != null).toList(); |
| 253 } |
| 240 } | 254 } |
| 241 | 255 |
| 242 /** | 256 /** |
| 243 * A visitor that finds the deep-most [Element] that contains the [offset]. | 257 * A visitor that finds the deep-most [Element] that contains the [offset]. |
| 244 */ | 258 */ |
| 245 class _ContainingElementFinder extends GeneralizingElementVisitor { | 259 class _ContainingElementFinder extends GeneralizingElementVisitor { |
| 246 final int offset; | 260 final int offset; |
| 247 Element containingElement; | 261 Element containingElement; |
| 248 | 262 |
| 249 _ContainingElementFinder(this.offset); | 263 _ContainingElementFinder(this.offset); |
| 250 | 264 |
| 251 visitElement(Element element) { | 265 visitElement(Element element) { |
| 252 if (element is ElementImpl) { | 266 if (element is ElementImpl) { |
| 253 if (element.codeOffset != null && | 267 if (element.codeOffset != null && |
| 254 element.codeOffset <= offset && | 268 element.codeOffset <= offset && |
| 255 offset <= element.codeOffset + element.codeLength) { | 269 offset <= element.codeOffset + element.codeLength) { |
| 256 containingElement = element; | 270 containingElement = element; |
| 257 super.visitElement(element); | 271 super.visitElement(element); |
| 258 } | 272 } |
| 259 } | 273 } |
| 260 } | 274 } |
| 261 } | 275 } |
| OLD | NEW |