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

Commit

Permalink
feat(npm): allow to set global settings (#3123)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Aug 29, 2022
1 parent ea6bf5b commit 4a4f985
Show file tree
Hide file tree
Showing 28 changed files with 642 additions and 350 deletions.
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ documentation = """
-p rome_console \
-p rome_*parser \
-p rome_rowan
-p rome_bin
--no-deps
"""
bench_parser = "run -p xtask_bench --release -- --feature parser"
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ jobs:
uses: actions/checkout@v3
with:
submodules: false
- name: Install wasm-pack
uses: jetli/[email protected]
with:
version: 'latest'
- name: Cache pnpm modules
uses: actions/cache@v3
with:
Expand Down
13 changes: 4 additions & 9 deletions crates/rome_cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,13 @@ pub(crate) fn check(mut session: CliSession) -> Result<(), Termination> {
};

if let Some(configuration) = configuration {
workspace_settings.merge_with_configuration(configuration)
session
.app
.workspace
.update_settings(UpdateSettingsParams { configuration })?;
}

apply_format_settings_from_cli(&mut session, &mut workspace_settings)?;

session
.app
.workspace
.update_settings(UpdateSettingsParams {
settings: workspace_settings,
})?;

let apply = session.args.contains("--apply");
let apply_suggested = session.args.contains("--apply-suggested");

Expand Down
13 changes: 4 additions & 9 deletions crates/rome_cli/src/commands/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,12 @@ pub(crate) fn ci(mut session: CliSession) -> Result<(), Termination> {
let mut workspace_settings = WorkspaceSettings::default();

if let Some(configuration) = configuration {
workspace_settings.merge_with_configuration(configuration);
session
.app
.workspace
.update_settings(UpdateSettingsParams { configuration })?;
}

apply_format_settings_from_cli(&mut session, &mut workspace_settings)?;

session
.app
.workspace
.update_settings(UpdateSettingsParams {
settings: workspace_settings,
})?;

execute_mode(Execution::new(TraversalMode::CI), session)
}
12 changes: 4 additions & 8 deletions crates/rome_cli/src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ pub(crate) fn format(mut session: CliSession) -> Result<(), Termination> {
let mut workspace_settings = WorkspaceSettings::default();

if let Some(configuration) = configuration {
workspace_settings.merge_with_configuration(configuration);
session
.app
.workspace
.update_settings(UpdateSettingsParams { configuration })?;
}

apply_format_settings_from_cli(&mut session, &mut workspace_settings)?;
Expand Down Expand Up @@ -57,13 +60,6 @@ pub(crate) fn format(mut session: CliSession) -> Result<(), Termination> {
})
};

session
.app
.workspace
.update_settings(UpdateSettingsParams {
settings: workspace_settings,
})?;

execute_mode(execution, session)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rome_js_syntax/src/suppression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn parse_suppression_comment(comment: &str) -> impl Iterator<Item = Suppress
.unwrap_or(comment);
true
}
token => panic!("comment with unknown opening token {token:?}"),
token => panic!("comment with unknown opening token {token:?}, from {comment}"),
};

comment.lines().filter_map(move |line| {
Expand Down
20 changes: 0 additions & 20 deletions crates/rome_lsp/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use rome_service::configuration::Configuration;
use rome_service::settings;
use serde::{Deserialize, Serialize};
use serde_json::{Error, Value};
use tracing::trace;
Expand Down Expand Up @@ -36,22 +34,4 @@ impl Config {
);
Ok(())
}

/// Convert the current configuration to an instance of [settings::WorkspaceSettings]
///
/// If the configuration file is found we use it with its defaults, otherwise
/// we use the settings coming from the client
pub(crate) fn as_workspace_settings(
&self,
configuration: Option<Configuration>,
) -> settings::WorkspaceSettings {
let mut settings = settings::WorkspaceSettings::default();

if let Some(configuration) = configuration {
trace!("Applying configuration coming from the configuration file");
settings.merge_with_configuration(configuration);
}

settings
}
}
21 changes: 11 additions & 10 deletions crates/rome_lsp/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,19 @@ impl Session {
})
.ok()?;
let mut configuration = self.configuration.write().unwrap();

// This operation is intended, we want to consume the configuration because once it's read
// from the LSP, it's not needed anymore
let settings = config.as_workspace_settings(configuration.take());

trace!(
"The LSP will now use the following configuration: \n {:?}",
&settings
);

self.workspace
.update_settings(UpdateSettingsParams { settings })
.ok()?;
if let Some(configuration) = configuration.take() {
trace!(
"The LSP will now use the following configuration: \n {:?}",
&configuration
);

self.workspace
.update_settings(UpdateSettingsParams { configuration })
.ok()?;
}

