From 1df1f38f15bf8bcfc859b59f86c31b55c9e15d8b Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Sat, 11 May 2024 18:00:10 +0800 Subject: [PATCH] add `hexColorLength` option --- docs/config.md | 43 +++++++++++++++++ dprint_plugin/deployment/schema.json | 5 ++ dprint_plugin/src/config.rs | 22 +++++++-- malva/src/config.rs | 12 +++++ malva/src/doc_gen/value.rs | 46 +++++++++++++++++-- malva/tests/fmt/css/color/hexcolor-long.css | 19 ++++++++ malva/tests/fmt/css/color/hexcolor-long.snap | 22 +++++++++ malva/tests/fmt/css/color/hexcolor-short.css | 19 ++++++++ malva/tests/fmt/css/color/hexcolor-short.snap | 22 +++++++++ 9 files changed, 201 insertions(+), 9 deletions(-) create mode 100644 malva/tests/fmt/css/color/hexcolor-long.css create mode 100644 malva/tests/fmt/css/color/hexcolor-long.snap create mode 100644 malva/tests/fmt/css/color/hexcolor-short.css create mode 100644 malva/tests/fmt/css/color/hexcolor-short.snap diff --git a/docs/config.md b/docs/config.md index bd09a6e..3deff20 100644 --- a/docs/config.md +++ b/docs/config.md @@ -131,6 +131,49 @@ a { } ``` +## `hexColorLength` + +Control the hex color values in short-hand form or long-hand form. + +Possible options: + +- `null`: Hex color values will be kept as-is. +- `"short"`: Hex color values will be converted to short-hand form. +- `"long"`: Hex color values will be converted to long-hand form. + +Default option is `null`. + +### Example for `null` + +[Playground](https://malva-play.vercel.app/?code=H4sIAAAAAAAAA0tUqOZSUEjOz8kvslJQTktLs0blgkVqAf9LeqomAAAA&config=H4sIAAAAAAAAA6vmqgUAqLu%2BcwMAAAA%3D&syntax=css) + +```css +a { + color: #fff; + color: #ffffff; +} +``` + +### Example for `"short"` + +[Playground](https://malva-play.vercel.app/?code=H4sIAAAAAAAAA0tUqOZSUEjOz8kvslJQTgMDa65aADLG74sXAAAA&config=H4sIAAAAAAAAA6vmUlBQykitcM7PyS%2FySc1LL8lQslJQKs7ILypR4qoFAAuXqYIfAAAA&syntax=css) + +```css +a { + color: #fff; +} +``` + +### Example for `"long"` + +[Playground](https://malva-play.vercel.app/?code=H4sIAAAAAAAAA0tUqOZSUEjOz8kvslJQTktLs%2BaqBQABymepFAAAAA%3D%3D&config=H4sIAAAAAAAAA6vmUlBQykitcM7PyS%2FySc1LL8lQslJQysnPS1fiqgUAuPAwgx4AAAA%3D&syntax=css) + +```css +a { + color: #ffffff; +} +``` + ## `quotes` Control the quotes of strings. diff --git a/dprint_plugin/deployment/schema.json b/dprint_plugin/deployment/schema.json index 8e93e5f..881f54d 100644 --- a/dprint_plugin/deployment/schema.json +++ b/dprint_plugin/deployment/schema.json @@ -29,6 +29,11 @@ "enum": ["ignore", "lower", "upper"], "default": "lower" }, + "hexColorLength": { + "type": ["string", "null"], + "enum": ["short", "long"], + "default": null + }, "quotes": { "type": "string", "enum": ["alwaysDouble", "alwaysSingle", "preferDouble", "preferSingle"], diff --git a/dprint_plugin/src/config.rs b/dprint_plugin/src/config.rs index 52c570a..e63de34 100644 --- a/dprint_plugin/src/config.rs +++ b/dprint_plugin/src/config.rs @@ -2,10 +2,7 @@ use dprint_core::configuration::{ get_nullable_value, get_unknown_property_diagnostics, get_value, ConfigKeyMap, ConfigurationDiagnostic, GlobalConfiguration, NewLineKind, ResolveConfigurationResult, }; -use malva::config::{ - BlockSelectorLineBreak, DeclarationOrder, FormatOptions, HexCase, LanguageOptions, - LayoutOptions, LineBreak, OperatorLineBreak, Quotes, -}; +use malva::config::*; pub(crate) fn resolve_config( mut config: ConfigKeyMap, @@ -72,6 +69,23 @@ pub(crate) fn resolve_config( Default::default() } }, + hex_color_length: get_nullable_value::( + &mut config, + "hexColorLength", + &mut diagnostics, + ) + .as_deref() + .and_then(|value| match value { + "short" => Some(HexColorLength::Short), + "long" => Some(HexColorLength::Long), + _ => { + diagnostics.push(ConfigurationDiagnostic { + property_name: "hexColorLength".into(), + message: "invalid value for config `hexColorLength`".into(), + }); + None + } + }), quotes: match &*get_value( &mut config, "quotes", diff --git a/malva/src/config.rs b/malva/src/config.rs index 77767e6..adc5a77 100644 --- a/malva/src/config.rs +++ b/malva/src/config.rs @@ -79,6 +79,10 @@ pub struct LanguageOptions { /// See [`hexCase`](https://github.com/g-plane/malva/blob/main/docs/config.md#hexcase) on GitHub pub hex_case: HexCase, + #[cfg_attr(feature = "config_serde", serde(alias = "hexColorLength"))] + /// See [`hexColorLength`](https://github.com/g-plane/malva/blob/main/docs/config.md#hexcolorlength) on GitHub + pub hex_color_length: Option, + /// See [`quotes`](https://github.com/g-plane/malva/blob/main/docs/config.md#quotes) on GitHub pub quotes: Quotes, @@ -135,6 +139,14 @@ pub enum HexCase { Upper, } +#[derive(Clone, Debug)] +#[cfg_attr(feature = "config_serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "config_serde", serde(rename_all = "kebab-case"))] +pub enum HexColorLength { + Short, + Long, +} + #[derive(Clone, Debug, Default)] #[cfg_attr(feature = "config_serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "config_serde", serde(rename_all = "kebab-case"))] diff --git a/malva/src/doc_gen/value.rs b/malva/src/doc_gen/value.rs index e3bd82f..e1ed74f 100644 --- a/malva/src/doc_gen/value.rs +++ b/malva/src/doc_gen/value.rs @@ -650,13 +650,49 @@ impl<'s> DocGen<'s> for UrlValue<'s> { } fn format_hex_raw(raw: &str, ctx: &Ctx) -> String { - use crate::config::HexCase; + use crate::config::{HexCase, HexColorLength}; + + let chars = raw.chars().collect::>(); + let mut hex = if chars.iter().all(|c| c.is_ascii_hexdigit()) { + match (chars.as_slice(), &ctx.options.hex_color_length) { + ([c1, c2, c3], Some(HexColorLength::Long)) => { + format!("#{c1}{c1}{c2}{c2}{c3}{c3}") + } + ([c1, c2, c3, c4], Some(HexColorLength::Long)) => { + format!("#{c1}{c1}{c2}{c2}{c3}{c3}{c4}{c4}") + } + ([c1, c2, c3, c4, c5, c6], Some(HexColorLength::Short)) + if c1.eq_ignore_ascii_case(c2) + && c3.eq_ignore_ascii_case(c4) + && c5.eq_ignore_ascii_case(c6) => + { + format!("#{c1}{c3}{c5}") + } + ([c1, c2, c3, c4, c5, c6, c7, c8], Some(HexColorLength::Short)) + if c1.eq_ignore_ascii_case(c2) + && c3.eq_ignore_ascii_case(c4) + && c5.eq_ignore_ascii_case(c6) + && c7.eq_ignore_ascii_case(c8) => + { + format!("#{c1}{c3}{c5}{c7}") + } + _ => format!("#{raw}"), + } + } else { + format!("#{raw}") + }; match ctx.options.hex_case { - HexCase::Ignore => format!("#{}", raw), - HexCase::Lower => format!("#{}", raw.to_ascii_lowercase()), - HexCase::Upper => format!("#{}", raw.to_ascii_uppercase()), - } + HexCase::Ignore => {} + HexCase::Lower => { + hex.make_ascii_lowercase(); + } + HexCase::Upper => { + hex.make_ascii_uppercase(); + } + }; + + hex } fn format_number_raw<'s>(raw: &'s str, ctx: &Ctx<'_, 's>) -> Cow<'s, str> { diff --git a/malva/tests/fmt/css/color/hexcolor-long.css b/malva/tests/fmt/css/color/hexcolor-long.css new file mode 100644 index 0000000..3290c2e --- /dev/null +++ b/malva/tests/fmt/css/color/hexcolor-long.css @@ -0,0 +1,19 @@ +/*cfg + hexColorLength = "long" + hexCase = "ignore" +*/ +.foo { + color: #AAA; + -o-color: #fabcd3; + -webkit-color: #873; + -moz-color: #6bcdef; + -ms-color: #AabBCc; + color: #F09F; + color: #FF0099FF; + + ignore: #a; + ignore: #aabbccddee; + ignore: #\61\61\61; + ignore: #\61\61\61\61\61\61; + ignore: #测测试试测试; +} diff --git a/malva/tests/fmt/css/color/hexcolor-long.snap b/malva/tests/fmt/css/color/hexcolor-long.snap new file mode 100644 index 0000000..85c1b22 --- /dev/null +++ b/malva/tests/fmt/css/color/hexcolor-long.snap @@ -0,0 +1,22 @@ +--- +source: malva/tests/fmt.rs +--- +/*cfg + hexColorLength = "long" + hexCase = "ignore" +*/ +.foo { + color: #AAAAAA; + -o-color: #fabcd3; + -webkit-color: #887733; + -moz-color: #6bcdef; + -ms-color: #AabBCc; + color: #FF0099FF; + color: #FF0099FF; + + ignore: #a; + ignore: #aabbccddee; + ignore: #\61\61\61; + ignore: #\61\61\61\61\61\61; + ignore: #测测试试测试; +} diff --git a/malva/tests/fmt/css/color/hexcolor-short.css b/malva/tests/fmt/css/color/hexcolor-short.css new file mode 100644 index 0000000..7d814c5 --- /dev/null +++ b/malva/tests/fmt/css/color/hexcolor-short.css @@ -0,0 +1,19 @@ +/*cfg + hexColorLength = "short" + hexCase = "ignore" +*/ +.foo { + color: #AAA; + -o-color: #fabcd3; + -webkit-color: #873; + -moz-color: #6bcdef; + -ms-color: #AabBCc; + color: #F09F; + color: #FF0099FF; + + ignore: #a; + ignore: #aabbccddee; + ignore: #\61\61\61; + ignore: #\61\61\61\61\61\61; + ignore: #测测试试测试; +} diff --git a/malva/tests/fmt/css/color/hexcolor-short.snap b/malva/tests/fmt/css/color/hexcolor-short.snap new file mode 100644 index 0000000..72f81cb --- /dev/null +++ b/malva/tests/fmt/css/color/hexcolor-short.snap @@ -0,0 +1,22 @@ +--- +source: malva/tests/fmt.rs +--- +/*cfg + hexColorLength = "short" + hexCase = "ignore" +*/ +.foo { + color: #AAA; + -o-color: #fabcd3; + -webkit-color: #873; + -moz-color: #6bcdef; + -ms-color: #AbC; + color: #F09F; + color: #F09F; + + ignore: #a; + ignore: #aabbccddee; + ignore: #\61\61\61; + ignore: #\61\61\61\61\61\61; + ignore: #测测试试测试; +}