Skip to content

Commit

Permalink
feat(config): custom file extensions (#325)
Browse files Browse the repository at this point in the history
Closes #313
  • Loading branch information
hougesen authored Jun 20, 2024
1 parent c979528 commit d1d55c3
Show file tree
Hide file tree
Showing 79 changed files with 173 additions and 143 deletions.
11 changes: 2 additions & 9 deletions codegen/src/language_to_filetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,11 @@ fn build_mapping(languages: std::collections::HashMap<String, LinguishLanguage>)
"// THIS CODE WAS GENERATED AND SHOULD NOT BE EDITED MANUALLY
#[allow(clippy::too_many_lines)]
pub fn language_to_ext(language: &str) -> String {{
pub fn language_to_ext(language: &str) -> &'static str {{
{WHITESPACE}#[allow(clippy::match_same_arms)]
{WHITESPACE}let ft = match language.to_lowercase().as_str() {{
{WHITESPACE}match language.to_lowercase().as_str() {{
{}
{WHITESPACE}}}
{WHITESPACE}.to_string();
{WHITESPACE}if ft.is_empty() {{
{WHITESPACE}{WHITESPACE}format!(\".{{language}}\")
{WHITESPACE}}} else {{
{WHITESPACE}{WHITESPACE}ft
{WHITESPACE}}}
}}
",
mappings.join("\n")
Expand Down
5 changes: 3 additions & 2 deletions mdsf.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/hougesen/mdsf/main/schemas/v0.1.1/mdsf.schema.json",
"$schema": "https://raw.githubusercontent.com/hougesen/mdsf/main/schemas/v0.1.2/mdsf.schema.json",
"format_finished_document": false,
"javascript_runtime": "node",
"languages": {
Expand Down Expand Up @@ -46,5 +46,6 @@
"vue": "prettier",
"yaml": [["prettier", "yamlfmt", "yamlfix"]],
"zig": "zigfmt"
}
},
"custom_file_extensions": {}
}
9 changes: 9 additions & 0 deletions schemas/v0.1.2/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
"default": "https://raw.githubusercontent.com/hougesen/mdsf/main/schemas/v0.1.2/mdsf.schema.json",
"type": "string"
},
"custom_file_extensions": {
"description": "Used for settings custom file extensions for a given language. ```json { \"custom_file_extensions\": { \"rust\": \".rust\" } } ```",
"default": {},
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"format_finished_document": {
"description": "Format the processed document with the selected markdown formatter.",
"default": false,
Expand All @@ -22,6 +30,7 @@
]
},
"languages": {
"description": "Defines which formatter is used by the language. ```json { \"languages\": { \"rust\": \"rustfmt\", \"js\": \"prettier\" } } ```",
"default": {},
"type": "object",
"additionalProperties": {
Expand Down
21 changes: 21 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,28 @@ pub struct MdsfConfig {
#[serde(default)]
pub javascript_runtime: JavaScriptRuntime,

/// Defines which formatter is used by the language.
/// ```json
/// {
/// "languages": {
/// "rust": "rustfmt",
/// "js": "prettier"
/// }
/// }
/// ```
#[serde(default)]
pub languages: std::collections::BTreeMap<String, MdsfFormatter<Tooling>>,

/// Used for settings custom file extensions for a given language.
/// ```json
/// {
/// "custom_file_extensions": {
/// "rust": ".rust"
/// }
/// }
/// ```
#[serde(default)]
pub custom_file_extensions: std::collections::BTreeMap<String, String>,
}

impl Default for MdsfConfig {
Expand All @@ -42,6 +62,7 @@ impl Default for MdsfConfig {
schema: default_schema_location(),
format_finished_document: false,
javascript_runtime: JavaScriptRuntime::default(),
custom_file_extensions: std::collections::BTreeMap::default(),

languages: default_tools(),
}
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/alejandra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ buildPythonPackage rec {
"#;

let snippet =
setup_snippet(input, &language_to_ext("nix")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("nix")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/autopep8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod test_autopep8 {
let expected_output = "def add(a: int, b: int) -> int: return a+b\n";

let snippet =
setup_snippet(input, &language_to_ext("python")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("python")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand Down
4 changes: 2 additions & 2 deletions src/formatters/beautysh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ add() {
";

let snippet =
setup_snippet(input, &language_to_ext("shell")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("shell")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand All @@ -59,7 +59,7 @@ add() {
";

let snippet =
setup_snippet(input, &language_to_ext("bash")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("bash")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand Down
6 changes: 3 additions & 3 deletions src/formatters/biome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ mod test_biome {
";

let snippet =
setup_snippet(input, &language_to_ext("json")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("json")).expect("it to create a snippet file");

let output = run_format(snippet.path())
.expect("it to be successful")
Expand All @@ -84,7 +84,7 @@ mod test_biome {
}
";

let snippet = setup_snippet(input, &language_to_ext("javascript"))
let snippet = setup_snippet(input, language_to_ext("javascript"))
.expect("it to create a snippet file");

let output = run_format(snippet.path())
Expand Down Expand Up @@ -114,7 +114,7 @@ number>
}
";

let snippet = setup_snippet(input, &language_to_ext("typescript"))
let snippet = setup_snippet(input, language_to_ext("typescript"))
.expect("it to create a snippet file");

let output = run_format(snippet.path())
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/black.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod test_black {
let expected_output = "def add(a: int, b: int) -> int:\n return a + b\n";

let snippet =
setup_snippet(input, &language_to_ext("python")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("python")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/blade_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ mod test_blade_formatter {
"#;

let snippet =
setup_snippet(input, &language_to_ext("sql")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("sql")).expect("it to create a snippet file");

let output = super::run(snippet.path())
.expect("it to be successful")
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/blue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod test_blue {
let expected_output = "def add(a: int, b: int) -> int:\n return a + b\n";

let snippet =
setup_snippet(input, &language_to_ext("python")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("python")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand Down
4 changes: 2 additions & 2 deletions src/formatters/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ mod test_buf {
}
";

let snippet = setup_snippet(input, &language_to_ext("protobuf"))
.expect("it to create a snippet file");
let snippet =
setup_snippet(input, language_to_ext("protobuf")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/cabal_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ executable msdf
";

let snippet =
setup_snippet(input, &language_to_ext("cabal")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("cabal")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand Down
18 changes: 9 additions & 9 deletions src/formatters/clang_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mod test_clang_format {
}";

let snippet =
setup_snippet(input, &language_to_ext("c")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("c")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand All @@ -53,7 +53,7 @@ mod test_clang_format {
}";

let snippet =
setup_snippet(input, &language_to_ext("cpp")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("cpp")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand All @@ -78,7 +78,7 @@ mod test_clang_format {
"namespace Mdsf {\nclass Adder {\n public static int add(int a, int b) {\n a - b;\n return a + b;\n }\n}\n}";

let snippet =
setup_snippet(input, &language_to_ext("csharp")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("csharp")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand All @@ -97,7 +97,7 @@ mod test_clang_format {

let expected_output = "async function asyncAddition(a, b) {\n a * b;\n return a + b\n}";

let snippet = setup_snippet(input, &language_to_ext("javascript"))
let snippet = setup_snippet(input, language_to_ext("javascript"))
.expect("it to create a snippet file");

let output = run(snippet.path())
Expand Down Expand Up @@ -126,7 +126,7 @@ mod test_clang_format {
}";

let snippet =
setup_snippet(input, &language_to_ext("json")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("json")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand All @@ -148,7 +148,7 @@ mod test_clang_format {
return a + b;
}";

let snippet = setup_snippet(input, &language_to_ext("objective-c"))
let snippet = setup_snippet(input, language_to_ext("objective-c"))
.expect("it to create a snippet file");

let output = run(snippet.path())
Expand All @@ -168,8 +168,8 @@ mod test_clang_format {
let expected_output =
"service SearchService { rpc Search(SearchRequest) returns (SearchResponse); }";

let snippet = setup_snippet(input, &language_to_ext("protobuf"))
.expect("it to create a snippet file");
let snippet =
setup_snippet(input, language_to_ext("protobuf")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand All @@ -196,7 +196,7 @@ mod test_clang_format {
}";

let snippet =
setup_snippet(input, &language_to_ext("java")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("java")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/cljstyle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod test_cljstyle {
";

let snippet =
setup_snippet(input, &language_to_ext("clojure")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("clojure")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/crystal_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ end
";

let snippet =
setup_snippet(input, &language_to_ext("crystal")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("crystal")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/csharpier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mod test_csharpier {
";

let snippet =
setup_snippet(input, &language_to_ext("csharp")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("csharp")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/dart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod test_dart_format {
";

let snippet =
setup_snippet(input, &language_to_ext("dart")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("dart")).expect("it to create a snippet file");

let output = super::run_format(snippet.path())
.expect("it to be successful")
Expand Down
6 changes: 3 additions & 3 deletions src/formatters/deno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ mod test_deno_fmt {
";

let snippet =
setup_snippet(input, &language_to_ext("json")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("json")).expect("it to create a snippet file");

let output = run_fmt(snippet.path())
.expect("it to be successful")
Expand All @@ -75,7 +75,7 @@ mod test_deno_fmt {
}
";

let snippet = setup_snippet(input, &language_to_ext("javascript"))
let snippet = setup_snippet(input, language_to_ext("javascript"))
.expect("it to create a snippet file");

let output = run_fmt(snippet.path())
Expand All @@ -102,7 +102,7 @@ mod test_deno_fmt {
}
";

let snippet = setup_snippet(input, &language_to_ext("typescript"))
let snippet = setup_snippet(input, language_to_ext("typescript"))
.expect("it to create a snippet file");

let output = run_fmt(snippet.path())
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/efmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ case Erlang of movie->[hello(mike,joe,robert),credits]; language->formatting_arg
"
;
let snippet =
setup_snippet(input, &language_to_ext("erlang")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("erlang")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/elm_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ main =
"#;

let snippet =
setup_snippet(input, &language_to_ext("elm")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("elm")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/erlfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ case Erlang of movie->[hello(mike,joe,robert),credits]; language->formatting_arg
language -> no_more_formatting_arguments
end.";
let snippet =
setup_snippet(input, &language_to_ext("erlang")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("erlang")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/fantomas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let add a b = a + b
";

let snippet =
setup_snippet(input, &language_to_ext("fsharp")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("fsharp")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/fourmolu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ addNumbers a b = do
";

let snippet =
setup_snippet(input, &language_to_ext("haskell")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("haskell")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/fprettify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ end program
";

let snippet =
setup_snippet(input, &language_to_ext("fortran")).expect("it to create a snippet file");
setup_snippet(input, language_to_ext("fortran")).expect("it to create a snippet file");

let output = run(snippet.path())
.expect("it to be successful")
Expand Down
Loading

0 comments on commit d1d55c3

Please sign in to comment.