Skip to content

Commit

Permalink
Generate custom JSON schema for dynamic setting
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jan 24, 2024
1 parent 57313d9 commit bdb6e11
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
31 changes: 28 additions & 3 deletions crates/ruff_python_formatter/src/options.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::fmt;
use std::path::Path;
use std::str::FromStr;

use ruff_formatter::printer::{LineEnding, PrinterOptions, SourceMapGeneration};
use ruff_formatter::{FormatOptions, IndentStyle, IndentWidth, LineWidth};
use ruff_macros::CacheKey;
use ruff_python_ast::PySourceType;
use std::fmt;
use std::path::Path;
use std::str::FromStr;

/// Resolved options for formatting one individual file. The difference to `FormatterSettings`
/// is that `FormatterSettings` stores the settings for multiple files (the entire project, a subdirectory, ..)
Expand Down Expand Up @@ -367,15 +368,39 @@ impl fmt::Display for DocstringCode {
#[cfg_attr(feature = "serde", serde(untagged))]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub enum DocstringCodeLineWidth {
/// Wrap docstring code examples at a fixed line width.
Fixed(LineWidth),

/// Respect the line length limit setting for the surrounding Python code.
#[default]
#[cfg_attr(
feature = "serde",
serde(deserialize_with = "deserialize_docstring_code_line_width_dynamic")
)]
#[cfg_attr(feature = "schemars", schemars(with = "DynamicSchema"))]
Dynamic,
}

/// A dummy type that is used to generate a schema for `DocstringCodeLineWidth::Dynamic`.
#[cfg(feature = "schemars")]
struct DynamicSchema;

#[cfg(feature = "schemars")]
impl schemars::JsonSchema for DynamicSchema {
fn schema_name() -> String {
"Dynamic".to_string()
}

fn json_schema(_: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
schemars::schema::SchemaObject {
instance_type: Some(schemars::schema::InstanceType::String.into()),
const_value: Some("dynamic".to_string().into()),
..Default::default()
}
.into()
}
}

impl fmt::Debug for DocstringCodeLineWidth {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Expand Down
18 changes: 16 additions & 2 deletions ruff.schema.json

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

0 comments on commit bdb6e11

Please sign in to comment.