Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(xml): add support for xmlformat #169

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,8 @@ jobs:
- name: csharpier
run: dotnet tool install -g csharpier

- name: xmlformatter/xmlformat
run: pip install xmlformatter

- name: run tests
run: cargo test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ mdsf init
| Toml | `taplo` |
| TypeScript | `biome`, `deno_fmt`, `prettier` |
| Vue | `prettier` |
| Xml | `xmllint` |
| Xml | `xmlformat`, `xmllint` |
| Yaml | `prettier`, `yamlfmt` |
| Zig | `zigfmt` |

Expand Down
2 changes: 1 addition & 1 deletion mdsf.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
},
"xml": {
"enabled": true,
"formatter": "xmllint"
"formatter": [["xmllint", "xmlformat"]]
},
"yaml": {
"enabled": true,
Expand Down
4 changes: 2 additions & 2 deletions schemas/v0.0.3/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@
"xml": {
"default": {
"enabled": true,
"formatter": "xmllint"
"formatter": [["xmllint", "xmlformat"]]
},
"allOf": [
{
Expand Down Expand Up @@ -1896,7 +1896,7 @@
},
"Xml": {
"type": "string",
"enum": ["xmllint"]
"enum": ["xmllint", "xmlformat"]
},
"Yaml": {
"type": "string",
Expand Down
16 changes: 3 additions & 13 deletions src/formatters/elm_format.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use crate::{runners::setup_npm_script, terminal::print_formatter_info};

use super::execute_command;
use crate::{runners::setup_npm_script, terminal::print_formatter_info};

#[inline]
fn set_elm_format_args(cmd: &mut std::process::Command, snippet_path: &std::path::Path) {
print_formatter_info("elm_format");

cmd.arg("--elm-version=0.19").arg("--yes").arg(snippet_path);
}

Expand All @@ -23,16 +20,9 @@ fn invoke_elm_format(
pub fn format_using_elm_format(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
let path_result = invoke_elm_format(setup_npm_script("elm-format"), snippet_path)?;

if !path_result.0 {
return Ok(path_result);
}
print_formatter_info("elm_format");

invoke_elm_format(
setup_npm_script("@johnnymorganz/elm_format-bin"),
snippet_path,
)
invoke_elm_format(setup_npm_script("elm-format"), snippet_path)
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub mod stylua;
pub mod swiftformat;
pub mod taplo;
pub mod usort;
pub mod xmlformat;
pub mod xmllint;
pub mod yamlfmt;
pub mod yapf;
Expand Down
50 changes: 50 additions & 0 deletions src/formatters/xmlformat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use super::execute_command;
use crate::terminal::print_formatter_info;

#[inline]
pub fn format_using_xmlformat(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_formatter_info("xmlformat");

let mut cmd = std::process::Command::new("xmlformat");

cmd.arg("--overwrite").arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}

#[cfg(test)]
mod test_xmlformat {
use super::format_using_xmlformat;
use crate::{formatters::setup_snippet, languages::Language};

#[test_with::executable(xmlformat)]
#[test]
fn it_should_format_xml() {
let input = "
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>";

let expected_output = "<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>";

let snippet =
setup_snippet(input, Language::Xml.to_file_ext()).expect("it to create a snippet file");

let output = format_using_xmlformat(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(output, expected_output);
}
}
12 changes: 5 additions & 7 deletions src/formatters/xmllint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::terminal::print_formatter_info;

use super::execute_command;
use crate::terminal::print_formatter_info;

#[inline]
pub fn format_using_xmllint(
Expand All @@ -19,10 +18,9 @@ pub fn format_using_xmllint(
}

#[cfg(test)]
mod test_isort {
use crate::{formatters::setup_snippet, languages::Language};

mod test_xmllint {
use super::format_using_xmllint;
use crate::{formatters::setup_snippet, languages::Language};

#[test_with::executable(xmllint)]
#[test]
Expand All @@ -44,8 +42,8 @@ mod test_isort {
</note>
"#;

let snippet = setup_snippet(input, Language::Python.to_file_ext())
.expect("it to create a snippet file");
let snippet =
setup_snippet(input, Language::Xml.to_file_ext()).expect("it to create a snippet file");

let output = format_using_xmllint(snippet.path())
.expect("it to be successful")
Expand Down
50 changes: 41 additions & 9 deletions src/languages/xml.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use schemars::JsonSchema;

use crate::formatters::{xmllint::format_using_xmllint, MdsfFormatter};

use super::{Lang, LanguageFormatter};
use crate::formatters::{
xmlformat::format_using_xmlformat, xmllint::format_using_xmllint, MdsfFormatter,
};

#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum Xml {
#[default]
#[serde(rename = "xmllint")]
Xmllint,
XmlLint,
#[serde(rename = "xmlformat")]
XmlFormat,
}

impl Default for Lang<Xml> {
Expand All @@ -25,7 +28,10 @@ impl Default for Lang<Xml> {
impl Default for MdsfFormatter<Xml> {
#[inline]
fn default() -> Self {
Self::Single(Xml::Xmllint)
Self::Multiple(vec![Self::Multiple(vec![
Self::Single(Xml::XmlLint),
Self::Single(Xml::XmlFormat),
])])
}
}

Expand All @@ -36,20 +42,20 @@ impl LanguageFormatter for Xml {
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
match self {
Self::Xmllint => format_using_xmllint(snippet_path),
Self::XmlLint => format_using_xmllint(snippet_path),
Self::XmlFormat => format_using_xmlformat(snippet_path),
}
}
}

#[cfg(test)]
mod test_xml {
use super::Xml;
use crate::{
formatters::{setup_snippet, MdsfFormatter},
languages::Lang,
};

use super::Xml;

const INPUT: &str = "
<note>
<to>Tove</to>
Expand All @@ -69,7 +75,7 @@ mod test_xml {
fn it_should_not_format_when_enabled_is_false() {
let l = Lang::<Xml> {
enabled: false,
formatter: MdsfFormatter::Single(Xml::Xmllint),
formatter: MdsfFormatter::Single(Xml::XmlLint),
};

let snippet = setup_snippet(INPUT, EXTENSION).expect("it to save the file");
Expand All @@ -83,7 +89,7 @@ mod test_xml {
fn test_xmllint() {
let l = Lang::<Xml> {
enabled: true,
formatter: MdsfFormatter::Single(Xml::Xmllint),
formatter: MdsfFormatter::Single(Xml::XmlLint),
};

let snippet = setup_snippet(INPUT, EXTENSION).expect("it to save the file");
Expand All @@ -105,4 +111,30 @@ mod test_xml {

assert_eq!(output, expected_output);
}

#[test_with::executable(xmlformat)]
#[test]
fn test_xmlformat() {
let l = Lang::<Xml> {
enabled: true,
formatter: MdsfFormatter::Single(Xml::XmlFormat),
};

let snippet = setup_snippet(INPUT, EXTENSION).expect("it to save the file");
let snippet_path = snippet.path();

let output = l
.format(snippet_path)
.expect("it to not fail")
.expect("it to be a snippet");

let expected_output = "<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>";

assert_eq!(output, expected_output);
}
}
Loading