Skip to content

Commit

Permalink
save & restore all editor view states
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Mar 5, 2020
1 parent a4a17ae commit ad22aa2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 82 deletions.
15 changes: 8 additions & 7 deletions src/vs/workbench/contrib/notebook/browser/notebookViewModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { NotebookEditorModel } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput';
import { CellViewModel } from 'vs/workbench/contrib/notebook/browser/renderers/cellViewModel';
import { DisposableStore, Disposable } from 'vs/base/common/lifecycle';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { Emitter, Event } from 'vs/base/common/event';
import { NotebookCellsSplice, ICellEditorViewState } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import * as editorCommon from 'vs/editor/common/editorCommon';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { NotebookEditorModel } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput';
import { CellFindMatch } from 'vs/workbench/contrib/notebook/browser/notebookFindWidget';
import { CellViewModel } from 'vs/workbench/contrib/notebook/browser/renderers/cellViewModel';
import { NotebookCellsSplice } from 'vs/workbench/contrib/notebook/common/notebookCommon';

export interface INotebookEditorViewState {
editingCells: { [key: number]: boolean };
editorViewStates: { [key: number]: ICellEditorViewState };
editorViewStates: { [key: number]: editorCommon.ICodeEditorViewState | null };
}

export class NotebookViewModel extends Disposable {
Expand Down Expand Up @@ -102,7 +103,7 @@ export class NotebookViewModel extends Disposable {
saveEditorViewState(): INotebookEditorViewState {
const state: { [key: number]: boolean } = {};
this.viewCells.filter(cell => cell.isEditing).forEach(cell => state[cell.cell.handle] = true);
const editorViewStates: { [key: number]: ICellEditorViewState } = {};
const editorViewStates: { [key: number]: editorCommon.ICodeEditorViewState } = {};
this.viewCells.map(cell => ({ handle: cell.cell.handle, state: cell.saveEditorViewState() })).forEach(viewState => {
if (viewState.state) {
editorViewStates[viewState.handle] = viewState.state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Disposable } from 'vs/base/common/lifecycle';
import * as UUID from 'vs/base/common/uuid';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { Range } from 'vs/editor/common/core/range';
import { ISelection } from 'vs/editor/common/core/selection';
import * as editorCommon from 'vs/editor/common/editorCommon';
import * as model from 'vs/editor/common/model';
import { SearchParams } from 'vs/editor/common/model/textModelSearch';
Expand All @@ -17,7 +16,7 @@ import { PrefixSumComputer } from 'vs/editor/common/viewModel/prefixSumComputer'
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { CellFindMatch } from 'vs/workbench/contrib/notebook/browser/notebookFindWidget';
import { MarkdownRenderer } from 'vs/workbench/contrib/notebook/browser/renderers/mdRenderer';
import { CellKind, EDITOR_BOTTOM_PADDING, EDITOR_TOP_PADDING, ICell, ICellEditorViewState, IOutput, NotebookCellOutputsSplice } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { CellKind, EDITOR_BOTTOM_PADDING, EDITOR_TOP_PADDING, ICell, IOutput, NotebookCellOutputsSplice } from 'vs/workbench/contrib/notebook/common/notebookCommon';

export class CellViewModel extends Disposable {

Expand Down Expand Up @@ -78,7 +77,7 @@ export class CellViewModel extends Disposable {
private _textModel?: model.ITextModel;
private _textEditor?: ICodeEditor;
private _buffer: model.ITextBuffer | null;
private _editorViewStates: ICellEditorViewState | null;
private _editorViewStates: editorCommon.ICodeEditorViewState | null;

readonly id: string = UUID.generateUuid();

Expand All @@ -104,15 +103,13 @@ export class CellViewModel extends Disposable {
this._isEditing = false;
}

restoreEditorViewState(editorViewStates: ICellEditorViewState) {
restoreEditorViewState(editorViewStates: editorCommon.ICodeEditorViewState | null) {
this._editorViewStates = editorViewStates;
}

saveEditorViewState() {
if (this._textEditor) {
this._editorViewStates = {
selections: this.saveViewState()
};
this._editorViewStates = this.saveViewState();
}

return this._editorViewStates;
Expand Down Expand Up @@ -167,77 +164,19 @@ export class CellViewModel extends Disposable {
return false;
}

private saveViewState(): editorCommon.ICursorState[] {
private saveViewState(): editorCommon.ICodeEditorViewState | null {
if (!this._textEditor) {
return [];
return null;
}

let result: editorCommon.ICursorState[] = [];

const selections = this._textEditor.getSelections();

if (!selections) {
return [];
}

for (let i = 0, len = selections.length; i < len; i++) {
const selection = selections[i];

result.push({
inSelectionMode: !selection.isEmpty(),
selectionStart: {
lineNumber: selection.selectionStartLineNumber,
column: selection.selectionStartColumn,
},
position: {
lineNumber: selection.positionLineNumber,
column: selection.positionColumn,
}
});
}

return result;
return this._textEditor.saveViewState();
}


private restoreViewState(states: editorCommon.ICursorState[]): void {

let desiredSelections: ISelection[] = [];

for (let i = 0, len = states.length; i < len; i++) {
const state = states[i];

let positionLineNumber = 1;
let positionColumn = 1;

// Avoid missing properties on the literal
if (state.position && state.position.lineNumber) {
positionLineNumber = state.position.lineNumber;
}
if (state.position && state.position.column) {
positionColumn = state.position.column;
}

let selectionStartLineNumber = positionLineNumber;
let selectionStartColumn = positionColumn;

// Avoid missing properties on the literal
if (state.selectionStart && state.selectionStart.lineNumber) {
selectionStartLineNumber = state.selectionStart.lineNumber;
}
if (state.selectionStart && state.selectionStart.column) {
selectionStartColumn = state.selectionStart.column;
}

desiredSelections.push({
selectionStartLineNumber: selectionStartLineNumber,
selectionStartColumn: selectionStartColumn,
positionLineNumber: positionLineNumber,
positionColumn: positionColumn
});
private restoreViewState(state: editorCommon.ICodeEditorViewState | null): void {
if (state) {
this._textEditor?.restoreViewState(state);
}

this._textEditor?.setSelections(desiredSelections);
}

//#endregion
Expand Down Expand Up @@ -318,14 +257,12 @@ export class CellViewModel extends Disposable {
this._textEditor = editor;

if (this._editorViewStates) {
this.restoreViewState(this._editorViewStates.selections);
this.restoreViewState(this._editorViewStates);
}
}

detachTextEditor() {
this._editorViewStates = {
selections: this.saveViewState()
};
this._editorViewStates = this.saveViewState();
this._textEditor = undefined;
}

Expand Down

0 comments on commit ad22aa2

Please sign in to comment.