summaryrefslogtreecommitdiff
path: root/support/texlab/src/forward_search.rs
diff options
context:
space:
mode:
Diffstat (limited to 'support/texlab/src/forward_search.rs')
-rw-r--r--support/texlab/src/forward_search.rs81
1 files changed, 42 insertions, 39 deletions
diff --git a/support/texlab/src/forward_search.rs b/support/texlab/src/forward_search.rs
index 6dbe924051..a3911ed9f3 100644
--- a/support/texlab/src/forward_search.rs
+++ b/support/texlab/src/forward_search.rs
@@ -1,55 +1,58 @@
-use log::*;
-use serde::{Deserialize, Serialize};
-use serde_repr::*;
-use std::io;
-use std::path::Path;
-use std::process::Stdio;
-use tokio_net::process::Command;
-
-#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)]
-pub struct ForwardSearchOptions {
- pub executable: Option<String>,
- pub args: Option<Vec<String>>,
-}
-
-#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize_repr, Deserialize_repr)]
-#[repr(i32)]
-pub enum ForwardSearchStatus {
- Success = 0,
- Error = 1,
- Failure = 2,
- Unconfigured = 3,
-}
-
-#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
-pub struct ForwardSearchResult {
- pub status: ForwardSearchStatus,
-}
+use crate::{
+ protocol::{ForwardSearchResult, ForwardSearchStatus, Options, Uri},
+ workspace::Snapshot,
+};
+use log::error;
+use std::{io, path::Path, process::Stdio};
+use tokio::process::Command;
pub async fn search<'a>(
- tex_file: &'a Path,
- parent: &'a Path,
+ snapshot: &'a Snapshot,
+ tex_uri: &'a Uri,
line_number: u64,
- options: ForwardSearchOptions,
+ options: &Options,
+ current_dir: &'a Path,
) -> Option<ForwardSearchResult> {
- if options.executable.is_none() || options.args.is_none() {
+ let pdf_path = snapshot
+ .resolve_aux_targets(
+ &snapshot.parent_subfile(tex_uri, options, current_dir)?.uri,
+ options,
+ current_dir,
+ "pdf",
+ )?
+ .into_iter()
+ .filter(|uri| uri.scheme() == "file")
+ .filter_map(|uri| uri.to_file_path().ok())
+ .find(|path| path.exists())?;
+
+ if tex_uri.scheme() != "file" {
return Some(ForwardSearchResult {
- status: ForwardSearchStatus::Unconfigured,
+ status: ForwardSearchStatus::Failure,
});
}
- let pdf_file = parent
- .parent()?
- .join(format!("{}.pdf", parent.file_stem()?.to_str()?));
+ let search_options = options
+ .latex
+ .as_ref()
+ .and_then(|opts| opts.forward_search.as_ref())
+ .map(Clone::clone)
+ .unwrap_or_default();
+
+ if search_options.executable.is_none() || search_options.args.is_none() {
+ return Some(ForwardSearchResult {
+ status: ForwardSearchStatus::Unconfigured,
+ });
+ }
- let args: Vec<String> = options
+ let tex_path = tex_uri.to_file_path().ok()?;
+ let args: Vec<String> = search_options
.args
.unwrap()
.into_iter()
- .flat_map(|arg| replace_placeholder(&tex_file, &pdf_file, line_number, arg))
+ .flat_map(|arg| replace_placeholder(&tex_path, &pdf_path, line_number, arg))
.collect();
- let status = match spawn_process(options.executable.unwrap(), args).await {
+ let status = match spawn_process(search_options.executable.unwrap(), args).await {
Ok(()) => ForwardSearchStatus::Success,
Err(why) => {
error!("Unable to execute forward search: {}", why);
@@ -71,7 +74,7 @@ fn replace_placeholder(
argument
.replace("%f", tex_file.to_str()?)
.replace("%p", pdf_file.to_str()?)
- .replace("%l", &line_number.to_string())
+ .replace("%l", &(line_number + 1).to_string())
};
Some(result)
}