diff --git a/decoder/code_lens.go b/decoder/code_lens.go new file mode 100644 index 00000000..35270c26 --- /dev/null +++ b/decoder/code_lens.go @@ -0,0 +1,25 @@ +package decoder + +import ( + "context" + + "github.com/hashicorp/hcl-lang/lang" +) + +func (d *PathDecoder) CodeLensesForFile(ctx context.Context, file string) ([]lang.CodeLens, error) { + lenses := make([]lang.CodeLens, 0) + + // TODO: multierror + + for _, clFunc := range d.decoderCtx.CodeLenses { + ctx = withPathContext(ctx, d.pathCtx) + + cls, err := clFunc(ctx, d.path, file) + if err != nil { + return lenses, err + } + lenses = append(lenses, cls...) + } + + return lenses, nil +} diff --git a/decoder/context.go b/decoder/context.go index 4fbf9934..c3aac3c2 100644 --- a/decoder/context.go +++ b/decoder/context.go @@ -1,5 +1,7 @@ package decoder +import "github.com/hashicorp/hcl-lang/lang" + type DecoderContext struct { // UTM parameters for docs URLs // utm_source parameter, typically language server identification @@ -8,6 +10,8 @@ type DecoderContext struct { UtmMedium string // utm_content parameter, e.g. documentHover or documentLink UseUtmContent bool + + CodeLenses []lang.CodeLensFunc } func (d *Decoder) SetContext(ctx DecoderContext) { diff --git a/decoder/path_context.go b/decoder/path_context.go index b6545dd8..ebaec109 100644 --- a/decoder/path_context.go +++ b/decoder/path_context.go @@ -1,6 +1,9 @@ package decoder import ( + "context" + "fmt" + "github.com/hashicorp/hcl-lang/reference" "github.com/hashicorp/hcl-lang/schema" "github.com/hashicorp/hcl/v2" @@ -12,3 +15,17 @@ type PathContext struct { ReferenceTargets reference.Targets Files map[string]*hcl.File } + +type pathCtxKey struct{} + +func withPathContext(ctx context.Context, pathCtx *PathContext) context.Context { + return context.WithValue(ctx, pathCtxKey{}, pathCtx) +} + +func PathCtx(ctx context.Context) (*PathContext, error) { + pathCtx, ok := ctx.Value(pathCtxKey{}).(*PathContext) + if !ok { + return nil, fmt.Errorf("path context not found") + } + return pathCtx, nil +} diff --git a/lang/code_lens.go b/lang/code_lens.go new file mode 100644 index 00000000..5d5adeff --- /dev/null +++ b/lang/code_lens.go @@ -0,0 +1,24 @@ +package lang + +import ( + "context" + + "github.com/hashicorp/hcl/v2" +) + +type CodeLensFunc func(ctx context.Context, path Path, file string) ([]CodeLens, error) + +type CodeLens struct { + Range hcl.Range + Command Command +} + +type Command struct { + Title string + ID string + Arguments []CommandArgument +} + +type CommandArgument interface { + MarshalJSON() ([]byte, error) +}