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.

85 lines
3.1 KiB

  1. 'use strict';
  2. import { CancellationToken, CodeLens, CodeLensProvider, DocumentSelector, Range, TextDocument, Uri } from 'vscode';
  3. import { Commands, DiffWithPreviousCommandArgs, DiffWithWorkingCommandArgs } from './commands';
  4. import { DocumentSchemes } from './constants';
  5. import { Container } from './container';
  6. import { GitCommit, GitUri } from './gitService';
  7. export class GitDiffWithWorkingCodeLens extends CodeLens {
  8. constructor(
  9. public readonly fileName: string,
  10. public readonly commit: GitCommit,
  11. range: Range
  12. ) {
  13. super(range);
  14. }
  15. }
  16. export class GitDiffWithPreviousCodeLens extends CodeLens {
  17. constructor(
  18. public readonly fileName: string,
  19. public readonly commit: GitCommit,
  20. range: Range
  21. ) {
  22. super(range);
  23. }
  24. }
  25. export class GitRevisionCodeLensProvider implements CodeLensProvider {
  26. static selector: DocumentSelector = { scheme: DocumentSchemes.GitLensGit };
  27. async provideCodeLenses(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> {
  28. const gitUri = GitUri.fromRevisionUri(document.uri);
  29. const lenses: CodeLens[] = [];
  30. const commit = await Container.git.getLogCommitForFile(gitUri.repoPath, gitUri.fsPath, { ref: gitUri.sha, firstIfNotFound: true });
  31. if (commit === undefined) return lenses;
  32. if (commit.previousSha) {
  33. lenses.push(new GitDiffWithPreviousCodeLens(commit.previousUri.fsPath, commit, new Range(0, 0, 0, 1)));
  34. }
  35. lenses.push(new GitDiffWithWorkingCodeLens(commit.uri.fsPath, commit, new Range(0, 1, 0, 2)));
  36. return lenses;
  37. }
  38. resolveCodeLens(lens: CodeLens, token: CancellationToken): Thenable<CodeLens> {
  39. if (lens instanceof GitDiffWithWorkingCodeLens) return this._resolveDiffWithWorkingTreeCodeLens(lens, token);
  40. if (lens instanceof GitDiffWithPreviousCodeLens) return this._resolveGitDiffWithPreviousCodeLens(lens, token);
  41. return Promise.reject<CodeLens>(undefined);
  42. }
  43. _resolveDiffWithWorkingTreeCodeLens(lens: GitDiffWithWorkingCodeLens, token: CancellationToken): Thenable<CodeLens> {
  44. lens.command = {
  45. title: `Compare Revision (${lens.commit.shortSha}) with Working`,
  46. command: Commands.DiffWithWorking,
  47. arguments: [
  48. Uri.file(lens.fileName),
  49. {
  50. commit: lens.commit,
  51. line: lens.range.start.line
  52. } as DiffWithWorkingCommandArgs
  53. ]
  54. };
  55. return Promise.resolve(lens);
  56. }
  57. _resolveGitDiffWithPreviousCodeLens(lens: GitDiffWithPreviousCodeLens, token: CancellationToken): Thenable<CodeLens> {
  58. lens.command = {
  59. title: `Compare Revision (${lens.commit.shortSha}) with Previous (${lens.commit.previousShortSha})`,
  60. command: Commands.DiffWithPrevious,
  61. arguments: [
  62. Uri.file(lens.fileName),
  63. {
  64. commit: lens.commit,
  65. line: lens.range.start.line
  66. } as DiffWithPreviousCommandArgs
  67. ]
  68. };
  69. return Promise.resolve(lens);
  70. }
  71. }