summaryrefslogtreecommitdiff
path: root/support/texlab/src/features/forward_search.rs
blob: 350fdd02b900ce8ca62e7940eaba8c45015ac09c (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
use std::{
    io,
    path::{Path, PathBuf},
    process::Stdio,
};

use log::error;
use lsp_types::{Position, Url};
use thiserror::Error;

use crate::{db::Workspace, util::line_index_ext::LineIndexExt, Db};

#[derive(Debug, Error)]
pub enum Error {
    #[error("TeX document '{0}' not found")]
    TexNotFound(Url),

    #[error("TeX document '{0}' is invalid")]
    InvalidTexFile(Url),

    #[error("PDF document '{0}' not found")]
    PdfNotFound(PathBuf),

    #[error("TeX document '{0}' is not a local file")]
    NoLocalFile(Url),

    #[error("PDF viewer is not configured")]
    Unconfigured,

    #[error("Failed to spawn process: {0}")]
    Spawn(io::Error),
}

pub struct Command {
    program: String,
    args: Vec<String>,
}

impl Command {
    pub fn configure(db: &dyn Db, uri: &Url, position: Option<Position>) -> Result<Self, Error> {
        let workspace = Workspace::get(db);
        let child = workspace
            .lookup_uri(db, uri)
            .ok_or_else(|| Error::TexNotFound(uri.clone()))?;

        let parent = workspace
            .parents(db, child)
            .iter()
            .copied()
            .next()
            .unwrap_or(child);

        let output_dir = workspace
            .output_dir(db, workspace.working_dir(db, parent.directory(db)))
            .path(db)
            .as_deref()
            .ok_or_else(|| Error::NoLocalFile(uri.clone()))?;

        let tex_path = child
            .location(db)
            .path(db)
            .as_deref()
            .ok_or_else(|| Error::NoLocalFile(uri.clone()))?;

        let pdf_path = match parent.location(db).stem(db) {
            Some(stem) => {
                let pdf_name = format!("{}.pdf", stem);
                output_dir.join(pdf_name)
            }
            None => {
                return Err(Error::InvalidTexFile(uri.clone()));
            }
        };

        if !pdf_path.exists() {
            return Err(Error::PdfNotFound(pdf_path));
        }

        let position =
            position.unwrap_or_else(|| child.line_index(db).line_col_lsp(child.cursor(db)));

        let Some(config) = &db.config().synctex else {
            return Err(Error::Unconfigured);
        };

        let program = config.program.clone();

        let args: Vec<_> = config
            .args
            .iter()
            .flat_map(|arg| replace_placeholder(tex_path, &pdf_path, position.line, arg))
            .collect();

        Ok(Self { program, args })
    }
}

impl Command {
    pub fn run(self) -> Result<(), Error> {
        log::debug!("Executing forward search: {} {:?}", self.program, self.args);

        std::process::Command::new(self.program)
            .args(self.args)
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .map_err(Error::Spawn)?;

        Ok(())
    }
}

/// Iterate overs chunks of a string. Either returns a slice of the
/// original string, or the placeholder replacement.
struct PlaceHolderIterator<'a> {
    remainder: &'a str,
    tex_file: &'a str,
    pdf_file: &'a str,
    line_number: &'a str,
}

impl<'a> PlaceHolderIterator<'a> {
    pub fn new(s: &'a str, tex_file: &'a str, pdf_file: &'a str, line_number: &'a str) -> Self {
        Self {
            remainder: s,
            tex_file,
            pdf_file,
            line_number,
        }
    }

    pub fn yield_remainder(&mut self) -> Option<&'a str> {
        let chunk = self.remainder;
        self.remainder = "";
        Some(chunk)
    }

    pub fn yield_placeholder(&mut self) -> Option<&'a str> {
        if self.remainder.len() >= 2 {
            let placeholder = self.remainder;
            self.remainder = &self.remainder[2..];
            match &placeholder[1..2] {
                "f" => Some(self.tex_file),
                "p" => Some(self.pdf_file),
                "l" => Some(self.line_number),
                "%" => Some("%"), // escape %
                _ => Some(&placeholder[0..2]),
            }
        } else {
            self.remainder = &self.remainder[1..];
            Some("%")
        }
    }

    pub fn yield_str(&mut self, end: usize) -> Option<&'a str> {
        let chunk = &self.remainder[..end];
        self.remainder = &self.remainder[end..];
        Some(chunk)
    }
}

impl<'a> Iterator for PlaceHolderIterator<'a> {
    type Item = &'a str;

    fn next(&mut self) -> Option<Self::Item> {
        return if self.remainder.is_empty() {
            None
        } else if self.remainder.starts_with('%') {
            self.yield_placeholder()
        } else {
            // yield up to the next % or to the end
            match self.remainder.find('%') {
                None => self.yield_remainder(),
                Some(end) => self.yield_str(end),
            }
        };
    }
}

fn replace_placeholder(
    tex_file: &Path,
    pdf_file: &Path,
    line_number: u32,
    argument: &str,
) -> Option<String> {
    let result = if argument.starts_with('"') || argument.ends_with('"') {
        argument.to_string()
    } else {
        let line = &(line_number + 1).to_string();
        let it = PlaceHolderIterator::new(argument, tex_file.to_str()?, pdf_file.to_str()?, line);
        it.collect::<Vec<&str>>().join("")
    };
    Some(result)
}