You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
9.1 KiB

пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
  1. 'use strict';
  2. import {
  3. CancellationTokenSource,
  4. commands,
  5. QuickPickItem,
  6. QuickPickOptions,
  7. TextDocumentShowOptions,
  8. TextEditor,
  9. Uri,
  10. window
  11. } from 'vscode';
  12. import { Commands, openEditor } from '../commands';
  13. import { configuration } from '../configuration';
  14. import { GlyphChars } from '../constants';
  15. import { Container } from '../container';
  16. import { GitLog, GitLogCommit, GitRepoSearchBy, GitStashCommit, GitUri } from '../git/gitService';
  17. import { KeyMapping, Keys } from '../keyboard';
  18. import { Strings } from '../system';
  19. import {
  20. BranchesAndTagsQuickPick,
  21. BranchQuickPickItem,
  22. RefQuickPickItem,
  23. TagQuickPickItem
  24. } from './branchesAndTagsQuickPick';
  25. export function getQuickPickIgnoreFocusOut() {
  26. return !configuration.get<boolean>(configuration.name('advanced')('quickPick')('closeOnFocusOut').value);
  27. }
  28. export function showQuickPickProgress(message: string, mapping?: KeyMapping): CancellationTokenSource {
  29. const cancellation = new CancellationTokenSource();
  30. void _showQuickPickProgress(message, cancellation, mapping);
  31. return cancellation;
  32. }
  33. async function _showQuickPickProgress(message: string, cancellation: CancellationTokenSource, mapping?: KeyMapping) {
  34. const scope = mapping && (await Container.keyboard.beginScope(mapping));
  35. try {
  36. await window.showQuickPick(
  37. _getInfiniteCancellablePromise(cancellation),
  38. {
  39. placeHolder: message,
  40. ignoreFocusOut: getQuickPickIgnoreFocusOut()
  41. } as QuickPickOptions,
  42. cancellation.token
  43. );
  44. }
  45. catch (ex) {
  46. // Not sure why this throws
  47. }
  48. finally {
  49. cancellation.cancel();
  50. scope && scope.dispose();
  51. }
  52. }
  53. function _getInfiniteCancellablePromise(cancellation: CancellationTokenSource) {
  54. return new Promise<QuickPickItem[]>((resolve, reject) => {
  55. const disposable = cancellation.token.onCancellationRequested(() => {
  56. disposable.dispose();
  57. resolve([]);
  58. });
  59. });
  60. }
  61. export interface QuickPickItem extends QuickPickItem {
  62. onDidSelect?(): void;
  63. onDidPressKey?(key: Keys): Promise<{} | undefined>;
  64. }
  65. export class CommandQuickPickItem implements QuickPickItem {
  66. label!: string;
  67. description!: string;
  68. detail?: string | undefined;
  69. protected command: Commands | undefined;
  70. protected args: any[] | undefined;
  71. constructor(item: QuickPickItem, args?: [Commands, any[]]);
  72. constructor(item: QuickPickItem, command?: Commands, args?: any[]);
  73. constructor(item: QuickPickItem, commandOrArgs?: Commands | [Commands, any[]], args?: any[]) {
  74. if (commandOrArgs === undefined) {
  75. this.command = undefined;
  76. this.args = args;
  77. }
  78. else if (typeof commandOrArgs === 'string') {
  79. this.command = commandOrArgs;
  80. this.args = args;
  81. }
  82. else {
  83. this.command = commandOrArgs[0];
  84. this.args = commandOrArgs.slice(1);
  85. }
  86. Object.assign(this, item);
  87. }
  88. execute(): Promise<{} | undefined> {
  89. if (this.command === undefined) return Promise.resolve(undefined);
  90. return commands.executeCommand(this.command, ...(this.args || [])) as Promise<{} | undefined>;
  91. }
  92. onDidPressKey(key: Keys): Promise<{} | undefined> {
  93. return this.execute();
  94. }
  95. }
  96. export class CommitQuickPickItem implements QuickPickItem {
  97. label: string;
  98. description: string;
  99. detail: string;
  100. constructor(
  101. public readonly commit: GitLogCommit
  102. ) {
  103. const message = commit.getShortMessage();
  104. if (commit.isStash) {
  105. this.label = message;
  106. this.description = '';
  107. this.detail = `${GlyphChars.Space} ${(commit as GitStashCommit).stashName || commit.shortSha} ${Strings.pad(
  108. GlyphChars.Dot,
  109. 1,
  110. 1
  111. )} ${commit.formattedDate} ${Strings.pad(GlyphChars.Dot, 1, 1)} ${commit.getFormattedDiffStatus({
  112. compact: true
  113. })}`;
  114. }
  115. else {
  116. this.label = message;
  117. this.description = `${Strings.pad('$(git-commit)', 1, 1)} ${commit.shortSha}`;
  118. this.detail = `${GlyphChars.Space} ${commit.author}, ${commit.formattedDate}${
  119. commit.isFile
  120. ? ''
  121. : ` ${Strings.pad(GlyphChars.Dot, 1, 1)} ${commit.getFormattedDiffStatus({ compact: true })}`
  122. }`;
  123. }
  124. }
  125. }
  126. export class ChooseFromBranchesAndTagsQuickPickItem extends CommandQuickPickItem {
  127. constructor(
  128. private readonly repoPath: string,
  129. private readonly placeHolder: string,
  130. private readonly _goBack?: CommandQuickPickItem,
  131. item: QuickPickItem = {
  132. label: 'Choose from Branch or Tag History...',
  133. description: `${Strings.pad(GlyphChars.Dash, 2, 2)} shows list of branches and tags`
  134. }
  135. ) {
  136. super(item, undefined, undefined);
  137. }
  138. execute(): Promise<CommandQuickPickItem | BranchQuickPickItem | TagQuickPickItem | RefQuickPickItem | undefined> {
  139. return new BranchesAndTagsQuickPick(this.repoPath).show(this.placeHolder, {
  140. allowCommitId: true,
  141. goBack: this._goBack
  142. });
  143. }
  144. }
  145. export class KeyCommandQuickPickItem extends CommandQuickPickItem {
  146. constructor(command: Commands, args?: any[]) {
  147. super({ label: '', description: '' } as QuickPickItem, command, args);
  148. }
  149. }
  150. export class MessageQuickPickItem extends CommandQuickPickItem {
  151. constructor(message: string) {
  152. super({ label: message, description: '' } as QuickPickItem);
  153. }
  154. }
  155. export class OpenFileCommandQuickPickItem extends CommandQuickPickItem {
  156. constructor(
  157. public readonly uri: Uri,
  158. item: QuickPickItem
  159. ) {
  160. super(item, undefined, undefined);
  161. }
  162. async execute(options?: TextDocumentShowOptions): Promise<TextEditor | undefined> {
  163. return openEditor(this.uri, options);
  164. }
  165. // onDidSelect(): Promise<{} | undefined> {
  166. // return this.execute({
  167. // preserveFocus: true,
  168. // preview: true
  169. // });
  170. // }
  171. onDidPressKey(key: Keys): Promise<{} | undefined> {
  172. return this.execute({
  173. preserveFocus: true,
  174. preview: false
  175. });
  176. }
  177. }
  178. export class OpenFilesCommandQuickPickItem extends CommandQuickPickItem {
  179. constructor(
  180. public readonly uris: Uri[],
  181. item: QuickPickItem
  182. ) {
  183. super(item, undefined, undefined);
  184. }
  185. async execute(
  186. options: TextDocumentShowOptions = { preserveFocus: false, preview: false }
  187. ): Promise<{} | undefined> {
  188. for (const uri of this.uris) {
  189. await openEditor(uri, options);
  190. }
  191. return undefined;
  192. }
  193. async onDidPressKey(key: Keys): Promise<{} | undefined> {
  194. return this.execute({
  195. preserveFocus: true,
  196. preview: false
  197. });
  198. }
  199. }
  200. export class ShowCommitInViewQuickPickItem extends CommandQuickPickItem {
  201. constructor(
  202. public readonly commit: GitLogCommit,
  203. item: QuickPickItem = {
  204. label: 'Show in View',
  205. description: `${Strings.pad(GlyphChars.Dash, 2, 2)} displays the commit in the GitLens Search Commits view`
  206. }
  207. ) {
  208. super(item, undefined, undefined);
  209. }
  210. async execute(): Promise<{} | undefined> {
  211. await Container.searchView.search(this.commit.repoPath, this.commit.sha, GitRepoSearchBy.Sha, {
  212. label: { label: `commits with an id matching '${this.commit.shortSha}'` }
  213. });
  214. return undefined;
  215. }
  216. }
  217. export class ShowCommitSearchResultsInViewQuickPickItem extends CommandQuickPickItem {
  218. constructor(
  219. public readonly search: string,
  220. public readonly searchBy: GitRepoSearchBy,
  221. public readonly results: GitLog,
  222. public readonly resultsLabel: string | { label: string; resultsType?: { singular: string; plural: string } },
  223. item: QuickPickItem = {
  224. label: 'Show in View',
  225. description: `${Strings.pad(
  226. GlyphChars.Dash,
  227. 2,
  228. 2
  229. )} displays the search results in the GitLens Search Commits view`
  230. }
  231. ) {
  232. super(item, undefined, undefined);
  233. }
  234. async execute(): Promise<{} | undefined> {
  235. await Container.searchView.showSearchResults(this.results.repoPath, this.search, this.searchBy, this.results, {
  236. label: this.resultsLabel
  237. });
  238. return undefined;
  239. }
  240. }
  241. export class ShowFileHistoryInViewQuickPickItem extends CommandQuickPickItem {
  242. constructor(
  243. public readonly uri: GitUri,
  244. public readonly baseRef: string | undefined,
  245. item: QuickPickItem = {
  246. label: 'Show in View',
  247. description: `${Strings.pad(
  248. GlyphChars.Dash,
  249. 2,
  250. 2
  251. )} displays the file history in the GitLens File History view`
  252. }
  253. ) {
  254. super(item, undefined, undefined);
  255. }
  256. async execute(): Promise<{} | undefined> {
  257. await Container.fileHistoryView.showHistoryForUri(this.uri, this.baseRef);
  258. return undefined;
  259. }
  260. }