Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(solidity): support forge fmt #231

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ mdsf init
| Rust | `leptosfmt`, `rustfmt`, `yew-fmt` |
| Scala | `scalafmt` |
| Shell | `beautysh`, `shfmt` |
| Solidity | `forge_fmt` |
| Sql | `sql-formatter`, `sqlfluff` |
| Swift | `swift-format`, `swiftformat` |
| Toml | `taplo` |
Expand Down
4 changes: 4 additions & 0 deletions mdsf.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@
"enabled": true,
"formatter": [["shfmt", "beautysh"]]
},
"solidity": {
"enabled": true,
"formatter": [["forge_fmt"]]
},
"sql": {
"enabled": true,
"formatter": [["sql-formatter", "sqlfluff"]]
Expand Down
41 changes: 41 additions & 0 deletions schemas/v0.0.6/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,17 @@
}
]
},
"solidity": {
"default": {
"enabled": true,
"formatter": [["forge_fmt"]]
},
"allOf": [
{
"$ref": "#/definitions/Lang_for_Solidity"
}
]
},
"sql": {
"default": {
"enabled": true,
Expand Down Expand Up @@ -1448,6 +1459,19 @@
},
"additionalProperties": false
},
"Lang_for_Solidity": {
"type": "object",
"properties": {
"enabled": {
"default": true,
"type": "boolean"
},
"formatter": {
"$ref": "#/definitions/MdsfFormatter_for_Solidity"
}
},
"additionalProperties": false
},
"Lang_for_Sql": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -2210,6 +2234,19 @@
}
]
},
"MdsfFormatter_for_Solidity": {
"anyOf": [
{
"$ref": "#/definitions/Solidity"
},
{
"type": "array",
"items": {
"$ref": "#/definitions/MdsfFormatter_for_Solidity"
}
}
]
},
"MdsfFormatter_for_Sql": {
"anyOf": [
{
Expand Down Expand Up @@ -2392,6 +2429,10 @@
"type": "string",
"enum": ["shfmt", "beautysh"]
},
"Solidity": {
"type": "string",
"enum": ["forge_fmt"]
},
"Sql": {
"type": "string",
"enum": ["sqlfluff", "sql-formatter"]
Expand Down
8 changes: 6 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::{
objective_c::ObjectiveC, ocaml::OCaml, perl::Perl, protobuf::Protobuf,
purescript::PureScript, python::Python, rescript::ReScript,
restructuredtext::ReStructuredText, roc::Roc, ruby::Ruby, rust::Rust, scala::Scala,
shell::Shell, sql::Sql, swift::Swift, toml::Toml, typescript::TypeScript, vue::Vue,
xml::Xml, yaml::Yaml, zig::Zig, Lang,
shell::Shell, solidity::Solidity, sql::Sql, swift::Swift, toml::Toml,
typescript::TypeScript, vue::Vue, xml::Xml, yaml::Yaml, zig::Zig, Lang,
},
runners::JavaScriptRuntime,
terminal::print_config_not_found,
Expand Down Expand Up @@ -187,6 +187,9 @@ pub struct MdsfConfig {
#[serde(default)]
pub shell: Lang<Shell>,

#[serde(default)]
pub solidity: Lang<Solidity>,

#[serde(default)]
pub sql: Lang<Sql>,

Expand Down Expand Up @@ -275,6 +278,7 @@ impl Default for MdsfConfig {
scala: Lang::<Scala>::default(),
shell: Lang::<Shell>::default(),
sql: Lang::<Sql>::default(),
solidity: Lang::<Solidity>::default(),
swift: Lang::<Swift>::default(),
toml: Lang::<Toml>::default(),
typescript: Lang::<TypeScript>::default(),
Expand Down
13 changes: 13 additions & 0 deletions src/formatters/forge_fmt.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_forge_fmt(
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("forge");

cmd.arg("fmt").arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}
2 changes: 2 additions & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub mod erb_formatter;
pub mod erlfmt;
pub mod fantomas;
pub mod findent;
pub mod forge_fmt;
pub mod fourmolu;
pub mod fprettify;
pub mod gci;
Expand Down Expand Up @@ -229,6 +230,7 @@ pub fn format_snippet(config: &MdsfConfig, info: &LineInfo, code: &str) -> Strin
Language::Rust => config.rust.format(snippet_path, info),
Language::Scala => config.scala.format(snippet_path, info),
Language::Shell(_flavor) => config.shell.format(snippet_path, info),
Language::Solidity => config.solidity.format(snippet_path, info),
Language::Sql => config.sql.format(snippet_path, info),
Language::Swift => config.swift.format(snippet_path, info),
Language::Toml => config.toml.format(snippet_path, info),
Expand Down
6 changes: 6 additions & 0 deletions src/languages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub mod ruby;
pub mod rust;
pub mod scala;
pub mod shell;
pub mod solidity;
pub mod sql;
pub mod swift;
pub mod toml;
Expand Down Expand Up @@ -173,6 +174,7 @@ pub enum Language {
Elm,
Erb,
Erlang,

Fortran,
FSharp,
Gleam,
Expand Down Expand Up @@ -217,6 +219,7 @@ pub enum Language {
Xml,
Yaml,
Zig,
Solidity,
}

impl core::fmt::Display for Language {
Expand Down Expand Up @@ -273,6 +276,7 @@ impl core::fmt::Display for Language {
Self::Rust => f.write_str("rust"),
Self::Scala => f.write_str("scala"),
Self::Shell(flavor) => flavor.fmt(f),
Self::Solidity => f.write_str("solidity"),
Self::Sql => f.write_str("sql"),
Self::Swift => f.write_str("swift"),
Self::Toml => f.write_str("toml"),
Expand Down Expand Up @@ -311,6 +315,7 @@ impl Language {
"elm" => Some(Self::Elm),
"erb" => Some(Self::Erb),
"erlang" => Some(Self::Erlang),
"solidity" => Some(Self::Solidity),
"fortran" => Some(Self::Fortran),
"fsharp" => Some(Self::FSharp),
"gleam" => Some(Self::Gleam),
Expand Down Expand Up @@ -389,6 +394,7 @@ impl Language {
Self::Elm => ".elm",
Self::Erb => ".erb",
Self::Erlang => ".erl",
Self::Solidity => ".sol",
Self::Fortran => ".f",
Self::FSharp => ".fs",
Self::Gleam => ".gleam",
Expand Down
64 changes: 64 additions & 0 deletions src/languages/solidity.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::{forge_fmt::format_using_forge_fmt, MdsfFormatter},
};

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

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

impl Default for MdsfFormatter<Solidity> {
#[inline]
fn default() -> Self {
Self::Multiple(vec![Self::Multiple(vec![Self::Single(Solidity::ForgeFmt)])])
}
}

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

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

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

#[test]
fn it_should_be_enabled_by_default() {
assert!(Lang::<Solidity>::default().enabled);
}
}
Loading