sync_lsp/text_document/
document_highlight.rs

1//! implementation of the `textDocument/documentHighlight` request
2//! 
3//! # Usage
4//! Whenever a user selects a range in the document, the server might want to highlight
5//! references or important parts of the document via [`Server::on_document_highlight`]. For example, if the user selects a
6//! break statement, the loop it breaks could be highlighted.
7
8use crate::TypeProvider;
9use crate::{Server, connection::Endpoint};
10use crate::connection::Callback;
11use super::{TextDocumentIdentifer, Position, Range, TextDocumentPositionParams};
12use serde::Serialize;
13use serde_repr::Serialize_repr;
14
15#[derive(Default, Clone)]
16pub(crate) struct DocumentHighlightOptions;
17
18/// A highlighted region of a document.
19#[derive(Serialize, Debug)]
20#[serde(rename_all = "camelCase")]
21pub struct DocumentHighlight  {
22    /// The range to highlight.
23    pub range: Range,
24    /// The kind of highlight, defaults to [`DocumentHighlightKind::Text`]
25    pub kind: Option<DocumentHighlightKind>
26}
27
28/// This enum changes the way highlights are rendered.
29#[repr(i32)]
30#[derive(Serialize_repr, Debug)]
31pub enum DocumentHighlightKind {
32    /// A textual symbol.
33    Text = 1,
34    /// A immutable symbol, like a constant variable.
35    Read = 2,
36    /// A mutable symbol, like a variable.
37    Write = 3
38}
39
40impl DocumentHighlightOptions {
41
42    pub(crate) const METHOD: &'static str = "textDocument/documentHighlight";
43    
44    pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, DocumentHighlightOptions> {
45        Endpoint::new(Callback::request(|_, _: TextDocumentPositionParams| Vec::<DocumentHighlight>::new()))
46    }
47}
48
49impl<T: TypeProvider> Server<T> {
50
51    /// Sets the callback that will be called to [highlight parts of a file](self).
52    /// 
53    /// # Argument
54    /// * `callback` - A callback which is called with the following parameters as soon as a highlight is requested:
55    ///     * The server instance receiving the response.
56    ///     * The [`TextDocumentIdentifer`] of the target document.
57    ///     * The [`Position`] of the cursor.
58    ///     * `return` - A list of highlights to display.
59
60    pub fn on_document_highlight(&mut self, callback: fn(&mut Server<T>, TextDocumentIdentifer, Position) -> Vec<DocumentHighlight>) {
61        self.text_document.document_highlight.set_callback(Callback::request(move |server, params: TextDocumentPositionParams| {
62            callback(server, params.text_document, params.position)
63        }))
64    }
65}