Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

184 řádky
5.8 KiB

před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
  1. 'use strict';
  2. import { commands, ConfigurationChangeEvent } from 'vscode';
  3. import { CompareViewConfig, configuration, ViewFilesLayout } from '../configuration';
  4. import {
  5. CommandContext,
  6. NamedRef,
  7. PinnedComparison,
  8. PinnedComparisons,
  9. setCommandContext,
  10. WorkspaceState,
  11. } from '../constants';
  12. import { Container } from '../container';
  13. import { CompareNode, CompareResultsNode, nodeSupportsConditionalDismissal, ViewNode } from './nodes';
  14. import { ViewBase } from './viewBase';
  15. export class CompareView extends ViewBase<CompareNode, CompareViewConfig> {
  16. protected readonly configKey = 'compare';
  17. constructor() {
  18. super('gitlens.views.compare', 'Compare');
  19. void setCommandContext(CommandContext.ViewsCompareKeepResults, this.keepResults);
  20. }
  21. getRoot() {
  22. return new CompareNode(this);
  23. }
  24. protected registerCommands() {
  25. void Container.viewCommands;
  26. commands.registerCommand(this.getQualifiedCommand('clear'), () => this.clear(), this);
  27. commands.registerCommand(
  28. this.getQualifiedCommand('copy'),
  29. () => commands.executeCommand('gitlens.views.copy', this.selection),
  30. this,
  31. );
  32. commands.registerCommand(this.getQualifiedCommand('refresh'), () => this.refresh(true), this);
  33. commands.registerCommand(
  34. this.getQualifiedCommand('setFilesLayoutToAuto'),
  35. () => this.setFilesLayout(ViewFilesLayout.Auto),
  36. this,
  37. );
  38. commands.registerCommand(
  39. this.getQualifiedCommand('setFilesLayoutToList'),
  40. () => this.setFilesLayout(ViewFilesLayout.List),
  41. this,
  42. );
  43. commands.registerCommand(
  44. this.getQualifiedCommand('setFilesLayoutToTree'),
  45. () => this.setFilesLayout(ViewFilesLayout.Tree),
  46. this,
  47. );
  48. commands.registerCommand(this.getQualifiedCommand('setKeepResultsToOn'), () => this.setKeepResults(true), this);
  49. commands.registerCommand(
  50. this.getQualifiedCommand('setKeepResultsToOff'),
  51. () => this.setKeepResults(false),
  52. this,
  53. );
  54. commands.registerCommand(this.getQualifiedCommand('setShowAvatarsOn'), () => this.setShowAvatars(true), this);
  55. commands.registerCommand(this.getQualifiedCommand('setShowAvatarsOff'), () => this.setShowAvatars(false), this);
  56. commands.registerCommand(this.getQualifiedCommand('pinComparison'), this.pinComparison, this);
  57. commands.registerCommand(this.getQualifiedCommand('unpinComparison'), this.unpinComparison, this);
  58. commands.registerCommand(this.getQualifiedCommand('swapComparison'), this.swapComparison, this);
  59. commands.registerCommand(this.getQualifiedCommand('selectForCompare'), this.selectForCompare, this);
  60. commands.registerCommand(this.getQualifiedCommand('compareWithSelected'), this.compareWithSelected, this);
  61. }
  62. protected filterConfigurationChanged(e: ConfigurationChangeEvent) {
  63. const changed = super.filterConfigurationChanged(e);
  64. if (
  65. !changed &&
  66. !configuration.changed(e, 'defaultDateFormat') &&
  67. !configuration.changed(e, 'defaultDateSource') &&
  68. !configuration.changed(e, 'defaultDateStyle') &&
  69. !configuration.changed(e, 'defaultGravatarsStyle')
  70. ) {
  71. return false;
  72. }
  73. return true;
  74. }
  75. get keepResults(): boolean {
  76. return Container.context.workspaceState.get<boolean>(WorkspaceState.ViewsCompareKeepResults, false);
  77. }
  78. clear() {
  79. this.root?.clear();
  80. }
  81. dismissNode(node: ViewNode) {
  82. if (this.root == null) return;
  83. if (nodeSupportsConditionalDismissal(node) && node.canDismiss() === false) return;
  84. this.root.dismiss(node);
  85. }
  86. compare(repoPath: string, ref1: string | NamedRef, ref2: string | NamedRef) {
  87. return this.addResults(
  88. new CompareResultsNode(
  89. this,
  90. repoPath,
  91. typeof ref1 === 'string' ? { ref: ref1 } : ref1,
  92. typeof ref2 === 'string' ? { ref: ref2 } : ref2,
  93. ),
  94. );
  95. }
  96. compareWithSelected(repoPath?: string, ref?: string | NamedRef) {
  97. const root = this.ensureRoot();
  98. void root.compareWithSelected(repoPath, ref);
  99. }
  100. selectForCompare(repoPath?: string, ref?: string | NamedRef) {
  101. const root = this.ensureRoot();
  102. void root.selectForCompare(repoPath, ref);
  103. }
  104. getPinnedComparisons() {
  105. const pinned = Container.context.workspaceState.get<PinnedComparisons>(WorkspaceState.PinnedComparisons);
  106. if (pinned == null) return [];
  107. return Object.values(pinned).map(p => new CompareResultsNode(this, p.path, p.ref1, p.ref2, true));
  108. }
  109. async updatePinnedComparison(id: string, pin?: PinnedComparison) {
  110. let pinned = Container.context.workspaceState.get<PinnedComparisons>(WorkspaceState.PinnedComparisons);
  111. if (pinned == null) {
  112. pinned = Object.create(null) as PinnedComparisons;
  113. }
  114. if (pin !== undefined) {
  115. pinned[id] = { ...pin };
  116. } else {
  117. const { [id]: _, ...rest } = pinned;
  118. pinned = rest;
  119. }
  120. await Container.context.workspaceState.update(WorkspaceState.PinnedComparisons, pinned);
  121. }
  122. private async addResults(results: ViewNode) {
  123. if (!this.visible) {
  124. await this.show();
  125. }
  126. const root = this.ensureRoot();
  127. root.addOrReplace(results, !this.keepResults);
  128. setImmediate(() => this.reveal(results, { expand: true, focus: true, select: true }));
  129. }
  130. private setFilesLayout(layout: ViewFilesLayout) {
  131. return configuration.updateEffective('views', this.configKey, 'files', 'layout', layout);
  132. }
  133. private setKeepResults(enabled: boolean) {
  134. void Container.context.workspaceState.update(WorkspaceState.ViewsCompareKeepResults, enabled);
  135. void setCommandContext(CommandContext.ViewsCompareKeepResults, enabled);
  136. }
  137. private setShowAvatars(enabled: boolean) {
  138. return configuration.updateEffective('views', this.configKey, 'avatars', enabled);
  139. }
  140. private pinComparison(node: ViewNode) {
  141. if (!(node instanceof CompareResultsNode)) return undefined;
  142. return node.pin();
  143. }
  144. private swapComparison(node: ViewNode) {
  145. if (!(node instanceof CompareResultsNode)) return undefined;
  146. return node.swap();
  147. }
  148. private unpinComparison(node: ViewNode) {
  149. if (!(node instanceof CompareResultsNode)) return undefined;
  150. return node.unpin();
  151. }
  152. }