Skip to content

Commit b262f60

Browse files
bwilkersonCommit Queue
authored andcommitted
Code cleanup in log player and server driver
Setting the command-line arguments separately from the protocol used to be necessary, but it no long is, so it's cleaner to just set the command line arguments when creating the server and moving the handling of the protocol option into the server. This resulted in a cleaner API and implementation. Change-Id: I9456e2a43310bb855b54c010af64ce0328a08ff7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/464785 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Keerti Parthasarathy <[email protected]>
1 parent 1a1d7da commit b262f60

File tree

2 files changed

+34
-40
lines changed

2 files changed

+34
-40
lines changed

pkg/analysis_server/tool/log_player/log_player.dart

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'package:analysis_server/src/server/driver.dart';
66
import 'package:analysis_server/src/session_logger/entry_kind.dart';
77
import 'package:analysis_server/src/session_logger/log_entry.dart';
88
import 'package:analysis_server/src/session_logger/process_id.dart';
9-
import 'package:collection/collection.dart';
109

1110
import 'log.dart';
1211
import 'server_driver.dart';
@@ -49,24 +48,7 @@ class LogPlayer {
4948
'Analysis server already started, only one instance is allowed.',
5049
);
5150
}
52-
// TODO(brianwilkerson): Stop parsing out the protocol. Instead,
53-
// consider verifying that a protocol was specified, throwing if not,
54-
// and then just pass all of the arguments to the constructor. This
55-
// would require allow-listing the protocol option.
56-
var parsedArgs = driverArgParser.parse(entry.argList);
57-
var protocolOption = parsedArgs.option(Driver.serverProtocolOption);
58-
var protocol = switch (protocolOption) {
59-
Driver.protocolAnalyzer => ServerProtocol.legacy,
60-
Driver.protocolLsp => ServerProtocol.lsp,
61-
_ => throw StateError('Unrecognized protocol $protocolOption'),
62-
};
63-
var server = this.server = ServerDriver(protocol: protocol);
64-
server.additionalArguments.addAll(
65-
entry.argList.whereNot(
66-
(element) =>
67-
element.startsWith('--${Driver.serverProtocolOption}'),
68-
),
69-
);
51+
var server = this.server = ServerDriver(arguments: entry.argList);
7052
await server.start();
7153
case EntryKind.message:
7254
if (entry.receiver == ProcessId.server) {

pkg/analysis_server/tool/log_player/server_driver.dart

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ class ServerDriver {
1515
/// The protocol being used by the server.
1616
final ServerProtocol _protocol;
1717

18-
/// A list of additional arguments from the comman-line used to start the
19-
/// server.
20-
final List<String> additionalArguments = [];
18+
/// A list of arguments from the command-line used to start the server.
19+
final List<String> arguments;
2120

2221
/// The sink used to send messages from the IDE to the server's stdin, or
2322
/// `null` if the server has not been started using [start].
@@ -37,24 +36,38 @@ class ServerDriver {
3736
/// The server is run in a separate process.
3837
// TODO(brianwilkerson): Add a flag controlling whether the server is in the
3938
// same process as the driver or in a separate process.
40-
ServerDriver({required ServerProtocol protocol}) : _protocol = protocol;
41-
42-
/// Returns the list of additional arguments that were passes on the command
43-
/// line when the server was started that should be passed to the server
44-
/// created by this driver.
45-
List<String> get filteredAdditionalArguments {
46-
var arguments = <String>[];
47-
var nextIndex = 0;
48-
while (nextIndex < additionalArguments.length) {
49-
var nextArgument = additionalArguments[nextIndex];
50-
if (nextArgument == Driver.withFineDependenciesOption) {
51-
arguments.add(nextArgument);
52-
}
53-
nextIndex++;
54-
}
55-
return arguments;
39+
factory ServerDriver({required List<String> arguments}) {
40+
var parsedArgs = Driver.createArgParser().parse(arguments);
41+
42+
var protocolOption = parsedArgs.option(Driver.serverProtocolOption);
43+
var protocol = switch (protocolOption) {
44+
Driver.protocolAnalyzer => ServerProtocol.legacy,
45+
Driver.protocolLsp => ServerProtocol.lsp,
46+
null => throw StateError('No protocol specified'),
47+
_ => throw StateError('Unrecognized protocol $protocolOption'),
48+
};
49+
50+
var useFineDependencies = parsedArgs.wasParsed(
51+
Driver.withFineDependenciesOption,
52+
);
53+
54+
return ServerDriver._(
55+
arguments: [
56+
'--${Driver.serverProtocolOption}=${protocol.flagValue}',
57+
if (useFineDependencies) '--${Driver.withFineDependenciesOption}',
58+
],
59+
protocol: protocol,
60+
);
5661
}
5762

63+
/// Creates a new driver that can be used to communicate with a server.
64+
///
65+
/// When the server is [start]ed, it will use the given [protocol].
66+
///
67+
/// The server is run in a separate process.
68+
ServerDriver._({required this.arguments, required ServerProtocol protocol})
69+
: _protocol = protocol;
70+
5871
/// Returns the path to the `dart` executable.
5972
String get _dartExecutable {
6073
return Platform.resolvedExecutable;
@@ -173,9 +186,8 @@ class ServerDriver {
173186
}
174187
var process = await Process.start(_dartExecutable, [
175188
'language-server',
176-
'--protocol=${_protocol.flagValue}',
177189
'--suppress-analytics',
178-
...filteredAdditionalArguments,
190+
...arguments,
179191
]);
180192
_stdinSink = process.stdin;
181193
if (_protocol == ServerProtocol.lsp) {

0 commit comments

Comments
 (0)