Syntax Highlighting For Python Docstrings in Neovim Treesitter - python

I'm using Treesitter with Neovim v0.8.2 and Python. With a default configuration of those 3, python docstrings are highlighted as strings, and I'd like to highlight them as comments.
I've tried creating a ~/.config/nvim/after/syntax/python.vim file with
syn region Comment start=/"""/ end=/"""/
and I expected """<things here>""" to be highlighted as comments.
I'm guessing this is because treesitter is disabling syntax highlighting, so on that note has anyone been able to add custom highlighting rules to Treesitter or after it?

Related

Custom highlighting for string literals in Jupyter

Is there prior art to either
(Simple?) Add a new cell type to Jupyter notebook and apply custom syntax highlighting to all string literals in cells of that type while otherwise treating these cells exactly like regular Python cells. I'd be fine to do the parsing myself, but I'm not sure how one would split the highlighting work between Jupyter and my custom highlighter. I'd be happy to dig around the Jupyter source code to make this work, i.e., I'd accept this not having the form of a regular Jupyter extension in the end.
(Hard?) Apply custom syntax highlighting to all strings with a s prefix (e.g. s'myString') and before executing the cell remove the s while keeping any additional valid python string prefixes like f or u or r)
?
If not, could someone sketch the steps required to make this work? I don't know much JS so I'd prefer Python solutions, but I'm happy to learn the required JS if needed.

How to disable squiggly lines underline in VSC 1.49.2?

Please help, it's driving me nuts. I tried following another older stack overflow post and it didn't work. I just declare a new variable and I already get squiggly line - what an annoying feature. Using python by the way on VSC.
Something like this:
My json.settings look like this:
Based on the information you provided, I reproduced the problem.
The reason is that Python's code analysis tool Pylint detects that the variable "words_to_remove" is not used, so it displays "Pylint(Unused variable)".
Solution:
Complete the code. for example:
Turn off the prompt of Pylint.
1). Close the "Unused variable" prompt of pylint:
Please use "python.linting.pylintArgs": ["--disable=W0612"], in settings.json:
2). Close Pylint.
Use "python.linting.enabled":false , in settings.json.
In addition, Pylint is an excellent Python code analysis tool. It will provide good suggestions on the format and syntax of the code we edit and it does not affect the execution of the code.
Therefore, if you want to turn off Pylint related prompts to remove the wavy lines, it is better to set it off after the code can be executed.

Python region folding syntax

I know python does not formally support any built in regional code folding syntax. I do know that a few syntaxes exist out in the wild with conventions tied to specific editors and particular comment pairs.
I know of the python source code folding syntax recognized by VS Code which uses the #region keyword
#region MY_CODE_REGION
...
#endregion
I came across another I don't recognize taken from some source over at
https://github.com/gitpython-developers/GitPython/blob/master/git/objects/tree.py#L96-L104
which used the following syntax
#{ MY_CODE_REGION
#} END MY_CODE_REGION
What other editor syntaxes exist for python? Is there a pending PEP open on the subject?
I think #%% is another option for the syntax you are looking for. This is from the Spyder editor as the method for separating sections of code (and it is listed there as the "standard cell divider" syntax)
https://docs.spyder-ide.org/3/editor.html#defining-code-cells
there are also ways to convert this to and from a jupyter notebook
https://stackoverflow.com/a/55920098/13665895

IPython change input cell syntax highlighting logic for entire session

You can use extensions or display helpers in IPython to make whatever syntax highlighting you'd like on output cells.
For some special cell magics, like %%javascript you can also see the input cell itself is rendered with that language's natural syntax highlighting.
How can you cause every input cell to be displayed with some chosen, non-Python syntax highlighting (regardless of any magics used on a cell, regardless of whether the cell embodies Python code, some other language).
In my case I am working with a custom-made cell magic for a proprietary language. The %%javascript syntax highlighting works well for this language, but if I have my own %%proprietarylang magic function, I obviously can't use %%javascript to help me with how the cell is displayed.
Is there a setting I can enable when I launch the notebook, or some property of the ipython object itself that can be programmatically set inside of my magic function, which will cause the same display logic to happen as if it was %%javascript.
I know that general-purpose on-the-fly syntax highlighting is not supported by the notebook currently. I'm asking specifically about how to make use of pre-existing syntax highlighting effects, such as that of %%javascript.
I've seen some documentation referring to IPython.config.cell_magic_highlight but this does not seem to exist anymore. Is there a standard replacement for it?
To replace IPython.config.cell_magic_highlight, you can use something like
import IPython
js = "IPython.CodeCell.config_defaults.highlight_modes['magic_fortran'] = {'reg':[/^%%fortran/]};"
IPython.core.display.display_javascript(js, raw=True)
so cells which begin with %%fortran will be syntax-highlighted like FORTRAN. (However, they will still be evaluated as python if you do only this.)
For recent IPython version, the selected answer no longer works. The 'config_default' property was renamed options_default (Ipython 6.0.0).
The following works:
import IPython
js = "IPython.CodeCell.options_default.highlight_modes['magic_fortran'] = {'reg':[/^%%fortran/]};"
IPython.core.display.display_javascript(js, raw=True)

