Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 71241dd

Browse files
author
Dart CI
committed
Version 2.19.0-426.0.dev
Merge 72493a7 into dev
2 parents f709764 + 72493a7 commit 71241dd

30 files changed

+3109
-720
lines changed

pkg/analysis_server/lib/lsp_protocol/protocol_special.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ class ErrorOr<T> extends Either2<ResponseError, T> {
253253
return _which == 2 ? _t2 as T : (throw 'Value is not a result');
254254
}
255255

256+
/// Returns the result or `null` if this object is an error.
257+
T? get resultOrNull {
258+
return _which == 2 ? _t2 as T : null;
259+
}
260+
256261
/// If this object is a result, maps [result] through [f], otherwise returns
257262
/// a new error object representing [error].
258263
FutureOr<ErrorOr<N>> mapResult<N>(FutureOr<ErrorOr<N>> Function(T) f) {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'package:analysis_server/lsp_protocol/protocol.dart';
8+
import 'package:analysis_server/src/lsp/client_capabilities.dart';
9+
import 'package:analysis_server/src/lsp/lsp_analysis_server.dart';
10+
import 'package:analysis_server/src/lsp/mapping.dart';
11+
import 'package:analysis_server/src/protocol_server.dart' hide Position;
12+
import 'package:analysis_server/src/request_handler_mixin.dart';
13+
import 'package:analyzer/source/line_info.dart';
14+
import 'package:meta/meta.dart';
15+
16+
/// A base for classes that produce [CodeAction]s for the LSP handler.
17+
abstract class AbstractCodeActionsProducer
18+
with RequestHandlerMixin<LspAnalysisServer> {
19+
final String path;
20+
final LineInfo lineInfo;
21+
final int offset;
22+
final int length;
23+
final bool Function(CodeActionKind?) shouldIncludeKind;
24+
final LspClientCapabilities capabilities;
25+
26+
@override
27+
final LspAnalysisServer server;
28+
29+
AbstractCodeActionsProducer(
30+
this.server,
31+
this.path,
32+
this.lineInfo, {
33+
required this.offset,
34+
required this.length,
35+
required this.shouldIncludeKind,
36+
required this.capabilities,
37+
});
38+
39+
String get name;
40+
41+
Set<DiagnosticTag> get supportedDiagnosticTags => capabilities.diagnosticTags;
42+
43+
bool get supportsApplyEdit => capabilities.applyEdit;
44+
45+
bool get supportsCodeDescription => capabilities.diagnosticCodeDescription;
46+
47+
bool get supportsLiterals => capabilities.literalCodeActions;
48+
49+
/// Creates a CodeAction to apply this assist. Note: This code will fetch the
50+
/// version of each document being modified so it's important to call this
51+
/// immediately after computing edits to ensure the document is not modified
52+
/// before the version number is read.
53+
@protected
54+
CodeAction createAssistAction(
55+
SourceChange change, String path, LineInfo lineInfo) {
56+
return CodeAction(
57+
title: change.message,
58+
kind: toCodeActionKind(change.id, CodeActionKind.Refactor),
59+
diagnostics: const [],
60+
edit: createWorkspaceEdit(server, change,
61+
allowSnippets: true, filePath: path, lineInfo: lineInfo),
62+
);
63+
}
64+
65+
/// Creates a CodeAction to apply this fix. Note: This code will fetch the
66+
/// version of each document being modified so it's important to call this
67+
/// immediately after computing edits to ensure the document is not modified
68+
/// before the version number is read.
69+
@protected
70+
CodeAction createFixAction(SourceChange change, Diagnostic diagnostic,
71+
String path, LineInfo lineInfo) {
72+
return CodeAction(
73+
title: change.message,
74+
kind: toCodeActionKind(change.id, CodeActionKind.QuickFix),
75+
diagnostics: [diagnostic],
76+
edit: createWorkspaceEdit(server, change,
77+
allowSnippets: true, filePath: path, lineInfo: lineInfo),
78+
);
79+
}
80+
81+
Future<List<CodeActionWithPriority>> getAssistActions();
82+
83+
Future<List<CodeActionWithPriority>> getFixActions();
84+
85+
Future<List<Either2<CodeAction, Command>>> getRefactorActions();
86+
87+
Future<List<Either2<CodeAction, Command>>> getSourceActions();
88+
}
89+
90+
/// A wrapper that contains an LSP [CodeAction] and a server-supplied priority
91+
/// used for sorting before sending to the client.
92+
class CodeActionWithPriority {
93+
final CodeAction action;
94+
final int priority;
95+
96+
CodeActionWithPriority(this.action, this.priority);
97+
}

0 commit comments

Comments
 (0)