1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! implementation of the `textDocument/codeAction` request
//!
//! # Usage
//! The [`Server::on_code_action`] endpoint is used to compute commands
//! for a range of a text document. Commonly these commands are displayed
//! in the user interface and may for example represent code fixes or
//! refactoring options.
use crate::TypeProvider;
use crate::workspace::execute_command::CommandContainer;
use crate::{Server, connection::Endpoint};
use crate::connection::Callback;
use serde::Deserialize;
use super::publish_diagnostics::Diagnostic;
use super::{TextDocumentIdentifer, Range};
#[derive(Default, Clone)]
pub(crate) struct CodeActionOptions;
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct CodeActionParams {
text_document: TextDocumentIdentifer,
range: Range,
context: CodeActionContext
}
/// Contains additional diagnostic information about the context in which a code action is run.
#[derive(Deserialize, Debug)]
pub struct CodeActionContext {
/// An array of diagnostics as defined by the server.
pub diagnostics: Vec<Diagnostic>
}
impl CodeActionOptions {
pub(crate) const METHOD: &'static str = "textDocument/codeAction";
pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, CodeActionOptions> {
Endpoint::new(Callback::request(|_, _: CodeActionParams| Vec::<()>::new()))
}
}
impl<T: TypeProvider> Server<T> {
/// Sets the callback that will be called to [compute code actions](self).
///
/// # Argument
/// * `callback` - A callback which is called with the following parameters as soon as code actions are requested:
/// * The server instance receiving the response.
/// * The [`TextDocumentIdentifer`] of the document for which code actions are requested.
/// * The [`Range`] of the document for which code actions are requested.
/// * The [`CodeActionContext`] for which code actions are requested.
/// * `return` - A list of commands to execute.
pub fn on_code_action(&mut self, callback: fn(&mut Server<T>, TextDocumentIdentifer, Range, CodeActionContext) -> Vec<T::Command>) {
self.text_document.code_action.set_callback(Callback::request(move |server, params: CodeActionParams| {
callback(server, params.text_document, params.range, params.context).into_iter()
.map(|command| CommandContainer(command))
.collect::<Vec<_>>()
}))
}
}