Skip to content

Commit

Permalink
Merge pull request #2073 from nteract/bug-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya authored May 18, 2021
2 parents f6612ad + 2436fbf commit 1312f0b
Show file tree
Hide file tree
Showing 18 changed files with 319 additions and 232 deletions.
4 changes: 4 additions & 0 deletions lib/code-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,7 @@ function adjustCellFoldRange(editor: TextEditor, range: Range) {
new Point(endRow, endWidth)
);
}

export function getEscapeBlankRowsEndRow(editor: TextEditor, end: Point) {
return end.row === editor.getLastBufferRow() ? end.row : end.row - 1;
}
2 changes: 1 addition & 1 deletion lib/kernel-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class KernelManager {
});
}

async updateKernelSpecs(grammar: Grammar | null | undefined) {
async updateKernelSpecs(grammar?: Grammar | null | undefined) {
const kernelSpecs = await this.update();

if (kernelSpecs.length === 0) {
Expand Down
8 changes: 6 additions & 2 deletions lib/kernel-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Grammar } from "atom";
import { observable, action } from "mobx";
import { log } from "./utils";
import type { KernelspecMetadata } from "@nteract/types";
import type { Kernel } from "@jupyterlab/services";

export type ResultsCallback = (
message: any,
Expand All @@ -18,15 +19,18 @@ export default class KernelTransport {
inspector = {
bundle: {},
};
kernelSpec: KernelspecMetadata;
kernelSpec: Kernel.ISpecModel | KernelspecMetadata;
grammar: Grammar;
language: string;
displayName: string;
// Only `WSKernel` would have `gatewayName` property and thus not initialize it here,
// still `KernelTransport` is better to have `gatewayName` property for code simplicity in the other parts of code
gatewayName: string | null | undefined;

constructor(kernelSpec: KernelspecMetadata, grammar: Grammar) {
constructor(
kernelSpec: Kernel.ISpecModel | KernelspecMetadata,
grammar: Grammar
) {
this.kernelSpec = kernelSpec;
this.grammar = grammar;
this.language = kernelSpec.language.toLowerCase();
Expand Down
5 changes: 3 additions & 2 deletions lib/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
import InputView from "./input-view";
import KernelTransport from "./kernel-transport";
import type { ResultsCallback } from "./kernel-transport";
import type { Kernel as JupyterlabKernel } from "@jupyterlab/services";

import type { Message } from "./hydrogen";
import type { KernelspecMetadata } from "@nteract/types";
Expand Down Expand Up @@ -215,7 +216,7 @@ export default class Kernel {
this.middleware = [delegateToTransport];
}

get kernelSpec(): KernelspecMetadata {
get kernelSpec(): JupyterlabKernel.ISpecModel | KernelspecMetadata {
return this.transport.kernelSpec;
}

Expand Down Expand Up @@ -305,7 +306,7 @@ export default class Kernel {
this.firstMiddlewareAdapter.shutdown();
}

restart(onRestarted: ((...args: Array<any>) => any) | null | undefined) {
restart(onRestarted?: ((...args: Array<any>) => any) | null | undefined) {
this.firstMiddlewareAdapter.restart(onRestarted);
this.setExecutionCount(0);
this.setLastExecutionTime("No execution");
Expand Down
164 changes: 84 additions & 80 deletions lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import KernelPicker from "./kernel-picker";
import WSKernelPicker from "./ws-kernel-picker";
import ExistingKernelPicker from "./existing-kernel-picker";
import HydrogenProvider from "./plugin-api/hydrogen-provider";
import store from "./store";
import store, { Store, StoreLike } from "./store";
import kernelManager from "./kernel-manager";
import services from "./services";
import * as commands from "./commands";
Expand Down Expand Up @@ -85,67 +85,72 @@ export function activate() {
})
);
store.subscriptions.add(
atom.commands.add("atom-text-editor:not([mini])", {
"hydrogen:run": () => run(),
"hydrogen:run-all": () => runAll(),
"hydrogen:run-all-above": () => runAllAbove(),
"hydrogen:run-and-move-down": () => run(true),
"hydrogen:run-cell": () => runCell(),
"hydrogen:run-cell-and-move-down": () => runCell(true),
"hydrogen:toggle-watches": () => atom.workspace.toggle(WATCHES_URI),
"hydrogen:toggle-output-area": () => commands.toggleOutputMode(),
"hydrogen:toggle-kernel-monitor": async () => {
const lastItem = atom.workspace.getActivePaneItem();
const lastPane = atom.workspace.paneForItem(lastItem);
await atom.workspace.toggle(KERNEL_MONITOR_URI);
if (lastPane) {
lastPane.activate();
}
},
"hydrogen:start-local-kernel": () => startZMQKernel(),
"hydrogen:connect-to-remote-kernel": () => connectToWSKernel(),
"hydrogen:connect-to-existing-kernel": () => connectToExistingKernel(),
"hydrogen:add-watch": () => {
if (store.kernel) {
store.kernel.watchesStore.addWatchFromEditor(store.editor);
openOrShowDock(WATCHES_URI);
}
},
"hydrogen:remove-watch": () => {
if (store.kernel) {
store.kernel.watchesStore.removeWatch();
openOrShowDock(WATCHES_URI);
}
},
"hydrogen:update-kernels": () => kernelManager.updateKernelSpecs(),
"hydrogen:toggle-inspector": () => commands.toggleInspector(store),
"hydrogen:interrupt-kernel": () =>
handleKernelCommand(
{
command: "interrupt-kernel",
},
store
),
"hydrogen:restart-kernel": () =>
handleKernelCommand(
{
command: "restart-kernel",
},
store
),
"hydrogen:shutdown-kernel": () =>
handleKernelCommand(
{
command: "shutdown-kernel",
},
store
),
"hydrogen:clear-result": () => result.clearResult(store),
"hydrogen:export-notebook": () => exportNotebook(),
"hydrogen:fold-current-cell": () => foldCurrentCell(),
"hydrogen:fold-all-but-current-cell": () => foldAllButCurrentCell(),
"hydrogen:clear-results": () => result.clearResults(store),
})
atom.commands.add<"atom-text-editor:not([mini])">(
"atom-text-editor:not([mini])",
{
"hydrogen:run": () => run(),
"hydrogen:run-all": () => runAll(),
"hydrogen:run-all-above": () => runAllAbove(),
"hydrogen:run-and-move-down": () => run(true),
"hydrogen:run-cell": () => runCell(),
"hydrogen:run-cell-and-move-down": () => runCell(true),
"hydrogen:toggle-watches": () => atom.workspace.toggle(WATCHES_URI),
"hydrogen:toggle-output-area": () => commands.toggleOutputMode(),
"hydrogen:toggle-kernel-monitor": async () => {
const lastItem = atom.workspace.getActivePaneItem();
const lastPane = atom.workspace.paneForItem(lastItem);
await atom.workspace.toggle(KERNEL_MONITOR_URI);
if (lastPane) {
lastPane.activate();
}
},
"hydrogen:start-local-kernel": () => startZMQKernel(),
"hydrogen:connect-to-remote-kernel": () => connectToWSKernel(),
"hydrogen:connect-to-existing-kernel": () => connectToExistingKernel(),
"hydrogen:add-watch": () => {
if (store.kernel) {
store.kernel.watchesStore.addWatchFromEditor(store.editor);
openOrShowDock(WATCHES_URI);
}
},
"hydrogen:remove-watch": () => {
if (store.kernel) {
store.kernel.watchesStore.removeWatch();
openOrShowDock(WATCHES_URI);
}
},
"hydrogen:update-kernels": async () => {
await kernelManager.updateKernelSpecs();
},
"hydrogen:toggle-inspector": () => commands.toggleInspector(store),
"hydrogen:interrupt-kernel": () =>
handleKernelCommand(
{
command: "interrupt-kernel",
},
store
),
"hydrogen:restart-kernel": () =>
handleKernelCommand(
{
command: "restart-kernel",
},
store
),
"hydrogen:shutdown-kernel": () =>
handleKernelCommand(
{
command: "shutdown-kernel",
},
store
),
"hydrogen:clear-result": () => result.clearResult(store),
"hydrogen:export-notebook": () => exportNotebook(),
"hydrogen:fold-current-cell": () => foldCurrentCell(),
"hydrogen:fold-all-but-current-cell": () => foldAllButCurrentCell(),
"hydrogen:clear-results": () => result.clearResults(store),
}
)
);
store.subscriptions.add(
atom.commands.add("atom-workspace", {
Expand Down Expand Up @@ -211,6 +216,9 @@ export function activate() {

case KERNEL_MONITOR_URI:
return new KernelMonitorPane(store);
default: {
return;
}
}
})
);
Expand Down Expand Up @@ -278,23 +286,19 @@ function connectToExistingKernel() {
existingKernelPicker.toggle();
}

interface KernelCommand {
command: string;
payload?: KernelspecMetadata | null | undefined;
}

function handleKernelCommand(
{
command,
payload,
}: {
command: string;
payload: KernelspecMetadata | null | undefined;
},
{
kernel,
markers,
}: {
kernel: Kernel | null | undefined;
markers: MarkerStore | null | undefined;
}
{ command, payload }: KernelCommand, // TODO payload is not used!
{ kernel, markers }: Store | StoreLike
) {
log("handleKernelCommand:", arguments);
log("handleKernelCommand:", [
{ command, payload },
{ kernel, markers },
]);

if (!kernel) {
const message = "No running kernel for grammar or editor found";
Expand Down Expand Up @@ -403,7 +407,7 @@ function _runAll(
const row = codeManager.escapeBlankRows(
editor,
start.row,
end.row == editor.getLastBufferRow() ? end.row : end.row - 1
codeManager.getEscapeBlankRowsEndRow(editor, end)
);
const cellType = codeManager.getMetadataForRow(editor, start);
const code =
Expand Down Expand Up @@ -457,7 +461,7 @@ function _runAllAbove(editor: TextEditor, kernel: Kernel) {
const row = codeManager.escapeBlankRows(
editor,
start.row,
end.row == editor.getLastBufferRow() ? end.row : end.row - 1
codeManager.getEscapeBlankRowsEndRow(editor, end)
);
const cellType = codeManager.getMetadataForRow(editor, start);

Expand Down Expand Up @@ -496,7 +500,7 @@ function runCell(moveDown: boolean = false) {
const row = codeManager.escapeBlankRows(
editor,
start.row,
end.row == editor.getLastBufferRow() ? end.row : end.row - 1
codeManager.getEscapeBlankRowsEndRow(editor, end)
);
const cellType = codeManager.getMetadataForRow(editor, start);
const code =
Expand Down
5 changes: 5 additions & 0 deletions lib/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,8 @@ const store = new Store();
export default store; // For debugging

window.hydrogen_store = store;

export interface StoreLike {
kernel?: Kernel | null | undefined;
markers?: MarkerStore | null | undefined;
}
12 changes: 11 additions & 1 deletion lib/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ export function msgSpecV4toV5(message: Message) {
message.content.text = message.content.data;
}
break;
default: {
// no conversion needed
}
}

return message;
}
const markupGrammars = new Set([
Expand Down Expand Up @@ -317,3 +319,11 @@ export function setPreviouslyFocusedElement(obj: {
obj.previouslyFocusedElement = activeElement;
}
}

/** Make the properties of a type Writable */
export type Writeable<T> = { -readonly [P in keyof T]: T[P] };

/** Make the properties of aexport type Writable recursively */
export type DeepWriteable<T> = {
-readonly [P in keyof T]: DeepWriteable<T[P]>;
};
Loading

0 comments on commit 1312f0b

Please sign in to comment.