Skip to content

JSON beautify on paste - #27

Merged
ndemianc merged 2 commits into
developfrom
feat/json-beautify-on-paste
Jul 21, 2026
Merged

JSON beautify on paste#27
ndemianc merged 2 commits into
developfrom
feat/json-beautify-on-paste

Conversation

@ndemianc

Copy link
Copy Markdown
Contributor
  • JSON beautify on paste

Copilot AI review requested due to automatic review settings July 21, 2026 16:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 DocumentPasteEditProvider that replaces text/plain pastes 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 thread extensions/levelcode-npp-pack/jsonBeautify.js Outdated
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>
@ndemianc
ndemianc merged commit c5e1f50 into develop Jul 21, 2026
1 check passed
@ndemianc
ndemianc deleted the feat/json-beautify-on-paste branch July 21, 2026 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants