]> source.dussan.org Git - sonarqube.git/commitdiff
fix SONAR-2295 : Line number representation in source code view are wrong for files...
authorFreddy Mallet <freddy.mallet@gmail.com>
Sat, 16 Apr 2011 22:27:30 +0000 (00:27 +0200)
committerFreddy Mallet <freddy.mallet@gmail.com>
Sat, 16 Apr 2011 22:27:30 +0000 (00:27 +0200)
sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java
sonar-colorizer/src/main/java/org/sonar/colorizer/HtmlDecorator.java
sonar-colorizer/src/test/java/org/sonar/colorizer/HtmlDecoratorTest.java

index 5515e7eb284606e0dfcf65d42913abf6fd795d8b..dbb28eb7b0bd2e2d76bb20dfd8c1d55cacbc5653 100644 (file)
@@ -94,6 +94,7 @@ public class CodeBuffer implements CharSequence {
   }
 
   private void updateCursorPosition(int character) {
+    //see Java Language Specification : http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.4
     if (character == LF || character == CR) {
       if ((lastChar != LF && lastChar != CR) || lastChar == character || lastChar == LF) {
         cursor.line++;
index 0d0b4386118e3de3845a43bee59820d3f37d8a59..5e4e2e512d858b337d965c8bc06529603ee7d0fc 100644 (file)
@@ -30,7 +30,7 @@ public class HtmlDecorator extends Tokenizer {
   private static final String CSS_PATH = "/sonar-colorizer.css";
 
   private HtmlOptions options;
-  private int lineId = 1;
+  private int lineId;
 
   private static final int LF = (int) '\n';
   private static final int CR = (int) '\r';
@@ -76,18 +76,15 @@ public class HtmlDecorator extends Tokenizer {
 
   @Override
   public boolean consume(CodeReader code, HtmlCodeBuilder codeBuilder) {
-    int currentChar = code.peek();
-
-    if (currentChar == LF) {
-      codeBuilder.appendWithoutTransforming(getTagAfter());
-      codeBuilder.appendWithoutTransforming(getTagBefore());
-    }
-
-    if (currentChar == LF || currentChar == CR) {
+    int lineNumber = code.getLinePosition();
+    if (code.peek() == LF || code.peek() == CR) {
       code.pop();
+      if (lineNumber != code.getLinePosition()) {
+        codeBuilder.appendWithoutTransforming(getTagAfter());
+        codeBuilder.appendWithoutTransforming(getTagBefore());
+      }
       return true;
     }
-
     return false;
   }
 
index a49dafe025b37306aea5ae32361c366708e83f5f..916640de16881657dda635dae621ff6a60ed4152 100644 (file)
@@ -80,7 +80,7 @@ public class HtmlDecoratorTest {
   public void shouldAddTagsBetweenEachLine() {
     HtmlOptions options = new HtmlOptions().setGenerateTable(true).setGenerateHtmlHeader(false);
     HtmlDecorator decorator = new HtmlDecorator(options);
-    CodeReader code = new CodeReader("\n\r\n");
+    CodeReader code = new CodeReader("\r\n\r");
     HtmlCodeBuilder output = new HtmlCodeBuilder();
 
     output.appendWithoutTransforming(decorator.getTagBeginOfFile());