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

Implement tab autocomplete for ruff config #15603

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion crates/ruff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use ruff_workspace::resolver::ConfigurationTransformer;
use rustc_hash::FxHashMap;
use toml;

use crate::commands::completions::config::{OptionString, OptionStringParser};

/// All configuration options that can be passed "globally",
/// i.e., can be passed to all subcommands
#[derive(Debug, Default, Clone, clap::Args)]
Expand Down Expand Up @@ -114,7 +116,11 @@ pub enum Command {
/// List or describe the available configuration options.
Config {
/// Config key to show
option: Option<String>,
#[arg(
value_parser = OptionStringParser,
hide_possible_values = true
)]
option: Option<OptionString>,
/// Output format
#[arg(long, value_enum, default_value = "text")]
output_format: HelpFormat,
Expand Down
158 changes: 158 additions & 0 deletions crates/ruff/src/commands/completions/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
use clap::builder::{PossibleValue, TypedValueParser, ValueParserFactory};
use itertools::Itertools;
use std::str::FromStr;

use ruff_workspace::{
options::Options,
options_base::{OptionField, OptionSet, OptionsMetadata, Visit},
};

#[derive(Default)]
struct CollectOptionsVisitor {
values: Vec<(String, String)>,
parents: Vec<String>,
}

impl IntoIterator for CollectOptionsVisitor {
type Item = (String, String);
type IntoIter = std::vec::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
self.values.into_iter()
}
}

impl Visit for CollectOptionsVisitor {
fn record_set(&mut self, name: &str, group: OptionSet) {
let fully_qualified_name = self
.parents
.iter()
.map(String::as_str)
.chain(std::iter::once(name))
.collect::<Vec<_>>()
.join(".");

// Only add the set to completion list if it has it's own documentation.
self.values.push((
fully_qualified_name,
group.documentation().unwrap_or("").to_owned(),
));

self.parents.push(name.to_owned());
group.record(self);
self.parents.pop();
}

fn record_field(&mut self, name: &str, field: OptionField) {
let fqn = self
.parents
.iter()
.map(String::as_str)
.chain(std::iter::once(name))
.collect::<Vec<_>>()
.join(".");

self.values.push((fqn, field.doc.to_owned()));
}
}

/// Opaque type used solely to enable tab completions
/// for `ruff option [OPTION]` command.
#[derive(Clone, Debug)]
pub struct OptionString(String);

impl From<String> for OptionString {
fn from(s: String) -> Self {
OptionString(s)
}
}

impl From<OptionString> for String {
fn from(value: OptionString) -> Self {
value.0
}
}

impl From<&str> for OptionString {
fn from(s: &str) -> Self {
OptionString(s.to_string())
}
}

impl std::ops::Deref for OptionString {
type Target = str;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl FromStr for OptionString {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
Options::metadata()
.has(s)
.then(|| OptionString(s.to_owned()))
.ok_or(())
}
}

#[derive(Clone)]
pub struct OptionStringParser;

impl ValueParserFactory for OptionString {
type Parser = OptionStringParser;

fn value_parser() -> Self::Parser {
OptionStringParser
}
}

impl TypedValueParser for OptionStringParser {
type Value = OptionString;

fn parse_ref(
&self,
cmd: &clap::Command,
arg: Option<&clap::Arg>,
value: &std::ffi::OsStr,
) -> Result<Self::Value, clap::Error> {
let value = value
.to_str()
.ok_or_else(|| clap::Error::new(clap::error::ErrorKind::InvalidUtf8))?;

value.parse().map_err(|()| {
let mut error = clap::Error::new(clap::error::ErrorKind::ValueValidation).with_cmd(cmd);
if let Some(arg) = arg {
error.insert(
clap::error::ContextKind::InvalidArg,
clap::error::ContextValue::String(arg.to_string()),
);
}
error.insert(
clap::error::ContextKind::InvalidValue,
clap::error::ContextValue::String(value.to_string()),
);
error
})
}

fn possible_values(&self) -> Option<Box<dyn Iterator<Item = PossibleValue> + '_>> {
let mut visitor = CollectOptionsVisitor::default();
Options::metadata().record(&mut visitor);

Some(Box::new(visitor.into_iter().map(|(name, doc)| {
let first_paragraph = doc
.lines()
.take_while(|line| !line.trim_end().is_empty())
// Replace double quotes with single quotes,to avoid clap's lack of escaping
// when creating zsh completions. This has no security implications, as it only
// affects the help string, which is never executed
.map(|s| s.replace('"', "'"))
.join(" ");

PossibleValue::new(name).help(first_paragraph)
})))
}
}
1 change: 1 addition & 0 deletions crates/ruff/src/commands/completions/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(crate) mod config;
2 changes: 2 additions & 0 deletions crates/ruff/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub(crate) mod completions;

pub(crate) mod add_noqa;
pub(crate) mod analyze_graph;
pub(crate) mod check;
Expand Down
Loading