Skip to content

feat(code-action): add "add to formals" action for undefined variables#777

Merged
takeokunn merged 3 commits into
mainfrom
feature/add-code-action-11
Jan 28, 2026
Merged

feat(code-action): add "add to formals" action for undefined variables#777
takeokunn merged 3 commits into
mainfrom
feature/add-code-action-11

Conversation

@takeokunn

Copy link
Copy Markdown
Collaborator

Summary

Add code action to add undefined variables to the formals of enclosing lambda expressions.

Example Transformation:

Before After
{x}: y {x, y}: y
{ }: y { y }: y
{ ... }: y { y, ... }: y
{x, ...}: y {x, y, ...}: y

Changes

File Description
nixd/lib/Controller/CodeActions/AddToFormals.h Header file for add-to-formals action
nixd/lib/Controller/CodeActions/AddToFormals.cpp Implementation with 4 insertion point cases
nixd/lib/Controller/CodeAction.cpp Register the action (requires VLA)
nixd/lib/meson.build Add source to build

Behavior

Action offered when:

  • Cursor is on an undefined variable (ExprVar node)
  • Enclosing lambda uses formals-style syntax {...}: body

Action NOT offered when:

  • Variable is already defined (in any scope)
  • Lambda uses simple argument style x: body
  • No enclosing lambda exists

Handles 4 insertion cases:

  1. Empty formals { }:{ y }:
  2. Normal { a }:{ a, y }:
  3. Ellipsis only { ... }:{ y, ... }:
  4. With ellipsis { a, ... }:{ a, y, ... }:

Test Plan

  • All 8 add-to-formals tests pass
  • Build succeeds
  • Tests cover:
    • basic.md: Single existing formal
    • empty-formals.md: Empty formals { }
    • ellipsis-only.md: Ellipsis-only { ... }
    • multiple-formals.md: Multiple existing formals
    • with-ellipsis.md: Formals with ellipsis
    • nested-lambda.md: Nested lambda (adds to innermost)
    • already-defined-negative.md: Negative - variable already in formals
    • no-formals-negative.md: Negative - simple x: body syntax

Related

… vars

- Introduce addToFormalsAction to suggest adding undefined variables
  to the formals list of the nearest enclosing lambda expression
- Handle insertion for empty, single, multiple, and ellipsis formals
- Only offer the action when the variable is truly undefined and the
  lambda uses curly-brace formals
- Add comprehensive tests for positive and negative scenarios
- Register new code action in build system
…ments

- Update comments in AddToFormals.cpp to clarify insertion after non-ellipsis formals
- Refine documentation in AddToFormals.h for argument style and formals handling
@takeokunn takeokunn marked this pull request as ready for review January 27, 2026 14:25
@takeokunn takeokunn requested a review from inclyc as a code owner January 27, 2026 14:25
Comment thread nixd/lib/Controller/CodeActions/AddToFormals.cpp Outdated
@takeokunn

Copy link
Copy Markdown
Collaborator Author

Thanks Review!!

Remove "Step N:" prefixes from comments as they function similarly to
line numbers and add unnecessary maintenance overhead for the team.

Requested-by: @inclyc
@takeokunn takeokunn force-pushed the feature/add-code-action-11 branch from cc3ab86 to f25d7d9 Compare January 28, 2026 06:41
@takeokunn takeokunn merged commit 145b26c into main Jan 28, 2026
19 checks passed
@takeokunn takeokunn deleted the feature/add-code-action-11 branch January 28, 2026 07:04
@inclyc inclyc added enhancement New feature or request nixd:controller labels Feb 7, 2026
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