Skip to content

Commit

Permalink
feat: add support for nimpretty (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Mar 5, 2024
1 parent a703b9e commit fc64c11
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ use std::{io::Write, process::Command};

use tempfile::NamedTempFile;

use self::{ruff::format_using_ruff, rustfmt::format_using_rustfmt, stylua::format_using_stylua};
use self::{
nimpretty::format_using_nimpretty, ruff::format_using_ruff, rustfmt::format_using_rustfmt,
stylua::format_using_stylua,
};

mod nimpretty;
mod ruff;
mod rustfmt;
mod stylua;
Expand All @@ -30,7 +34,7 @@ pub fn format_snippet(language: &str, code: String) -> String {
"rust" => format_using_rustfmt(&code),
"lua" => format_using_stylua(&code),
"python" => format_using_ruff(&code),

"nim" => format_using_nimpretty(&code),
_ => Ok(None),
} {
return formatted_code;
Expand Down
16 changes: 16 additions & 0 deletions src/formatters/nimpretty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use super::{execute_command, read_snippet, setup_snippet};

pub fn format_using_nimpretty(code: &str) -> std::io::Result<Option<String>> {
let file = setup_snippet(code)?;
let file_path = file.path();

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

cmd.arg(file_path);

if execute_command(&mut cmd)? {
return read_snippet(file_path).map(Some);
}

Ok(None)
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod config;
mod formatters;

fn format_file(path: &std::path::Path) -> std::io::Result<()> {
println!("Formatting {:#?}", path);
println!("Formatting {path:#?}");

let input = std::fs::read_to_string(path)?;

Expand Down
8 changes: 8 additions & 0 deletions test/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ def add (



```

```nim
proc add( a :int , b:int ) : int =
return a + b
```

0 comments on commit fc64c11

Please sign in to comment.