molsonkiko / JsonToolsNppPlugin

A Notepad++ plugin providing tools for JSON like linting, querying, a tree view, and CSV conversion.
Apache License 2.0
70 stars 9 forks source link

[Feature Request] - Pretty print only selection of text #43

Closed rbeede closed 5 months ago

rbeede commented 11 months ago

It'd be nice to have an option to only pretty print the currently selected text in a document. I may have a .log file with a mix of plain-text and some JSON lines in it.

If I could select just some lines of text and only format those that would be nice.

molsonkiko commented 11 months ago

I've been thinking about that for some time now. I'll try to come up with something soon.

On Thu, Aug 3, 2023, 2:02 PM Rodney Beede @.***> wrote:

It'd be nice to have an option to only pretty print the currently selected text in a document. I may have a .log file with a mix of plain-text and some JSON lines in it.

If I could select just some lines of text and only format those that would be nice.

— Reply to this email directly, view it on GitHub https://github.com/molsonkiko/JsonToolsNppPlugin/issues/43, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALAQAI5CQGE65VRBS2NJ7ITXTQGVLANCNFSM6AAAAAA3DKJJ44 . You are receiving this because you are subscribed to this thread.Message ID: @.***>

molsonkiko commented 11 months ago

I am going to work on this, but FWIW, a somewhat decent option for dealing with log files containing JSON is to use the Mark All feature in the Notepad++ find-replace form to mark all JSON, then use Copy Marked Text to copy all the JSON to a new document, where each JSON element will be on a separate line. Then JsonTools can handle it as a JSON Lines document.

For example, consider this fake log file:

Error (1/1/1900): {"a": 1, "b": "foo", "c": false}
Warning (1/1/2103): {"a": 2, "b": "bar", "c": true}
Info (2/2/1200): {"a": 3, "b": "baz", "c": true}
Debug (11/4/1900): {"a": 4, "b": "quz", "c": false}
Error (5/9/1900): {"a": 5, "b": "orun", "c": false}
Error (12/31/1900): {"a": 6, "b": "runeo", "c": true}

Open the find/replace form, select the Mark tab, use Mark All with the regex (?i-s)^[a-z]+ \(\d\d?/\d\d?/\d{4}\): \K.+ (regular expressions on), then use Copy Marked Text, open a new tab, paste the clipboard into a new tab and you get:

{"a": 1, "b": "foo", "c": false}
{"a": 2, "b": "bar", "c": true}
{"a": 3, "b": "baz", "c": true}
{"a": 4, "b": "quz", "c": false}
{"a": 5, "b": "orun", "c": false}
{"a": 6, "b": "runeo", "c": true}

which is a JSON lines document.

I know this isn't a real substitute, but just something to think about.

EDIT: In the above example, if you first do a find/replace of (?i-s)^([a-z]+)( \((\d\d?/\d\d?/\d{4})\): )(.+)}$ with ${1}${2}${4}, "loglevel": "${1}", "date": "${3}"} and then do the steps described above, you would get:

{"a": 1, "b": "foo", "c": false, "loglevel": "Error", "date": "1/1/1900"}
{"a": 2, "b": "bar", "c": true, "loglevel": "Warning", "date": "1/1/2103"}
{"a": 3, "b": "baz", "c": true, "loglevel": "Info", "date": "2/2/1200"}
{"a": 4, "b": "quz", "c": false, "loglevel": "Debug", "date": "11/4/1900"}
{"a": 5, "b": "orun", "c": false, "loglevel": "Error", "date": "5/9/1900"}
{"a": 6, "b": "runeo", "c": true, "loglevel": "Error", "date": "12/31/1900"}
molsonkiko commented 10 months ago

I'm going to leave this open so that people can respond with any bugs in the implementation I made in v5.5.

One known bug is that every now and then there's a (harmless but annoying) plugin crash when switching between buffers. If anyone can figure out what's causing that, I would be eternally grateful.

molsonkiko commented 5 months ago

See selecting all JSON in a non-JSON file