summaryrefslogtreecommitdiff
path: root/support/texlab/src/features/formatting/latexindent.rs
blob: 300f5125788959c5b11afe421b5a708a452b5b0c (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
use std::{
    fs,
    process::{Command, Stdio},
};

use cancellation::CancellationToken;
use cstree::{TextLen, TextRange};
use lsp_types::{DocumentFormattingParams, TextEdit};
use tempfile::tempdir;

use crate::{features::FeatureRequest, DocumentLanguage, LineIndexExt};

pub fn format_with_latexindent(
    request: &FeatureRequest<DocumentFormattingParams>,
    _cancellation_token: &CancellationToken,
) -> Option<Vec<TextEdit>> {
    let directory = tempdir().ok()?;
    let document = request.main_document();

    let options = request.context.options.read().unwrap();
    let current_dir = options
        .root_directory
        .as_ref()
        .cloned()
        .or_else(|| {
            if document.uri.scheme() == "file" {
                document
                    .uri
                    .to_file_path()
                    .unwrap()
                    .parent()
                    .map(ToOwned::to_owned)
            } else {
                None
            }
        })
        .unwrap_or_else(|| ".".into());

    let local = match &options.latexindent.local {
        Some(local) => format!("--local={}", local),
        None => "-l".to_string(),
    };

    let modify_line_breaks = options.latexindent.modify_line_breaks;

    drop(options);

    let path = directory.path();
    let _ = fs::copy(
        current_dir.join("localSettings.yaml"),
        path.join("localSettings.yaml"),
    );
    let _ = fs::copy(
        current_dir.join(".localSettings.yaml"),
        path.join(".localSettings.yaml"),
    );
    let _ = fs::copy(
        current_dir.join("latexindent.yaml"),
        path.join("latexindent.yaml"),
    );

    let name = if document.language() == DocumentLanguage::Bibtex {
        "file.bib"
    } else {
        "file.tex"
    };

    fs::write(directory.path().join(name), &document.text).ok()?;

    let mut args = Vec::new();
    if modify_line_breaks {
        args.push("--modifylinebreaks");
    }
    args.push(&local);
    args.push(name);

    let output = Command::new("latexindent")
        .args(&args)
        .current_dir(current_dir)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .current_dir(directory.path())
        .output()
        .ok()?;

    let new_text = String::from_utf8_lossy(&output.stdout).into_owned();
    if new_text.is_empty() {
        None
    } else {
        Some(vec![TextEdit {
            range: document
                .line_index
                .line_col_lsp_range(TextRange::new(0.into(), document.text.text_len())),
            new_text,
        }])
    }
}