Skip to content

feat(completion): support deep completion for indexed variable traversals#2109

Open
Oussama-El-Amrani wants to merge 2 commits intohashicorp:mainfrom
Oussama-El-Amrani:main
Open

feat(completion): support deep completion for indexed variable traversals#2109
Oussama-El-Amrani wants to merge 2 commits intohashicorp:mainfrom
Oussama-El-Amrani:main

Conversation

@Oussama-El-Amrani
Copy link
Copy Markdown

Summary

This PR implements discovery of literal index steps (e.g., var.foo[0] or var.map["key"]) by scanning module files. This allows the Language Server to provide autocompletion for deep attributes within complex variables (lists, maps, and objects) that were previously opaque.

Problem

Currently, terraform-ls struggles to provide completion for nested attributes when they are accessed through an index or key, because indexing every possible value for every variable is computationally expensive. This is a major part of the limitation tracked in #653.

Solution

Instead of trying to index all possible paths, this PR uses a "best-effort discovery" mechanism:

  • It scans module files for var. traversals.
  • It extracts literal indices (numbers or strings) found in the code.
  • It recursively expands the cty.Type targets for these specific discovered keys.
  • This results in a lightweight, effective way to "learn" the structure the user is actually working with.

Changes

  • Modified internal/features/modules/decoder/references.go to include the discovery logic.
  • Added comprehensive tests in internal/langserver/handlers/complete_test.go.
  • Improved test robustness by preferring local Terraform binaries and avoiding networked module installs in tests.

Fixes #653

Oussama-El-Amrani and others added 2 commits April 15, 2026 14:46
…sals

Implement discovery of literal index steps (e.g., var.foo[0]) by scanning
module files. This allows the LSP to provide autocompletion for deep
attributes within complex variables that were previously opaque.

- Scans module files for 'var.' traversals.
- Recursively expands cty.Type targets based on discovered index keys.
- Adds comprehensive tests for nested objects, lists, and maps.
- Fixes hashicorp#653
@Oussama-El-Amrani Oussama-El-Amrani requested a review from a team as a code owner April 15, 2026 13:51
@hashicorp-cla-app
Copy link
Copy Markdown

hashicorp-cla-app bot commented Apr 15, 2026

CLA assistant check
All committers have signed the CLA.

@hashicorp-cla-app
Copy link
Copy Markdown

CLA assistant check

Thank you for your submission! We require that all contributors sign our Contributor License Agreement ("CLA") before we can accept the contribution. Read and sign the agreement

Learn more about why HashiCorp requires a CLA and what the CLA includes

Have you signed the CLA already but the status is still pending? Recheck it.

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.

Provide references to variables of complex types

1 participant