-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONARJAVA-4963 Line and column positions are wrong after text blocks …
…using '\' line continuations (#4864)
- Loading branch information
1 parent
d543e1d
commit 03e34a9
Showing
11 changed files
with
270 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
java-frontend/src/main/java/org/sonar/java/model/LineColumnConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* SonarQube Java | ||
* Copyright (C) 2012-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.java.model; | ||
|
||
import java.util.Arrays; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* There is a different convention in the JDT for line and column numbers. | ||
* The difference is only when there are some line continuation characters in text blocks. | ||
* Eclipse does not count the line continuation as a new line. | ||
* So we cannot use the following methods to get the line and column number: | ||
* {@link org.eclipse.jdt.core.dom.CompilationUnit#getColumnNumber(int position)} | ||
* {@link org.eclipse.jdt.core.dom.CompilationUnit#getLineNumber(int position)} | ||
*/ | ||
public class LineColumnConverter { | ||
|
||
private static final Pattern LINE_SEPARATOR_PATTERN = Pattern.compile("\r\n?|\n"); | ||
|
||
private int[] lineStartIndexes = new int[64]; | ||
private int lineStartIndexesLength = 0; | ||
|
||
public LineColumnConverter(String source) { | ||
Matcher matcher = LINE_SEPARATOR_PATTERN.matcher(source); | ||
addLineStartIndex(0); | ||
while (matcher.find()) { | ||
addLineStartIndex(matcher.end()); | ||
} | ||
addLineStartIndex(Integer.MAX_VALUE); | ||
} | ||
|
||
private void addLineStartIndex(int index) { | ||
if (lineStartIndexesLength == lineStartIndexes.length) { | ||
lineStartIndexes = Arrays.copyOf(lineStartIndexes, lineStartIndexes.length * 2); | ||
} | ||
lineStartIndexes[lineStartIndexesLength] = index; | ||
lineStartIndexesLength++; | ||
} | ||
|
||
public Pos toPos(int absolutSourcePosition) { | ||
int searchResult = Arrays.binarySearch(lineStartIndexes, 0, lineStartIndexesLength, absolutSourcePosition); | ||
if (searchResult < 0) { | ||
return new Pos(-searchResult - 1, absolutSourcePosition - lineStartIndexes[-searchResult - 2]); | ||
} else { | ||
return new Pos(searchResult + 1, 0); | ||
} | ||
} | ||
|
||
public record Pos(int line, int columnOffset) { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class TextBlock { | ||
String a = """ | ||
Hello, | ||
world! | ||
"""; | ||
} |
11 changes: 11 additions & 0 deletions
11
java-frontend/src/test/files/metrics/TextBlockWithLineContinuation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class TextBlock { | ||
String a = """ | ||
Hello,\ | ||
world! | ||
"""; | ||
String b = """ | ||
Goodbye,\ | ||
cruel \ | ||
world! | ||
"""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.