Skip to content
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ The parser supports all standard EditorConfig properties:
| `trim_trailing_whitespace` | `bool` | `true`, `false` |
| `insert_final_newline` | `bool` | `true`, `false` |
| `max_line_length` | `MaxLineLength` | Positive integer or `off` |
| `quote_type` | `QuoteType` | `single`, `double`, `auto` |

Note: `max_line_length` is not part of the official EditorConfig spec but is commonly used by tools like [Prettier](https://prettier.io/docs/next/configuration#editorconfig).
Note: `max_line_length` and `quote_type` are not part of the official EditorConfig spec but are commonly used by tools like [Prettier](https://prettier.io/docs/next/configuration#editorconfig).

## How It Works

Expand Down
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ pub struct EditorConfigProperties {
/// Not part of spec <https://github.com/editorconfig/editorconfig-vscode/issues/53#issuecomment-462432616>
/// But documented in <https://prettier.io/docs/next/configuration#editorconfig>
pub max_line_length: EditorConfigProperty<MaxLineLength>,

/// Quote type for string literals.
/// Not part of spec but supported by Prettier, VSCode, IntelliJ, and others as a domain-specific extension.
pub quote_type: EditorConfigProperty<QuoteType>,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
Expand Down Expand Up @@ -121,6 +125,13 @@ pub enum Charset {
Utf16le,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum QuoteType {
Single,
Double,
Auto,
}

impl EditorConfig {
/// <https://spec.editorconfig.org/index.html#id6>
pub fn parse(source_text: &str) -> Self {
Expand Down Expand Up @@ -198,6 +209,9 @@ impl EditorConfig {
properties.max_line_length =
EditorConfigProperty::<MaxLineLength>::parse(value);
}
"quote_type" => {
properties.quote_type = QuoteType::parse(value);
}
_ => {}
}
}
Expand Down Expand Up @@ -245,6 +259,7 @@ impl EditorConfigProperties {
self.trim_trailing_whitespace.override_with(&other.trim_trailing_whitespace);
self.insert_final_newline.override_with(&other.insert_final_newline);
self.max_line_length.override_with(&other.max_line_length);
self.quote_type.override_with(&other.quote_type);
}
}

Expand Down Expand Up @@ -322,6 +337,22 @@ impl EditorConfigProperty<Charset> {
}
}

impl QuoteType {
fn parse(s: &str) -> EditorConfigProperty<Self> {
if s.eq_ignore_ascii_case("single") {
EditorConfigProperty::Value(Self::Single)
} else if s.eq_ignore_ascii_case("double") {
EditorConfigProperty::Value(Self::Double)
} else if s.eq_ignore_ascii_case("auto") {
EditorConfigProperty::Value(Self::Auto)
} else if s.eq_ignore_ascii_case("unset") {
EditorConfigProperty::Unset
} else {
EditorConfigProperty::None
}
}
}

impl EditorConfigProperty<MaxLineLength> {
fn parse(s: &str) -> Self {
if s.eq_ignore_ascii_case("off") {
Expand Down
41 changes: 40 additions & 1 deletion tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::{Path, PathBuf};

use editorconfig_parser::{
Charset, EditorConfig, EditorConfigProperties, EditorConfigProperty::Value, EndOfLine,
IndentStyle, MaxLineLength,
IndentStyle, MaxLineLength, QuoteType,
};

#[test]
Expand Down Expand Up @@ -64,6 +64,44 @@ fn values() {
assert_eq!(properties.max_line_length, Value(MaxLineLength::Number(80)));
}

#[test]
fn quote_type() {
let editor_config = EditorConfig::parse(
"
[*]
quote_type = single",
);
let properties = &editor_config.sections()[0].properties;
assert_eq!(properties.quote_type, Value(QuoteType::Single));

let editor_config = EditorConfig::parse(
"
[*]
quote_type = double",
);
let properties = &editor_config.sections()[0].properties;
assert_eq!(properties.quote_type, Value(QuoteType::Double));

let editor_config = EditorConfig::parse(
"
[*]
quote_type = auto",
);
let properties = &editor_config.sections()[0].properties;
assert_eq!(properties.quote_type, Value(QuoteType::Auto));
}

#[test]
fn quote_type_case_insensitive() {
let editor_config = EditorConfig::parse(
"
[*]
quote_type = SINGLE",
);
let properties = &editor_config.sections()[0].properties;
assert_eq!(properties.quote_type, Value(QuoteType::Single));
}

#[test]
fn max_line_length_off() {
let editor_config = EditorConfig::parse(
Expand Down Expand Up @@ -174,6 +212,7 @@ fn unset() {
indent_style = unset
indent_size = unset
max_line_length = unset
quote_type = unset
",
);
let path = Path::new("/").join("file.foo");
Expand Down
Loading