IJulia.jl icon indicating copy to clipboard operation
IJulia.jl copied to clipboard

Special syntax to wrap cell in a string macro

Open MasonProtter opened this issue 5 years ago • 2 comments

I think that given that we don't have a way to hook into custom repl modes other than the ones hard coded into IJulia, it could be really nice to have a special syntax for wrapping the contents of a cell in a string macro. The idea would be that the string macros used in this way are analogous to custom repl modes, and the special syntax would allow us to use them in such a way that would not interfere with syntax highlighting and would require less typing.

For instance, if someone did using PyCall, they have access to the py_str macro so they could just have a codeblock that looks like

%% py
import numpy as np

def sinpi(x):
    return np.sin(np.pi * x)

sinpi(1)

would be translated into

py"""
import numpy as np

def sinpi(x):
    return np.sin(np.pi * x)

sinpi(1)
"""

Alternatively to the %% syntax, maybe we could just handle a comment on the first line specially, i.e. #mode mymode would use the mymode_str macro? I'm not sure what the best syntax to use would be.

MasonProtter avatar Apr 23 '20 17:04 MasonProtter

I'm not sure why %% py is better than py""" .... """ other than saving a couple characters? The latter has the advantage that it won't be formatted as Julia code.

stevengj avatar Apr 23 '20 19:04 stevengj

The latter has the advantage that it won't be formatted as Julia code.

It also has the disadvantage that it won't be formatted as Julia code. Maybe PyCall is a poor example because Python's indentation style could cause headaches.

There are cases where one might want to use a string macro for a code block but still mostly adhere to Julia's indentation rules. One example being https://github.com/swadey/LispSyntax.jl, or something like PyCall but for Matlab. Another example would be a side-project I'm working on to support Bra-Ket notation in julia with a string macro.

MasonProtter avatar Apr 23 '20 19:04 MasonProtter