summaryrefslogtreecommitdiff
path: root/support/texlab/src/server.rs
blob: e22fe4f5c96009e0e4fbefd277dd9f344d616749 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
#[cfg(feature = "citation")]
use crate::citeproc::render_citation;

use crate::{
    build::BuildProvider,
    completion::{CompletionItemData, CompletionProvider},
    components::COMPONENT_DATABASE,
    config::ConfigManager,
    definition::DefinitionProvider,
    diagnostics::DiagnosticsManager,
    feature::{DocumentView, FeatureProvider, FeatureRequest},
    folding::FoldingProvider,
    forward_search,
    highlight::HighlightProvider,
    hover::HoverProvider,
    link::LinkProvider,
    protocol::*,
    reference::ReferenceProvider,
    rename::{PrepareRenameProvider, RenameProvider},
    symbol::{document_symbols, workspace_symbols, SymbolProvider},
    syntax::{bibtex, latexindent, CharStream, SyntaxNode},
    tex::{Distribution, DistributionKind, KpsewhichError},
    workspace::{DocumentContent, Workspace},
};
use async_trait::async_trait;
use chashmap::CHashMap;
use futures::lock::Mutex;
use jsonrpc::{server::Result, Middleware};
use jsonrpc_derive::{jsonrpc_method, jsonrpc_server};
use log::{debug, error, info, warn};
use once_cell::sync::{Lazy, OnceCell};
use std::{mem, path::PathBuf, sync::Arc};

pub struct LatexLspServer<C> {
    distro: Arc<dyn Distribution>,
    client: Arc<C>,
    client_capabilities: OnceCell<Arc<ClientCapabilities>>,
    current_dir: Arc<PathBuf>,
    config_manager: OnceCell<ConfigManager<C>>,
    action_manager: ActionManager,
    workspace: Workspace,
    build_provider: BuildProvider<C>,
    completion_provider: CompletionProvider,
    definition_provider: DefinitionProvider,
    folding_provider: FoldingProvider,
    highlight_provider: HighlightProvider,
    link_provider: LinkProvider,
    reference_provider: ReferenceProvider,
    prepare_rename_provider: PrepareRenameProvider,
    rename_provider: RenameProvider,
    symbol_provider: SymbolProvider,
    hover_provider: HoverProvider,
    diagnostics_manager: DiagnosticsManager,
    last_position_by_uri: CHashMap<Uri, Position>,
}

#[jsonrpc_server]
impl<C: LspClient + Send + Sync + 'static> LatexLspServer<C> {
    pub fn new(distro: Arc<dyn Distribution>, client: Arc<C>, current_dir: Arc<PathBuf>) -> Self {
        let workspace = Workspace::new(distro.clone(), Arc::clone(&current_dir));
        Self {
            distro,
            client: Arc::clone(&client),
            client_capabilities: OnceCell::new(),
            current_dir,
            config_manager: OnceCell::new(),
            action_manager: ActionManager::default(),
            workspace,
            build_provider: BuildProvider::new(client),
            completion_provider: CompletionProvider::new(),
            definition_provider: DefinitionProvider::new(),
            folding_provider: FoldingProvider::new(),
            highlight_provider: HighlightProvider::new(),
            link_provider: LinkProvider::new(),
            reference_provider: ReferenceProvider::new(),
            prepare_rename_provider: PrepareRenameProvider::new(),
            rename_provider: RenameProvider::new(),
            symbol_provider: SymbolProvider::new(),
            hover_provider: HoverProvider::new(),
            diagnostics_manager: DiagnosticsManager::default(),
            last_position_by_uri: CHashMap::new(),
        }
    }

    fn client_capabilities(&self) -> Arc<ClientCapabilities> {
        Arc::clone(
            self.client_capabilities
                .get()
                .expect("initialize has not been called"),
        )
    }

    fn config_manager(&self) -> &ConfigManager<C> {
        self.config_manager
            .get()
            .expect("initialize has not been called")
    }

    #[jsonrpc_method("initialize", kind = "request")]
    pub async fn initialize(&self, params: InitializeParams) -> Result<InitializeResult> {
        self.client_capabilities
            .set(Arc::new(params.capabilities))
            .expect("initialize was called two times");

        let _ = self.config_manager.set(ConfigManager::new(
            Arc::clone(&self.client),
            self.client_capabilities(),
        ));

        let capabilities = ServerCapabilities {
            text_document_sync: Some(TextDocumentSyncCapability::Options(
                TextDocumentSyncOptions {
                    open_close: Some(true),
                    change: Some(TextDocumentSyncKind::Full),
                    will_save: None,
                    will_save_wait_until: None,
                    save: Some(SaveOptions {
                        include_text: Some(false),
                    }),
                },
            )),
            hover_provider: Some(true),
            completion_provider: Some(CompletionOptions {
                resolve_provider: Some(true),
                trigger_characters: Some(vec![
                    "\\".into(),
                    "{".into(),
                    "}".into(),
                    "@".into(),
                    "/".into(),
                    " ".into(),
                ]),
                ..CompletionOptions::default()
            }),
            definition_provider: Some(true),
            references_provider: Some(true),
            document_highlight_provider: Some(true),
            document_symbol_provider: Some(true),
            workspace_symbol_provider: Some(true),
            document_formatting_provider: Some(true),
            rename_provider: Some(RenameProviderCapability::Options(RenameOptions {
                prepare_provider: Some(true),
                work_done_progress_options: WorkDoneProgressOptions::default(),
            })),
            document_link_provider: Some(DocumentLinkOptions {
                resolve_provider: Some(false),
                work_done_progress_options: WorkDoneProgressOptions::default(),
            }),
            folding_range_provider: Some(FoldingRangeProviderCapability::Simple(true)),
            ..ServerCapabilities::default()
        };

        Lazy::force(&COMPONENT_DATABASE);
        Ok(InitializeResult {
            capabilities,
            server_info: Some(ServerInfo {
                name: "TexLab".to_owned(),
                version: Some(env!("CARGO_PKG_VERSION").to_owned()),
            }),
        })
    }

    #[jsonrpc_method("initialized", kind = "notification")]
    pub async fn initialized(&self, _params: InitializedParams) {
        self.action_manager.push(Action::PullConfiguration).await;
        self.action_manager.push(Action::RegisterCapabilities).await;
        self.action_manager.push(Action::LoadDistribution).await;
        self.action_manager.push(Action::PublishDiagnostics).await;
    }

    #[jsonrpc_method("shutdown", kind = "request")]
    pub async fn shutdown(&self, _params: ()) -> Result<()> {
        Ok(())
    }

    #[jsonrpc_method("exit", kind = "notification")]
    pub async fn exit(&self, _params: ()) {}

    #[jsonrpc_method("$/cancelRequest", kind = "notification")]
    pub async fn cancel_request(&self, _params: CancelParams) {}

    #[jsonrpc_method("textDocument/didOpen", kind = "notification")]
    pub async fn did_open(&self, params: DidOpenTextDocumentParams) {
        let uri = params.text_document.uri.clone();
        let options = self.config_manager().get().await;
        self.workspace.add(params.text_document, &options).await;
        self.action_manager
            .push(Action::DetectRoot(uri.clone().into()))
            .await;
        self.action_manager
            .push(Action::RunLinter(uri.into(), LintReason::Save))
            .await;
        self.action_manager.push(Action::PublishDiagnostics).await;
    }

    #[jsonrpc_method("textDocument/didChange", kind = "notification")]
    pub async fn did_change(&self, params: DidChangeTextDocumentParams) {
        let options = self.config_manager().get().await;
        for change in params.content_changes {
            let uri = params.text_document.uri.clone();
            self.workspace
                .update(uri.into(), change.text, &options)
                .await;
        }
        self.action_manager
            .push(Action::RunLinter(
                params.text_document.uri.clone().into(),
                LintReason::Change,
            ))
            .await;
        self.action_manager.push(Action::PublishDiagnostics).await;
    }

    #[jsonrpc_method("textDocument/didSave", kind = "notification")]
    pub async fn did_save(&self, params: DidSaveTextDocumentParams) {
        self.action_manager
            .push(Action::Build(params.text_document.uri.clone().into()))
            .await;

        self.action_manager
            .push(Action::RunLinter(
                params.text_document.uri.into(),
                LintReason::Save,
            ))
            .await;
        self.action_manager.push(Action::PublishDiagnostics).await;
    }

    #[jsonrpc_method("textDocument/didClose", kind = "notification")]
    pub async fn did_close(&self, _params: DidCloseTextDocumentParams) {}

    #[jsonrpc_method("workspace/didChangeConfiguration", kind = "notification")]
    pub async fn did_change_configuration(&self, params: DidChangeConfigurationParams) {
        let config_manager = self.config_manager();
        config_manager.push(params.settings).await;
        let options = config_manager.get().await;
        self.workspace.reparse(&options).await;
    }

    #[jsonrpc_method("window/workDoneProgress/cancel", kind = "notification")]
    pub async fn work_done_progress_cancel(&self, params: WorkDoneProgressCancelParams) {
        self.build_provider.cancel(params.token).await;
    }

    #[jsonrpc_method("textDocument/completion", kind = "request")]
    pub async fn completion(&self, params: CompletionParams) -> Result<CompletionList> {
        let req = self
            .make_feature_request(params.text_document_position.text_document.as_uri(), params)
            .await?;

        self.last_position_by_uri.insert(
            req.current().uri.clone(),
            req.params.text_document_position.position,
        );

        Ok(CompletionList {
            is_incomplete: true,
            items: self.completion_provider.execute(&req).await,
        })
    }

    #[jsonrpc_method("completionItem/resolve", kind = "request")]
    pub async fn completion_resolve(&self, mut item: CompletionItem) -> Result<CompletionItem> {
        let data: CompletionItemData = serde_json::from_value(item.data.clone().unwrap()).unwrap();
        match data {
            CompletionItemData::Package | CompletionItemData::Class => {
                item.documentation = COMPONENT_DATABASE
                    .documentation(&item.label)
                    .map(Documentation::MarkupContent);
            }
            #[cfg(feature = "citation")]
            CompletionItemData::Citation { uri, key } => {
                let snapshot = self.workspace.get().await;
                if let Some(doc) = snapshot.find(&uri) {
                    if let DocumentContent::Bibtex(tree) = &doc.content {
                        let markup = render_citation(&tree, &key);
                        item.documentation = markup.map(Documentation::MarkupContent);
                    }
                }
            }
            _ => {}
        };
        Ok(item)
    }

    #[jsonrpc_method("textDocument/hover", kind = "request")]
    pub async fn hover(&self, params: TextDocumentPositionParams) -> Result<Option<Hover>> {
        let req = self
            .make_feature_request(params.text_document.as_uri(), params)
            .await?;

        self.last_position_by_uri
            .insert(req.current().uri.clone(), req.params.position);

        Ok(self.hover_provider.execute(&req).await)
    }

    #[jsonrpc_method("textDocument/definition", kind = "request")]
    pub async fn definition(
        &self,
        params: TextDocumentPositionParams,
    ) -> Result<DefinitionResponse> {
        let req = self
            .make_feature_request(params.text_document.as_uri(), params)
            .await?;
        let results = self.definition_provider.execute(&req).await;
        let response = if req.client_capabilities.has_definition_link_support() {
            DefinitionResponse::LocationLinks(results)
        } else {
            DefinitionResponse::Locations(
                results
                    .into_iter()
                    .map(|link| Location::new(link.target_uri, link.target_selection_range))
                    .collect(),
            )
        };

        Ok(response)
    }

    #[jsonrpc_method("textDocument/references", kind = "request")]
    pub async fn references(&self, params: ReferenceParams) -> Result<Vec<Location>> {
        let req = self
            .make_feature_request(params.text_document_position.as_uri(), params)
            .await?;
        Ok(self.reference_provider.execute(&req).await)
    }

    #[jsonrpc_method("textDocument/documentHighlight", kind = "request")]
    pub async fn document_highlight(
        &self,
        params: TextDocumentPositionParams,
    ) -> Result<Vec<DocumentHighlight>> {
        let req = self
            .make_feature_request(params.text_document.as_uri(), params)
            .await?;
        Ok(self.highlight_provider.execute(&req).await)
    }

    #[jsonrpc_method("workspace/symbol", kind = "request")]
    pub async fn workspace_symbol(
        &self,
        params: WorkspaceSymbolParams,
    ) -> Result<Vec<SymbolInformation>> {
        let distro = self.distro.clone();
        let client_capabilities = self.client_capabilities();
        let snapshot = self.workspace.get().await;
        let options = self.config_manager().get().await;
        let symbols = workspace_symbols(
            distro,
            client_capabilities,
            snapshot,
            &options,
            Arc::clone(&self.current_dir),
            &params,
        )
        .await;
        Ok(symbols)
    }

    #[jsonrpc_method("textDocument/documentSymbol", kind = "request")]
    pub async fn document_symbol(
        &self,
        params: DocumentSymbolParams,
    ) -> Result<DocumentSymbolResponse> {
        let req = self
            .make_feature_request(params.text_document.as_uri(), params)
            .await?;

        let symbols = self.symbol_provider.execute(&req).await;
        let response = document_symbols(
            &req.client_capabilities,
            req.snapshot(),
            &req.current().uri,
            &req.options,
            &req.current_dir,
            symbols.into_iter().map(Into::into).collect(),
        );
        Ok(response)
    }

    #[jsonrpc_method("textDocument/documentLink", kind = "request")]
    pub async fn document_link(&self, params: DocumentLinkParams) -> Result<Vec<DocumentLink>> {
        let req = self
            .make_feature_request(params.text_document.as_uri(), params)
            .await?;
        Ok(self.link_provider.execute(&req).await)
    }

    #[jsonrpc_method("textDocument/formatting", kind = "request")]
    pub async fn formatting(&self, params: DocumentFormattingParams) -> Result<Vec<TextEdit>> {
        let req = self
            .make_feature_request(params.text_document.as_uri(), params)
            .await?;
        let mut edits = Vec::new();
        match &req.current().content {
            DocumentContent::Latex(_) => {
                Self::run_latexindent(&req.current().text, "tex", &mut edits).await;
            }
            DocumentContent::Bibtex(tree) => {
                let options = req
                    .options
                    .bibtex
                    .clone()
                    .and_then(|opts| opts.formatting)
                    .unwrap_or_default();

                match options.formatter.unwrap_or_default() {
                    BibtexFormatter::Texlab => {
                        let params = bibtex::FormattingParams {
                            tab_size: req.params.options.tab_size as usize,
                            insert_spaces: req.params.options.insert_spaces,
                            options: &options,
                        };

                        for node in tree.children(tree.root) {
                            let should_format = match &tree.graph[node] {
                                bibtex::Node::Preamble(_) | bibtex::Node::String(_) => true,
                                bibtex::Node::Entry(entry) => !entry.is_comment(),
                                _ => false,
                            };
                            if should_format {
                                let text = bibtex::format(&tree, node, params);
                                edits.push(TextEdit::new(tree.graph[node].range(), text));
                            }
                        }
                    }
                    BibtexFormatter::Latexindent => {
                        Self::run_latexindent(&req.current().text, "bib", &mut edits).await;
                    }
                }
            }
        }
        Ok(edits)
    }

    async fn run_latexindent(old_text: &str, extension: &str, edits: &mut Vec<TextEdit>) {
        match latexindent::format(old_text, extension).await {
            Ok(new_text) => {
                let mut stream = CharStream::new(&old_text);
                while stream.next().is_some() {}
                let range = Range::new(Position::new(0, 0), stream.current_position);
                edits.push(TextEdit::new(range, new_text));
            }
            Err(why) => {
                debug!("Failed to run latexindent.pl: {}", why);
            }
        }
    }

    #[jsonrpc_method("textDocument/prepareRename", kind = "request")]
    pub async fn prepare_rename(
        &self,
        params: TextDocumentPositionParams,
    ) -> Result<Option<Range>> {
        let req = self
            .make_feature_request(params.text_document.as_uri(), params)
            .await?;
        Ok(self.prepare_rename_provider.execute(&req).await)
    }

    #[jsonrpc_method("textDocument/rename", kind = "request")]
    pub async fn rename(&self, params: RenameParams) -> Result<Option<WorkspaceEdit>> {
        let req = self
            .make_feature_request(params.text_document_position.text_document.as_uri(), params)
            .await?;
        Ok(self.rename_provider.execute(&req).await)
    }

    #[jsonrpc_method("textDocument/foldingRange", kind = "request")]
    pub async fn folding_range(&self, params: FoldingRangeParams) -> Result<Vec<FoldingRange>> {
        let req = self
            .make_feature_request(params.text_document.as_uri(), params)
            .await?;
        Ok(self.folding_provider.execute(&req).await)
    }

    #[jsonrpc_method("textDocument/build", kind = "request")]
    pub async fn build(&self, params: BuildParams) -> Result<BuildResult> {
        let req = self
            .make_feature_request(params.text_document.as_uri(), params)
            .await?;

        let pos = self
            .last_position_by_uri
            .get(&req.current().uri)
            .map(|pos| *pos)
            .unwrap_or_default();

        let res = self.build_provider.execute(&req).await;

        if req
            .options
            .latex
            .and_then(|opts| opts.build)
            .unwrap_or_default()
            .forward_search_after()
            && !self.build_provider.is_building()
        {
            let params = TextDocumentPositionParams::new(req.params.text_document, pos);
            self.forward_search(params).await?;
        }

        Ok(res)
    }

    #[jsonrpc_method("textDocument/forwardSearch", kind = "request")]
    pub async fn forward_search(
        &self,
        params: TextDocumentPositionParams,
    ) -> Result<ForwardSearchResult> {
        let req = self
            .make_feature_request(params.text_document.as_uri(), params)
            .await?;

        forward_search::search(
            &req.view.snapshot,
            &req.current().uri,
            req.params.position.line,
            &req.options,
            &self.current_dir,
        )
        .await
        .ok_or_else(|| "Unable to execute forward search".into())
    }

    #[jsonrpc_method("$/detectRoot", kind = "request")]
    pub async fn detect_root(&self, params: TextDocumentIdentifier) -> Result<()> {
        let options = self.config_manager().get().await;
        let _ = self.workspace.detect_root(&params.as_uri(), &options).await;
        Ok(())
    }

    async fn make_feature_request<P>(&self, uri: Uri, params: P) -> Result<FeatureRequest<P>> {
        let options = self.pull_configuration().await;
        let snapshot = self.workspace.get().await;
        let client_capabilities = self.client_capabilities();
        match snapshot.find(&uri) {
            Some(current) => Ok(FeatureRequest {
                params,
                view: DocumentView::analyze(snapshot, current, &options, &self.current_dir),
                distro: self.distro.clone(),
                client_capabilities,
                options,
                current_dir: Arc::clone(&self.current_dir),
            }),
            None => {
                let msg = format!("Unknown document: {}", uri);
                Err(msg)
            }
        }
    }

    async fn pull_configuration(&self) -> Options {
        let config_manager = self.config_manager();
        let has_changed = config_manager.pull().await;
        let options = config_manager.get().await;
        if has_changed {
            self.workspace.reparse(&options).await;
        }
        options
    }

    async fn update_build_diagnostics(&self) {
        let snapshot = self.workspace.get().await;
        let options = self.config_manager().get().await;

        for doc in snapshot.0.iter().filter(|doc| doc.uri.scheme() == "file") {
            if let DocumentContent::Latex(table) = &doc.content {
                if table.is_standalone {
                    match self
                        .diagnostics_manager
                        .build
                        .update(&snapshot, &doc.uri, &options, &self.current_dir)
                        .await
                    {
                        Ok(true) => self.action_manager.push(Action::PublishDiagnostics).await,
                        Ok(false) => (),
                        Err(why) => {
                            warn!("Unable to read log file ({}): {}", why, doc.uri.as_str())
                        }
                    }
                }
            }
        }
    }

    async fn load_distribution(&self) {
        info!("Detected TeX distribution: {}", self.distro.kind());
        if self.distro.kind() == DistributionKind::Unknown {
            let params = ShowMessageParams {
                message: "Your TeX distribution could not be detected. \
                          Please make sure that your distribution is in your PATH."
                    .into(),
                typ: MessageType::Error,
            };
            self.client.show_message(params).await;
        }

        if let Err(why) = self.distro.load().await {
            let message = match why {
                KpsewhichError::NotInstalled | KpsewhichError::InvalidOutput => {
                    "An error occurred while executing `kpsewhich`.\
                     Please make sure that your distribution is in your PATH \
                     environment variable and provides the `kpsewhich` tool."
                }
                KpsewhichError::CorruptDatabase | KpsewhichError::NoDatabase => {
                    "The file database of your TeX distribution seems \
                     to be corrupt. Please rebuild it and try again."
                }
                KpsewhichError::Decode(_) => {
                    "An error occurred while decoding the output of `kpsewhich`."
                }
                KpsewhichError::IO(why) => {
                    error!("An I/O error occurred while executing 'kpsewhich': {}", why);
                    "An I/O error occurred while executing 'kpsewhich'"
                }
            };
            let params = ShowMessageParams {
                message: message.into(),
                typ: MessageType::Error,
            };
            self.client.show_message(params).await;
        };
    }
}

