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.

113 line
5.6 KiB

  1. 'use strict';
  2. import { commands, ExtensionContext, Uri, window } from 'vscode';
  3. import { BuiltInCommands } from './constants';
  4. import { GitCommit } from './gitService';
  5. import { Logger } from './logger';
  6. export type SuppressedKeys = 'suppressCommitHasNoPreviousCommitWarning' |
  7. 'suppressCommitNotFoundWarning' |
  8. 'suppressFileNotUnderSourceControlWarning' |
  9. 'suppressGitVersionWarning' |
  10. 'suppressLineUncommittedWarning' |
  11. 'suppressNoRepositoryWarning' |
  12. 'suppressUpdateNotice' |
  13. 'suppressWelcomeNotice';
  14. export const SuppressedKeys = {
  15. CommitHasNoPreviousCommitWarning: 'suppressCommitHasNoPreviousCommitWarning' as SuppressedKeys,
  16. CommitNotFoundWarning: 'suppressCommitNotFoundWarning' as SuppressedKeys,
  17. FileNotUnderSourceControlWarning: 'suppressFileNotUnderSourceControlWarning' as SuppressedKeys,
  18. GitVersionWarning: 'suppressGitVersionWarning' as SuppressedKeys,
  19. LineUncommittedWarning: 'suppressLineUncommittedWarning' as SuppressedKeys,
  20. NoRepositoryWarning: 'suppressNoRepositoryWarning' as SuppressedKeys,
  21. UpdateNotice: 'suppressUpdateNotice' as SuppressedKeys,
  22. WelcomeNotice: 'suppressWelcomeNotice' as SuppressedKeys
  23. };
  24. export class Messages {
  25. static context: ExtensionContext;
  26. static configure(context: ExtensionContext) {
  27. this.context = context;
  28. }
  29. static showCommitHasNoPreviousCommitWarningMessage(commit?: GitCommit): Promise<string | undefined> {
  30. if (commit === undefined) return Messages._showMessage('info', `Commit has no previous commit`, SuppressedKeys.CommitHasNoPreviousCommitWarning);
  31. return Messages._showMessage('info', `Commit ${commit.shortSha} (${commit.author}, ${commit.fromNow()}) has no previous commit`, SuppressedKeys.CommitHasNoPreviousCommitWarning);
  32. }
  33. static showCommitNotFoundWarningMessage(message: string): Promise<string | undefined> {
  34. return Messages._showMessage('warn', `${message}. The commit could not be found`, SuppressedKeys.CommitNotFoundWarning);
  35. }
  36. static showFileNotUnderSourceControlWarningMessage(message: string): Promise<string | undefined> {
  37. return Messages._showMessage('warn', `${message}. The file is probably not under source control`, SuppressedKeys.FileNotUnderSourceControlWarning);
  38. }
  39. static showLineUncommittedWarningMessage(message: string): Promise<string | undefined> {
  40. return Messages._showMessage('warn', `${message}. The line has uncommitted changes`, SuppressedKeys.LineUncommittedWarning);
  41. }
  42. static showNoRepositoryWarningMessage(message: string): Promise<string | undefined> {
  43. return Messages._showMessage('warn', `${message}. No repository could be found`, SuppressedKeys.NoRepositoryWarning);
  44. }
  45. static showUnsupportedGitVersionErrorMessage(version: string): Promise<string | undefined> {
  46. return Messages._showMessage('error', `GitLens requires a newer version of Git (>= 2.2.0) than is currently installed (${version}). Please install a more recent version of Git.`, SuppressedKeys.GitVersionWarning);
  47. }
  48. static async showUpdateMessage(version: string): Promise<string | undefined> {
  49. const viewReleaseNotes = 'View Release Notes';
  50. const result = await Messages._showMessage('info', `GitLens has been updated to v${version}`, SuppressedKeys.UpdateNotice, undefined, viewReleaseNotes);
  51. if (result === viewReleaseNotes) {
  52. commands.executeCommand(BuiltInCommands.Open, Uri.parse('https://marketplace.visualstudio.com/items/eamodio.gitlens/changelog'));
  53. }
  54. return result;
  55. }
  56. static async showWelcomeMessage(): Promise<string | undefined> {
  57. const viewDocs = 'View Docs';
  58. const result = await Messages._showMessage('info', `Thank you for choosing GitLens! GitLens is powerful, feature rich, and highly configurable, so please be sure to view the docs and tailor it to suit your needs.`, SuppressedKeys.WelcomeNotice, null, viewDocs);
  59. if (result === viewDocs) {
  60. commands.executeCommand(BuiltInCommands.Open, Uri.parse('https://marketplace.visualstudio.com/items/eamodio.gitlens'));
  61. }
  62. return result;
  63. }
  64. private static async _showMessage(type: 'info' | 'warn' | 'error', message: string, suppressionKey: SuppressedKeys, dontShowAgain: string | null = 'Don\'t Show Again', ...actions: any[]): Promise<string | undefined> {
  65. Logger.log(`ShowMessage(${type}, '${message}', ${suppressionKey}, ${dontShowAgain})`);
  66. if (Messages.context.globalState.get(suppressionKey, false)) {
  67. Logger.log(`ShowMessage(${type}, ${message}, ${suppressionKey}, ${dontShowAgain}) skipped`);
  68. return undefined;
  69. }
  70. if (dontShowAgain !== null) {
  71. actions.push(dontShowAgain);
  72. }
  73. let result: string | undefined = undefined;
  74. switch (type) {
  75. case 'info':
  76. result = await window.showInformationMessage(message, ...actions);
  77. break;
  78. case 'warn':
  79. result = await window.showWarningMessage(message, ...actions);
  80. break;
  81. case 'error':
  82. result = await window.showErrorMessage(message, ...actions);
  83. break;
  84. }
  85. if (dontShowAgain === null || result === dontShowAgain) {
  86. Logger.log(`ShowMessage(${type}, '${message}', ${suppressionKey}, ${dontShowAgain}) don't show again requested`);
  87. await Messages.context.globalState.update(suppressionKey, true);
  88. if (result === dontShowAgain) return undefined;
  89. }
  90. Logger.log(`ShowMessage(${type}, '${message}', ${suppressionKey}, ${dontShowAgain}) returned ${result}`);
  91. return result;
  92. }
  93. }