/
domain_execution.dart
226 lines (208 loc) · 7.61 KB
/
domain_execution.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library domain.execution;
import 'dart:async';
import 'dart:collection';
import 'package:analysis_server/src/analysis_server.dart';
import 'package:analysis_server/src/constants.dart';
import 'package:analysis_server/src/protocol.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/source.dart';
/**
* Instances of the class [ExecutionDomainHandler] implement a [RequestHandler]
* that handles requests in the `execution` domain.
*/
class ExecutionDomainHandler implements RequestHandler {
/**
* The analysis server that is using this handler to process requests.
*/
final AnalysisServer server;
/**
* The next execution context identifier to be returned.
*/
int nextContextId = 0;
/**
* A table mapping execution context id's to the root of the context.
*/
Map<String, String> contextMap = new HashMap<String, String>();
/**
* The subscription to the 'onAnalysisComplete' events,
* used to send notifications when
*/
StreamSubscription onFileAnalyzed;
/**
* Initialize a newly created handler to handle requests for the given [server].
*/
ExecutionDomainHandler(this.server);
/**
* Implement the `execution.createContext` request.
*/
Response createContext(Request request) {
String file =
new ExecutionCreateContextParams.fromRequest(request).contextRoot;
String contextId = (nextContextId++).toString();
contextMap[contextId] = file;
return new ExecutionCreateContextResult(contextId).toResponse(request.id);
}
/**
* Implement the `execution.deleteContext` request.
*/
Response deleteContext(Request request) {
String contextId = new ExecutionDeleteContextParams.fromRequest(request).id;
contextMap.remove(contextId);
return new ExecutionDeleteContextResult().toResponse(request.id);
}
@override
Response handleRequest(Request request) {
try {
String requestName = request.method;
if (requestName == EXECUTION_CREATE_CONTEXT) {
return createContext(request);
} else if (requestName == EXECUTION_DELETE_CONTEXT) {
return deleteContext(request);
} else if (requestName == EXECUTION_MAP_URI) {
return mapUri(request);
} else if (requestName == EXECUTION_SET_SUBSCRIPTIONS) {
return setSubscriptions(request);
}
} on RequestFailure catch (exception) {
return exception.response;
}
return null;
}
/**
* Implement the 'execution.mapUri' request.
*/
Response mapUri(Request request) {
ExecutionMapUriParams params =
new ExecutionMapUriParams.fromRequest(request);
String contextId = params.id;
String path = contextMap[contextId];
if (path == null) {
return new Response.invalidParameter(
request,
'id',
'There is no execution context with an id of $contextId');
}
AnalysisContext context = server.getAnalysisContext(path);
if (params.file != null) {
if (params.uri != null) {
return new Response.invalidParameter(
request,
'file',
'Either file or uri must be provided, but not both');
}
Source source = server.getSource(params.file);
String uri = context.sourceFactory.restoreUri(source).toString();
return new ExecutionMapUriResult(uri: uri).toResponse(request.id);
} else if (params.uri != null) {
Source source = context.sourceFactory.forUri(params.uri);
String file = source.fullName;
return new ExecutionMapUriResult(file: file).toResponse(request.id);
}
return new Response.invalidParameter(
request,
'file',
'Either file or uri must be provided');
}
/**
* Implement the 'execution.setSubscriptions' request.
*/
Response setSubscriptions(Request request) {
List<ExecutionService> subscriptions =
new ExecutionSetSubscriptionsParams.fromRequest(request).subscriptions;
if (subscriptions.contains(ExecutionService.LAUNCH_DATA)) {
if (onFileAnalyzed == null) {
onFileAnalyzed = server.onFileAnalyzed.listen(_fileAnalyzed);
_reportCurrentFileStatus();
}
} else {
if (onFileAnalyzed != null) {
onFileAnalyzed.cancel();
onFileAnalyzed = null;
}
}
return new ExecutionSetSubscriptionsResult().toResponse(request.id);
}
void _fileAnalyzed(ChangeNotice notice) {
Source source = notice.source;
String filePath = source.fullName;
if (!_isInAnalysisRoot(filePath)) {
return;
}
AnalysisContext context = server.getAnalysisContext(filePath);
if (AnalysisEngine.isDartFileName(filePath)) {
ExecutableKind kind = ExecutableKind.NOT_EXECUTABLE;
if (context.isClientLibrary(source)) {
kind = ExecutableKind.CLIENT;
if (context.isServerLibrary(source)) {
kind = ExecutableKind.EITHER;
}
} else if (context.isServerLibrary(source)) {
kind = ExecutableKind.SERVER;
}
server.sendNotification(
new ExecutionLaunchDataParams(
filePath,
kind: kind).toNotification());
} else if (AnalysisEngine.isHtmlFileName(filePath)) {
List<Source> libraries = context.getLibrariesReferencedFromHtml(source);
server.sendNotification(
new ExecutionLaunchDataParams(
filePath,
referencedFiles: _getFullNames(libraries)).toNotification());
}
}
/**
* Return `true` if the given [filePath] represents a file that is in an
* analysis root.
*/
bool _isInAnalysisRoot(String filePath)
=> server.contextDirectoryManager.isInAnalysisRoot(filePath);
void _reportCurrentFileStatus() {
for (AnalysisContext context in server.getAnalysisContexts()) {
List<Source> librarySources = context.librarySources;
List<Source> clientSources = context.launchableClientLibrarySources;
List<Source> serverSources = context.launchableServerLibrarySources;
for (Source source in clientSources) {
if (serverSources.remove(source)) {
_sendKindNotification(source.fullName, ExecutableKind.EITHER);
} else {
_sendKindNotification(source.fullName, ExecutableKind.CLIENT);
}
librarySources.remove(source);
}
for (Source source in serverSources) {
_sendKindNotification(source.fullName, ExecutableKind.SERVER);
librarySources.remove(source);
}
for (Source source in librarySources) {
_sendKindNotification(source.fullName, ExecutableKind.NOT_EXECUTABLE);
}
for (Source source in context.htmlSources) {
String filePath = source.fullName;
if (_isInAnalysisRoot(filePath)) {
List<Source> libraries = context.getLibrariesReferencedFromHtml(source);
server.sendNotification(
new ExecutionLaunchDataParams(
filePath,
referencedFiles: _getFullNames(libraries)).toNotification());
}
}
}
}
/**
* Send a notification indicating the [kind] of the file with the given
* [filePath], but only if the file is in an analysis root.
*/
void _sendKindNotification(String filePath, ExecutableKind kind) {
if (_isInAnalysisRoot(filePath)) {
server.sendNotification(
new ExecutionLaunchDataParams(filePath, kind: kind).toNotification());
}
}
static List<String> _getFullNames(List<Source> sources) {
return sources.map((Source source) => source.fullName).toList();
}
}