Override Default keybindings from jupyterlab extensions

I am building an extension which provides code autocompletion using some LLM APIs when user presses Ctrl + L. I did the following

app.commands.addKeyBinding({
      command: 'command-registered',
      keys: ['Ctrl L'],
      selector: '.jp-Notebook',
      args: {
        api: 'getNotebookCellCompletion',
        completionType: 'some-type'
      },
      preventDefault: true
    });

I have explicitly mentioned preventDefault: True but still when I install and use it while typing some code on pressing Ctrl + L rather than autocompletion it just selects the entire line.

Looks like some default keybinding for the code editor. How to disable it ?? Autocompletion works if i press ESC and go to command mode. How to make it work when in edit mode.

NOTE: Above syntax worked in version 3.x and gave desired results, the above is issue with version 4.x.

1 Like

Two potential problems:

  • the keybinding may be defined by CodeMirror6 defaults rather by JupyterLab shortcuts system; in that case you should open a PR moving it to JupyterLab shortcut, or
  • if the keybinding is defined on JupyterLab level, your selector might be less specific than that of the keybinding meaning that the other keybinding takes precedence

Skimming the code it seems like its the former. Can you open a PR mirroring Upgrade lumino to v2024.3.25, improve CodeMirror shortcuts handling by krassowski · Pull Request #16078 · jupyterlab/jupyterlab · GitHub?

Thankyou for your response @krassowski .

I think I didn’t really understand that. I saw here that codemirror uses Ctrl+L for line selection which will be default action but given that I am using preventDefault=true shouldn’t it override that ?

Also I think Ctrl+L was also used in prior versions of Codemirror for selecting line but back then the preventDefault trick did worked well.

Is there any work around or a way to directly disable it ?

Thanks

JupyterLab 3.x used CodeMirror 5, completely different editor implementation. CodeMirror 6 was rewritten from scratch.

No, I think there is no other good solution. If you need help with the PR let me know.

1 Like

Okay, Thanks

Can you send some resources which will help me to get started with it. I haven’t worked on it ever before :sweat_smile: .

Thanks

We have a contributing guide here: Contribute — JupyterLab 4.4.0a0 documentation

You will need to modify these files:

  • packages/codemirror-extension/schema/plugin.json
  • packages/codemirror-extension/src/commands.ts
  • packages/codemirror/src/extension.ts

Have a look at hoe these were modified in Upgrade lumino to v2024.3.25, improve CodeMirror shortcuts handling by krassowski · Pull Request #16078 · jupyterlab/jupyterlab · GitHub

1 Like