From 7b4dd77fbc63296965ef977ea013c259f0117a2b Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 19 Sep 2016 21:28:52 -0400 Subject: [PATCH] Adds new CodeLens visibility & location settings Adds new command to toggle CodeLens on and off when `gitlens.codeLens.visibility` is set to `ondemand` --- README.md | 28 +++++++++----- package.json | 50 ++++++++++++++++++++---- src/commands.ts | 10 +++++ src/configuration.ts | 18 +++++++++ src/constants.ts | 7 ++-- src/extension.ts | 3 +- src/gitBlameController.ts | 2 +- src/gitCodeLensProvider.ts | 95 ++++++++++++++++++++++++++++++---------------- src/gitProvider.ts | 50 +++++++++++++++++++----- 9 files changed, 200 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index cae8030..c1cc772 100644 --- a/README.md +++ b/README.md @@ -21,15 +21,18 @@ Must be using Git and it must be in your path. |Name | Description |-----|------------ -|_gitlens.blame.annotation.style_|Specifies the style of the blame annotations. Compact - groups annotations to limit the repetition and also adds author and date when possible. Expanded - shows an annotation on every line -|_gitlens.blame.annotation.sha_|Specifies whether the commit sha will be shown in the blame annotations. Applies only to the Expanded annotation style -|_gitlens.blame.annotation.author_|Specifies whether the committer will be shown in the blame annotations. Applies only to the Expanded annotation style -|_gitlens.blame.annotation.date_|Specifies whether the commit date will be shown in the blame annotations. Applies only to the Expanded annotation style -|_gitlens.blame.annotation.useCodeActions_|Specifies whether code actions (Diff with Working, Diff with Previous) will be provided for the selected line, when annotating. Not required as context menu options are always provided -|_gitlens.codeLens.recentChange.enabled_|Specifies whether the recent change CodeLens is shown -|_gitlens.codeLens.recentChange.command_|Specifies the command executed when the recent change CodeLens is clicked. Annotate - toggles blame annotations. Explorer - opens the blame explorer. History - opens a file history picker, which requires the Git History (git log) extension -|_gitlens.codeLens.authors.enabled_|Specifies whether the authors CodeLens is shown -|_gitlens.codeLens.authors.command_|Specifies the command executed when the authors CodeLens is clicked. Annotate - toggles blame annotations. Explorer - opens the blame explorer. History - opens a file history picker, which requires the Git History (git log) extension +|`gitlens.blame.annotation.style`|Specifies the style of the blame annotations. `compact` - groups annotations to limit the repetition and also adds author and date when possible. `expanded` - shows an annotation on every line +|`gitlens.blame.annotation.sha`|Specifies whether the commit sha will be shown in the blame annotations. Applies only to the `expanded` annotation style +|`gitlens.blame.annotation.author`|Specifies whether the committer will be shown in the blame annotations. Applies only to the `expanded` annotation style +|`gitlens.blame.annotation.date`|Specifies whether the commit date will be shown in the blame annotations. Applies only to the `expanded` annotation style +|`gitlens.blame.annotation.useCodeActions`|Specifies whether code actions (Diff with Working, Diff with Previous) will be provided for the selected line, when annotating. Not required as context menu options are always provided +|`gitlens.codeLens.visibility`|Specifies when CodeLens will be triggered in the active document. `auto` - automatically. `ondemand` - only when requested. `off` - disables all active document CodeLens +|`gitlens.codeLens.location`|Specifies where CodeLens will be rendered in the active document. `all` - render at the top of the document, on container-like (classes, modules, etc), and on member-like (methods, functions, properties, etc) lines. `document+containers` - render at the top of the document and on container-like lines. `document` - only render at the top of the document. `custom` - rendering controlled by `gitlens.codeLens.locationCustomSymbols` +|`gitlens.codeLens.locationCustomSymbols`|Specifies the set of document symbols to render active document CodeLens on. Must be a member of `SymbolKind` +|`gitlens.codeLens.recentChange.enabled`|Specifies whether the recent change CodeLens is shown +|`gitlens.codeLens.recentChange.command`|Specifies the command executed when the recent change CodeLens is clicked. `blame.annotate` - toggles blame annotations. `blame.explorer` - opens the blame explorer. `git.history` - opens a file history picker, which requires the Git History (git log) extension +|`gitlens.codeLens.authors.enabled`|Specifies whether the authors CodeLens is shown +|`gitlens.codeLens.authors.command`|Specifies the command executed when the authors CodeLens is clicked. `blame.annotate` - toggles blame annotations. `blame.explorer` - opens the blame explorer. `git.history` - opens a file history picker, which requires the Git History (git log) extension ## Known Issues @@ -40,6 +43,11 @@ Must be using Git and it must be in your path. ## Release Notes +### 0.3.0 + + - Adds new CodeLens visibility & location settings -- see **Extension Settings** above for details + - Adds new command to toggle CodeLens on and off when `gitlens.codeLens.visibility` is set to `ondemand` + ### 0.2.0 - Fixes [#1](https://github.com/eamodio/vscode-gitlens/issues/1) - Support blame on files outside the workspace repository @@ -52,7 +60,7 @@ Must be using Git and it must be in your path. - Improved blame annotations, now with sha and author by default - Add new blame annotation styles -- compact and expanded (default) - - Adds many new configuration settings; see Contributions tab above + - Adds many new configuration settings; see **Extension Settings** above for details ### 0.0.7 diff --git a/package.json b/package.json index 8fa4e02..a01e9b6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gitlens", - "version": "0.2.0", + "version": "0.3.0", "author": { "name": "Eric Amodio", "email": "eamodio@gmail.com" @@ -45,28 +45,53 @@ "compact", "expanded" ], - "description": "Specifies the style of the blame annotations. Compact - groups annotations to limit the repetition and also adds author and date when possible. Expanded - shows an annotation on every line" + "description": "Specifies the style of the blame annotations. `compact` - groups annotations to limit the repetition and also adds author and date when possible. `expanded` - shows an annotation on every line" }, "gitlens.blame.annotation.sha": { "type": "boolean", "default": true, - "description": "Specifies whether the commit sha will be shown in the blame annotations. Applies only to the Expanded annotation style" + "description": "Specifies whether the commit sha will be shown in the blame annotations. Applies only to the `expanded` annotation style" }, "gitlens.blame.annotation.author": { "type": "boolean", "default": true, - "description": "Specifies whether the committer will be shown in the blame annotations. Applies only to the Expanded annotation style" + "description": "Specifies whether the committer will be shown in the blame annotations. Applies only to the `expanded` annotation style" }, "gitlens.blame.annotation.date": { "type": "boolean", "default": false, - "description": "Specifies whether the commit date will be shown in the blame annotations. Applies only to the Expanded annotation style" + "description": "Specifies whether the commit date will be shown in the blame annotations. Applies only to the `expanded` annotation style" }, "gitlens.blame.annotation.useCodeActions": { "type": "boolean", "default": false, "description": "Specifies whether code actions (Diff with Working, Diff with Previous) will be provided for the selected line, when annotating. Not required as context menu options are always provided" }, + "gitlens.codeLens.visibility": { + "type": "string", + "default": "auto", + "enum": [ + "auto", + "ondemand", + "off" + ], + "description": "Specifies when CodeLens will be triggered in the active document. `auto` - automatically. `ondemand` - only when requested. `off` - disables all active document CodeLens" + }, + "gitlens.codeLens.location": { + "type": "string", + "default": "document+containers", + "enum": [ + "all", + "document+containers", + "document", + "custom" + ], + "description": "Specifies where CodeLens will be rendered in the active document. `all` - render at the top of the document, on container-like (classes, modules, etc), and on member-like (methods, functions, properties, etc) lines. `document+containers` - render at the top of the document and on container-like lines. `document` - only render at the top of the document. `custom` - rendering controlled by `gitlens.codeLens.locationCustomSymbols`" + }, + "gitlens.codeLens.locationCustomSymbols": { + "type": "array", + "description": "Specifies the set of document symbols to render active document CodeLens on. Must be a member of `SymbolKind`" + }, "gitlens.codeLens.recentChange.enabled": { "type": "boolean", "default": true, @@ -80,7 +105,7 @@ "blame.explorer", "git.history" ], - "description": "Specifies the command executed when the recent change CodeLens is clicked. Annotate - toggles blame annotations. Explorer - opens the blame explorer. History - opens a file history picker, which requires the Git History (git log) extension" + "description": "Specifies the command executed when the recent change CodeLens is clicked. `blame.annotate` - toggles blame annotations. `blame.explorer` - opens the blame explorer. `git.history` - opens a file history picker, which requires the Git History (git log) extension" }, "gitlens.codeLens.authors.enabled": { "type": "boolean", @@ -95,7 +120,7 @@ "blame.explorer", "git.history" ], - "description": "Specifies the command executed when the authors CodeLens is clicked. Annotate - toggles blame annotations. Explorer - opens the blame explorer. History - opens a file history picker, which requires the Git History (git log) extension" + "description": "Specifies the command executed when the authors CodeLens is clicked. `blame.annotate` - toggles blame annotations. `blame.explorer` - opens the blame explorer. `git.history` - opens a file history picker, which requires the Git History (git log) extension" }, "gitlens.advanced.caching.enabled": { "type": "boolean", @@ -123,6 +148,11 @@ "command": "gitlens.toggleBlame", "title": "Git: Toggle Blame", "category": "GitLens" + }, + { + "command": "gitlens.toggleCodeLens", + "title": "Git: Toggle CodeLens", + "category": "GitLens" }], "menus": { "editor/title": [{ @@ -152,6 +182,12 @@ "key": "alt+b", "mac": "alt+b", "when": "editorTextFocus" + }, + { + "command": "gitlens.toggleCodeLens", + "key": "alt+shift+b", + "mac": "alt+shift+b", + "when": "editorTextFocus" }] }, "activationEvents": [ diff --git a/src/commands.ts b/src/commands.ts index eb0834b..8483b4a 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -154,4 +154,14 @@ export class ToggleBlameCommand extends EditorCommand { .catch(ex => console.error('[GitLens.ToggleBlameCommand]', 'getBlameForLine', ex)) .then(blame => this.blameController.toggleBlame(editor, blame && blame.commit.sha)); } +} + +export class ToggleCodeLensCommand extends EditorCommand { + constructor(private git: GitProvider) { + super(Commands.ToggleCodeLens); + } + + execute(editor: TextEditor, edit: TextEditorEdit) { + return this.git.toggleCodeLens(editor); + } } \ No newline at end of file diff --git a/src/configuration.ts b/src/configuration.ts index 7f26f94..43c79b5 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -23,12 +23,30 @@ export const CodeLensCommand = { GitHistory: 'git.history' as CodeLensCommand } +export type CodeLensLocation = 'all' | 'document+containers' | 'document' | 'custom'; +export const CodeLensLocation = { + All: 'all' as CodeLensLocation, + DocumentAndContainers: 'document+containers' as CodeLensLocation, + Document: 'document' as CodeLensLocation, + Custom: 'custom' as CodeLensLocation, +} + +export type CodeLensVisibility = 'auto' | 'ondemand' | 'off'; +export const CodeLensVisibility = { + Auto: 'auto' as CodeLensVisibility, + OnDemand: 'ondemand' as CodeLensVisibility, + Off: 'off' as CodeLensVisibility +} + export interface ICodeLensConfig { enabled: boolean; command: CodeLensCommand; } export interface ICodeLensesConfig { + visibility: CodeLensVisibility; + location: CodeLensLocation; + locationCustomSymbols: string[]; recentChange: ICodeLensConfig; authors: ICodeLensConfig; } diff --git a/src/constants.ts b/src/constants.ts index 9240a80..cbddf2e 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -16,20 +16,21 @@ export const BuiltInCommands = { ToggleRenderWhitespace: 'editor.action.toggleRenderWhitespace' as BuiltInCommands } -export type Commands = 'gitlens.diffWithPrevious' | 'gitlens.diffWithWorking' | 'gitlens.showBlame' | 'gitlens.showHistory' | 'gitlens.toggleBlame'; +export type Commands = 'gitlens.diffWithPrevious' | 'gitlens.diffWithWorking' | 'gitlens.showBlame' | 'gitlens.showHistory' | 'gitlens.toggleBlame' | 'gitlens.toggleCodeLens'; export const Commands = { DiffWithPrevious: 'gitlens.diffWithPrevious' as Commands, DiffWithWorking: 'gitlens.diffWithWorking' as Commands, ShowBlame: 'gitlens.showBlame' as Commands, ShowBlameHistory: 'gitlens.showHistory' as Commands, ToggleBlame: 'gitlens.toggleBlame' as Commands, + ToggleCodeLens: 'gitlens.toggleCodeLens' as Commands, } -export type DocumentSchemes = 'file' | 'git' | 'gitblame'; +export type DocumentSchemes = 'file' | 'git' | 'git-blame'; export const DocumentSchemes = { File: 'file' as DocumentSchemes, Git: 'git' as DocumentSchemes, - GitBlame: 'gitblame' as DocumentSchemes + GitBlame: 'git-blame' as DocumentSchemes } export type WorkspaceState = 'hasGitHistoryExtension' | 'repoPath'; diff --git a/src/extension.ts b/src/extension.ts index 4ed5a67..dba7c17 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -5,7 +5,7 @@ import GitBlameCodeLensProvider from './gitBlameCodeLensProvider'; import GitBlameContentProvider from './gitBlameContentProvider'; import GitBlameController from './gitBlameController'; import GitProvider, {Git} from './gitProvider'; -import {DiffWithPreviousCommand, DiffWithWorkingCommand, ShowBlameCommand, ShowBlameHistoryCommand, ToggleBlameCommand} from './commands'; +import {DiffWithPreviousCommand, DiffWithWorkingCommand, ShowBlameCommand, ShowBlameHistoryCommand, ToggleBlameCommand, ToggleCodeLensCommand} from './commands'; import {ICodeLensesConfig} from './configuration'; import {WorkspaceState} from './constants'; @@ -41,6 +41,7 @@ export function activate(context: ExtensionContext) { context.subscriptions.push(new ShowBlameCommand(git, blameController)); context.subscriptions.push(new ToggleBlameCommand(git, blameController)); context.subscriptions.push(new ShowBlameHistoryCommand(git)); + context.subscriptions.push(new ToggleCodeLensCommand(git)); }).catch(reason => console.warn('[GitLens]', reason)); } diff --git a/src/gitBlameController.ts b/src/gitBlameController.ts index cd5c92a..e9c04b3 100644 --- a/src/gitBlameController.ts +++ b/src/gitBlameController.ts @@ -50,7 +50,7 @@ export default class GitBlameController extends Disposable { // })); workspace.onDidCloseTextDocument(d => { - if (!this._controller || this._controller.uri.fsPath !== d.uri.fsPath) return; + if (!this._controller || this._controller.uri.toString() !== d.uri.toString()) return; this.clear(); }) diff --git a/src/gitCodeLensProvider.ts b/src/gitCodeLensProvider.ts index 406bac0..fa75e0e 100644 --- a/src/gitCodeLensProvider.ts +++ b/src/gitCodeLensProvider.ts @@ -1,7 +1,7 @@ 'use strict'; import {CancellationToken, CodeLens, CodeLensProvider, commands, DocumentSelector, ExtensionContext, Location, Position, Range, SymbolInformation, SymbolKind, TextDocument, Uri, window, workspace} from 'vscode'; import {BuiltInCommands, Commands, DocumentSchemes, WorkspaceState} from './constants'; -import {CodeLensCommand, ICodeLensesConfig} from './configuration'; +import {CodeLensCommand, CodeLensLocation, ICodeLensesConfig} from './configuration'; import GitProvider, {IGitBlame, IGitBlameLines, IGitCommit} from './gitProvider'; import * as _ from 'lodash'; import * as moment from 'moment'; @@ -49,14 +49,16 @@ export default class GitCodeLensProvider implements CodeLensProvider { const lenses: CodeLens[] = []; symbols.forEach(sym => this._provideCodeLens(fileName, document, sym, lenses)); - // Check if we have a lens for the whole document -- if not add one - if (!lenses.find(l => l.range.start.line === 0 && l.range.end.line === 0)) { - const blameRange = document.validateRange(new Range(0, 1000000, 1000000, 1000000)); - if (this._config.recentChange.enabled) { - lenses.push(new GitRecentChangeCodeLens(this.git, fileName, SymbolKind.File, blameRange, new Range(0, 0, 0, blameRange.start.character))); - } - if (this._config.authors.enabled) { - lenses.push(new GitAuthorsCodeLens(this.git, fileName, SymbolKind.File, blameRange, new Range(0, 1, 0, blameRange.start.character))); + if (this._config.location !== CodeLensLocation.Custom || (this._config.locationCustomSymbols || []).find(_ => _.toLowerCase() === 'file')) { + // Check if we have a lens for the whole document -- if not add one + if (!lenses.find(l => l.range.start.line === 0 && l.range.end.line === 0)) { + const blameRange = document.validateRange(new Range(0, 1000000, 1000000, 1000000)); + if (this._config.recentChange.enabled) { + lenses.push(new GitRecentChangeCodeLens(this.git, fileName, SymbolKind.File, blameRange, new Range(0, 0, 0, blameRange.start.character))); + } + if (this._config.authors.enabled) { + lenses.push(new GitAuthorsCodeLens(this.git, fileName, SymbolKind.File, blameRange, new Range(0, 1, 0, blameRange.start.character))); + } } } @@ -64,29 +66,36 @@ export default class GitCodeLensProvider implements CodeLensProvider { }); } - foo: string; bar: number; + private _isValidSymbol(kind: SymbolKind) { + switch (this._config.location) { + case CodeLensLocation.All: + case CodeLensLocation.DocumentAndContainers: + switch (kind) { + case SymbolKind.File: + case SymbolKind.Package: + case SymbolKind.Module: + case SymbolKind.Namespace: + case SymbolKind.Class: + case SymbolKind.Interface: + return true; + case SymbolKind.Constructor: + case SymbolKind.Method: + case SymbolKind.Function: + case SymbolKind.Property: + case SymbolKind.Enum: + return this._config.location === CodeLensLocation.All; + default: + return false; + } + case CodeLensLocation.Document: + return false; + case CodeLensLocation.Custom: + return !!(this._config.locationCustomSymbols || []).find(_ => _.toLowerCase() === SymbolKind[kind].toLowerCase()); + } + } private _provideCodeLens(fileName: string, document: TextDocument, symbol: SymbolInformation, lenses: CodeLens[]): void { - let multiline = false; - switch (symbol.kind) { - case SymbolKind.Package: - case SymbolKind.Module: - case SymbolKind.Class: - case SymbolKind.Interface: - case SymbolKind.Constructor: - case SymbolKind.Method: - case SymbolKind.Function: - case SymbolKind.Enum: - // HACK for Omnisharp, since it doesn't return full ranges - multiline = fileName.endsWith('.cs') || (symbol.location.range.end.line - symbol.location.range.start.line) > 1; - break; - case SymbolKind.Property: - multiline = (symbol.location.range.end.line - symbol.location.range.start.line) > 1; - if (!multiline) return; - break; - default: - return; - } + if (!this._isValidSymbol(symbol.kind)) return; const line = document.lineAt(symbol.location.range.start); // Make sure there is only 1 lense per line @@ -109,8 +118,30 @@ export default class GitCodeLensProvider implements CodeLensProvider { lenses.push(new GitRecentChangeCodeLens(this.git, fileName, symbol.kind, symbol.location.range, line.range.with(new Position(line.range.start.line, startChar)))); startChar++; } - if (multiline && this._config.authors.enabled) { - lenses.push(new GitAuthorsCodeLens(this.git, fileName, symbol.kind, symbol.location.range, line.range.with(new Position(line.range.start.line, startChar)))); + + if (this._config.authors.enabled) { + // HACK for Omnisharp, since it doesn't return full ranges + let multiline = (symbol.location.range.end.line - symbol.location.range.start.line) > 1; + if (!multiline && fileName.endsWith('.cs')) { + switch (symbol.kind) { + case SymbolKind.File: + case SymbolKind.Package: + case SymbolKind.Module: + case SymbolKind.Namespace: + case SymbolKind.Class: + case SymbolKind.Interface: + case SymbolKind.Constructor: + case SymbolKind.Method: + case SymbolKind.Function: + case SymbolKind.Enum: + multiline = true; + break; + } + } + + if (multiline) { + lenses.push(new GitAuthorsCodeLens(this.git, fileName, symbol.kind, symbol.location.range, line.range.with(new Position(line.range.start.line, startChar)))); + } } } diff --git a/src/gitProvider.ts b/src/gitProvider.ts index fcb88ac..e0c5b57 100644 --- a/src/gitProvider.ts +++ b/src/gitProvider.ts @@ -1,7 +1,7 @@ 'use strict' -import {Disposable, ExtensionContext, languages, Location, Position, Range, Uri, workspace} from 'vscode'; +import {Disposable, DocumentFilter, ExtensionContext, languages, Location, Position, Range, TextDocument, TextEditor, Uri, window, workspace} from 'vscode'; import {DocumentSchemes, WorkspaceState} from './constants'; -import {IConfig} from './configuration'; +import {CodeLensVisibility, IConfig} from './configuration'; import GitCodeLensProvider from './gitCodeLensProvider'; import Git, {GitBlameParserEnricher, GitBlameFormat, GitCommit, IGitAuthor, IGitBlame, IGitBlameCommitLines, IGitBlameLine, IGitBlameLines, IGitCommit} from './git/git'; import * as fs from 'fs' @@ -32,6 +32,7 @@ export default class GitProvider extends Disposable { private _config: IConfig; private _disposable: Disposable; private _codeLensProviderDisposable: Disposable; + private _codeLensProviderSelector: DocumentFilter; private _gitignore: Promise; static BlameEmptyPromise = Promise.resolve(null); @@ -84,8 +85,9 @@ export default class GitProvider extends Disposable { if (!_.isEqual(config.codeLens, this._config && this._config.codeLens)) { this._codeLensProviderDisposable && this._codeLensProviderDisposable.dispose(); - if (config.codeLens.recentChange.enabled || config.codeLens.authors.enabled) { - this._codeLensProviderDisposable = languages.registerCodeLensProvider(GitCodeLensProvider.selector, new GitCodeLensProvider(this.context, this)); + if (config.codeLens.visibility === CodeLensVisibility.Auto && (config.codeLens.recentChange.enabled || config.codeLens.authors.enabled)) { + this._codeLensProviderSelector = GitCodeLensProvider.selector; + this._codeLensProviderDisposable = languages.registerCodeLensProvider(this._codeLensProviderSelector, new GitCodeLensProvider(this.context, this)); } else { this._codeLensProviderDisposable = null; } @@ -99,11 +101,11 @@ export default class GitProvider extends Disposable { const disposables: Disposable[] = []; // TODO: Maybe stop clearing on close and instead limit to a certain number of recent blames - disposables.push(workspace.onDidCloseTextDocument(d => this._removeCachedBlame(d.fileName, RemoveCacheReason.DocumentClosed))); + disposables.push(workspace.onDidCloseTextDocument(d => this._removeCachedBlame(d, RemoveCacheReason.DocumentClosed))); const removeCachedBlameFn = _.debounce(this._removeCachedBlame.bind(this), 2500); - disposables.push(workspace.onDidSaveTextDocument(d => removeCachedBlameFn(d.fileName, RemoveCacheReason.DocumentSaved))); - disposables.push(workspace.onDidChangeTextDocument(e => removeCachedBlameFn(e.document.fileName, RemoveCacheReason.DocumentChanged))); + disposables.push(workspace.onDidSaveTextDocument(d => removeCachedBlameFn(d, RemoveCacheReason.DocumentSaved))); + disposables.push(workspace.onDidChangeTextDocument(e => removeCachedBlameFn(e.document, RemoveCacheReason.DocumentChanged))); this._blameCacheDisposable = Disposable.from(...disposables); } else { @@ -121,10 +123,11 @@ export default class GitProvider extends Disposable { return fileName.toLowerCase(); } - private _removeCachedBlame(fileName: string, reason: RemoveCacheReason) { + private _removeCachedBlame(document: TextDocument, reason: RemoveCacheReason) { if (!this.UseCaching) return; + if (document.uri.scheme != DocumentSchemes.File) return; - fileName = Git.normalizePath(fileName); + const fileName = Git.normalizePath(document.fileName); const cacheKey = this._getBlameCacheKey(fileName); if (reason === RemoveCacheReason.DocumentClosed) { @@ -300,6 +303,35 @@ export default class GitProvider extends Disposable { return Git.getVersionedFileText(fileName, repoPath, sha); } + toggleCodeLens(editor: TextEditor) { + if (this._config.codeLens.visibility !== CodeLensVisibility.OnDemand || + (!this._config.codeLens.recentChange.enabled && !this._config.codeLens.authors.enabled)) return; + + if (this._codeLensProviderDisposable) { + this._codeLensProviderDisposable.dispose(); + + if (editor.document.fileName === (this._codeLensProviderSelector && this._codeLensProviderSelector.pattern)) { + this._codeLensProviderDisposable = null; + return; + } + } + + const disposables: Disposable[] = []; + + this._codeLensProviderSelector = { scheme: DocumentSchemes.File, pattern: editor.document.fileName }; + + disposables.push(languages.registerCodeLensProvider(this._codeLensProviderSelector, new GitCodeLensProvider(this.context, this))); + + disposables.push(window.onDidChangeActiveTextEditor(e => { + if (e.viewColumn && e.document !== editor.document) { + this._codeLensProviderDisposable && this._codeLensProviderDisposable.dispose(); + this._codeLensProviderDisposable = null; + } + })); + + this._codeLensProviderDisposable = Disposable.from(...disposables); + } + static fromBlameUri(uri: Uri): IGitBlameUriData { if (uri.scheme !== DocumentSchemes.GitBlame) throw new Error(`fromGitUri(uri=${uri}) invalid scheme`); const data = GitProvider._fromGitUri(uri);