Skip to content

Commit 9dce295

Browse files
authored
Merge pull request #1803 from guwirth/fix-highlighting
fix syntax highlighting
2 parents 8e88787 + e6c6cc8 commit 9dce295

File tree

11 files changed

+60
-33
lines changed

11 files changed

+60
-33
lines changed

cxx-checks/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.sonarsource.sonarqube-plugins.cxx</groupId>
77
<artifactId>cxx</artifactId>
8-
<version>1.3.0-SNAPSHOT</version>
8+
<version>1.3.2-SNAPSHOT</version>
99
</parent>
1010

1111
<artifactId>cxx-checks</artifactId>

cxx-sensors/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.sonarsource.sonarqube-plugins.cxx</groupId>
77
<artifactId>cxx</artifactId>
8-
<version>1.3.0-SNAPSHOT</version>
8+
<version>1.3.2-SNAPSHOT</version>
99
</parent>
1010

1111
<artifactId>cxx-sensors</artifactId>

cxx-sensors/src/main/java/org/sonar/cxx/sensors/visitors/CxxHighlighterVisitor.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public CxxHighlighterVisitor(SensorContext context) {
5454
public void visitFile(@Nullable AstNode astNode) {
5555
newHighlighting = context.newHighlighting();
5656
InputFile inputFile = context.fileSystem().inputFile(context.fileSystem().predicates()
57-
.is(getContext().getFile().getAbsoluteFile()));
57+
.is(getContext().getFile().getAbsoluteFile()));
5858
if (inputFile != null) {
5959
newHighlighting.onFile(inputFile);
6060
}
@@ -83,10 +83,10 @@ public void visitToken(Token token) {
8383
} else if (token.getType().equals(CxxTokenType.STRING)) {
8484
Optional<Trivia> triviaWithConcatenatedLiterals = getTriviaWithConcatenatedLiterals(token);
8585
if (!triviaWithConcatenatedLiterals.isPresent()) {
86-
last = highlight(last, new TokenLocation(token), TypeOfText.STRING);
86+
last = highlight(last, new StringLocation(token), TypeOfText.STRING);
8787
} else {
8888
for (Token concatenatedLiterals : triviaWithConcatenatedLiterals.get().getTokens()) {
89-
last = highlight(last, new TokenLocation(concatenatedLiterals), TypeOfText.STRING);
89+
last = highlight(last, new StringLocation(concatenatedLiterals), TypeOfText.STRING);
9090
}
9191
}
9292
}
@@ -95,7 +95,7 @@ public void visitToken(Token token) {
9595
if (trivia.isComment()) {
9696
highlight(last, new CommentLocation(trivia.getToken()), TypeOfText.COMMENT);
9797
} else if (trivia.isSkippedText()
98-
&& trivia.getToken().getType().equals(CxxTokenType.PREPROCESSOR)) {
98+
&& trivia.getToken().getType().equals(CxxTokenType.PREPROCESSOR)) {
9999
highlight(last, new PreprocessorDirectiveLocation(trivia.getToken()), TypeOfText.PREPROCESS_DIRECTIVE);
100100
}
101101
}
@@ -104,20 +104,24 @@ public void visitToken(Token token) {
104104

105105
private Optional<Trivia> getTriviaWithConcatenatedLiterals(Token stringToken) {
106106
return stringToken.getTrivia().stream()
107-
.filter(t -> t.isSkippedText() && CxxTokenType.STRING.equals(t.getToken().getType())).findFirst();
107+
.filter(t -> t.isSkippedText() && CxxTokenType.STRING.equals(t.getToken().getType())).findFirst();
108108
}
109109

110110
private TokenLocation highlight(TokenLocation last, TokenLocation current, TypeOfText typeOfText) {
111111
try {
112112
if (!current.overlaps(last)) {
113113
newHighlighting.highlight(current.startLine(), current.startLineOffset(),
114-
current.endLine(), current.endLineOffset(), typeOfText);
114+
current.endLine(), current.endLineOffset(), typeOfText);
115115
}
116116
} catch (IllegalArgumentException ex) {
117117
// ignore highlight errors: parsing errors could lead to wrong location data
118-
LOG.warn("Highlighting error in file '{}' at line:{}, column:{}", getContext().getFile().getAbsoluteFile(),
119-
current.startLine(), current.startLineOffset());
120-
LOG.debug("highlighting exception {}", ex);
118+
LOG.debug("Highlighting error in file '{}' at start:{}:{} end:{}:{}",
119+
getContext().getFile().getAbsoluteFile(),
120+
current.startLine(),
121+
current.startLineOffset(),
122+
current.endLine(),
123+
current.endLineOffset()
124+
);
121125
}
122126
return current;
123127
}
@@ -155,15 +159,31 @@ public int endLineOffset() {
155159
public boolean overlaps(@Nullable TokenLocation other) {
156160
if (other != null) {
157161
return !(startLineOffset() > other.endLineOffset()
158-
|| other.startLineOffset() > endLineOffset()
159-
|| startLine() > other.endLine()
160-
|| other.startLine() > endLine());
162+
|| other.startLineOffset() > endLineOffset()
163+
|| startLine() > other.endLine()
164+
|| other.startLine() > endLine());
161165
}
162166
return false;
163167
}
164168

165169
}
166170

