Skip to content

Commit

Permalink
history - fix issue with selection event when restoring view state
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Feb 8, 2022
1 parent e5bf0ee commit 8ae03a1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/vs/editor/browser/widget/codeEditorWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,9 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE
if (codeEditorState && codeEditorState.cursorState && codeEditorState.viewState) {
const cursorState = <any>codeEditorState.cursorState;
if (Array.isArray(cursorState)) {
this._modelData.viewModel.restoreCursorState(<editorCommon.ICursorState[]>cursorState);
if (cursorState.length > 0) {
this._modelData.viewModel.restoreCursorState(<editorCommon.ICursorState[]>cursorState);
}
} else {
// Backwards compatibility
this._modelData.viewModel.restoreCursorState([<editorCommon.ICursorState>cursorState]);
Expand Down
19 changes: 15 additions & 4 deletions src/vs/workbench/contrib/files/browser/editors/textFileEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,21 @@ export class TextFileEditor extends BaseTextEditor<ICodeEditorViewState> {
const textEditor = assertIsDefined(this.getControl());
textEditor.setModel(textFileModel.textEditorModel);

// View state
const editorViewState = this.loadEditorViewState(input, context);
if (editorViewState) {
textEditor.restoreViewState(editorViewState);
// Restore view state (unless provided by options)
if (!options?.viewState) {
const editorViewState = this.loadEditorViewState(input, context);
if (editorViewState) {
if (options?.selection) {
// If we have a selection, make sure to not
// restore any selection from the view state
// to ensure the right selection change event
// is fired and we avoid changing selections
// twice.
editorViewState.cursorState = [];
}

textEditor.restoreViewState(editorViewState);
}
}

// Apply options to editor if any
Expand Down
28 changes: 21 additions & 7 deletions src/vs/workbench/services/history/browser/historyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,29 @@ export class HistoryService extends Disposable implements IHistoryService {

private readonly canNavigateBackContextKey = (new RawContextKey<boolean>('canNavigateBack', false, localize('canNavigateBack', "Whether it is possible to navigate back in editor history"))).bindTo(this.contextKeyService);
private readonly canNavigateForwardContextKey = (new RawContextKey<boolean>('canNavigateForward', false, localize('canNavigateForward', "Whether it is possible to navigate forward in editor history"))).bindTo(this.contextKeyService);
private readonly canNavigateToLastEditLocationContextKey = (new RawContextKey<boolean>('canNavigateToLastEditLocation', false, localize('canNavigateToLastEditLocation', "Whether it is possible to navigate to the last edit location"))).bindTo(this.contextKeyService);

private readonly canNavigateBackInNavigationsContextKey = (new RawContextKey<boolean>('canNavigateBackInNavigationLocations', false, localize('canNavigateBackInNavigationLocations', "Whether it is possible to navigate back in editor navigation locations history"))).bindTo(this.contextKeyService);
private readonly canNavigateForwardInNavigationsContextKey = (new RawContextKey<boolean>('canNavigateForwardInNavigationLocations', false, localize('canNavigateForwardInNavigationLocations', "Whether it is possible to navigate forward in editor navigation locations history"))).bindTo(this.contextKeyService);
private readonly canNavigateToLastNavigationLocationContextKey = (new RawContextKey<boolean>('canNavigateToLastNavigationLocation', false, localize('canNavigateToLastNavigationLocation', "Whether it is possible to navigate to the last editor navigation location"))).bindTo(this.contextKeyService);

private readonly canNavigateBackInEditsContextKey = (new RawContextKey<boolean>('canNavigateBackInEditLocations', false, localize('canNavigateBackInEditLocations', "Whether it is possible to navigate back in editor edit locations history"))).bindTo(this.contextKeyService);
private readonly canNavigateForwardInEditsContextKey = (new RawContextKey<boolean>('canNavigateForwardInEditLocations', false, localize('canNavigateForwardInEditLocations', "Whether it is possible to navigate forward in editor edit locations history"))).bindTo(this.contextKeyService);
private readonly canNavigateToLastEditLocationContextKey = (new RawContextKey<boolean>('canNavigateToLastEditLocation', false, localize('canNavigateToLastEditLocation', "Whether it is possible to navigate to the last editor edit location"))).bindTo(this.contextKeyService);

private readonly canReopenClosedEditorContextKey = (new RawContextKey<boolean>('canReopenClosedEditor', false, localize('canReopenClosedEditor', "Whether it is possible to reopen the last closed editor"))).bindTo(this.contextKeyService);

updateContextKeys(): void {
this.contextKeyService.bufferChangeEvents(() => {
this.canNavigateBackContextKey.set(this.globalDefaultEditorNavigationStack.canGoBack());
this.canNavigateForwardContextKey.set(this.globalDefaultEditorNavigationStack.canGoForward());

this.canNavigateToLastEditLocationContextKey.set(this.globalModificationsEditorNavigationStack.canGoLast());
this.canNavigateBackInNavigationsContextKey.set(this.globalNavigationsEditorNavigationStack.canGoBack());
this.canNavigateForwardInNavigationsContextKey.set(this.globalNavigationsEditorNavigationStack.canGoForward());
this.canNavigateToLastNavigationLocationContextKey.set(this.globalNavigationsEditorNavigationStack.canGoLast());

this.canNavigateBackInEditsContextKey.set(this.globalEditsEditorNavigationStack.canGoBack());
this.canNavigateForwardInEditsContextKey.set(this.globalEditsEditorNavigationStack.canGoForward());
this.canNavigateToLastEditLocationContextKey.set(this.globalEditsEditorNavigationStack.canGoLast());

this.canReopenClosedEditorContextKey.set(this.recentlyClosedEditors.length > 0);
});
Expand All @@ -269,18 +283,18 @@ export class HistoryService extends Disposable implements IHistoryService {
//#region Editor History Navigation (limit: 50)

private readonly globalDefaultEditorNavigationStack = this.createEditorNavigationStack();
private readonly globalModificationsEditorNavigationStack = this.createEditorNavigationStack();
private readonly globalEditsEditorNavigationStack = this.createEditorNavigationStack();
private readonly globalNavigationsEditorNavigationStack = this.createEditorNavigationStack();

private readonly editorNavigationStacks: EditorNavigationStack[] = [
this.globalDefaultEditorNavigationStack,
this.globalModificationsEditorNavigationStack,
this.globalEditsEditorNavigationStack,
this.globalNavigationsEditorNavigationStack
];

readonly onDidChangeEditorNavigationStack = Event.any(
this.globalDefaultEditorNavigationStack.onDidChange,
this.globalModificationsEditorNavigationStack.onDidChange,
this.globalEditsEditorNavigationStack.onDidChange,
this.globalNavigationsEditorNavigationStack.onDidChange
);

Expand Down Expand Up @@ -321,7 +335,7 @@ export class HistoryService extends Disposable implements IHistoryService {
// Send to specific navigation stack based on reason
switch (event.reason) {
case EditorPaneSelectionChangeReason.EDIT:
this.globalModificationsEditorNavigationStack.notifyNavigation(editorPane, event);
this.globalEditsEditorNavigationStack.notifyNavigation(editorPane, event);
break;
case EditorPaneSelectionChangeReason.NAVIGATION: {

Expand All @@ -342,7 +356,7 @@ export class HistoryService extends Disposable implements IHistoryService {
private getStack(filter = GoFilter.NONE): EditorNavigationStack {
switch (filter) {
case GoFilter.NONE: return this.globalDefaultEditorNavigationStack;
case GoFilter.EDITS: return this.globalModificationsEditorNavigationStack;
case GoFilter.EDITS: return this.globalEditsEditorNavigationStack;
case GoFilter.NAVIGATION: return this.globalNavigationsEditorNavigationStack;
}
}
Expand Down

0 comments on commit 8ae03a1

Please sign in to comment.