hcl-lang icon indicating copy to clipboard operation
hcl-lang copied to clipboard

`CompletionAtPos` returns items when the position is inside a comment

Open rcjsuen opened this issue 1 year ago • 0 comments

I would've expected the second call to return zero items but it returns an item. Am I doing something wrong?

1
1
package main

import (
	"context"
	"fmt"

	"github.com/hashicorp/hcl-lang/decoder"
	"github.com/hashicorp/hcl-lang/lang"
	"github.com/hashicorp/hcl-lang/schema"
	"github.com/hashicorp/hcl/v2"
	"github.com/hashicorp/hcl/v2/hclsyntax"
)

type PathReader struct {
	Files map[string]*hcl.File
}

func (pr *PathReader) PathContext(path lang.Path) (*decoder.PathContext, error) {
	schema := &schema.BodySchema{
		Blocks: map[string]*schema.BlockSchema{"group": {}},
	}

	pathContext := &decoder.PathContext{Schema: schema, Files: pr.Files}
	return pathContext, nil
}

func (pr *PathReader) Paths(ctx context.Context) []lang.Path {
	return []lang.Path{}
}

func getNumItems(src []byte, pos hcl.Pos) int {
	file, pDiags := hclsyntax.ParseConfig(src, "example.hcl", hcl.InitialPos)
	if len(pDiags) > 0 {
		panic("errors found stop now")
	}

	decoder := decoder.NewDecoder(&PathReader{Files: map[string]*hcl.File{"example.hcl": file}})
	pathDecoder, err := decoder.Path(lang.Path{})
	if err != nil {
		panic(err)
	}

	completions, err := pathDecoder.CompletionAtPos(context.Background(), "example.hcl", pos)
	if err != nil {
		panic(err)
	}
	return len(completions.List)
}

func main() {
	// 1234567
	// group{}
	fmt.Println(getNumItems([]byte("group{}"), hcl.Pos{Line: 1, Column: 3, Byte: 3}))
	// 12345678 9101112
	// group{}\n#  c o mment
	fmt.Println(getNumItems([]byte("group{}\n# comment"), hcl.Pos{Line: 2, Column: 3, Byte: 12}))
}

rcjsuen avatar Oct 13 '24 00:10 rcjsuen