<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
+
<parent>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar</artifactId>
<version>3.2-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
+
<artifactId>sonar-colorizer</artifactId>
- <packaging>jar</packaging>
+
<name>Sonar :: Code Colorizer</name>
<description>Code syntax highlighter</description>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.easytesting</groupId>
+ <artifactId>fest-assert</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
-</project>
\ No newline at end of file
+
+</project>
*/
package org.sonar.colorizer;
-import java.util.Arrays;
-
import org.sonar.channel.CodeReader;
import org.sonar.channel.EndMatcher;
+import java.util.Arrays;
+
public class MultilinesDocTokenizer extends Tokenizer {
private static final String COMMENT_STARTED_ON_PREVIOUS_LINE = "COMMENT_STARTED_ON_PREVIOUS_LINE";
public boolean consume(CodeReader code, HtmlCodeBuilder codeBuilder) {
if (hasNextToken(code, codeBuilder)) {
codeBuilder.appendWithoutTransforming(tagBefore);
- code.popTo(new MultilineEndTokenMatcher(codeBuilder), codeBuilder);
+ code.popTo(new MultilineEndTokenMatcher(code, codeBuilder), codeBuilder);
codeBuilder.appendWithoutTransforming(tagAfter);
return true;
} else {
private class MultilineEndTokenMatcher implements EndMatcher {
- private final HtmlCodeBuilder code;
- private final StringBuilder colorizedCode;
+ private final CodeReader code;
+ private final HtmlCodeBuilder codeBuilder;
private int commentSize = 0;
- public MultilineEndTokenMatcher(HtmlCodeBuilder code) {
+ public MultilineEndTokenMatcher(CodeReader code, HtmlCodeBuilder codeBuilder) {
this.code = code;
- this.colorizedCode = code.getColorizedCode();
+ this.codeBuilder = codeBuilder;
}
public boolean match(int endFlag) {
commentSize++;
- if (commentSize >= endToken.length + startToken.length || (commentSize >= endToken.length && isCommentStartedOnPreviousLine(code))) {
+ if (commentSize >= endToken.length + startToken.length || (commentSize >= endToken.length && isCommentStartedOnPreviousLine(codeBuilder))) {
boolean matches = true;
for (int i = 1; i <= endToken.length; i++) {
- if (colorizedCode.charAt(colorizedCode.length() - i) != endToken[endToken.length - i]) {
+ if (code.charAt(-i) != endToken[endToken.length - i]) {
matches = false;
break;
}
}
if (matches) {
- setCommentStartedOnPreviousLine(code, Boolean.FALSE);
+ setCommentStartedOnPreviousLine(codeBuilder, Boolean.FALSE);
return true;
}
}
if (endFlag == '\r' || endFlag == '\n') {
- setCommentStartedOnPreviousLine(code, Boolean.TRUE);
+ setCommentStartedOnPreviousLine(codeBuilder, Boolean.TRUE);
return true;
}
return false;
private String getTokenizerId() {
return getClass().getSimpleName();
}
+
}
import org.junit.Test;
import org.sonar.channel.CodeReader;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.fest.assertions.Assertions.assertThat;
public class MultilinesDocTokenizerTest {
@Test
public void testStandardComment() {
MultilinesDocTokenizer tokenizer = new MultiLineDocTokenizerImpl("{[||", "");
- assertThat(tokenizer.hasNextToken(new CodeReader("{[|| And here is strange multi-line comment"), new HtmlCodeBuilder()), is(true));
- assertThat(tokenizer.hasNextToken(new CodeReader("// this is not a strange multi-line comment"), new HtmlCodeBuilder()), is(false));
+ assertThat(tokenizer.hasNextToken(new CodeReader("{[|| And here is strange multi-line comment"), new HtmlCodeBuilder())).isTrue();
+ assertThat(tokenizer.hasNextToken(new CodeReader("// this is not a strange multi-line comment"), new HtmlCodeBuilder())).isFalse();
}
@Test
public void testLongStartToken() {
+ CodeReader reader = new CodeReader("/*** multi-line comment**/ private part");
MultilinesDocTokenizer tokenizer = new MultiLineDocTokenizerImpl("/***", "**/");
- assertTrue(tokenizer.consume(new CodeReader("/*** multi-line comment**/ private part"), codeBuilder));
- assertEquals("/*** multi-line comment**/", codeBuilder.toString());
+ assertThat(tokenizer.consume(reader, codeBuilder)).isTrue();
+ assertThat(codeBuilder.toString()).isEqualTo("/*** multi-line comment**/");
}
-
+
@Test
public void testStartTokenEndTokenOverlapping() {
+ CodeReader reader = new CodeReader("/*// multi-line comment*/ private part");
MultilinesDocTokenizer tokenizer = new MultiLineDocTokenizerImpl("/*", "*/");
- assertTrue(tokenizer.consume(new CodeReader("/*// multi-line comment*/ private part"), codeBuilder));
- assertEquals("/*// multi-line comment*/", codeBuilder.toString());
+ assertThat(tokenizer.consume(reader, codeBuilder)).isTrue();
+ assertThat(codeBuilder.toString()).isEqualTo("/*// multi-line comment*/");
}
-
+
@Test
public void testMultilinesComment() {
CodeReader reader = new CodeReader("/* multi-line comment\n*/ private part");
MultilinesDocTokenizer tokenizer = new MultiLineDocTokenizerImpl("/*", "*/");
- assertTrue(tokenizer.consume(reader, codeBuilder));
- assertEquals("/* multi-line comment", codeBuilder.toString());
+ assertThat(tokenizer.consume(reader, codeBuilder)).isTrue();
+ assertThat(codeBuilder.toString()).isEqualTo("/* multi-line comment");
reader.pop();
- assertTrue(tokenizer.consume(reader, codeBuilder));
- assertEquals("/* multi-line comment*/", codeBuilder.toString());
+ assertThat(tokenizer.consume(reader, codeBuilder)).isTrue();
+ assertThat(codeBuilder.toString()).isEqualTo("/* multi-line comment*/");
}
- public class MultiLineDocTokenizerImpl extends MultilinesDocTokenizer {
+ /**
+ * SONAR-3531
+ */
+ @Test
+ public void should_work_for_html_comments() {
+ CodeReader reader = new CodeReader("<!-- multi-line comment\n--> private part");
+ MultilinesDocTokenizer tokenizer = new MultiLineDocTokenizerImpl("<!--", "-->");
+ assertThat(tokenizer.consume(reader, codeBuilder)).isTrue();
+ assertThat(codeBuilder.toString()).isEqualTo("<!-- multi-line comment");
+ reader.pop();
+ assertThat(tokenizer.consume(reader, codeBuilder)).isTrue();
+ assertThat(codeBuilder.toString()).isEqualTo("<!-- multi-line comment-->");
+ }
+ public class MultiLineDocTokenizerImpl extends MultilinesDocTokenizer {
public MultiLineDocTokenizerImpl(String startToken, String endToken) {
super(startToken, endToken, "", "");
}