Formatter: Show preceding, following and enclosing nodes of comments, Attempt 2 (#6813)

This commit is contained in:
konsti 2023-09-06 12:26:13 +02:00 committed by GitHub
parent e3114a144c
commit 447b7cb0e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 32 deletions

View file

@ -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))