forked from astral-sh/ruff
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement tab autocomplete for ruff config. fixes astral-sh#4551
- Loading branch information
Showing
6 changed files
with
174 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
#[cfg(feature = "clap")] | ||
pub mod clap_completion { | ||
use clap::builder::{PossibleValue, TypedValueParser, ValueParserFactory}; | ||
use std::str::FromStr; | ||
|
||
use crate::{ | ||
options::Options, | ||
options_base::{OptionField, OptionSet, OptionsMetadata, Visit}, | ||
}; | ||
|
||
#[derive(Default)] | ||
struct CollectOptionsVisitor { | ||
values: Vec<(String, String)>, | ||
parents: Vec<String>, | ||
} | ||
|
||
impl IntoIterator for CollectOptionsVisitor { | ||
type IntoIter = std::vec::IntoIter<(String, String)>; | ||
type Item = (String, String); | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
self.values.into_iter() | ||
} | ||
} | ||
|
||
impl Visit for CollectOptionsVisitor { | ||
fn record_set(&mut self, name: &str, group: OptionSet) { | ||
let fqn = self | ||
.parents | ||
.iter() | ||
.map(String::as_str) | ||
.chain(std::iter::once(name)) | ||
.collect::<Vec<_>>() | ||
.join("."); | ||
|
||
self.values | ||
.push((fqn, group.documentation().unwrap_or_default().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 for option strings on the command line. | ||
#[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(o: OptionString) -> Self { | ||
o.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_line = doc | ||
.lines() | ||
.next() | ||
.unwrap_or("") | ||
.chars() | ||
.take(80) | ||
.collect::<String>(); | ||
|
||
PossibleValue::new(name).help(first_line) | ||
}))) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#[cfg(feature = "clap")] | ||
pub mod cli; | ||
pub mod configuration; | ||
pub mod options; | ||
pub mod pyproject; | ||
|