|
| 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