ipython / ipython

Official repository for IPython itself. Other repos in the IPython organization contain things like the website, documentation builds, etc.
https://ipython.readthedocs.org
BSD 3-Clause "New" or "Revised" License
16.25k stars 4.43k forks source link

Syntax highlighting in presence of line magics #7548

Open abalkin opened 9 years ago

abalkin commented 9 years ago

IPython notebook knows how to switch language for (some) cell magic directives, but line magics and the ! directive seem to be completely ignored.

minrk commented 9 years ago

That's correct. Only cell magics trigger language switching. There isn't any mode switching for lines. Disabling Python highlighting on line magics should probably be added to the IPython codemirror mode.

abalkin commented 9 years ago

The issue is the same on the "server" side. This should probably be handled in the IPythonLexer.

abalkin commented 9 years ago

The best solution would be to allow line/cell magic objects to provide the necessary pieces such as a pygments lexer and a codemirror mode.

takluyver commented 9 years ago

Possibly, but the rendering code doesn't load the magic commands, so I'm not sure how the information would be conveyed.

abalkin commented 9 years ago

I was thinking of just providing the name of pygments lexer which in most cases will be the same as the matching cm mode, but for better interoperability the mime types are probably better. A magic object can define a mimetypes list and other IPython components may pick the best match.

Both pygments and codemirror specify mimetypes, but since those are non-standard they don't always match. For example, pygments defines bash lexer mimetypes as

    mimetypes = ['application/x-sh', 'application/x-shellscript']

but codemirror uses text/x-sh. The shell magic can specify all three and highlighters will pick the one they understand.

abalkin commented 9 years ago

Interestingly, I was only able to find one IANA-registered MIME type for a computer language - application/sql for SQL. And neither pygments nor cm uses it.

Pygments:

    mimetypes = ['text/x-sql']

Codemirror: text/x-sql, text/x-mysql, text/x-mariadb, text/x-cassandra, text/x-plsql, text/x-mssql, text/x-hive.

takluyver commented 9 years ago

Yep, but the code rendering the notebook doesn't load the magic functions, so we'd have to come up with some way to convey the highlighting information to the frontend and store it in the notebook.

abalkin commented 9 years ago

I am new to IPython development, so I would like to proceed in baby steps:

  1. Write tests for the current behavior.
  2. Implement no highlighting between the line magic operator and the end of the line.
  3. Figure out how to convey magics' language information to the lexer.
takluyver commented 9 years ago

If the current behaviour isn't desired, I'm not sure it's worth writing tests for it.

Hopefully it should be fairly easy to get no highlighting in pygments and codemirror for line magic arguments.

Getting the magics' information to the lexer is probably rather more than a baby step - that bit would require a certain amount of large scale architecture design.

aggFTW commented 8 years ago

@ellisonbg Are there plans to support syntax highlights for line magics?