diff --git a/crates/texlab_completion/src/latex/include.rs b/crates/texlab_completion/src/latex/include.rs index fb57b3c8f..8560f6262 100644 --- a/crates/texlab_completion/src/latex/include.rs +++ b/crates/texlab_completion/src/latex/include.rs @@ -84,15 +84,12 @@ fn current_directory( .options .latex .as_ref() - .and_then(|latex| latex.root_directory.as_ref()) - .map_or_else( - || { - let mut path = request.document().uri.to_file_path().unwrap(); - path.pop(); - path - }, - Clone::clone, - ); + .and_then(|latex| latex.root_directory()) + .unwrap_or_else(|| { + let mut path = request.document().uri.to_file_path().unwrap(); + path.pop(); + path + }); path = PathBuf::from(path.to_string_lossy().into_owned().replace('\\', "/")); diff --git a/crates/texlab_protocol/src/options.rs b/crates/texlab_protocol/src/options.rs index ead07ff73..48279bcf4 100644 --- a/crates/texlab_protocol/src/options.rs +++ b/crates/texlab_protocol/src/options.rs @@ -1,4 +1,4 @@ -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Deserializer, Serialize}; use std::path::{Path, PathBuf}; #[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)] @@ -68,7 +68,24 @@ pub struct LatexOptions { pub forward_search: Option, pub lint: Option, pub build: Option, - pub root_directory: Option, + #[serde(default, deserialize_with = "deserialize_some")] + pub root_directory: Option>, +} + +fn deserialize_some<'de, T, D>(deserializer: D) -> Result, D::Error> +where + T: Deserialize<'de>, + D: Deserializer<'de>, +{ + Deserialize::deserialize(deserializer).map(Some) +} + +impl LatexOptions { + pub fn root_directory(&self) -> Option { + self.root_directory + .clone() + .unwrap_or_else(|| Some(PathBuf::from("."))) + } } #[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)] @@ -98,7 +115,7 @@ impl Options { .or_else(|| { self.latex .as_ref() - .and_then(|latex| latex.root_directory.as_ref()) + .and_then(|latex| latex.root_directory()) .map(|path| path.join(&name)) .and_then(|path| dunce::canonicalize(path).ok()) }) diff --git a/crates/texlab_syntax/src/lib.rs b/crates/texlab_syntax/src/lib.rs index 89f50a341..bfa6aed1a 100644 --- a/crates/texlab_syntax/src/lib.rs +++ b/crates/texlab_syntax/src/lib.rs @@ -14,7 +14,7 @@ use std::path::PathBuf; use texlab_distro::{Language, Resolver}; use texlab_protocol::{Options, Uri}; -#[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[derive(Debug, PartialEq, Clone, Copy)] pub struct SyntaxTreeInput<'a> { pub options: &'a Options, pub resolver: &'a Resolver, @@ -28,7 +28,7 @@ impl<'a> SyntaxTreeInput<'a> { self.options .latex .as_ref() - .and_then(|opts| opts.root_directory.as_ref()) + .and_then(|opts| opts.root_directory()) .and_then(|path| dunce::canonicalize(path).ok()) .or_else(|| { self.uri.to_file_path().ok().map(|mut path| { diff --git a/src/build.rs b/src/build.rs index 2f3608205..cabebb9fd 100644 --- a/src/build.rs +++ b/src/build.rs @@ -41,9 +41,8 @@ where .map(Clone::clone) .unwrap_or_default(); - let build_dir = self - .options - .root_directory + let root_directory = self.options.root_directory(); + let build_dir = root_directory .as_ref() .map(AsRef::as_ref) .or_else(|| path.parent())