CudaFormatter ignoring selection used for sublexer

Plugins for SynWrite and CudaText...
Post Reply
smartmic
Posts: 8
Joined: 16.11.2021 10:11

CudaFormatter ignoring selection used for sublexer

Post by smartmic »

Hi there,

I have created my own lexer which includes several sublexers using SynWrite (I want to use it for literate programming in noweb where the main text is LaTeX interweaved with code chunks in Python, Lua etc.). This worked fine and the result looks beautiful.

However, I have problems to use the CudaFormatter plugin with the sublexers. Say I want to format only a Python code chunk, I select this and try to run Formatter. I tried all three options: Formatter (menu), Formatter per-lexer and Formatter cross-lexer. For the last two options I have assigned "A" and "1" to Python Black formatter respectively, which I also installed as plugin. Only the last option "cross-lexer" does anything but tries to format the whole file, ignoring my current selection. But since the file header is latex text/code, it gives me this error:

Code: Select all

cuda_fmt_python_black.black.InvalidInput: Cannot parse: 1:0: %latex-header-start
ERROR: Exception in CudaText for format_1: InvalidInput: Cannot parse: 1:0: %latex-header-start
What did I wrong or what would be the best practice to use a formatter for a sublexer?

Kind regards,
Martin
main Alexey
Posts: 2265
Joined: 25.08.2021 18:15

Post by main Alexey »

It seems the CudaFormatter must be fixed-- when we have a selection of a sub-lexer block, formatter must take this block only + detect its lexer.
main Alexey
Posts: 2265
Joined: 25.08.2021 18:15

Post by main Alexey »

Plugin updated, pls test?
changed logic of 'formatter (menu)' and 'formatter (per lexer)' commands. not for 'cross-lexer' yet. does 'cross-lexer' command need a change?
main Alexey
Posts: 2265
Joined: 25.08.2021 18:15

Post by main Alexey »

About 'Python Black formats entire file'. that's because its config in install.inf has this force_all flag!

[fmt1]
caption=Python Black
method=do_format
lexers=Python
force_all=1
config=cuda_fmt_python_black.json

AFAIR it was made in purpose, Black maybe doesnt support formatting only a block.
smartmic
Posts: 8
Joined: 16.11.2021 10:11

Post by smartmic »

I tested it using the updated plugin and a Lua code chunk. This works well after I select the code chunk and hit either "formatter (menu)" or "formatter (per lexer)".

Concerning Python Black formatter, I used it on the commandline with stdin successfully. The documentation gives code input alternatives to the entire file. Not sure if this helps or can be implemented in Cudatext. But if not, what are alternative for formatting a Python segment in a sublexer? Are there other formatters for Python around?
main Alexey
Posts: 2265
Joined: 25.08.2021 18:15

Post by main Alexey »

Maybe I will find time to allow Black to format a selection.

>Are there other formatters for Python around?
Only one in Addons Manager: "formatter Python Reindent".
main Alexey
Posts: 2265
Joined: 25.08.2021 18:15

Post by main Alexey »

I changed in the folder py/cuda_fmt_python_black, in install.inf file
force_all=1
to
force_all=0
Now Black can format selection too. That's it! Pls change install.inf and test it. I see it works with n selected lines.
smartmic
Posts: 8
Joined: 16.11.2021 10:11

Post by smartmic »

Yes, confirmed, now it works like a charm! Thank you very much for the quick support 8-)
—Martin
Post Reply