Skip to content
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

CompletionAtPos returns items when the position is inside a comment #421

Open
rcjsuen opened this issue Oct 13, 2024 · 0 comments
Open

CompletionAtPos returns items when the position is inside a comment #421

rcjsuen opened this issue Oct 13, 2024 · 0 comments

Comments

@rcjsuen
Copy link

rcjsuen commented Oct 13, 2024

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}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant