Skip to content

Commit

Permalink
Prefill Required Attributes
Browse files Browse the repository at this point in the history
This adds a new experimental setting for prefilling required fields as blocks are completed from intellisense. For example, completing a resource provider will fill in the required attributes automatically.
  • Loading branch information
jpogran committed Oct 7, 2021
1 parent 8840e7b commit bee7ef8
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
10 changes: 10 additions & 0 deletions docs/SETTINGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ Enabling this feature will run terraform validate within the folder of the file
- Validation is not run on file open, only once it's saved.
- When editing a module file, validation is not run due to not knowing which "rootmodule" to run validation from (there could be multiple). This creates an awkward workflow where when saving a file in a rootmodule, a diagnostic is raised in a module file. Editing the module file will not clear the diagnostic for the reason mentioned above, it will only clear once a file is saved back in the original "rootmodule". We will continue to attempt improve this user experience.

### `experimentalFeatures.prefillRequiredFields`

Enables advanced snippet completion for Terraform blocks by providing a snippet that completes the label and the required fields for that block.

Our current Terraform block auto-complete snippet only provides the label name and completes the stanza. This block is not considered complete/valid until it has all required attributes and nested blocks. This means that users have to either scroll over a potentially long list of attributes and blocks to add all required ones and/or consult documentation in a separate window.

This modifies the label completion logic to provide an auto-complete snippet that lists all required fields (attributes and blocks) and expands the TextEdit range to the entire line instead of just to the tabstop. This will replace the entire line with the chosen snippet. The attributes and blocks are sorted alphabetically to ensure consistent ordering for each invocation.

For example, when completing the `aws_appmesh_route` resource the `mesh_name`, `name`, `virtual_router_name` attributes and the `spec` block will fill and prompt you for appropriate values.

## How to pass settings

The server expects static settings to be passed as part of LSP `initialize` call,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/hashicorp/go-memdb v1.3.2
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-version v1.3.0
github.com/hashicorp/hcl-lang v0.0.0-20210823185445-8fcbc27a6a22
github.com/hashicorp/hcl-lang v0.0.0-20210824132129-4bf451bad31e
github.com/hashicorp/hcl/v2 v2.10.1
github.com/hashicorp/terraform-exec v0.14.0
github.com/hashicorp/terraform-json v0.12.0
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,10 @@ github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uG
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl-lang v0.0.0-20210803155453-7c098e4940bc/go.mod h1:xzXU6Fn+TWVaZUFxV8CyAsObi2oMgSEFAmLvCx2ArzM=
github.com/hashicorp/hcl-lang v0.0.0-20210823185445-8fcbc27a6a22 h1:Yji8S7wFSfEj/KyzP/0J/f007tyKboV9rBz5u4uZ2r4=
github.com/hashicorp/hcl-lang v0.0.0-20210823185445-8fcbc27a6a22/go.mod h1:D7lBT7dekCcgbxzIHHBFvaRm42u5jY0pDoiC2J6A2KM=
github.com/hashicorp/hcl-lang v0.0.0-20210824132129-4bf451bad31e h1:RJJCsEHFbEh3GByXTyKvNVMRN8VRVOWd45WQfb5oddQ=
github.com/hashicorp/hcl-lang v0.0.0-20210824132129-4bf451bad31e/go.mod h1:D7lBT7dekCcgbxzIHHBFvaRm42u5jY0pDoiC2J6A2KM=
github.com/hashicorp/hcl-lang v0.0.0-20211006152007-86c2c237ce8d h1:5v9AxvjOjmen1wYFD1x2LpsY7d8YZSSPwYa8ahLY5bM=
github.com/hashicorp/hcl-lang v0.0.0-20211006152007-86c2c237ce8d/go.mod h1:D7lBT7dekCcgbxzIHHBFvaRm42u5jY0pDoiC2J6A2KM=
github.com/hashicorp/hcl/v2 v2.10.1 h1:h4Xx4fsrRE26ohAk/1iGF/JBqRQbyUqu5Lvj60U54ys=
github.com/hashicorp/hcl/v2 v2.10.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
Expand Down
7 changes: 7 additions & 0 deletions internal/langserver/handlers/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ func (h *logHandler) TextDocumentComplete(ctx context.Context, params lsp.Comple
}
d.SetSchema(schema)

expFeatures, err := lsctx.ExperimentalFeatures(ctx)
if err != nil {
return list, err
}

d.PrefillRequiredFields = expFeatures.PrefillRequiredFields

fPos, err := ilsp.FilePositionFromDocumentPosition(params.TextDocumentPositionParams, file)
if err != nil {
return list, err
Expand Down
1 change: 1 addition & 0 deletions internal/langserver/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {
ctx = lsctx.WithDocumentStorage(ctx, svc.fs)
ctx = lsctx.WithClientCapabilities(ctx, cc)
ctx = lsctx.WithModuleFinder(ctx, svc.modMgr)
ctx = lsctx.WithExperimentalFeatures(ctx, &expFeatures)

return handle(ctx, req, lh.TextDocumentComplete)
},
Expand Down
3 changes: 2 additions & 1 deletion internal/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

type ExperimentalFeatures struct {
ValidateOnSave bool `mapstructure:"validateOnSave"`
ValidateOnSave bool `mapstructure:"validateOnSave"`
PrefillRequiredFields bool `mapstructure:"prefillRequiredFields"`
}

type Options struct {
Expand Down

0 comments on commit bee7ef8

Please sign in to comment.