Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 Changes.ChangesView = class extends UI.VBox { | |
| 6 constructor() { | |
| 7 super(true); | |
| 8 this.registerRequiredCSS('changes/changesView.css'); | |
| 9 var splitWidget = new UI.SplitWidget(true, false); | |
|
lushnikov
2017/03/24 04:02:34
nit: lets' annotate booleans: true /* something */
einbinder
2017/03/28 00:07:24
Done.
| |
| 10 var mainWidget = new UI.Widget(); | |
| 11 splitWidget.setMainWidget(mainWidget); | |
| 12 splitWidget.show(this.contentElement); | |
| 13 var diffArea = mainWidget.element.createChild('div', 'diff-area'); | |
|
lushnikov
2017/03/24 04:02:33
you don't need diffArea
einbinder
2017/03/28 00:07:13
Done.
| |
| 14 | |
| 15 this._emptyWidget = new UI.EmptyWidget(Common.UIString('No changes')); | |
| 16 this._emptyWidget.show(diffArea); | |
| 17 | |
| 18 this._workspaceDiff = WorkspaceDiff.workspaceDiff(); | |
| 19 var uiSourceCodeList = new Changes.UISourceCodeList(this._workspaceDiff); | |
| 20 uiSourceCodeList.on(Changes.UISourceCodeList.SelectedUISourceCodeChanged, th is._selectedUISourceCodeChanged, this); | |
| 21 splitWidget.setSidebarWidget(uiSourceCodeList); | |
| 22 | |
| 23 /** @type {?Workspace.UISourceCode} */ | |
| 24 this._uiSourceCode = null; | |
|
lushnikov
2017/03/24 04:02:34
this._selectedUISourceCode = null;
einbinder
2017/03/28 00:07:24
Done.
| |
| 25 | |
| 26 /** @type {!Array<!Changes.ChangesView.Row>} */ | |
| 27 this._rows = []; | |
| 28 | |
| 29 this._maxLineDigits = 1; | |
| 30 | |
| 31 this._editor = | |
| 32 new TextEditor.CodeMirrorTextEditor({lineNumbers: true, lineWrapping: fa lse, maxHighlightLength: Infinity}); | |
|
lushnikov
2017/03/24 04:02:33
let's put a comment why we're killing maxHighlight
einbinder
2017/03/28 00:07:24
Done.
| |
| 33 this._editor.setReadOnly(true); | |
| 34 this._editor.show(diffArea); | |
| 35 this._editor.hideWidget(); | |
| 36 | |
| 37 this._editor.element.addEventListener('click', this._click.bind(this), false ); | |
| 38 | |
| 39 this._toolbar = new UI.Toolbar('changes-toolbar', mainWidget.element); | |
| 40 var revertButton = new UI.ToolbarButton(Common.UIString('Revert all changes' ), 'largeicon-undo'); | |
| 41 revertButton.addEventListener(UI.ToolbarButton.Events.Click, this._revert.bi nd(this)); | |
| 42 this._toolbar.appendToolbarItem(revertButton); | |
| 43 this._diffStats = new UI.ToolbarText(''); | |
| 44 this._toolbar.appendToolbarItem(this._diffStats); | |
| 45 this._toolbar.setEnabled(false); | |
| 46 } | |
| 47 | |
| 48 /** | |
| 49 * @param {!Changes.UISourceCodeList.SelectedUISourceCodeChanged} event | |
| 50 */ | |
| 51 _selectedUISourceCodeChanged(event) { | |
| 52 this._revealUISourceCode(event.uiSourceCode); | |
| 53 } | |
| 54 | |
| 55 _revert() { | |
| 56 var uiSourceCode = this._uiSourceCode; | |
| 57 if (!uiSourceCode) | |
| 58 return; | |
| 59 uiSourceCode.requestOriginalContent().then(original => uiSourceCode.addRevis ion(original || '')); | |
| 60 } | |
| 61 | |
| 62 _click(event) { | |
|
lushnikov
2017/03/24 04:02:34
jsdoc
einbinder
2017/03/28 00:07:17
Done.
| |
| 63 var selection = this._editor.selection(); | |
| 64 if (!selection.isEmpty()) | |
| 65 return; | |
| 66 var row = this._rows[selection.startLine]; | |
| 67 if (!row) | |
|
lushnikov
2017/03/24 04:02:33
let's typecast rather then fast-return
einbinder
2017/03/28 00:07:24
Removed.
| |
| 68 return; | |
| 69 Common.Revealer.reveal(this._uiSourceCode.uiLocation(row.current - 1, select ion.startColumn), false); | |
|
lushnikov
2017/03/24 04:02:33
how come you need to "-1"?
einbinder
2017/03/28 00:07:14
row.current is the displayed line number, 1-indexe
| |
| 70 event.consume(true); | |
| 71 } | |
| 72 | |
| 73 /** | |
| 74 * @param {?Workspace.UISourceCode} uiSourceCode | |
| 75 */ | |
| 76 _revealUISourceCode(uiSourceCode) { | |
| 77 if (this._uiSourceCode === uiSourceCode) | |
| 78 return; | |
| 79 | |
| 80 if (this._uiSourceCode) | |
| 81 this._workspaceDiff.unsubscribeFromDiffChange(this._uiSourceCode, this._re freshDiff, this); | |
| 82 if (uiSourceCode && this.isShowing()) | |
| 83 this._workspaceDiff.subscribeToDiffChange(uiSourceCode, this._refreshDiff, this); | |
| 84 | |
| 85 this._uiSourceCode = uiSourceCode; | |
| 86 if (!uiSourceCode || this.isShowing()) | |
|
lushnikov
2017/03/24 04:02:33
let's kill this: !uiSourceCode ||
einbinder
2017/03/28 00:07:24
Done.
| |
| 87 this._refreshDiff(); | |
| 88 } | |
| 89 | |
| 90 /** | |
| 91 * @override | |
| 92 */ | |
| 93 wasShown() { | |
| 94 if (!this._uiSourceCode) | |
|
lushnikov
2017/03/24 04:02:33
consider using ThrottledWidget
einbinder
2017/03/28 00:07:23
Not needed.
| |
| 95 return; | |
| 96 this._workspaceDiff.subscribeToDiffChange(this._uiSourceCode, this._refreshD iff, this); | |
|
lushnikov
2017/03/24 04:02:34
let's stay subscribed
einbinder
2017/03/28 00:07:24
Done.
| |
| 97 this._refreshDiff(); | |
| 98 } | |
| 99 | |
| 100 /** | |
| 101 * @override | |
| 102 */ | |
| 103 willHide() { | |
| 104 if (this._uiSourceCode) | |
| 105 this._workspaceDiff.unsubscribeFromDiffChange(this._uiSourceCode, this._re freshDiff, this); | |
| 106 } | |
| 107 | |
| 108 _refreshDiff() { | |
| 109 if (!this._uiSourceCode) { | |
|
lushnikov
2017/03/24 04:02:34
you want to fast-return here if widget is not show
einbinder
2017/03/28 00:07:24
Yep
| |
| 110 this._renderRows(null); | |
| 111 return; | |
| 112 } | |
| 113 var uiSourceCode = this._uiSourceCode; | |
| 114 this._workspaceDiff.requestDiff(uiSourceCode).then(diff => { | |
| 115 if (this._uiSourceCode !== uiSourceCode) | |
| 116 return; | |
| 117 this._renderRows(diff); | |
| 118 }); | |
| 119 } | |
| 120 | |
| 121 /** | |
| 122 * @param {?Diff.Diff.DiffArray} diff | |
| 123 */ | |
| 124 _renderRows(diff) { | |
| 125 this._rows = []; | |
| 126 | |
| 127 if (!diff || (diff.length === 1 && diff[0][0] === Diff.Diff.Operation.Equal) ) { | |
| 128 this._diffStats.setText(''); | |
| 129 this._toolbar.setEnabled(false); | |
| 130 this._editor.hideWidget(); | |
| 131 this._emptyWidget.showWidget(); | |
| 132 return; | |
| 133 } | |
| 134 | |
| 135 var insertions = 0; | |
| 136 var deletions = 0; | |
| 137 var currentLineNumber = 0; | |
| 138 var baselineLineNumber = 0; | |
| 139 var paddingLines = 3; | |
| 140 var originalLines = []; | |
| 141 var currentLines = []; | |
| 142 | |
| 143 for (var i = 0; i < diff.length; ++i) { | |
| 144 var token = diff[i]; | |
| 145 switch (token[0]) { | |
| 146 case Diff.Diff.Operation.Equal: | |
| 147 this._rows.pushAll(createEqualRows(token[1], i === 0, i === diff.lengt h - 1)); | |
| 148 originalLines.pushAll(token[1]); | |
| 149 currentLines.pushAll(token[1]); | |
|
lushnikov
2017/03/24 04:02:33
let's manage baselineLineNumber/currentLineNubmer/
einbinder
2017/03/28 00:07:23
Done.
| |
| 150 break; | |
| 151 case Diff.Diff.Operation.Insert: | |
| 152 for (var line of token[1]) | |
| 153 this._rows.push(createRow(line, 'addition')); | |
| 154 currentLines.pushAll(token[1]); | |
| 155 break; | |
| 156 case Diff.Diff.Operation.Delete: | |
| 157 originalLines.pushAll(token[1]); | |
| 158 if (diff[i + 1] && diff[i + 1][0] === Diff.Diff.Operation.Insert) { | |
| 159 i++; | |
| 160 this._rows.pushAll(createModifyRows(token[1].join('\n'), diff[i][1]. join('\n'))); | |
| 161 currentLines.pushAll(diff[i][1]); | |
| 162 } else { | |
| 163 for (var line of token[1]) | |
| 164 this._rows.push(createRow(line, 'deletion')); | |
| 165 } | |
| 166 break; | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 this._maxLineDigits = Math.max(Math.ceil(Math.log10(Math.max(currentLineNumb er, baselineLineNumber))), 1); | |
|
lushnikov
2017/03/24 04:02:33
let's drop the "1" in the end
einbinder
2017/03/28 00:07:12
Done.
| |
| 171 | |
| 172 this._editor.operation(() => { | |
| 173 this._editor.setHighlightMode({ | |
| 174 name: 'devtools-diff', | |
| 175 rows: this._rows, | |
| 176 mimeType: | |
| 177 Bindings.NetworkProject.uiSourceCodeMimeType(/** @type {!Workspace.U ISourceCode} */ (this._uiSourceCode)), | |
| 178 baselineLines: originalLines, | |
| 179 currentLines: currentLines | |
| 180 }); | |
| 181 this._editor.setText(this._rows.map(row => row.content.map(t => t.text).jo in('')).join('\n')); | |
|
lushnikov
2017/03/24 04:02:34
consider using [].reduce?
einbinder
2017/03/28 00:07:15
Still looks ugly.
| |
| 182 this._editor.setLineNumberFormatter(this._lineFormatter.bind(this)); | |
| 183 }); | |
| 184 | |
| 185 this._diffStats.setText(Common.UIString( | |
| 186 '%d insertion%s (+), %d deletion%s (-)', insertions, insertions > 1 ? 's ' : '', deletions, | |
|
lushnikov
2017/03/24 04:02:34
plurals!
einbinder
2017/03/28 00:07:12
Done.
| |
| 187 deletions > 1 ? 's' : '')); | |
| 188 this._toolbar.setEnabled(true); | |
| 189 this._emptyWidget.hideWidget(); | |
| 190 this._editor.showWidget(); | |
| 191 | |
| 192 /** | |
| 193 * @param {!Array<string>} lines | |
| 194 * @param {boolean} atStart | |
| 195 * @param {boolean} atEnd | |
| 196 * @return {!Array<!Changes.ChangesView.Row>}} | |
| 197 */ | |
| 198 function createEqualRows(lines, atStart, atEnd) { | |
| 199 var equalRows = []; | |
| 200 if (!atStart) { | |
| 201 for (var i = 0; i < paddingLines && i < lines.length; i++) | |
| 202 equalRows.push(createRow(lines[i], 'equal')); | |
| 203 if (lines.length > paddingLines * 2 + 1 && !atEnd) { | |
| 204 equalRows.push(createRow( | |
| 205 Common.UIString('( \u2026 Skipping ') + (lines.length - paddingLin es * 2) + | |
| 206 Common.UIString(' matching lines \u2026 )'), | |
| 207 'spacer')); | |
| 208 } | |
| 209 } | |
| 210 if (!atEnd) { | |
| 211 var start = Math.max(lines.length - paddingLines - 1, atStart ? 0 : padd ingLines); | |
|
lushnikov
2017/03/24 04:02:33
let's just increment |index| as you push rows in t
| |
| 212 var skip = lines.length - paddingLines - 1; | |
| 213 if (!atStart) | |
| 214 skip -= paddingLines; | |
| 215 if (skip > 0) { | |
| 216 baselineLineNumber += skip; | |
| 217 currentLineNumber += skip; | |
| 218 } | |
| 219 | |
| 220 for (var i = start; i < lines.length; i++) | |
|
lushnikov
2017/03/24 04:02:33
let's make this function to populate two different
| |
| 221 equalRows.push(createRow(lines[i], 'equal')); | |
| 222 } | |
| 223 return equalRows; | |
| 224 } | |
| 225 | |
| 226 /** | |
| 227 * @param {string} before | |
| 228 * @param {string} after | |
| 229 * @return {!Array<!Changes.ChangesView.Row>}} | |
| 230 */ | |
| 231 function createModifyRows(before, after) { | |
| 232 var internalDiff = Diff.Diff.charDiff(before, after, true); | |
|
lushnikov
2017/03/24 04:02:33
nit: let's annotate "true"
it feels scary to do d
einbinder
2017/03/28 00:07:12
Done.
| |
| 233 var deletionRows = [createRow('', 'deletion')]; | |
| 234 var insertionRows = [createRow('', 'addition')]; | |
| 235 | |
| 236 for (var token of internalDiff) { | |
| 237 var text = token[1]; | |
| 238 var type = token[0]; | |
| 239 var className = type === Diff.Diff.Operation.Equal ? '' : 'double'; | |
|
lushnikov
2017/03/24 04:02:34
s/double/inner-diff/
| |
| 240 var lines = text.split('\n'); | |
| 241 for (var i = 0; i < lines.length; i++) { | |
| 242 if (i > 0 && type !== Diff.Diff.Operation.Insert) | |
| 243 deletionRows.push(createRow('', 'deletion')); | |
| 244 if (i > 0 && type !== Diff.Diff.Operation.Delete) | |
| 245 insertionRows.push(createRow('', 'addition')); | |
| 246 if (!lines[i]) | |
| 247 continue; | |
| 248 if (type !== Diff.Diff.Operation.Insert) | |
| 249 deletionRows[deletionRows.length - 1].content.push({text: lines[i], className: className}); | |
| 250 if (type !== Diff.Diff.Operation.Delete) | |
| 251 insertionRows[insertionRows.length - 1].content.push({text: lines[i] , className: className}); | |
| 252 } | |
| 253 } | |
| 254 return deletionRows.concat(insertionRows); | |
| 255 } | |
| 256 | |
| 257 /** | |
| 258 * @param {string} text | |
| 259 * @param {string} className | |
| 260 * @return {!Changes.ChangesView.Row} | |
| 261 */ | |
| 262 function createRow(text, className) { | |
| 263 if (className === 'addition') { | |
| 264 currentLineNumber++; | |
| 265 insertions++; | |
| 266 } | |
| 267 if (className === 'deletion') { | |
| 268 baselineLineNumber++; | |
| 269 deletions++; | |
| 270 } | |
| 271 if (className === 'equal') { | |
| 272 baselineLineNumber++; | |
| 273 currentLineNumber++; | |
| 274 } | |
| 275 return { | |
| 276 base: baselineLineNumber, | |
| 277 current: currentLineNumber, | |
| 278 content: text ? [{text: text, className: 'double'}] : [], | |
|
lushnikov
2017/03/24 04:02:33
s/double/inline-diff/
einbinder
2017/03/28 00:07:24
done.
| |
| 279 className: className, | |
| 280 loc: currentLineNumber | |
|
lushnikov
2017/03/24 04:02:33
drop loc
einbinder
2017/03/28 00:07:12
Done.
| |
| 281 }; | |
| 282 } | |
| 283 } | |
| 284 | |
| 285 /** | |
| 286 * @param {number} lineNumber | |
| 287 * @return {string} | |
| 288 */ | |
| 289 _lineFormatter(lineNumber) { | |
| 290 var row = this._rows[lineNumber - 1]; | |
| 291 if (!row) | |
| 292 return spacesPadding(this._maxLineDigits * 2 + 1); | |
|
lushnikov
2017/03/24 04:02:33
this is not needed
einbinder
2017/03/28 00:07:12
Done.
| |
| 293 var showBaseNumber = row.className === 'deletion'; | |
| 294 var showCurrentNumber = row.className === 'addition'; | |
| 295 if (row.className === 'equal') { | |
|
lushnikov
2017/03/24 04:02:34
can we have row.className as an enum?
einbinder
2017/03/28 00:07:12
Done.
| |
| 296 showBaseNumber = true; | |
| 297 showCurrentNumber = true; | |
| 298 } | |
| 299 var base = showBaseNumber ? numberToStringWithSpacesPadding(row.base, this._ maxLineDigits) : | |
| 300 spacesPadding(this._maxLineDigits); | |
| 301 var current = showCurrentNumber ? numberToStringWithSpacesPadding(row.curren t, this._maxLineDigits) : | |
| 302 spacesPadding(this._maxLineDigits); | |
| 303 return base + spacesPadding(1) + current; | |
| 304 } | |
| 305 }; | |
| 306 | |
| 307 /** @typedef {!{base: number, current: number, content: !Array<!{text: string, c lassName: string}>, className: string, loc: number}} */ | |
|
lushnikov
2017/03/24 04:02:33
baseLineNumber
lineLevelClassName
loc???
einbinder
2017/03/28 00:07:24
Done. I used type instaed of lineLevelClassName
| |
| 308 Changes.ChangesView.Row; | |
| OLD | NEW |