#[async_trait]
impl<C: LspClient + Send + Sync + 'static> Middleware for LatexLspServer<C> {
    async fn before_message(&self) {
        if let Some(config_manager) = self.config_manager.get() {
            let options = config_manager.get().await;
            self.workspace.detect_children(&options).await;
            self.workspace.reparse_all_if_newer(&options).await;
        }
    }

    async fn after_message(&self) {
        self.update_build_diagnostics().await;
        for action in self.action_manager.take().await {
            match action {
                Action::LoadDistribution => {
                    self.load_distribution().await;
                }
                Action::RegisterCapabilities => {
                    let config_manager = self.config_manager();
                    config_manager.register().await;
                }
                Action::PullConfiguration => {
                    self.pull_configuration().await;
                }
                Action::DetectRoot(uri) => {
                    let options = self.config_manager().get().await;
                    let _ = self.workspace.detect_root(&uri, &options).await;
                }
                Action::PublishDiagnostics => {
                    let snapshot = self.workspace.get().await;
                    for doc in &snapshot.0 {
                        let diagnostics = self.diagnostics_manager.get(doc).await;
                        let params = PublishDiagnosticsParams {
                            uri: doc.uri.clone().into(),
                            diagnostics,
                            version: None,
                        };
                        self.client.publish_diagnostics(params).await;
                    }
                }
                Action::Build(uri) => {
                    let options = self
                        .config_manager()
                        .get()
                        .await
                        .latex
                        .and_then(|opts| opts.build)
                        .unwrap_or_default();

                    if options.on_save() {
                        let text_document = TextDocumentIdentifier::new(uri.into());
                        self.build(BuildParams { text_document }).await.unwrap();
                    }
                }
                Action::RunLinter(uri, reason) => {
                    let options = self
                        .config_manager()
                        .get()
                        .await
                        .latex
                        .and_then(|opts| opts.lint)
                        .unwrap_or_default();

                    let should_lint = match reason {
                        LintReason::Change => options.on_change(),
                        LintReason::Save => options.on_save() || options.on_change(),
                    };

                    if should_lint {
                        let snapshot = self.workspace.get().await;
                        if let Some(doc) = snapshot.find(&uri) {
                            if let DocumentContent::Latex(_) = &doc.content {
                                self.diagnostics_manager.latex.update(&uri, &doc.text).await;
                            }
                        }
                    }
                }
            }
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum LintReason {
    Change,
    Save,
}

#[derive(Debug, PartialEq, Clone)]
enum Action {
    LoadDistribution,
    RegisterCapabilities,
    PullConfiguration,
    DetectRoot(Uri),
    PublishDiagnostics,
    Build(Uri),
    RunLinter(Uri, LintReason),
}

#[derive(Debug, Default)]
struct ActionManager {
    actions: Mutex<Vec<Action>>,
}

impl ActionManager {
    pub async fn push(&self, action: Action) {
        let mut actions = self.actions.lock().await;
        actions.push(action);
    }

    pub async fn take(&self) -> Vec<Action> {
        let mut actions = self.actions.lock().await;
        mem::replace(&mut *actions, Vec::new())
    }
}