nteract / hydrogen

:atom: Run code interactively, inspect data, and plot. All the power of Jupyter kernels, inside your favorite text editor.
https://nteract.gitbooks.io/hydrogen/
MIT License
3.92k stars 334 forks source link

TypeError when using hydrogen with pulsar #2163

Open ttxtea opened 1 year ago

ttxtea commented 1 year ago

Description

When running a cell the TypeError: editor.languageMode.rowRangeForCodeFoldAtBufferRow is not a function, see below happens.

Prerequisites

Versions

Atom: 1.101.0-beta x64 Electron: 12.2.3 OS: Ubuntu 22.04.1 Thrown From: Hydrogen package 2.16.3

Stack Trace

Uncaught TypeError: editor.languageMode.rowRangeForCodeFoldAtBufferRow is not a function

At $HOME/.pulsar/packages/Hydrogen/dist/utils.js:177

TypeError: editor.languageMode.rowRangeForCodeFoldAtBufferRow is not a function
    at Object.rowRangeForCodeFoldAtBufferRow (/packages/Hydrogen/dist/utils.js:177:36)
    at Object.findCodeBlock (/packages/Hydrogen/dist/code-manager.js:357:31)
    at run (/packages/Hydrogen/dist/main.js:258:35)
    at /packages/Hydrogen/dist/main.js:80:31)
    at CommandRegistry.handleCommandEvent (/app.asar/src/command-registry.js:405:43)
    at KeymapManager.module.exports.KeymapManager.dispatchCommandEvent (/app.asar/node_modules/atom-keymap/lib/keymap-manager.js:617:16)
    at KeymapManager.module.exports.KeymapManager.handleKeyboardEvent (/app.asar/node_modules/atom-keymap/lib/keymap-manager.js:408:22)
    at WindowEventHandler.handleDocumentKeyEvent (/app.asar/src/window-event-handler.js:153:34)

Commands

     -0:05.8.0 hydrogen:run (input.hidden-input)
     -0:05.8.0 autocomplete-plus:cancel (atom-text-editor.editor.is-focused)

Non-Core Packages

Hydrogen 2.16.3 
contang0 commented 1 year ago

Same here. I'm always using the "Run cell and move down" command, but it only works if I highlight the code to be run. If I don't, I get the error @ttxtea mentions. Meanwhile, the "Run cell" command runs everything in the file, whereas it should really only run current line / block or what is highlighted.

antonscharton commented 1 year ago

Had the same issue and this bugged me a lot. I came up with a small workaround to restore original behavior of the hydrogen run command.

Add to the init.js script

atom.commands.add('atom-text-editor', 'custom:hydrogen-run', function () {
  const editor = this.getModel();
  atom.commands.dispatch(atom.views.getView(editor), 'editor:select-line');
  atom.commands.dispatch(atom.views.getView(editor), 'hydrogen:run');
  atom.commands.dispatch(atom.views.getView(editor), 'editor:move-to-end-of-line');
});

and map a key-binding to this function in the keymap, e.g.:

'atom-text-editor':
  'shift-enter': 'custom:hydrogen-run'

EDIT:

If you want it to work with (python) indent-blocks, e.g. function definitions in python, you could do the following. This would allow you to put the cursor on the first line and the code will figure out the corresponding indent block.

atom.commands.add('atom-text-editor', 'custom:hydrogen-run', function () {
  const editor = this.getModel();

  atom.commands.dispatch(atom.views.getView(editor), 'editor:select-line');
  const row = editor.getLastSelection().getBufferRange().start.row;
  var k = 1;

  while (/^\s+$/.test(editor.getTextInBufferRange([[row+k, 0], [row+k, 1]]))) {
    atom.commands.dispatch(atom.views.getView(editor), 'core:select-down');
    k = k + 1;
  }
  if (k != 1){
    atom.commands.dispatch(atom.views.getView(editor), 'editor:select-to-end-of-line');
  }

  atom.commands.dispatch(atom.views.getView(editor), 'hydrogen:run');
  atom.commands.dispatch(atom.views.getView(editor), 'editor:move-to-end-of-line');
});
contang0 commented 1 year ago

@antonscharton this worked great! The second snippet is exactly what I needed.

AJSchiller commented 10 months ago

Thanks @antonscharton ! This restores expected behaviour for me, but I still get the error pop-up everytime. Does it do that for anyone else?