Skip to content

Commit

Permalink
implement generic code lens interface
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Oct 28, 2021
1 parent 6e8883e commit b5fa835
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
25 changes: 25 additions & 0 deletions decoder/code_lens.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions decoder/context.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions decoder/path_context.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
}
24 changes: 24 additions & 0 deletions lang/code_lens.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit b5fa835

Please sign in to comment.