JSON, XML formatters

ertank
Posts: 61
Joined: 06.06.2021 21:56

JSON, XML formatters

Post by ertank »

Hello,

I have a lot of JSON and XML text that I make pretty formatted. I understand that CudaText uses plugins/formatters based on file type open.

Currently I read "No formatters for Log Files" I used to open new file and paste whatever I would like to pretty format and format. Can I do same with CudaText?

I think I will be fine if I set "none" and "Log Files" If I need to do something specific to file type.

Thanks
uvviewsoft
Posts: 392
Joined: 01.12.2020 13:46

Post by uvviewsoft »

You need to set lexer for a file (e.g. 'log files'), then you also need CudaFormatter's formatter for that lexer. no formatter for 'log files' - you must create it.
https://wiki.lazarus.freepascal.org/Cud ... aFormatter
ertank
Posts: 61
Joined: 06.06.2021 21:56

Post by ertank »

I tried to read that wiki. Read your suggestion over and over. But, I failed to comprehend it to make it happen.
I think, I will select file type before I actually do particular formatting for the time being.
Thank you.
uvviewsoft
Posts: 392
Joined: 01.12.2020 13:46

Post by uvviewsoft »

What did you try and what does not work?
ertank
Posts: 61
Joined: 06.06.2021 21:56

Post by ertank »

uvviewsoft wrote:What did you try and what does not work?
I tried to understand what is explained in the wiki. Since I could not understand it. I did not do anything at all.

Sorry it was not clear from the beginning.
uvviewsoft
Posts: 392
Joined: 01.12.2020 13:46

Post by uvviewsoft »

- You install CudaFormatter plugin
- You install 2-nd level formatter for your language, e.g. formatter for Python
- You call CudaFormatter menu items in the Plugins menu, it will use 'formatters'
ertank
Posts: 61
Joined: 06.06.2021 21:56

Post by ertank »

Hello,

I started to understand how CudaText formatters work. Though, I still look for a shortcut use of xml/json formatter without any need to "link them to file extension(s)" or a lexer for my use case.

I check a lot of log files which contains XML/JSON in them between the lines. Log files I open are mostly ".log" or ".txt" extension. I look up relevant JSON/XML in these log files, select and copy that relevant part, open a new editor tab using CTRL+N, paste the json/xml to be pretty formatted in this new file. I do not need that data in the new file. I am only checking and searching for a problem. Once my check is over, that data is a trash for me. I do not save them. Such checks are part of my daily routine.

If I am using CudaText and when I use CTRL+N for a new file in editor, that new file has no extension and no lexer assigned to it (that is just fine). My problem is, I cannot use formatters unless I manually select "Lexer language" for new file. Because Lexer is assigned to a file extension. Formatters are assigned to a lexer.

On the other hand, my old editor Notepad++ can pretty print any extension using formatter plugin. I do not need to "link" plugin to file extension. If content is not a valid XML/JSON plugin does nothing which is fine by me.
I also checked Sublime Text, it can pretty print any file extension like Notepad++. Using Sublime Text it is also possible to assign a keyboard shortcut for a formatter which works for all file types. Shortcut is way faster compared to Notepad++ plugins. I could not make Notepad++ plugin shortcuts work flawless. They behave weird for me and I need to use mouse all the time to do formatting.

I do not know internals of CudaText. I do not know if it is possible to make it behave like Notepad++ or Sublime Text. Though, I appreciate you consider making CudaText work like Notepad++ and Sublime Text. Independent of file extension and lexer.

Thanks & Regards,
Ertan
uvviewsoft
Posts: 392
Joined: 01.12.2020 13:46

Post by uvviewsoft »

I suggest this.
1) install plugin Macros.
2) write a tiny plugin "set_lexer" with 2 commands: set XML lexer, set JSON lexer.
I add this plugin in zip file.

file py/cuda_set_lexer/__init__.py

Code: Select all

import os
from cudatext import *

class Command:
    def xml(self):
        ed.set_prop(PROP_LEXER_FILE, 'XML')

    def json(self):
        ed.set_prop(PROP_LEXER_FILE, 'JSON')
file py/cuda_set_lexer/install.inf

Code: Select all

[info]
title=set_lexer
desc=(Fill the description)
type=cudatext-plugin
subdir=cuda_set_lexer
homepage=(Fill the GitHub repo URL)

[item2]
section=commands
caption=set_lexer\set XML
method=xml

[item3]
section=commands
caption=set_lexer\set JSON
method=json
3) write a macro in the Macros plugin which runs 2 commands:
3a: call "set_lexer" to set XML.
3b: call CudaFormatter to format with menu.

Macro properties in Macros-dialog:

Code: Select all

# do xml
app.app_proc(app.PROC_EXEC_PLUGIN, 'cuda_set_lexer,xml,')
app.app_proc(app.PROC_EXEC_PLUGIN, 'cuda_fmt,format,')
Now just use your macro. 1 macro per 1 lexer (xml, json). and assign a hotkey per macro.
Attachments
cuda_set_lexer.zip.txt
(495 Bytes) Downloaded 173 times
uvviewsoft
Posts: 392
Joined: 01.12.2020 13:46

Post by uvviewsoft »

And new solution, w/o macros - with the new CudaFormatter.
See info here - viewtopic.php?p=15197#p15197
ertank
Posts: 61
Joined: 06.06.2021 21:56

Post by ertank »

Thank you very much.
Post Reply