diff --git a/src/codelens/codeLensController.ts b/src/codelens/codeLensController.ts index c962a5b..bc2c95f 100644 --- a/src/codelens/codeLensController.ts +++ b/src/codelens/codeLensController.ts @@ -11,12 +11,11 @@ import type { DocumentDirtyIdleTriggerEvent, GitDocumentState, } from '../trackers/gitDocumentTracker'; -import { GitCodeLensProvider } from './codeLensProvider'; export class GitCodeLensController implements Disposable { private _canToggle: boolean = false; private _disposable: Disposable | undefined; - private _provider: GitCodeLensProvider | undefined; + private _provider: import('./codeLensProvider').GitCodeLensProvider | undefined; private _providerDisposable: Disposable | undefined; constructor(private readonly container: Container) { @@ -43,7 +42,7 @@ export class GitCodeLensController implements Disposable { const cfg = configuration.get('codeLens'); if (cfg.enabled && (cfg.recentChange.enabled || cfg.authors.enabled)) { - this.ensureProvider(); + void this.ensureProvider(); } else { this._providerDisposable?.dispose(); this._provider = undefined; @@ -83,10 +82,10 @@ export class GitCodeLensController implements Disposable { return; } - this.ensureProvider(); + void this.ensureProvider(); } - private ensureProvider() { + private async ensureProvider() { if (this._provider != null) { this._provider.reset(); @@ -95,6 +94,8 @@ export class GitCodeLensController implements Disposable { this._providerDisposable?.dispose(); + const { GitCodeLensProvider } = await import(/* webpackChunkName: "codelens" */ './codeLensProvider'); + this._provider = new GitCodeLensProvider(this.container); this._providerDisposable = Disposable.from( languages.registerCodeLensProvider(GitCodeLensProvider.selector, this._provider), diff --git a/src/codelens/codeLensProvider.ts b/src/codelens/codeLensProvider.ts index df3b270..c4e4904 100644 --- a/src/codelens/codeLensProvider.ts +++ b/src/codelens/codeLensProvider.ts @@ -33,7 +33,7 @@ import { filterMap, find, first, join, map } from '../system/iterable'; import { Logger } from '../system/logger'; import { isVirtualUri } from '../system/utils'; -export class GitRecentChangeCodeLens extends CodeLens { +class GitRecentChangeCodeLens extends CodeLens { constructor( public readonly languageId: string, public readonly symbol: DocumentSymbol | SymbolInformation, @@ -54,7 +54,7 @@ export class GitRecentChangeCodeLens extends CodeLens { } } -export class GitAuthorsCodeLens extends CodeLens { +class GitAuthorsCodeLens extends CodeLens { constructor( public readonly languageId: string, public readonly symbol: DocumentSymbol | SymbolInformation, @@ -515,50 +515,40 @@ export class GitCodeLensProvider implements CodeLensProvider { } if (lens.desiredCommand === false) { - return this.applyCommandWithNoClickAction(title, lens); + return applyCommandWithNoClickAction(title, lens); } switch (lens.desiredCommand) { case CodeLensCommand.CopyRemoteCommitUrl: - return this.applyCopyOrOpenCommitOnRemoteCommand( - title, - lens, - recentCommit, - true, - ); + return applyCopyOrOpenCommitOnRemoteCommand(title, lens, recentCommit, true); case CodeLensCommand.CopyRemoteFileUrl: - return this.applyCopyOrOpenFileOnRemoteCommand( - title, - lens, - recentCommit, - true, - ); + return applyCopyOrOpenFileOnRemoteCommand(title, lens, recentCommit, true); case CodeLensCommand.DiffWithPrevious: - return this.applyDiffWithPreviousCommand(title, lens, recentCommit); + return applyDiffWithPreviousCommand(title, lens, recentCommit); case CodeLensCommand.OpenCommitOnRemote: - return this.applyCopyOrOpenCommitOnRemoteCommand(title, lens, recentCommit); + return applyCopyOrOpenCommitOnRemoteCommand(title, lens, recentCommit); case CodeLensCommand.OpenFileOnRemote: - return this.applyCopyOrOpenFileOnRemoteCommand(title, lens, recentCommit); + return applyCopyOrOpenFileOnRemoteCommand(title, lens, recentCommit); case CodeLensCommand.RevealCommitInView: - return this.applyRevealCommitInViewCommand(title, lens, recentCommit); + return applyRevealCommitInViewCommand(title, lens, recentCommit); case CodeLensCommand.ShowCommitsInView: - return this.applyShowCommitsInViewCommand(title, lens, blame, recentCommit); + return applyShowCommitsInViewCommand(title, lens, blame, recentCommit); case CodeLensCommand.ShowQuickCommitDetails: - return this.applyShowQuickCommitDetailsCommand(title, lens, recentCommit); + return applyShowQuickCommitDetailsCommand(title, lens, recentCommit); case CodeLensCommand.ShowQuickCommitFileDetails: - return this.applyShowQuickCommitFileDetailsCommand(title, lens, recentCommit); + return applyShowQuickCommitFileDetailsCommand(title, lens, recentCommit); case CodeLensCommand.ShowQuickCurrentBranchHistory: - return this.applyShowQuickCurrentBranchHistoryCommand(title, lens); + return applyShowQuickCurrentBranchHistoryCommand(title, lens); case CodeLensCommand.ShowQuickFileHistory: - return this.applyShowQuickFileHistoryCommand(title, lens); + return applyShowQuickFileHistoryCommand(title, lens); case CodeLensCommand.ToggleFileBlame: - return this.applyToggleFileBlameCommand(title, lens); + return applyToggleFileBlameCommand(title, lens); case CodeLensCommand.ToggleFileChanges: - return this.applyToggleFileChangesCommand(title, lens, recentCommit); + return applyToggleFileChangesCommand(title, lens, recentCommit); case CodeLensCommand.ToggleFileChangesOnly: - return this.applyToggleFileChangesCommand(title, lens, recentCommit, true); + return applyToggleFileChangesCommand(title, lens, recentCommit, true); case CodeLensCommand.ToggleFileHeatmap: - return this.applyToggleFileHeatmapCommand(title, lens); + return applyToggleFileHeatmapCommand(title, lens); default: return lens; } @@ -586,7 +576,7 @@ export class GitCodeLensProvider implements CodeLensProvider { } if (lens.desiredCommand === false) { - return this.applyCommandWithNoClickAction(title, lens); + return applyCommandWithNoClickAction(title, lens); } const commit = find(blame.commits.values(), c => c.author.name === author) ?? first(blame.commits.values()); @@ -594,280 +584,280 @@ export class GitCodeLensProvider implements CodeLensProvider { switch (lens.desiredCommand) { case CodeLensCommand.CopyRemoteCommitUrl: - return this.applyCopyOrOpenCommitOnRemoteCommand(title, lens, commit, true); + return applyCopyOrOpenCommitOnRemoteCommand(title, lens, commit, true); case CodeLensCommand.CopyRemoteFileUrl: - return this.applyCopyOrOpenFileOnRemoteCommand(title, lens, commit, true); + return applyCopyOrOpenFileOnRemoteCommand(title, lens, commit, true); case CodeLensCommand.DiffWithPrevious: - return this.applyDiffWithPreviousCommand(title, lens, commit); + return applyDiffWithPreviousCommand(title, lens, commit); case CodeLensCommand.OpenCommitOnRemote: - return this.applyCopyOrOpenCommitOnRemoteCommand(title, lens, commit); + return applyCopyOrOpenCommitOnRemoteCommand(title, lens, commit); case CodeLensCommand.OpenFileOnRemote: - return this.applyCopyOrOpenFileOnRemoteCommand(title, lens, commit); + return applyCopyOrOpenFileOnRemoteCommand(title, lens, commit); case CodeLensCommand.RevealCommitInView: - return this.applyRevealCommitInViewCommand(title, lens, commit); + return applyRevealCommitInViewCommand(title, lens, commit); case CodeLensCommand.ShowCommitsInView: - return this.applyShowCommitsInViewCommand(title, lens, blame); + return applyShowCommitsInViewCommand(title, lens, blame); case CodeLensCommand.ShowQuickCommitDetails: - return this.applyShowQuickCommitDetailsCommand(title, lens, commit); + return applyShowQuickCommitDetailsCommand(title, lens, commit); case CodeLensCommand.ShowQuickCommitFileDetails: - return this.applyShowQuickCommitFileDetailsCommand(title, lens, commit); + return applyShowQuickCommitFileDetailsCommand(title, lens, commit); case CodeLensCommand.ShowQuickCurrentBranchHistory: - return this.applyShowQuickCurrentBranchHistoryCommand(title, lens); + return applyShowQuickCurrentBranchHistoryCommand(title, lens); case CodeLensCommand.ShowQuickFileHistory: - return this.applyShowQuickFileHistoryCommand(title, lens); + return applyShowQuickFileHistoryCommand(title, lens); case CodeLensCommand.ToggleFileBlame: - return this.applyToggleFileBlameCommand(title, lens); + return applyToggleFileBlameCommand(title, lens); case CodeLensCommand.ToggleFileChanges: - return this.applyToggleFileChangesCommand(title, lens, commit); + return applyToggleFileChangesCommand(title, lens, commit); case CodeLensCommand.ToggleFileChangesOnly: - return this.applyToggleFileChangesCommand(title, lens, commit, true); + return applyToggleFileChangesCommand(title, lens, commit, true); case CodeLensCommand.ToggleFileHeatmap: - return this.applyToggleFileHeatmapCommand(title, lens); + return applyToggleFileHeatmapCommand(title, lens); default: return lens; } } - private applyDiffWithPreviousCommand( - title: string, - lens: T, - commit: GitCommit | undefined, - ): T { - lens.command = asCommand<[undefined, DiffWithPreviousCommandArgs]>({ - title: title, - command: Commands.DiffWithPrevious, - arguments: [ - undefined, - { - commit: commit, - uri: lens.uri!.toFileUri(), - }, - ], - }); - return lens; + private getDirtyTitle(cfg: CodeLensConfig) { + if (cfg.recentChange.enabled && cfg.authors.enabled) { + return configuration.get('strings.codeLens.unsavedChanges.recentChangeAndAuthors'); + } + if (cfg.recentChange.enabled) return configuration.get('strings.codeLens.unsavedChanges.recentChangeOnly'); + return configuration.get('strings.codeLens.unsavedChanges.authorsOnly'); } +} - private applyCopyOrOpenCommitOnRemoteCommand( - title: string, - lens: T, - commit: GitCommit, - clipboard: boolean = false, - ): T { - lens.command = asCommand<[OpenOnRemoteCommandArgs]>({ - title: title, - command: Commands.OpenOnRemote, - arguments: [ - { - resource: { - type: RemoteResourceType.Commit, - sha: commit.sha, - }, - repoPath: commit.repoPath, - clipboard: clipboard, - }, - ], - }); - return lens; - } +function applyDiffWithPreviousCommand( + title: string, + lens: T, + commit: GitCommit | undefined, +): T { + lens.command = asCommand<[undefined, DiffWithPreviousCommandArgs]>({ + title: title, + command: Commands.DiffWithPrevious, + arguments: [ + undefined, + { + commit: commit, + uri: lens.uri!.toFileUri(), + }, + ], + }); + return lens; +} - private applyCopyOrOpenFileOnRemoteCommand( - title: string, - lens: T, - commit: GitCommit, - clipboard: boolean = false, - ): T { - lens.command = asCommand<[OpenOnRemoteCommandArgs]>({ - title: title, - command: Commands.OpenOnRemote, - arguments: [ - { - resource: { - type: RemoteResourceType.Revision, - fileName: commit.file?.path ?? '', - sha: commit.sha, - }, - repoPath: commit.repoPath, - clipboard: clipboard, +function applyCopyOrOpenCommitOnRemoteCommand( + title: string, + lens: T, + commit: GitCommit, + clipboard: boolean = false, +): T { + lens.command = asCommand<[OpenOnRemoteCommandArgs]>({ + title: title, + command: Commands.OpenOnRemote, + arguments: [ + { + resource: { + type: RemoteResourceType.Commit, + sha: commit.sha, }, - ], - }); - return lens; - } + repoPath: commit.repoPath, + clipboard: clipboard, + }, + ], + }); + return lens; +} - private applyRevealCommitInViewCommand( - title: string, - lens: T, - commit: GitCommit | undefined, - ): T { - lens.command = asCommand<[Uri, ShowQuickCommitCommandArgs]>({ - title: title, - command: commit?.isUncommitted ? '' : CodeLensCommand.RevealCommitInView, - arguments: [ - lens.uri!.toFileUri(), - { - commit: commit, - sha: commit === undefined ? undefined : commit.sha, +function applyCopyOrOpenFileOnRemoteCommand( + title: string, + lens: T, + commit: GitCommit, + clipboard: boolean = false, +): T { + lens.command = asCommand<[OpenOnRemoteCommandArgs]>({ + title: title, + command: Commands.OpenOnRemote, + arguments: [ + { + resource: { + type: RemoteResourceType.Revision, + fileName: commit.file?.path ?? '', + sha: commit.sha, }, - ], - }); - return lens; - } + repoPath: commit.repoPath, + clipboard: clipboard, + }, + ], + }); + return lens; +} - private applyShowCommitsInViewCommand( - title: string, - lens: T, - blame: GitBlameLines, - commit?: GitCommit, - ): T { - let refs; - if (commit === undefined) { - refs = [...filterMap(blame.commits.values(), c => (c.isUncommitted ? undefined : c.ref))]; - } else { - refs = [commit.ref]; - } +function applyRevealCommitInViewCommand( + title: string, + lens: T, + commit: GitCommit | undefined, +): T { + lens.command = asCommand<[Uri, ShowQuickCommitCommandArgs]>({ + title: title, + command: commit?.isUncommitted ? '' : CodeLensCommand.RevealCommitInView, + arguments: [ + lens.uri!.toFileUri(), + { + commit: commit, + sha: commit === undefined ? undefined : commit.sha, + }, + ], + }); + return lens; +} - lens.command = asCommand<[ShowCommitsInViewCommandArgs]>({ - title: title, - command: refs.length === 0 ? '' : Commands.ShowCommitsInView, - arguments: [ - { - repoPath: blame.repoPath, - refs: refs, - }, - ], - }); - return lens; +function applyShowCommitsInViewCommand( + title: string, + lens: T, + blame: GitBlameLines, + commit?: GitCommit, +): T { + let refs; + if (commit === undefined) { + refs = [...filterMap(blame.commits.values(), c => (c.isUncommitted ? undefined : c.ref))]; + } else { + refs = [commit.ref]; } - private applyShowQuickCommitDetailsCommand( - title: string, - lens: T, - commit: GitCommit | undefined, - ): T { - lens.command = asCommand<[Uri, ShowQuickCommitCommandArgs]>({ - title: title, - command: commit?.isUncommitted ? '' : CodeLensCommand.ShowQuickCommitDetails, - arguments: [ - lens.uri!.toFileUri(), - { - commit: commit, - sha: commit === undefined ? undefined : commit.sha, - }, - ], - }); - return lens; - } + lens.command = asCommand<[ShowCommitsInViewCommandArgs]>({ + title: title, + command: refs.length === 0 ? '' : Commands.ShowCommitsInView, + arguments: [ + { + repoPath: blame.repoPath, + refs: refs, + }, + ], + }); + return lens; +} - private applyShowQuickCommitFileDetailsCommand( - title: string, - lens: T, - commit: GitCommit | undefined, - ): T { - lens.command = asCommand<[Uri, ShowQuickCommitFileCommandArgs]>({ - title: title, - command: commit?.isUncommitted ? '' : CodeLensCommand.ShowQuickCommitFileDetails, - arguments: [ - lens.uri!.toFileUri(), - { - commit: commit, - sha: commit === undefined ? undefined : commit.sha, - }, - ], - }); - return lens; - } +function applyShowQuickCommitDetailsCommand( + title: string, + lens: T, + commit: GitCommit | undefined, +): T { + lens.command = asCommand<[Uri, ShowQuickCommitCommandArgs]>({ + title: title, + command: commit?.isUncommitted ? '' : CodeLensCommand.ShowQuickCommitDetails, + arguments: [ + lens.uri!.toFileUri(), + { + commit: commit, + sha: commit === undefined ? undefined : commit.sha, + }, + ], + }); + return lens; +} - private applyShowQuickCurrentBranchHistoryCommand( - title: string, - lens: T, - ): T { - lens.command = asCommand<[Uri]>({ - title: title, - command: CodeLensCommand.ShowQuickCurrentBranchHistory, - arguments: [lens.uri!.toFileUri()], - }); - return lens; - } +function applyShowQuickCommitFileDetailsCommand( + title: string, + lens: T, + commit: GitCommit | undefined, +): T { + lens.command = asCommand<[Uri, ShowQuickCommitFileCommandArgs]>({ + title: title, + command: commit?.isUncommitted ? '' : CodeLensCommand.ShowQuickCommitFileDetails, + arguments: [ + lens.uri!.toFileUri(), + { + commit: commit, + sha: commit === undefined ? undefined : commit.sha, + }, + ], + }); + return lens; +} - private applyShowQuickFileHistoryCommand( - title: string, - lens: T, - ): T { - lens.command = asCommand<[Uri, ShowQuickFileHistoryCommandArgs]>({ - title: title, - command: CodeLensCommand.ShowQuickFileHistory, - arguments: [ - lens.uri!.toFileUri(), - { - range: lens.isFullRange ? undefined : lens.blameRange, - }, - ], - }); - return lens; - } +function applyShowQuickCurrentBranchHistoryCommand( + title: string, + lens: T, +): T { + lens.command = asCommand<[Uri]>({ + title: title, + command: CodeLensCommand.ShowQuickCurrentBranchHistory, + arguments: [lens.uri!.toFileUri()], + }); + return lens; +} - private applyToggleFileBlameCommand( - title: string, - lens: T, - ): T { - lens.command = asCommand<[Uri]>({ - title: title, - command: Commands.ToggleFileBlame, - arguments: [lens.uri!.toFileUri()], - }); - return lens; - } +function applyShowQuickFileHistoryCommand( + title: string, + lens: T, +): T { + lens.command = asCommand<[Uri, ShowQuickFileHistoryCommandArgs]>({ + title: title, + command: CodeLensCommand.ShowQuickFileHistory, + arguments: [ + lens.uri!.toFileUri(), + { + range: lens.isFullRange ? undefined : lens.blameRange, + }, + ], + }); + return lens; +} - private applyToggleFileChangesCommand( - title: string, - lens: T, - commit: GitCommit, - only?: boolean, - ): T { - lens.command = asCommand<[Uri, ToggleFileChangesAnnotationCommandArgs]>({ - title: title, - command: Commands.ToggleFileChanges, - arguments: [ - lens.uri!.toFileUri(), - { - type: FileAnnotationType.Changes, - context: { sha: commit.sha, only: only, selection: false }, - }, - ], - }); - return lens; - } +function applyToggleFileBlameCommand( + title: string, + lens: T, +): T { + lens.command = asCommand<[Uri]>({ + title: title, + command: Commands.ToggleFileBlame, + arguments: [lens.uri!.toFileUri()], + }); + return lens; +} - private applyToggleFileHeatmapCommand( - title: string, - lens: T, - ): T { - lens.command = asCommand<[Uri]>({ - title: title, - command: Commands.ToggleFileHeatmap, - arguments: [lens.uri!.toFileUri()], - }); - return lens; - } +function applyToggleFileChangesCommand( + title: string, + lens: T, + commit: GitCommit, + only?: boolean, +): T { + lens.command = asCommand<[Uri, ToggleFileChangesAnnotationCommandArgs]>({ + title: title, + command: Commands.ToggleFileChanges, + arguments: [ + lens.uri!.toFileUri(), + { + type: FileAnnotationType.Changes, + context: { sha: commit.sha, only: only, selection: false }, + }, + ], + }); + return lens; +} - private applyCommandWithNoClickAction( - title: string, - lens: T, - ): T { - lens.command = { - title: title, - command: '', - }; - return lens; - } +function applyToggleFileHeatmapCommand( + title: string, + lens: T, +): T { + lens.command = asCommand<[Uri]>({ + title: title, + command: Commands.ToggleFileHeatmap, + arguments: [lens.uri!.toFileUri()], + }); + return lens; +} - private getDirtyTitle(cfg: CodeLensConfig) { - if (cfg.recentChange.enabled && cfg.authors.enabled) { - return configuration.get('strings.codeLens.unsavedChanges.recentChangeAndAuthors'); - } - if (cfg.recentChange.enabled) return configuration.get('strings.codeLens.unsavedChanges.recentChangeOnly'); - return configuration.get('strings.codeLens.unsavedChanges.authorsOnly'); - } +function applyCommandWithNoClickAction( + title: string, + lens: T, +): T { + lens.command = { + title: title, + command: '', + }; + return lens; } function getRangeFromSymbol(symbol: DocumentSymbol | SymbolInformation) {