Skip to content

Commit

Permalink
Fix execution in console in terminal interaction mode (#16348)
Browse files Browse the repository at this point in the history
* Add an integration test for console in terminal mode

* Do not prevent default on enter in terminal interaction mode

* Integrity
  • Loading branch information
krassowski authored May 22, 2024
1 parent 44b82c8 commit f1a006d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 38 deletions.
52 changes: 41 additions & 11 deletions galata/test/jupyterlab/console.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import { expect, test } from '@jupyterlab/galata';
import {
expect,
galata,
IJupyterLabPageFixture,
test
} from '@jupyterlab/galata';

const CELL_EDITOR_SELECTOR = '.jp-InputArea-editor';
const CODE_MIRROR_CURSOR = '.cm-cursor';
const EXECUTED_CELL = '[aria-label="Code Cell Content with Output"]';

test.describe('Console', () => {
test.beforeEach(async ({ page }) => {
await page.menu.clickMenuItem('File>New>Console');
async function setupConsole(page: IJupyterLabPageFixture) {
await page.menu.clickMenuItem('File>New>Console');

await page.click('button:has-text("Select")');
await page.click('button:has-text("Select")');

await page.locator('[aria-label="Code Cell Content"]').waitFor();
await page.locator('text=| Idle').waitFor();
});
await page.locator('[aria-label="Code Cell Content"]').waitFor();
await page.locator('text=| Idle').waitFor();
}

test.describe('Console', () => {
test.beforeEach(async ({ page }) => setupConsole(page));

test('Executed cells should become read-only', async ({ page }) => {
await page.keyboard.type('2 + 2');
await page.keyboard.press('Shift+Enter');

const executedCell = page.locator(
'[aria-label="Code Cell Content with Output"]'
);
const executedCell = page.locator(EXECUTED_CELL);
await executedCell.waitFor();

const cellEditor = executedCell.locator(CELL_EDITOR_SELECTOR);
Expand All @@ -42,3 +48,27 @@ test.describe('Console', () => {
expect(await cellEditor.innerText()).toBe('2 + 2');
});
});

test.describe('Console (terminal mode)', () => {
test.use({
mockSettings: {
...galata.DEFAULT_SETTINGS,
'@jupyterlab/console-extension:tracker': {
...galata.DEFAULT_SETTINGS['@jupyterlab/console-extension:tracker'],
interactionMode: 'terminal'
}
}
});

test.beforeEach(async ({ page }) => setupConsole(page));

test('Cells get executed with Enter ', async ({ page }) => {
await page.keyboard.type('2**22');
await page.keyboard.press('Enter');

const executedCell = page.locator(EXECUTED_CELL);
await executedCell.waitFor();

await expect(executedCell).toContainText('4194304');
});
});
9 changes: 9 additions & 0 deletions packages/codemirror/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import {
*/
const CODE_RUNNER_SELECTOR = '[data-jp-code-runner]';

/**
* Selector for a widget that can run code in terminal mode.
*/
const TERMINAL_CODE_RUNNER_SELECTOR = '[data-jp-interaction-mode="terminal"]';

/**
* Selector for a widget that can open a tooltip.
*/
Expand Down Expand Up @@ -77,6 +82,10 @@ export namespace StateCommands {
// do not prevent default to allow completer `enter` action
return false;
}
if (target.dom.closest(TERMINAL_CODE_RUNNER_SELECTOR)) {
// do not prevent default to allow for the cell to run
return false;
}

const arg = { state: target.state, dispatch: target.dispatch };
return insertNewlineAndIndent(arg);
Expand Down
2 changes: 0 additions & 2 deletions packages/console/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
"watch": "tsc -b --watch"
},
"dependencies": {
"@codemirror/state": "^6.4.1",
"@codemirror/view": "^6.26.3",
"@jupyter/ydoc": "^2.0.1",
"@jupyterlab/apputils": "^4.3.0",
"@jupyterlab/cells": "^4.2.0",
Expand Down
23 changes: 0 additions & 23 deletions packages/console/src/widget.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import { Prec } from '@codemirror/state';
import { EditorView } from '@codemirror/view';
import { createStandaloneCell, ISharedRawCell } from '@jupyter/ydoc';
import { DOMUtils, ISessionContext } from '@jupyterlab/apputils';
import {
Expand Down Expand Up @@ -94,11 +92,6 @@ const JUPYTER_CELL_MIME = 'application/vnd.jupyter.cells';
* The data attribute added to a widget that can undo.
*/
const UNDOER = 'jpUndoer';
/**
* The data attribute Whether the console interaction mimics the notebook
* or terminal keyboard shortcuts.
*/
const INTERACTION_MODE = 'jpInteractionMode';

/**
* A widget containing a Jupyter console.
Expand Down Expand Up @@ -788,33 +781,17 @@ export class CodeConsole extends Widget {
* Create the options used to initialize a code cell widget.
*/
private _createCodeCellOptions(): CodeCell.IOptions {
const { node } = this;
const contentFactory = this.contentFactory;
const modelFactory = this.modelFactory;
const model = modelFactory.createCodeCell({});
const rendermime = this.rendermime;
const editorConfig = this.editorConfig;

// Suppress the default "Enter" key handling.
const onKeyDown = EditorView.domEventHandlers({
keydown: (event: KeyboardEvent, view: EditorView) => {
if (
event.keyCode === 13 &&
node.dataset[INTERACTION_MODE] === 'terminal'
) {
event.preventDefault();
return true;
}
return false;
}
});

return {
model,
rendermime,
contentFactory,
editorConfig,
editorExtensions: [Prec.high(onKeyDown)],
placeholder: false,
translator: this._translator
};
Expand Down
2 changes: 0 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2670,8 +2670,6 @@ __metadata:
version: 0.0.0-use.local
resolution: "@jupyterlab/console@workspace:packages/console"
dependencies:
"@codemirror/state": ^6.4.1
"@codemirror/view": ^6.26.3
"@jupyter/ydoc": ^2.0.1
"@jupyterlab/apputils": ^4.3.0
"@jupyterlab/cells": ^4.2.0
Expand Down

0 comments on commit f1a006d

Please sign in to comment.