diff options
Diffstat (limited to 'support/texlab/src/features/formatting/latexindent.rs')
-rw-r--r-- | support/texlab/src/features/formatting/latexindent.rs | 96 |
1 files changed, 72 insertions, 24 deletions
diff --git a/support/texlab/src/features/formatting/latexindent.rs b/support/texlab/src/features/formatting/latexindent.rs index f603728a5d..300f512578 100644 --- a/support/texlab/src/features/formatting/latexindent.rs +++ b/support/texlab/src/features/formatting/latexindent.rs @@ -1,50 +1,98 @@ use std::{ - io::{BufWriter, Write}, + fs, process::{Command, Stdio}, }; use cancellation::CancellationToken; use cstree::{TextLen, TextRange}; use lsp_types::{DocumentFormattingParams, TextEdit}; +use tempfile::tempdir; -use crate::{features::FeatureRequest, LineIndexExt}; +use crate::{features::FeatureRequest, DocumentLanguage, LineIndexExt}; pub fn format_with_latexindent( request: &FeatureRequest<DocumentFormattingParams>, _cancellation_token: &CancellationToken, ) -> Option<Vec<TextEdit>> { + let directory = tempdir().ok()?; let document = request.main_document(); - let current_dir = &request.context.current_directory; let options = request.context.options.read().unwrap(); - let current_dir = match &options.root_directory { - Some(root_directory) => current_dir.join(root_directory), - None => current_dir.clone(), + let current_dir = options + .root_directory + .as_ref() + .cloned() + .or_else(|| { + if document.uri.scheme() == "file" { + document + .uri + .to_file_path() + .unwrap() + .parent() + .map(ToOwned::to_owned) + } else { + None + } + }) + .unwrap_or_else(|| ".".into()); + + let local = match &options.latexindent.local { + Some(local) => format!("--local={}", local), + None => "-l".to_string(), }; + + let modify_line_breaks = options.latexindent.modify_line_breaks; + drop(options); - let mut process = Command::new("latexindent") - .arg("-l") + let path = directory.path(); + let _ = fs::copy( + current_dir.join("localSettings.yaml"), + path.join("localSettings.yaml"), + ); + let _ = fs::copy( + current_dir.join(".localSettings.yaml"), + path.join(".localSettings.yaml"), + ); + let _ = fs::copy( + current_dir.join("latexindent.yaml"), + path.join("latexindent.yaml"), + ); + + let name = if document.language() == DocumentLanguage::Bibtex { + "file.bib" + } else { + "file.tex" + }; + + fs::write(directory.path().join(name), &document.text).ok()?; + + let mut args = Vec::new(); + if modify_line_breaks { + args.push("--modifylinebreaks"); + } + args.push(&local); + args.push(name); + + let output = Command::new("latexindent") + .args(&args) .current_dir(current_dir) - .stdin(Stdio::piped()) + .stdin(Stdio::null()) .stdout(Stdio::piped()) .stderr(Stdio::null()) - .spawn() + .current_dir(directory.path()) + .output() .ok()?; - let stdin = process.stdin.take()?; - let mut stdin = BufWriter::new(stdin); - stdin.write_all(document.text.as_bytes()).ok()?; - drop(stdin); - - let output = process.wait_with_output().ok()?; - let new_text = String::from_utf8_lossy(&output.stdout).into_owned(); - - Some(vec![TextEdit { - range: document - .line_index - .line_col_lsp_range(TextRange::new(0.into(), document.text.text_len())), - new_text, - }]) + if new_text.is_empty() { + None + } else { + Some(vec![TextEdit { + range: document + .line_index + .line_col_lsp_range(TextRange::new(0.into(), document.text.text_len())), + new_text, + }]) + } } |