Skip to content

Commit

Permalink
refactor(assists): add action options
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Oct 29, 2024
1 parent 92286ab commit 7b5fbbf
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 73 deletions.
54 changes: 40 additions & 14 deletions crates/biome_configuration/src/analyzer/assists/actions.rs

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

118 changes: 95 additions & 23 deletions crates/biome_configuration/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub enum RuleConfiguration<T: Default> {
Plain(RulePlainConfiguration),
WithOptions(RuleWithOptions<T>),
}

impl<T: Default + Deserializable> Deserializable for RuleConfiguration<T> {
fn deserialize(
value: &impl DeserializableValue,
Expand All @@ -35,7 +34,6 @@ impl<T: Default + Deserializable> Deserializable for RuleConfiguration<T> {
}
}
}

impl<T: Default> RuleConfiguration<T> {
pub fn is_disabled(&self) -> bool {
matches!(self.level(), RulePlainConfiguration::Off)
Expand All @@ -56,7 +54,6 @@ impl<T: Default> RuleConfiguration<T> {
}
}
}

// Rule configuration has a custom [Merge] implementation so that overriding the
// severity doesn't override the options.
impl<T: Clone + Default> Merge for RuleConfiguration<T> {
Expand All @@ -74,7 +71,6 @@ impl<T: Clone + Default> Merge for RuleConfiguration<T> {
}
}
}

impl<T: Clone + Default + 'static> RuleConfiguration<T> {
pub fn get_options(&self) -> Option<RuleOptions> {
match self {
Expand All @@ -83,7 +79,6 @@ impl<T: Clone + Default + 'static> RuleConfiguration<T> {
}
}
}

impl<T: Default> Default for RuleConfiguration<T> {
fn default() -> Self {
Self::Plain(RulePlainConfiguration::Error)
Expand All @@ -97,13 +92,11 @@ pub enum RuleFixConfiguration<T: Default> {
Plain(RulePlainConfiguration),
WithOptions(RuleWithFixOptions<T>),
}

impl<T: Default> Default for RuleFixConfiguration<T> {
fn default() -> Self {
Self::Plain(RulePlainConfiguration::Error)
}
}

impl<T: Default + Deserializable> Deserializable for RuleFixConfiguration<T> {
fn deserialize(
value: &impl DeserializableValue,
Expand All @@ -118,7 +111,6 @@ impl<T: Default + Deserializable> Deserializable for RuleFixConfiguration<T> {
}
}
}

impl<T: Default> RuleFixConfiguration<T> {
pub fn is_disabled(&self) -> bool {
matches!(self.level(), RulePlainConfiguration::Off)
Expand All @@ -139,7 +131,6 @@ impl<T: Default> RuleFixConfiguration<T> {
}
}
}

// Rule configuration has a custom [Merge] implementation so that overriding the
// severity doesn't override the options.
impl<T: Clone + Default> Merge for RuleFixConfiguration<T> {
Expand All @@ -157,7 +148,6 @@ impl<T: Clone + Default> Merge for RuleFixConfiguration<T> {
}
}
}

impl<T: Clone + Default + 'static> RuleFixConfiguration<T> {
pub fn get_options(&self) -> Option<RuleOptions> {
match self {
Expand All @@ -168,7 +158,6 @@ impl<T: Clone + Default + 'static> RuleFixConfiguration<T> {
}
}
}

