-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhunk-element.html
More file actions
127 lines (110 loc) · 3.93 KB
/
hunk-element.html
File metadata and controls
127 lines (110 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!--
@license
Copyright (c) 2017 Preview-Code. All rights reserved.
This code may only be used under the BSD style license found in LICENSE.txt
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="code-with-comment.html">
<!--
Syntax highlighting for textual diffs.
@demo demo/index.html
-->
<dom-module id="hunk-element">
<template>
<style>
:host {
display: block;
}
</style>
<template is="dom-repeat" items="{{_chunks}}">
<code-with-comment on-add-chunk-with-comment="_insertChunk" code="{{item.code}}" before-start="{{item.beforeStart}}" after-start="{{item.afterStart}}"
comments="{{item.comments}}" _onHunkChange hide-comments="[[hideComments]]" start-line="{{item.startLine}}" project="[[project]]"
hide-add-comments="{{item.hideAddComments}}" language="[[language]]"></code-with-comment>
</template>
</template>
<script>
Polymer({
is: 'hunk-element',
observers: ['_onHunkChange(hunk.*)'],
properties: {
hunk: Object,
hideComments: Boolean,
project: Object,
language: String,
_chunks: {
type: Array,
}
},
_insertChunk: function (e) {
e.detail.comments = [];
e.detail.hideAddComments = false;
this.splice('_chunks', e.model.index, 0, e.detail);
},
_sort: function (comments) {
var compare = function (a, b) {
if (a.position < b.position)
return -1;
if (a.position > b.position)
return 1;
if (a.created_at > b.created_at)
return 1;
return 0;
}
return comments.sort(compare);
},
_onHunkChange: function (hunkObj) {
this._chunks = [];
var hunk = hunkObj.base;
var hunkComments = this._sort(hunk.comments.slice(0))
.filter(function (comment) {
return comment.position >= hunk.startLine
});
//There are no comments for this hunk
if (!hunkComments || hunkComments.length === 0) {
hunk.comments = [];
hunk.hideAddComments = true;
this._chunks = [hunk];
return;
}
var code = hunk.code.split('\n');
var beforeStart = hunk.beforeStart;
var afterStart = hunk.afterStart;
var newBeforeStart = hunk.beforeStart;
var newAfterStart = hunk.afterStart;
var oldChunk = 0;
var comments = [];
code.forEach(function (line, index) {
if (!line.startsWith('+')) newBeforeStart++;
if (!line.startsWith('-')) newAfterStart++;
while (hunkComments.length && (index + Number(hunk.startLine)) === hunkComments[0].position) {
comments.push(hunkComments[0]);
hunkComments.shift();
}
if (comments.length > 0) {
var newCode = code.slice(oldChunk, index + 1).join('\n');
this.push('_chunks', {
beforeStart: beforeStart, afterStart: afterStart, code: newCode,
comments: comments, startLine: hunk.startLine + oldChunk, hideAddComments: true
});
comments = [];
beforeStart = newBeforeStart;
afterStart = newAfterStart;
oldChunk = index + 1;
if (hunkComments.length === 0) {
return;
}
}
}.bind(this));
//If there is still code left after the last comment make a chunk for this.
//We should ignore if that is a line we later ommit.
if (code[oldChunk] && !(oldChunk + 1 === code.length && code[code.length - 1].startsWith('\\'))) {
var newCode = code.slice(oldChunk, code.length).join('\n');
this.push('_chunks', {
beforeStart: beforeStart, afterStart: afterStart, code: newCode,
comments: [], startLine: hunk.startLine + oldChunk, hideAddComments: true
});
}
}
})
</script>
</dom-module>