Skip to content

Commit

Permalink
Introduce hooks package and register first completion hook
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Jul 27, 2022
1 parent c86d874 commit c4a57cb
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
8 changes: 5 additions & 3 deletions internal/decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ func varsPathContext(mod *state.Module) (*decoder.PathContext, error) {

func DecoderContext(ctx context.Context) decoder.DecoderContext {
dCtx := decoder.DecoderContext{
UtmSource: utm.UtmSource,
UtmMedium: utm.UtmMedium(ctx),
UseUtmContent: true,
UtmSource: utm.UtmSource,
UtmMedium: utm.UtmMedium(ctx),
UseUtmContent: true,
CompletionHooks: make(decoder.CompletionFuncMap),
CompletionResolveHooks: make(decoder.CompletionResolveFuncMap),
}

cc, err := ilsp.ClientCapabilities(ctx)
Expand Down
21 changes: 21 additions & 0 deletions internal/hooks/hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Package hooks enables the implementation of hooks for dynamic
// autocompletion. Hooks should be added to this package and
// registered in AppendCompletionHooks in completion_hooks.go.
//
// A hook must have the following signature:
// func (h *Hooks) Name(ctx context.Context, value cty.Value) ([]decoder.Candidate, error)
// It receives the current value of the attribute and must return
// a list of completion candidates.
//
// All hooks have access to path, filename and pos via context:
//
// path, ok := decoder.PathFromContext(ctx)
// filename, ok := decoder.FilenameFromContext(ctx)
// pos, ok := decoder.PosFromContext(ctx)
package hooks

import "github.com/hashicorp/terraform-ls/internal/state"

type Hooks struct {
ModStore *state.ModuleStore
}
18 changes: 18 additions & 0 deletions internal/hooks/module_source_local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package hooks

import (
"context"

"github.com/hashicorp/hcl-lang/decoder"
"github.com/zclconf/go-cty/cty"
)

func (h *Hooks) LocalModuleSources(ctx context.Context, value cty.Value) ([]decoder.Candidate, error) {
candidates := make([]decoder.Candidate, 0)

// Obtain indexed modules via h.modStore.List()
// TODO filter modules inside .terraform
// TODO build candidates

return candidates, nil
}
15 changes: 15 additions & 0 deletions internal/langserver/handlers/completion_hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package handlers

import (
"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/terraform-ls/internal/hooks"
)

func (s *service) AppendCompletionHooks(ctx decoder.DecoderContext) {
h := hooks.Hooks{
ModStore: s.modStore,
}

ctx.CompletionHooks["CompleteLocalModuleSources"] = h.LocalModuleSources

}
1 change: 1 addition & 0 deletions internal/langserver/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ func (svc *service) configureSessionDependencies(ctx context.Context, cfgOpts *s
SchemaReader: svc.schemaStore,
})
decoderContext := idecoder.DecoderContext(ctx)
svc.AppendCompletionHooks(decoderContext)
svc.decoder.SetContext(decoderContext)

err = schemas.PreloadSchemasToStore(svc.stateStore.ProviderSchemas)
Expand Down

0 comments on commit c4a57cb

Please sign in to comment.