Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: separate project from configuration settings #7053

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
88 changes: 69 additions & 19 deletions crates/uv-dev/src/generate_options_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,29 @@ pub(crate) fn main(args: &Args) -> Result<()> {
Ok(())
}

enum OptionType {
Configuration,
ProjectMetadata,
}

fn generate() -> String {
let mut output = String::new();

generate_set(
&mut output,
Set::Global(CombinedOptions::metadata()),
Set::Global {
set: WorkspaceOptions::metadata(),
option_type: OptionType::ProjectMetadata,
},
&mut Vec::new(),
);

generate_set(
&mut output,
Set::Global {
set: SettingsOptions::metadata(),
option_type: OptionType::Configuration,
},
&mut Vec::new(),
);

Expand All @@ -107,16 +124,20 @@ fn generate() -> String {

fn generate_set(output: &mut String, set: Set, parents: &mut Vec<Set>) {
match &set {
Set::Global(_) => {
output.push_str("## Global\n");
Set::Global { option_type, .. } => {
let header = match option_type {
OptionType::Configuration => "## Configuration\n",
OptionType::ProjectMetadata => "## Project metadata\n",
};
output.push_str(header);
}
Set::Named { name, .. } => {
let title = parents
.iter()
.filter_map(|set| set.name())
.chain(std::iter::once(name.as_str()))
.join(".");
writeln!(output, "## `{title}`\n").unwrap();
writeln!(output, "### `{title}`\n").unwrap();

if let Some(documentation) = set.metadata().documentation() {
output.push_str(documentation);
Expand Down Expand Up @@ -158,28 +179,34 @@ fn generate_set(output: &mut String, set: Set, parents: &mut Vec<Set>) {
}

enum Set {
Global(OptionSet),
Named { name: String, set: OptionSet },
Global {
option_type: OptionType,
set: OptionSet,
},
Named {
name: String,
set: OptionSet,
},
}

impl Set {
fn name(&self) -> Option<&str> {
match self {
Set::Global(_) => None,
Set::Global { .. } => None,
Set::Named { name, .. } => Some(name),
}
}

fn metadata(&self) -> &OptionSet {
match self {
Set::Global(set) => set,
Set::Global { set, .. } => set,
Set::Named { set, .. } => set,
}
}
}

fn emit_field(output: &mut String, name: &str, field: &OptionField, parents: &[Set]) {
let header_level = if parents.is_empty() { "###" } else { "####" };
let header_level = if parents.len() > 1 { "####" } else { "###" };
let parents_anchor = parents.iter().filter_map(|parent| parent.name()).join("_");

if parents_anchor.is_empty() {
Expand Down Expand Up @@ -233,16 +260,35 @@ fn emit_field(output: &mut String, name: &str, field: &OptionField, parents: &[S
}
output.push('\n');
output.push_str("**Example usage**:\n\n");
output.push_str(&format_tab(
"pyproject.toml",
&format_header(field.scope, parents, ConfigurationFile::PyprojectToml),
field.example,
));
output.push_str(&format_tab(
"uv.toml",
&format_header(field.scope, parents, ConfigurationFile::UvToml),
field.example,
));

match parents[0] {
Set::Global {
option_type: OptionType::ProjectMetadata,
..
} => {
output.push_str(&format_code(
"pyproject.toml",
&format_header(field.scope, parents, ConfigurationFile::PyprojectToml),
field.example,
));
}
Set::Global {
option_type: OptionType::Configuration,
..
} => {
output.push_str(&format_tab(
"pyproject.toml",
&format_header(field.scope, parents, ConfigurationFile::PyprojectToml),
field.example,
));
output.push_str(&format_tab(
"uv.toml",
&format_header(field.scope, parents, ConfigurationFile::UvToml),
field.example,
));
}
_ => {}
}
output.push('\n');
}

Expand All @@ -255,6 +301,10 @@ fn format_tab(tab_name: &str, header: &str, content: &str) -> String {
)
}

fn format_code(file_name: &str, header: &str, content: &str) -> String {
format!("```toml title=\"{file_name}\"\n{header}\n{content}\n```\n")
}

/// Format the TOML header for the example usage for a given option.
///
/// For example: `[tool.uv.pip]`.
Expand Down
Loading
Loading