tinymist_query/
color_presentation.rs

1use typst::foundations::Repr;
2
3use crate::prelude::*;
4
5/// The [`textDocument/colorPresentation`] request is sent from the client to
6/// the server to obtain a list of presentations for a color value at a given
7/// location.
8///
9/// [`textDocument/colorPresentation`]: https://microsoft.github.io/language-server-protocol/specification#textDocument_colorPresentation
10///
11/// Clients can use the result to:
12///
13/// * Modify a color reference
14/// * Show in a color picker and let users pick one of the presentations
15///
16/// # Compatibility
17///
18/// This request was introduced in specification version 3.6.0.
19///
20/// This request has no special capabilities and registration options since it
21/// is sent as a resolve request for the [`textDocument/documentColor`] request.
22///
23/// [`textDocument/documentColor`]: https://microsoft.github.io/language-server-protocol/specification#textDocument_documentColor
24#[derive(Debug, Clone)]
25pub struct ColorPresentationRequest {
26    /// The path of the document to request color presentations for.
27    pub path: PathBuf,
28    /// The color to request presentations for.
29    pub color: lsp_types::Color,
30    /// The range of the color to request presentations for.
31    pub range: LspRange,
32}
33
34impl ColorPresentationRequest {
35    /// Serve the request.
36    pub fn request(self) -> Option<Vec<ColorPresentation>> {
37        let color = typst::visualize::Color::Rgb(typst::visualize::Rgb::new(
38            self.color.red,
39            self.color.green,
40            self.color.blue,
41            self.color.alpha,
42        ));
43        Some(vec![
44            simple(format!("{:?}", color.to_hex())),
45            simple(color.to_rgb().repr().to_string()),
46            simple(color.to_luma().repr().to_string()),
47            simple(color.to_oklab().repr().to_string()),
48            simple(color.to_oklch().repr().to_string()),
49            simple(color.to_rgb().repr().to_string()),
50            simple(color.to_linear_rgb().repr().to_string()),
51            simple(color.to_cmyk().repr().to_string()),
52            simple(color.to_hsl().repr().to_string()),
53            simple(color.to_hsv().repr().to_string()),
54        ])
55    }
56}
57
58fn simple(label: String) -> ColorPresentation {
59    ColorPresentation {
60        label,
61        ..ColorPresentation::default()
62    }
63}