Skip to content

Commit

Permalink
add hexColorLength option
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed May 11, 2024
1 parent b9ba9b3 commit 1df1f38
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 9 deletions.
43 changes: 43 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions dprint_plugin/deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
22 changes: 18 additions & 4 deletions dprint_plugin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -72,6 +69,23 @@ pub(crate) fn resolve_config(
Default::default()
}
},
hex_color_length: get_nullable_value::<String>(
&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",
Expand Down
12 changes: 12 additions & 0 deletions malva/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HexColorLength>,

/// See [`quotes`](https://github.com/g-plane/malva/blob/main/docs/config.md#quotes) on GitHub
pub quotes: Quotes,

Expand Down Expand Up @@ -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"))]
Expand Down
46 changes: 41 additions & 5 deletions malva/src/doc_gen/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
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> {
Expand Down
19 changes: 19 additions & 0 deletions malva/tests/fmt/css/color/hexcolor-long.css
Original file line number Diff line number Diff line change
@@ -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: #测测试试测试;
}
22 changes: 22 additions & 0 deletions malva/tests/fmt/css/color/hexcolor-long.snap
Original file line number Diff line number Diff line change
@@ -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: #测测试试测试;
}
19 changes: 19 additions & 0 deletions malva/tests/fmt/css/color/hexcolor-short.css
Original file line number Diff line number Diff line change
@@ -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: #测测试试测试;
}
22 changes: 22 additions & 0 deletions malva/tests/fmt/css/color/hexcolor-short.snap
Original file line number Diff line number Diff line change
@@ -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: #测测试试测试;
}

0 comments on commit 1df1f38

Please sign in to comment.