JSON beautify on paste - #27
Merged
Merged
Conversation
ndemianc
commented
Jul 21, 2026
Contributor
- JSON beautify on paste
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a Notepad++-pack feature that automatically pretty-prints JSON when pasting into any editor buffer (including untitled scratch tabs), with a pure core function and unit tests to ensure only valid JSON containers are transformed.
Changes:
- Introduces a
DocumentPasteEditProviderthat replacestext/plainpastes with formatted JSON when the entire paste is a valid JSON object/array. - Adds a pure
analyzePaste()helper (no VS Code dependency) to decide when/what to beautify, plus unit tests. - Exposes settings to enable/disable the feature and control indentation behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| extensions/levelcode-npp-pack/test/jsonBeautify.test.js | Adds unit tests for JSON paste analysis/beautification logic. |
| extensions/levelcode-npp-pack/package.json | Adds configuration schema for JSON beautify-on-paste settings. |
| extensions/levelcode-npp-pack/jsonPaste.js | Implements the VS Code paste provider glue for beautifying JSON on paste. |
| extensions/levelcode-npp-pack/jsonBeautify.js | Adds pure logic to detect/format beautifiable JSON pastes with a size guard. |
| extensions/levelcode-npp-pack/extension.js | Wires the new JSON paste feature into extension activation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+83
to
+88
| test('honors the size guard (parses nothing past maxBytes)', () => { | ||
| const r = analyzePaste('[1,2,3,4,5]', { maxBytes: 4 }); | ||
| assert.strictEqual(r.beautify, false); | ||
| assert.strictEqual(r.reason, 'too-large'); | ||
| assert.ok(MAX_BYTES >= 1024 * 1024, 'default cap should be sizeable'); | ||
| }); |
Addresses the PR #27 review (Copilot). The guard is named/documented as a byte cap (MAX_BYTES / maxBytes) but checked `trimmed.length` — UTF-16 code units. For non-ASCII pastes that undercounts the real size (a CJK char is 1 code unit but 3 UTF-8 bytes), so a payload up to ~3× the intended cap could slip past and get parsed/stringified on paste, defeating the ext-host-stall protection. Switched the check to Buffer.byteLength(trimmed, 'utf8') so the cap means what its name says, and added a regression test: a JSON blob whose .length is under the cap but whose byte size is over it is now blocked (and still beautifies when the cap is raised above its byte size). Verified: 13 tests pass; a 58-code-unit / 158-byte payload is now too-large at maxBytes=100, where a .length guard would have parsed it. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.