171+
private static class StringLocation extends TokenLocation {
172+
173+
public StringLocation(Token token) {
174+
super(token);
175+
String value = token.getValue();
176+
if (value.startsWith("R")) { // Raw String?
177+
String[] lines = CxxUtils.EOL_PATTERN.split(value, -1);
178+
179+
if (lines.length > 1) {
180+
endLine = token.getLine() + lines.length - 1;
181+
endLineOffset = lines[lines.length - 1].length();
182+
}
183+
}
184+
}
185+
}
186+
167187
private static class CommentLocation extends TokenLocation {
168188

169189
public CommentLocation(Token token) {

cxx-sensors/src/test/java/org/sonar/cxx/sensors/visitors/CxxHighlighterTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void scanFile() throws IOException {
4747

4848
String content = Files.contentOf(target, StandardCharsets.UTF_8);
4949
DefaultInputFile inputFile = TestInputFileBuilder.create("ProjectKey", baseDir, target)
50-
.setContents(content).setCharset(StandardCharsets.UTF_8).build();
50+
.setContents(content).setCharset(StandardCharsets.UTF_8).build();
5151

5252
context = SensorContextTester.create(baseDir);
5353
context.fileSystem().add(inputFile);
@@ -113,6 +113,9 @@ public void stringLiteral() {
113113
checkOnRange(96, 25, 7, TypeOfText.STRING);
114114
checkOnRange(97, 25, 6, TypeOfText.STRING);
115115
checkOnRange(98, 25, 5, TypeOfText.STRING);
116+
117+
// R"(\r\n Hello World!\r\n )" issue #1768
118+
check(103, 14, TypeOfText.STRING);
116119
}
117120

118121
@Test
@@ -127,20 +130,17 @@ public void character() {
127130
@SuppressWarnings("squid:S2699") // ... checkOnRange contains the assertion
128131
public void comment() {
129132

130-
check(1, 0, TypeOfText.COMMENT);
131-
/*\r\n comment\r\n*/
133+
check(1, 0, TypeOfText.COMMENT); /*\r\n comment\r\n*/
132134
check(3, 1, TypeOfText.COMMENT);
133135

134-
checkOnRange(5, 0, 2, TypeOfText.COMMENT); //
135-
checkOnRange(6, 0, 10, TypeOfText.COMMENT); // comment
136-
checkOnRange(7, 0, 2, TypeOfText.COMMENT); //
136+
checkOnRange(5, 0, 2, TypeOfText.COMMENT); //
137+
checkOnRange(6, 0, 10, TypeOfText.COMMENT); // comment
138+
checkOnRange(7, 0, 2, TypeOfText.COMMENT); //
137139

138140
checkOnRange(57, 22, 10, TypeOfText.COMMENT); // comment
139141
checkOnRange(58, 3, 10, TypeOfText.COMMENT); // comment
140-
checkOnRange(61, 3, 13, TypeOfText.COMMENT);
141-
/* comment */
142-
checkOnRange(64, 20, 13, TypeOfText.COMMENT);
143-
/* comment */
142+
checkOnRange(61, 3, 13, TypeOfText.COMMENT); /* comment */
143+
checkOnRange(64, 20, 13, TypeOfText.COMMENT); /* comment */
144144
}
145145

146146
@Test
@@ -180,8 +180,8 @@ public void preprocessDirective() {
180180
}
181181

182182
/**
183-
* Checks the highlighting of a range of columns. The first column of a line has index 0. The range is the columns of
184-
* the token.
183+
* Checks the highlighting of a range of columns. The first column of a line
184+
* has index 0. The range is the columns of the token.
185185
*/
186186
private void checkOnRange(int line, int firstColumn, int length, TypeOfText expectedTypeOfText) {
187187
// check that every column of the token is highlighted (and with the expected type)

cxx-sensors/src/test/resources/org/sonar/cxx/sensors/highlighter.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,11 @@ void test3()
9898
"Lou";
9999
}
100100

101+
void test4()
102+
{
103+
auto txt = R"(
104+
Hello World!
105+
)";
106+
}
107+
101108
/* EOF */

cxx-squid/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.sonarsource.sonarqube-plugins.cxx</groupId>
77
<artifactId>cxx</artifactId>
8-
<version>1.3.0-SNAPSHOT</version>
8+
<version>1.3.2-SNAPSHOT</version>
99
</parent>
1010

1111
<artifactId>cxx-squid</artifactId>

integration-tests/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>org.sonarsource.sonarqube-plugins.cxx</groupId>
55
<artifactId>cxx</artifactId>
6-
<version>1.3.0-SNAPSHOT</version>
6+
<version>1.3.2-SNAPSHOT</version>
77
</parent>
88

99
<artifactId>integration-tests</artifactId>

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<groupId>org.sonarsource.sonarqube-plugins.cxx</groupId>
1414
<artifactId>cxx</artifactId>
15-
<version>1.3.0-SNAPSHOT</version>
15+
<version>1.3.2-SNAPSHOT</version>
1616
<packaging>pom</packaging>
1717

1818
<name>Cxx</name>
@@ -374,7 +374,7 @@
374374
<artifactId>guava</artifactId>
375375
<version>19.0</version>
376376
</dependency>
377-
</dependencies>
377+
</dependencies>
378378
</dependencyManagement>
379379
<build>
380380
<!-- To define the plugin version in your parent POM -->

sonar-c-plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.sonarsource.sonarqube-plugins.cxx</groupId>
77
<artifactId>cxx</artifactId>
8-
<version>1.3.0-SNAPSHOT</version>
8+
<version>1.3.2-SNAPSHOT</version>
99
</parent>
1010

1111

sonar-cxx-plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.sonarsource.sonarqube-plugins.cxx</groupId>
77
<artifactId>cxx</artifactId>
8-
<version>1.3.0-SNAPSHOT</version>
8+
<version>1.3.2-SNAPSHOT</version>
99
</parent>
1010

1111
<artifactId>sonar-cxx-plugin</artifactId>

0 commit comments

Comments
 (0)