mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-14 07:38:21 +00:00
Formatter: Show preceding, following and enclosing nodes of comments, Attempt 2 (#6813)
This commit is contained in:
parent
e3114a144c
commit
447b7cb0e2
3 changed files with 90 additions and 32 deletions
|
@ -9,7 +9,9 @@ use ruff_formatter::SourceCode;
|
|||
use ruff_python_index::CommentRangesBuilder;
|
||||
use ruff_python_parser::lexer::lex;
|
||||
use ruff_python_parser::{parse_tokens, Mode};
|
||||
use ruff_text_size::Ranged;
|
||||
|
||||
use crate::comments::collect_comments;
|
||||
use crate::{format_node, PyFormatOptions};
|
||||
|
||||
#[derive(ValueEnum, Clone, Debug)]
|
||||
|
@ -64,6 +66,26 @@ pub fn format_and_debug_print(input: &str, cli: &Cli, source_type: &Path) -> Res
|
|||
println!("{}", formatted.document().display(SourceCode::new(input)));
|
||||
}
|
||||
if cli.print_comments {
|
||||
// Print preceding, following and enclosing nodes
|
||||
let source_code = SourceCode::new(input);
|
||||
let decorated_comments = collect_comments(&python_ast, source_code, &comment_ranges);
|
||||
for comment in decorated_comments {
|
||||
println!(
|
||||
"{:?} {:?} {:?} {:?} {:?}",
|
||||
comment.slice().range(),
|
||||
comment
|
||||
.preceding_node()
|
||||
.map(|node| (node.kind(), node.range())),
|
||||
comment
|
||||
.following_node()
|
||||
.map(|node| (node.kind(), node.range())),
|
||||
(
|
||||
comment.enclosing_node().kind(),
|
||||
comment.enclosing_node().range()
|
||||
),
|
||||
comment.slice().text(SourceCode::new(input)),
|
||||
);
|
||||
}
|
||||
println!(
|
||||
"{:#?}",
|
||||
formatted.context().comments().debug(SourceCode::new(input))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue