Skip to content

Commit

Permalink
feat(assembly): support asmfmt (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Jun 3, 2024
1 parent 89ba75d commit 88f06e0
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ mdsf init

| Language | Formatters |
| ---------------- | ---------------------------------------------------------------------------------------------------- |
| Assembly | `asmfmt` |
| Blade | `blade-formatter` |
| C | `clang-format` |
| CSharp | `clang-format`, `csharpier` |
Expand Down
4 changes: 4 additions & 0 deletions mdsf.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"$schema": "https://raw.githubusercontent.com/hougesen/mdsf/main/schemas/v0.0.7/mdsf.schema.json",
"format_finished_document": false,
"javascript_runtime": "node",
"assembly": {
"enabled": true,
"formatter": "asmfmt"
},
"blade": {
"enabled": true,
"formatter": "blade-formatter"
Expand Down
41 changes: 41 additions & 0 deletions schemas/v0.0.7/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
"default": "https://raw.githubusercontent.com/hougesen/mdsf/main/schemas/v0.0.7/mdsf.schema.json",
"type": "string"
},
"assembly": {
"default": {
"enabled": true,
"formatter": "asmfmt"
},
"allOf": [
{
"$ref": "#/definitions/Lang_for_Assembly"
}
]
},
"blade": {
"default": {
"enabled": true,
Expand Down Expand Up @@ -692,6 +703,10 @@
},
"additionalProperties": false,
"definitions": {
"Assembly": {
"type": "string",
"enum": ["asmfmt"]
},
"Blade": {
"type": "string",
"enum": ["blade-formatter"]
Expand Down Expand Up @@ -828,6 +843,19 @@
"type": "string",
"enum": ["ktlint", "ktfmt"]
},
"Lang_for_Assembly": {
"type": "object",
"properties": {
"enabled": {
"default": true,
"type": "boolean"
},
"formatter": {
"$ref": "#/definitions/MdsfFormatter_for_Assembly"
}
},
"additionalProperties": false
},
"Lang_for_Blade": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -1623,6 +1651,19 @@
"misspell"
]
},
"MdsfFormatter_for_Assembly": {
"anyOf": [
{
"$ref": "#/definitions/Assembly"
},
{
"type": "array",
"items": {
"$ref": "#/definitions/MdsfFormatter_for_Assembly"
}
}
]
},
"MdsfFormatter_for_Blade": {
"anyOf": [
{
Expand Down
26 changes: 15 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use schemars::JsonSchema;
use crate::{
error::MdsfError,
languages::{
blade::Blade, c::C, cabal::Cabal, clojure::Clojure, cpp::Cpp, crystal::Crystal,
csharp::CSharp, css::Css, d::D, dart::Dart, elixir::Elixir, elm::Elm, erb::Erb,
erlang::Erlang, fortran::Fortran, fsharp::FSharp, gleam::Gleam, go::Go, graphql::GraphQL,
groovy::Groovy, handlebars::Handlebars, haskell::Haskell, hcl::Hcl, html::Html, java::Java,
javascript::JavaScript, json::Json, julia::Julia, just::Just, kcl::Kcl, kotlin::Kotlin,
lua::Lua, markdown::Markdown, mustache::Mustache, nim::Nim, nix::Nix, nunjucks::Nunjucks,
objective_c::ObjectiveC, ocaml::OCaml, perl::Perl, protobuf::Protobuf, puppet::Puppet,
purescript::PureScript, python::Python, rescript::ReScript,
restructuredtext::ReStructuredText, roc::Roc, ruby::Ruby, rust::Rust, scala::Scala,
shell::Shell, solidity::Solidity, sql::Sql, swift::Swift, toml::Toml,
assembly::Assembly, blade::Blade, c::C, cabal::Cabal, clojure::Clojure, cpp::Cpp,
crystal::Crystal, csharp::CSharp, css::Css, d::D, dart::Dart, elixir::Elixir, elm::Elm,
erb::Erb, erlang::Erlang, fortran::Fortran, fsharp::FSharp, gleam::Gleam, go::Go,
graphql::GraphQL, groovy::Groovy, handlebars::Handlebars, haskell::Haskell, hcl::Hcl,
html::Html, java::Java, javascript::JavaScript, json::Json, julia::Julia, just::Just,
kcl::Kcl, kotlin::Kotlin, lua::Lua, markdown::Markdown, mustache::Mustache, nim::Nim,
nix::Nix, nunjucks::Nunjucks, objective_c::ObjectiveC, ocaml::OCaml, perl::Perl,
protobuf::Protobuf, puppet::Puppet, purescript::PureScript, python::Python,
rescript::ReScript, restructuredtext::ReStructuredText, roc::Roc, ruby::Ruby, rust::Rust,
scala::Scala, shell::Shell, solidity::Solidity, sql::Sql, swift::Swift, toml::Toml,
typescript::TypeScript, vue::Vue, xml::Xml, yaml::Yaml, zig::Zig, Lang,
},
runners::JavaScriptRuntime,
Expand Down Expand Up @@ -43,6 +43,9 @@ pub struct MdsfConfig {
#[serde(default)]
pub javascript_runtime: JavaScriptRuntime,

#[serde(default)]
pub assembly: Lang<Assembly>,

#[serde(default)]
pub blade: Lang<Blade>,

Expand Down Expand Up @@ -232,6 +235,8 @@ impl Default for MdsfConfig {
format_finished_document: false,
javascript_runtime: JavaScriptRuntime::default(),

assembly: Lang::<Assembly>::default(),
blade: Lang::<Blade>::default(),
c: Lang::<C>::default(),
cabal: Lang::<Cabal>::default(),
clojure: Lang::<Clojure>::default(),
Expand Down Expand Up @@ -291,7 +296,6 @@ impl Default for MdsfConfig {
xml: Lang::<Xml>::default(),
yaml: Lang::<Yaml>::default(),
zig: Lang::<Zig>::default(),
blade: Lang::<Blade>::default(),
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/formatters/asmfmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_asmfmt(
file_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("asmfmt");

cmd.arg("-w").arg(file_path);

execute_command(&mut cmd, file_path)
}
2 changes: 2 additions & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use which::which;
use crate::{config::MdsfConfig, error::MdsfError, languages::Language, LineInfo, DEBUG};

pub mod alejandra;
pub mod asmfmt;
pub mod auto_optional;
pub mod autocorrect;
pub mod autopep8;
Expand Down Expand Up @@ -187,6 +188,7 @@ pub fn format_snippet(config: &MdsfConfig, info: &LineInfo, code: &str) -> Strin
let snippet_path = snippet.path();

if let Ok(Some(formatted_code)) = match info.language {
Language::Assembly => config.assembly.format(snippet_path, info),
Language::Blade => config.blade.format(snippet_path, info),
Language::C => config.c.format(snippet_path, info),
Language::CSharp => config.csharp.format(snippet_path, info),
Expand Down
64 changes: 64 additions & 0 deletions src/languages/assembly.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use schemars::JsonSchema;

use super::{Lang, LanguageFormatter};
use crate::{
error::MdsfError,
formatters::{asmfmt::format_using_asmfmt, MdsfFormatter},
};

#[derive(Default, serde::Serialize, serde::Deserialize, JsonSchema)]
#[cfg_attr(test, derive(Debug, PartialEq, Eq))]
pub enum Assembly {
#[default]
#[serde(rename = "asmfmt")]
Asmfmt,
}

impl Default for Lang<Assembly> {
#[inline]
fn default() -> Self {
Self {
enabled: true,
formatter: MdsfFormatter::<Assembly>::default(),
}
}
}

impl Default for MdsfFormatter<Assembly> {
#[inline]
fn default() -> Self {
Self::Single(Assembly::Asmfmt)
}
}

impl LanguageFormatter for Assembly {
#[inline]
fn format_snippet(
&self,
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
match self {
Self::Asmfmt => format_using_asmfmt(snippet_path),
}
}
}

impl core::fmt::Display for Assembly {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Asmfmt => write!(f, "asmfmt"),
}
}
}

#[cfg(test)]
mod test_assembly {
use super::Assembly;
use crate::languages::Lang;

#[test]
fn it_should_be_enabled_by_default() {
assert!(Lang::<Assembly>::default().enabled);
}
}
5 changes: 5 additions & 0 deletions src/languages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
LineInfo,
};

pub mod assembly;
pub mod blade;
pub mod c;
pub mod cabal;
Expand Down Expand Up @@ -161,6 +162,7 @@ impl core::fmt::Display for TypeScriptFlavor {

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Language {
Assembly,
Blade,
C,
CSharp,
Expand Down Expand Up @@ -227,6 +229,7 @@ impl core::fmt::Display for Language {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Assembly => f.write_str("assembly"),
Self::Blade => f.write_str("blade"),
Self::C => f.write_str("c"),
Self::CSharp => f.write_str("c#"),
Expand Down Expand Up @@ -302,6 +305,7 @@ impl Language {
#[inline]
pub fn maybe_from_str(input: &str) -> Option<Self> {
match input.to_ascii_lowercase().as_str() {
"assembly" | "asm" => Some(Self::Assembly),
"bash" => Some(Self::Shell(ShellFlavor::Bash)),
"blade" => Some(Self::Blade),
"c" | "clang" => Some(Self::C),
Expand Down Expand Up @@ -380,6 +384,7 @@ impl Language {
#[inline]
pub const fn to_file_ext(&self) -> &'static str {
match self {
Self::Assembly => ".s",
Self::Blade => ".blade.php",
Self::C => ".c",
Self::CSharp => ".cs",
Expand Down

0 comments on commit 88f06e0

Please sign in to comment.