Skip to content

feat(code-action): add "with" to "let/inherit" refactor action#768

Merged
inclyc merged 4 commits into
mainfrom
feature/add-code-action-7
Jan 13, 2026
Merged

feat(code-action): add "with" to "let/inherit" refactor action#768
inclyc merged 4 commits into
mainfrom
feature/add-code-action-7

Conversation

@takeokunn

Copy link
Copy Markdown
Collaborator

Summary

Add a new code action that converts with expressions to explicit let/inherit syntax.

Example transformation:

# Before
with lib; optionalString true "yes"

# After
let inherit (lib) optionalString; in optionalString true "yes"

Features

  • Only offered when cursor is on the with keyword
  • Automatically collects variables used from the with scope
  • Variables are sorted alphabetically in the inherit list
  • Handles nested with expressions correctly
  • Skips unused with (handled by existing "remove with" quickfix)

Test Plan

  • Added 10 regression tests covering:
    • Basic conversion
    • Multiple variables with alphabetic sorting
    • Complex source expressions (e.g., pkgs.lib)
    • Nested with expressions
    • Variable shadowing
    • Cursor position validation
    • Unused with handling

Related

- Introduce code action to convert `with` expressions to `let/inherit`
- Add implementation and tests for various scenarios, including:
- Attribute set bodies, lambda bodies, multiple and nested variables
- Alphabetical sorting and correct handling of variable shadowing
- Action only offered when cursor is on `with` and variables are used
- Update build configuration to include new code action sources
Comment thread nixd/tools/nixd/test/code-action/with-to-let/with-to-let-nested.md
…ested with

- Add unwrapParen and hasDirectlyNestedWith helpers to detect nested with chains
- Only offer with-to-let code action for innermost with in nested expressions
- Update tests to verify correct behavior for nested, parenthesized, and triple-nested cases
- Document new restriction in WithToLet.h and test descriptions
@takeokunn takeokunn requested a review from inclyc January 12, 2026 04:17
Comment thread nixd/lib/Controller/CodeActions/WithToLet.cpp Outdated
Address review feedback to detect indirect nested with scopes.
- Add getWithScopes() to VariableLookupAnalysis for tracking with scopes
- Replace AST-based hasDirectlyNestedWith with semantic hasNestedWithScope
- Add unit tests for getWithScopes in various nesting scenarios
- Add integration tests for indirect nesting cases

Ref: #768 (comment)
…ction

- Update comments in VariableLookup.cpp and WithToLet.cpp for better
  grammar and readability
- Clarify intent and semantics of code action logic
- No functional code changes made
@takeokunn takeokunn added nixd:controller enhancement New feature or request labels Jan 13, 2026
@inclyc inclyc merged commit ed80bcf into main Jan 13, 2026
19 checks passed
@inclyc inclyc deleted the feature/add-code-action-7 branch January 13, 2026 15:49
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