提交 c7a04cbd 编写于 作者: B Benjamin Pasero

get rid of editor event

上级 909be6a1
......@@ -21,7 +21,7 @@ import errors = require('vs/base/common/errors');
import {Scope as MementoScope} from 'vs/workbench/common/memento';
import {Scope} from 'vs/workbench/browser/actionBarRegistry';
import {Part} from 'vs/workbench/browser/part';
import {EventType as WorkbenchEventType, EditorEvent, EditorInputEvent} from 'vs/workbench/common/events';
import {EventType as WorkbenchEventType, EditorInputEvent} from 'vs/workbench/common/events';
import {IEditorRegistry, Extensions as EditorExtensions, BaseEditor, EditorDescriptor} from 'vs/workbench/browser/parts/editor/baseEditor';
import {EditorInput, EditorOptions, TextEditorOptions, ConfirmResult} from 'vs/workbench/common/editor';
import {BaseTextEditor} from 'vs/workbench/browser/parts/editor/textEditor';
......@@ -390,7 +390,7 @@ export class EditorPart extends Part implements IEditorPart {
// editor title area is up to date.
if (group.activeEditor && group.activeEditor.matches(input)) {
this.doRecreateEditorTitleArea();
this.emit(WorkbenchEventType.EDITOR_INPUT_CHANGED, new EditorEvent(editor, group.activeEditor, options, position));
this.emit(WorkbenchEventType.EDITOR_INPUT_CHANGED, editor);
}
return editor;
......@@ -407,7 +407,7 @@ export class EditorPart extends Part implements IEditorPart {
// Emit Input-Changed Event (if input changed)
if (inputChanged) {
this.emit(WorkbenchEventType.EDITOR_INPUT_CHANGED, new EditorEvent(editor, input, options, position));
this.emit(WorkbenchEventType.EDITOR_INPUT_CHANGED, editor);
}
// Update Title Area
......@@ -523,7 +523,7 @@ export class EditorPart extends Part implements IEditorPart {
this.doHideEditor(position, true);
// Emit Input-Changed Event
this.emit(WorkbenchEventType.EDITOR_INPUT_CHANGED, new EditorEvent(null, null, null, position));
this.emit(WorkbenchEventType.EDITOR_INPUT_CHANGED, {});
// Focus next group if we have an active one left
const currentActiveGroup = this.stacks.activeGroup;
......@@ -805,7 +805,7 @@ export class EditorPart extends Part implements IEditorPart {
// Emit as editor input change event so that clients get aware of new active editor
let activeEditor = this.sideBySideControl.getActiveEditor();
if (activeEditor) {
this.emit(WorkbenchEventType.EDITOR_INPUT_CHANGED, new EditorEvent(activeEditor));
this.emit(WorkbenchEventType.EDITOR_INPUT_CHANGED, {});
}
// Update Title Area
......
......@@ -20,7 +20,7 @@ import {ContributableActionProvider} from 'vs/workbench/browser/actionBarRegistr
import {ITree, IElementCallback} from 'vs/base/parts/tree/browser/tree';
import {Registry} from 'vs/platform/platform';
import {WorkbenchComponent} from 'vs/workbench/common/component';
import {EditorEvent, EditorInputEvent, EventType} from 'vs/workbench/common/events';
import {EditorInputEvent, EventType} from 'vs/workbench/common/events';
import Event, {Emitter} from 'vs/base/common/event';
import {Identifiers} from 'vs/workbench/common/constants';
import {IEditorInput} from 'vs/platform/editor/common/editor';
......@@ -124,7 +124,7 @@ export class QuickOpenController extends WorkbenchComponent implements IQuickOpe
public create(): void {
// Listen on Editor Input Changes to show in MRU List
this.toUnbind.push(this.eventService.addListener2(EventType.EDITOR_INPUT_CHANGED, (e: EditorEvent) => this.onEditorInputChanged(e)));
this.toUnbind.push(this.eventService.addListener2(EventType.EDITOR_INPUT_CHANGED, () => this.onEditorInputChanged()));
this.toUnbind.push(this.eventService.addListener2(EventType.EDITOR_SET_INPUT_ERROR, (e: EditorInputEvent) => this.onEditorInputSetError(e)));
// Editor History Model
......@@ -135,17 +135,14 @@ export class QuickOpenController extends WorkbenchComponent implements IQuickOpe
}
}
private onEditorInputChanged(e: EditorEvent): void {
if (e.editorInput) {
// If an active editor is set, but is different from the one from the event, return early
let activeEditor = this.editorService.getActiveEditor();
if (activeEditor && e.editor && activeEditor !== e.editor) {
private onEditorInputChanged(): void {
let activeEditorInput = this.editorService.getActiveEditorInput();
if (!activeEditorInput) {
return;
}
// Add to History
this.editorHistoryModel.add(e.editorInput);
this.editorHistoryModel.add(activeEditorInput);
// Save to Local Storage periodically
if (this.autoSaveHistoryCounter++ >= AUTO_SAVE_HISTORY_THRESHOLD) {
......@@ -153,7 +150,6 @@ export class QuickOpenController extends WorkbenchComponent implements IQuickOpe
this.autoSaveHistoryCounter = 0;
}
}
}
private onEditorInputSetError(e: EditorInputEvent): void {
this.removeEditorHistoryEntry(e.editorInput); // make sure this input does not show up in history if it failed to open
......
......@@ -6,8 +6,7 @@
import URI from 'vs/base/common/uri';
import {Event} from 'vs/base/common/events';
import {IEditor, IEditorInput, IEditorOptions} from 'vs/platform/editor/common/editor';
import {Position} from 'vs/platform/editor/common/editor';
import {IEditorInput} from 'vs/platform/editor/common/editor';
/**
* All workbench events are listed here.
......@@ -111,47 +110,6 @@ export class EditorInputEvent extends Event {
}
}
/**
* Editor events are being emitted when the editor input changes, shows, is being saved or when the editor content changes.
*/
export class EditorEvent extends Event {
private _editor: IEditor;
private _editorId: string;
private _editorInput: IEditorInput;
private _editorOptions: IEditorOptions;
private _position: Position;
constructor(editor: IEditor, editorInput = editor.input, editorOptions = editor.options, position = editor.position, originalEvent?: any) {
super(originalEvent);
this._editor = editor;
this._editorId = editor ? editor.getId() : void 0;
this._editorInput = editorInput;
this._editorOptions = editorOptions;
this._position = position;
}
public get editor(): IEditor {
return this._editor;
}
public get editorId(): string {
return this._editorId;
}
public get editorInput(): IEditorInput {
return this._editorInput;
}
public get editorOptions(): IEditorOptions {
return this._editorOptions;
}
public get position(): Position {
return this._position;
}
}
/**
* Option change events are send when the options in the running instance change.
*/
......
......@@ -9,7 +9,7 @@ import platform = require('vs/base/common/platform');
import paths = require('vs/base/common/paths');
import uri from 'vs/base/common/uri';
import {Identifiers} from 'vs/workbench/common/constants';
import {EventType, EditorEvent} from 'vs/workbench/common/events';
import {EventType} from 'vs/workbench/common/events';
import workbenchEditorCommon = require('vs/workbench/common/editor');
import {IViewletService} from 'vs/workbench/services/viewlet/common/viewletService';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
......@@ -52,13 +52,8 @@ export class ElectronWindow {
// React to editor input changes (Mac only)
if (platform.platform === platform.Platform.Mac) {
this.eventService.addListener2(EventType.EDITOR_INPUT_CHANGED, (e: EditorEvent) => {
let activeEditor = this.editorService.getActiveEditor();
if (activeEditor !== e.editor) {
return; // only care about active editor
}
let fileInput = workbenchEditorCommon.asFileEditorInput(e.editorInput, true);
this.eventService.addListener2(EventType.EDITOR_INPUT_CHANGED, () => {
let fileInput = workbenchEditorCommon.asFileEditorInput(this.editorService.getActiveEditorInput(), true);
let representedFilename = '';
if (fileInput) {
representedFilename = fileInput.getResource().fsPath;
......@@ -127,7 +122,7 @@ export class ElectronWindow {
});
// Handle window.open() calls
(<any>window).open = function(url: string, target: string, features: string, replace: boolean) {
(<any>window).open = function (url: string, target: string, features: string, replace: boolean) {
shell.openExternal(url);
return null;
......
......@@ -15,7 +15,7 @@ import {Action, IActionRunner, IAction} from 'vs/base/common/actions';
import {prepareActions} from 'vs/workbench/browser/actionBarRegistry';
import {ITree} from 'vs/base/parts/tree/browser/tree';
import {Tree} from 'vs/base/parts/tree/browser/treeImpl';
import {EditorEvent, EventType as WorkbenchEventType} from 'vs/workbench/common/events';
import {EventType as WorkbenchEventType} from 'vs/workbench/common/events';
import {EditorOptions} from 'vs/workbench/common/editor';
import {LocalFileChangeEvent, IFilesConfiguration} from 'vs/workbench/parts/files/common/files';
import {IFileStat, IResolveFileOptions, FileChangeType, FileChangesEvent, IFileChange, EventType as FileEventType, IFileService} from 'vs/platform/files/common/files';
......@@ -140,38 +140,30 @@ export class ExplorerView extends CollapsibleViewletView {
return this.doRefresh().then(() => {
// When the explorer viewer is loaded, listen to changes to the editor input
this.toDispose.push(this.eventService.addListener2(WorkbenchEventType.EDITOR_INPUT_CHANGED, (e: EditorEvent) => this.onEditorInputChanged(e)));
this.toDispose.push(this.eventService.addListener2(WorkbenchEventType.EDITOR_INPUT_CHANGED, () => this.onEditorInputChanged()));
// Also handle configuration updates
this.toDispose.push(this.configurationService.onDidUpdateConfiguration(e => this.onConfigurationUpdated(e.config, true)));
});
}
private onEditorInputChanged(e: EditorEvent): void {
// During workbench startup, the editor area might restore more than one editor from a previous
// session. When this happens there might be editor input changing events for side editors that
// don't have focus. In these cases we do not adjust explorer selection for non-focused editors
// because we only want to react for the editor that has focus.
if (!this.partService.isCreated() && e.editorOptions && e.editorOptions.preserveFocus) {
return;
}
private onEditorInputChanged(): void {
let activeInput = this.editorService.getActiveEditorInput();
let clearSelection = true;
let clearFocus = false;
// Handle File Input
if (e.editorInput && e.editorInput instanceof FileEditorInput) {
let fileInput = (<FileEditorInput>e.editorInput);
if (activeInput instanceof FileEditorInput) {
const fileResource = activeInput.getResource();
// Always remember last opened file
this.settings[ExplorerView.MEMENTO_LAST_ACTIVE_FILE_RESOURCE] = fileInput.getResource().toString();
this.settings[ExplorerView.MEMENTO_LAST_ACTIVE_FILE_RESOURCE] = fileResource.toString();
// Select file if input is FileEditorInput
if (this.isVisible && this.contextService.isInsideWorkspace(fileInput.getResource())) {
let selection = this.hasSelection(fileInput.getResource());
if (this.isVisible && this.contextService.isInsideWorkspace(fileResource)) {
let selection = this.hasSelection(fileResource);
if (!selection) {
this.select(fileInput.getResource()).done(null, errors.onUnexpectedError);
this.select(fileResource).done(null, errors.onUnexpectedError);
}
clearSelection = false;
......@@ -179,7 +171,7 @@ export class ExplorerView extends CollapsibleViewletView {
}
// Handle closed (convince explorer to not reopen any file when getting visible)
if (!e.editorInput) {
if (!activeInput) {
this.settings[ExplorerView.MEMENTO_LAST_ACTIVE_FILE_RESOURCE] = void 0;
clearFocus = true;
}
......
......@@ -103,7 +103,7 @@ export class ChangesView extends EventEmitter.EventEmitter implements GitView.IV
this.toDispose = [
this.smartCommitAction = this.instantiationService.createInstance(GitActions.SmartCommitAction, this),
eventService.addListener2(WorkbenchEvents.EventType.EDITOR_INPUT_CHANGED, (e:WorkbenchEvents.EditorEvent) => this.onEditorInputChanged(e.editorInput).done(null, Errors.onUnexpectedError)),
eventService.addListener2(WorkbenchEvents.EventType.EDITOR_INPUT_CHANGED, () => this.onEditorInputChanged(this.editorService.getActiveEditorInput()).done(null, Errors.onUnexpectedError)),
this.gitService.addListener2(git.ServiceEvents.OPERATION_START, (e) => this.onGitOperationStart(e)),
this.gitService.addListener2(git.ServiceEvents.OPERATION_END, (e) => this.onGitOperationEnd(e)),
this.gitService.getModel().addListener2(git.ModelEvents.MODEL_UPDATED, this.onGitModelUpdate.bind(this))
......
......@@ -13,7 +13,7 @@ import {getBaseThemeId} from 'vs/platform/theme/common/themes';
import {IWorkbenchContribution} from 'vs/workbench/common/contributions';
import {IFrameEditor} from 'vs/workbench/browser/parts/editor/iframeEditor';
import {MarkdownEditorInput} from 'vs/workbench/parts/markdown/common/markdownEditorInput';
import {EditorEvent, EventType as WorkbenchEventType} from 'vs/workbench/common/events';
import {EventType as WorkbenchEventType} from 'vs/workbench/common/events';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {IWorkspaceContextService} from 'vs/workbench/services/workspace/common/contextService';
import {IConfigurationService, IConfigurationServiceEvent} from 'vs/platform/configuration/common/configuration';
......@@ -67,7 +67,7 @@ export class MarkdownFileTracker implements IWorkbenchContribution {
this.configFileChangeListener = this.configurationService.onDidUpdateConfiguration(e => this.onConfigFileChange(e));
// reload markdown editors when their resources change
this.editorInputChangeListener = this.eventService.addListener2(WorkbenchEventType.EDITOR_INPUT_CHANGED, (e: EditorEvent) => this.onEditorInputChanged(e));
this.editorInputChangeListener = this.eventService.addListener2(WorkbenchEventType.EDITOR_INPUT_CHANGED, () => this.onEditorInputChanged());
// initially read the config for CSS styles in preview
this.readMarkdownConfiguration(this.configurationService.getConfiguration<ILanguageConfiguration>());
......@@ -79,8 +79,8 @@ export class MarkdownFileTracker implements IWorkbenchContribution {
});
}
private onEditorInputChanged(e: EditorEvent): void {
let input = e.editorInput;
private onEditorInputChanged(): void {
let input = this.editorService.getActiveEditorInput();
if (input instanceof MarkdownEditorInput) {
let markdownResource = input.getResource();
let editorModel = this.modelService.getModel(markdownResource);
......
......@@ -11,7 +11,7 @@ import {EventType} from 'vs/base/common/events';
import {IEditor as IBaseEditor} from 'vs/platform/editor/common/editor';
import {TextEditorOptions, EditorInput} from 'vs/workbench/common/editor';
import {BaseTextEditor} from 'vs/workbench/browser/parts/editor/textEditor';
import {EditorEvent, EventType as WorkbenchEventType} from 'vs/workbench/common/events';
import {EventType as WorkbenchEventType} from 'vs/workbench/common/events';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {IHistoryService} from 'vs/workbench/services/history/common/history';
import {Selection} from 'vs/editor/common/core/selection';
......@@ -81,13 +81,10 @@ export abstract class BaseHistoryService {
window.document.title = this.getWindowTitle(null);
// Editor Input Changes
this.toUnbind.push(this.eventService.addListener2(WorkbenchEventType.EDITOR_INPUT_CHANGED, (e: EditorEvent) => this.onEditorInputChanged(e)));
this.toUnbind.push(this.eventService.addListener2(WorkbenchEventType.EDITOR_INPUT_CHANGED, () => this.onEditorInputChanged()));
}
private onEditorInputChanged(event: EditorEvent): void {
// Propagate to history
this.onEditorEvent(event.editor);
private onEditorInputChanged(): void {
// Dispose old listeners
dispose(this.activeEditorListeners);
......@@ -96,6 +93,9 @@ export abstract class BaseHistoryService {
let activeEditor = this.editorService.getActiveEditor();
let activeInput = activeEditor ? activeEditor.input : void 0;
// Propagate to history
this.onEditorEvent(activeEditor);
// Apply listener for dirty changes
if (activeInput instanceof EditorInput) {
this.activeEditorListeners.push(activeInput.onDidChangeDirty(() => {
......@@ -115,12 +115,6 @@ export abstract class BaseHistoryService {
private onEditorEvent(editor: IBaseEditor): void {
let input = editor ? editor.input : null;
// If an active editor is set, but is different from the one from the event, prevent update because the editor is not active.
let activeEditor = this.editorService.getActiveEditor();
if (activeEditor && editor && activeEditor !== editor) {
return;
}
// Calculate New Window Title
this.updateWindowTitle(input);
......
......@@ -25,8 +25,7 @@ import URI from 'vs/base/common/uri';
import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection';
import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {EventType, EditorEvent} from 'vs/workbench/common/events';
import {Position} from 'vs/platform/editor/common/editor';
import {EventType} from 'vs/workbench/common/events';
function toResource(path) {
return URI.file(join('C:\\', path));
......@@ -77,24 +76,6 @@ suite('Workbench QuickOpen', () => {
assert(!entry2.run(Mode.PREVIEW, { event: null, quickNavigateConfiguration: null, keymods: [] }));
});
test('EditorHistoryEntry is removed when open fails', () => {
let editorService = new TestEditorService();
let contextService = new TestContextService();
let inst = new InstantiationService();
let model = new EditorHistoryModel(editorService, null, contextService);
let input1 = inst.createInstance(StringEditorInput, 'name1', 'description', 'value1', 'text/plain', false);
model.add(input1);
assert.equal(1, model.getEntries().length);
assert(model.getEntries()[0].run(Mode.OPEN, { event: null, quickNavigateConfiguration: null, keymods: [] }));
assert.equal(0, model.getEntries().length);
});
test('EditorHistoryModel', () => {
Registry.as('workbench.contributions.editors').setInstantiationService(new InstantiationService());
......@@ -240,13 +221,16 @@ suite('Workbench QuickOpen', () => {
assert.equal(0, controller.getEditorHistoryModel().getEntries().length);
let cinput1 = <EditorInput>inst.createInstance(fileInputCtor, toResource('Hello World'), 'text/plain', null);
let event = new EditorEvent(null, cinput1, null, Position.LEFT);
eventService.emit(EventType.EDITOR_INPUT_CHANGED, event);
editorService.activeEditorInput = cinput1;
eventService.emit(EventType.EDITOR_INPUT_CHANGED, {});
assert.equal(1, controller.getEditorHistoryModel().getEntries().length);
cinput1.dispose();
assert.equal(1, controller.getEditorHistoryModel().getEntries().length);
editorService.activeEditorInput = void 0;
});
});
\ No newline at end of file
......@@ -10,7 +10,7 @@ import URI from 'vs/base/common/uri';
import * as Paths from 'vs/base/common/paths';
import * as Files from 'vs/platform/files/common/files';
import {Event, PropertyChangeEvent} from 'vs/base/common/events';
import {CompositeEvent, EditorEvent} from 'vs/workbench/common/events';
import {CompositeEvent} from 'vs/workbench/common/events';
let FileChangesEvent = Files.FileChangesEvent;
......@@ -24,22 +24,6 @@ suite('Workbench Events', () => {
assert(event.time);
});
test('Editor Change Event', function () {
let editor: any = { getId: () => 'foo.bar' };
let origEvent: any = {};
let input: any = {};
let options: any = {};
let id = 'foo.bar';
let event = new EditorEvent(editor, input, options, 0, origEvent);
assert.strictEqual(event.editor, editor);
assert.strictEqual(event.originalEvent, origEvent);
assert.strictEqual(event.editorId, id);
assert.strictEqual(event.editorInput, input);
assert.strictEqual(event.editorOptions, options);
assert(event.time);
});
test('Property Change Event', function () {
let key = 'foo';
let origEvent: any = {};
......
......@@ -336,7 +336,7 @@ export class TestEditorService implements WorkbenchEditorService.IWorkbenchEdito
public getActiveEditorInput(): IEditorInput {
this.callback('getActiveEditorInput');
return null;
return this.activeEditorInput;
}
public getVisibleEditors(): IEditor[] {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册