diff --git a/autocorrect/src/code/mod.rs b/autocorrect/src/code/mod.rs index 806e9db7..fcfe71f0 100644 --- a/autocorrect/src/code/mod.rs +++ b/autocorrect/src/code/mod.rs @@ -30,6 +30,7 @@ mod strings; mod swift; mod xml; mod yaml; +mod zig; pub use code::*; pub use types::*; @@ -62,6 +63,9 @@ pub use strings::*; pub use swift::*; pub use xml::*; pub use yaml::*; +pub use zig::*; + +use self::zig::lint_zig; /// Lint a file content with filetype. /// @@ -111,6 +115,7 @@ pub fn lint_for(raw: &str, filename_or_ext: &str) -> LintResult { "c" => lint_c(raw), "xml" => lint_xml(raw), "jupyter" => lint_jupyter(raw), + "zig" => lint_zig(raw), "text" => lint_markdown(raw), _ => LintResult::new(raw), }; @@ -168,6 +173,7 @@ pub fn format_for(raw: &str, filename_or_ext: &str) -> FormatResult { "c" => format_c(raw), "xml" => format_xml(raw), "jupyter" => format_jupyter(raw), + "zig" => format_zig(raw), "text" => format_markdown(raw), _ => { let mut result = FormatResult::new(raw); diff --git a/autocorrect/src/code/zig.rs b/autocorrect/src/code/zig.rs new file mode 100644 index 00000000..a54bb337 --- /dev/null +++ b/autocorrect/src/code/zig.rs @@ -0,0 +1,43 @@ +// autocorrect: false +use super::*; +use autocorrect_derive::GrammarParser; +use pest::Parser as P; +use pest_derive::Parser; + +#[derive(GrammarParser, Parser)] +#[grammar = "../grammar/rust.pest"] +struct ZigParser; + +#[cfg(test)] +mod tests { + use super::*; + use indoc::indoc; + use pretty_assertions::assert_eq; + + #[test] + fn it_format_yaml() { + let example = indoc! {r###" + //! 这是top-level文档注释 + const std = @import("std"); + + /// 这是main函数 + pub fn main() !void { + // 这是comment + const text = "hello你好"; + } + "###}; + + let expect = indoc! {r###" + //! 这是 top-level 文档注释 + const std = @import("std"); + + /// 这是 main 函数 + pub fn main() !void { + // 这是 comment + const text = "hello 你好"; + } + "###}; + + assert_eq!(expect, format_for(example, "zig").to_string()) + } +}