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

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

Closed
rbeede opened this issue Aug 3, 2023 · 4 comments
Closed

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

rbeede opened this issue Aug 3, 2023 · 4 comments
Labels
enhancement New feature or request probably fixed? the issue/feature appears to be fixed/implemented, but there still may be issues

Comments

@rbeede
Copy link

rbeede commented Aug 3, 2023

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
Copy link
Owner

molsonkiko commented Aug 4, 2023 via email

@molsonkiko
Copy link
Owner

molsonkiko commented Aug 4, 2023

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 added a commit that referenced this issue Aug 13, 2023
## [5.5.0] - 2023-08-13

### Added

1. __Add support for [operating on selections](/docs/README.md#working-with-selections)__ (fix [issue 43](#43)).
2. Add method for [selecting every valid JSON element in the file](/docs/README.md#selecting-all-valid-json)
3. Add `D&ump text of current document as JSON string` and `Dump JSON string(s) as ra&w text` convenience methods.
4. Add [`parse`](/docs/RemesPath.md#vectorized-functions), [`type` and `stringify`](/docs/RemesPath.md#non-vectorized-functions) RemesPath functions.
5. Added UI tests.

### Changed

1. Improved RemesPath [boolean indices](/docs/RemesPath.md#boolean-indexing) so that they can be more easily chained together.
2. Removed unneeded RemesPath lexer tests.

### Fixed

1. Bug where `s_slice`, `max_by`, `min_by`, `group_by`, and `sort_by` all did not allow Python-style negative indices.
2. Bug where out-of-bounds negative indices when indexing in an array would throw an error rather than returning an empty array (which is the correct behavior, since RemesPath is not supposed to throw errors for indexing out of bounds).
3. Eliminated huge latency when viewing very long JSON strings in the treeview.
4. Eliminated potential access violation during plugin cleanup.
5. Bug in which the plugin would be mistaken about the position of JSON elements after PPrint-style printing of some JSON containing non-ASCII characters in strings.
6. Bug in which multiple Sort forms could be open.
7. Bug in which running tests in Notepad++ versions older than v8 could cause Notepad++ to crash.
@molsonkiko
Copy link
Owner

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 molsonkiko added the probably fixed? the issue/feature appears to be fixed/implemented, but there still may be issues label Aug 19, 2023
@molsonkiko molsonkiko added the enhancement New feature or request label Nov 2, 2023
@molsonkiko
Copy link
Owner

See selecting all JSON in a non-JSON file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request probably fixed? the issue/feature appears to be fixed/implemented, but there still may be issues
Projects
None yet
Development

No branches or pull requests

2 participants