diff --git a/src/forward_search.rs b/src/forward_search.rs index 970dbdb58..a3911ed9f 100644 --- a/src/forward_search.rs +++ b/src/forward_search.rs @@ -74,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) } diff --git a/src/protocol/options.rs b/src/protocol/options.rs index 8c4e38366..86718d1e7 100644 --- a/src/protocol/options.rs +++ b/src/protocol/options.rs @@ -51,6 +51,7 @@ pub struct LatexBuildOptions { pub args: Option>, pub on_save: Option, pub output_directory: Option, + pub forward_search_after: Option, } impl LatexBuildOptions { @@ -74,6 +75,10 @@ impl LatexBuildOptions { pub fn on_save(&self) -> bool { self.on_save.unwrap_or(false) } + + pub fn forward_search_after(&self) -> bool { + self.forward_search_after.unwrap_or(false) + } } #[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)] diff --git a/src/server.rs b/src/server.rs index c77d0acb4..23d0b208c 100644 --- a/src/server.rs +++ b/src/server.rs @@ -21,6 +21,7 @@ use crate::{ 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}; @@ -48,6 +49,7 @@ pub struct LatexLspServer { symbol_provider: SymbolProvider, hover_provider: HoverProvider, diagnostics_manager: DiagnosticsManager, + last_position_by_uri: CHashMap, } #[jsonrpc_server] @@ -74,6 +76,7 @@ impl LatexLspServer { symbol_provider: SymbolProvider::new(), hover_provider: HoverProvider::new(), diagnostics_manager: DiagnosticsManager::default(), + last_position_by_uri: CHashMap::new(), } } @@ -242,6 +245,12 @@ impl LatexLspServer { 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, @@ -276,6 +285,10 @@ impl LatexLspServer { 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) } @@ -464,7 +477,27 @@ impl LatexLspServer { let req = self .make_feature_request(params.text_document.as_uri(), params) .await?; - Ok(self.build_provider.execute(&req).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() + { + let params = TextDocumentPositionParams::new(req.params.text_document, pos); + self.forward_search(params).await?; + } + + Ok(res) } #[jsonrpc_method("textDocument/forwardSearch", kind = "request")]