taplo_lsp/
config.rs

1use figment::{providers::Serialized, Figment};
2use lsp_types::Url;
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5use std::path::PathBuf;
6use taplo_common::{
7    config::Rule,
8    schema::{associations::DEFAULT_CATALOGS, cache::DEFAULT_LRU_CACHE_EXPIRATION_TIME},
9    HashMap,
10};
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
13#[serde(rename_all = "camelCase")]
14pub struct InitConfig {
15    pub cache_path: Option<PathBuf>,
16    #[serde(default = "default_configuration_section")]
17    pub configuration_section: String,
18}
19
20impl Default for InitConfig {
21    fn default() -> Self {
22        Self {
23            cache_path: Default::default(),
24            configuration_section: default_configuration_section(),
25        }
26    }
27}
28
29fn default_configuration_section() -> String {
30    String::from("evenBetterToml")
31}
32
33#[derive(Debug, Clone, Default, Serialize, Deserialize)]
34#[serde(rename_all = "camelCase")]
35pub struct LspConfig {
36    pub taplo: TaploConfig,
37    pub schema: SchemaConfig,
38    pub completion: CompletionConfig,
39    pub syntax: SyntaxConfig,
40    pub formatter: taplo::formatter::OptionsIncompleteCamel,
41    pub rules: Vec<Rule>,
42}
43
44impl LspConfig {
45    pub fn update_from_json(&mut self, json: &Value) -> Result<(), anyhow::Error> {
46        *self = Figment::new()
47            .merge(Serialized::defaults(&self))
48            .merge(Serialized::defaults(json))
49            .extract()?;
50        Ok(())
51    }
52}
53
54#[derive(Debug, Clone, Serialize, Deserialize)]
55#[serde(rename_all = "camelCase")]
56pub struct CompletionConfig {
57    pub max_keys: usize,
58}
59
60impl Default for CompletionConfig {
61    fn default() -> Self {
62        Self { max_keys: 5 }
63    }
64}
65
66#[derive(Debug, Clone, Serialize, Deserialize)]
67#[serde(rename_all = "camelCase")]
68pub struct SyntaxConfig {
69    pub semantic_tokens: bool,
70}
71
72impl Default for SyntaxConfig {
73    fn default() -> Self {
74        Self {
75            semantic_tokens: true,
76        }
77    }
78}
79
80#[derive(Debug, Clone, Serialize, Deserialize)]
81#[serde(rename_all = "camelCase")]
82pub struct SchemaConfig {
83    pub enabled: bool,
84    pub associations: HashMap<String, String>,
85    pub catalogs: Vec<Url>,
86    pub links: bool,
87    pub cache: SchemaCacheConfig,
88}
89
90impl Default for SchemaConfig {
91    fn default() -> Self {
92        Self {
93            enabled: true,
94            associations: Default::default(),
95            catalogs: DEFAULT_CATALOGS
96                .iter()
97                .map(|c| c.parse().unwrap())
98                .collect(),
99            links: false,
100            cache: Default::default(),
101        }
102    }
103}
104
105#[derive(Debug, Clone, Serialize, Deserialize)]
106#[serde(rename_all = "camelCase")]
107pub struct SchemaCacheConfig {
108    pub memory_expiration: u64,
109    pub disk_expiration: u64,
110}
111
112impl Default for SchemaCacheConfig {
113    fn default() -> Self {
114        Self {
115            memory_expiration: DEFAULT_LRU_CACHE_EXPIRATION_TIME.as_secs(),
116            disk_expiration: DEFAULT_LRU_CACHE_EXPIRATION_TIME.as_secs(),
117        }
118    }
119}
120
121#[derive(Debug, Default, Clone, Serialize, Deserialize)]
122#[serde(rename_all = "camelCase")]
123pub struct TaploConfig {
124    pub config_file: TaploConfigFileConfig,
125}
126
127#[derive(Debug, Clone, Serialize, Deserialize)]
128pub struct TaploConfigFileConfig {
129    pub path: Option<PathBuf>,
130    pub enabled: bool,
131}
132
133impl Default for TaploConfigFileConfig {
134    fn default() -> Self {
135        Self {
136            path: Default::default(),
137            enabled: true,
138        }
139    }
140}