From a255eea949c9f48aefd71577f9abd317a83d3ec0 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Sat, 26 Aug 2017 15:36:32 -0400 Subject: [PATCH] Splits code lens out of GitService --- src/codeLensController.ts | 81 ++++++++++++++++++++++++++++++++++ src/commands/toggleCodeLens.ts | 6 +-- src/extension.ts | 8 +++- src/gitService.ts | 54 ++--------------------- 4 files changed, 94 insertions(+), 55 deletions(-) create mode 100644 src/codeLensController.ts diff --git a/src/codeLensController.ts b/src/codeLensController.ts new file mode 100644 index 0000000..39d4832 --- /dev/null +++ b/src/codeLensController.ts @@ -0,0 +1,81 @@ +'use strict'; +import { Objects } from './system'; +import { Disposable, ExtensionContext, languages, TextEditor, workspace } from 'vscode'; +import { IConfig } from './configuration'; +import { CommandContext, ExtensionKey, setCommandContext } from './constants'; +import { GitCodeLensProvider } from './gitCodeLensProvider'; +import { GitService } from './gitService'; +import { Logger } from './logger'; + +export class CodeLensController extends Disposable { + + private _codeLensProvider: GitCodeLensProvider | undefined; + private _codeLensProviderDisposable: Disposable | undefined; + private _config: IConfig; + private _disposable: Disposable | undefined; + + constructor(private context: ExtensionContext, private git: GitService) { + super(() => this.dispose()); + + this._onConfigurationChanged(); + + const subscriptions: Disposable[] = []; + + subscriptions.push(workspace.onDidChangeConfiguration(this._onConfigurationChanged, this)); + subscriptions.push(git.onDidChangeGitCache(this._onGitCacheChanged, this)); + + this._disposable = Disposable.from(...subscriptions); + } + + dispose() { + this._disposable && this._disposable.dispose(); + + this._codeLensProviderDisposable && this._codeLensProviderDisposable.dispose(); + this._codeLensProviderDisposable = undefined; + this._codeLensProvider = undefined; + } + + private _onConfigurationChanged() { + const cfg = workspace.getConfiguration().get(ExtensionKey)!; + + if (!Objects.areEquivalent(cfg.codeLens, this._config && this._config.codeLens)) { + Logger.log('CodeLens config changed; resetting CodeLens provider'); + if (cfg.codeLens.enabled && (cfg.codeLens.recentChange.enabled || cfg.codeLens.authors.enabled)) { + if (this._codeLensProvider) { + this._codeLensProvider.reset(); + } + else { + this._codeLensProvider = new GitCodeLensProvider(this.context, this.git); + this._codeLensProviderDisposable = languages.registerCodeLensProvider(GitCodeLensProvider.selector, this._codeLensProvider); + } + } + else { + this._codeLensProviderDisposable && this._codeLensProviderDisposable.dispose(); + this._codeLensProviderDisposable = undefined; + this._codeLensProvider = undefined; + } + + setCommandContext(CommandContext.CanToggleCodeLens, cfg.codeLens.recentChange.enabled || cfg.codeLens.authors.enabled); + } + + this._config = cfg; + } + + private _onGitCacheChanged() { + Logger.log('Git cache changed; resetting CodeLens provider'); + this._codeLensProvider && this._codeLensProvider.reset(); + } + + toggleCodeLens(editor: TextEditor) { + if (!this._config.codeLens.recentChange.enabled && !this._config.codeLens.authors.enabled) return; + + Logger.log(`toggleCodeLens()`); + if (this._codeLensProviderDisposable) { + this._codeLensProviderDisposable.dispose(); + this._codeLensProviderDisposable = undefined; + return; + } + + this._codeLensProviderDisposable = languages.registerCodeLensProvider(GitCodeLensProvider.selector, new GitCodeLensProvider(this.context, this.git)); + } +} diff --git a/src/commands/toggleCodeLens.ts b/src/commands/toggleCodeLens.ts index b7294fb..aef3c54 100644 --- a/src/commands/toggleCodeLens.ts +++ b/src/commands/toggleCodeLens.ts @@ -1,15 +1,15 @@ 'use strict'; import { TextEditor, TextEditorEdit } from 'vscode'; +import { CodeLensController } from '../codeLensController'; import { Commands, EditorCommand } from './common'; -import { GitService } from '../gitService'; export class ToggleCodeLensCommand extends EditorCommand { - constructor(private git: GitService) { + constructor(private codeLensController: CodeLensController) { super(Commands.ToggleCodeLens); } execute(editor: TextEditor, edit: TextEditorEdit) { - return this.git.toggleCodeLens(editor); + return this.codeLensController.toggleCodeLens(editor); } } \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index bb11cdd..5ec6931 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -17,6 +17,7 @@ import { StashApplyCommand, StashDeleteCommand, StashSaveCommand } from './comma import { ToggleCodeLensCommand } from './commands'; import { CodeLensLocations, IConfig, LineHighlightLocations } from './configuration'; import { ApplicationInsightsKey, CommandContext, ExtensionKey, QualifiedExtensionId, setCommandContext, WorkspaceState } from './constants'; +import { CodeLensController } from './codeLensController'; import { CurrentLineController, LineAnnotationType } from './currentLineController'; import { GitContentProvider } from './gitContentProvider'; // import { GitExplorer } from './views/gitExplorer'; @@ -71,7 +72,7 @@ export async function activate(context: ExtensionContext) { await context.globalState.update(WorkspaceState.GitLensVersion, gitlensVersion); - const git = new GitService(context, repoPath); + const git = new GitService(repoPath); context.subscriptions.push(git); const gitContextTracker = new GitContextTracker(git); @@ -84,6 +85,9 @@ export async function activate(context: ExtensionContext) { const annotationController = new AnnotationController(context, git, gitContextTracker); context.subscriptions.push(annotationController); + const codeLensController = new CodeLensController(context, git); + context.subscriptions.push(codeLensController); + const currentLineController = new CurrentLineController(context, git, gitContextTracker, annotationController); context.subscriptions.push(currentLineController); @@ -134,7 +138,7 @@ export async function activate(context: ExtensionContext) { context.subscriptions.push(new StashApplyCommand(git)); context.subscriptions.push(new StashDeleteCommand(git)); context.subscriptions.push(new StashSaveCommand(git)); - context.subscriptions.push(new ToggleCodeLensCommand(git)); + context.subscriptions.push(new ToggleCodeLensCommand(codeLensController)); // Constantly over my data cap so stop collecting initialized event // Telemetry.trackEvent('initialized', Objects.flatten(cfg, 'config', true)); diff --git a/src/gitService.ts b/src/gitService.ts index 9d267cc..f871b10 100644 --- a/src/gitService.ts +++ b/src/gitService.ts @@ -1,11 +1,10 @@ 'use strict'; import { Functions, Iterables, Objects } from './system'; -import { Disposable, Event, EventEmitter, ExtensionContext, FileSystemWatcher, languages, Location, Position, Range, TextDocument, TextDocumentChangeEvent, TextEditor, Uri, workspace } from 'vscode'; +import { Disposable, Event, EventEmitter, FileSystemWatcher, Location, Position, Range, TextDocument, TextDocumentChangeEvent, TextEditor, Uri, workspace } from 'vscode'; import { IConfig } from './configuration'; -import { CommandContext, DocumentSchemes, ExtensionKey, GlyphChars, setCommandContext } from './constants'; +import { DocumentSchemes, ExtensionKey, GlyphChars } from './constants'; import { Git, GitAuthor, GitBlame, GitBlameCommit, GitBlameLine, GitBlameLines, GitBlameParser, GitBranch, GitCommit, GitDiff, GitDiffChunkLine, GitDiffParser, GitLog, GitLogCommit, GitLogParser, GitRemote, GitStash, GitStashParser, GitStatus, GitStatusFile, GitStatusParser, IGit, setDefaultEncoding } from './git/git'; import { GitUri, IGitCommitInfo, IGitUriData } from './git/gitUri'; -import { GitCodeLensProvider } from './gitCodeLensProvider'; import { Logger } from './logger'; import * as fs from 'fs'; import * as ignore from 'ignore'; @@ -90,8 +89,6 @@ export class GitService extends Disposable { private _uriCache: Map; config: IConfig; - private _codeLensProvider: GitCodeLensProvider | undefined; - private _codeLensProviderDisposable: Disposable | undefined; private _disposable: Disposable | undefined; private _gitignore: Promise; private _repoWatcher: FileSystemWatcher | undefined; @@ -99,7 +96,7 @@ export class GitService extends Disposable { static EmptyPromise: Promise = Promise.resolve(undefined); - constructor(private context: ExtensionContext, public repoPath: string) { + constructor(public repoPath: string) { super(() => this.dispose()); this._gitCache = new Map(); @@ -118,10 +115,6 @@ export class GitService extends Disposable { dispose() { this._disposable && this._disposable.dispose(); - this._codeLensProviderDisposable && this._codeLensProviderDisposable.dispose(); - this._codeLensProviderDisposable = undefined; - this._codeLensProvider = undefined; - this._cacheDisposable && this._cacheDisposable.dispose(); this._cacheDisposable = undefined; @@ -146,30 +139,7 @@ export class GitService extends Disposable { const cfg = workspace.getConfiguration().get(ExtensionKey)!; - const codeLensChanged = !Objects.areEquivalent(cfg.codeLens, this.config && this.config.codeLens); - const advancedChanged = !Objects.areEquivalent(cfg.advanced, this.config && this.config.advanced); - - if (codeLensChanged) { - Logger.log('CodeLens config changed; resetting CodeLens provider'); - if (cfg.codeLens.enabled && (cfg.codeLens.recentChange.enabled || cfg.codeLens.authors.enabled)) { - if (this._codeLensProvider) { - this._codeLensProvider.reset(); - } - else { - this._codeLensProvider = new GitCodeLensProvider(this.context, this); - this._codeLensProviderDisposable = languages.registerCodeLensProvider(GitCodeLensProvider.selector, this._codeLensProvider); - } - } - else { - this._codeLensProviderDisposable && this._codeLensProviderDisposable.dispose(); - this._codeLensProviderDisposable = undefined; - this._codeLensProvider = undefined; - } - - setCommandContext(CommandContext.CanToggleCodeLens, cfg.codeLens.recentChange.enabled || cfg.codeLens.authors.enabled); - } - - if (advancedChanged) { + if (!Objects.areEquivalent(cfg.advanced, this.config && this.config.advanced)) { if (cfg.advanced.caching.enabled) { this._cacheDisposable && this._cacheDisposable.dispose(); @@ -264,9 +234,6 @@ export class GitService extends Disposable { } private _fireGitCacheChangeCore() { - // Refresh the code lenses - this._codeLensProvider && this._codeLensProvider.reset(); - this._onDidChangeGitCache.fire(); } @@ -1053,19 +1020,6 @@ export class GitService extends Disposable { return Git.stash_save(repoPath, message, unstagedOnly); } - toggleCodeLens(editor: TextEditor) { - if (!this.config.codeLens.recentChange.enabled && !this.config.codeLens.authors.enabled) return; - - Logger.log(`toggleCodeLens()`); - if (this._codeLensProviderDisposable) { - this._codeLensProviderDisposable.dispose(); - this._codeLensProviderDisposable = undefined; - return; - } - - this._codeLensProviderDisposable = languages.registerCodeLensProvider(GitCodeLensProvider.selector, new GitCodeLensProvider(this.context, this)); - } - static getGitPath(gitPath?: string): Promise { return Git.getGitPath(gitPath); }