Skip to content

feat(code-action): add selection-based JSON to Nix conversion action#763

Merged
inclyc merged 3 commits into
mainfrom
feature/code-actions-4
Jan 8, 2026
Merged

feat(code-action): add selection-based JSON to Nix conversion action#763
inclyc merged 3 commits into
mainfrom
feature/code-actions-4

Conversation

@takeokunn

@takeokunn takeokunn commented Jan 7, 2026

Copy link
Copy Markdown
Collaborator

Summary

Add a new code action that converts selected JSON text to Nix expression syntax.
This is a selection-based action that works on arbitrary text within a Nix file, allowing users to quickly convert JSON data structures to their Nix equivalents.

Example:

{"foo": 1, "bar": true, "nested": {"key": "value"}}

{
  foo = 1;
  bar = true;
  nested = {
    key = "value";
  };
}

Changes

File Description
nixd/lib/Controller/CodeAction.cpp Add escapeNixString(), jsonToNix(), and addJsonToNixAction() functions
Positive Tests
json-to-nix.md Basic test for simple JSON object conversion
json-to-nix-array.md Test for JSON arrays
json-to-nix-nested.md Test for nested structures with proper indentation
json-to-nix-numeric.md Test for numeric types (integer and float)
json-to-nix-types.md Test for all JSON types (null, boolean, string, number)
json-to-nix-escaping.md Test for escape sequences in strings
json-to-nix-interpolation.md Test for ${ interpolation escaping
json-to-nix-special-keys.md Test for keys requiring quoting
Negative Tests
json-to-nix-invalid.md Test that invalid JSON does NOT offer the action
json-to-nix-empty.md Test that empty objects {} do NOT offer the action
json-to-nix-empty-array.md Test that empty arrays [] do NOT offer the action
Edge Case Tests
json-to-nix-depth-limit.md Test safety limit for deeply nested structures
json-to-nix-width-limit.md Test safety limit for very wide arrays/objects

Behavior

Action offered when:

  • User has selected text in a Nix file
  • Selected text starts with { or [
  • Selected text is valid JSON
  • JSON is non-empty (has at least one element)

Explicitly excluded:

  • Invalid JSON: No action offered
  • Empty structures ({}, []): Already valid Nix, no transformation needed
  • Text not starting with { or [: Quick rejection for performance

Safety limits:

  • Depth limit: 100 levels of nesting (prevents stack overflow)
  • Width limit: 10,000 elements per array/object (prevents memory issues)

Escape handling:

  • " → "
  • \ → \
  • ${ → ${ (prevent Nix interpolation)
  • \n, \r, \t → preserved escape sequences

Test Plan

  • 13 regression tests covering positive, negative, and edge cases
  • All existing tests pass
  • Manual testing in editor
CleanShot 2026-01-08 at 02 23 20@2x

Related

- Introduce escapeNixString for proper Nix string escaping
- Refactor quoteNixAttrKey to use escapeNixString
- Add jsonToNix for converting JSON values to Nix syntax
- Implement addJsonToNixAction to enable selection-based JSON-to-Nix code action
- Integrate new action into onCodeAction for arbitrary text selections
- Add tests for arrays, nested objects, numeric edge cases, string
  escaping, interpolation, special keys, and all JSON types
- Add negative tests for empty arrays/objects, invalid JSON, and
  exceeding depth/width limits
- Ensure "Convert JSON to Nix" action is only offered for valid,
  convertible JSON selections
- Split overly long comments to improve readability
- No functional changes to code logic
@takeokunn takeokunn changed the title [wip] feat(code-action): add selection-based JSON to Nix conversion action feat(code-action): add selection-based JSON to Nix conversion action Jan 7, 2026
@takeokunn takeokunn marked this pull request as ready for review January 7, 2026 17:23
@takeokunn takeokunn requested a review from inclyc January 7, 2026 17:24
@takeokunn takeokunn added enhancement New feature or request nixd:controller labels Jan 7, 2026
@inclyc inclyc merged commit b835a58 into main Jan 8, 2026
19 checks passed
@inclyc inclyc deleted the feature/code-actions-4 branch January 8, 2026 07:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request nixd:controller

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants