Skip to content

Commit da1d59c

Browse files
srawlinsCommit Queue
authored andcommitted
DAS plugins: handle added, modified, removed files
Change-Id: Idc8cd1378e5a7e8c8191dfad9a37edd990e88120 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/446920 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent ad3513f commit da1d59c

File tree

2 files changed

+98
-6
lines changed

2 files changed

+98
-6
lines changed

pkg/analysis_server_plugin/lib/src/plugin_server.dart

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,9 @@ class PluginServer {
467467
switch (request.method) {
468468
case protocol.ANALYSIS_REQUEST_GET_NAVIGATION:
469469
case protocol.ANALYSIS_REQUEST_HANDLE_WATCH_EVENTS:
470-
result = null;
470+
var params =
471+
protocol.AnalysisHandleWatchEventsParams.fromRequest(request);
472+
result = await _handleAnalysisWatchEvents(params);
471473

472474
case protocol.ANALYSIS_REQUEST_SET_CONTEXT_ROOTS:
473475
var params =
@@ -608,21 +610,55 @@ class PluginServer {
608610

609611
changedPaths.add(path);
610612
});
611-
await _handleContentChanged(changedPaths.toList());
613+
await _handleContentChanged(modifiedPaths: changedPaths.toList());
612614
return protocol.AnalysisUpdateContentResult();
613615
}
614616

615-
/// Handles the fact that files with [paths] were changed.
616-
Future<void> _handleContentChanged(List<String> paths) async {
617+
/// Handles an 'analysis.handleWatchEvents' request.
618+
Future<protocol.AnalysisHandleWatchEventsResult> _handleAnalysisWatchEvents(
619+
protocol.AnalysisHandleWatchEventsParams parameters) async {
620+
final addedPaths = parameters.events
621+
.where((e) => e.type == protocol.WatchEventType.ADD)
622+
.map((e) => e.path)
623+
.toList();
624+
final modifiedPaths = parameters.events
625+
.where((e) => e.type == protocol.WatchEventType.MODIFY)
626+
.map((e) => e.path)
627+
.toList();
628+
final removedPaths = parameters.events
629+
.where((e) => e.type == protocol.WatchEventType.REMOVE)
630+
.map((e) => e.path)
631+
.toList();
632+
633+
await _handleContentChanged(
634+
addedPaths: addedPaths,
635+
modifiedPaths: modifiedPaths,
636+
removedPaths: removedPaths,
637+
);
638+
639+
return protocol.AnalysisHandleWatchEventsResult();
640+
}
641+
642+
/// Handles added files, modified files, and removed files.
643+
Future<void> _handleContentChanged(
644+
{List<String> addedPaths = const [],
645+
List<String> modifiedPaths = const [],
646+
List<String> removedPaths = const []}) async {
617647
if (_contextCollection case var contextCollection?) {
618648
_channel.sendNotification(
619649
protocol.PluginStatusParams(analysis: protocol.AnalysisStatus(true))
620650
.toNotification());
621651
await _forAnalysisContexts(contextCollection, (analysisContext) async {
622-
for (var path in paths) {
652+
for (var path in modifiedPaths) {
653+
analysisContext.changeFile(path);
654+
}
655+
for (var path in removedPaths) {
623656
analysisContext.changeFile(path);
624657
}
625-
var affected = await analysisContext.applyPendingFileChanges();
658+
var affected = [
659+
...await analysisContext.applyPendingFileChanges(),
660+
...addedPaths,
661+
];
626662
await _handleAffectedFiles(
627663
analysisContext: analysisContext, paths: affected);
628664
});

pkg/analysis_server_plugin/test/src/plugin_server_test.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,62 @@ bool b = false;
300300
expect(params.errors, isEmpty);
301301
}
302302

303+
Future<void> test_watchEvent_add() async {
304+
writeAnalysisOptionsWithPlugin();
305+
await channel
306+
.sendRequest(protocol.AnalysisSetContextRootsParams([contextRoot]));
307+
308+
var paramsQueue = _analysisErrorsParams;
309+
310+
newFile(filePath, 'bool b = false;');
311+
312+
await channel.sendRequest(protocol.AnalysisHandleWatchEventsParams(
313+
[WatchEvent(WatchEventType.ADD, filePath)]));
314+
315+
var params = await paramsQueue.next;
316+
expect(params.errors, hasLength(1));
317+
_expectAnalysisError(params.errors.single, message: 'No bools message');
318+
}
319+
320+
Future<void> test_watchEvent_modify() async {
321+
writeAnalysisOptionsWithPlugin();
322+
newFile(filePath, 'int b = 7;');
323+
await channel
324+
.sendRequest(protocol.AnalysisSetContextRootsParams([contextRoot]));
325+
326+
var paramsQueue = _analysisErrorsParams;
327+
var params = await paramsQueue.next;
328+
expect(params.errors, isEmpty);
329+
330+
newFile(filePath, 'bool b = false;');
331+
332+
await channel.sendRequest(protocol.AnalysisHandleWatchEventsParams(
333+
[WatchEvent(WatchEventType.MODIFY, filePath)]));
334+
335+
params = await paramsQueue.next;
336+
expect(params.errors, hasLength(1));
337+
_expectAnalysisError(params.errors.single, message: 'No bools message');
338+
}
339+
340+
Future<void> test_watchEvent_remove() async {
341+
writeAnalysisOptionsWithPlugin();
342+
newFile(filePath, 'int b = 7;');
343+
await channel
344+
.sendRequest(protocol.AnalysisSetContextRootsParams([contextRoot]));
345+
346+
var paramsQueue = _analysisErrorsParams;
347+
var params = await paramsQueue.next;
348+
expect(params.errors, isEmpty);
349+
350+
deleteFile(filePath);
351+
352+
await channel.sendRequest(protocol.AnalysisHandleWatchEventsParams(
353+
[WatchEvent(WatchEventType.REMOVE, filePath)]));
354+
355+
params = await paramsQueue.next;
356+
expect(params.errors, isEmpty);
357+
}
358+
303359
void writeAnalysisOptionsWithPlugin(
304360
[Map<String, String> diagnosticConfiguration = const {}]) {
305361
var buffer = StringBuffer('''

0 commit comments

Comments
 (0)