Skip to content

Commit

Permalink
feat(config): add support for fallback formatters (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Sep 1, 2024
1 parent d3d0308 commit 092cc56
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

#### [Unreleased](https://github.com/hougesen/mdsf/compare/v0.2.2...HEAD)

- chore: update language ext map [`#445`](https://github.com/hougesen/mdsf/pull/445)
- build(deps): bump clap_complete from 4.5.12 to 4.5.24 [`#439`](https://github.com/hougesen/mdsf/pull/439)
- build(deps): bump serde_json from 1.0.122 to 1.0.127 in /codegen [`#443`](https://github.com/hougesen/mdsf/pull/443)
- build(deps): bump serde_json from 1.0.122 to 1.0.127 [`#438`](https://github.com/hougesen/mdsf/pull/438)
Expand Down
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,20 @@ mdsf init

```json
{
// Only run `ruff` on Python snippets,
"python": "ruff",
// Run `usort` on file and then `black`
"python": ["usort", "black"],
// Run `usort`, if that fails run `isort`, finally run `black`
"python": [["usort", "isort"], "black"]
"languages": {
// Only run `ruff` on Python snippets,
"python": "ruff",
// Run `usort` on file and then `black`
"python": ["usort", "black"],
// Run `usort`, if that fails run `isort`, finally run `black`
"python": [["usort", "isort"], "black"],

// Formatters listed under "*" will be run on any snippet.
"*": ["typos"],

// Formatters listed under "_" will only be run when there is not formatter configured for the file type OR globally ("*").
"_": "prettier"
}
}
```

Expand Down
34 changes: 29 additions & 5 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,17 @@ pub fn execute_command(

#[inline]
pub fn format_snippet(config: &MdsfConfig, info: &LineInfo, code: &str) -> String {
if let Some(formatters) = config.languages.get(info.language) {
let always_ran = config.languages.get("*");

let language_formatters = config.languages.get(info.language).or_else(|| {
if always_ran.is_none() {
config.languages.get("_")
} else {
None
}
});

if always_ran.is_some() || language_formatters.is_some() {
if let Ok(snippet) = setup_snippet(
code,
config
Expand All @@ -305,12 +315,26 @@ pub fn format_snippet(config: &MdsfConfig, info: &LineInfo, code: &str) -> Strin
) {
let snippet_path = snippet.path();

if let Ok(Some(formatted_code)) = formatters.format(snippet_path, info) {
let mut f = formatted_code.trim().to_owned();
if let Some(formatters) = always_ran {
if let Ok(Some(formatted_code)) = formatters.format(snippet_path, info) {
if language_formatters.is_none() {
let mut f = formatted_code.trim().to_owned();

f.push('\n');
f.push('\n');

return f;
return f;
}
}
}

if let Some(formatters) = language_formatters {
if let Ok(Some(formatted_code)) = formatters.format(snippet_path, info) {
let mut f = formatted_code.trim().to_owned();

f.push('\n');

return f;
}
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ fn format_file(config: &MdsfConfig, filename: &std::path::Path, input: &str) ->
if line.starts_with("```") {
let language = line.strip_prefix("```").map(str::trim).unwrap_or_default();

if config.languages.contains_key(language) {
// "*" is always ran
// "_" is fallback formatters
if config.languages.contains_key(language)
|| config.languages.contains_key("*")
|| config.languages.contains_key("_")
{
let is_go = language == "go" || language == "golang";

let (is_snippet, code_snippet, snippet_lines) = if is_go {
Expand Down

0 comments on commit 092cc56

Please sign in to comment.