Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'use strict';
import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api';
import { IServerState } from '../../../datascience-ui/interactive-common/mainState';
import { IAddCellAction } from '../../../datascience-ui/interactive-common/redux/reducers/types';
import { CssMessages, IGetCssRequest, IGetCssResponse, IGetMonacoThemeRequest } from '../messages';
import { ICell, IInteractiveWindowInfo, IJupyterVariable, IJupyterVariablesRequest, IJupyterVariablesResponse } from '../types';

Expand Down Expand Up @@ -307,7 +308,7 @@ export class IInteractiveWindowMapping {
public [InteractiveWindowMessages.GetAllCells]: ICell;
public [InteractiveWindowMessages.ReturnAllCells]: ICell[];
public [InteractiveWindowMessages.DeleteCell]: never | undefined;
public [InteractiveWindowMessages.DeleteAllCells]: never | undefined;
public [InteractiveWindowMessages.DeleteAllCells]: IAddCellAction;
public [InteractiveWindowMessages.Undo]: never | undefined;
public [InteractiveWindowMessages.Redo]: never | undefined;
public [InteractiveWindowMessages.ExpandAll]: never | undefined;
Expand Down
5 changes: 3 additions & 2 deletions src/datascience-ui/history-react/redux/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { IMainState, IServerState } from '../../interactive-common/mainState';
import { IncomingMessageActions } from '../../interactive-common/redux/postOffice';
import {
CommonActionType,
IAddCellAction,
ICellAction,
ICodeAction,
IEditCellAction,
Expand Down Expand Up @@ -42,7 +43,7 @@ export class IInteractiveActionMapping {
public [CommonActionType.GATHER_CELL]: InteractiveReducerFunc<ICellAction>;
public [CommonActionType.EDIT_CELL]: InteractiveReducerFunc<IEditCellAction>;
public [CommonActionType.SUBMIT_INPUT]: InteractiveReducerFunc<ICodeAction>;
public [CommonActionType.DELETE_ALL_CELLS]: InteractiveReducerFunc<never | undefined>;
public [CommonActionType.DELETE_ALL_CELLS]: InteractiveReducerFunc<IAddCellAction>;
public [CommonActionType.EXPAND_ALL]: InteractiveReducerFunc<never | undefined>;
public [CommonActionType.COLLAPSE_ALL]: InteractiveReducerFunc<never | undefined>;
public [CommonActionType.EDITOR_LOADED]: InteractiveReducerFunc<never | undefined>;
Expand All @@ -63,7 +64,7 @@ export class IInteractiveActionMapping {
public [IncomingMessageActions.GETALLCELLS]: InteractiveReducerFunc<never | undefined>;
public [IncomingMessageActions.EXPANDALL]: InteractiveReducerFunc<never | undefined>;
public [IncomingMessageActions.COLLAPSEALL]: InteractiveReducerFunc<never | undefined>;
public [IncomingMessageActions.DELETEALLCELLS]: InteractiveReducerFunc<never | undefined>;
public [IncomingMessageActions.DELETEALLCELLS]: InteractiveReducerFunc<IAddCellAction>;
public [IncomingMessageActions.STARTPROGRESS]: InteractiveReducerFunc<never | undefined>;
public [IncomingMessageActions.STOPPROGRESS]: InteractiveReducerFunc<never | undefined>;
public [IncomingMessageActions.UPDATESETTINGS]: InteractiveReducerFunc<string>;
Expand Down
21 changes: 18 additions & 3 deletions src/datascience-ui/interactive-common/redux/reducers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ export interface ICellAction {
cellId: string | undefined;
}

export interface IAddCellAction {
/**
* Id of the new cell that is to be added.
* If none provided, then generate a new id.
*/
newCellId: string;
}

export interface ICodeAction extends ICellAction {
code: string;
}
Expand All @@ -103,9 +111,16 @@ export interface IEditCellAction extends ICodeAction {
modelId: string;
}

export interface IExecuteAction extends ICodeAction {
moveOp: 'add' | 'select' | 'none';
}
// I.e. when using the operation `add`, we need the corresponding `IAddCellAction`.
// They are mutually exclusive, if not `add`, then there's no `newCellId`.
export type IExecuteAction =
| (ICodeAction & {
moveOp: 'select' | 'none';
})
| (ICodeAction &
IAddCellAction & {
moveOp: 'add';
});

export interface ICodeCreatedAction extends ICellAction {
modelId: string;
Expand Down
28 changes: 19 additions & 9 deletions src/datascience-ui/native-editor/redux/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// Licensed under the MIT License.
'use strict';
import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api';

import * as uuid from 'uuid/v4';
import { NativeCommandType } from '../../../client/datascience/interactive-common/interactiveWindowTypes';
import { IJupyterVariable, IJupyterVariablesRequest } from '../../../client/datascience/types';
import { CursorPos } from '../../interactive-common/mainState';
import {
CommonAction,
CommonActionType,
IAddCellAction,
ICellAction,
ICellAndCursorAction,
IChangeCellTypeAction,
Expand All @@ -25,9 +26,9 @@ import {

// See https://react-redux.js.org/using-react-redux/connect-mapdispatch#defining-mapdispatchtoprops-as-an-object
export const actionCreators = {
insertAbove: (cellId: string | undefined): CommonAction<ICellAction> => ({ type: CommonActionType.INSERT_ABOVE, payload: { cellId } }),
insertAboveFirst: (): CommonAction<never | undefined> => ({ type: CommonActionType.INSERT_ABOVE_FIRST }),
insertBelow: (cellId: string | undefined): CommonAction<ICellAction> => ({ type: CommonActionType.INSERT_BELOW, payload: { cellId } }),
insertAbove: (cellId: string | undefined): CommonAction<ICellAction & IAddCellAction> => ({ type: CommonActionType.INSERT_ABOVE, payload: { cellId, newCellId: uuid() } }),
insertAboveFirst: (): CommonAction<never | undefined> => ({ type: CommonActionType.INSERT_ABOVE_FIRST, payload: { newCellId: uuid() } }),
insertBelow: (cellId: string | undefined): CommonAction<ICellAction & IAddCellAction> => ({ type: CommonActionType.INSERT_BELOW, payload: { cellId, newCellId: uuid() } }),
focusCell: (cellId: string, cursorPos: CursorPos = CursorPos.Current): CommonAction<ICellAndCursorAction> => ({
type: CommonActionType.FOCUS_CELL,
payload: { cellId, cursorPos }
Expand All @@ -37,11 +38,20 @@ export const actionCreators = {
type: CommonActionType.SELECT_CELL,
payload: { cellId, cursorPos }
}),
addCell: (): CommonAction<never | undefined> => ({ type: CommonActionType.ADD_NEW_CELL }),
executeCell: (cellId: string, code: string, moveOp: 'add' | 'select' | 'none'): CommonAction<IExecuteAction> => ({
type: CommonActionType.EXECUTE_CELL,
payload: { cellId, code, moveOp }
}),
addCell: (): CommonAction<never | undefined> => ({ type: CommonActionType.ADD_NEW_CELL, payload: { newCellId: uuid() } }),
executeCell: (cellId: string, code: string, moveOp: 'add' | 'select' | 'none'): CommonAction<IExecuteAction> => {
if (moveOp === 'add') {
return {
type: CommonActionType.EXECUTE_CELL,
payload: { cellId, code, moveOp, newCellId: uuid() }
};
} else {
return {
type: CommonActionType.EXECUTE_CELL,
payload: { cellId, code, moveOp }
};
}
},
executeAllCells: (): CommonAction<never | undefined> => ({ type: CommonActionType.EXECUTE_ALL_CELLS }),
executeAbove: (cellId: string): CommonAction<ICellAction> => ({ type: CommonActionType.EXECUTE_ABOVE, payload: { cellId } }),
executeCellAndBelow: (cellId: string, code: string): CommonAction<ICodeAction> => ({ type: CommonActionType.EXECUTE_CELL_AND_BELOW, payload: { cellId, code } }),
Expand Down
13 changes: 7 additions & 6 deletions src/datascience-ui/native-editor/redux/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { IMainState, IServerState } from '../../interactive-common/mainState';
import { IncomingMessageActions } from '../../interactive-common/redux/postOffice';
import {
CommonActionType,
IAddCellAction,
ICellAction,
ICellAndCursorAction,
IChangeCellTypeAction,
Expand All @@ -27,12 +28,12 @@ type NativeEditorReducerFunc<T> = ReducerFunc<IMainState, CommonActionType, T>;
export type NativeEditorReducerArg<T = never | undefined> = ReducerArg<IMainState, CommonActionType, T>;

export class INativeEditorActionMapping {
public [CommonActionType.INSERT_ABOVE]: NativeEditorReducerFunc<ICellAction>;
public [CommonActionType.INSERT_BELOW]: NativeEditorReducerFunc<ICellAction>;
public [CommonActionType.INSERT_ABOVE_FIRST]: NativeEditorReducerFunc<never | undefined>;
public [CommonActionType.INSERT_ABOVE]: NativeEditorReducerFunc<ICellAction & IAddCellAction>;
public [CommonActionType.INSERT_BELOW]: NativeEditorReducerFunc<ICellAction & IAddCellAction>;
public [CommonActionType.INSERT_ABOVE_FIRST]: NativeEditorReducerFunc<IAddCellAction>;
public [CommonActionType.FOCUS_CELL]: NativeEditorReducerFunc<ICellAndCursorAction>;
public [CommonActionType.UNFOCUS_CELL]: NativeEditorReducerFunc<ICodeAction>;
public [CommonActionType.ADD_NEW_CELL]: NativeEditorReducerFunc<never | undefined>;
public [CommonActionType.ADD_NEW_CELL]: NativeEditorReducerFunc<IAddCellAction>;
public [CommonActionType.EXECUTE_CELL]: NativeEditorReducerFunc<IExecuteAction>;
public [CommonActionType.EXECUTE_ALL_CELLS]: NativeEditorReducerFunc<never | undefined>;
public [CommonActionType.EXECUTE_ABOVE]: NativeEditorReducerFunc<ICellAction>;
Expand Down Expand Up @@ -74,9 +75,9 @@ export class INativeEditorActionMapping {
public [IncomingMessageActions.LOADALLCELLS]: NativeEditorReducerFunc<ILoadAllCells>;
public [IncomingMessageActions.NOTEBOOKRUNALLCELLS]: NativeEditorReducerFunc<never | undefined>;
public [IncomingMessageActions.NOTEBOOKRUNSELECTEDCELL]: NativeEditorReducerFunc<never | undefined>;
public [IncomingMessageActions.NOTEBOOKADDCELLBELOW]: NativeEditorReducerFunc<never | undefined>;
public [IncomingMessageActions.NOTEBOOKADDCELLBELOW]: NativeEditorReducerFunc<IAddCellAction>;
public [IncomingMessageActions.DOSAVE]: NativeEditorReducerFunc<never | undefined>;
public [IncomingMessageActions.DELETEALLCELLS]: NativeEditorReducerFunc<never | undefined>;
public [IncomingMessageActions.DELETEALLCELLS]: NativeEditorReducerFunc<IAddCellAction>;
public [IncomingMessageActions.UNDO]: NativeEditorReducerFunc<never | undefined>;
public [IncomingMessageActions.REDO]: NativeEditorReducerFunc<never | undefined>;
public [IncomingMessageActions.STARTPROGRESS]: NativeEditorReducerFunc<never | undefined>;
Expand Down
22 changes: 11 additions & 11 deletions src/datascience-ui/native-editor/redux/reducers/creation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ICell, IDataScienceExtraSettings } from '../../../../client/datascience
import { createCellVM, createEmptyCell, CursorPos, extractInputText, ICellViewModel, IMainState } from '../../../interactive-common/mainState';
import { createPostableAction } from '../../../interactive-common/redux/postOffice';
import { Helpers } from '../../../interactive-common/redux/reducers/helpers';
import { ICellAction } from '../../../interactive-common/redux/reducers/types';
import { IAddCellAction, ICellAction } from '../../../interactive-common/redux/reducers/types';
import { actionCreators } from '../actions';
import { NativeEditorReducerArg } from '../mapping';

Expand Down Expand Up @@ -37,8 +37,8 @@ export namespace Creation {
}
}

export function insertAbove(arg: NativeEditorReducerArg<ICellAction>): IMainState {
const newVM = prepareCellVM(createEmptyCell(uuid(), null), false, arg.prevState.settings);
export function insertAbove(arg: NativeEditorReducerArg<ICellAction & IAddCellAction>): IMainState {
const newVM = prepareCellVM(createEmptyCell(arg.payload.newCellId || uuid(), null), false, arg.prevState.settings);
const newList = [...arg.prevState.cellVMs];

// Find the position where we want to insert
Expand Down Expand Up @@ -69,8 +69,8 @@ export namespace Creation {
return result;
}

export function insertBelow(arg: NativeEditorReducerArg<ICellAction>): IMainState {
const newVM = prepareCellVM(createEmptyCell(uuid(), null), false, arg.prevState.settings);
export function insertBelow(arg: NativeEditorReducerArg<ICellAction & IAddCellAction>): IMainState {
const newVM = prepareCellVM(createEmptyCell(arg.payload.newCellId || uuid(), null), false, arg.prevState.settings);
const newList = [...arg.prevState.cellVMs];

// Find the position where we want to insert
Expand Down Expand Up @@ -104,17 +104,17 @@ export namespace Creation {
return result;
}

export function insertAboveFirst(arg: NativeEditorReducerArg): IMainState {
export function insertAboveFirst(arg: NativeEditorReducerArg<IAddCellAction>): IMainState {
// Get the first cell id
const firstCellId = arg.prevState.cellVMs.length > 0 ? arg.prevState.cellVMs[0].cell.id : undefined;

// Do what an insertAbove does
return insertAbove({ ...arg, payload: { cellId: firstCellId } });
return insertAbove({ ...arg, payload: { cellId: firstCellId, newCellId: arg.payload.newCellId } });
}

export function addNewCell(arg: NativeEditorReducerArg): IMainState {
export function addNewCell(arg: NativeEditorReducerArg<IAddCellAction>): IMainState {
// Do the same thing that an insertBelow does using the currently selected cell.
return insertBelow({ ...arg, payload: { cellId: arg.prevState.selectedCellId } });
return insertBelow({ ...arg, payload: { cellId: arg.prevState.selectedCellId, newCellId: arg.payload.newCellId } });
}

export function startCell(arg: NativeEditorReducerArg<ICell>): IMainState {
Expand All @@ -129,13 +129,13 @@ export namespace Creation {
return Helpers.updateOrAdd(arg, (c: ICell, s: IMainState) => prepareCellVM(c, true, s.settings));
}

export function deleteAllCells(arg: NativeEditorReducerArg): IMainState {
export function deleteAllCells(arg: NativeEditorReducerArg<IAddCellAction>): IMainState {
// Send messages to other side to indicate the deletes
arg.queueAction(createPostableAction(InteractiveWindowMessages.DeleteAllCells));

// Just leave one single blank empty cell
const newVM: ICellViewModel = {
cell: createEmptyCell(uuid(), null),
cell: createEmptyCell(arg.payload.newCellId, null),
editable: true,
inputBlockOpen: true,
inputBlockShow: true,
Expand Down
46 changes: 22 additions & 24 deletions src/datascience-ui/native-editor/redux/reducers/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,28 @@ export namespace Execution {
const executeResult = executeRange(arg.prevState, index, index, [arg.payload.code], arg.queueAction);

// Modify the execute result if moving
switch (arg.payload.moveOp) {
case 'add':
// Add a new cell below
return Creation.insertBelow({ ...arg, prevState: executeResult });

case 'select':
// Select the cell below this one, but don't focus it
if (index < arg.prevState.cellVMs.length - 1) {
return Effects.selectCell({
...arg,
prevState: {
...executeResult
},
payload: {
...arg.payload,
cellId: arg.prevState.cellVMs[index + 1].cell.id,
cursorPos: CursorPos.Current
}
});
}
return executeResult;

default:
return executeResult;
// Use `if` instead of `switch case` to ensure type safety.
if (arg.payload.moveOp === 'add') {
// Add a new cell below
return Creation.insertBelow({ ...arg, prevState: executeResult, payload: { ...arg.payload } });
} else if (arg.payload.moveOp === 'select') {
// Select the cell below this one, but don't focus it
if (index < arg.prevState.cellVMs.length - 1) {
return Effects.selectCell({
...arg,
prevState: {
...executeResult
},
payload: {
...arg.payload,
cellId: arg.prevState.cellVMs[index + 1].cell.id,
cursorPos: CursorPos.Current
}
});
}
return executeResult;
} else {
return executeResult;
}
}
return arg.prevState;
Expand Down