Switch to using GroupChange event

This commit is contained in:
Logan Ramos 2021-11-17 13:59:46 -05:00
parent 70c528430e
commit c37587ee2d
No known key found for this signature in database
GPG key ID: D9CCFF14F0B18183
4 changed files with 122 additions and 58 deletions

View file

@ -524,48 +524,46 @@ export class EditorGroupView extends Themable implements IEditorGroupView {
// Model Events
this._register(this.model.onDidModelChange(e => {
if (e.kind === GroupChangeKind.GROUP_LOCKED) {
this.onDidChangeGroupLocked();
return;
}
if (!e.editor) {
return;
}
switch (e.kind) {
case GroupChangeKind.GROUP_LOCKED:
this.onDidChangeGroupLocked();
break;
case GroupChangeKind.EDITOR_PIN:
if (e.editorInputOrEvent instanceof EditorInput) {
this.onDidChangeEditorPinned(e.editorInputOrEvent);
}
this.onDidChangeEditorPinned(e.editor);
break;
case GroupChangeKind.EDITOR_STICKY:
if (e.editorInputOrEvent instanceof EditorInput) {
this.onDidChangeEditorSticky(e.editorInputOrEvent);
}
this.onDidChangeEditorSticky(e.editor);
break;
case GroupChangeKind.EDITOR_MOVE:
this.onDidMoveEditor(e.editorInputOrEvent as IEditorMoveEvent);
if (e.oldEditorIndex !== undefined && e.editorIndex !== undefined) {
this.onDidMoveEditor({ editor: e.editor, index: e.oldEditorIndex, newIndex: e.editorIndex, target: this.id, groupId: this.id });
}
break;
case GroupChangeKind.EDITOR_OPEN:
this.onDidOpenEditor(e.editorInputOrEvent as IEditorOpenEvent);
if (e.editorIndex !== undefined) {
this.onDidOpenEditor({ editor: e.editor, index: e.editorIndex, groupId: this.id });
}
break;
case GroupChangeKind.EDITOR_CLOSE:
this.handleOnDidCloseEditor(e.editorInputOrEvent as IEditorCloseEvent);
if (e.editorIndex !== undefined && e.closeContext !== undefined && e.closedSticky !== undefined) {
this.handleOnDidCloseEditor({ editor: e.editor, index: e.editorIndex, groupId: this.id, context: e.closeContext, sticky: e.closedSticky });
}
break;
case GroupChangeKind.EDITOR_DISPOSE:
if (e.editorInputOrEvent instanceof EditorInput) {
this.onWillDisposeEditor(e.editorInputOrEvent);
}
this.onWillDisposeEditor(e.editor);
break;
case GroupChangeKind.EDITOR_DIRTY:
if (e.editorInputOrEvent instanceof EditorInput) {
this.onDidChangeEditorDirty(e.editorInputOrEvent);
}
this.onDidChangeEditorDirty(e.editor);
break;
case GroupChangeKind.EDITOR_LABEL:
if (e.editorInputOrEvent instanceof EditorInput) {
this.onDidChangeEditorLabel(e.editorInputOrEvent);
}
this.onDidChangeEditorLabel(e.editor);
break;
case GroupChangeKind.EDITOR_CAPABILITIES:
if (e.editorInputOrEvent instanceof EditorInput) {
this.onDidChangeEditorCapabilities(e.editorInputOrEvent);
}
this.onDidChangeEditorCapabilities(e.editor);
break;
}
}));

View file

@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import { Event, Emitter } from 'vs/base/common/event';
import { IEditorFactoryRegistry, GroupIdentifier, EditorsOrder, EditorExtensions, IUntypedEditorInput, SideBySideEditor, IEditorMoveEvent, IEditorOpenEvent, EditorCloseContext, IEditorCloseEvent } from 'vs/workbench/common/editor';
import { IEditorFactoryRegistry, GroupIdentifier, EditorsOrder, EditorExtensions, IUntypedEditorInput, SideBySideEditor, EditorCloseContext, IEditorCloseEvent } from 'vs/workbench/common/editor';
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
import { SideBySideEditorInput } from 'vs/workbench/common/editor/sideBySideEditorInput';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
@ -12,7 +12,7 @@ import { IConfigurationService } from 'vs/platform/configuration/common/configur
import { dispose, Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { Registry } from 'vs/platform/registry/common/platform';
import { coalesce } from 'vs/base/common/arrays';
import { GroupChangeKind } from 'vs/workbench/services/editor/common/editorGroupsService';
import { GroupChangeKind, IGroupChangeEvent } from 'vs/workbench/services/editor/common/editorGroupsService';
const EditorOpenPositioning = {
LEFT: 'left',
@ -71,18 +71,13 @@ export interface IMatchOptions {
strictEquals?: boolean;
}
export interface EditorGroupModelChangeEvent {
kind: GroupChangeKind;
editorInputOrEvent?: EditorInput | IEditorOpenEvent | IEditorCloseEvent | IEditorMoveEvent;
}
export class EditorGroupModel extends Disposable {
private static IDS = 0;
//#region events
private readonly _onDidModelChange = this._register(new Emitter<EditorGroupModelChangeEvent>());
private readonly _onDidModelChange = this._register(new Emitter<IGroupChangeEvent>());
readonly onDidModelChange = this._onDidModelChange.event;
//#endregion
@ -262,7 +257,11 @@ export class EditorGroupModel extends Disposable {
this.registerEditorListeners(newEditor);
// Event
this._onDidModelChange.fire(({ kind: GroupChangeKind.EDITOR_OPEN, editorInputOrEvent: { editor: newEditor, groupId: this.id, index: targetIndex } }));
this._onDidModelChange.fire({
kind: GroupChangeKind.EDITOR_OPEN,
editor: newEditor,
editorIndex: targetIndex
});
// Handle active
if (makeActive) {
@ -313,29 +312,41 @@ export class EditorGroupModel extends Disposable {
// Re-emit disposal of editor input as our own event
listeners.add(Event.once(editor.onWillDispose)(() => {
if (this.indexOf(editor) >= 0) {
this._onDidModelChange.fire(({ kind: GroupChangeKind.EDITOR_DISPOSE, editorInputOrEvent: editor }));
this._onDidModelChange.fire({
kind: GroupChangeKind.EDITOR_DISPOSE,
editor
});
}
}));
// Re-Emit dirty state changes
listeners.add(editor.onDidChangeDirty(() => {
this._onDidModelChange.fire(({ kind: GroupChangeKind.EDITOR_DIRTY, editorInputOrEvent: editor }));
this._onDidModelChange.fire({
kind: GroupChangeKind.EDITOR_DIRTY,
editor
});
}));
// Re-Emit label changes
listeners.add(editor.onDidChangeLabel(() => {
this._onDidModelChange.fire(({ kind: GroupChangeKind.EDITOR_LABEL, editorInputOrEvent: editor }));
this._onDidModelChange.fire({
kind: GroupChangeKind.EDITOR_LABEL,
editor
});
}));
// Re-Emit capability changes
listeners.add(editor.onDidChangeCapabilities(() => {
this._onDidModelChange.fire(({ kind: GroupChangeKind.EDITOR_CAPABILITIES, editorInputOrEvent: editor }));
this._onDidModelChange.fire({
kind: GroupChangeKind.EDITOR_CAPABILITIES,
editor
});
}));
// Clean up dispose listeners once the editor gets closed
listeners.add(this.onDidModelChange(event => {
if (event.kind === GroupChangeKind.EDITOR_CLOSE && (event.editorInputOrEvent as IEditorCloseEvent).editor.matches(editor)) {
if (event.kind === GroupChangeKind.EDITOR_CLOSE && event.editor?.matches(editor)) {
dispose(listeners);
}
}));
@ -350,7 +361,13 @@ export class EditorGroupModel extends Disposable {
this.splice(replaceIndex, false, replaceWith);
if (event) {
this._onDidModelChange.fire({ kind: GroupChangeKind.EDITOR_CLOSE, editorInputOrEvent: event });
this._onDidModelChange.fire({
kind: GroupChangeKind.EDITOR_CLOSE,
editor: event.editor,
editorIndex: event.index,
closeContext: event.context,
closedSticky: event.sticky
});
}
}
@ -358,8 +375,13 @@ export class EditorGroupModel extends Disposable {
const event = this.doCloseEditor(candidate, context, openNext);
if (event) {
this._onDidModelChange.fire({ kind: GroupChangeKind.EDITOR_CLOSE, editorInputOrEvent: event });
this._onDidModelChange.fire({
kind: GroupChangeKind.EDITOR_CLOSE,
editor: event.editor,
editorIndex: event.index,
closeContext: event.context,
closedSticky: event.sticky
});
return event;
}
@ -443,7 +465,12 @@ export class EditorGroupModel extends Disposable {
this.editors.splice(toIndex, 0, editor);
// Event
this._onDidModelChange.fire({ kind: GroupChangeKind.EDITOR_MOVE, editorInputOrEvent: { editor, groupId: this.id, index, newIndex: toIndex, target: this.id } });
this._onDidModelChange.fire({
kind: GroupChangeKind.EDITOR_MOVE,
editor,
oldEditorIndex: index,
editorIndex: toIndex,
});
return editor;
}
@ -474,7 +501,10 @@ export class EditorGroupModel extends Disposable {
this.mru.unshift(editor);
// Event
this._onDidModelChange.fire({ kind: GroupChangeKind.EDITOR_ACTIVE, editorInputOrEvent: editor });
this._onDidModelChange.fire({
kind: GroupChangeKind.EDITOR_ACTIVE,
editor
});
}
pin(candidate: EditorInput): EditorInput | undefined {
@ -499,7 +529,10 @@ export class EditorGroupModel extends Disposable {
this.preview = null;
// Event
this._onDidModelChange.fire({ kind: GroupChangeKind.EDITOR_PIN, editorInputOrEvent: editor });
this._onDidModelChange.fire({
kind: GroupChangeKind.EDITOR_PIN,
editor
});
}
unpin(candidate: EditorInput): EditorInput | undefined {
@ -525,7 +558,10 @@ export class EditorGroupModel extends Disposable {
this.preview = editor;
// Event
this._onDidModelChange.fire({ kind: GroupChangeKind.EDITOR_PIN, editorInputOrEvent: editor });
this._onDidModelChange.fire({
kind: GroupChangeKind.EDITOR_PIN,
editor,
});
// Close old preview editor if any
if (oldPreview) {
@ -572,7 +608,10 @@ export class EditorGroupModel extends Disposable {
this.sticky++;
// Event
this._onDidModelChange.fire({ kind: GroupChangeKind.EDITOR_STICKY, editorInputOrEvent: editor });
this._onDidModelChange.fire({
kind: GroupChangeKind.EDITOR_STICKY,
editor
});
}
unstick(candidate: EditorInput): EditorInput | undefined {
@ -600,7 +639,10 @@ export class EditorGroupModel extends Disposable {
this.sticky--;
// Event
this._onDidModelChange.fire({ kind: GroupChangeKind.EDITOR_STICKY, editorInputOrEvent: editor });
this._onDidModelChange.fire({
kind: GroupChangeKind.EDITOR_STICKY,
editor
});
}
isSticky(candidateOrIndex: EditorInput | number): boolean {

View file

@ -5,7 +5,7 @@
import { Event } from 'vs/base/common/event';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { IEditorPane, GroupIdentifier, EditorInputWithOptions, CloseDirection, IEditorPartOptions, IEditorPartOptionsChangeEvent, EditorsOrder, IVisibleEditorPane, IEditorCloseEvent, IUntypedEditorInput, isEditorInput, IEditorWillMoveEvent, IEditorWillOpenEvent } from 'vs/workbench/common/editor';
import { IEditorPane, GroupIdentifier, EditorInputWithOptions, CloseDirection, IEditorPartOptions, IEditorPartOptionsChangeEvent, EditorsOrder, IVisibleEditorPane, IEditorCloseEvent, IUntypedEditorInput, isEditorInput, IEditorWillMoveEvent, IEditorWillOpenEvent, EditorCloseContext } from 'vs/workbench/common/editor';
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
import { IEditorOptions } from 'vs/platform/editor/common/editor';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
@ -441,6 +441,20 @@ export interface IGroupChangeEvent {
* the index the editor is moving to.
*/
oldEditorIndex?: number;
/**
* For `EDITOR_CLOSE` only: Signifies
* the context in which the editor is being closed.
* This allows for understanding if a replace or reopen is occuring
*/
closeContext?: EditorCloseContext;
/**
* For `EDITOR_CLOSE` only: Signifies
* whether or not the closed editor was sticky.
* This is necessary becasue state is lost after closing.
*/
closedSticky?: boolean;
}
export const enum OpenEditorContext {

View file

@ -107,30 +107,40 @@ suite('EditorGroupModel', () => {
};
group.onDidModelChange(e => {
if (e.kind === GroupChangeKind.GROUP_LOCKED) {
groupEvents.locked.push(group.id);
return;
}
if (!e.editor) {
return;
}
switch (e.kind) {
case GroupChangeKind.GROUP_LOCKED:
groupEvents.locked.push(group.id);
break;
case GroupChangeKind.EDITOR_OPEN:
groupEvents.opened.push(e.editorInputOrEvent as IEditorOpenEvent);
if (e.editorIndex !== undefined) {
groupEvents.opened.push({ editor: e.editor, index: e.editorIndex, groupId: group.id });
}
break;
case GroupChangeKind.EDITOR_CLOSE:
groupEvents.closed.push(e.editorInputOrEvent as IEditorCloseEvent);
if (e.editorIndex !== undefined && e.closeContext !== undefined && e.closedSticky !== undefined) {
groupEvents.closed.push({ editor: e.editor, index: e.editorIndex, groupId: group.id, context: e.closeContext, sticky: e.closedSticky });
}
break;
case GroupChangeKind.EDITOR_ACTIVE:
groupEvents.activated.push(e.editorInputOrEvent as EditorInput);
groupEvents.activated.push(e.editor);
break;
case GroupChangeKind.EDITOR_PIN:
group.isPinned(e.editorInputOrEvent as EditorInput) ? groupEvents.pinned.push(e.editorInputOrEvent as EditorInput) : groupEvents.unpinned.push(e.editorInputOrEvent as EditorInput);
group.isPinned(e.editor) ? groupEvents.pinned.push(e.editor) : groupEvents.unpinned.push(e.editor);
break;
case GroupChangeKind.EDITOR_STICKY:
group.isSticky(e.editorInputOrEvent as EditorInput) ? groupEvents.sticky.push(e.editorInputOrEvent as EditorInput) : groupEvents.unsticky.push(e.editorInputOrEvent as EditorInput);
group.isSticky(e.editor) ? groupEvents.sticky.push(e.editor) : groupEvents.unsticky.push(e.editor);
break;
case GroupChangeKind.EDITOR_MOVE:
groupEvents.moved.push(e.editorInputOrEvent as IEditorMoveEvent);
if (e.oldEditorIndex !== undefined && e.editorIndex !== undefined) {
groupEvents.moved.push({ editor: e.editor, index: e.oldEditorIndex, newIndex: e.editorIndex, target: group.id, groupId: group.id });
}
break;
case GroupChangeKind.EDITOR_DISPOSE:
groupEvents.disposed.push(e.editorInputOrEvent as EditorInput);
groupEvents.disposed.push(e.editor);
break;
}
});