Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce hooks for dynamic completion #868

Closed
10 tasks done
Tracked by #869
radeksimko opened this issue Apr 11, 2022 · 1 comment · Fixed by #1017
Closed
10 tasks done
Tracked by #869

Introduce hooks for dynamic completion #868

radeksimko opened this issue Apr 11, 2022 · 1 comment · Fixed by #1017
Assignees
Labels
enhancement New feature or request modules Functionality related to the module block and modules generally textDocument/completion
Milestone

Comments

@radeksimko
Copy link
Member

radeksimko commented Apr 11, 2022

Background

We currently provide completion based entirely on schemas, which are sourced either from providers (as JSON), declared within terraform-schema, or augmented from the configuration (such as module inputs/outputs based on variables and outputs).

Use Cases

There are use cases for a more dynamic completion however

which cannot be served using a static nor purely config-informed schema.

Future Related Use Cases

The solution may not necessarily be ready to solve any of these but these look like the same problems that could be solved by the same solution:

Proposal

hashicorp/hcl-lang

  • IntroduceCompletionHooks []lang.CompletionHook to schema.AttributeSchema
  • Introduce CompletionHooks CompletionFuncMap to decoder.DecoderContext
  • Introduce CompletionResolveHooks CompletionResolveFuncMap to decoder.DecoderContext
  • Introduce Meta interface{} field to decoder.PathContext to allow LS to pass extra context (such as which provider/module to pull versions for) to completion hooks
  • Introduce MetaFromContext(ctx) to decoder to help obtain the metadata from completion hooks in LS
  • Update CandidatesAtPos() in PathDecoder
    • Add ctx context.Context argument (for cancellation)
    • Ensure completion hooks are called if defined within AttributeSchema
      • Pass hcl.Position via ctx to each hook
      • Pass filename via ctx to each hook
      • Pass lang.Path via ctx to each hook
  • Introduce ResolveCandidate() to decoder.PathDecoder
type AttributeSchema struct {
// ...
	// CompletionHooks represents any hooks which provide
	// additional completion candidates for the attribute dynamically
	CompletionHooks []lang.CompletionHook
}
type Decoder struct {
// ...
	CompletionHooks        CompletionFuncMap
	CompletionResolveHooks CompletionResolveFuncMap
}

type CompletionFunc func(ctx context.Context, value cty.Value, args ...interface{}) ([]Candidate, error)
type CompletionFuncMap map[string]CompletionFunc

type CompletionResolveFuncMap map[string]CompletionResolveFunc
type CompletionResolveFunc func(ctx context.Context, candidate lang.Candidate, args ...interface{}) (ResolvedCandidate, error)

type Candidate struct {
	// Value represents the value to be inserted
	Value cty.Value

	// Label represents a human-readable name of the candidate
	// if one exists (otherwise Value is used)
	Label string

	// Description represents human-readable description
	// of the candidate
	Description lang.MarkupContent

	// IsDeprecated indicates whether the candidate is deprecated
	IsDeprecated bool

	// ResolveHook represents a resolve hook to call
	// and any arguments to pass to it
	ResolveHook *lang.ResolveHook
}

type ResolvedCandidate struct {
	Description lang.MarkupContent
}
package lang

type CompletionHook struct {
	Name string
	Args []interface{}
}

type ResolveHook struct {
	Name string
	Args []interface{}
}
func (d *PathDecoder) ResolveCandidate(ctx context.Context, candidate lang.Candidate) (lang.Candidate, error) {
    // TODO ...
}

terraform-ls

@github-actions
Copy link

github-actions bot commented Sep 2, 2022

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request modules Functionality related to the module block and modules generally textDocument/completion
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants