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 domain.analysis; | 5 library domain.analysis; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:core' hide Resource; | 8 import 'dart:core' hide Resource; |
9 | 9 |
10 import 'package:analysis_server/src/analysis_server.dart'; | 10 import 'package:analysis_server/src/analysis_server.dart'; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 // delay response | 105 // delay response |
106 return Response.DELAYED_RESPONSE; | 106 return Response.DELAYED_RESPONSE; |
107 } | 107 } |
108 | 108 |
109 /** | 109 /** |
110 * Implement the `analysis.getNavigation` request. | 110 * Implement the `analysis.getNavigation` request. |
111 */ | 111 */ |
112 Response getNavigation(Request request) { | 112 Response getNavigation(Request request) { |
113 var params = new AnalysisGetNavigationParams.fromRequest(request); | 113 var params = new AnalysisGetNavigationParams.fromRequest(request); |
114 String file = params.file; | 114 String file = params.file; |
115 int offset = params.offset; | 115 Future<AnalysisDoneReason> analysisFuture = |
116 Future<AnalysisDoneReason> completionFuture = | |
117 server.onFileAnalysisComplete(file); | 116 server.onFileAnalysisComplete(file); |
118 if (completionFuture == null) { | 117 if (analysisFuture == null) { |
119 return new Response.getNavigationInvalidFile(request); | 118 return new Response.getNavigationInvalidFile(request); |
120 } | 119 } |
121 completionFuture.then((AnalysisDoneReason reason) { | 120 analysisFuture.then((AnalysisDoneReason reason) { |
122 switch (reason) { | 121 switch (reason) { |
123 case AnalysisDoneReason.COMPLETE: | 122 case AnalysisDoneReason.COMPLETE: |
124 List<CompilationUnit> units = | 123 List<CompilationUnit> units = |
125 server.getResolvedCompilationUnits(file); | 124 server.getResolvedCompilationUnits(file); |
126 if (units.isEmpty) { | 125 if (units.isEmpty) { |
127 server.sendResponse(new Response.getNavigationInvalidFile(request)); | 126 server.sendResponse(new Response.getNavigationInvalidFile(request)); |
128 } else { | 127 } else { |
129 DartUnitNavigationComputer computer = | 128 DartUnitNavigationComputer computer = |
130 new DartUnitNavigationComputer(); | 129 new DartUnitNavigationComputer(); |
| 130 _GetNavigationAstVisitor visitor = new _GetNavigationAstVisitor( |
| 131 params.offset, params.offset + params.length, computer); |
131 for (CompilationUnit unit in units) { | 132 for (CompilationUnit unit in units) { |
132 AstNode node = new NodeLocator(offset).searchWithin(unit); | 133 unit.accept(visitor); |
133 if (node != null) { | |
134 computer.compute(node); | |
135 } | |
136 } | 134 } |
137 server.sendResponse(new AnalysisGetNavigationResult( | 135 server.sendResponse(new AnalysisGetNavigationResult( |
138 computer.files, computer.targets, computer.regions) | 136 computer.files, computer.targets, computer.regions) |
139 .toResponse(request.id)); | 137 .toResponse(request.id)); |
140 } | 138 } |
141 break; | 139 break; |
142 case AnalysisDoneReason.CONTEXT_REMOVED: | 140 case AnalysisDoneReason.CONTEXT_REMOVED: |
143 // The active contexts have changed, so try again. | 141 // The active contexts have changed, so try again. |
144 Response response = getNavigation(request); | 142 Response response = getNavigation(request); |
145 if (response != Response.DELAYED_RESPONSE) { | 143 if (response != Response.DELAYED_RESPONSE) { |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 } | 283 } |
286 if (newOptions.generateLints != null) { | 284 if (newOptions.generateLints != null) { |
287 updaters.add((engine.AnalysisOptionsImpl options) { | 285 updaters.add((engine.AnalysisOptionsImpl options) { |
288 options.lint = newOptions.generateLints; | 286 options.lint = newOptions.generateLints; |
289 }); | 287 }); |
290 } | 288 } |
291 server.updateOptions(updaters); | 289 server.updateOptions(updaters); |
292 return new AnalysisUpdateOptionsResult().toResponse(request.id); | 290 return new AnalysisUpdateOptionsResult().toResponse(request.id); |
293 } | 291 } |
294 } | 292 } |
| 293 |
| 294 /** |
| 295 * An AST visitor that computer navigation regions in the givne region. |
| 296 */ |
| 297 class _GetNavigationAstVisitor extends UnifyingAstVisitor { |
| 298 final int rangeStart; |
| 299 final int rangeEnd; |
| 300 final DartUnitNavigationComputer computer; |
| 301 |
| 302 _GetNavigationAstVisitor(this.rangeStart, this.rangeEnd, this.computer); |
| 303 |
| 304 bool isInRange(int offset) { |
| 305 return rangeStart <= offset && offset <= rangeEnd; |
| 306 } |
| 307 |
| 308 @override |
| 309 visitNode(AstNode node) { |
| 310 // The node ends before the range starts. |
| 311 if (node.end < rangeStart) { |
| 312 return; |
| 313 } |
| 314 // The node starts after the range ends. |
| 315 if (node.offset > rangeEnd) { |
| 316 return; |
| 317 } |
| 318 // The node starts or ends in the range. |
| 319 if (isInRange(node.offset) || isInRange(node.end)) { |
| 320 computer.compute(node); |
| 321 return; |
| 322 } |
| 323 super.visitNode(node); |
| 324 } |
| 325 } |
OLD | NEW |