Some(())
});
Expand Down
80 changes: 45 additions & 35 deletions crates/rome_service/src/configuration/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,64 +16,74 @@ pub struct JavascriptConfiguration {
///
/// If defined here, they should not emit diagnostics.
#[serde(
skip_serializing_if = "IndexSet::is_empty",
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_globals",
serialize_with = "serialize_globals"
)]
pub globals: IndexSet<String>,
pub globals: Option<IndexSet<String>>,
}

pub(crate) fn deserialize_globals<'de, D>(deserializer: D) -> Result<IndexSet<String>, D::Error>
pub(crate) fn deserialize_globals<'de, D>(
deserializer: D,
) -> Result<Option<IndexSet<String>>, D::Error>
where
D: serde::de::Deserializer<'de>,
{
deserializer.deserialize_seq(IndexVisitor::new())
}

struct IndexVisitor {
marker: PhantomData<fn() -> IndexSet<String>>,
}
struct IndexVisitor {
marker: PhantomData<fn() -> Option<IndexSet<String>>>,
}

impl IndexVisitor {
fn new() -> Self {
IndexVisitor {
marker: PhantomData,
impl IndexVisitor {
fn new() -> Self {
IndexVisitor {
marker: PhantomData,
}
}
}
}

impl<'de> Visitor<'de> for IndexVisitor {
type Value = IndexSet<String>;
impl<'de> Visitor<'de> for IndexVisitor {
type Value = Option<IndexSet<String>>;

// Format a message stating what data this Visitor expects to receive.
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("expecting a sequence")
}
// Format a message stating what data this Visitor expects to receive.
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("expecting a sequence")
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut index_set = IndexSet::with_capacity(seq.size_hint().unwrap_or(0));
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut index_set = IndexSet::with_capacity(seq.size_hint().unwrap_or(0));

while let Some(value) = seq.next_element()? {
index_set.insert(value);
}
while let Some(value) = seq.next_element()? {
index_set.insert(value);
}

Ok(index_set)
Ok(Some(index_set))
}
}

deserializer.deserialize_seq(IndexVisitor::new())
}

pub(crate) fn serialize_globals<S>(globals: &IndexSet<String>, s: S) -> Result<S::Ok, S::Error>
pub(crate) fn serialize_globals<S>(
globals: &Option<IndexSet<String>>,
s: S,
) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
let mut sequence = s.serialize_seq(Some(globals.len()))?;
let iter = globals.into_iter();
for global in iter {
sequence.serialize_element(global)?;
if let Some(globals) = globals {
let mut sequence = s.serialize_seq(Some(globals.len()))?;
let iter = globals.into_iter();
for global in iter {
sequence.serialize_element(global)?;
}

sequence.end()
} else {
s.serialize_none()
}
sequence.end()
}

#[derive(Default, Debug, Deserialize, Serialize, Eq, PartialEq)]
Expand Down
8 changes: 3 additions & 5 deletions crates/rome_service/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ impl WorkspaceSettings {
self.linter = LinterSettings::from(linter)
}

let globals = configuration.javascript.map(|j| j.globals);
if let Some(globals) = globals {
self.languages.javascript.globals = globals;
}
let globals = configuration.javascript.and_then(|j| j.globals);
self.languages.javascript.globals = globals;
}

/// It retrieves the severity based on the `code` of the rule and the current configuration.
Expand Down Expand Up @@ -168,7 +166,7 @@ pub struct LanguageSettings<L: Language> {
deserialize_with = "crate::configuration::deserialize_globals",
serialize_with = "crate::configuration::serialize_globals"
)]
pub globals: IndexSet<String>,
pub globals: Option<IndexSet<String>>,
}

/// Handle object holding a temporary lock on the workspace settings until
Expand Down
5 changes: 2 additions & 3 deletions crates/rome_service/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
//! document does not implement the required capability: for instance trying to
//! format a file with a language that does not have a formatter
use crate::settings::WorkspaceSettings;
use crate::RomeError;
use crate::{Configuration, RomeError};
use rome_analyze::ActionCategory;
pub use rome_analyze::RuleCategories;
use rome_diagnostics::{CodeSuggestion, Diagnostic};
Expand Down Expand Up @@ -86,7 +85,7 @@ pub enum FeatureName {
#[derive(serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct UpdateSettingsParams {
pub settings: WorkspaceSettings,
pub configuration: Configuration,
}

#[derive(serde::Serialize, serde::Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_service/src/workspace/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl Workspace for WorkspaceServer {
/// by another thread having previously panicked while holding the lock
fn update_settings(&self, params: UpdateSettingsParams) -> Result<(), RomeError> {
let mut settings = self.settings.write().unwrap();
*settings = params.settings;
settings.merge_with_configuration(params.configuration);
Ok(())
}

Expand Down
Loading

0 comments on commit 4a4f985

Please sign in to comment.