mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-01-14 01:25:43 -05:00
Splits code lens out of GitService
This commit is contained in:
81
src/codeLensController.ts
Normal file
81
src/codeLensController.ts
Normal file
@@ -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<IConfig>(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));
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
|
||||
@@ -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<string, UriCacheEntry>;
|
||||
|
||||
config: IConfig;
|
||||
private _codeLensProvider: GitCodeLensProvider | undefined;
|
||||
private _codeLensProviderDisposable: Disposable | undefined;
|
||||
private _disposable: Disposable | undefined;
|
||||
private _gitignore: Promise<ignore.Ignore | undefined>;
|
||||
private _repoWatcher: FileSystemWatcher | undefined;
|
||||
@@ -99,7 +96,7 @@ export class GitService extends Disposable {
|
||||
|
||||
static EmptyPromise: Promise<GitBlame | GitDiff | GitLog | undefined> = 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<IConfig>(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<IGit> {
|
||||
return Git.getGitPath(gitPath);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user