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

editor - move auto save to its own file (fixes web to start)

上级 1a88e9d1
......@@ -54,6 +54,7 @@ import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
import { withNullAsUndefined } from 'vs/base/common/types';
import { registerAndGetAmdImageURL } from 'vs/base/common/amd';
import { IFilesConfigurationService } from 'vs/workbench/services/filesConfiguration/common/filesConfigurationService';
import { EditorAutoSave } from 'vs/workbench/browser/parts/editor/editorAutoSave';
// Register String Editor
Registry.as<IEditorRegistry>(EditorExtensions.Editors).registerEditor(
......@@ -224,6 +225,9 @@ registerEditorContribution(OpenWorkspaceButtonContribution.ID, OpenWorkspaceButt
// Register Editor Status
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).registerWorkbenchContribution(EditorStatus, LifecyclePhase.Ready);
// Register Editor Auto Save
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).registerWorkbenchContribution(EditorAutoSave, LifecyclePhase.Ready);
// Register Status Actions
const registry = Registry.as<IWorkbenchActionRegistry>(ActionExtensions.WorkbenchActions);
registry.registerWorkbenchAction(SyncActionDescriptor.create(ChangeModeAction, ChangeModeAction.ID, ChangeModeAction.LABEL, { primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyCode.KEY_M) }), 'Change Language Mode');
......
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { IFilesConfigurationService, AutoSaveMode } from 'vs/workbench/services/filesConfiguration/common/filesConfigurationService';
import { IHostService } from 'vs/workbench/services/host/browser/host';
import { SaveReason, IEditorIdentifier, IEditorInput, GroupIdentifier } from 'vs/workbench/common/editor';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { ICodeEditor, isCodeEditor, isDiffEditor } from 'vs/editor/browser/editorBrowser';
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
export class EditorAutoSave extends Disposable implements IWorkbenchContribution {
private lastActiveEditor: IEditorInput | null = null;
private lastActiveGroupId: GroupIdentifier | null = null;
private lastActiveEditorControlDisposable = this._register(new DisposableStore());
constructor(
@IFilesConfigurationService private readonly filesConfigurationService: IFilesConfigurationService,
@IHostService private readonly hostService: IHostService,
@IEditorService private readonly editorService: IEditorService,
@IEditorGroupsService private readonly editorGroupService: IEditorGroupsService
) {
super();
this.registerListeners();
}
private registerListeners(): void {
this._register(this.hostService.onDidChangeFocus(focused => this.onWindowFocusChange(focused)));
this._register(this.editorService.onDidActiveEditorChange(() => this.onDidActiveEditorChange()));
}
private onWindowFocusChange(focused: boolean): void {
if (!focused) {
this.maybeTriggerAutoSave(SaveReason.WINDOW_CHANGE);
}
}
private onDidActiveEditorChange(): void {
// Treat editor change like a focus change for our last active editor if any
if (this.lastActiveEditor && typeof this.lastActiveGroupId === 'number') {
this.maybeTriggerAutoSave(SaveReason.FOCUS_CHANGE, { groupId: this.lastActiveGroupId, editor: this.lastActiveEditor });
}
// Remember as last active
const activeGroup = this.editorGroupService.activeGroup;
const activeEditor = this.lastActiveEditor = activeGroup.activeEditor;
this.lastActiveGroupId = activeGroup.id;
// Dispose previous active control listeners
this.lastActiveEditorControlDisposable.clear();
// Listen to focus changes on control for auto save
if (activeGroup && activeEditor) {
const activeTextEditorWidget = this.editorService.activeTextEditorWidget;
const controlsToObserve: ICodeEditor[] = [];
if (isCodeEditor(activeTextEditorWidget)) {
controlsToObserve.push(activeTextEditorWidget);
} else if (isDiffEditor(activeTextEditorWidget)) {
controlsToObserve.push(activeTextEditorWidget.getOriginalEditor(), activeTextEditorWidget.getModifiedEditor());
}
controlsToObserve.forEach(control => this.lastActiveEditorControlDisposable.add(control.onDidBlurEditorWidget(() => {
this.maybeTriggerAutoSave(SaveReason.FOCUS_CHANGE, { groupId: activeGroup.id, editor: activeEditor });
})));
}
}
private maybeTriggerAutoSave(reason: SaveReason, editorIdentifier?: IEditorIdentifier): void {
if (editorIdentifier && (editorIdentifier.editor.isReadonly() || editorIdentifier.editor.isUntitled())) {
return; // no auto save for readonly or untitled editors
}
const mode = this.filesConfigurationService.getAutoSaveMode();
// Determine if we need to save all. In case of a window focus change we also save if auto save mode
// is configured to be ON_FOCUS_CHANGE (editor focus change)
if (
(reason === SaveReason.WINDOW_CHANGE && (mode === AutoSaveMode.ON_FOCUS_CHANGE || mode === AutoSaveMode.ON_WINDOW_CHANGE)) ||
(reason === SaveReason.FOCUS_CHANGE && mode === AutoSaveMode.ON_FOCUS_CHANGE)
) {
if (editorIdentifier) {
this.editorService.save(editorIdentifier, { reason });
} else {
this.editorService.saveAll({ reason });
}
}
}
}
......@@ -5,7 +5,7 @@
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { IResourceInput, ITextEditorOptions, IEditorOptions, EditorActivation } from 'vs/platform/editor/common/editor';
import { IEditorInput, IEditor, GroupIdentifier, IFileEditorInput, IUntitledTextResourceInput, IResourceDiffInput, IResourceSideBySideInput, IEditorInputFactoryRegistry, Extensions as EditorExtensions, IFileInputFactory, EditorInput, SideBySideEditorInput, IEditorInputWithOptions, isEditorInputWithOptions, EditorOptions, TextEditorOptions, IEditorIdentifier, IEditorCloseEvent, ITextEditor, ITextDiffEditor, ITextSideBySideEditor, toResource, SideBySideEditor, IRevertOptions, SaveReason } from 'vs/workbench/common/editor';
import { IEditorInput, IEditor, GroupIdentifier, IFileEditorInput, IUntitledTextResourceInput, IResourceDiffInput, IResourceSideBySideInput, IEditorInputFactoryRegistry, Extensions as EditorExtensions, IFileInputFactory, EditorInput, SideBySideEditorInput, IEditorInputWithOptions, isEditorInputWithOptions, EditorOptions, TextEditorOptions, IEditorIdentifier, IEditorCloseEvent, ITextEditor, ITextDiffEditor, ITextSideBySideEditor, toResource, SideBySideEditor, IRevertOptions } from 'vs/workbench/common/editor';
import { ResourceEditorInput } from 'vs/workbench/common/editor/resourceEditorInput';
import { DataUriEditorInput } from 'vs/workbench/common/editor/dataUriEditorInput';
import { Registry } from 'vs/platform/registry/common/platform';
......@@ -28,8 +28,6 @@ import { IEditorGroupView, IEditorOpeningEvent, EditorServiceImpl } from 'vs/wor
import { ILabelService } from 'vs/platform/label/common/label';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { withNullAsUndefined } from 'vs/base/common/types';
import { IFilesConfigurationService, AutoSaveMode } from 'vs/workbench/services/filesConfiguration/common/filesConfigurationService';
import { IHostService } from 'vs/workbench/services/host/browser/host';
type CachedEditorInput = ResourceEditorInput | IFileEditorInput | DataUriEditorInput;
type OpenInEditorGroup = IEditorGroup | GroupIdentifier | SIDE_GROUP_TYPE | ACTIVE_GROUP_TYPE;
......@@ -61,7 +59,6 @@ export class EditorService extends Disposable implements EditorServiceImpl {
private lastActiveEditor: IEditorInput | null = null;
private lastActiveGroupId: GroupIdentifier | null = null;
private lastActiveEditorControlDisposable = this._register(new DisposableStore());
constructor(
@IEditorGroupsService private readonly editorGroupService: IEditorGroupsService,
......@@ -69,9 +66,7 @@ export class EditorService extends Disposable implements EditorServiceImpl {
@IInstantiationService private readonly instantiationService: IInstantiationService,
@ILabelService private readonly labelService: ILabelService,
@IFileService private readonly fileService: IFileService,
@IConfigurationService private readonly configurationService: IConfigurationService,
@IFilesConfigurationService private readonly filesConfigurationService: IFilesConfigurationService,
@IHostService private readonly hostService: IHostService
@IConfigurationService private readonly configurationService: IConfigurationService
) {
super();
......@@ -86,33 +81,6 @@ export class EditorService extends Disposable implements EditorServiceImpl {
this.editorGroupService.whenRestored.then(() => this.onEditorsRestored());
this.editorGroupService.onDidActiveGroupChange(group => this.handleActiveEditorChange(group));
this.editorGroupService.onDidAddGroup(group => this.registerGroupListeners(group as IEditorGroupView));
// Auto save support
this._register(this.onDidActiveEditorChange(() => this.onEditorFocusLost()));
this._register(this.hostService.onDidChangeFocus(focused => this.onWindowFocusChange(focused)));
}
private onEditorFocusLost(): void {
this.maybeTriggerSaveAll(SaveReason.FOCUS_CHANGE);
}
private onWindowFocusChange(focused: boolean): void {
if (!focused) {
this.maybeTriggerSaveAll(SaveReason.WINDOW_CHANGE);
}
}
private maybeTriggerSaveAll(reason: SaveReason): void {
const mode = this.filesConfigurationService.getAutoSaveMode();
// Determine if we need to save all. In case of a window focus change we also save if auto save mode
// is configured to be ON_FOCUS_CHANGE (editor focus change)
if (
(reason === SaveReason.WINDOW_CHANGE && (mode === AutoSaveMode.ON_FOCUS_CHANGE || mode === AutoSaveMode.ON_WINDOW_CHANGE)) ||
(reason === SaveReason.FOCUS_CHANGE && mode === AutoSaveMode.ON_FOCUS_CHANGE)
) {
this.saveAll({ reason });
}
}
private onEditorsRestored(): void {
......@@ -150,18 +118,6 @@ export class EditorService extends Disposable implements EditorServiceImpl {
this.lastActiveGroupId = activeGroup.id;
this.lastActiveEditor = activeGroup.activeEditor;
// Dispose previous active control listeners
this.lastActiveEditorControlDisposable.clear();
// Listen to focus changes on control for auto save
const controlsToObserve: ICodeEditor[] = [];
if (isCodeEditor(this.activeTextEditorWidget)) {
controlsToObserve.push(this.activeTextEditorWidget);
} else if (isDiffEditor(this.activeTextEditorWidget)) {
controlsToObserve.push(this.activeTextEditorWidget.getOriginalEditor(), this.activeTextEditorWidget.getModifiedEditor());
}
controlsToObserve.forEach(control => this.lastActiveEditorControlDisposable.add(control.onDidBlurEditorWidget(() => this.onEditorFocusLost())));
// Fire event to outside parties
this._onDidActiveEditorChange.fire();
}
......@@ -824,9 +780,7 @@ export class DelegatingEditorService extends EditorService {
@IInstantiationService instantiationService: IInstantiationService,
@ILabelService labelService: ILabelService,
@IFileService fileService: IFileService,
@IConfigurationService configurationService: IConfigurationService,
@IFilesConfigurationService filesConfigurationService: IFilesConfigurationService,
@IHostService hostService: IHostService
@IConfigurationService configurationService: IConfigurationService
) {
super(
editorGroupService,
......@@ -834,9 +788,7 @@ export class DelegatingEditorService extends EditorService {
instantiationService,
labelService,
fileService,
configurationService,
filesConfigurationService,
hostService
configurationService
);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册