How to change default markdown's inline highlight style?

I'm using pelican in Python to generate the static pages. It is fine to use markdown with Pygment to highlight a code block, however when it comes to the inline highlight, there is a significant difference.
Please check my page. The page is in a dark background, but the inline highlight with the back ticks `` are white background.
The markdown code is
To clearify what exactly the command returns,
one can use `pack('>i', -2500).encode('hex')`{.python}.
Or I have a more complex solution like, `''.join(r'\x%02X'
% ord(ch) for ch in src)`{.python}.
So the question here is how to define the inline highlight style?
I tried to altered the pygments default.css file in the pelican theme, but it didn't work.
I found some related links:
Inline code syntax highlighting in GitHub markdown?
Inline code highlighting in reStructuredText
Pelican uses Python-Markdown with the CodeHilite Extension to pass code blocks to Pygments. Note that code blocks do not include inline code snippets. In fact I am not aware of any Markdown parser which supports highlighting inline code. Generally, inline code snippets are too short to effectively highlight and almost always too short to take advantage of Pygments' language guessing algorithm.
I see you have attempted to inform Markdown of the language using the Attribute List Extension. However, all that does is add a class to the HTML <code> tag that wraps the code snippet. As the CodeHilite Extension only works on blocks, it never even looks at it, and certainly never passes it to Pygments.
There are a few different options you have available (each increasing in the amount of work required):
You can write your own CSS which changes the background and text color so that they match the blocks. You won't get syntax highlighting, but the inline code snippets will at least match your theme. No need to define the language here.
You could use a JavaScript code highlighting library to highlight all the code snippets based on the language you define with the Attribute List Extension. However, this may require you to redefine the CSS for the JS library so it matches the output of Pygments.
You could write your own Python-Markdown extension (or perhaps fork CodeHilite) which also highlights inline code snippets in addition to blocks.
Personally, I would recommend (1) above. In fact, looking at your page, is appears that the code blocks have the following defined:
.highlight pre, .highlighttable pre {
background: #272822;
color: #f8f8f2;
}
That is, a <pre> element with a parent assigned the "highlight" class (or the "highlighttable" class) will be displayed with those colors. As you want the same colors assigned to inline code, add the following rule:
code {
background: #272822;
color: #f8f8f2;
}
Just be sure that you define that after the styles defined by the Pelican theme (which appears to be in http://vfxware.com/theme/css/bootstrap.slate.min.css).
Sure, you could alter the existing CSS, but then if you later decide to update to a newer version of the theme or change themes entirely, you might have to redefine your own rules. Therefore, I like to define my own set of overrides in a separate file which I always make sure is the last one loaded.
I've been bitten by this all too often. My problem was that I was using an older template's old pygment.css file while upgrading the Pygment modules (CSS didn't catch up with the Pygment plugin.
In my case, the older Pygment CSS was using an HTML tag class highlight when it should be using the newer codehilite class name that newer Pygment code is now using. (Thanks to Firefox->Windows->Web Developer->Toggle menu option for pointing out this "breakage".
Replacing the pygments.css CSS-style file does the trick in restoring this much needed code syntax highlighting capability.
To regenerate the pygments.css file, execute:
pygmentize -S default -f html -a .codehilite > pygments.css
or for a darker theme, use Pygmentize monokai style, as listed in pygmentize -L:
pygmentize -S monokai -f html -a .codehilite > pygments.css
Again, problem was that Pygments changed its HTTP tag class to a newer codehilite from an older highlight.

Categories