impl<T: Default> From<&RuleConfiguration<T>> for Severity {
fn from(conf: &RuleConfiguration<T>) -> Self {
match conf {
Expand All @@ -180,7 +169,6 @@ impl<T: Default> From<&RuleConfiguration<T>> for Severity {
}
}
}

impl From<RulePlainConfiguration> for Severity {
fn from(conf: RulePlainConfiguration) -> Self {
match conf {
Expand All @@ -193,12 +181,11 @@ impl From<RulePlainConfiguration> for Severity {
}
}
}

impl From<RuleAssistConfiguration> for Severity {
fn from(conf: RuleAssistConfiguration) -> Self {
impl From<RuleAssistPlainConfiguration> for Severity {
fn from(conf: RuleAssistPlainConfiguration) -> Self {
match conf {
RuleAssistConfiguration::On => Severity::Hint,
RuleAssistConfiguration::Off => {
RuleAssistPlainConfiguration::On => Severity::Hint,
RuleAssistPlainConfiguration::Off => {
unreachable!("the rule is turned off, it should not step in here")
}
}
Expand All @@ -216,16 +203,87 @@ pub enum RulePlainConfiguration {
Off,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields, untagged)]
pub enum RuleAssistConfiguration<T: Default> {
Plain(RuleAssistPlainConfiguration),
WithOptions(RuleAssistWithOptions<T>),
}
impl<T: Default + Deserializable> Deserializable for RuleAssistConfiguration<T> {
fn deserialize(
value: &impl DeserializableValue,
rule_name: &str,
diagnostics: &mut Vec<DeserializationDiagnostic>,
) -> Option<Self> {
if value.visitable_type()? == DeserializableType::Str {
Deserializable::deserialize(value, rule_name, diagnostics).map(Self::Plain)
} else {
Deserializable::deserialize(value, rule_name, diagnostics)
.map(|rule| Self::WithOptions(rule))
}
}
}
impl<T: Default> RuleAssistConfiguration<T> {
pub fn is_disabled(&self) -> bool {
matches!(self.level(), RuleAssistPlainConfiguration::Off)
}
pub fn is_enabled(&self) -> bool {
!self.is_disabled()
}
pub fn level(&self) -> RuleAssistPlainConfiguration {
match self {
Self::Plain(plain) => *plain,
Self::WithOptions(options) => options.level,
}
}
pub fn set_level(&mut self, level: RuleAssistPlainConfiguration) {
match self {
Self::Plain(plain) => *plain = level,
Self::WithOptions(options) => options.level = level,
}
}
}
// Rule configuration has a custom [Merge] implementation so that overriding the
// severity doesn't override the options.
impl<T: Clone + Default> Merge for RuleAssistConfiguration<T> {
fn merge_with(&mut self, other: Self) {
match self {
Self::Plain(_) => *self = other,
Self::WithOptions(this) => match other {
Self::Plain(level) => {
this.level = level;
}
Self::WithOptions(other) => {
this.merge_with(other);
}
},
}
}
}
impl<T: Clone + Default + 'static> RuleAssistConfiguration<T> {
pub fn get_options(&self) -> Option<RuleOptions> {
match self {
Self::Plain(_) => None,
Self::WithOptions(options) => Some(RuleOptions::new(options.options.clone(), None)),
}
}
}
impl<T: Default> Default for RuleAssistConfiguration<T> {
fn default() -> Self {
Self::Plain(RuleAssistPlainConfiguration::Off)
}
}

#[derive(Clone, Copy, Debug, Default, Deserialize, Deserializable, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(rename_all = "camelCase")]
pub enum RuleAssistConfiguration {
pub enum RuleAssistPlainConfiguration {
#[default]
On,
Off,
}

impl RuleAssistConfiguration {
impl RuleAssistPlainConfiguration {
pub const fn is_enabled(&self) -> bool {
matches!(self, Self::On)
}
Expand All @@ -234,13 +292,28 @@ impl RuleAssistConfiguration {
matches!(self, Self::Off)
}
}

impl Merge for RuleAssistConfiguration {
impl Merge for RuleAssistPlainConfiguration {
fn merge_with(&mut self, other: Self) {
*self = other;
}
}

#[derive(Clone, Debug, Default, Deserialize, Deserializable, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct RuleAssistWithOptions<T: Default> {
/// The severity of the emitted diagnostics by the rule
pub level: RuleAssistPlainConfiguration,
/// Rule's options
pub options: T,
}
impl<T: Default> Merge for RuleAssistWithOptions<T> {
fn merge_with(&mut self, other: Self) {
self.level = other.level;
self.options = other.options;
}
}

#[derive(Clone, Debug, Default, Deserialize, Deserializable, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
Expand All @@ -250,7 +323,6 @@ pub struct RuleWithOptions<T: Default> {
/// Rule's options
pub options: T,
}

impl<T: Default> Merge for RuleWithOptions<T> {
fn merge_with(&mut self, other: Self) {
self.level = other.level;
Expand Down
15 changes: 12 additions & 3 deletions crates/biome_configuration/src/generated/assists.rs

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

Loading

0 comments on commit 7b5fbbf

Please sign in to comment.