git: dirtydiff decorators lifecycle

This commit is contained in:
Joao Moreno
2016-10-11 18:06:38 +02:00
parent 4ec94e9525
commit eb07dfb380
3 changed files with 96 additions and 7 deletions

View File

@@ -5,22 +5,74 @@
'use strict';
import { Event, TextEditor, window } from 'vscode';
import { Event, TextEditor, window, workspace } from 'vscode';
import { IDisposable, dispose, mapEvent } from './util';
type TextEditorsEvent = Event<TextEditor[]>;
export class DirtyDiffDecorator implements IDisposable {
type IDecoration = void;
class ResourceDecorator implements IDisposable {
private textEditors: TextEditor[] = [];
constructor(private path: string) {
// console.log(`creating: ${this.path}`);
}
add(textEditor: TextEditor): void {
this.remove(textEditor);
this.textEditors.push(textEditor);
}
remove(textEditor: TextEditor): void {
this.textEditors = this.textEditors.filter(e => e !== textEditor);
}
get count(): number {
return this.textEditors.length;
}
dispose(): void {
// console.log(`disposing: ${this.path}`);
}
}
export class DirtyDiff implements IDisposable {
private textEditors: { editor: TextEditor; path: string; }[] = [];
private decorators: { [uri: string]: ResourceDecorator } = Object.create(null);
private disposables: IDisposable[] = [];
constructor() {
mapEvent(window.onDidChangeActiveTextEditor, () => window.visibleTextEditors)
(this.onDidVisibleEditorsChange, this, this.disposables);
const onVisibleTextEditorsChange = mapEvent(window.onDidChangeActiveTextEditor, () => window.visibleTextEditors);
onVisibleTextEditorsChange(this.onDidVisibleEditorsChange, this, this.disposables);
this.onDidVisibleEditorsChange(window.visibleTextEditors);
const watcher = workspace.createFileSystemWatcher('**');
this.disposables.push(watcher);
}
private onDidVisibleEditorsChange(textEditors: TextEditor[]) {
// TODO
const added = textEditors.filter(a => this.textEditors.every(({ editor }) => a !== editor)).map(editor => ({ editor, path: workspace.asRelativePath(editor.document.uri) }));
const removed = this.textEditors.filter(({ editor }) => textEditors.every(b => editor !== b));
this.textEditors = textEditors.map(editor => ({ editor, path: workspace.asRelativePath(editor.document.uri) }));
removed.forEach(({ editor, path }) => {
const decorator = this.decorators[path];
decorator.remove(editor);
if (decorator.count === 0) {
decorator.dispose();
delete this.decorators[path];
}
});
added.forEach(({ editor, path }) => {
const decorator = this.decorators[path] || (this.decorators[path] = new ResourceDecorator(path));
decorator.add(editor);
});
}
dispose(): void {

View File

@@ -6,11 +6,11 @@
'use strict';
import { ExtensionContext } from 'vscode';
import { DirtyDiffDecorator } from './dirtydiff';
import { DirtyDiff } from './dirtydiff';
export function activate(context: ExtensionContext) {
context.subscriptions.push(
new DirtyDiffDecorator()
new DirtyDiff()
);
}

View File

@@ -16,10 +16,47 @@ export function dispose<T extends IDisposable>(disposables: T[]): T[] {
return [];
}
export function combinedDisposable(disposables: IDisposable[]): IDisposable {
return { dispose: () => dispose(disposables) };
}
export function mapEvent<I, O>(event: Event<I>, map: (i: I) => O): Event<O> {
return (listener, thisArgs = null, disposables?) => event(i => listener.call(thisArgs, map(i)), null, disposables);
}
export function filterEvent<T>(event: Event<T>, filter: (e: T) => boolean): Event<T> {
return (listener, thisArgs = null, disposables?) => event(e => filter(e) && listener.call(thisArgs, e), null, disposables);
}
export function any<T>(...events: Event<T>[]): Event<T> {
return (listener, thisArgs = null, disposables?) => combinedDisposable(events.map(event => event(i => listener.call(thisArgs, i), disposables)));
}
interface IListener<T> {
(e: T): any;
}
export class Emitter<T> {
private listeners: IListener<T>[];
get event(): Event<T> {
return (listener: IListener<T>, thisArgs = null, disposables?: IDisposable[]) => {
const _listener = thisArgs ? listener.bind(thisArgs) : listener;
this.listeners.push(_listener);
const dispose = () => { this.listeners = this.listeners.filter(l => l !== _listener); };
const result = { dispose };
if (disposables) {
disposables.push(result);
}
return result;
};
}
fire(e: T = null): void {
}
}