OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, 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 src.domain_experimental; | |
6 | |
7 import 'dart:collection'; | |
8 import 'dart:core' hide Resource; | |
9 | |
10 import 'package:analysis_server/plugin/protocol/protocol.dart'; | |
11 import 'package:analysis_server/src/analysis_server.dart'; | |
12 import 'package:analyzer/file_system/file_system.dart'; | |
13 import 'package:analyzer/src/context/cache.dart'; | |
14 import 'package:analyzer/src/context/context.dart'; | |
15 import 'package:analyzer/src/generated/engine.dart' | |
16 hide AnalysisCache, AnalysisContextImpl; | |
17 import 'package:analyzer/src/generated/source.dart'; | |
18 import 'package:analyzer/src/generated/utilities_collection.dart'; | |
19 import 'package:analyzer/src/task/driver.dart'; | |
20 import 'package:analyzer/task/model.dart'; | |
21 | |
22 /// Extract context data from the given [context]. | |
23 ContextData extractData(AnalysisContext context) { | |
24 int explicitFiles = 0; | |
25 int implicitFiles = 0; | |
26 int workItems = 0; | |
27 Set<String> exceptions = new HashSet<String>(); | |
28 if (context is AnalysisContextImpl) { | |
29 // Work Item count. | |
30 AnalysisDriver driver = context.driver; | |
31 List<WorkItem> items = driver.currentWorkOrder?.workItems; | |
32 workItems ??= items?.length; | |
33 var cache = context.analysisCache; | |
34 if (cache is AnalysisCache) { | |
35 Set<AnalysisTarget> countedTargets = new HashSet<AnalysisTarget>(); | |
36 MapIterator<AnalysisTarget, CacheEntry> iterator = cache.iterator(); | |
37 while (iterator.moveNext()) { | |
38 AnalysisTarget target = iterator.key; | |
39 if (countedTargets.add(target)) { | |
40 CacheEntry cacheEntry = iterator.value; | |
41 if (target is Source) { | |
42 if (cacheEntry.explicitlyAdded) { | |
43 explicitFiles++; | |
44 } else { | |
45 implicitFiles++; | |
46 } | |
47 } | |
48 // Caught exceptions. | |
49 if (cacheEntry.exception != null) { | |
50 exceptions.add(cacheEntry.exception.toString()); | |
51 } | |
52 } | |
53 } | |
54 } | |
55 } | |
56 return new ContextData(context.name, explicitFiles, implicitFiles, workItems, | |
57 exceptions.toList()); | |
58 } | |
59 | |
60 /// Instances of the class [ExperimentalDomainHandler] implement a | |
61 /// [RequestHandler] that handles requests in the `experimental` domain. | |
62 class ExperimentalDomainHandler implements RequestHandler { | |
63 /// The name of the request used to get diagnostic information. | |
64 static const String EXPERIMENTAL_DIAGNOSTICS = 'experimental.getDiagnostics'; | |
65 | |
66 /// The analysis server that is using this handler to process requests. | |
67 final AnalysisServer server; | |
68 | |
69 /// Initialize a newly created handler to handle requests for the given | |
70 /// [server]. | |
71 ExperimentalDomainHandler(this.server); | |
72 | |
73 /// Answer the `experimental.diagnostics` request. | |
74 Response computeDiagnostics(Request request) { | |
75 List<ContextData> infos = <ContextData>[]; | |
76 server.folderMap.forEach((Folder folder, AnalysisContext context) { | |
77 infos.add(extractData(context)); | |
78 }); | |
79 | |
80 return new ExperimentalGetDiagnosticsResult(infos).toResponse(request.id); | |
81 } | |
82 | |
83 @override | |
84 Response handleRequest(Request request) { | |
85 try { | |
86 String requestName = request.method; | |
87 if (requestName == EXPERIMENTAL_DIAGNOSTICS) { | |
88 return computeDiagnostics(request); | |
89 } | |
90 } on RequestFailure catch (exception) { | |
91 return exception.response; | |
92 } | |
93 return null; | |
94 } | |
95 } | |
OLD | NEW |