-
Notifications
You must be signed in to change notification settings - Fork 131
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
Complete module source
and version
#1024
Conversation
a2ea679
to
83a1835
Compare
83a1835
to
67d0f41
Compare
8897ba4
to
ff56bd0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works great for me! I just left some implementation-related questions and suggestions in-line.
internal/context/context.go
Outdated
func WithAlgoliaCredentials(ctx context.Context, algoliaAppID, algoliaAPIKey string) context.Context { | ||
return context.WithValue(ctx, ctxAlgoliaCredentials, algolia.AlgoliaCredentials{ | ||
AlgoliaAppID: algoliaAppID, | ||
AlgoliaAPIKey: algoliaAPIKey, | ||
}) | ||
} | ||
|
||
func AlgoliaCredentials(ctx context.Context) (algolia.AlgoliaCredentials, bool) { | ||
credentials, ok := ctx.Value(ctxAlgoliaCredentials).(algolia.AlgoliaCredentials) | ||
return credentials, ok | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know the context package was/is used quite a lot for various things, but it actually represents some technical debt in that a better pattern is keeping the context helpers With*
and FromContext
within the package which produces the data we end up adding to the context - i.e. within algolia
package, so that it's more self-contained.
} | ||
|
||
for _, mod := range modules { | ||
if strings.Contains(mod.Path, datadir.DataDirName) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this also match anything.terraformanything
? i.e. do we not need to match the filepath.Separator
on both sides here?
continue | ||
} | ||
if mod.Path == path.Path { | ||
// Exclude the current module |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Exclude the current module | |
// Exclude the module we're providing completion in | |
// to avoid cyclic references |
if !strings.HasPrefix(relPath, ".") { | ||
relPath = fmt.Sprintf("./%s", relPath) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this necessary here? 🤔
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to also test early cancellation of the request via context.
|
||
for _, mod := range modules { | ||
if strings.Contains(mod.Path, datadir.DataDirName) { | ||
// Skip anything from the data directory |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Skip anything from the data directory | |
// Skip installed module copies in cache directories |
var algoliaAppID = "" | ||
|
||
// Algolia API key which should be used for searching Terraform registry modules | ||
var algoliaAPIKey = "" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mind updating the GoReleaser config as well to pass these?
I have just added the secrets to the repo as ALGOLIA_API_KEY
and ALGOLIA_APP_ID
.
|
||
modules, err := h.fetchModulesFromAlgolia(ctx, prefix) | ||
if err != nil { | ||
return candidates, err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that we ignore these errors from hooks upstream https://github.com/hashicorp/hcl-lang/blob/b66451713db4e6e3bcf6cde7d70bd8cb51cc2729/decoder/expression_candidates.go#L278
is it worth logging it here? e.g. by plumbing logger to Hooks
and using it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Just one minor (non-blocking) note.
ctx.CompletionHooks["CompleteLocalModuleSources"] = h.LocalModuleSources | ||
credentials, ok := algolia.CredentialsFromContext(s.srvCtx) | ||
if ok { | ||
h.AlgoliaClient = search.NewClient(credentials.AppID, credentials.APIKey) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: I suppose we could also avoid registering the completion hook if the credentials aren't available?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting idea. I think the code looks a bit cleaner if we always register the hook and have it exiting early. So it's up to the hook to decide if all requirements are met for a run.
I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
Closes hashicorp/vscode-terraform#672
This PR adds three hooks for completing module
source
andversion
.CompleteLocalModuleSources
Uses the local indexed modules and offers relative path suggestions, when completing module sources.
CompleteRegistryModuleSources
Searches the Terraform registry modules via Algolia and fetches the full source address and module description.
For this to work, we now need to pass
main.algoliaAppID
andmain.algoliaAPIKey
vialdflags
on build.CompleteRegistryModuleVersions
Fetches the available module versions from the Terraform registry.
Deferred work
Completion of required inputs was moved to #1029