Skip to content

Commit 6060554

Browse files
authored
SONARPY-2420 Fix crash because of mismatch between line and pythonLine (#2224)
1 parent ac3982b commit 6060554

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

python-frontend/src/main/java/org/sonar/python/tree/TreeUtils.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static Tree firstAncestorOfKind(Tree tree, Kind... kinds) {
9696
}
9797

9898
public static Comparator<Tree> getTreeByPositionComparator() {
99-
return Comparator.comparing((Tree t) -> t.firstToken().line()).thenComparing((Tree t) -> t.firstToken().column());
99+
return Comparator.comparing((Tree t) -> t.firstToken().pythonLine()).thenComparing((Tree t) -> t.firstToken().pythonColumn());
100100
}
101101

102102
public static List<Token> tokens(Tree tree) {
@@ -256,8 +256,8 @@ public static int findIndentationSize(Tree tree) {
256256
var treeToken = tree.firstToken();
257257
var parentToken = parent.firstToken();
258258

259-
if (treeToken.line() != parentToken.line()) {
260-
return treeToken.column() - parentToken.column();
259+
if (treeToken.pythonLine() != parentToken.pythonLine()) {
260+
return treeToken.pythonColumn() - parentToken.pythonColumn();
261261
} else {
262262
return findIndentationSize(parent);
263263
}
@@ -270,8 +270,8 @@ private static int findIndentDownTree(Tree parent) {
270270
.map(child -> {
271271

272272
var childToken = child.firstToken();
273-
if (childToken.line() > parentToken.line() && childToken.column() > parentToken.column()) {
274-
return childToken.column() - parentToken.column();
273+
if (childToken.pythonLine() > parentToken.pythonLine() && childToken.pythonColumn() > parentToken.pythonColumn()) {
274+
return childToken.pythonColumn() - parentToken.pythonColumn();
275275
} else {
276276
return findIndentDownTree(child);
277277
}
@@ -477,8 +477,8 @@ public static Optional<Tree> firstChild(Tree tree, Predicate<Tree> filter) {
477477

478478
public static String treeToString(Tree tree, boolean renderMultiline) {
479479
if (!renderMultiline) {
480-
var firstLine = tree.firstToken().line();
481-
var lastLine = tree.lastToken().line();
480+
var firstLine = tree.firstToken().pythonLine();
481+
var lastLine = tree.lastToken().pythonLine();
482482

483483
// We decided to not support multiline default parameters
484484
// because it requires indents calculation for place where the value should be copied.

0 commit comments

Comments
 (0)