summaryrefslogtreecommitdiff
path: root/support/texlab/src/config.rs
blob: 84de2268fafeb1d744d48f67357d55e261d70b77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use crate::protocol::*;
use futures::lock::Mutex;
use log::{error, warn};
use serde::de::DeserializeOwned;
use std::sync::Arc;

#[derive(Debug)]
pub struct ConfigManager<C> {
    client: Arc<C>,
    client_capabilities: Arc<ClientCapabilities>,
    options: Mutex<Options>,
}

impl<C: LspClient + Send + Sync + 'static> ConfigManager<C> {
    pub fn new(client: Arc<C>, client_capabilities: Arc<ClientCapabilities>) -> Self {
        Self {
            client,
            client_capabilities,
            options: Mutex::default(),
        }
    }

    pub async fn get(&self) -> Options {
        self.options.lock().await.clone()
    }

    pub async fn register(&self) {
        if !self.client_capabilities.has_pull_configuration_support()
            && self.client_capabilities.has_push_configuration_support()
        {
            let registration = Registration {
                id: "pull-config".into(),
                method: "workspace/didChangeConfiguration".into(),
                register_options: None,
            };
            let params = RegistrationParams {
                registrations: vec![registration],
            };

            if let Err(why) = self.client.register_capability(params).await {
                error!(
                    "Failed to register \"workspace/didChangeConfiguration\": {}",
                    why.message
                );
            }
        }
    }

    pub async fn push(&self, options: serde_json::Value) {
        match serde_json::from_value(options) {
            Ok(options) => {
                *self.options.lock().await = options;
            }
            Err(why) => {
                error!("Invalid configuration: {}", why);
            }
        }
    }

    pub async fn pull(&self) -> bool {
        if self.client_capabilities.has_pull_configuration_support() {
            let latex = self.pull_section("latex").await;
            let bibtex = self.pull_section("bibtex").await;

            let new_options = Options {
                latex: Some(latex),
                bibtex: Some(bibtex),
            };
            let mut old_options = self.options.lock().await;
            let has_changed = *old_options != new_options;
            *old_options = new_options;
            has_changed
        } else {
            false
        }
    }

    async fn pull_section<T: DeserializeOwned + Default>(&self, section: &str) -> T {
        let params = ConfigurationParams {
            items: vec![ConfigurationItem {
                section: Some(section.into()),
                scope_uri: None,
            }],
        };

        match self.client.configuration(params).await {
            Ok(json) => match serde_json::from_value::<Vec<T>>(json) {
                Ok(config) => config.into_iter().next().unwrap(),
                Err(_) => {
                    warn!("Invalid configuration: {}", section);
                    T::default()
                }
            },
            Err(why) => {
                error!(
                    "Retrieving configuration for {} failed: {}",
                    section, why.message
                );
                T::default()
            }
        }
    }
}