-
Notifications
You must be signed in to change notification settings - Fork 290
/
creation.ts
203 lines (177 loc) · 7.91 KB
/
creation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { Identifiers } from '../../../../client/datascience/constants';
import {
IFinishCell,
InteractiveWindowMessages
} from '../../../../client/datascience/interactive-common/interactiveWindowTypes';
import { ICell, IJupyterExtraSettings } from '../../../../client/datascience/types';
import { removeLinesFromFrontAndBack } from '../../../common';
import { createCellVM, extractInputText, ICellViewModel, IMainState } from '../../../interactive-common/mainState';
import { postActionToExtension } from '../../../interactive-common/redux/helpers';
import { Helpers } from '../../../interactive-common/redux/reducers/helpers';
import { IAddCellAction, ICellAction } from '../../../interactive-common/redux/reducers/types';
import { InteractiveReducerArg } from '../mapping';
export namespace Creation {
function isCellSupported(state: IMainState, cell: ICell): boolean {
// Skip message cells in test mode
if (state.testMode) {
return cell.data.cell_type !== 'messages';
}
return true;
}
function extractInputBlockText(cellVM: ICellViewModel, settings?: IJupyterExtraSettings) {
// Use the base function first
const text = extractInputText(cellVM, settings);
// Then remove text on the front and back. We only do this for the interactive window
return removeLinesFromFrontAndBack(text);
}
export function alterCellVM(
cellVM: ICellViewModel,
settings?: IJupyterExtraSettings,
visible?: boolean,
expanded?: boolean
): ICellViewModel {
if (cellVM.cell.data.cell_type === 'code') {
// If we are already in the correct state, return back our initial cell vm
if (cellVM.inputBlockShow === visible && cellVM.inputBlockOpen === expanded) {
return cellVM;
}
const newCellVM = { ...cellVM };
if (cellVM.inputBlockShow !== visible) {
if (visible) {
// Show the cell, the rest of the function will add on correct collapse state
newCellVM.inputBlockShow = true;
} else {
// Hide this cell
newCellVM.inputBlockShow = false;
}
}
// No elseif as we want newly visible cells to pick up the correct expand / collapse state
if (cellVM.inputBlockOpen !== expanded && cellVM.inputBlockCollapseNeeded && cellVM.inputBlockShow) {
let newText = extractInputBlockText(cellVM, settings);
// While extracting the text, we might eliminate all extra lines
if (newText.includes('\n')) {
if (expanded) {
// Expand the cell
newCellVM.inputBlockOpen = true;
newCellVM.inputBlockText = newText;
} else {
// Collapse the cell
if (newText.length > 0) {
newText = newText.split('\n', 1)[0];
newText = newText.slice(0, 255); // Slice to limit length, slicing past length is fine
newText = newText.concat('...');
}
newCellVM.inputBlockOpen = false;
newCellVM.inputBlockText = newText;
}
} else {
// If all lines eliminated, get rid of the collapse bar.
newCellVM.inputBlockCollapseNeeded = false;
newCellVM.inputBlockOpen = true;
newCellVM.inputBlockText = newText;
}
}
return newCellVM;
}
return cellVM;
}
export function prepareCellVM(cell: ICell, mainState: IMainState): ICellViewModel {
let cellVM: ICellViewModel = createCellVM(cell, mainState.settings, false, mainState.debugging);
const visible = mainState.settings ? mainState.settings.showCellInputCode : false;
const expanded = !mainState.settings?.collapseCellInputCodeByDefault;
// Set initial cell visibility and collapse
cellVM = alterCellVM(cellVM, mainState.settings, visible, expanded);
cellVM.hasBeenRun = true;
return cellVM;
}
export function startCell(arg: InteractiveReducerArg<ICell>): IMainState {
if (isCellSupported(arg.prevState, arg.payload.data)) {
const result = Helpers.updateOrAdd(arg, prepareCellVM);
if (
result.cellVMs.length > arg.prevState.cellVMs.length &&
arg.payload.data.id !== Identifiers.EditCellId
) {
const cellVM = result.cellVMs[result.cellVMs.length - 1];
// We're adding a new cell here. Tell the intellisense engine we have a new cell
postActionToExtension(arg, InteractiveWindowMessages.UpdateModel, {
source: 'user',
kind: 'add',
oldDirty: arg.prevState.dirty,
newDirty: true,
cell: cellVM.cell,
fullText: extractInputText(cellVM, result.settings),
currentText: cellVM.inputBlockText
});
}
return result;
}
return arg.prevState;
}
export function updateCell(arg: InteractiveReducerArg<ICell>): IMainState {
if (isCellSupported(arg.prevState, arg.payload.data)) {
return Helpers.updateOrAdd(arg, prepareCellVM);
}
return arg.prevState;
}
export function finishCell(arg: InteractiveReducerArg<IFinishCell>): IMainState {
if (isCellSupported(arg.prevState, arg.payload.data.cell)) {
return Helpers.updateOrAdd(
{ ...arg, payload: { ...arg.payload, data: arg.payload.data.cell } },
prepareCellVM
);
}
return arg.prevState;
}
export function deleteAllCells(arg: InteractiveReducerArg<IAddCellAction>): IMainState {
// Send messages to other side to indicate the deletes
postActionToExtension(arg, InteractiveWindowMessages.DeleteAllCells);
return {
...arg.prevState,
cellVMs: [],
undoStack: Helpers.pushStack(arg.prevState.undoStack, arg.prevState.cellVMs)
};
}
export function deleteCell(arg: InteractiveReducerArg<ICellAction>): IMainState {
const index = arg.prevState.cellVMs.findIndex((c) => c.cell.id === arg.payload.data.cellId);
if (index >= 0 && arg.payload.data.cellId) {
// Send messages to other side to indicate the delete
postActionToExtension(arg, InteractiveWindowMessages.UpdateModel, {
source: 'user',
kind: 'remove',
index,
oldDirty: arg.prevState.dirty,
newDirty: true,
cell: arg.prevState.cellVMs[index].cell
});
const newVMs = arg.prevState.cellVMs.filter((_c, i) => i !== index);
return {
...arg.prevState,
cellVMs: newVMs,
undoStack: Helpers.pushStack(arg.prevState.undoStack, arg.prevState.cellVMs)
};
}
return arg.prevState;
}
export function unmount(arg: InteractiveReducerArg): IMainState {
return {
...arg.prevState,
cellVMs: [],
undoStack: [],
redoStack: [],
editCellVM: undefined
};
}
export function loaded(arg: InteractiveReducerArg<{ cells: ICell[] }>): IMainState {
postActionToExtension(arg, InteractiveWindowMessages.LoadAllCellsComplete, {
cells: []
});
return {
...arg.prevState,
loaded: true,
busy: false
};
}
}