Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_service): configuration file shape #2825

Merged
merged 6 commits into from
Jul 6, 2022
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
14 changes: 8 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions crates/rome_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ use rome_rowan::{
};
use std::error::Error;
use std::fmt;
use std::fmt::Display;
use std::num::ParseIntError;
use std::str::FromStr;

Expand Down Expand Up @@ -172,6 +173,16 @@ impl TryFrom<u16> for LineWidth {
}
}

impl Display for LineWidthFromIntError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(
f,
"The line width exceeds the maximum value ({})",
LineWidth::MAX
)
}
}

impl From<LineWidth> for u16 {
fn from(value: LineWidth) -> Self {
value.0
Expand Down
3 changes: 2 additions & 1 deletion crates/rome_service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ edition = "2021"

[dependencies]
dashmap = "5.2.0"

serde = { version = "1.0.133", features = ["derive"] }
serde_json = "1.0.74"
rome_analyze = { path = "../rome_analyze" }
rome_console = { path = "../rome_console" }
rome_diagnostics = { path = "../rome_diagnostics" }
Expand Down
66 changes: 66 additions & 0 deletions crates/rome_service/src/configuration/formatter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use rome_formatter::{IndentStyle, LineWidth};
use serde::Deserialize;
#[derive(Deserialize, Debug, Eq, PartialEq)]
#[serde(rename_all = "camelCase", default)]
pub struct FormatterConfiguration {
// if `false`, it disables the feature. `true` by default
pub enabled: bool,

/// Stores whether formatting should be allowed to proceed if a given file
/// has syntax errors
pub format_with_errors: bool,

/// The indent style.
pub indent_style: PlainIndentStyle,

/// The size of the indentation, 2 by default
indent_size: u8,

/// What's the max width of a line. Defaults to 80.
#[serde(deserialize_with = "deserialize_line_width")]
pub line_width: LineWidth,
}

impl From<&FormatterConfiguration> for IndentStyle {
fn from(c: &FormatterConfiguration) -> Self {
match c.indent_style {
PlainIndentStyle::Tab => IndentStyle::Tab,
PlainIndentStyle::Space => IndentStyle::Space(c.indent_size),
}
}
}

fn deserialize_line_width<'de, D>(deserializer: D) -> Result<LineWidth, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let value: u16 = Deserialize::deserialize(deserializer)?;
LineWidth::try_from(value).map_err(serde::de::Error::custom)
}

impl Default for FormatterConfiguration {
fn default() -> Self {
Self {
enabled: true,
format_with_errors: false,
indent_size: 2,
indent_style: PlainIndentStyle::default(),
line_width: LineWidth::default(),
}
}
}

#[derive(Deserialize, Debug, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum PlainIndentStyle {
/// Tab
Tab,
/// Space
Space,
}

impl Default for PlainIndentStyle {
fn default() -> Self {
Self::Tab
}
}
36 changes: 36 additions & 0 deletions crates/rome_service/src/configuration/javascript.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use rome_js_formatter::context::QuoteStyle;
use serde::Deserialize;

#[derive(Default, Debug, Deserialize, Eq, PartialEq)]
pub struct JavascriptConfiguration {
pub formatter: JavascriptFormatter,
}

#[derive(Default, Debug, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase", default)]
pub struct JavascriptFormatter {
/// The style for quotes. Defaults to double.
pub quote_style: PlainQuoteStyle,
}

#[derive(Deserialize, Debug, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum PlainQuoteStyle {
Double,
Single,
}

impl Default for PlainQuoteStyle {
fn default() -> Self {
Self::Double
}
}

impl From<JavascriptConfiguration> for QuoteStyle {
fn from(j: JavascriptConfiguration) -> Self {
match j.formatter.quote_style {
PlainQuoteStyle::Double => QuoteStyle::Double,
PlainQuoteStyle::Single => QuoteStyle::Single,
}
}
}
27 changes: 27 additions & 0 deletions crates/rome_service/src/configuration/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! This module contains the configuration of `rome.json`
//!
//! The configuration is divided by "tool", and then it's possible to further customise it
//! by language. The language might further options divided by tool.

use crate::configuration::formatter::FormatterConfiguration;
use crate::configuration::javascript::JavascriptConfiguration;
use serde::Deserialize;

mod formatter;
mod javascript;

/// The configuration that is contained inside the file `rome.json`
#[derive(Default, Debug, Eq, PartialEq, Deserialize)]
#[serde(default)]
pub struct Configuration {
/// One root file should exist. Useful when `extends` comes into play.
///
/// If `true`, this file should be the master configuration.
pub root: bool,

/// The configuration of the formatter
pub formatter: FormatterConfiguration,

/// Specific configuration for the JavaScript language
pub javascript: JavascriptConfiguration,
}
1 change: 1 addition & 0 deletions crates/rome_service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::ops::{Deref, DerefMut};

pub mod configuration;
mod file_handlers;
pub mod settings;
pub mod workspace;
Expand Down
15 changes: 15 additions & 0 deletions crates/rome_service/tests/all_fields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"root": true,
"formatter": {
"enabled": true,
"formatWithErrors": true,
"indentStyle": "tab",
"indentSize": 2,
"lineWidth": 80
},
"javascript": {
"formatter": {
"quoteStyle": "double"
}
}
}
1 change: 1 addition & 0 deletions crates/rome_service/tests/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
6 changes: 6 additions & 0 deletions crates/rome_service/tests/line_width.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"root": true,
"formatter": {
"lineWidth": 450
}
}
55 changes: 55 additions & 0 deletions crates/rome_service/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
mod configuration {
use rome_service::configuration::Configuration;
use std::env::current_dir;
use std::fs::read_to_string;

#[test]
fn parse_all_fields() {
let mut working_dir = current_dir().unwrap();
working_dir.push("tests");
working_dir.push("all_fields.json");
let content = read_to_string(working_dir).unwrap();

let configuration = serde_json::from_str::<Configuration>(&content);

assert!(configuration.is_ok())
}

#[test]
fn parse_default_values() {
let mut working_dir = current_dir().unwrap();
working_dir.push("tests");
working_dir.push("empty.json");
let content = read_to_string(working_dir).unwrap();

let configuration = serde_json::from_str::<Configuration>(&content);

match configuration {
Ok(configuration) => {
assert_eq!(configuration, Configuration::default());
}
Err(err) => {
panic!("{err}");
}
}
}

#[test]
fn line_width_error() {
let mut working_dir = current_dir().unwrap();
working_dir.push("tests");
working_dir.push("line_width.json");
let content = read_to_string(working_dir).unwrap();

let configuration = serde_json::from_str::<Configuration>(&content);

assert!(configuration.is_err());

if let Err(err) = configuration {
assert!(err
.to_string()
.as_str()
.contains("The line width exceeds the maximum value (320)"),)
}
}
}