From 048047713667e4746e728558c6e1adf194a0323d Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Fri, 10 Mar 2017 12:37:20 -0500 Subject: [PATCH] Consolidates setContext into commands Adds context for toggling CodeLens --- package.json | 8 ++++---- src/blameabilityTracker.ts | 6 +++--- src/commands.ts | 18 +++++++++++++++++- src/commands/keyboard.ts | 6 +++--- src/extension.ts | 11 ++++++----- src/gitProvider.ts | 3 +++ 6 files changed, 36 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 61bffc7..606f722 100644 --- a/package.json +++ b/package.json @@ -491,7 +491,7 @@ }, { "command": "gitlens.toggleCodeLens", - "when": "gitlens:enabled" + "when": "gitlens:enabled && gitlens:canToggleCodeLens" }, { "command": "gitlens.showBlameHistory", @@ -669,7 +669,7 @@ "command": "gitlens.toggleCodeLens", "key": "alt+shift+b", "mac": "alt+shift+b", - "when": "editorTextFocus && gitlens:enabled" + "when": "editorTextFocus && gitlens:enabled && gitlens:canToggleCodeLens" }, { "command": "gitlens.showQuickFileHistory", @@ -746,11 +746,11 @@ "devDependencies": { "@types/copy-paste": "^1.1.30", "@types/mocha": "^2.2.39", - "@types/node": "^7.0.7", + "@types/node": "^7.0.8", "@types/tmp": "^0.0.32", "mocha": "^3.2.0", "tslint": "^4.5.1", "typescript": "^2.2.1", - "vscode": "^1.0.5" + "vscode": "^1.1.0" } } \ No newline at end of file diff --git a/src/blameabilityTracker.ts b/src/blameabilityTracker.ts index f427ae8..230312f 100644 --- a/src/blameabilityTracker.ts +++ b/src/blameabilityTracker.ts @@ -1,7 +1,7 @@ 'use strict'; -import { commands, Disposable, Event, EventEmitter, TextDocument, TextDocumentChangeEvent, TextEditor, window, workspace } from 'vscode'; +import { Disposable, Event, EventEmitter, TextDocument, TextDocumentChangeEvent, TextEditor, window, workspace } from 'vscode'; +import { CommandContext, setCommandContext } from './commands'; import { TextDocumentComparer } from './comparers'; -import { BuiltInCommands } from './constants'; import { GitProvider } from './gitProvider'; export interface BlameabilityChangeEvent { @@ -91,7 +91,7 @@ export class BlameabilityTracker extends Disposable { private updateBlameability(blameable: boolean, force: boolean = false) { if (!force && this._isBlameable === blameable) return; - commands.executeCommand(BuiltInCommands.SetContext, 'gitlens:isBlameable', blameable); + setCommandContext(CommandContext.IsBlameable, blameable); this._onDidChange.fire({ blameable: blameable, editor: this._editor diff --git a/src/commands.ts b/src/commands.ts index 090322b..708e665 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -1,4 +1,7 @@ 'use strict'; +import { commands } from 'vscode'; +import { BuiltInCommands } from './constants'; + export { Keyboard } from './commands/keyboard'; export { ActiveEditorCommand, Command, Commands, EditorCommand, openEditor } from './commands/commands'; @@ -20,4 +23,17 @@ export { ShowQuickFileHistoryCommand } from './commands/showQuickFileHistory'; export { ShowQuickRepoHistoryCommand } from './commands/showQuickRepoHistory'; export { ShowQuickRepoStatusCommand } from './commands/showQuickRepoStatus'; export { ToggleBlameCommand } from './commands/toggleBlame'; -export { ToggleCodeLensCommand } from './commands/toggleCodeLens'; \ No newline at end of file +export { ToggleCodeLensCommand } from './commands/toggleCodeLens'; + +export type CommandContext = 'gitlens:canToggleCodeLens' | 'gitlens:enabled' | 'gitlens:isBlameable' | 'gitlens:key'; +export const CommandContext = { + CanToggleCodeLens: 'gitlens:canToggleCodeLens' as CommandContext, + Enabled: 'gitlens:enabled' as CommandContext, + IsBlameable: 'gitlens:isBlameable' as CommandContext, + Key: 'gitlens:key' as CommandContext +}; + + +export function setCommandContext(key: CommandContext, value: any) { + return commands.executeCommand(BuiltInCommands.SetContext, key, value); +} \ No newline at end of file diff --git a/src/commands/keyboard.ts b/src/commands/keyboard.ts index bed1b6e..67c1985 100644 --- a/src/commands/keyboard.ts +++ b/src/commands/keyboard.ts @@ -1,6 +1,6 @@ 'use strict'; import { commands, Disposable, ExtensionContext, QuickPickItem } from 'vscode'; -import { BuiltInCommands } from '../constants'; +import { CommandContext, setCommandContext } from '../commands'; import { CommandQuickPickItem, OpenFileCommandQuickPickItem } from '../quickPicks/quickPicks'; //import { Logger } from '../logger'; @@ -53,7 +53,7 @@ export class Keyboard extends Disposable { } async enterScope(...keyCommands: [Keys, QuickPickItem][]) { - await commands.executeCommand(BuiltInCommands.SetContext, 'gitlens:key', ++scopeCount); + await setCommandContext(CommandContext.Key, ++scopeCount); if (keyCommands && Array.isArray(keyCommands) && keyCommands.length) { for (const [key, command] of keyCommands) { await this.setKeyCommand(key as Keys, command); @@ -62,7 +62,7 @@ export class Keyboard extends Disposable { } async exitScope(clear: boolean = true) { - await commands.executeCommand(BuiltInCommands.SetContext, 'gitlens:key', --scopeCount); + await setCommandContext(CommandContext.Key, --scopeCount); if (clear && !scopeCount) { for (const key of keys) { await this.clearKeyCommand(key); diff --git a/src/extension.ts b/src/extension.ts index d610060..36197e1 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,9 +1,10 @@ 'use strict'; -import { commands, ExtensionContext, languages, window, workspace } from 'vscode'; +import { ExtensionContext, languages, window, workspace } from 'vscode'; import { BlameabilityTracker } from './blameabilityTracker'; import { BlameActiveLineController } from './blameActiveLineController'; import { BlameAnnotationController } from './blameAnnotationController'; import { configureCssCharacters } from './blameAnnotationFormatter'; +import { CommandContext, setCommandContext } from './commands'; import { CloseUnchangedFilesCommand, OpenChangedFilesCommand } from './commands'; import { CopyMessageToClipboardCommand, CopyShaToClipboardCommand } from './commands'; import { DiffDirectoryCommand, DiffLineWithPreviousCommand, DiffLineWithWorkingCommand, DiffWithPreviousCommand, DiffWithWorkingCommand} from './commands'; @@ -13,7 +14,7 @@ import { ShowQuickCommitDetailsCommand, ShowQuickCommitFileDetailsCommand, ShowQ import { ToggleCodeLensCommand } from './commands'; import { Keyboard } from './commands'; import { IAdvancedConfig, IBlameConfig } from './configuration'; -import { BuiltInCommands, WorkspaceState } from './constants'; +import { WorkspaceState } from './constants'; import { GitContentProvider } from './gitContentProvider'; import { Git, GitProvider } from './gitProvider'; import { GitRevisionCodeLensProvider } from './gitRevisionCodeLensProvider'; @@ -47,7 +48,7 @@ export async function activate(context: ExtensionContext) { if (ex.message.includes('Unable to find git')) { await window.showErrorMessage(`GitLens was unable to find Git. Please make sure Git is installed. Also ensure that Git is either in the PATH, or that 'gitlens.advanced.git' is pointed to its installed location.`); } - commands.executeCommand(BuiltInCommands.SetContext, 'gitlens:enabled', false); + setCommandContext(CommandContext.Enabled, false); return; } @@ -59,11 +60,11 @@ export async function activate(context: ExtensionContext) { } let gitEnabled = workspace.getConfiguration('git').get('enabled'); - commands.executeCommand(BuiltInCommands.SetContext, 'gitlens:enabled', gitEnabled); + setCommandContext(CommandContext.Enabled, gitEnabled); context.subscriptions.push(workspace.onDidChangeConfiguration(() => { if (gitEnabled !== workspace.getConfiguration('git').get('enabled')) { gitEnabled = !gitEnabled; - commands.executeCommand(BuiltInCommands.SetContext, 'gitlens:enabled', gitEnabled); + setCommandContext(CommandContext.Enabled, gitEnabled); } }, this)); diff --git a/src/gitProvider.ts b/src/gitProvider.ts index f4dacfd..f84781a 100644 --- a/src/gitProvider.ts +++ b/src/gitProvider.ts @@ -1,6 +1,7 @@ 'use strict'; import { Iterables, Objects } from './system'; import { Disposable, Event, EventEmitter, ExtensionContext, FileSystemWatcher, languages, Location, Position, Range, TextDocument, TextEditor, Uri, workspace } from 'vscode'; +import { CommandContext, setCommandContext } from './commands'; import { CodeLensVisibility, IConfig } from './configuration'; import { DocumentSchemes, WorkspaceState } from './constants'; import Git, { GitBlameParserEnricher, GitBlameFormat, GitCommit, GitFileStatusItem, GitLogParserEnricher, IGitAuthor, IGitBlame, IGitBlameLine, IGitBlameLines, IGitLog } from './git/git'; @@ -143,6 +144,8 @@ export class GitProvider extends Disposable { this._codeLensProviderDisposable = undefined; this._codeLensProvider = undefined; } + + setCommandContext(CommandContext.CanToggleCodeLens, config.codeLens.visibility === CodeLensVisibility.OnDemand && (config.codeLens.recentChange.enabled || config.codeLens.authors.enabled)); } if (advancedChanged) {