tinymist_query/
document_color.rs

1use crate::{SemanticRequest, analysis::ColorExprWorker, prelude::*};
2
3/// The [`textDocument/documentColor`] request is sent from the client to the
4/// server to list all color references found in a given text document. Along
5/// with the range, a color value in RGB is returned.
6///
7/// [`textDocument/documentColor`]: https://microsoft.github.io/language-server-protocol/specification#textDocument_documentColor
8///
9/// Clients can use the result to decorate color references in an editor. For
10/// example:
11///
12/// * Color boxes showing the actual color next to the reference
13/// * Show a color picker when a color reference is edited
14///
15/// # Compatibility
16///
17/// This request was introduced in specification version 3.6.0.
18#[derive(Debug, Clone)]
19pub struct DocumentColorRequest {
20    /// The path of the document to request color for.
21    pub path: PathBuf,
22}
23
24impl SemanticRequest for DocumentColorRequest {
25    type Response = Vec<ColorInformation>;
26
27    fn request(self, ctx: &mut LocalContext) -> Option<Self::Response> {
28        let source = ctx.source_by_path(&self.path).ok()?;
29
30        let mut worker = ColorExprWorker::new(ctx, source.clone());
31        worker.work(LinkedNode::new(source.root()))?;
32        Some(worker.colors)
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39    use crate::tests::*;
40
41    #[test]
42    fn test() {
43        snapshot_testing("document_color", &|ctx, path| {
44            let request = DocumentColorRequest { path: path.clone() };
45
46            let result = request.request(ctx);
47            assert_snapshot!(JsonRepr::new_redacted(result, &REDACT_LOC));
48        });
49    }
50}