summaryrefslogtreecommitdiff
path: root/support/texlab/src/test/server.rs
blob: 77bed68c079bc077bb9da0a4c9020893ce320783 (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
use crate::protocol::*;
use aovec::Aovec;
use async_trait::async_trait;
use chashmap::CHashMap;
use futures::lock::Mutex;
use jsonrpc::server::{Middleware, Result};
use jsonrpc_derive::{jsonrpc_method, jsonrpc_server};

pub struct TestLatexLspServer {
    pub options: Mutex<Options>,
    pub show_message_buf: Aovec<ShowMessageParams>,
    pub register_capability_buf: Aovec<RegistrationParams>,
    pub diagnostics_by_uri: CHashMap<Uri, Vec<Diagnostic>>,
    pub progress_buf: Aovec<ProgressParams>,
    pub work_done_progress_create_buf: Aovec<WorkDoneProgressCreateParams>,
    pub log_message_buf: Aovec<LogMessageParams>,
}

#[jsonrpc_server]
impl TestLatexLspServer {
    pub fn new(options: Options) -> Self {
        let base = 16;
        Self {
            options: Mutex::new(options),
            show_message_buf: Aovec::new(base),
            register_capability_buf: Aovec::new(base),
            diagnostics_by_uri: CHashMap::new(),
            progress_buf: Aovec::new(base),
            work_done_progress_create_buf: Aovec::new(base),
            log_message_buf: Aovec::new(base),
        }
    }

    #[jsonrpc_method("workspace/configuration", kind = "request")]
    pub async fn configuration(&self, params: ConfigurationParams) -> Result<serde_json::Value> {
        let options = self.options.lock().await;
        if params.items[0].section.as_ref().unwrap() == "latex" {
            Ok(serde_json::to_value(vec![options.latex.clone().unwrap_or_default()]).unwrap())
        } else {
            Ok(serde_json::to_value(vec![options.bibtex.clone().unwrap_or_default()]).unwrap())
        }
    }

    #[jsonrpc_method("window/showMessage", kind = "notification")]
    pub async fn show_message(&self, params: ShowMessageParams) {
        self.show_message_buf.push(params);
    }

    #[jsonrpc_method("client/registerCapability", kind = "request")]
    pub async fn register_capability(&self, params: RegistrationParams) -> Result<()> {
        self.register_capability_buf.push(params);
        Ok(())
    }

    #[jsonrpc_method("textDocument/publishDiagnostics", kind = "notification")]
    pub async fn publish_diagnostics(&self, params: PublishDiagnosticsParams) {
        let _ = self
            .diagnostics_by_uri
            .insert(params.uri.into(), params.diagnostics);
    }

    #[jsonrpc_method("$/progress", kind = "notification")]
    pub async fn progress(&self, params: ProgressParams) {
        self.progress_buf.push(params);
    }

    #[jsonrpc_method("window/workDoneProgress/create", kind = "request")]
    pub async fn work_done_progress_create(
        &self,
        params: WorkDoneProgressCreateParams,
    ) -> Result<()> {
        self.work_done_progress_create_buf.push(params);
        Ok(())
    }

    #[jsonrpc_method("window/logMessage", kind = "notification")]
    pub async fn log_message(&self, params: LogMessageParams) {
        self.log_message_buf.push(params);
    }
}

#[async_trait]
impl Middleware for TestLatexLspServer {
    async fn before_message(&self) {}

    async fn after_message(&self) {}
}