Generic "comment to node" association logic (#4642)

This commit is contained in:
Micha Reiser 2023-05-30 11:28:01 +02:00 committed by GitHub
parent 84a5584888
commit 0cd453bdf0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 1574 additions and 65 deletions

View file

@ -1,16 +1,17 @@
---
source: crates/ruff_python_formatter/src/comments/debug.rs
expression: formatted
expression: comments.debug(source_code)
---
{
StmtContinue(
StmtContinue {
range: 0..0,
},
): {
Node {
kind: StmtContinue,
range: 0..0,
source: ``,
}: {
"leading": [
SourceComment {
text: "# leading comment",
position: OwnLine,
formatted: false,
},
],
@ -18,18 +19,20 @@ expression: formatted
"trailing": [
SourceComment {
text: "# trailing",
position: EndOfLine,
formatted: false,
},
],
},
StmtBreak(
StmtBreak {
range: 0..0,
},
): {
Node {
kind: StmtBreak,
range: 0..0,
source: ``,
}: {
"leading": [
SourceComment {
text: "# break leading",
position: OwnLine,
formatted: false,
},
],

View file

@ -0,0 +1,66 @@
---
source: crates/ruff_python_formatter/src/comments/mod.rs
expression: comments.debug(test_case.source_code)
---
{
Node {
kind: StmtFunctionDef,
range: 28..212,
source: `def test(x, y):⏎`,
}: {
"leading": [
SourceComment {
text: "# Function Leading comment",
position: OwnLine,
formatted: false,
},
],
"dangling": [],
"trailing": [],
},
Node {
kind: ExprCompare,
range: 51..57,
source: `x == y`,
}: {
"leading": [],
"dangling": [],
"trailing": [
SourceComment {
text: "# if statement end of line comment",
position: EndOfLine,
formatted: false,
},
],
},
Node {
kind: StmtIf,
range: 144..212,
source: `elif x < y:⏎`,
}: {
"leading": [
SourceComment {
text: "# Leading comment",
position: OwnLine,
formatted: false,
},
],
"dangling": [],
"trailing": [],
},
Node {
kind: StmtExpr,
range: 234..246,
source: `test(10, 20)`,
}: {
"leading": [
SourceComment {
text: "# own line comment",
position: OwnLine,
formatted: false,
},
],
"dangling": [],
"trailing": [],
},
}

View file

@ -0,0 +1,21 @@
---
source: crates/ruff_python_formatter/src/comments/mod.rs
expression: comments.debug(test_case.source_code)
---
{
Node {
kind: Arguments,
range: 9..39,
source: `(⏎`,
}: {
"leading": [],
"dangling": [
SourceComment {
text: "# Some comment",
position: OwnLine,
formatted: false,
},
],
"trailing": [],
},
}

View file

@ -0,0 +1,5 @@
---
source: crates/ruff_python_formatter/src/comments/mod.rs
expression: comments.debug(test_case.source_code)
---
{}

View file

@ -0,0 +1,21 @@
---
source: crates/ruff_python_formatter/src/comments/mod.rs
expression: comments.debug(test_case.source_code)
---
{
Node {
kind: StmtExpr,
range: 19..20,
source: `x`,
}: {
"leading": [
SourceComment {
text: "# leading comment",
position: OwnLine,
formatted: false,
},
],
"dangling": [],
"trailing": [],
},
}

View file

@ -0,0 +1,26 @@
---
source: crates/ruff_python_formatter/src/comments/mod.rs
expression: comments.debug(test_case.source_code)
---
{
Node {
kind: ModModule,
range: 0..0,
source: ``,
}: {
"leading": [],
"dangling": [
SourceComment {
text: "# Some comment",
position: OwnLine,
formatted: false,
},
SourceComment {
text: "# another comment",
position: OwnLine,
formatted: false,
},
],
"trailing": [],
},
}

View file

@ -0,0 +1,36 @@
---
source: crates/ruff_python_formatter/src/comments/mod.rs
expression: comments.debug(test_case.source_code)
---
{
Node {
kind: ExprName,
range: 1..2,
source: `a`,
}: {
"leading": [],
"dangling": [],
"trailing": [
SourceComment {
text: "# Trailing comment",
position: EndOfLine,
formatted: false,
},
],
},
Node {
kind: ExprConstant,
range: 30..32,
source: `10`,
}: {
"leading": [],
"dangling": [],
"trailing": [
SourceComment {
text: "# More comments",
position: EndOfLine,
formatted: false,
},
],
},
}

View file

@ -0,0 +1,21 @@
---
source: crates/ruff_python_formatter/src/comments/mod.rs
expression: comments.debug(test_case.source_code)
---
{
Node {
kind: ExprName,
range: 6..7,
source: `a`,
}: {
"leading": [],
"dangling": [],
"trailing": [
SourceComment {
text: "# comment",
position: OwnLine,
formatted: false,
},
],
},
}

View file

@ -0,0 +1,21 @@
---
source: crates/ruff_python_formatter/src/comments/mod.rs
expression: comments.debug(test_case.source_code)
---
{
Node {
kind: Arg,
range: 15..16,
source: `a`,
}: {
"leading": [],
"dangling": [],
"trailing": [
SourceComment {
text: "# Trailing comment for argument `a`",
position: EndOfLine,
formatted: false,
},
],
},
}

View file

@ -0,0 +1,21 @@
---
source: crates/ruff_python_formatter/src/comments/mod.rs
expression: comments.debug(test_case.source_code)
---
{
Node {
kind: StmtExpr,
range: 143..155,
source: `test(10, 20)`,
}: {
"leading": [
SourceComment {
text: "# Trailing comment",
position: OwnLine,
formatted: false,
},
],
"dangling": [],
"trailing": [],
},
}

View file

@ -0,0 +1,36 @@
---
source: crates/ruff_python_formatter/src/comments/mod.rs
expression: comments.debug(test_case.source_code)
---
{
Node {
kind: StmtExpr,
range: 1..2,
source: `x`,
}: {
"leading": [],
"dangling": [],
"trailing": [
SourceComment {
text: "# trailing comment",
position: EndOfLine,
formatted: false,
},
],
},
Node {
kind: StmtExpr,
range: 22..23,
source: `y`,
}: {
"leading": [],
"dangling": [],
"trailing": [
SourceComment {
text: "# trailing last node",
position: EndOfLine,
formatted: false,
},
],
},
}

View file

@ -0,0 +1,36 @@
---
source: crates/ruff_python_formatter/src/comments/mod.rs
expression: comments.debug(test_case.source_code)
---
{
Node {
kind: ExprConstant,
range: 11..12,
source: `3`,
}: {
"leading": [],
"dangling": [],
"trailing": [
SourceComment {
text: "# trailing comment",
position: EndOfLine,
formatted: false,
},
],
},
Node {
kind: StmtExpr,
range: 1..33,
source: `x + (⏎`,
}: {
"leading": [],
"dangling": [],
"trailing": [
SourceComment {
text: "# outer",
position: EndOfLine,
formatted: false,
},
],
},
}