terraform-ls icon indicating copy to clipboard operation
terraform-ls copied to clipboard

Pre-fill required local module inputs on source completion

Open dbanck opened this issue 3 years ago • 5 comments

Background

In https://github.com/hashicorp/terraform-ls/pull/1024, we introduced completion for local module sources. It uses the local indexed modules to build a list of source strings relative to the current file. The indexed modules also contain a list of module inputs (and outputs). So we can use those inputs to build a text snippet containing only the required ones.

Contrary to the registry module sources we can accomplish this without a resolve handler for completion items, since it doesn't involve any HTTP requests. All the data we need is already present.

Expected User Experience

Fill the required module inputs when completing a local module source, providing a similar experience as for resources: 2022-08-05 11 26 59

This includes:

  • Using snippets with placeholders for tabbing through all inputs after insertion
  • Showing a preview if a user has editor.suggest.preview enabled

Proposal

hashicorp/terraform-ls

  • Modify the a CompleteLocalModuleSources hook
  • Get the module inputs from mod.Meta.Variables
  • Extend the textEdit for each candidate with a snippet based on the inputs
    • We're doing something similar for resource fields here
  • Make sure the range of the textEdit still is a single line, while the snippet may contain multiple newlines.

Additional context / links

  • https://github.com/hashicorp/terraform-ls/issues/1029 similar issue for registry module sources

dbanck avatar Aug 12 '22 15:08 dbanck

@dbanck apologies for the hijack as I didn't want to open a new issue and can't comment on https://github.com/hashicorp/terraform-ls/pull/1024 Would https://github.com/hashicorp/terraform-ls/pull/1024 work for neovim folks? I'm having a hard time finding a way to trigger the autocomplete like in your gif. My init.vim is configured with coc's recommended settings. I can do things like ctrl+enter and it will provide me the list of options for a resource and help fill whether it's a list or map, but dunno how to trigger something in your example or when calling a module, have it auto-fillin the options. Thanks!

nocode99 avatar Apr 07 '23 18:04 nocode99

Hi @nocode99,

Module source and version completion should work for all editors. However, you might see different results depending on your Terraform LS installation source. This is because the binaries we build and publish on releases.hashicorp.com contain the static schema for the top registry providers and Algolia tokens, which power the module search. Both aren't included if you compile the language server or install it via the default Homebrew formula.

You can check which version you use via the -v flag.

Installation via Homebrew community reports

$ terraform-ls -v
0.30.3+Homebrew

Installation via HashiCorp Homebrew Tab or releases.hashicorp.com or Linux packaging reports

$ terraform-ls -v
0.30.3

When using the latter, module source and version completion should work. Autocompleting the source to a local module should work for all language server versions.

dbanck avatar Apr 10 '23 20:04 dbanck

Thanks for the reply @dbanck. I'm running on Ubuntu. I'll be sure to review my settings and see what I need to do to get this to work.

nocode99 avatar Apr 11 '23 19:04 nocode99

Following on nocode99's issue -- I'm also unable to complete remote module sources. I've verified I have 0.34.2 from upstream (md5sum db742e18bf7a32f71960dfc12abbbd1d)

Version completion works fine, which is interesting! (local version also completes, but that's not as interesting since it doesn't require the algolia key)

travisby avatar Aug 08 '24 13:08 travisby

Hi @travisby, can you please open a separate bug if this is still happening for you.

dbanck avatar Oct 01 '24 08:10 dbanck