Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git: re-add support for git decoration preferences #11674

Merged
merged 2 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 67 additions & 12 deletions packages/git/src/browser/git-decoration-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,55 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************

import { inject, injectable } from '@theia/core/shared/inversify';
import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
import { GitFileChange, GitFileStatus, GitStatusChangeEvent } from '../common';
import { CancellationToken, Emitter, Event } from '@theia/core/lib/common';
import { Decoration, DecorationsProvider } from '@theia/core/lib/browser/decorations-service';
import { GitRepositoryTracker } from './git-repository-tracker';
import URI from '@theia/core/lib/common/uri';
import { GitConfiguration, GitPreferences } from './git-preferences';
import { PreferenceChangeEvent } from '@theia/core/lib/browser';

@injectable()
export class GitDecorationProvider implements DecorationsProvider {

@inject(GitPreferences) protected readonly preferences: GitPreferences;
@inject(GitRepositoryTracker) protected readonly gitRepositoryTracker: GitRepositoryTracker;

protected decorationsEnabled: boolean;
protected colorsEnabled: boolean;

protected decorations = new Map<string, Decoration>();
protected uris = new Set<string>();

private readonly onDidChangeDecorationsEmitter = new Emitter<URI[]>();
readonly onDidChange: Event<URI[]> = this.onDidChangeDecorationsEmitter.event;

private decorations = new Map<string, Decoration>();
@postConstruct()
protected init(): void {
this.decorationsEnabled = this.preferences['git.decorations.enabled'];
this.colorsEnabled = this.preferences['git.decorations.colors'];
this.gitRepositoryTracker.onGitEvent((event: GitStatusChangeEvent | undefined) => this.handleGitEvent(event));
vince-fugnitto marked this conversation as resolved.
Show resolved Hide resolved
this.preferences.onPreferenceChanged((event: PreferenceChangeEvent<GitConfiguration>) => this.handlePreferenceChange(event));
}

constructor(@inject(GitRepositoryTracker) protected readonly gitRepositoryTracker: GitRepositoryTracker) {
this.gitRepositoryTracker.onGitEvent((event: GitStatusChangeEvent | undefined) => {
this.onGitEvent(event);
});
protected async handleGitEvent(event: GitStatusChangeEvent | undefined): Promise<void> {
this.updateDecorations(event);
this.triggerDecorationChange();
}

private async onGitEvent(event: GitStatusChangeEvent | undefined): Promise<void> {
protected updateDecorations(event?: GitStatusChangeEvent): void {
if (!event) {
return;
}

const newDecorations = new Map<string, Decoration>();
this.collectDecorationData(event.status.changes, newDecorations);

const uris = new Set([...this.decorations.keys()].concat([...newDecorations.keys()]));
this.uris = new Set([...this.decorations.keys()].concat([...newDecorations.keys()]));
this.decorations = newDecorations;
this.onDidChangeDecorationsEmitter.fire(Array.from(uris, value => new URI(value)));
}

private collectDecorationData(changes: GitFileChange[], bucket: Map<string, Decoration>): void {
protected collectDecorationData(changes: GitFileChange[], bucket: Map<string, Decoration>): void {
changes.forEach(change => {
const color = GitFileStatus.getColor(change.status, change.staged);
bucket.set(change.uri, {
Expand All @@ -61,7 +75,48 @@ export class GitDecorationProvider implements DecorationsProvider {
}

provideDecorations(uri: URI, token: CancellationToken): Decoration | Promise<Decoration | undefined> | undefined {
return this.decorations.get(uri.toString());
if (this.decorationsEnabled) {
const decoration = this.decorations.get(uri.toString());
if (decoration && !this.colorsEnabled) {
// Remove decoration color if disabled.
return {
...decoration,
colorId: undefined
};
}
return decoration;
}
return undefined;
}

protected handlePreferenceChange(event: PreferenceChangeEvent<GitConfiguration>): void {
const { preferenceName, newValue } = event;
let updateDecorations = false;
if (preferenceName === 'git.decorations.enabled') {
updateDecorations = true;
const decorationsEnabled = !!newValue;
if (this.decorationsEnabled !== decorationsEnabled) {
this.decorationsEnabled = decorationsEnabled;
}
}
if (preferenceName === 'git.decorations.colors') {
updateDecorations = true;
const colorsEnabled = !!newValue;
if (this.colorsEnabled !== colorsEnabled) {
this.colorsEnabled = colorsEnabled;
}
}
if (updateDecorations) {
this.triggerDecorationChange();
}
}

/**
* Notify that the provider has been updated to trigger a re-render of decorations.
*/
protected triggerDecorationChange(): void {
this.onDidChangeDecorationsEmitter.fire(Array.from(this.uris, value => new URI(value)));
}

}

2 changes: 1 addition & 1 deletion packages/git/src/browser/git-preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const GitConfigSchema: PreferenceSchema = {
'git.decorations.colors': {
'type': 'boolean',
'description': nls.localize('theia/git/gitDecorationsColors', 'Use color decoration in the navigator.'),
'default': false
'default': true
},
'git.editor.decorations.enabled': {
'type': 'boolean',
Expand Down