Skip to content

Commit

Permalink
Add tex-fmt as formatter option (#1321)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster authored Jan 21, 2025
1 parent b01b9b8 commit 1b8ca8e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `texlab.symbols.customEnvironments` setting for specifying additional environments that will be included in the document symbols
([#1292](https://github.com/latex-lsp/texlab/issues/1292))
- Add `texlab.experimental.labelReferenceRangeCommands` setting ([#1210](https://github.com/latex-lsp/texlab/issues/1210))
- Add `tex-fmt` as a formatter for `latex` and `bibtex` ([#1320](https://github.com/latex-lsp/texlab/issues/1320))

### Fixed

Expand Down
1 change: 1 addition & 0 deletions crates/base-db/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub enum Formatter {
Null,
Server,
LatexIndent,
TexFmt,
}

#[derive(Debug, Default)]
Expand Down
8 changes: 7 additions & 1 deletion crates/texlab/src/features/formatting.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
mod bibtex_internal;
mod latexindent;
mod texfmt;

use base_db::{Formatter, Workspace};
use distro::Language;

use self::{bibtex_internal::format_bibtex_internal, latexindent::format_with_latexindent};
use self::{
bibtex_internal::format_bibtex_internal, latexindent::format_with_latexindent,
texfmt::format_with_texfmt,
};

pub fn format_source_code(
workspace: &Workspace,
Expand All @@ -17,11 +21,13 @@ pub fn format_source_code(
Formatter::Null => None,
Formatter::Server => None,
Formatter::LatexIndent => format_with_latexindent(workspace, document),
Formatter::TexFmt => format_with_texfmt(document),
},
Language::Bib => match workspace.config().formatting.bib_formatter {
Formatter::Null => None,
Formatter::Server => format_bibtex_internal(workspace, document, options),
Formatter::LatexIndent => format_with_latexindent(workspace, document),
Formatter::TexFmt => format_with_texfmt(document),
},
Language::Aux
| Language::Log
Expand Down
34 changes: 34 additions & 0 deletions crates/texlab/src/features/formatting/texfmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::{
io::Write,
process::{Command, Stdio},
};

use base_db::Document;
use rowan::{TextLen, TextRange};

use crate::util::line_index_ext::LineIndexExt;

pub fn format_with_texfmt(document: &Document) -> Option<Vec<lsp_types::TextEdit>> {
let mut child = Command::new("tex-fmt")
.arg("--stdin")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()
.ok()?;

let mut stdin = child.stdin.take()?;
std::thread::scope(|s| {
s.spawn(move || {
let _ = stdin.write_all(document.text.clone().as_bytes());
});

let output = child.wait_with_output().ok()?;
let new_text = String::from_utf8(output.stdout).ok()?;
let range = document
.line_index
.line_col_lsp_range(TextRange::new(0.into(), document.text.text_len()))?;

Some(vec![lsp_types::TextEdit { range, new_text }])
})
}
2 changes: 2 additions & 0 deletions crates/texlab/src/server/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub enum BibtexFormatter {
None,
Texlab,
Latexindent,
TexFmt,
}

impl Default for BibtexFormatter {
Expand All @@ -42,6 +43,7 @@ pub enum LatexFormatter {
None,
Texlab,
Latexindent,
TexFmt,
}

impl Default for LatexFormatter {
Expand Down
2 changes: 2 additions & 0 deletions crates/texlab/src/util/from_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,14 @@ pub fn config(value: Options) -> Config {
LatexFormatter::None => Formatter::Null,
LatexFormatter::Texlab => Formatter::Server,
LatexFormatter::Latexindent => Formatter::LatexIndent,
LatexFormatter::TexFmt => Formatter::TexFmt,
};

config.formatting.bib_formatter = match value.bibtex_formatter {
BibtexFormatter::None => Formatter::Null,
BibtexFormatter::Texlab => Formatter::Server,
BibtexFormatter::Latexindent => Formatter::LatexIndent,
BibtexFormatter::TexFmt => Formatter::TexFmt,
};

config.formatting.line_length =
Expand Down

0 comments on commit 1b8ca8e

Please sign in to comment.