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

add RST highlighting for command line / shells (also fixes #16858) #17789

Merged
merged 2 commits into from
Apr 21, 2021

Conversation

a-mr
Copy link
Contributor

@a-mr a-mr commented Apr 19, 2021

A very generic cmdline tokenizer is implemented in highlite.nim. It does not support programming constructs or escaping. But it can be used meaningfully for Unix shells, Powershell, even Windows cmd (mostly).

Pseudo-language cmd and role :cmd: is introduced for this highlighting. Here is code-block:: cmd in black theme:

image

Also added roles :program: and :option: (borrowed from Sphinx) to input them separately:


See `testament`:program: or `testament`:cmd:.

...

even with `--assertions:off`:option:.

image

image

RST option list style/color is tweaked respectively:
image

Also a new RST node rnCodeFragment is used to revive highlighting for :tok: role (ref #16858 ):

image

cc @narimiran

@a-mr a-mr force-pushed the rst-shell-cmdline-highlighting branch from 7d774a4 to 6f3e1ff Compare April 20, 2021 16:39
============
Contributing
============

.. default-role:: code
.. include:: rstcommon.rst
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that include file we have .. default-role:: nim. Do we need .. default-role:: code above it too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For nim rst2html we don't need .. default-role:: code. This is for compatibility with GitHub. Github doesn't seem to include files and it does not highlight Nim even if inserted directly to file (yet?). So effectively it sees only .. default-role:: code, which is satisfactory.

This trick was proposed by @timotheecour , see discussion around #17585 (comment)


Not all the tests follow the convention here, feel free to change the ones
that don't. Always leave the code cleaner than you found it.

Stdlib
------

Each stdlib module (anything under `lib/`, e.g. `lib/pure/os.nim`) should
preferably have a corresponding separate test file, e.g. `tests/stdlib/tos.nim`.
Each stdlib module (anything under ``lib/``, e.g. ``lib/pure/os.nim``) should
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these single-backtick to double-backtick changes needed (at some (not all) places)?

Copy link
Contributor Author

@a-mr a-mr Apr 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rule was introduced in #17585 into contributing.rst.

It's simple in essence: we use double backticks where we don't want syntax highlighting. In the case of single backticks / and . would be highlighted as Nim operators. (Interestingly, Sphinx has a dedicated :file: role for files.)

Currently I selected the following uniform scheme:

  • files/dir names are not highlighted in :cmd: role
  • and they are not highlighted when using ``double backticks``

Of course this is still up to discussion, we can change our mind and remove/modify the rule, please share your thoughts...

@narimiran narimiran merged commit 8f79bc5 into nim-lang:devel Apr 21, 2021
TokenClass* = enum
gtEof, gtNone, gtWhitespace, gtDecNumber, gtBinNumber, gtHexNumber,
gtOctNumber, gtFloatNumber, gtIdentifier, gtKeyword, gtStringLit,
gtLongStringLit, gtCharLit, gtEscapeSequence, # escape sequence like \xff
gtOperator, gtPunctuation, gtComment, gtLongComment, gtRegularExpression,
gtTagStart, gtTagEnd, gtKey, gtValue, gtRawData, gtAssembler,
gtPreprocessor, gtDirective, gtCommand, gtRule, gtHyperlink, gtLabel,
gtReference, gtOther
gtReference, gtProgram, gtOption, gtOther
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about using same approach as #15775 for these to simplify code:

  TokenClass* = enum
    gtEof = "Eof"
    ...

(the raw "gtEof" can always be had with enumutils.symbolName in the rare case it's needed)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timotheecour yes, that would be a better style.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants