Skip to content

Commit 1312f0b

Browse files
authored
Merge pull request #2073 from nteract/bug-fixes
2 parents f6612ad + 2436fbf commit 1312f0b

18 files changed

+319
-232
lines changed

lib/code-manager.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,7 @@ function adjustCellFoldRange(editor: TextEditor, range: Range) {
498498
new Point(endRow, endWidth)
499499
);
500500
}
501+
502+
export function getEscapeBlankRowsEndRow(editor: TextEditor, end: Point) {
503+
return end.row === editor.getLastBufferRow() ? end.row : end.row - 1;
504+
}

lib/kernel-manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export class KernelManager {
137137
});
138138
}
139139

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

143143
if (kernelSpecs.length === 0) {

lib/kernel-transport.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Grammar } from "atom";
22
import { observable, action } from "mobx";
33
import { log } from "./utils";
44
import type { KernelspecMetadata } from "@nteract/types";
5+
import type { Kernel } from "@jupyterlab/services";
56

67
export type ResultsCallback = (
78
message: any,
@@ -18,15 +19,18 @@ export default class KernelTransport {
1819
inspector = {
1920
bundle: {},
2021
};
21-
kernelSpec: KernelspecMetadata;
22+
kernelSpec: Kernel.ISpecModel | KernelspecMetadata;
2223
grammar: Grammar;
2324
language: string;
2425
displayName: string;
2526
// Only `WSKernel` would have `gatewayName` property and thus not initialize it here,
2627
// still `KernelTransport` is better to have `gatewayName` property for code simplicity in the other parts of code
2728
gatewayName: string | null | undefined;
2829

29-
constructor(kernelSpec: KernelspecMetadata, grammar: Grammar) {
30+
constructor(
31+
kernelSpec: Kernel.ISpecModel | KernelspecMetadata,
32+
grammar: Grammar
33+
) {
3034
this.kernelSpec = kernelSpec;
3135
this.grammar = grammar;
3236
this.language = kernelSpec.language.toLowerCase();

lib/kernel.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type {
2020
import InputView from "./input-view";
2121
import KernelTransport from "./kernel-transport";
2222
import type { ResultsCallback } from "./kernel-transport";
23+
import type { Kernel as JupyterlabKernel } from "@jupyterlab/services";
2324

2425
import type { Message } from "./hydrogen";
2526
import type { KernelspecMetadata } from "@nteract/types";
@@ -215,7 +216,7 @@ export default class Kernel {
215216
this.middleware = [delegateToTransport];
216217
}
217218

218-
get kernelSpec(): KernelspecMetadata {
219+
get kernelSpec(): JupyterlabKernel.ISpecModel | KernelspecMetadata {
219220
return this.transport.kernelSpec;
220221
}
221222

@@ -305,7 +306,7 @@ export default class Kernel {
305306
this.firstMiddlewareAdapter.shutdown();
306307
}
307308

308-
restart(onRestarted: ((...args: Array<any>) => any) | null | undefined) {
309+
restart(onRestarted?: ((...args: Array<any>) => any) | null | undefined) {
309310
this.firstMiddlewareAdapter.restart(onRestarted);
310311
this.setExecutionCount(0);
311312
this.setLastExecutionTime("No execution");

lib/main.ts

Lines changed: 84 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import KernelPicker from "./kernel-picker";
2222
import WSKernelPicker from "./ws-kernel-picker";
2323
import ExistingKernelPicker from "./existing-kernel-picker";
2424
import HydrogenProvider from "./plugin-api/hydrogen-provider";
25-
import store from "./store";
25+
import store, { Store, StoreLike } from "./store";
2626
import kernelManager from "./kernel-manager";
2727
import services from "./services";
2828
import * as commands from "./commands";
@@ -85,67 +85,72 @@ export function activate() {
8585
})
8686
);
8787
store.subscriptions.add(
88-
atom.commands.add("atom-text-editor:not([mini])", {
89-
"hydrogen:run": () => run(),
90-
"hydrogen:run-all": () => runAll(),
91-
"hydrogen:run-all-above": () => runAllAbove(),
92-
"hydrogen:run-and-move-down": () => run(true),
93-
"hydrogen:run-cell": () => runCell(),
94-
"hydrogen:run-cell-and-move-down": () => runCell(true),
95-
"hydrogen:toggle-watches": () => atom.workspace.toggle(WATCHES_URI),
96-
"hydrogen:toggle-output-area": () => commands.toggleOutputMode(),
97-
"hydrogen:toggle-kernel-monitor": async () => {
98-
const lastItem = atom.workspace.getActivePaneItem();
99-
const lastPane = atom.workspace.paneForItem(lastItem);
100-
await atom.workspace.toggle(KERNEL_MONITOR_URI);
101-
if (lastPane) {
102-
lastPane.activate();
103-
}
104-
},
105-
"hydrogen:start-local-kernel": () => startZMQKernel(),
106-
"hydrogen:connect-to-remote-kernel": () => connectToWSKernel(),
107-
"hydrogen:connect-to-existing-kernel": () => connectToExistingKernel(),
108-
"hydrogen:add-watch": () => {
109-
if (store.kernel) {
110-
store.kernel.watchesStore.addWatchFromEditor(store.editor);
111-
openOrShowDock(WATCHES_URI);
112-
}
113-
},
114-
"hydrogen:remove-watch": () => {
115-
if (store.kernel) {
116-
store.kernel.watchesStore.removeWatch();
117-
openOrShowDock(WATCHES_URI);
118-
}
119-
},
120-
"hydrogen:update-kernels": () => kernelManager.updateKernelSpecs(),
121-
"hydrogen:toggle-inspector": () => commands.toggleInspector(store),
122-
"hydrogen:interrupt-kernel": () =>
123-
handleKernelCommand(
124-
{
125-
command: "interrupt-kernel",
126-
},
127-
store
128-
),
129-
"hydrogen:restart-kernel": () =>
130-
handleKernelCommand(
131-
{
132-
command: "restart-kernel",
133-
},
134-
store
135-
),
136-
"hydrogen:shutdown-kernel": () =>
137-
handleKernelCommand(
138-
{
139-
command: "shutdown-kernel",
140-
},
141-
store
142-
),
143-
"hydrogen:clear-result": () => result.clearResult(store),
144-
"hydrogen:export-notebook": () => exportNotebook(),
145-
"hydrogen:fold-current-cell": () => foldCurrentCell(),
146-
"hydrogen:fold-all-but-current-cell": () => foldAllButCurrentCell(),
147-
"hydrogen:clear-results": () => result.clearResults(store),
148-
})
88+
atom.commands.add<"atom-text-editor:not([mini])">(
89+
"atom-text-editor:not([mini])",
90+
{
91+
"hydrogen:run": () => run(),
92+
"hydrogen:run-all": () => runAll(),
93+
"hydrogen:run-all-above": () => runAllAbove(),
94+
"hydrogen:run-and-move-down": () => run(true),
95+
"hydrogen:run-cell": () => runCell(),
96+
"hydrogen:run-cell-and-move-down": () => runCell(true),
97+
"hydrogen:toggle-watches": () => atom.workspace.toggle(WATCHES_URI),
98+
"hydrogen:toggle-output-area": () => commands.toggleOutputMode(),
99+
"hydrogen:toggle-kernel-monitor": async () => {
100+
const lastItem = atom.workspace.getActivePaneItem();
101+
const lastPane = atom.workspace.paneForItem(lastItem);
102+
await atom.workspace.toggle(KERNEL_MONITOR_URI);
103+
if (lastPane) {
104+
lastPane.activate();
105+
}
106+
},
107+
"hydrogen:start-local-kernel": () => startZMQKernel(),
108+
"hydrogen:connect-to-remote-kernel": () => connectToWSKernel(),
109+
"hydrogen:connect-to-existing-kernel": () => connectToExistingKernel(),
110+
"hydrogen:add-watch": () => {
111+
if (store.kernel) {
112+
store.kernel.watchesStore.addWatchFromEditor(store.editor);
113+
openOrShowDock(WATCHES_URI);
114+
}
115+
},
116+
"hydrogen:remove-watch": () => {
117+
if (store.kernel) {
118+
store.kernel.watchesStore.removeWatch();
119+
openOrShowDock(WATCHES_URI);
120+
}
121+
},
122+
"hydrogen:update-kernels": async () => {
123+
await kernelManager.updateKernelSpecs();
124+
},
125+
"hydrogen:toggle-inspector": () => commands.toggleInspector(store),
126+
"hydrogen:interrupt-kernel": () =>
127+
handleKernelCommand(
128+
{
129+
command: "interrupt-kernel",
130+
},
131+
store
132+
),
133+
"hydrogen:restart-kernel": () =>
134+
handleKernelCommand(
135+
{
136+
command: "restart-kernel",
137+
},
138+
store
139+
),
140+
"hydrogen:shutdown-kernel": () =>
141+
handleKernelCommand(
142+
{
143+
command: "shutdown-kernel",
144+
},
145+
store
146+
),
147+
"hydrogen:clear-result": () => result.clearResult(store),
148+
"hydrogen:export-notebook": () => exportNotebook(),
149+
"hydrogen:fold-current-cell": () => foldCurrentCell(),
150+
"hydrogen:fold-all-but-current-cell": () => foldAllButCurrentCell(),
151+
"hydrogen:clear-results": () => result.clearResults(store),
152+
}
153+
)
149154
);
150155
store.subscriptions.add(
151156
atom.commands.add("atom-workspace", {
@@ -211,6 +216,9 @@ export function activate() {
211216

212217
case KERNEL_MONITOR_URI:
213218
return new KernelMonitorPane(store);
219+
default: {
220+
return;
221+
}
214222
}
215223
})
216224
);
@@ -278,23 +286,19 @@ function connectToExistingKernel() {
278286
existingKernelPicker.toggle();
279287
}
280288

289+
interface KernelCommand {
290+
command: string;
291+
payload?: KernelspecMetadata | null | undefined;
292+
}
293+
281294
function handleKernelCommand(
282-
{
283-
command,
284-
payload,
285-
}: {
286-
command: string;
287-
payload: KernelspecMetadata | null | undefined;
288-
},
289-
{
290-
kernel,
291-
markers,
292-
}: {
293-
kernel: Kernel | null | undefined;
294-
markers: MarkerStore | null | undefined;
295-
}
295+
{ command, payload }: KernelCommand, // TODO payload is not used!
296+
{ kernel, markers }: Store | StoreLike
296297
) {
297-
log("handleKernelCommand:", arguments);
298+
log("handleKernelCommand:", [
299+
{ command, payload },
300+
{ kernel, markers },
301+
]);
298302

299303
if (!kernel) {
300304
const message = "No running kernel for grammar or editor found";
@@ -403,7 +407,7 @@ function _runAll(
403407
const row = codeManager.escapeBlankRows(
404408
editor,
405409
start.row,
406-
end.row == editor.getLastBufferRow() ? end.row : end.row - 1
410+
codeManager.getEscapeBlankRowsEndRow(editor, end)
407411
);
408412
const cellType = codeManager.getMetadataForRow(editor, start);
409413
const code =
@@ -457,7 +461,7 @@ function _runAllAbove(editor: TextEditor, kernel: Kernel) {
457461
const row = codeManager.escapeBlankRows(
458462
editor,
459463
start.row,
460-
end.row == editor.getLastBufferRow() ? end.row : end.row - 1
464+
codeManager.getEscapeBlankRowsEndRow(editor, end)
461465
);
462466
const cellType = codeManager.getMetadataForRow(editor, start);
463467

@@ -496,7 +500,7 @@ function runCell(moveDown: boolean = false) {
496500
const row = codeManager.escapeBlankRows(
497501
editor,
498502
start.row,
499-
end.row == editor.getLastBufferRow() ? end.row : end.row - 1
503+
codeManager.getEscapeBlankRowsEndRow(editor, end)
500504
);
501505
const cellType = codeManager.getMetadataForRow(editor, start);
502506
const code =

lib/store/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,8 @@ const store = new Store();
331331
export default store; // For debugging
332332

333333
window.hydrogen_store = store;
334+
335+
export interface StoreLike {
336+
kernel?: Kernel | null | undefined;
337+
markers?: MarkerStore | null | undefined;
338+
}

lib/utils.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ export function msgSpecV4toV5(message: Message) {
115115
message.content.text = message.content.data;
116116
}
117117
break;
118+
default: {
119+
// no conversion needed
120+
}
118121
}
119-
120122
return message;
121123
}
122124
const markupGrammars = new Set([
@@ -317,3 +319,11 @@ export function setPreviouslyFocusedElement(obj: {
317319
obj.previouslyFocusedElement = activeElement;
318320
}
319321
}
322+
323+
/** Make the properties of a type Writable */
324+
export type Writeable<T> = { -readonly [P in keyof T]: T[P] };
325+
326+
/** Make the properties of aexport type Writable recursively */
327+
export type DeepWriteable<T> = {
328+
-readonly [P in keyof T]: DeepWriteable<T[P]>;
329+
};

0 commit comments

Comments
 (0)