sync_lsp/text_document/
rename.rs

1//! implementation of the `textDocument/rename` request
2//! 
3//! # Usage
4//! If a user chooses to rename a symbol, the server can be notified via [`Server::on_rename`]
5//! and also rename all references to that symbol.
6
7use crate::TypeProvider;
8use crate::workspace::apply_edit::WorkspaceEdit;
9use crate::{Server, connection::Endpoint};
10use crate::connection::Callback;
11use super::{TextDocumentIdentifer, Position};
12use serde::Deserialize;
13
14#[derive(Default, Clone)]
15pub(crate) struct RenameOptions;
16
17#[derive(Deserialize)]
18#[serde(rename_all = "camelCase")]
19struct RenameParams {
20    text_document: TextDocumentIdentifer,
21    position: Position,
22    new_name: String
23}
24
25impl RenameOptions {
26    pub(crate) const METHOD: &'static str = "textDocument/rename";
27    
28    pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, RenameOptions> {
29        Endpoint::new(Callback::request(|_, _: RenameParams| WorkspaceEdit::default()))
30    }
31}
32
33impl<T: TypeProvider> Server<T> {
34
35    /// Sets the callback that will be called to [rename symbols](self).
36    /// 
37    /// # Argument
38    /// * `callback` - A callback which is called with the following parameters as soon as a symbol is renamed:
39    ///     * The server instance receiving the response.
40    ///     * The [`TextDocumentIdentifer`] of the target document.
41    ///     * The [`Position`] of the cursor.
42    ///     * The new name of the symbol.
43    ///     * `return` - A [`WorkspaceEdit`] that contains the changes to apply to the workspace.
44
45    pub fn on_rename(&mut self, callback: fn(&mut Server<T>, TextDocumentIdentifer, Position, String) -> WorkspaceEdit) {
46        self.text_document.rename.set_callback(Callback::request(move |server, params: RenameParams | {
47            callback(server, params.text_document, params.position, params.new_name)
48        }))
49    }
50}