[analyzer] Improve null handling in LSP server

Change-Id: I2060912853d131fad1d02ddc68f0b62ab16e3b23
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/196123
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Danny Tuppeny
2021-04-20 19:30:38 +00:00
committed by commit-bot@chromium.org
parent 5662ce3813
commit 97e9186df8
14 changed files with 329 additions and 321 deletions
@@ -38,7 +38,7 @@ class AnalyzerStatusParams implements ToJsonable {
return __result;
}
static bool canParse(Object obj, LspJsonReporter reporter) {
static bool canParse(Object? obj, LspJsonReporter reporter) {
if (obj is Map<String, dynamic>) {
reporter.push('isAnalyzing');
try {
@@ -105,7 +105,7 @@ class ClosingLabel implements ToJsonable {
return __result;
}
static bool canParse(Object obj, LspJsonReporter reporter) {
static bool canParse(Object? obj, LspJsonReporter reporter) {
if (obj is Map<String, dynamic>) {
reporter.push('range');
try {
@@ -197,7 +197,7 @@ class CompletionItemResolutionInfo implements ToJsonable {
return __result;
}
static bool canParse(Object obj, LspJsonReporter reporter) {
static bool canParse(Object? obj, LspJsonReporter reporter) {
if (obj is Map<String, dynamic>) {
reporter.push('file');
try {
@@ -313,7 +313,7 @@ class DartCompletionItemResolutionInfo
return __result;
}
static bool canParse(Object obj, LspJsonReporter reporter) {
static bool canParse(Object? obj, LspJsonReporter reporter) {
if (obj is Map<String, dynamic>) {
reporter.push('libId');
try {
@@ -492,7 +492,7 @@ class DartDiagnosticServer implements ToJsonable {
return __result;
}
static bool canParse(Object obj, LspJsonReporter reporter) {
static bool canParse(Object? obj, LspJsonReporter reporter) {
if (obj is Map<String, dynamic>) {
reporter.push('port');
try {
@@ -590,7 +590,7 @@ class Element implements ToJsonable {
return __result;
}
static bool canParse(Object obj, LspJsonReporter reporter) {
static bool canParse(Object? obj, LspJsonReporter reporter) {
if (obj is Map<String, dynamic>) {
reporter.push('range');
try {
@@ -782,7 +782,7 @@ class FlutterOutline implements ToJsonable {
return __result;
}
static bool canParse(Object obj, LspJsonReporter reporter) {
static bool canParse(Object? obj, LspJsonReporter reporter) {
if (obj is Map<String, dynamic>) {
reporter.push('kind');
try {
@@ -973,7 +973,7 @@ class FlutterOutlineAttribute implements ToJsonable {
return __result;
}
static bool canParse(Object obj, LspJsonReporter reporter) {
static bool canParse(Object? obj, LspJsonReporter reporter) {
if (obj is Map<String, dynamic>) {
reporter.push('name');
try {
@@ -1090,7 +1090,7 @@ class Outline implements ToJsonable {
return __result;
}
static bool canParse(Object obj, LspJsonReporter reporter) {
static bool canParse(Object? obj, LspJsonReporter reporter) {
if (obj is Map<String, dynamic>) {
reporter.push('element');
try {
@@ -1218,7 +1218,7 @@ class PubPackageCompletionItemResolutionInfo
return __result;
}
static bool canParse(Object obj, LspJsonReporter reporter) {
static bool canParse(Object? obj, LspJsonReporter reporter) {
if (obj is Map<String, dynamic>) {
reporter.push('packageName');
try {
@@ -1328,7 +1328,7 @@ class PublishClosingLabelsParams implements ToJsonable {
return __result;
}
static bool canParse(Object obj, LspJsonReporter reporter) {
static bool canParse(Object? obj, LspJsonReporter reporter) {
if (obj is Map<String, dynamic>) {
reporter.push('uri');
try {
@@ -1419,7 +1419,7 @@ class PublishFlutterOutlineParams implements ToJsonable {
return __result;
}
static bool canParse(Object obj, LspJsonReporter reporter) {
static bool canParse(Object? obj, LspJsonReporter reporter) {
if (obj is Map<String, dynamic>) {
reporter.push('uri');
try {
@@ -1504,7 +1504,7 @@ class PublishOutlineParams implements ToJsonable {
return __result;
}
static bool canParse(Object obj, LspJsonReporter reporter) {
static bool canParse(Object? obj, LspJsonReporter reporter) {
if (obj is Map<String, dynamic>) {
reporter.push('uri');
try {
@@ -1602,7 +1602,7 @@ class SnippetTextEdit implements TextEdit, ToJsonable {
return __result;
}
static bool canParse(Object obj, LspJsonReporter reporter) {
static bool canParse(Object? obj, LspJsonReporter reporter) {
if (obj is Map<String, dynamic>) {
reporter.push('insertTextFormat');
try {
File diff suppressed because it is too large Load Diff
@@ -266,9 +266,9 @@ abstract class IncomingMessage {
/// A helper to allow handlers to declare both a JSON validation function and
/// parse function.
class LspJsonHandler<T> {
final bool Function(Map<String, Object>, LspJsonReporter reporter)
final bool Function(Map<String, Object?>?, LspJsonReporter reporter)
validateParams;
final T Function(Map<String, Object>) convertParams;
final T Function(Map<String, Object?>) convertParams;
const LspJsonHandler(this.validateParams, this.convertParams);
}
@@ -22,7 +22,7 @@ class LspClientConfiguration {
bool get completeFunctionCalls => _settings['completeFunctionCalls'] ?? false;
bool get enableSdkFormatter => _settings['enableSdkFormatter'] ?? true;
int get lineLength => _settings['lineLength'];
int? get lineLength => _settings['lineLength'];
/// A preview flag for enabling commit characters for completions.
///
@@ -45,10 +45,10 @@ class PerformRefactorCommandHandler extends SimpleEditCommandHandler {
String kind = arguments[0];
String path = arguments[1];
int docVersion = arguments[2];
int? docVersion = arguments[2];
int offset = arguments[3];
int length = arguments[4];
Map<String, dynamic> options = arguments[5];
Map<String, dynamic>? options = arguments[5];
final result = await requireResolvedUnit(path);
return result.mapResult((result) async {
@@ -9,7 +9,7 @@ import 'package:analysis_server/lsp_protocol/protocol_special.dart';
import 'package:analysis_server/src/lsp/handlers/handlers.dart';
import 'package:analysis_server/src/lsp/lsp_analysis_server.dart';
class ExitMessageHandler extends MessageHandler<void, void> {
class ExitMessageHandler extends MessageHandler<Null, Null> {
final bool clientDidCallShutdown;
ExitMessageHandler(
@@ -21,10 +21,10 @@ class ExitMessageHandler extends MessageHandler<void, void> {
Method get handlesMessage => Method.exit;
@override
LspJsonHandler<void> get jsonHandler => NullJsonHandler;
LspJsonHandler<Null> get jsonHandler => NullJsonHandler;
@override
Future<ErrorOr<void>> handle(void _, CancellationToken token) async {
Future<ErrorOr<Null>> handle(Null _, CancellationToken token) async {
// Set a flag that the server shutdown is being controlled here to ensure
// that the normal code that shuts down the server when the channel closes
// does not fire.
@@ -9,7 +9,7 @@ import 'package:analysis_server/src/lsp/lsp_analysis_server.dart';
/// A [MessageHandler] that rejects specific tpyes of messages with a given
/// error code/message.
class RejectMessageHandler extends MessageHandler<Object?, void> {
class RejectMessageHandler extends MessageHandler<Object?, Null> {
@override
final Method handlesMessage;
final ErrorCodes errorCode;
@@ -19,10 +19,10 @@ class RejectMessageHandler extends MessageHandler<Object?, void> {
: super(server);
@override
LspJsonHandler<Object?> get jsonHandler => NullJsonHandler;
LspJsonHandler<Null> get jsonHandler => NullJsonHandler;
@override
ErrorOr<void> handle(void _, CancellationToken token) {
ErrorOr<Null> handle(Object? _, CancellationToken token) {
return error(errorCode, errorMessage, null);
}
}
@@ -8,16 +8,16 @@ import 'package:analysis_server/src/lsp/handlers/handler_states.dart';
import 'package:analysis_server/src/lsp/handlers/handlers.dart';
import 'package:analysis_server/src/lsp/lsp_analysis_server.dart';
class ShutdownMessageHandler extends MessageHandler<void, void> {
class ShutdownMessageHandler extends MessageHandler<Null, Null> {
ShutdownMessageHandler(LspAnalysisServer server) : super(server);
@override
Method get handlesMessage => Method.shutdown;
@override
LspJsonHandler<void> get jsonHandler => NullJsonHandler;
LspJsonHandler<Null> get jsonHandler => NullJsonHandler;
@override
ErrorOr<void> handle(void _, CancellationToken token) {
ErrorOr<Null> handle(void _, CancellationToken token) {
// Move to the Shutting Down state so we won't process any more
// requests and the Exit notification will know it was a clean shutdown.
server.messageHandler = ShuttingDownStateMessageHandler(server);
@@ -144,7 +144,9 @@ abstract class MessageHandler<P, R>
);
}
final params = jsonHandler.convertParams(message.params);
final params = message.params != null
? jsonHandler.convertParams(message.params)
: null as P;
return handle(params, token);
}
}
@@ -79,7 +79,7 @@ ErrorOr<Pair<String, List<plugin.SourceEdit>>> applyAndConvertEditsToServer(
ErrorOr<List<TextEdit>?> generateEditsForFormatting(
ParsedUnitResult result,
int lineLength, {
int? lineLength, {
Range? range,
}) {
final unformattedSource = result.content;
@@ -65,7 +65,7 @@ class PackageDetailsCache {
}
final packagesJson = json['packages'];
if (packagesJson is! List<Object>) {
if (packagesJson is! List<Object?>) {
return null;
}
@@ -194,7 +194,7 @@ Iterable<String> _wrapLines(List<String> lines, int maxLength) sync* {
void _writeCanParseMethod(IndentableStringBuffer buffer, Interface interface) {
buffer
..writeIndentedln(
'static bool canParse(Object obj, LspJsonReporter reporter) {')
'static bool canParse(Object? obj, LspJsonReporter reporter) {')
..indent()
..writeIndentedln('if (obj is Map<String, dynamic>) {')
..indent();
@@ -125,6 +125,9 @@ String? getImprovedType(String interfaceName, String? fieldName) {
'ProgressParams': {
'value': 'object',
},
'SemanticTokens': {
'data': 'int[]',
},
'ServerCapabilities': {
'changeNotifications': 'bool',
},
@@ -672,7 +672,10 @@ class Parser {
if (improveTypes) {
final improvedTypeName = getImprovedType(containerName, fieldName);
if (improvedTypeName != null) {
type = Type.identifier(improvedTypeName);
type = improvedTypeName.endsWith('[]')
? ArrayType(Type.identifier(
improvedTypeName.substring(0, improvedTypeName.length - 2)))
: Type.identifier(improvedTypeName);
}
}
return type;