mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-23 01:58:53 +01:00
git: dirtydiff decorators lifecycle
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user