From 67ed52d9765da1f205afe57f24734323b687ffbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20F=C3=B6rster?= Date: Wed, 26 Feb 2020 16:24:44 +0100 Subject: [PATCH] Pass configuration to LaTeX parser --- crates/texlab_syntax/src/lib.rs | 3 ++- crates/texlab_workspace/src/document.rs | 9 ++++++++- crates/texlab_workspace/src/workspace.rs | 3 ++- src/server.rs | 22 +++++++++++++++------- src/workspace_manager.rs | 23 +++++++++++++++-------- 5 files changed, 42 insertions(+), 18 deletions(-) diff --git a/crates/texlab_syntax/src/lib.rs b/crates/texlab_syntax/src/lib.rs index 71def4a41..d4dea1d6e 100644 --- a/crates/texlab_syntax/src/lib.rs +++ b/crates/texlab_syntax/src/lib.rs @@ -11,7 +11,7 @@ pub use self::lsp_kind::*; pub use self::text::*; use texlab_distro::{Language, Resolver}; -use texlab_protocol::Uri; +use texlab_protocol::{Options, Uri}; #[derive(Debug, PartialEq, Eq, Clone)] pub enum SyntaxTree { @@ -21,6 +21,7 @@ pub enum SyntaxTree { #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub struct SyntaxTreeInput<'a> { + pub options: &'a Options, pub resolver: &'a Resolver, pub uri: &'a Uri, pub text: &'a str, diff --git a/crates/texlab_workspace/src/document.rs b/crates/texlab_workspace/src/document.rs index a342da33d..b5cfa1134 100644 --- a/crates/texlab_workspace/src/document.rs +++ b/crates/texlab_workspace/src/document.rs @@ -12,8 +12,15 @@ pub struct Document { } impl Document { - pub fn parse(uri: Uri, text: String, language: Language, resolver: &Resolver) -> Self { + pub fn parse( + uri: Uri, + text: String, + language: Language, + options: &Options, + resolver: &Resolver, + ) -> Self { let input = SyntaxTreeInput { + options, resolver, uri: &uri, text: &text, diff --git a/crates/texlab_workspace/src/workspace.rs b/crates/texlab_workspace/src/workspace.rs index 96c02b417..db73ec691 100644 --- a/crates/texlab_workspace/src/workspace.rs +++ b/crates/texlab_workspace/src/workspace.rs @@ -158,10 +158,11 @@ impl TestWorkspaceBuilder { pub fn add_document(&mut self, name: &str, text: &str) -> Uri { let resolver = Resolver::default(); + let options = Options::default(); let path = env::temp_dir().join(name); let language = Language::by_extension(path.extension().unwrap().to_str().unwrap()).unwrap(); let uri = Uri::from_file_path(path).unwrap(); - let document = Document::parse(uri.clone(), text.to_owned(), language, &resolver); + let document = Document::parse(uri.clone(), text.to_owned(), language, &options, &resolver); self.workspace.documents.push(Arc::new(document)); uri } diff --git a/src/server.rs b/src/server.rs index 4c77eefaf..4be97e132 100644 --- a/src/server.rs +++ b/src/server.rs @@ -171,7 +171,8 @@ impl LatexLspServer { #[jsonrpc_method("textDocument/didOpen", kind = "notification")] pub async fn did_open(&self, params: DidOpenTextDocumentParams) { let uri = params.text_document.uri.clone(); - self.workspace_manager.add(params.text_document); + let options = self.configuration(false).await; + self.workspace_manager.add(params.text_document, &options); self.action_manager .push(Action::DetectRoot(uri.clone().into())); self.action_manager @@ -181,9 +182,11 @@ impl LatexLspServer { #[jsonrpc_method("textDocument/didChange", kind = "notification")] pub async fn did_change(&self, params: DidChangeTextDocumentParams) { + let options = self.configuration(false).await; for change in params.content_changes { let uri = params.text_document.uri.clone(); - self.workspace_manager.update(uri.into(), change.text); + self.workspace_manager + .update(uri.into(), change.text, &options); } self.action_manager.push(Action::RunLinter( params.text_document.uri.into(), @@ -496,7 +499,7 @@ impl LatexLspServer { let workspace = self.workspace_manager.get(); for path in workspace.unresolved_includes(&options) { if path.exists() { - changed |= self.workspace_manager.load(&path).is_ok(); + changed |= self.workspace_manager.load(&path, &options).is_ok(); } } @@ -506,7 +509,11 @@ impl LatexLspServer { } } - fn update_document(&self, document: &Document) -> std::result::Result<(), WorkspaceLoadError> { + fn update_document( + &self, + document: &Document, + options: &Options, + ) -> std::result::Result<(), WorkspaceLoadError> { if document.uri.scheme() != "file" { return Ok(()); } @@ -514,7 +521,7 @@ impl LatexLspServer { let path = document.uri.to_file_path().unwrap(); let data = fs::metadata(&path).map_err(WorkspaceLoadError::IO)?; if data.modified().map_err(WorkspaceLoadError::IO)? > document.modified { - self.workspace_manager.load(&path) + self.workspace_manager.load(&path, &options) } else { Ok(()) } @@ -573,7 +580,7 @@ impl LatexLspServer { { if let Ok(parent_uri) = Uri::from_file_path(entry.path()) { if workspace.find(&parent_uri).is_none() { - let _ = self.workspace_manager.load(entry.path()); + let _ = self.workspace_manager.load(entry.path(), &options); } } } @@ -587,9 +594,10 @@ impl Middleware for LatexLspServer { async fn before_message(&self) { self.detect_children().await; + let options = self.configuration(false).await; let workspace = self.workspace_manager.get(); for document in &workspace.documents { - let _ = self.update_document(document); + let _ = self.update_document(document, &options); } } diff --git a/src/workspace_manager.rs b/src/workspace_manager.rs index bd4c4814a..50cc1f529 100644 --- a/src/workspace_manager.rs +++ b/src/workspace_manager.rs @@ -6,7 +6,7 @@ use std::path::Path; use std::sync::Arc; use std::sync::Mutex; use texlab_distro::{Distribution, Language}; -use texlab_protocol::{TextDocumentItem, Uri}; +use texlab_protocol::{Options, TextDocumentItem, Uri}; use texlab_syntax::SyntaxTree; use texlab_workspace::{Document, Workspace}; @@ -35,7 +35,7 @@ impl WorkspaceManager { Arc::clone(&workspace) } - pub fn add(&self, document: TextDocumentItem) { + pub fn add(&self, document: TextDocumentItem, options: &Options) { let language = match Language::by_language_id(&document.language_id) { Some(language) => language, None => { @@ -45,10 +45,16 @@ impl WorkspaceManager { }; let mut workspace = self.workspace.lock().unwrap(); - *workspace = self.add_or_update(&workspace, document.uri.into(), document.text, language); + *workspace = self.add_or_update( + &workspace, + document.uri.into(), + document.text, + language, + options, + ); } - pub fn load(&self, path: &Path) -> Result<(), WorkspaceLoadError> { + pub fn load(&self, path: &Path, options: &Options) -> Result<(), WorkspaceLoadError> { let language = match path .extension() .and_then(OsStr::to_str) @@ -78,11 +84,11 @@ impl WorkspaceManager { }; let mut workspace = self.workspace.lock().unwrap(); - *workspace = self.add_or_update(&workspace, uri, text, language); + *workspace = self.add_or_update(&workspace, uri, text, language, options); Ok(()) } - pub fn update(&self, uri: Uri, text: String) { + pub fn update(&self, uri: Uri, text: String, options: &Options) { let mut workspace = self.workspace.lock().unwrap(); let old_document = match workspace.documents.iter().find(|x| x.uri == uri) { @@ -98,7 +104,7 @@ impl WorkspaceManager { SyntaxTree::Bibtex(_) => Language::Bibtex, }; - *workspace = self.add_or_update(&workspace, uri, text, language); + *workspace = self.add_or_update(&workspace, uri, text, language, options); } fn add_or_update( @@ -107,9 +113,10 @@ impl WorkspaceManager { uri: Uri, text: String, language: Language, + options: &Options, ) -> Arc { let resolver = block_on(self.distribution.resolver()); - let document = Document::parse(uri, text, language, &resolver); + let document = Document::parse(uri, text, language, &options, &resolver); let mut documents: Vec> = workspace .documents .iter()