astoff / code-cells.el

Emacs utilities for code split into cells, including Jupyter notebooks
GNU General Public License v3.0
180 stars 11 forks source link

Add support for leading whitespace in code cells comments #21

Closed spacegoing closed 10 months ago

spacegoing commented 1 year ago

Dear astoff,

I hope this message finds you well. I'm writing to propose a small yet significant modification to the code-cells-boundary-regexp in the code-cells mode.

Motivation: While using the code-cells mode for Python code, I noticed that the current implementation only recognizes cell boundary comments if they do not have any leading spaces. Specifically, it will catch a line such as # current implementation will catch this line, but not a line with leading spaces like:

#* this line is recognized
def func():
    #* but not this line

This limitation can affect the usability of code-cells mode, especially when we structure Python code with indentation which is a common practice.

Modification: I suggest a modification to the code-cells-boundary-regexp definition to handle leading whitespaces. The change involves using the (syntax whitespace) function before (syntax comment-start), allowing for any number of whitespace characters before the comment start symbol.

Here's the proposed change:

(defcustom code-cells-boundary-regexp
  (rx (* (syntax whitespace)) 
      (+ (syntax comment-start))
      (or (seq (* (syntax whitespace)) "%" (group-n 1 (+ "%")))
          (group-n 1 (+ "*"))
          (seq " In[" (* (any space digit)) "]:")))
  "Regular expression specifying cell boundaries.
It should match at the beginning of a line.  The length of the
first capture determines the outline level."
  :type 'regexp)

Results: With this modification, code-cells now recognizes cell boundary comments with leading spaces, enhancing its compatibility with indented Python code.

For instance, previously, this block of code would not have its comments recognized:

def func():
    #* Previous implementation would miss this comment
    pass

With the modification, the above comment is successfully recognized as a cell boundary comment, as demonstrated in this example:

def func():
    #* Modified implementation successfully catches this comment
    pass

I hope this change will enhance the functionality of code-cells mode. I'm eager to hear your thoughts or any further suggestions you may have.

egr95 commented 11 months ago

@spacegoing I'm curious why you closed this as complete? As far as I can see this wasn't added into the package yet, but I agree it's a good change.

astoff commented 10 months ago

I think this modification is best left as a customization, which hopefully is easy enough to achieve. The problem with a more permissive regexp is that one could get false positives, which are very annoying. So I'll close this ticket.