提交 ad22aa2f 编写于 作者: R rebornix

save & restore all editor view states

上级 a4a17ae7
......@@ -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 {
......@@ -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;
......
......@@ -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';
......@@ -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 {
......@@ -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();
......@@ -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;
......@@ -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
......@@ -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;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册