r311570 - [clang-diff] HTML diff navigation

Johannes Altmanninger via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 23 09:32:13 PDT 2017


Author: krobelus
Date: Wed Aug 23 09:32:13 2017
New Revision: 311570

URL: http://llvm.org/viewvc/llvm-project?rev=311570&view=rev
Log:
[clang-diff] HTML diff navigation

Summary:
This adds shortcuts j and k to jump between changes.
It is especially useful in diffs with few changes.

Reviewers: arphaman

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D36685

Modified:
    cfe/trunk/tools/clang-diff/ClangDiff.cpp

Modified: cfe/trunk/tools/clang-diff/ClangDiff.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-diff/ClangDiff.cpp?rev=311570&r1=311569&r2=311570&view=diff
==============================================================================
--- cfe/trunk/tools/clang-diff/ClangDiff.cpp (original)
+++ cfe/trunk/tools/clang-diff/ClangDiff.cpp Wed Aug 23 09:32:13 2017
@@ -139,13 +139,14 @@ div.code {
 highlightStack = []
 function clearHighlight() {
   while (highlightStack.length) {
-    let [l, r] = highlightStack.pop()
+    var [l, r] = highlightStack.pop()
     document.getElementById(l).style.backgroundColor = 'white'
-    document.getElementById(r).style.backgroundColor = 'white'
+    if (r[1] != '-')
+      document.getElementById(r).style.backgroundColor = 'white'
   }
 }
 function highlight(event) {
-  id = event.target['id']
+  var id = event.target['id']
   doHighlight(id)
 }
 function doHighlight(id) {
@@ -153,20 +154,66 @@ function doHighlight(id) {
   source = document.getElementById(id)
   if (!source.attributes['tid'])
     return
-  tid = source.attributes['tid'].value
-  target = document.getElementById(tid)
-  if (!target || source.parentElement && source.parentElement.classList.contains('code'))
+  var mapped = source
+  while (mapped && mapped.parentElement && mapped.attributes['tid'].value.substr(1) === '-1')
+    mapped = mapped.parentElement
+  var tid = null, target = null
+  if (mapped) {
+    tid = mapped.attributes['tid'].value
+    target = document.getElementById(tid)
+  }
+  if (source.parentElement && source.parentElement.classList.contains('code'))
     return
-  source.style.backgroundColor = target.style.backgroundColor = 'lightgrey'
-  highlightStack.push([id, tid])
+  source.style.backgroundColor = 'lightgrey'
   source.scrollIntoView()
-  target.scrollIntoView()
+  if (target) {
+    if (mapped === source)
+      target.style.backgroundColor = 'lightgrey'
+    target.scrollIntoView()
+  }
+  highlightStack.push([id, tid])
   location.hash = '#' + id
 }
 function scrollToBoth() {
   doHighlight(location.hash.substr(1))
 }
+function changed(elem) {
+  return elem.classList.length == 0
+}
+function nextChangedNode(prefix, increment, number) {
+  do {
+    number += increment
+    var elem = document.getElementById(prefix + number)
+  } while(elem && !changed(elem))
+  return elem ? number : null
+}
+function handleKey(e) {
+  var down = e.code === "KeyJ"
+  var up = e.code === "KeyK"
+  if (!down && !up)
+    return
+  var id = highlightStack[0] ? highlightStack[0][0] : 'R0'
+  var oldelem = document.getElementById(id)
+  var number = parseInt(id.substr(1))
+  var increment = down ? 1 : -1
+  var lastnumber = number
+  var prefix = id[0]
+  do {
+    number = nextChangedNode(prefix, increment, number)
+    var elem = document.getElementById(prefix + number)
+    if (up && elem) {
+      while (elem.parentElement && changed(elem.parentElement))
+        elem = elem.parentElement
+      number = elem.id.substr(1)
+    }
+  } while ((down && id !== 'R0' && oldelem.contains(elem)))
+  if (!number)
+    number = lastnumber
+  elem = document.getElementById(prefix + number)
+  doHighlight(prefix + number)
+}
 window.onload = scrollToBoth
+window.onkeydown = handleKey
 </script>
 <body>
 <div onclick='highlight(event)'>




More information about the cfe-commits mailing list