summaryrefslogtreecommitdiff
path: root/support/texlab/src/features/execute_command.rs
blob: b1b89d4f504f4c861ded5fab54cfb73308efb842 (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 std::{path::PathBuf, process::Stdio, sync::Arc};

use anyhow::Result;
use lsp_types::{TextDocumentIdentifier, Url};

use crate::Workspace;

pub fn execute_command(
    workspace: &Workspace,
    name: &str,
    args: Vec<serde_json::Value>,
) -> Result<()> {
    match name {
        "texlab.cleanAuxiliary" => {
            let params = args
                .into_iter()
                .next()
                .ok_or_else(|| anyhow::anyhow!("texlab.cleanAuxiliary requires one argument"))?;

            clean_output_files(workspace, CleanOptions::Auxiliary, params)?;
        }
        "texlab.cleanArtifacts" => {
            let params = args
                .into_iter()
                .next()
                .ok_or_else(|| anyhow::anyhow!("texlab.cleanArtifacts requires one argument"))?;

            clean_output_files(workspace, CleanOptions::Artifacts, params)?;
        }
        _ => anyhow::bail!("Unknown command: {}", name),
    }

    Ok(())
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
enum CleanOptions {
    Auxiliary,
    Artifacts,
}

fn clean_output_files(
    workspace: &Workspace,
    options: CleanOptions,
    params: serde_json::Value,
) -> Result<()> {
    let params: TextDocumentIdentifier = serde_json::from_value(params)?;

    let uri = workspace
        .find_parent(&params.uri)
        .map(|document| document.uri)
        .unwrap_or_else(|| Arc::new(params.uri));

    if let Some(cx) = BuildContext::find(workspace, &uri) {
        let flag = match options {
            CleanOptions::Auxiliary => "-c",
            CleanOptions::Artifacts => "-C",
        };

        std::process::Command::new("latexmk")
            .arg(format!("-outdir={}", cx.output_dir.to_string_lossy()))
            .arg(flag)
            .arg(cx.input_file)
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()?;
    }

    Ok(())
}

struct BuildContext {
    input_file: PathBuf,
    output_dir: PathBuf,
}

impl BuildContext {
    pub fn find(workspace: &Workspace, uri: &Url) -> Option<Self> {
        if uri.scheme() != "file" {
            return None;
        }

        let input_file = uri.to_file_path().ok()?;
        let options = &workspace.environment.options;
        let current_dir = &workspace.environment.current_directory;
        let output_dir = match (
            options.root_directory.as_ref(),
            options.aux_directory.as_ref(),
        ) {
            (_, Some(aux_dir)) => current_dir.join(aux_dir),
            (Some(root_dir), None) => current_dir.join(root_dir),
            (None, None) => input_file.parent()?.to_path_buf(),
        };

        log::info!("Output = {:#?}", output_dir);

        Some(Self {
            input_file,
            output_dir,
        })
    }
}