Repository: ErykMroczek/mofmt (language: python)

Issue:
No information on which file causes parser to panic
Before switching to Rust parser, formatter printed out path of the file that could not be formatted properly (due to e.g. incorrect syntax). Now it is not printed making it hard to find the problematic file when formatting directories.


Discussion / hints:

Fix patch:
diff --git a/Cargo.toml b/Cargo.toml
index ed779d8..bdcbc3d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,7 +12,7 @@ keywords = ["Modelica", "development", "formatter"]
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
-moparse = "0.1.5-rc1"
+moparse = "0.1.5-rc2"
 
 [profile.release]
 strip = true
diff --git a/src/main.rs b/src/main.rs
index 86e93ea..3829c17 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -59,25 +59,30 @@ fn format_files(args: &[String], check: bool) {
         .for_each(|mut v| files.append(&mut v));
     files.iter().for_each(|p| {
         let contents = read_file(p);
+        let name = p.display();
         match contents {
             Ok(source) => {
-                let parsed = parse(&source, SyntaxKind::StoredDefinition);
+                let parsed = parse(
+                    name.to_string().as_str(),
+                    &source,
+                    SyntaxKind::StoredDefinition,
+                );
                 if !parsed.errors.is_empty() {
-                    let messages: Vec<String> = parsed
-                        .errors
-                        .iter()
-                        .map(|e| format!("{}:{}", p.display(), e))
-                        .collect();
-                    writeln!(lock, "Syntax errors detected:\n{}", messages.join("\n")).unwrap();
+                    writeln!(
+                        lock,
+                        "Syntax errors detected:\n{}",
+                        parsed.errors.join("\n")
+                    )
+                    .unwrap();
                     code = 1;
                 } else {
                     let output = pretty_print(parsed.tokens, parsed.comments, parsed.events);
                     if check {
                         if output != source {
                             code = 1;
-                            writeln!(lock, "{}: check failed", p.display()).unwrap();
+                            writeln!(lock, "{}: check failed", name).unwrap();
                         } else {
-                            writeln!(lock, "{}: check passed", p.display()).unwrap();
+                            writeln!(lock, "{}: check passed", name).unwrap();
                         }
                     } else {
                         write_file(p, output);
@@ -85,7 +90,7 @@ fn format_files(args: &[String], check: bool) {
                 }
             }
             Err(e) => {
-                eprintln!("{}: error: {}", p.display(), e);
+                eprintln!("{}: error: {}", name, e);
                 code = 1;
             }
         }
diff --git a/src/tree.rs b/src/tree.rs
index 00553d5..cb38241 100644
--- a/src/tree.rs
+++ b/src/tree.rs
@@ -102,7 +102,7 @@ mod tests {
     use super::*;
 
     fn tree(source: &str, start: SyntaxKind) -> Tree {
-        let parsed = parse(source, start);
+        let parsed = parse("none", source, start);
         build_tree(parsed.tokens, parsed.events)
     }
 


Test patch:
diff --git a/tests/style_tests.rs b/tests/style_tests.rs
index 73c4dc2..473760b 100644
--- a/tests/style_tests.rs
+++ b/tests/style_tests.rs
@@ -5,7 +5,7 @@ use std::fs;
 // Helper functions
 fn format_file(path: &str) -> String {
     let input = fs::read_to_string(path).expect("error");
-    let parsed = moparse::parse(&input, moparse::SyntaxKind::StoredDefinition);
+    let parsed = moparse::parse(path, &input, moparse::SyntaxKind::StoredDefinition);
     mofmt::pretty_print(parsed.tokens, parsed.comments, parsed.events)
 }