request method
Sends a request and awaits its result. Throws LspRequestException
on a server error response, on timeout, or when the connection dies
mid-flight.
Implementation
Future<Object?> request(String method, Object? params, {Duration? timeout}) {
if (status == LspClientStatus.closed) {
return Future.error(
LspRequestException('LSP connection is closed ($method)'),
);
}
final id = ++_nextId;
final completer = Completer<Object?>();
_pending[id] = completer;
_write({'jsonrpc': '2.0', 'id': id, 'method': method, 'params': params});
final effectiveTimeout = timeout ?? requestTimeout;
Timer? timer;
timer = Timer(effectiveTimeout, () {
if (_pending.remove(id) != null) {
completer.completeError(
LspRequestException(
'LSP request $method timed out after ${effectiveTimeout.inMilliseconds}ms',
),
);
}
});
return completer.future.whenComplete(() => timer?.cancel());
}