Skip to content

Commit

Permalink
feat: language selection into context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
CrystallineCat committed Aug 20, 2022
1 parent bf3023a commit ce7e3c5
Show file tree
Hide file tree
Showing 28 changed files with 277 additions and 301 deletions.
24 changes: 0 additions & 24 deletions packages/actions/__tests__/actions-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,6 @@ import * as actionTypes from "../src";

const actions = actionTypes;

describe("setLanguageInfo", () => {
test("creates a SET_LANGUAGE_INFO action", () => {
const langInfo = {
codemirror_mode: { name: "ipython", version: 3 },
file_extension: ".py",
mimetype: "text/x-python",
name: "python",
nbconvert_exporter: "python",
pygments_lexer: "ipython3",
version: "3.5.1"
};

const kernelRef = createKernelRef();
const contentRef = createContentRef();

expect(
actions.setLanguageInfo({ langInfo, kernelRef, contentRef })
).toEqual({
type: actionTypes.SET_LANGUAGE_INFO,
payload: { langInfo, kernelRef, contentRef }
});
});
});

describe("unhideAll", () => {
test("allows being called with sets defaults for outputHidden and inputHidden", () => {
const contentRef = createContentRef();
Expand Down
2 changes: 1 addition & 1 deletion packages/actions/src/actionTypes/kernel_misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const KERNEL_RAW_STDERR = "KERNEL_RAW_STDERR";
export const DELETE_CONNECTION_FILE_FAILED = "DELETE_CONNECTION_FILE_FAILED";
export const DELETE_CONNECTION_FILE_SUCCESSFUL = "DELETE_CONNECTION_FILE_SUCCESSFUL";

export type SetKernelInfo = Action <typeof SET_KERNEL_INFO, HasKernel & { info: KernelInfo }>;
export type SetKernelInfo = Action <typeof SET_KERNEL_INFO, HasContent & HasKernel & { info: KernelInfo }>;
export type SetKernelMetadata = Action <typeof SET_KERNEL_METADATA, HasContent & { kernelInfo: KernelspecRecord }>;
export type KernelRawStdout = Action <typeof KERNEL_RAW_STDOUT, HasKernel & { text: string }>;
export type KernelRawStderr = Action <typeof KERNEL_RAW_STDERR, HasKernel & { text: string }>;
Expand Down
34 changes: 7 additions & 27 deletions packages/commutable/__tests__/v4.spec.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,18 @@
import Immutable, { Record } from "immutable";

import {
makeDisplayData,
makeErrorOutput,
makeExecuteResult,
makeStreamOutput,
OnDiskStreamOutput
} from "../src/outputs";
import {
createCodeCell,
createMarkdownCell,
NotebookRecordParams,
ImmutableNotebook,
makeNotebookRecord
} from "../src/structures";

import * as Immutable from "immutable";
import { makeDisplayData, makeErrorOutput, makeExecuteResult, makeStreamOutput, OnDiskStreamOutput } from "../src/outputs";
import * as primitives from "../src/primitives";

import {
cellToJS,
outputToJS,
fromJS,
toJS,
NotebookV4,
Cell
} from "../src/v4";
import { CodeCellParams, ImmutableCell } from "../src/cells";
import { createCodeCell, createMarkdownCell, makeNotebookRecord } from "../src/structures";
import { cellToJS, fromJS, NotebookV4, outputToJS, toJS } from "../src/v4";

describe("cellToJS", () => {
it("throws an error for unkown cell types", () => {
const cell = Immutable.Map({
cell_type: "not_real"
});

// @ts-ignore supposed to fail
const invocation = () => cellToJS(cell);

expect(invocation).toThrowError("Cell type unknown at runtime");
});
it("process known cell types", () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/commutable/src/cells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface MarkdownCellParams {
id?: string;
source: string;
metadata: ImmutableMap<string, any>;
attachments: ImmutableMap<string, ImmutableMap<string, string>>;
}

export const makeMarkdownCell = Record<MarkdownCellParams>({
Expand All @@ -60,6 +61,7 @@ export const makeMarkdownCell = Record<MarkdownCellParams>({
}),
}),
source: "",
attachments: ImmutableMap(),
});

export type ImmutableMarkdownCell = RecordOf<MarkdownCellParams>;
Expand All @@ -71,6 +73,7 @@ export interface RawCellParams {
cell_type: "raw";
source: string;
metadata: ImmutableMap<string, any>;
attachments: ImmutableMap<string, ImmutableMap<string, string>>;
}

export const makeRawCell = Record<RawCellParams>({
Expand All @@ -83,6 +86,7 @@ export const makeRawCell = Record<RawCellParams>({
}),
}),
source: "",
attachments: ImmutableMap(),
});

export type ImmutableRawCell = RecordOf<RawCellParams>;
Expand Down
2 changes: 1 addition & 1 deletion packages/commutable/src/structures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export type ImmutableNotebook = ImmutableRecord<NotebookRecordParams> &
export const makeNotebookRecord = ImmutableRecord<NotebookRecordParams>({
cellOrder: ImmutableList(),
cellMap: ImmutableMap(),
nbformat_minor: 0,
nbformat_minor: 5,
nbformat: 4,
metadata: ImmutableMap()
});
Expand Down
34 changes: 2 additions & 32 deletions packages/epics/__tests__/kernel-lifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,10 @@ describe("acquireKernelInfo", () => {
const actions = await obs.pipe(toArray()).toPromise();

expect(actions).toEqual([
{
payload: {
contentRef: "fakeContentRef",
kernelRef: "fakeKernelRef",
langInfo: {
name: "python",
version: "3.6.5",
mimetype: "text/x-python",
codemirror_mode: { name: "ipython", version: 3 },
pygments_lexer: "ipython3",
nbconvert_exporter: "python",
file_extension: ".py"
}
},
type: "SET_LANGUAGE_INFO"
},
{
type: "CORE/SET_KERNEL_INFO",
payload: {
contentRef: "fakeContentRef",
info: {
protocolVersion: "5.1",
implementation: "ipython",
Expand Down Expand Up @@ -316,25 +301,10 @@ describe("acquireKernelInfo", () => {
const actions = await obs.pipe(toArray()).toPromise();

expect(actions).toEqual([
{
payload: {
contentRef: "fakeContentRef",
kernelRef: "fakeKernelRef",
langInfo: {
name: "python",
version: "3.6.5",
mimetype: "text/x-python",
codemirror_mode: { name: "ipython", version: 3 },
pygments_lexer: "ipython3",
nbconvert_exporter: "python",
file_extension: ".py"
}
},
type: "SET_LANGUAGE_INFO"
},
{
type: "CORE/SET_KERNEL_INFO",
payload: {
contentRef: "fakeContentRef",
info: {
protocolVersion: "5.1",
implementation: "ipython",
Expand Down
9 changes: 1 addition & 8 deletions packages/epics/src/kernel-lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,9 @@ export function acquireKernelInfo(
];
} else {
result = [
// The original action we were using
/* TODO Set language info
actions.setLanguageInfo({
langInfo: msg.content.language_info,
kernelRef,
contentRef
}),
*/
actions.setKernelInfo({
kernelRef,
contentRef,
info
}),
actions.setExecutionState({ kernelStatus: KernelStatus.Launched, kernelRef })
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { EditableText } from "@blueprintjs/core";
import { MythicComponent } from "@nteract/myths";
import React from "react";
import styled from "styled-components";
import { CellAddress } from "../../types";
import { setCellLanguageFromName } from "../myths/set-cell-language-from-name";
import { languageOfCellOnly } from "../selectors";

const LeftRightLayout =
styled.div`
display: flex;
flex-grow: 1;
& > *:nth-child(1) {
flex-grow: 1;
}
& > *:nth-child(2) {
& > * {
text-align: right;
}
& > input {
color: black;
background: white;
opacity: 50%;
}
}
`;

export const LanguageSelector =
setCellLanguageFromName.createConnectedComponent(
"LanguageSelector",
class extends MythicComponent<
typeof setCellLanguageFromName,
{cellAddress: CellAddress, language?: string}
> {
render(): JSX.Element {
const onTextChange = (text: string) =>
this.props.setCellLanguageFromName({ref: this.props.cellAddress, langName: text});

return (
<LeftRightLayout className="set-cell-language">
<output>{this.props.language}</output>
<EditableText
selectAllOnFocus={true}
placeholder="Set cell language..."
onChange={onTextChange}
/>
</LeftRightLayout>
);
}

onTextChange(text: string) {
this.props.setCellLanguageFromName({ref: this.props.cellAddress, langName: text});
}
},
(state, { cellAddress }) => ({
language: languageOfCellOnly(state, cellAddress),
}),
);
24 changes: 16 additions & 8 deletions packages/mythic-notebook/src/language-info/data/Languages.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"shell.execute_reply": "2021-10-27T09:05:51.168Z",
"iopub.status.idle": "2021-10-27T09:05:50.886Z"
}
}
},
"id": "2883bc08-e181-435a-b67a-16b2dd6c2596"
},
{
"cell_type": "code",
Expand Down Expand Up @@ -99,7 +100,8 @@
"iopub.status.idle": "2021-10-27T09:05:50.957Z",
"shell.execute_reply": "2021-10-27T09:05:51.177Z"
}
}
},
"id": "aed711d2-c58a-4d48-b912-2aecff345594"
},
{
"cell_type": "code",
Expand All @@ -108,6 +110,7 @@
"\n",
"@register_cell_magic\n",
"def assign(line, cell):\n",
" \"\"\"Assign the text contents of the cell to the variable named.\"\"\"\n",
" get_ipython().user_ns[line.strip()] = cell"
],
"outputs": [],
Expand All @@ -128,7 +131,8 @@
"iopub.status.idle": "2021-10-27T09:05:51.019Z",
"shell.execute_reply": "2021-10-27T09:05:51.186Z"
}
}
},
"id": "8de87bfe-5f4d-4571-b2ab-7fb1415fb611"
},
{
"cell_type": "code",
Expand Down Expand Up @@ -174,15 +178,17 @@
"nteract": {
"transient": {
"deleting": false
}
},
"languageName": "ts"
},
"execution": {
"iopub.status.busy": "2021-10-27T09:05:51.039Z",
"iopub.execute_input": "2021-10-27T09:05:51.052Z",
"iopub.status.idle": "2021-10-27T09:05:51.078Z",
"shell.execute_reply": "2021-10-27T09:05:51.195Z"
}
}
},
"id": "5f4d7bfe-5f4d-4571-b2ab-7fb1415fb611"
},
{
"cell_type": "code",
Expand Down Expand Up @@ -223,7 +229,8 @@
"iopub.status.idle": "2021-10-27T09:05:51.153Z",
"shell.execute_reply": "2021-10-27T09:05:51.204Z"
}
}
},
"id": "0d6783e1-2022-4415-b02a-5320967a128b"
},
{
"cell_type": "code",
Expand All @@ -240,7 +247,8 @@
"deleting": false
}
}
}
},
"id": "278bb97b-c567-4470-bef1-8d3c3200856c"
}
],
"metadata": {
Expand Down Expand Up @@ -276,5 +284,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 5
}
4 changes: 3 additions & 1 deletion packages/mythic-notebook/src/language-info/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export { languageInfo } from "./package";
export * from "./types";
export * from "./selectors";
export * from "./myths/set-language";
export * from "./myths/set-content-language-from-info";
export * from "./myths/set-cell-language-from-name";
export * from "./components/language-selector";

0 comments on commit ce7e3c5

Please sign in to comment.