bpfmt: Preserve line of comment when sorting arrays

When given as an input
```
array: [
    "a",
    // Unicorn
    "b",
]
```
bpfmt with `-s` option was outputing
```
array: [
    "a", // Unicorn
    "b",
]
```

Which is not ideal because the comment was targetting
the second value and now it seems to be targetting the
first one

This patch preserve the difference in line number between
the value and the comment to give the same output when
```
array: [
    "a",
    // Unicorn
    "b",
]
```
is given as input

Test: Manual tests + run bpfmt -w -s on packages/modules/Bluetooth
Change-Id: I2b58f20da463bea77c22a4e6978aa9beb4b4fcc8
This commit is contained in:
David Duarte 2023-04-18 04:23:37 +00:00 committed by William Escande
parent 2621c909e5
commit 65aa5a505f

View file

@ -227,7 +227,8 @@ func sortSubList(values []Expression, nextPos scanner.Position, file *File) {
values[i].(*String).LiteralPos = curPos
for j, c := range copyComments {
if c.Pos().Offset > e.pos.Offset && c.Pos().Offset < e.nextPos.Offset {
file.Comments[j].Comments[0].Slash.Line = curPos.Line
file.Comments[j].Comments[0].Slash.Line = curPos.Line + c.Pos().Line - e.pos.Line
curPos.Line += c.Pos().Line - e.pos.Line
file.Comments[j].Comments[0].Slash.Offset += values[i].Pos().Offset - e.pos.Offset
}
}