rtfpessoa / diff2html

Pretty diff to html javascript library (diff2html)
https://diff2html.xyz
MIT License
2.94k stars 282 forks source link

Incorrect code highlight when a previous chunk end with a block comment #148

Closed tdurieux closed 7 years ago

tdurieux commented 7 years ago

Step 0: Describe your environment

Step 1: Describe the problem:

The code of the second chunk is highlighted as a comment because the first chunk ends with a block comment.

image

diff example:

diff --git a/src/com/google/javascript/jscomp/TypedScopeCreator.java b/src/com/google/javascript/jscomp/TypedScopeCreator.java
index 92d8715..f9943a1 100644
--- a/src/com/google/javascript/jscomp/TypedScopeCreator.java
+++ b/src/com/google/javascript/jscomp/TypedScopeCreator.java
@@ -417,6 +417,7 @@ final class TypedScopeCreator implements ScopeCreator {
      * For more information, see
      * http://code.google.com/p/closure-compiler/issues/detail?id=314
      */
+    private List<Node> lentObjectLiterals = null;

     /**
      * Type-less stubs.
@@ -544,6 +545,13 @@ final class TypedScopeCreator implements ScopeCreator {
       }

       // Analyze any @lends object literals in this statement.
+      if (n.getParent() != null && NodeUtil.isStatement(n) &&
+          lentObjectLiterals != null) {
+        for (Node objLit : lentObjectLiterals) {
+          defineObjectLiteral(objLit);
+        }
+        lentObjectLiterals.clear();
+      }
     }

Relevant Code:

var diff2htmlUi = new Diff2HtmlUI({ diff: diff });
diff2htmlUi.draw($(elem), {inputFormat: 'java', showFiles: false, matching: 'lines'});
diff2htmlUi.highlightCode($(elem));
rtfpessoa commented 7 years ago

@tdurieux this is a know issue, but is also the best I could do with the partial information available in the diff. If you want to take a look at the code in https://github.com/rtfpessoa/diff2html/blob/master/src/ui/js/diff2html-ui.js#L82 and make any suggestions, fell free to open a PR.

NOTE: If you retrieve a diff with more context can also help but you will have bigger diffs.