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

Commit

Permalink
chore(rome_service): remove constraints from settings (#3157)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Sep 6, 2022
1 parent 43fd932 commit 96ce01c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 56 deletions.
6 changes: 3 additions & 3 deletions crates/rome_cli/src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ pub(crate) fn apply_format_settings_from_cli(

match indent_style {
Some(IndentStyle::Tab) => {
workspace_settings.format.indent_style = Some(IndentStyle::Tab);
workspace_settings.formatter.indent_style = Some(IndentStyle::Tab);
}
Some(IndentStyle::Space(default_size)) => {
workspace_settings.format.indent_style =
workspace_settings.formatter.indent_style =
Some(IndentStyle::Space(size.unwrap_or(default_size)));
}
None => {}
Expand Down Expand Up @@ -133,7 +133,7 @@ pub(crate) fn apply_format_settings_from_cli(
})?;

if let Some(line_width) = line_width {
workspace_settings.format.line_width = Some(line_width);
workspace_settings.formatter.line_width = Some(line_width);
}

Ok(())
Expand Down
1 change: 0 additions & 1 deletion crates/rome_service/src/configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ mod formatter;
mod javascript;
pub mod linter;

pub(crate) use javascript::{deserialize_globals, serialize_globals};
pub use linter::{RuleConfiguration, Rules};

/// The configuration that is contained inside the file `rome.json`
Expand Down
65 changes: 22 additions & 43 deletions crates/rome_service/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,32 @@ use rome_js_syntax::JsLanguage;
use std::sync::{RwLock, RwLockReadGuard};

/// Global settings for the entire workspace
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[derive(Debug, Default)]
pub struct WorkspaceSettings {
/// Formatter settings applied to all files in the workspaces
#[serde(default)]
pub format: FormatSettings,
pub formatter: FormatSettings,
/// Linter settings applied to all files in the workspace
#[serde(default)]
pub linter: LinterSettings,
/// Language specific settings
#[serde(default)]
pub languages: LanguagesSettings,
}

impl WorkspaceSettings {
/// Retrieves the settings of the formatter
pub fn formatter(&self) -> &FormatSettings {
&self.formatter
}

/// Retrieves the settings of the linter
pub fn linter(&self) -> &LinterSettings {
&self.linter
}

/// The (configuration)[Configuration] is merged into the workspace
pub fn merge_with_configuration(&mut self, configuration: Configuration) {
// formatter part
if let Some(formatter) = configuration.formatter {
self.format = FormatSettings::from(formatter);
self.formatter = FormatSettings::from(formatter);
}
let formatter = configuration
.javascript
Expand Down Expand Up @@ -62,8 +68,7 @@ impl WorkspaceSettings {
}

/// Formatter settings for the entire workspace
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[derive(Debug)]
pub struct FormatSettings {
/// Enabled by default
pub enabled: bool,
Expand All @@ -86,8 +91,7 @@ impl Default for FormatSettings {
}

/// Linter settings for the entire workspace
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[derive(Debug)]
pub struct LinterSettings {
/// Enabled by default
pub enabled: bool,
Expand All @@ -106,33 +110,16 @@ impl Default for LinterSettings {
}

/// Static map of language names to language-specific settings
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[derive(Debug, Default)]
pub struct LanguagesSettings {
#[serde(default)]
pub javascript: LanguageSettings<JsLanguage>,
}

pub trait Language: rome_rowan::Language {
#[cfg(not(feature = "schemars"))]
/// Formatter settings type for this language
type FormatSettings: Default + serde::Serialize + serde::de::DeserializeOwned;
#[cfg(feature = "schemars")]
/// Formatter settings type for this language
type FormatSettings: Default
+ serde::Serialize
+ serde::de::DeserializeOwned
+ schemars::JsonSchema;

#[cfg(not(feature = "schemars"))]
/// Linter settings type for this language
type LinterSettings: Default + serde::Serialize + serde::de::DeserializeOwned;
#[cfg(feature = "schemars")]
/// Linter settings type for this language
type LinterSettings: Default
+ serde::Serialize
+ serde::de::DeserializeOwned
+ schemars::JsonSchema;
type FormatSettings: Default;

type LinterSettings: Default;

/// Fully resolved formatter options type for this language
type FormatOptions: rome_formatter::FormatOptions;
Expand All @@ -149,30 +136,22 @@ pub trait Language: rome_rowan::Language {
) -> Self::FormatOptions;
}

#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[derive(Debug, Default)]
pub struct LanguageSettings<L: Language> {
/// Formatter settings for this language
#[serde(default)]
pub format: L::FormatSettings,

/// Linter settings for this language
#[serde(default)]
pub linter: L::LinterSettings,

/// Globals variables/bindings that can be found in a file
#[serde(
default,
deserialize_with = "crate::configuration::deserialize_globals",
serialize_with = "crate::configuration::serialize_globals"
)]
pub globals: Option<IndexSet<String>>,
}

/// Handle object holding a temporary lock on the workspace settings until
/// the deferred language-specific options resolution is called
#[derive(Debug)]
pub(crate) struct SettingsHandle<'a> {
pub struct SettingsHandle<'a> {
inner: RwLockReadGuard<'a, WorkspaceSettings>,
}

Expand All @@ -197,7 +176,7 @@ impl<'a> SettingsHandle<'a> {
L: Language,
{
L::resolve_format_options(
&self.inner.format,
&self.inner.formatter,
&L::lookup_settings(&self.inner.languages).format,
path,
)
Expand Down
18 changes: 9 additions & 9 deletions crates/rome_service/src/workspace/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ impl Workspace for WorkspaceServer {
let settings = self.settings.read().unwrap();
match params.feature {
FeatureName::Format => {
capabilities.formatter.format.is_some() && settings.format.enabled
capabilities.formatter.format.is_some() && settings.formatter().enabled
}
FeatureName::Lint => capabilities.analyzer.lint.is_some() && settings.linter.enabled,
FeatureName::Lint => capabilities.analyzer.lint.is_some() && settings.linter().enabled,
}
}

Expand Down Expand Up @@ -240,7 +240,7 @@ impl Workspace for WorkspaceServer {
let parse = self.get_parse(params.path.clone())?;
let settings = self.settings();

if !settings.as_ref().format.format_with_errors && parse.has_errors() {
if !settings.as_ref().formatter().format_with_errors && parse.has_errors() {
return Err(RomeError::FormatWithErrorsDisabled);
}

Expand Down Expand Up @@ -285,7 +285,7 @@ impl Workspace for WorkspaceServer {

let parse = self.get_parse(params.path.clone())?;
let settings = self.settings.read().unwrap();
let rules = settings.linter.rules.as_ref();
let rules = settings.linter().rules.as_ref();
let enabled_rules: Option<Vec<RuleFilter>> = if let Some(rules) = rules {
let enabled: IndexSet<RuleFilter> = rules.as_enabled_rules();
Some(enabled.into_iter().collect())
Expand Down Expand Up @@ -316,7 +316,7 @@ impl Workspace for WorkspaceServer {
let parse = self.get_parse(params.path.clone())?;

let settings = self.settings.read().unwrap();
let rules = settings.linter.rules.as_ref();
let rules = settings.linter().rules.as_ref();

Ok(code_actions(&params.path, parse, params.range, rules))
}
Expand All @@ -333,7 +333,7 @@ impl Workspace for WorkspaceServer {
let parse = self.get_parse(params.path.clone())?;
let settings = self.settings();

if !settings.as_ref().format.format_with_errors && parse.has_errors() {
if !settings.as_ref().formatter().format_with_errors && parse.has_errors() {
return Err(RomeError::FormatWithErrorsDisabled);
}

Expand All @@ -350,7 +350,7 @@ impl Workspace for WorkspaceServer {
let parse = self.get_parse(params.path.clone())?;
let settings = self.settings();

if !settings.as_ref().format.format_with_errors && parse.has_errors() {
if !settings.as_ref().formatter().format_with_errors && parse.has_errors() {
return Err(RomeError::FormatWithErrorsDisabled);
}

Expand All @@ -367,7 +367,7 @@ impl Workspace for WorkspaceServer {
let parse = self.get_parse(params.path.clone())?;
let settings = self.settings();

if !settings.as_ref().format.format_with_errors && parse.has_errors() {
if !settings.as_ref().formatter().format_with_errors && parse.has_errors() {
return Err(RomeError::FormatWithErrorsDisabled);
}

Expand All @@ -383,7 +383,7 @@ impl Workspace for WorkspaceServer {

let parse = self.get_parse(params.path.clone())?;
let settings = self.settings.read().unwrap();
let rules = settings.linter.rules.as_ref();
let rules = settings.linter().rules.as_ref();
fix_all(FixAllParams {
rome_path: &params.path,
parse,
Expand Down

0 comments on commit 96ce01c

Please sign in to comment.