-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Abstract stylist to libcst style conversion #4749
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. I don't know if it was intentional that the ast
crate doesn't depend on the cst
crate.
PR Check ResultsEcosystem✅ ecosystem check detected no changes. BenchmarkLinux
Windows
|
i already though about making this a free standing function in the ruff crate otherwise, @charliermarsh any preferences? |
It wasn't intentional but it is a heavy dependency. Should we make it a feature? |
Follow-up/alternative to #4749 we can either merge into it or discard.
Current dependencies on/for this PR:
This comment was auto-generated by Graphite. |
crates/ruff/src/autofix/codemods.rs
Outdated
/// Given an import statement, remove any imports that are specified in the `imports` iterator. | ||
/// | ||
/// Returns `Ok(None)` if the statement is empty after removing the imports. | ||
pub(crate) fn remove_imports<'a>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this just need a rebase? Why was this deleted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think the rebase went wrong, i have to fix that
Found a better solution that abstracts away more code |
Looks reasonable to me. |
crates/ruff/src/autofix/codemods.rs
Outdated
@@ -11,6 +11,23 @@ use ruff_python_ast::source_code::{Locator, Stylist}; | |||
use crate::cst::helpers::compose_module_path; | |||
use crate::cst::matchers::match_statement; | |||
|
|||
/// Glue code to make libcst codegen work with ruff's Stylist | |||
pub(crate) trait CodegenStylist<'a>: Codegen<'a> { | |||
fn codegen_stylist(&self, stylist: &'a Stylist<'a>) -> String; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be sufficient
fn codegen_stylist(&self, stylist: &'a Stylist<'a>) -> String; | |
fn codegen_stylist(&self, stylist: &'a Stylist) -> String; |
Replace all duplicate invocations of ```rust let mut state = CodegenState { default_newline: &stylist.line_ending(), default_indent: stylist.indentation(), ..CodegenState::default() } tree.codegen(&mut state); state.to_string() ``` with ```rust tree.codegen_stylist(&stylist); ``` No functional changes.
* Abstract codegen with stylist into a CodegenStylist trait Replace all duplicate invocations of ```rust let mut state = CodegenState { default_newline: &stylist.line_ending(), default_indent: stylist.indentation(), ..CodegenState::default() } tree.codegen(&mut state); state.to_string() ``` with ```rust tree.codegen_stylist(&stylist); ``` No functional changes.
Replace all duplicate invocations of
with
No functional changes.