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