kpeeters / cadabra2

A field-theory motivated approach to computer algebra.
https://cadabra.science/
GNU General Public License v3.0
215 stars 37 forks source link

Small CodeMirror change #215

Closed fjebaker closed 3 years ago

fjebaker commented 3 years ago

Summary

A quick bugfix for comment highlighting in the Jupyter kernel, and operator highlights for ? and ?? wildcards.

Details

The old CodeMirror mode for Cadabra2 doesn't highlight uses of # correctly.

I'm a little confused as to how comments are used in Cadabra2, given the # operator for indices, however, this commit ammends the CodeMirror lexer to define a comment as any string obeying ^\s*#, in accordance to

Any line starting with a "#" sign is considered to be a comment (even when it appears within a multi-line expression). Comments are always ignored completely (they do not end up in the expression tree.

from the manpage on input formats.

That is to say

# this is a valid comment 
    # this is a valid comment

\lorem{} # this is not a valid comment (cadabra2 parse error)
print("Hello") # this is also not a valid comment (although technically python valid)

and common uses like

\dg{#}::LaTeXForm("\delta g").
{a, b, c, u#}::Indices().

will highlight correctly with # as an operator, with comments written on seperate lines:

# This is my comment...
\dg{#}::LaTeXForm("\delta g").
# ... and here's another.
{a, b, c, u# }::Indices().
kpeeters commented 3 years ago

The way '#' is handled does not exactly match that description. It's very simple: inside a line containing a property declaration (that is, a :: symbol) or inside a line containing an expression declaration (that is, a := symbol), the # symbol acts as a wildcard (these lines are wrapped as strings, more or less, and then fed to Cadabra).

In all other situations, # behaves as in Python.

fjebaker commented 3 years ago

Aha!

I'll push a change for that shortly to my fork -- thanks for the clarification! 🙂

fjebaker commented 3 years ago

Okay, so there's still one edge-case caveat, namely comments which contain the tokens :: or :=. To illustrate this, consider the snippet:

Screenshot 2020-11-24 at 20 38 36

I can't think of a simple way to differentiate :: and := being used in code or in comment form (using just regex expressions).

fjebaker commented 3 years ago

By the way, are you still experiencing that MIME-type error?

kpeeters commented 3 years ago

Yes, I still have that mime type mismatch error, so I do not see any of the syntax colouring. My suspicion is that my jupyter installation (from pip) sends the codemirror files which come with it (and are stored somewhere below ~/.local) correctly, but does not attach the right mime content type if the file is coming from /usr/local. That's clearly a jupyter problem.