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.

31 lines
1.3 KiB

  1. 'use strict';
  2. import { CancellationToken, ExtensionContext, TextDocumentContentProvider, Uri, window } from 'vscode';
  3. import { DocumentSchemes } from './constants';
  4. import { GitService } from './gitService';
  5. import { Logger } from './logger';
  6. import * as path from 'path';
  7. export class GitContentProvider implements TextDocumentContentProvider {
  8. static scheme = DocumentSchemes.GitLensGit;
  9. constructor(context: ExtensionContext, private git: GitService) { }
  10. async provideTextDocumentContent(uri: Uri, token: CancellationToken): Promise<string | undefined> {
  11. const data = GitService.fromGitContentUri(uri);
  12. const fileName = data.originalFileName || data.fileName;
  13. try {
  14. let text = data.sha !== GitService.fakeSha
  15. ? await this.git.getVersionedFileText(data.repoPath, fileName, data.sha)
  16. : '';
  17. if (data.decoration) {
  18. text = `${data.decoration}\n${text}`;
  19. }
  20. return text;
  21. }
  22. catch (ex) {
  23. Logger.error(ex, 'GitContentProvider', 'getVersionedFileText');
  24. window.showErrorMessage(`Unable to show Git revision ${GitService.shortenSha(data.sha)} of '${path.relative(data.repoPath, fileName)}'`);
  25. return undefined;
  26. }
  27. }
  28. }