From 4244ad5f26264417b4228c5b4ee32466bcd735ce Mon Sep 17 00:00:00 2001 From: konstin Date: Thu, 7 Sep 2023 19:24:56 +0200 Subject: [PATCH] Show header for formatter comment decoration info **Summary** Show a header in the formatter comment decoration debug output that shows which node is preceding/following/enclosing (https://github.com/astral-sh/ruff/pull/6813#issuecomment-1708119550). I also kept this intentionally condensed to make it easy to use this is a small sidebar without vertical scrolling. ``` # Comment decoration: Range Preceding Following Enclosing Comment 1292..1298 Some((ExprName, 1274..1280)) Some((ExprName, 1307..1310)) (StmtAnnAssign, 1274..1316) "# type" 2507..2555 None Some((StmtIf, 2634..2891)) (StmtClassDef, 2478..3279) "# didn't match the name in the C implementation," 2560..2629 None Some((StmtIf, 2634..2891)) (StmtClassDef, 2478..3279) "# meaning it is only *safe* to pass it as a keyword argument on 3.12+" 3281..3298 Some((StmtClassDef, 2478..3279)) Some((StmtFunctionDef, 3300..3317)) (ModModule, 0..4162) "# Top level rules" 3481..3496 Some((StmtClassDef, 3452..3479)) Some((StmtClassDef, 3498..3876)) (ModModule, 0..4162) "# Nesting rules" { Node { kind: ExprName, range: 1307..1310, source: `int`, }: { ... ``` **Test Plan** It's debug output. --- crates/ruff_python_formatter/src/cli.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/ruff_python_formatter/src/cli.rs b/crates/ruff_python_formatter/src/cli.rs index b1c1d2b1d25e8..161e6c27a0145 100644 --- a/crates/ruff_python_formatter/src/cli.rs +++ b/crates/ruff_python_formatter/src/cli.rs @@ -69,6 +69,9 @@ pub fn format_and_debug_print(input: &str, cli: &Cli, source_type: &Path) -> Res // Print preceding, following and enclosing nodes let source_code = SourceCode::new(input); let decorated_comments = collect_comments(&python_ast, source_code, &comment_ranges); + if !decorated_comments.is_empty() { + println!("# Comment decoration: Range Preceding Following Enclosing Comment"); + } for comment in decorated_comments { println!( "{:?} {:?} {:?} {:?} {:?}",