Skip to content

Commit 44771c6

Browse files
author
Todd Whiteman
committed
addons: add the code for side-by-side diff extension
git-svn-id: http://svn.openkomodo.com/repos/addons/sbsdiff@3934 b87d16e7-d29f-4ee1-b5ec-817d645d335f
0 parents  commit 44771c6

File tree

744 files changed

+89818
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

744 files changed

+89818
-0
lines changed

chrome.manifest

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
content sbsdiff content/ xpcnativewrappers=yes contentaccessible=yes
2+
skin sbsdiff classic/1.0 pylib/reviewboard/htdocs/media/rb/css/
3+
overlay chrome://komodo/content/dialogs/diff.xul chrome://sbsdiff/content/diff-overlay.xul

components/sbsDiff.py

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/usr/bin/env python
2+
3+
# ***** BEGIN LICENSE BLOCK *****
4+
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
5+
#
6+
# The contents of this file are subject to the Mozilla Public License
7+
# Version 1.1 (the "License"); you may not use this file except in
8+
# compliance with the License. You may obtain a copy of the License at
9+
# http://www.mozilla.org/MPL/
10+
#
11+
# Software distributed under the License is distributed on an "AS IS"
12+
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing rights and limitations
14+
# under the License.
15+
#
16+
# The Original Code is "side by side diff" code.
17+
#
18+
# The Initial Developer of the Original Code is ActiveState Software Inc.
19+
# Portions created by ActiveState Software Inc are Copyright (C) 2008-2009
20+
# ActiveState Software Inc. All Rights Reserved.
21+
#
22+
# Contributor(s):
23+
# Todd Whiteman @ ActiveState Software Inc
24+
#
25+
# Alternatively, the contents of this file may be used under the terms of
26+
# either the GNU General Public License Version 2 or later (the "GPL"), or
27+
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28+
# in which case the provisions of the GPL or the LGPL are applicable instead
29+
# of those above. If you wish to allow use of your version of this file only
30+
# under the terms of either the GPL or the LGPL, and not to allow others to
31+
# use your version of this file under the terms of the MPL, indicate your
32+
# decision by deleting the provisions above and replace them with the notice
33+
# and other provisions required by the GPL or the LGPL. If you do not delete
34+
# the provisions above, a recipient may use your version of this file under
35+
# the terms of any one of the MPL, the GPL or the LGPL.
36+
#
37+
# ***** END LICENSE BLOCK *****
38+
39+
import os
40+
import sys
41+
42+
from xpcom import components, ServerException, nsError
43+
from xpcom.server import WrapObject, UnwrapObject
44+
45+
46+
rvb_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "pylib", "reviewboard")
47+
if rvb_path not in sys.path:
48+
sys.path.append(rvb_path)
49+
os.environ["DJANGO_SETTINGS_MODULE"] = "settings"
50+
51+
52+
class sbsDiff:
53+
_com_interfaces_ = [components.interfaces.sbsIDiff]
54+
_reg_clsid_ = "{63a9448f-cfd2-4bed-9358-84fabf68d910}"
55+
_reg_contractid_ = "@activestate.com/sbsDiff;1"
56+
_reg_desc_ = "Side by side diff component for generating HTML diffs"
57+
58+
59+
def __init__(self):
60+
self.enable_syntax_highlighting = True
61+
self.cwd = None
62+
self.koDiff = None
63+
64+
html_template = """
65+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
66+
<html lang="en">
67+
<head>
68+
<title>Diff</title>
69+
<link rel="stylesheet" type="text/css" href="chrome://sbsdiff/skin/common.css" />
70+
<link rel="stylesheet" type="text/css" href="chrome://sbsdiff/skin/diffviewer.css" />
71+
<link rel="stylesheet" type="text/css" href="chrome://sbsdiff/skin/syntax.css" />
72+
73+
<script type="text/javascript" src="chrome://sbsdiff/content/diffviewer.js"></script>
74+
75+
</head>
76+
<body>
77+
78+
%s
79+
80+
</body>
81+
</html>
82+
"""
83+
84+
def generateSbsDiff(self, koIDiff):
85+
#diff_data = file("/tmp/fd.patch").read()
86+
import sbs_diff_helper
87+
reload(sbs_diff_helper)
88+
self.koDiff = UnwrapObject(koIDiff)
89+
sbsdiff = sbs_diff_helper.SideBySideDiff(self.koDiff,
90+
self.cwd,
91+
self.enable_syntax_highlighting)
92+
return self.html_template % (sbsdiff.toHTML())
93+
94+
def filepathFromChunkId(self, chunk_id):
95+
sp = chunk_id.split(".")
96+
if len(sp) == 3:
97+
try:
98+
file_pos = int(sp[1]) - 1
99+
if file_pos >= 0 and file_pos < len(self.koDiff.diffex.file_diffs):
100+
return self.koDiff.diffex.file_diffs[file_pos].best_path(self.cwd)
101+
except ValueError:
102+
pass
103+
104+
def diffLinenoFromChunkId(self, chunk_id):
105+
sp = chunk_id.split(".")
106+
if len(sp) == 3:
107+
try:
108+
file_pos = int(sp[1]) - 1
109+
if file_pos >= 0 and file_pos < len(self.koDiff.diffex.file_diffs):
110+
fd = self.koDiff.diffex.file_diffs[file_pos]
111+
diff_lineno = fd.hunks[0].start_line
112+
return self.koDiff.diffex.file_pos_from_diff_pos(diff_lineno, 0)[1]
113+
except ValueError:
114+
pass
115+
return -1

components/sbsIDiff.idl

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "nsISupports.idl"
2+
#include "koIDocument.idl"
3+
4+
[scriptable, uuid(3b50561f-f580-4935-ae1f-c5e4ed26191f)]
5+
interface sbsIDiff: nsISupports {
6+
attribute AString cwd;
7+
attribute boolean enable_syntax_highlighting;
8+
wstring generateSbsDiff(in koIDiff diff);
9+
AString filepathFromChunkId(in AString chunkid);
10+
long diffLinenoFromChunkId(in AString chunkid);
11+
};

components/sbsIDiff.xpt

387 Bytes
Binary file not shown.

content/diff-overlay.js

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/* ***** BEGIN LICENSE BLOCK *****
2+
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
3+
*
4+
* The contents of this file are subject to the Mozilla Public License
5+
* Version 1.1 (the "License"); you may not use this file except in
6+
* compliance with the License. You may obtain a copy of the License at
7+
* http://www.mozilla.org/MPL/
8+
*
9+
* Software distributed under the License is distributed on an "AS IS"
10+
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11+
* License for the specific language governing rights and limitations
12+
* under the License.
13+
*
14+
* The Original Code is "side by side diff" code.
15+
*
16+
* The Initial Developer of the Original Code is ActiveState Software Inc.
17+
* Portions created by ActiveState Software Inc are Copyright (C) 2008-2009
18+
* ActiveState Software Inc. All Rights Reserved.
19+
*
20+
* Contributor(s):
21+
* Todd Whiteman @ ActiveState Software Inc
22+
*
23+
* Alternatively, the contents of this file may be used under the terms of
24+
* either the GNU General Public License Version 2 or later (the "GPL"), or
25+
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26+
* in which case the provisions of the GPL or the LGPL are applicable instead
27+
* of those above. If you wish to allow use of your version of this file only
28+
* under the terms of either the GPL or the LGPL, and not to allow others to
29+
* use your version of this file under the terms of the MPL, indicate your
30+
* decision by deleting the provisions above and replace them with the notice
31+
* and other provisions required by the GPL or the LGPL. If you do not delete
32+
* the provisions above, a recipient may use your version of this file under
33+
* the terms of any one of the MPL, the GPL or the LGPL.
34+
*
35+
* ***** END LICENSE BLOCK ***** */
36+
37+
// Globals
38+
39+
var g_diff_result = null;
40+
var g_diff_cwd = null;
41+
var g_sbsDiff = null;
42+
43+
44+
// Overriding functionality - overrides the diff.js loadDiffResult function.
45+
46+
var _original_loadDiffResult = loadDiffResult;
47+
var loadDiffResult = function overwritten_loadDiffResult(result, cwd) {
48+
g_diff_result = result;
49+
g_diff_cwd = cwd;
50+
_original_loadDiffResult(result, cwd);
51+
loadSBSDiff();
52+
}
53+
54+
55+
// Side-by-side diff implementation.
56+
57+
function loadSBSDiff() {
58+
var koIDiff = Components.classes["@activestate.com/koDiff;1"].
59+
createInstance(Components.interfaces.koIDiff);
60+
koIDiff.initWithDiffContent(g_diff_result);
61+
g_sbsDiff = Components.classes["@activestate.com/sbsDiff;1"].
62+
createInstance(Components.interfaces.sbsIDiff)
63+
g_sbsDiff.enable_syntax_highlighting = document.getElementById('enable_highlighting_checkbox').checked;
64+
g_sbsDiff.cwd = g_diff_cwd;
65+
if (!g_diff_cwd) {
66+
// Cannot show full context diffs.
67+
document.getElementById('enable_highlighting_checkbox').setAttribute('disabled', 'true');
68+
}
69+
var html = g_sbsDiff.generateSbsDiff(koIDiff);
70+
71+
// Get the extension's on-disk location.
72+
var MY_ID = "[email protected]";
73+
var em = Components.classes["@mozilla.org/extensions/manager;1"].
74+
getService(Components.interfaces.nsIExtensionManager);
75+
var aFile = em.getInstallLocation(MY_ID).getItemFile(MY_ID, "content");
76+
aFile.append("diff.html");
77+
if (aFile.exists())
78+
aFile.remove(false);
79+
aFile.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0660);
80+
var stream = Components.classes["@mozilla.org/network/safe-file-output-stream;1"]
81+
.createInstance(Components.interfaces.nsIFileOutputStream);
82+
stream.init(aFile, 0x04 | 0x08 | 0x20, 0600, 0); // write, create, truncate
83+
stream.write(html, html.length);
84+
if (stream instanceof Components.interfaces.nsISafeOutputStream) {
85+
stream.finish();
86+
} else {
87+
stream.close();
88+
}
89+
90+
// do whatever you need to the created file
91+
//dump("file.path: " + ko.uriparse.localPathToURI(aFile.path) + "\n");
92+
var filepath = "chrome://sbsdiff/content/diff.html";
93+
// Set src to "", in order to clear any existing path (to reload itself).
94+
document.getElementById("sbs_diff_browser").setAttribute("src", "");
95+
document.getElementById("sbs_diff_browser").setAttribute("src", filepath);
96+
}
97+
98+
99+
function changeDiffStyle(style) {
100+
var deck = document.getElementById('deck');
101+
if (style == 'contextual') {
102+
deck.selectedIndex = 0;
103+
} else if (style == 'side-by-side') {
104+
deck.selectedIndex = 1;
105+
}
106+
}
107+
108+
function reloadDiffResult() {
109+
loadSBSDiff();
110+
}
111+
112+
function diffViewer_revealInEditor(menuitem, event) {
113+
//ko.logging.dumpEvent(event);
114+
//dump("\n\n");
115+
//ko.logging.dumpObject(document.popupNode);
116+
var name;
117+
var chunk_id;
118+
var lineno = 0;
119+
var node = document.popupNode;
120+
while (node) {
121+
if (node.nodeName.toLowerCase() == "tbody") {
122+
chunk_id = node.getAttribute("id");
123+
break;
124+
} else if ((lineno == 0) && (node.nodeName.toLowerCase() == "tr")) {
125+
for (var i=node.childNodes.length-1; i >=0; i--) {
126+
var child = node.childNodes[i];
127+
if (child.nodeName.toLowerCase() == "th") {
128+
lineno = parseInt(child.textContent);
129+
if (lineno > 0)
130+
break;
131+
}
132+
}
133+
}
134+
node = node.parentNode;
135+
}
136+
if (!chunk_id || (chunk_id.substr(0, 6) != "chunk.")) {
137+
alert("Unable to reveal the position at this location");
138+
return;
139+
}
140+
// We now know which file and which chunk. If we have a full-contextual
141+
// diff then we may know the line number as well.
142+
var filepath = g_sbsDiff.filepathFromChunkId(chunk_id);
143+
var uri = ko.uriparse.pathToURI(filepath);
144+
if (lineno <= 0 || !g_diff_cwd) {
145+
lineno = g_sbsDiff.diffLinenoFromChunkId(chunk_id);
146+
}
147+
var kowin = ko.windowManager.getMainWindow();
148+
kowin.ko.views.manager.doFileOpenAtLineAsync(uri, lineno, null,
149+
null, -1,
150+
function(v) {
151+
if (v) {
152+
kowin.focus();
153+
}
154+
});
155+
}
156+
157+
function sbsEnableUI() {
158+
var deck = document.getElementById('deck');
159+
if (deck.selectedIndex == 0) {
160+
document.getElementById('enable_highlighting_checkbox').setAttribute('disabled', true);
161+
} else if (g_diff_cwd) {
162+
document.getElementById('enable_highlighting_checkbox').removeAttribute('disabled');
163+
}
164+
165+
}
166+
167+
function sbsOnload() {
168+
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
169+
.getService(Components.interfaces.nsIPrefService);
170+
prefs = prefs.getBranch("extensions.sbsdiff.");
171+
var deck = document.getElementById('deck');
172+
deck.selectedIndex = prefs.getIntPref("deck.selectedIndex");
173+
document.getElementById("diff_style_menulist").selectedIndex = deck.selectedIndex;
174+
sbsEnableUI();
175+
}
176+
177+
function sbsOnunload() {
178+
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
179+
.getService(Components.interfaces.nsIPrefService);
180+
prefs = prefs.getBranch("extensions.sbsdiff.");
181+
var deck = document.getElementById('deck');
182+
prefs.setIntPref("deck.selectedIndex", deck.selectedIndex);
183+
}
184+
185+
window.addEventListener("load", sbsOnload, false);
186+
window.addEventListener("unload", sbsOnunload, false);

0 commit comments

Comments
 (0)