提交 03262df5 编写于 作者: B Benjamin Pasero

fix #40333

上级 9812d210
......@@ -16,7 +16,7 @@ import { IModeService } from 'vs/editor/common/services/modeService';
import { getIconClasses } from 'vs/workbench/browser/labels';
import { IModelService } from 'vs/editor/common/services/modelService';
import { QuickOpenHandler } from 'vs/workbench/browser/quickopen';
import { Position, IEditorOptions } from 'vs/platform/editor/common/editor';
import { Position } from 'vs/platform/editor/common/editor';
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
......@@ -71,13 +71,15 @@ export class EditorPickerEntry extends QuickOpenEntryGroup {
}
public run(mode: Mode, context: IEntryRunContext): boolean {
let options: IEditorOptions;
if (mode === Mode.PREVIEW) {
options = { preserveFocus: true }; // in preview, make sure to keep focus in quick open
if (mode === Mode.OPEN) {
return this.runOpen(context);
}
// Open Editor
this.editorService.openEditor(this.editor, options, this.stacks.positionOfGroup(this.group)).done(null, errors.onUnexpectedError);
return super.run(mode, context);
}
private runOpen(context: IEntryRunContext): boolean {
this.editorService.openEditor(this.editor, null, this.stacks.positionOfGroup(this.group)).done(null, errors.onUnexpectedError);
return true;
}
......
......@@ -89,76 +89,6 @@ interface IEditorIdentifier {
position: GroupPosition;
}
export abstract class BaseHistoryService {
protected toUnbind: IDisposable[];
private activeEditorListeners: IDisposable[];
private lastActiveEditor: IEditorIdentifier;
constructor(
protected editorGroupService: IEditorGroupService,
protected editorService: IWorkbenchEditorService
) {
this.toUnbind = [];
this.activeEditorListeners = [];
// Listeners
this.toUnbind.push(this.editorGroupService.onEditorsChanged(() => this.onEditorsChanged()));
}
private onEditorsChanged(): void {
const activeEditor = this.editorService.getActiveEditor();
if (this.lastActiveEditor && this.matchesEditor(this.lastActiveEditor, activeEditor)) {
return; // return if the active editor is still the same
}
// Remember as last active editor (can be undefined if none opened)
this.lastActiveEditor = activeEditor ? { editor: activeEditor.input, position: activeEditor.position } : void 0;
// Dispose old listeners
dispose(this.activeEditorListeners);
this.activeEditorListeners = [];
// Propagate to history
this.handleActiveEditorChange(activeEditor);
// Apply listener for selection changes if this is a text editor
const control = getCodeEditor(activeEditor);
if (control) {
// Debounce the event with a timeout of 0ms so that multiple calls to
// editor.setSelection() are folded into one. We do not want to record
// subsequent history navigations for such API calls.
this.activeEditorListeners.push(debounceEvent(control.onDidChangeCursorPosition, (last, event) => event, 0)((event => {
this.handleEditorSelectionChangeEvent(activeEditor, event);
})));
}
}
private matchesEditor(identifier: IEditorIdentifier, editor?: IBaseEditor): boolean {
if (!editor) {
return false;
}
if (identifier.position !== editor.position) {
return false;
}
return identifier.editor.matches(editor.input);
}
protected abstract handleExcludesChange(): void;
protected abstract handleEditorSelectionChangeEvent(editor?: IBaseEditor, event?: ICursorPositionChangedEvent): void;
protected abstract handleActiveEditorChange(editor?: IBaseEditor): void;
public dispose(): void {
this.toUnbind = dispose(this.toUnbind);
}
}
interface IStackEntry {
input: IEditorInput | IResourceInput;
selection?: ITextEditorSelection;
......@@ -170,7 +100,7 @@ interface IRecentlyClosedFile {
index: number;
}
export class HistoryService extends BaseHistoryService implements IHistoryService {
export class HistoryService implements IHistoryService {
public _serviceBrand: any;
......@@ -179,6 +109,11 @@ export class HistoryService extends BaseHistoryService implements IHistoryServic
private static readonly MAX_STACK_ITEMS = 20;
private static readonly MAX_RECENTLY_CLOSED_EDITORS = 20;
private toUnbind: IDisposable[];
private activeEditorListeners: IDisposable[];
private lastActiveEditor: IEditorIdentifier;
private stack: IStackEntry[];
private index: number;
private lastIndex: number;
......@@ -191,8 +126,8 @@ export class HistoryService extends BaseHistoryService implements IHistoryServic
private resourceFilter: ResourceGlobMatcher;
constructor(
@IWorkbenchEditorService editorService: IWorkbenchEditorService,
@IEditorGroupService editorGroupService: IEditorGroupService,
@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
@IEditorGroupService private editorGroupService: IEditorGroupService,
@IWorkspaceContextService private contextService: IWorkspaceContextService,
@IStorageService private storageService: IStorageService,
@IConfigurationService private configurationService: IConfigurationService,
......@@ -201,7 +136,8 @@ export class HistoryService extends BaseHistoryService implements IHistoryServic
@IWindowsService private windowService: IWindowsService,
@IInstantiationService private instantiationService: IInstantiationService,
) {
super(editorGroupService, editorService);
this.toUnbind = [];
this.activeEditorListeners = [];
this.index = -1;
this.lastIndex = -1;
......@@ -217,11 +153,6 @@ export class HistoryService extends BaseHistoryService implements IHistoryServic
this.registerListeners();
}
private setIndex(value: number): void {
this.lastIndex = this.index;
this.index = value;
}
private getExcludes(root?: URI): IExpression {
const scope = root ? { resource: root } : void 0;
......@@ -229,6 +160,7 @@ export class HistoryService extends BaseHistoryService implements IHistoryServic
}
private registerListeners(): void {
this.toUnbind.push(this.editorGroupService.onEditorsChanged(() => this.onEditorsChanged()));
this.toUnbind.push(this.lifecycleService.onShutdown(reason => this.saveHistory()));
this.toUnbind.push(this.editorGroupService.onEditorOpenFail(editor => this.remove(editor)));
this.toUnbind.push(this.editorGroupService.getStacksModel().onEditorClosed(event => this.onEditorClosed(event)));
......@@ -236,6 +168,47 @@ export class HistoryService extends BaseHistoryService implements IHistoryServic
this.toUnbind.push(this.resourceFilter.onExpressionChange(() => this.handleExcludesChange()));
}
private onEditorsChanged(): void {
const activeEditor = this.editorService.getActiveEditor();
if (this.lastActiveEditor && this.matchesEditor(this.lastActiveEditor, activeEditor)) {
return; // return if the active editor is still the same
}
// Remember as last active editor (can be undefined if none opened)
this.lastActiveEditor = activeEditor ? { editor: activeEditor.input, position: activeEditor.position } : void 0;
// Dispose old listeners
dispose(this.activeEditorListeners);
this.activeEditorListeners = [];
// Propagate to history
this.handleActiveEditorChange(activeEditor);
// Apply listener for selection changes if this is a text editor
const control = getCodeEditor(activeEditor);
if (control) {
// Debounce the event with a timeout of 0ms so that multiple calls to
// editor.setSelection() are folded into one. We do not want to record
// subsequent history navigations for such API calls.
this.activeEditorListeners.push(debounceEvent(control.onDidChangeCursorPosition, (last, event) => event, 0)((event => {
this.handleEditorSelectionChangeEvent(activeEditor, event);
})));
}
}
private matchesEditor(identifier: IEditorIdentifier, editor?: IBaseEditor): boolean {
if (!editor) {
return false;
}
if (identifier.position !== editor.position) {
return false;
}
return identifier.editor.matches(editor.input);
}
private onFileChanges(e: FileChangesEvent): void {
if (e.gotDeleted()) {
this.remove(e); // remove from history files that got deleted or moved
......@@ -292,6 +265,11 @@ export class HistoryService extends BaseHistoryService implements IHistoryServic
this.navigate();
}
private setIndex(value: number): void {
this.lastIndex = this.index;
this.index = value;
}
private doForwardAcrossEditors(): void {
let currentIndex = this.index;
const currentEntry = this.stack[this.index];
......@@ -840,4 +818,8 @@ export class HistoryService extends BaseHistoryService implements IHistoryServic
return void 0;
}
public dispose(): void {
this.toUnbind = dispose(this.toUnbind);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册