Skip to content

Commit 24ebb68

Browse files
committed
Adjust tab width char handling
With pmd/pmd#2656 tab characters are now handled with column with of 1. That means, the position that PMD's parser reports uses this with. The display of the tab character might be different in the code are of the designer (and in any other text editor).
1 parent 3290177 commit 24ebb68

File tree

2 files changed

+13
-39
lines changed

2 files changed

+13
-39
lines changed

src/main/java/net/sourceforge/pmd/util/fxdesigner/util/codearea/PmdCoordinatesSystem.java

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ public static TextPos2D getPmdLineAndColumnFromOffset(CodeArea codeArea, int abs
5454

5555
Position pos = codeArea.offsetToPosition(absoluteOffset, Bias.Forward);
5656

57-
return new TextPos2D(getPmdLineFromRtfxParIndex(pos.getMajor()),
58-
getPmdColumnIndexFromRtfxColumn(codeArea, pos.getMajor(), pos.getMinor()));
57+
return new TextPos2D(getPmdLineFromRtfxParIndex(pos.getMajor()), pos.getMinor() + 1);
5958
}
6059

6160

@@ -65,7 +64,7 @@ public static TextPos2D getPmdLineAndColumnFromOffset(CodeArea codeArea, int abs
6564
*
6665
* <ul>
6766
* <li>CodeArea counts a tab as 1 column width but displays it as 8 columns width.
68-
* PMD counts it correctly as 8 columns, so the position must be offset.
67+
* PMD counts it correctly as 1 column (since PMD 6.27.0, before it was 8 columns).
6968
* <li>PMD lines start at 1 but paragraph nums start at 0 in the code area,
7069
* same for columns.
7170
* <li>PMD's end column is inclusive and not exclusive.
@@ -76,36 +75,11 @@ public static int getOffsetFromPmdPosition(CodeArea codeArea, int line, int colu
7675
column = max(column, 1);
7776

7877
int parIdx = getRtfxParIndexFromPmdLine(line);
79-
int raw = codeArea.getAbsolutePosition(parIdx, getRtfxColumnIndexFromPmdColumn(codeArea, parIdx, column));
78+
int raw = codeArea.getAbsolutePosition(parIdx, column - 1);
8079
return clip(raw, 0, codeArea.getLength() - 1);
8180
}
8281

8382

84-
private static int getRtfxColumnIndexFromPmdColumn(CodeArea codeArea, int parIdx, int column) {
85-
String parTxt = codeArea.getParagraph(parIdx).getText();
86-
int end = column - 1;
87-
for (int i = 0; i < end && end > 0; i++) {
88-
char c = parTxt.charAt(i);
89-
if (c == '\t') {
90-
end = max(end - 7, 0);
91-
}
92-
}
93-
return end;
94-
}
95-
96-
private static int getPmdColumnIndexFromRtfxColumn(CodeArea codeArea, int parIdx, int rtfxCol) {
97-
String parTxt = codeArea.getParagraph(parIdx).getText();
98-
int mapped = rtfxCol;
99-
for (int i = 0; i < rtfxCol && i < parTxt.length(); i++) {
100-
char c = parTxt.charAt(i);
101-
if (c == '\t') {
102-
mapped += 7;
103-
}
104-
}
105-
return mapped + 1;
106-
}
107-
108-
10983
private static int clip(int val, int min, int max) {
11084
return max(min, min(val, max));
11185
}

src/test/kotlin/net/sourceforge/pmd/util/fxdesigner/util/codearea/CoordinateMappingTest.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,27 @@ class CoordinateMappingTest : FunSpec({
5353
//--------
5454

5555
codeArea.fromPmd(1, 1) shouldBe 0
56-
codeArea.fromPmd(1, 2) shouldBe 0
57-
codeArea.fromPmd(1, 7) shouldBe 0
58-
codeArea.fromPmd(1, 8) shouldBe 0
59-
codeArea.fromPmd(1, 9) shouldBe 1
60-
codeArea.fromPmd(1, 10) shouldBe 2
56+
codeArea.fromPmd(1, 2) shouldBe 1
57+
codeArea.fromPmd(1, 3) shouldBe 2
58+
codeArea.fromPmd(1, 8) shouldBe 7
6159

6260
codeArea.toPmd(0) shouldBe TextPos2D(1, 1)
63-
codeArea.toPmd(1) shouldBe TextPos2D(1, 9)
64-
codeArea.toPmd(2) shouldBe TextPos2D(1, 10)
61+
codeArea.toPmd(1) shouldBe TextPos2D(1, 2)
62+
codeArea.toPmd(2) shouldBe TextPos2D(1, 3)
63+
codeArea.toPmd(7) shouldBe TextPos2D(1, 8)
6564
}
6665

6766
test("All tabs corner case") {
6867

6968
val codeArea = CodeArea("\t\t\t")
7069

7170
codeArea.fromPmd(1, 1) shouldBe 0
72-
codeArea.fromPmd(1, 2) shouldBe 0
71+
codeArea.fromPmd(1, 2) shouldBe 1
72+
codeArea.fromPmd(1, 3) shouldBe 2
7373

7474
codeArea.toPmd(0) shouldBe TextPos2D(1, 1)
75-
codeArea.toPmd(1) shouldBe TextPos2D(1, 9)
76-
codeArea.toPmd(2) shouldBe TextPos2D(1, 17)
75+
codeArea.toPmd(1) shouldBe TextPos2D(1, 2)
76+
codeArea.toPmd(2) shouldBe TextPos2D(1, 3)
7777
}
7878

7979
test("Empty corner case") {

0 commit comments

Comments
 (0)