Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Comment Indentation #954

Closed
AlJohri opened this issue Jan 14, 2016 · 13 comments
Closed

Block Comment Indentation #954

AlJohri opened this issue Jan 14, 2016 · 13 comments

Comments

@AlJohri
Copy link

AlJohri commented Jan 14, 2016

When trying to comment a line within a block, it seems to make more sense to indent the comment symbol as well.

For example:

if 10 + 5 == 15:
    print("Yay")
    print("Woohoo")

When commenting the first line inside the block, I expect the code to look like this:

if 10 + 5 == 15:
    # print("Yay")
    print("Woohoo")

but notebook defaults to this:

if 10 + 5 == 15:
#    print("Yay")
    print("Woohoo")

Here is a gif describing the above:

This is the default way of commenting in Sublime Text.

Is this possible to configure in jupyter notebook? Thanks!

@minrk
Copy link
Member

minrk commented Jan 14, 2016

The notebook just triggers CodeMirror's toggleComment action. How that's performed is entirely up to CodeMirror, you may want to ask over there if there's a way to influence this.

@minrk minrk added this to the not notebook milestone Jun 16, 2016
@minrk minrk closed this as completed Jun 16, 2016
@scholer
Copy link

scholer commented Sep 14, 2016

Hi @minrk , just to follow up. I can see that CodeMirror devs is saying there is no bug in CodeMirror, and

It's simply a matter of calling toggleComment with indent: true

So, how would I go about changing my notebook's behaviour such that toggleComment is called with indent=true? Is there a configuration setting, or would I have to implement it myself by altering the Jupyter Notebook source code?

@lzkelley
Copy link

lzkelley commented Apr 9, 2017

Even if the issue is as simple as providing a configurable option that is then passed to CodeMirror, it still seems like this is a relevant "issue" that should be reopened, no @minrk?
@scholer was there an issue on CodeMirror (that was then closed) about this, or just a private (e.g. e-mail conversation)?

@scholer
Copy link

scholer commented Apr 13, 2017

Hi @lzkelley , I looked at it briefly until I realized it isn't possible to configure Jupyter Notebook to pass arguments when calling CodeMirror functions from the keymap options (or at least, it wasn't obvious to me how to do so).

My understanding from a cursory review:

  • Keys are bound by a simple "Ctrl-/" : "toggleComment", entry in options.cm_config.extra_keys. The value, e.g. toggleComment is the name of a function.
  • The named function is referenced in lookupKey(...) where it is passed, as found to handle(found).

The current Notebook code doesn't seem to anticipate having to pass any arguments/options when calling lookupKey(...) or handle(found).

I see two obvious paths to solving this:

  1. Either Add support for defining arguments to be passed (e.g. pass options object with {indent: true}, in addition to the function name (toggleComment).
  2. Or create a new function that simply calls toggleComment with {indent: true}.

However, I don't have the insight into Jupyter Notebook development to know which approach is preferable.

Edit: Just saw that @juhasch has updated jupyter_contrib_nbextensions, updating the toggle_comment function to call cm.toggleComment with {indent: true/false} in a user-configurable manner.

@juhasch
Copy link
Contributor

juhasch commented Apr 13, 2017

@scholer: It looked like a simple enough addition. Glad if it helps.

@lzkelley
Copy link

@juhasch thanks for this! Can you clarify how to enable this?

I tried doing:

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
jupyter nbextension enable comment-uncomment/comment_uncomment_indent  # Fails
jupyter nbextension enable comment-uncomment/indent  # Fails
jupyter nbextension enable comment-uncomment # Fails

The errors I get look like:

Enabling notebook extension comment-uncomment/comment_uncomment_indent...
      - Validating: problems found:
        - require?  X comment-uncomment/comment_uncomment_indent

@juhasch
Copy link
Contributor

juhasch commented Jun 15, 2017

You need to do

jupyter nbextension enable comment-uncomment/main

or activate the extension using the configurator.

The installation docs can be found here.

@lautjy
Copy link

lautjy commented Nov 9, 2018

Cannot get this to work.

Installed and enable those cryptic nbextension (my love for jupyter is crumbling fast), but no cigar. Block comments still start at beginning of line.
nbconfig/notebook.json has this

{
  "load_extensions": {
    "nbextensions_configurator/config_menu/main": true,
    "contrib_nbextensions_help_item/main": true,
    "comment-uncomment/main": true
  },
  "comment_uncomment_indent": true
}

Which... to me means the proper indentation should work.

@juhasch
Copy link
Contributor

juhasch commented Dec 29, 2018

This may be related to you keyboard shortcut not working with the extension. Did you use Alt-C to toggle the comment ?

@munael
Copy link

munael commented Dec 30, 2018

@juhasch Rather, Ctrl /.

@juhasch
Copy link
Contributor

juhasch commented Dec 30, 2018

Ctrl / is a standard notebook hotkey. This is not what the extension uses. I can't say if it is possible to override the standard hotkey and use Ctrl / with the extension, as I have no US keyboard and therefore can't type Ctrl /.

Try using Alt c or changing the hotkey in the extension to Ctrl /.

@technic
Copy link

technic commented Aug 6, 2019

Hi, does this fix also works with JupyterLab?

@maximillianus
Copy link

Is there any update to this?

I am using Mac with US keyboard.
I tried with these and it does not work
Ctrl + /
Cmd + /

What works is this:
Alt + /

Seems like the either the default setting of standard hotkey can not be overridden or comment-uncomment extension only works with Alt.

I think it would be more intuitive to be able to use OS specific key with this extension to do commenting just like what common editor does.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants