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

ruff autocompletion for --select rules #2808

Closed
spaceone opened this issue Feb 12, 2023 · 8 comments · Fixed by #2906
Closed

ruff autocompletion for --select rules #2808

spaceone opened this issue Feb 12, 2023 · 8 comments · Fixed by #2906
Labels
cli Related to the command-line interface

Comments

@spaceone
Copy link
Contributor

ruff check --select E currently suggests filenames starting with E instead of the rules starting with E.

It would be nice if that was supported for --select, --ignore, etc.

@spaceone
Copy link
Contributor Author

Same for ruff rule E

@charliermarsh charliermarsh added the cli Related to the command-line interface label Feb 12, 2023
@charliermarsh
Copy link
Member

Probably has to do with the fact that we use a custom from_str for these, so they aren't picked up in auto-completion. But maybe there's a way around it.

@not-my-profile
Copy link
Contributor

not-my-profile commented Feb 12, 2023

Yes there's indeed a way around this. I have already implemented this locally but would wait till #2517 is merged since that logic has to change when the RuleCodePrefix enum is split up.

not-my-profile added a commit to not-my-profile/ruff that referenced this issue Feb 14, 2023
For example:

    $ ruff check --select=EM<Tab>
    EM          -- flake8-errmsg
    EM10   EM1  --
    EM101       -- raw-string-in-exception
    EM102       -- f-string-in-exception
    EM103       -- dot-format-in-exception

Fixes astral-sh#2808.
not-my-profile added a commit to not-my-profile/ruff that referenced this issue Feb 14, 2023
For example:

    $ ruff check --select=EM<Tab>
    EM          -- flake8-errmsg
    EM10   EM1  --
    EM101       -- raw-string-in-exception
    EM102       -- f-string-in-exception
    EM103       -- dot-format-in-exception

Fixes astral-sh#2808.
not-my-profile added a commit to not-my-profile/ruff that referenced this issue Feb 14, 2023
For example:

    $ ruff check --select=EM<Tab>
    EM          -- flake8-errmsg
    EM10   EM1  --
    EM101       -- raw-string-in-exception
    EM102       -- f-string-in-exception
    EM103       -- dot-format-in-exception

(You will need to enable autocompletion as described
in the Autocompletion section in the README.)

Fixes astral-sh#2808.
not-my-profile added a commit to not-my-profile/ruff that referenced this issue Feb 14, 2023
For example:

    $ ruff check --select=EM<Tab>
    EM          -- flake8-errmsg
    EM10   EM1  --
    EM101       -- raw-string-in-exception
    EM102       -- f-string-in-exception
    EM103       -- dot-format-in-exception

(You will need to enable autocompletion as described
in the Autocompletion section in the README.)

Fixes astral-sh#2808.
not-my-profile added a commit to not-my-profile/ruff that referenced this issue Feb 14, 2023
For example:

    $ ruff check --select=EM<Tab>
    EM          -- flake8-errmsg
    EM10   EM1  --
    EM101       -- raw-string-in-exception
    EM102       -- f-string-in-exception
    EM103       -- dot-format-in-exception

(You will need to enable autocompletion as described
in the Autocompletion section in the README.)

Fixes astral-sh#2808.
not-my-profile added a commit to not-my-profile/ruff that referenced this issue Feb 15, 2023
For example:

    $ ruff check --select=EM<Tab>
    EM          -- flake8-errmsg
    EM10   EM1  --
    EM101       -- raw-string-in-exception
    EM102       -- f-string-in-exception
    EM103       -- dot-format-in-exception

(You will need to enable autocompletion as described
 in the Autocompletion section in the README.)

Fixes astral-sh#2808.

(The --help help change in the README is due to a clap bug,
 for which I already submitted a fix:
 clap-rs/clap#4710.)
charliermarsh pushed a commit that referenced this issue Feb 15, 2023
For example:

    $ ruff check --select=EM<Tab>
    EM          -- flake8-errmsg
    EM10   EM1  --
    EM101       -- raw-string-in-exception
    EM102       -- f-string-in-exception
    EM103       -- dot-format-in-exception

(You will need to enable autocompletion as described
 in the Autocompletion section in the README.)

Fixes #2808.

(The --help help change in the README is due to a clap bug,
 for which I already submitted a fix:
 clap-rs/clap#4710.)
@spaceone
Copy link
Contributor Author

Same for ruff rule E
@not-my-profile can you do this as well?

I tried too long for myself but failed with:

diff --git crates/ruff_cli/src/args.rs crates/ruff_cli/src/args.rs
index 445534c7..e69bb043 100644                                                                                                                                             
--- crates/ruff_cli/src/args.rs  
+++ crates/ruff_cli/src/args.rs          
@@ -38,7 +38,7 @@ pub enum Command {                                                                                                                                        
     #[clap(alias = "--explain")]                                                                                                            
     Rule {                                                                                                                                                                 
         #[arg(value_parser=Rule::from_code)]                                                                                                                               
-        rule: Rule,                                                     
+        rule: RuleSelector,                                  
                                                    
         /// Output format                                    
         #[arg(long, value_enum, default_value = "pretty")]          
diff --git crates/ruff_cli/src/main.rs crates/ruff_cli/src/main.rs                                                                                                           
index cf806cde..ea3af2b4 100644          
--- crates/ruff_cli/src/main.rs     
+++ crates/ruff_cli/src/main.rs                                                                                                                                             
@@ -8,6 +8,7 @@ use clap::{CommandFactory, Parser, Subcommand};           
 use colored::Colorize;                       
 use notify::{recommended_watcher, RecursiveMode, Watcher};       
                               
+use ruff::registry::Rule;                          
 use ::ruff::logging::{set_up_logging, LogLevel};             
 use ::ruff::resolver::PyprojectDiscovery;
 use ::ruff::settings::types::SerializationFormat;                                                                                                                           
@@ -107,7 +108,7 @@ quoting the executed command, along with the relevant file contents and `pyproje
     set_up_logging(&log_level)?;            
                                                                             
     match command {        
-        Command::Rule { rule, format } => commands::rule::rule(&rule, format)?,
+        Command::Rule { rule, format } => commands::rule::rule(&Rule::from_code(rule.prefix_and_code().1).unwrap(), format)?,
         Command::Config { option } => return Ok(commands::config::config(option.as_deref())),                                                                              
         Command::Linter { format } => commands::linter::linter(format)?,
         Command::Clean => commands::clean::clean(log_level)?,

Also how can I enable that it displays the short-code like in the PR?:

$ ruff check --select=EM<Tab>
EM          -- flake8-errmsg
EM10   EM1  --
EM101       -- raw-string-in-exception
EM102       -- f-string-in-exception
EM103       -- dot-format-in-exception

for me it displays only the raw codes.

@not-my-profile
Copy link
Contributor

can you do this as well?

I am currently busy with other stuff.

how can I enable that it displays the short-code like in the PR?

I don't know I am using Zsh and this just worked ... are you using a different shell?

@spaceone
Copy link
Contributor Author

I am using bash.

@not-my-profile
Copy link
Contributor

Ah ok yeah ... sorry it appears that clap_complete doesn't include the value help text for bash ... perhaps because bash doesn't support it?

$ ruff generate-shell-completion zsh | grep banned-api
TID251\:"banned-api"
TID251\:"banned-api"
TID251\:"banned-api"
TID251\:"banned-api"
TID251\:"banned-api"
TID251\:"banned-api"
$ ruff generate-shell-completion bash | grep banned-api
# nothing

I can recommend zsh ... it does in general have much better autocompletion than bash.

@spaceone
Copy link
Contributor Author

ok, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli Related to the command-line interface
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants