diffReview.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  6. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  7. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  8. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  9. return c > 3 && r && Object.defineProperty(target, key, r), r;
  10. };
  11. var __param = (this && this.__param) || function (paramIndex, decorator) {
  12. return function (target, key) { decorator(target, key, paramIndex); }
  13. };
  14. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  15. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  16. return new (P || (P = Promise))(function (resolve, reject) {
  17. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  18. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  19. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  20. step((generator = generator.apply(thisArg, _arguments || [])).next());
  21. });
  22. };
  23. var _a;
  24. import './media/diffReview.css';
  25. import * as nls from '../../../nls.js';
  26. import * as dom from '../../../base/browser/dom.js';
  27. import { createFastDomNode } from '../../../base/browser/fastDomNode.js';
  28. import { ActionBar } from '../../../base/browser/ui/actionbar/actionbar.js';
  29. import { DomScrollableElement } from '../../../base/browser/ui/scrollbar/scrollableElement.js';
  30. import { Action } from '../../../base/common/actions.js';
  31. import { Disposable } from '../../../base/common/lifecycle.js';
  32. import { Configuration } from '../config/configuration.js';
  33. import { EditorAction, registerEditorAction } from '../editorExtensions.js';
  34. import { ICodeEditorService } from '../services/codeEditorService.js';
  35. import { EditorFontLigatures } from '../../common/config/editorOptions.js';
  36. import { LineTokens } from '../../common/core/lineTokens.js';
  37. import { Position } from '../../common/core/position.js';
  38. import { editorLineNumbers } from '../../common/view/editorColorRegistry.js';
  39. import { RenderLineInput, renderViewLine2 as renderViewLine } from '../../common/viewLayout/viewLineRenderer.js';
  40. import { ViewLineRenderingData } from '../../common/viewModel/viewModel.js';
  41. import { ContextKeyExpr } from '../../../platform/contextkey/common/contextkey.js';
  42. import { scrollbarShadow } from '../../../platform/theme/common/colorRegistry.js';
  43. import { registerThemingParticipant, ThemeIcon } from '../../../platform/theme/common/themeService.js';
  44. import { Codicon } from '../../../base/common/codicons.js';
  45. import { registerIcon } from '../../../platform/theme/common/iconRegistry.js';
  46. import { IModeService } from '../../common/services/modeService.js';
  47. const DIFF_LINES_PADDING = 3;
  48. class DiffEntry {
  49. constructor(originalLineStart, originalLineEnd, modifiedLineStart, modifiedLineEnd) {
  50. this.originalLineStart = originalLineStart;
  51. this.originalLineEnd = originalLineEnd;
  52. this.modifiedLineStart = modifiedLineStart;
  53. this.modifiedLineEnd = modifiedLineEnd;
  54. }
  55. getType() {
  56. if (this.originalLineStart === 0) {
  57. return 1 /* Insert */;
  58. }
  59. if (this.modifiedLineStart === 0) {
  60. return 2 /* Delete */;
  61. }
  62. return 0 /* Equal */;
  63. }
  64. }
  65. class Diff {
  66. constructor(entries) {
  67. this.entries = entries;
  68. }
  69. }
  70. const diffReviewInsertIcon = registerIcon('diff-review-insert', Codicon.add, nls.localize('diffReviewInsertIcon', 'Icon for \'Insert\' in diff review.'));
  71. const diffReviewRemoveIcon = registerIcon('diff-review-remove', Codicon.remove, nls.localize('diffReviewRemoveIcon', 'Icon for \'Remove\' in diff review.'));
  72. const diffReviewCloseIcon = registerIcon('diff-review-close', Codicon.close, nls.localize('diffReviewCloseIcon', 'Icon for \'Close\' in diff review.'));
  73. let DiffReview = class DiffReview extends Disposable {
  74. constructor(diffEditor, _modeService) {
  75. super();
  76. this._modeService = _modeService;
  77. this._width = 0;
  78. this._diffEditor = diffEditor;
  79. this._isVisible = false;
  80. this.shadow = createFastDomNode(document.createElement('div'));
  81. this.shadow.setClassName('diff-review-shadow');
  82. this.actionBarContainer = createFastDomNode(document.createElement('div'));
  83. this.actionBarContainer.setClassName('diff-review-actions');
  84. this._actionBar = this._register(new ActionBar(this.actionBarContainer.domNode));
  85. this._actionBar.push(new Action('diffreview.close', nls.localize('label.close', "Close"), 'close-diff-review ' + ThemeIcon.asClassName(diffReviewCloseIcon), true, () => __awaiter(this, void 0, void 0, function* () { return this.hide(); })), { label: false, icon: true });
  86. this.domNode = createFastDomNode(document.createElement('div'));
  87. this.domNode.setClassName('diff-review monaco-editor-background');
  88. this._content = createFastDomNode(document.createElement('div'));
  89. this._content.setClassName('diff-review-content');
  90. this._content.setAttribute('role', 'code');
  91. this.scrollbar = this._register(new DomScrollableElement(this._content.domNode, {}));
  92. this.domNode.domNode.appendChild(this.scrollbar.getDomNode());
  93. this._register(diffEditor.onDidUpdateDiff(() => {
  94. if (!this._isVisible) {
  95. return;
  96. }
  97. this._diffs = this._compute();
  98. this._render();
  99. }));
  100. this._register(diffEditor.getModifiedEditor().onDidChangeCursorPosition(() => {
  101. if (!this._isVisible) {
  102. return;
  103. }
  104. this._render();
  105. }));
  106. this._register(dom.addStandardDisposableListener(this.domNode.domNode, 'click', (e) => {
  107. e.preventDefault();
  108. let row = dom.findParentWithClass(e.target, 'diff-review-row');
  109. if (row) {
  110. this._goToRow(row);
  111. }
  112. }));
  113. this._register(dom.addStandardDisposableListener(this.domNode.domNode, 'keydown', (e) => {
  114. if (e.equals(18 /* DownArrow */)
  115. || e.equals(2048 /* CtrlCmd */ | 18 /* DownArrow */)
  116. || e.equals(512 /* Alt */ | 18 /* DownArrow */)) {
  117. e.preventDefault();
  118. this._goToRow(this._getNextRow());
  119. }
  120. if (e.equals(16 /* UpArrow */)
  121. || e.equals(2048 /* CtrlCmd */ | 16 /* UpArrow */)
  122. || e.equals(512 /* Alt */ | 16 /* UpArrow */)) {
  123. e.preventDefault();
  124. this._goToRow(this._getPrevRow());
  125. }
  126. if (e.equals(9 /* Escape */)
  127. || e.equals(2048 /* CtrlCmd */ | 9 /* Escape */)
  128. || e.equals(512 /* Alt */ | 9 /* Escape */)
  129. || e.equals(1024 /* Shift */ | 9 /* Escape */)) {
  130. e.preventDefault();
  131. this.hide();
  132. }
  133. if (e.equals(10 /* Space */)
  134. || e.equals(3 /* Enter */)) {
  135. e.preventDefault();
  136. this.accept();
  137. }
  138. }));
  139. this._diffs = [];
  140. this._currentDiff = null;
  141. }
  142. prev() {
  143. let index = 0;
  144. if (!this._isVisible) {
  145. this._diffs = this._compute();
  146. }
  147. if (this._isVisible) {
  148. let currentIndex = -1;
  149. for (let i = 0, len = this._diffs.length; i < len; i++) {
  150. if (this._diffs[i] === this._currentDiff) {
  151. currentIndex = i;
  152. break;
  153. }
  154. }
  155. index = (this._diffs.length + currentIndex - 1);
  156. }
  157. else {
  158. index = this._findDiffIndex(this._diffEditor.getPosition());
  159. }
  160. if (this._diffs.length === 0) {
  161. // Nothing to do
  162. return;
  163. }
  164. index = index % this._diffs.length;
  165. const entries = this._diffs[index].entries;
  166. this._diffEditor.setPosition(new Position(entries[0].modifiedLineStart, 1));
  167. this._diffEditor.setSelection({ startColumn: 1, startLineNumber: entries[0].modifiedLineStart, endColumn: 1073741824 /* MAX_SAFE_SMALL_INTEGER */, endLineNumber: entries[entries.length - 1].modifiedLineEnd });
  168. this._isVisible = true;
  169. this._diffEditor.doLayout();
  170. this._render();
  171. this._goToRow(this._getNextRow());
  172. }
  173. next() {
  174. let index = 0;
  175. if (!this._isVisible) {
  176. this._diffs = this._compute();
  177. }
  178. if (this._isVisible) {
  179. let currentIndex = -1;
  180. for (let i = 0, len = this._diffs.length; i < len; i++) {
  181. if (this._diffs[i] === this._currentDiff) {
  182. currentIndex = i;
  183. break;
  184. }
  185. }
  186. index = (currentIndex + 1);
  187. }
  188. else {
  189. index = this._findDiffIndex(this._diffEditor.getPosition());
  190. }
  191. if (this._diffs.length === 0) {
  192. // Nothing to do
  193. return;
  194. }
  195. index = index % this._diffs.length;
  196. const entries = this._diffs[index].entries;
  197. this._diffEditor.setPosition(new Position(entries[0].modifiedLineStart, 1));
  198. this._diffEditor.setSelection({ startColumn: 1, startLineNumber: entries[0].modifiedLineStart, endColumn: 1073741824 /* MAX_SAFE_SMALL_INTEGER */, endLineNumber: entries[entries.length - 1].modifiedLineEnd });
  199. this._isVisible = true;
  200. this._diffEditor.doLayout();
  201. this._render();
  202. this._goToRow(this._getNextRow());
  203. }
  204. accept() {
  205. let jumpToLineNumber = -1;
  206. let current = this._getCurrentFocusedRow();
  207. if (current) {
  208. let lineNumber = parseInt(current.getAttribute('data-line'), 10);
  209. if (!isNaN(lineNumber)) {
  210. jumpToLineNumber = lineNumber;
  211. }
  212. }
  213. this.hide();
  214. if (jumpToLineNumber !== -1) {
  215. this._diffEditor.setPosition(new Position(jumpToLineNumber, 1));
  216. this._diffEditor.revealPosition(new Position(jumpToLineNumber, 1), 1 /* Immediate */);
  217. }
  218. }
  219. hide() {
  220. this._isVisible = false;
  221. this._diffEditor.updateOptions({ readOnly: false });
  222. this._diffEditor.focus();
  223. this._diffEditor.doLayout();
  224. this._render();
  225. }
  226. _getPrevRow() {
  227. let current = this._getCurrentFocusedRow();
  228. if (!current) {
  229. return this._getFirstRow();
  230. }
  231. if (current.previousElementSibling) {
  232. return current.previousElementSibling;
  233. }
  234. return current;
  235. }
  236. _getNextRow() {
  237. let current = this._getCurrentFocusedRow();
  238. if (!current) {
  239. return this._getFirstRow();
  240. }
  241. if (current.nextElementSibling) {
  242. return current.nextElementSibling;
  243. }
  244. return current;
  245. }
  246. _getFirstRow() {
  247. return this.domNode.domNode.querySelector('.diff-review-row');
  248. }
  249. _getCurrentFocusedRow() {
  250. let result = document.activeElement;
  251. if (result && /diff-review-row/.test(result.className)) {
  252. return result;
  253. }
  254. return null;
  255. }
  256. _goToRow(row) {
  257. let prev = this._getCurrentFocusedRow();
  258. row.tabIndex = 0;
  259. row.focus();
  260. if (prev && prev !== row) {
  261. prev.tabIndex = -1;
  262. }
  263. this.scrollbar.scanDomNode();
  264. }
  265. isVisible() {
  266. return this._isVisible;
  267. }
  268. layout(top, width, height) {
  269. this._width = width;
  270. this.shadow.setTop(top - 6);
  271. this.shadow.setWidth(width);
  272. this.shadow.setHeight(this._isVisible ? 6 : 0);
  273. this.domNode.setTop(top);
  274. this.domNode.setWidth(width);
  275. this.domNode.setHeight(height);
  276. this._content.setHeight(height);
  277. this._content.setWidth(width);
  278. if (this._isVisible) {
  279. this.actionBarContainer.setAttribute('aria-hidden', 'false');
  280. this.actionBarContainer.setDisplay('block');
  281. }
  282. else {
  283. this.actionBarContainer.setAttribute('aria-hidden', 'true');
  284. this.actionBarContainer.setDisplay('none');
  285. }
  286. }
  287. _compute() {
  288. const lineChanges = this._diffEditor.getLineChanges();
  289. if (!lineChanges || lineChanges.length === 0) {
  290. return [];
  291. }
  292. const originalModel = this._diffEditor.getOriginalEditor().getModel();
  293. const modifiedModel = this._diffEditor.getModifiedEditor().getModel();
  294. if (!originalModel || !modifiedModel) {
  295. return [];
  296. }
  297. return DiffReview._mergeAdjacent(lineChanges, originalModel.getLineCount(), modifiedModel.getLineCount());
  298. }
  299. static _mergeAdjacent(lineChanges, originalLineCount, modifiedLineCount) {
  300. if (!lineChanges || lineChanges.length === 0) {
  301. return [];
  302. }
  303. let diffs = [], diffsLength = 0;
  304. for (let i = 0, len = lineChanges.length; i < len; i++) {
  305. const lineChange = lineChanges[i];
  306. const originalStart = lineChange.originalStartLineNumber;
  307. const originalEnd = lineChange.originalEndLineNumber;
  308. const modifiedStart = lineChange.modifiedStartLineNumber;
  309. const modifiedEnd = lineChange.modifiedEndLineNumber;
  310. let r = [], rLength = 0;
  311. // Emit before anchors
  312. {
  313. const originalEqualAbove = (originalEnd === 0 ? originalStart : originalStart - 1);
  314. const modifiedEqualAbove = (modifiedEnd === 0 ? modifiedStart : modifiedStart - 1);
  315. // Make sure we don't step into the previous diff
  316. let minOriginal = 1;
  317. let minModified = 1;
  318. if (i > 0) {
  319. const prevLineChange = lineChanges[i - 1];
  320. if (prevLineChange.originalEndLineNumber === 0) {
  321. minOriginal = prevLineChange.originalStartLineNumber + 1;
  322. }
  323. else {
  324. minOriginal = prevLineChange.originalEndLineNumber + 1;
  325. }
  326. if (prevLineChange.modifiedEndLineNumber === 0) {
  327. minModified = prevLineChange.modifiedStartLineNumber + 1;
  328. }
  329. else {
  330. minModified = prevLineChange.modifiedEndLineNumber + 1;
  331. }
  332. }
  333. let fromOriginal = originalEqualAbove - DIFF_LINES_PADDING + 1;
  334. let fromModified = modifiedEqualAbove - DIFF_LINES_PADDING + 1;
  335. if (fromOriginal < minOriginal) {
  336. const delta = minOriginal - fromOriginal;
  337. fromOriginal = fromOriginal + delta;
  338. fromModified = fromModified + delta;
  339. }
  340. if (fromModified < minModified) {
  341. const delta = minModified - fromModified;
  342. fromOriginal = fromOriginal + delta;
  343. fromModified = fromModified + delta;
  344. }
  345. r[rLength++] = new DiffEntry(fromOriginal, originalEqualAbove, fromModified, modifiedEqualAbove);
  346. }
  347. // Emit deleted lines
  348. {
  349. if (originalEnd !== 0) {
  350. r[rLength++] = new DiffEntry(originalStart, originalEnd, 0, 0);
  351. }
  352. }
  353. // Emit inserted lines
  354. {
  355. if (modifiedEnd !== 0) {
  356. r[rLength++] = new DiffEntry(0, 0, modifiedStart, modifiedEnd);
  357. }
  358. }
  359. // Emit after anchors
  360. {
  361. const originalEqualBelow = (originalEnd === 0 ? originalStart + 1 : originalEnd + 1);
  362. const modifiedEqualBelow = (modifiedEnd === 0 ? modifiedStart + 1 : modifiedEnd + 1);
  363. // Make sure we don't step into the next diff
  364. let maxOriginal = originalLineCount;
  365. let maxModified = modifiedLineCount;
  366. if (i + 1 < len) {
  367. const nextLineChange = lineChanges[i + 1];
  368. if (nextLineChange.originalEndLineNumber === 0) {
  369. maxOriginal = nextLineChange.originalStartLineNumber;
  370. }
  371. else {
  372. maxOriginal = nextLineChange.originalStartLineNumber - 1;
  373. }
  374. if (nextLineChange.modifiedEndLineNumber === 0) {
  375. maxModified = nextLineChange.modifiedStartLineNumber;
  376. }
  377. else {
  378. maxModified = nextLineChange.modifiedStartLineNumber - 1;
  379. }
  380. }
  381. let toOriginal = originalEqualBelow + DIFF_LINES_PADDING - 1;
  382. let toModified = modifiedEqualBelow + DIFF_LINES_PADDING - 1;
  383. if (toOriginal > maxOriginal) {
  384. const delta = maxOriginal - toOriginal;
  385. toOriginal = toOriginal + delta;
  386. toModified = toModified + delta;
  387. }
  388. if (toModified > maxModified) {
  389. const delta = maxModified - toModified;
  390. toOriginal = toOriginal + delta;
  391. toModified = toModified + delta;
  392. }
  393. r[rLength++] = new DiffEntry(originalEqualBelow, toOriginal, modifiedEqualBelow, toModified);
  394. }
  395. diffs[diffsLength++] = new Diff(r);
  396. }
  397. // Merge adjacent diffs
  398. let curr = diffs[0].entries;
  399. let r = [], rLength = 0;
  400. for (let i = 1, len = diffs.length; i < len; i++) {
  401. const thisDiff = diffs[i].entries;
  402. const currLast = curr[curr.length - 1];
  403. const thisFirst = thisDiff[0];
  404. if (currLast.getType() === 0 /* Equal */
  405. && thisFirst.getType() === 0 /* Equal */
  406. && thisFirst.originalLineStart <= currLast.originalLineEnd) {
  407. // We are dealing with equal lines that overlap
  408. curr[curr.length - 1] = new DiffEntry(currLast.originalLineStart, thisFirst.originalLineEnd, currLast.modifiedLineStart, thisFirst.modifiedLineEnd);
  409. curr = curr.concat(thisDiff.slice(1));
  410. continue;
  411. }
  412. r[rLength++] = new Diff(curr);
  413. curr = thisDiff;
  414. }
  415. r[rLength++] = new Diff(curr);
  416. return r;
  417. }
  418. _findDiffIndex(pos) {
  419. const lineNumber = pos.lineNumber;
  420. for (let i = 0, len = this._diffs.length; i < len; i++) {
  421. const diff = this._diffs[i].entries;
  422. const lastModifiedLine = diff[diff.length - 1].modifiedLineEnd;
  423. if (lineNumber <= lastModifiedLine) {
  424. return i;
  425. }
  426. }
  427. return 0;
  428. }
  429. _render() {
  430. const originalOptions = this._diffEditor.getOriginalEditor().getOptions();
  431. const modifiedOptions = this._diffEditor.getModifiedEditor().getOptions();
  432. const originalModel = this._diffEditor.getOriginalEditor().getModel();
  433. const modifiedModel = this._diffEditor.getModifiedEditor().getModel();
  434. const originalModelOpts = originalModel.getOptions();
  435. const modifiedModelOpts = modifiedModel.getOptions();
  436. if (!this._isVisible || !originalModel || !modifiedModel) {
  437. dom.clearNode(this._content.domNode);
  438. this._currentDiff = null;
  439. this.scrollbar.scanDomNode();
  440. return;
  441. }
  442. this._diffEditor.updateOptions({ readOnly: true });
  443. const diffIndex = this._findDiffIndex(this._diffEditor.getPosition());
  444. if (this._diffs[diffIndex] === this._currentDiff) {
  445. return;
  446. }
  447. this._currentDiff = this._diffs[diffIndex];
  448. const diffs = this._diffs[diffIndex].entries;
  449. let container = document.createElement('div');
  450. container.className = 'diff-review-table';
  451. container.setAttribute('role', 'list');
  452. container.setAttribute('aria-label', 'Difference review. Use "Stage | Unstage | Revert Selected Ranges" commands');
  453. Configuration.applyFontInfoSlow(container, modifiedOptions.get(43 /* fontInfo */));
  454. let minOriginalLine = 0;
  455. let maxOriginalLine = 0;
  456. let minModifiedLine = 0;
  457. let maxModifiedLine = 0;
  458. for (let i = 0, len = diffs.length; i < len; i++) {
  459. const diffEntry = diffs[i];
  460. const originalLineStart = diffEntry.originalLineStart;
  461. const originalLineEnd = diffEntry.originalLineEnd;
  462. const modifiedLineStart = diffEntry.modifiedLineStart;
  463. const modifiedLineEnd = diffEntry.modifiedLineEnd;
  464. if (originalLineStart !== 0 && ((minOriginalLine === 0 || originalLineStart < minOriginalLine))) {
  465. minOriginalLine = originalLineStart;
  466. }
  467. if (originalLineEnd !== 0 && ((maxOriginalLine === 0 || originalLineEnd > maxOriginalLine))) {
  468. maxOriginalLine = originalLineEnd;
  469. }
  470. if (modifiedLineStart !== 0 && ((minModifiedLine === 0 || modifiedLineStart < minModifiedLine))) {
  471. minModifiedLine = modifiedLineStart;
  472. }
  473. if (modifiedLineEnd !== 0 && ((maxModifiedLine === 0 || modifiedLineEnd > maxModifiedLine))) {
  474. maxModifiedLine = modifiedLineEnd;
  475. }
  476. }
  477. let header = document.createElement('div');
  478. header.className = 'diff-review-row';
  479. let cell = document.createElement('div');
  480. cell.className = 'diff-review-cell diff-review-summary';
  481. const originalChangedLinesCnt = maxOriginalLine - minOriginalLine + 1;
  482. const modifiedChangedLinesCnt = maxModifiedLine - minModifiedLine + 1;
  483. cell.appendChild(document.createTextNode(`${diffIndex + 1}/${this._diffs.length}: @@ -${minOriginalLine},${originalChangedLinesCnt} +${minModifiedLine},${modifiedChangedLinesCnt} @@`));
  484. header.setAttribute('data-line', String(minModifiedLine));
  485. const getAriaLines = (lines) => {
  486. if (lines === 0) {
  487. return nls.localize('no_lines_changed', "no lines changed");
  488. }
  489. else if (lines === 1) {
  490. return nls.localize('one_line_changed', "1 line changed");
  491. }
  492. else {
  493. return nls.localize('more_lines_changed', "{0} lines changed", lines);
  494. }
  495. };
  496. const originalChangedLinesCntAria = getAriaLines(originalChangedLinesCnt);
  497. const modifiedChangedLinesCntAria = getAriaLines(modifiedChangedLinesCnt);
  498. header.setAttribute('aria-label', nls.localize({
  499. key: 'header',
  500. comment: [
  501. 'This is the ARIA label for a git diff header.',
  502. 'A git diff header looks like this: @@ -154,12 +159,39 @@.',
  503. 'That encodes that at original line 154 (which is now line 159), 12 lines were removed/changed with 39 lines.',
  504. 'Variables 0 and 1 refer to the diff index out of total number of diffs.',
  505. 'Variables 2 and 4 will be numbers (a line number).',
  506. 'Variables 3 and 5 will be "no lines changed", "1 line changed" or "X lines changed", localized separately.'
  507. ]
  508. }, "Difference {0} of {1}: original line {2}, {3}, modified line {4}, {5}", (diffIndex + 1), this._diffs.length, minOriginalLine, originalChangedLinesCntAria, minModifiedLine, modifiedChangedLinesCntAria));
  509. header.appendChild(cell);
  510. // @@ -504,7 +517,7 @@
  511. header.setAttribute('role', 'listitem');
  512. container.appendChild(header);
  513. const lineHeight = modifiedOptions.get(58 /* lineHeight */);
  514. let modLine = minModifiedLine;
  515. for (let i = 0, len = diffs.length; i < len; i++) {
  516. const diffEntry = diffs[i];
  517. DiffReview._renderSection(container, diffEntry, modLine, lineHeight, this._width, originalOptions, originalModel, originalModelOpts, modifiedOptions, modifiedModel, modifiedModelOpts, this._modeService.languageIdCodec);
  518. if (diffEntry.modifiedLineStart !== 0) {
  519. modLine = diffEntry.modifiedLineEnd;
  520. }
  521. }
  522. dom.clearNode(this._content.domNode);
  523. this._content.domNode.appendChild(container);
  524. this.scrollbar.scanDomNode();
  525. }
  526. static _renderSection(dest, diffEntry, modLine, lineHeight, width, originalOptions, originalModel, originalModelOpts, modifiedOptions, modifiedModel, modifiedModelOpts, languageIdCodec) {
  527. const type = diffEntry.getType();
  528. let rowClassName = 'diff-review-row';
  529. let lineNumbersExtraClassName = '';
  530. const spacerClassName = 'diff-review-spacer';
  531. let spacerIcon = null;
  532. switch (type) {
  533. case 1 /* Insert */:
  534. rowClassName = 'diff-review-row line-insert';
  535. lineNumbersExtraClassName = ' char-insert';
  536. spacerIcon = diffReviewInsertIcon;
  537. break;
  538. case 2 /* Delete */:
  539. rowClassName = 'diff-review-row line-delete';
  540. lineNumbersExtraClassName = ' char-delete';
  541. spacerIcon = diffReviewRemoveIcon;
  542. break;
  543. }
  544. const originalLineStart = diffEntry.originalLineStart;
  545. const originalLineEnd = diffEntry.originalLineEnd;
  546. const modifiedLineStart = diffEntry.modifiedLineStart;
  547. const modifiedLineEnd = diffEntry.modifiedLineEnd;
  548. const cnt = Math.max(modifiedLineEnd - modifiedLineStart, originalLineEnd - originalLineStart);
  549. const originalLayoutInfo = originalOptions.get(130 /* layoutInfo */);
  550. const originalLineNumbersWidth = originalLayoutInfo.glyphMarginWidth + originalLayoutInfo.lineNumbersWidth;
  551. const modifiedLayoutInfo = modifiedOptions.get(130 /* layoutInfo */);
  552. const modifiedLineNumbersWidth = 10 + modifiedLayoutInfo.glyphMarginWidth + modifiedLayoutInfo.lineNumbersWidth;
  553. for (let i = 0; i <= cnt; i++) {
  554. const originalLine = (originalLineStart === 0 ? 0 : originalLineStart + i);
  555. const modifiedLine = (modifiedLineStart === 0 ? 0 : modifiedLineStart + i);
  556. const row = document.createElement('div');
  557. row.style.minWidth = width + 'px';
  558. row.className = rowClassName;
  559. row.setAttribute('role', 'listitem');
  560. if (modifiedLine !== 0) {
  561. modLine = modifiedLine;
  562. }
  563. row.setAttribute('data-line', String(modLine));
  564. let cell = document.createElement('div');
  565. cell.className = 'diff-review-cell';
  566. cell.style.height = `${lineHeight}px`;
  567. row.appendChild(cell);
  568. const originalLineNumber = document.createElement('span');
  569. originalLineNumber.style.width = (originalLineNumbersWidth + 'px');
  570. originalLineNumber.style.minWidth = (originalLineNumbersWidth + 'px');
  571. originalLineNumber.className = 'diff-review-line-number' + lineNumbersExtraClassName;
  572. if (originalLine !== 0) {
  573. originalLineNumber.appendChild(document.createTextNode(String(originalLine)));
  574. }
  575. else {
  576. originalLineNumber.innerText = '\u00a0';
  577. }
  578. cell.appendChild(originalLineNumber);
  579. const modifiedLineNumber = document.createElement('span');
  580. modifiedLineNumber.style.width = (modifiedLineNumbersWidth + 'px');
  581. modifiedLineNumber.style.minWidth = (modifiedLineNumbersWidth + 'px');
  582. modifiedLineNumber.style.paddingRight = '10px';
  583. modifiedLineNumber.className = 'diff-review-line-number' + lineNumbersExtraClassName;
  584. if (modifiedLine !== 0) {
  585. modifiedLineNumber.appendChild(document.createTextNode(String(modifiedLine)));
  586. }
  587. else {
  588. modifiedLineNumber.innerText = '\u00a0';
  589. }
  590. cell.appendChild(modifiedLineNumber);
  591. const spacer = document.createElement('span');
  592. spacer.className = spacerClassName;
  593. if (spacerIcon) {
  594. const spacerCodicon = document.createElement('span');
  595. spacerCodicon.className = ThemeIcon.asClassName(spacerIcon);
  596. spacerCodicon.innerText = '\u00a0\u00a0';
  597. spacer.appendChild(spacerCodicon);
  598. }
  599. else {
  600. spacer.innerText = '\u00a0\u00a0';
  601. }
  602. cell.appendChild(spacer);
  603. let lineContent;
  604. if (modifiedLine !== 0) {
  605. let html = this._renderLine(modifiedModel, modifiedOptions, modifiedModelOpts.tabSize, modifiedLine, languageIdCodec);
  606. if (DiffReview._ttPolicy) {
  607. html = DiffReview._ttPolicy.createHTML(html);
  608. }
  609. cell.insertAdjacentHTML('beforeend', html);
  610. lineContent = modifiedModel.getLineContent(modifiedLine);
  611. }
  612. else {
  613. let html = this._renderLine(originalModel, originalOptions, originalModelOpts.tabSize, originalLine, languageIdCodec);
  614. if (DiffReview._ttPolicy) {
  615. html = DiffReview._ttPolicy.createHTML(html);
  616. }
  617. cell.insertAdjacentHTML('beforeend', html);
  618. lineContent = originalModel.getLineContent(originalLine);
  619. }
  620. if (lineContent.length === 0) {
  621. lineContent = nls.localize('blankLine', "blank");
  622. }
  623. let ariaLabel = '';
  624. switch (type) {
  625. case 0 /* Equal */:
  626. if (originalLine === modifiedLine) {
  627. ariaLabel = nls.localize({ key: 'unchangedLine', comment: ['The placeholders are contents of the line and should not be translated.'] }, "{0} unchanged line {1}", lineContent, originalLine);
  628. }
  629. else {
  630. ariaLabel = nls.localize('equalLine', "{0} original line {1} modified line {2}", lineContent, originalLine, modifiedLine);
  631. }
  632. break;
  633. case 1 /* Insert */:
  634. ariaLabel = nls.localize('insertLine', "+ {0} modified line {1}", lineContent, modifiedLine);
  635. break;
  636. case 2 /* Delete */:
  637. ariaLabel = nls.localize('deleteLine', "- {0} original line {1}", lineContent, originalLine);
  638. break;
  639. }
  640. row.setAttribute('aria-label', ariaLabel);
  641. dest.appendChild(row);
  642. }
  643. }
  644. static _renderLine(model, options, tabSize, lineNumber, languageIdCodec) {
  645. const lineContent = model.getLineContent(lineNumber);
  646. const fontInfo = options.get(43 /* fontInfo */);
  647. const lineTokens = LineTokens.createEmpty(lineContent, languageIdCodec);
  648. const isBasicASCII = ViewLineRenderingData.isBasicASCII(lineContent, model.mightContainNonBasicASCII());
  649. const containsRTL = ViewLineRenderingData.containsRTL(lineContent, isBasicASCII, model.mightContainRTL());
  650. const r = renderViewLine(new RenderLineInput((fontInfo.isMonospace && !options.get(29 /* disableMonospaceOptimizations */)), fontInfo.canUseHalfwidthRightwardsArrow, lineContent, false, isBasicASCII, containsRTL, 0, lineTokens, [], tabSize, 0, fontInfo.spaceWidth, fontInfo.middotWidth, fontInfo.wsmiddotWidth, options.get(104 /* stopRenderingLineAfter */), options.get(87 /* renderWhitespace */), options.get(82 /* renderControlCharacters */), options.get(44 /* fontLigatures */) !== EditorFontLigatures.OFF, null));
  651. return r.html;
  652. }
  653. };
  654. DiffReview._ttPolicy = (_a = window.trustedTypes) === null || _a === void 0 ? void 0 : _a.createPolicy('diffReview', { createHTML: value => value });
  655. DiffReview = __decorate([
  656. __param(1, IModeService)
  657. ], DiffReview);
  658. export { DiffReview };
  659. // theming
  660. registerThemingParticipant((theme, collector) => {
  661. const lineNumbers = theme.getColor(editorLineNumbers);
  662. if (lineNumbers) {
  663. collector.addRule(`.monaco-diff-editor .diff-review-line-number { color: ${lineNumbers}; }`);
  664. }
  665. const shadow = theme.getColor(scrollbarShadow);
  666. if (shadow) {
  667. collector.addRule(`.monaco-diff-editor .diff-review-shadow { box-shadow: ${shadow} 0 -6px 6px -6px inset; }`);
  668. }
  669. });
  670. class DiffReviewNext extends EditorAction {
  671. constructor() {
  672. super({
  673. id: 'editor.action.diffReview.next',
  674. label: nls.localize('editor.action.diffReview.next', "Go to Next Difference"),
  675. alias: 'Go to Next Difference',
  676. precondition: ContextKeyExpr.has('isInDiffEditor'),
  677. kbOpts: {
  678. kbExpr: null,
  679. primary: 65 /* F7 */,
  680. weight: 100 /* EditorContrib */
  681. }
  682. });
  683. }
  684. run(accessor, editor) {
  685. const diffEditor = findFocusedDiffEditor(accessor);
  686. if (diffEditor) {
  687. diffEditor.diffReviewNext();
  688. }
  689. }
  690. }
  691. class DiffReviewPrev extends EditorAction {
  692. constructor() {
  693. super({
  694. id: 'editor.action.diffReview.prev',
  695. label: nls.localize('editor.action.diffReview.prev', "Go to Previous Difference"),
  696. alias: 'Go to Previous Difference',
  697. precondition: ContextKeyExpr.has('isInDiffEditor'),
  698. kbOpts: {
  699. kbExpr: null,
  700. primary: 1024 /* Shift */ | 65 /* F7 */,
  701. weight: 100 /* EditorContrib */
  702. }
  703. });
  704. }
  705. run(accessor, editor) {
  706. const diffEditor = findFocusedDiffEditor(accessor);
  707. if (diffEditor) {
  708. diffEditor.diffReviewPrev();
  709. }
  710. }
  711. }
  712. function findFocusedDiffEditor(accessor) {
  713. const codeEditorService = accessor.get(ICodeEditorService);
  714. const diffEditors = codeEditorService.listDiffEditors();
  715. const activeCodeEditor = codeEditorService.getActiveCodeEditor();
  716. if (!activeCodeEditor) {
  717. return null;
  718. }
  719. for (let i = 0, len = diffEditors.length; i < len; i++) {
  720. const diffEditor = diffEditors[i];
  721. if (diffEditor.getModifiedEditor().getId() === activeCodeEditor.getId() || diffEditor.getOriginalEditor().getId() === activeCodeEditor.getId()) {
  722. return diffEditor;
  723. }
  724. }
  725. return null;
  726. }
  727. registerEditorAction(DiffReviewNext);
  728. registerEditorAction(DiffReviewPrev);