Skip to content

Commit

Permalink
refactor: Introduce DirContext and DecoderContext
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Oct 21, 2021
1 parent 04776b6 commit 8d52dea
Show file tree
Hide file tree
Showing 35 changed files with 912 additions and 790 deletions.
2 changes: 1 addition & 1 deletion decoder/block_candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// blockSchemaToCandidate generates a lang.Candidate used for auto-complete inside an editor from a BlockSchema.
// If `prefillRequiredFields` is `false`, it returns a snippet that does not expect any prefilled fields.
// If `prefillRequiredFields` is `true`, it returns a snippet that is compatiable with a list of prefilled fields from `generateRequiredFieldsSnippet`
func (d *Decoder) blockSchemaToCandidate(blockType string, block *schema.BlockSchema, rng hcl.Range) lang.Candidate {
func (d *DirDecoder) blockSchemaToCandidate(blockType string, block *schema.BlockSchema, rng hcl.Range) lang.Candidate {
triggerSuggest := false
if len(block.Labels) > 0 {
// We make some naive assumptions here for simplicity
Expand Down
2 changes: 1 addition & 1 deletion decoder/body_candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// bodySchemaCandidates returns candidates for completion of fields inside a body or block.
func (d *Decoder) bodySchemaCandidates(body *hclsyntax.Body, schema *schema.BodySchema, prefixRng, editRng hcl.Range) lang.Candidates {
func (d *DirDecoder) bodySchemaCandidates(body *hclsyntax.Body, schema *schema.BodySchema, prefixRng, editRng hcl.Range) lang.Candidates {
prefix, _ := d.bytesFromRange(prefixRng)

candidates := lang.NewCandidates()
Expand Down
59 changes: 28 additions & 31 deletions decoder/body_candidates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,18 @@ func TestDecoder_CandidateAtPos_incompleteAttributes(t *testing.T) {
},
}

d := NewDecoder()
d.maxCandidates = 1

d.SetSchema(bodySchema)

f, _ := hclsyntax.ParseConfig([]byte(`customblock "label1" {
attr
}
`), "test.tf", hcl.InitialPos)
err := d.LoadFile("test.tf", f)
if err != nil {
t.Fatal(err)
}

d := testDirDecoder(t, &DirContext{
Schema: bodySchema,
Files: map[string]*hcl.File{
"test.tf": f,
},
})
d.maxCandidates = 1

candidates, err := d.CandidatesAtPos("test.tf", hcl.Pos{
Line: 2,
Expand Down Expand Up @@ -103,17 +102,16 @@ func TestDecoder_CandidateAtPos_computedAttributes(t *testing.T) {
},
}

d := NewDecoder()
d.SetSchema(bodySchema)

f, _ := hclsyntax.ParseConfig([]byte(`customblock "label1" {
attr
}
`), "test.tf", hcl.InitialPos)
err := d.LoadFile("test.tf", f)
if err != nil {
t.Fatal(err)
}
d := testDirDecoder(t, &DirContext{
Schema: bodySchema,
Files: map[string]*hcl.File{
"test.tf": f,
},
})

candidates, err := d.CandidatesAtPos("test.tf", hcl.Pos{
Line: 2,
Expand Down Expand Up @@ -175,20 +173,18 @@ func TestDecoder_CandidateAtPos_incompleteBlocks(t *testing.T) {
},
}

d := NewDecoder()
d.maxCandidates = 1

d.SetSchema(bodySchema)

f, _ := hclsyntax.ParseConfig([]byte(`customblock "label1" {
block1 {}
block
}
`), "test.tf", hcl.InitialPos)
err := d.LoadFile("test.tf", f)
if err != nil {
t.Fatal(err)
}
d := testDirDecoder(t, &DirContext{
Schema: bodySchema,
Files: map[string]*hcl.File{
"test.tf": f,
},
})
d.maxCandidates = 1

candidates, err := d.CandidatesAtPos("test.tf", hcl.Pos{
Line: 3,
Expand Down Expand Up @@ -252,14 +248,15 @@ func TestDecoder_CandidateAtPos_duplicateNames(t *testing.T) {
},
},
}
d := NewDecoder()
d.SetSchema(bodySchema)

f, _ := hclsyntax.ParseConfig([]byte("\n"), "test.tf", hcl.InitialPos)
err := d.LoadFile("test.tf", f)
if err != nil {
t.Fatal(err)
}

d := testDirDecoder(t, &DirContext{
Schema: bodySchema,
Files: map[string]*hcl.File{
"test.tf": f,
},
})

candidates, err := d.CandidatesAtPos("test.tf", hcl.InitialPos)
if err != nil {
Expand Down
17 changes: 7 additions & 10 deletions decoder/candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
//
// Schema is required in order to return any candidates and method will return
// error if there isn't one.
func (d *Decoder) CandidatesAtPos(filename string, pos hcl.Pos) (lang.Candidates, error) {
func (d *DirDecoder) CandidatesAtPos(filename string, pos hcl.Pos) (lang.Candidates, error) {
f, err := d.fileByName(filename)
if err != nil {
return lang.ZeroCandidates(), err
Expand All @@ -24,10 +24,7 @@ func (d *Decoder) CandidatesAtPos(filename string, pos hcl.Pos) (lang.Candidates
return lang.ZeroCandidates(), err
}

d.rootSchemaMu.RLock()
defer d.rootSchemaMu.RUnlock()

if d.rootSchema == nil {
if d.ctx.Schema == nil {
return lang.ZeroCandidates(), &NoSchemaError{}
}

Expand All @@ -40,10 +37,10 @@ func (d *Decoder) CandidatesAtPos(filename string, pos hcl.Pos) (lang.Candidates
outerBodyRng = ob.Range()
}

return d.candidatesAtPos(rootBody, outerBodyRng, d.rootSchema, pos)
return d.candidatesAtPos(rootBody, outerBodyRng, d.ctx.Schema, pos)
}

func (d *Decoder) candidatesAtPos(body *hclsyntax.Body, outerBodyRng hcl.Range, bodySchema *schema.BodySchema, pos hcl.Pos) (lang.Candidates, error) {
func (d *DirDecoder) candidatesAtPos(body *hclsyntax.Body, outerBodyRng hcl.Range, bodySchema *schema.BodySchema, pos hcl.Pos) (lang.Candidates, error) {
if bodySchema == nil {
return lang.ZeroCandidates(), nil
}
Expand Down Expand Up @@ -148,7 +145,7 @@ func (d *Decoder) candidatesAtPos(body *hclsyntax.Body, outerBodyRng hcl.Range,
return d.bodySchemaCandidates(body, bodySchema, rng, rng), nil
}

func (d *Decoder) isPosInsideAttrExpr(attr *hclsyntax.Attribute, pos hcl.Pos) bool {
func (d *DirDecoder) isPosInsideAttrExpr(attr *hclsyntax.Attribute, pos hcl.Pos) bool {
if attr.Expr.Range().ContainsPos(pos) {
return true
}
Expand Down Expand Up @@ -180,7 +177,7 @@ func (d *Decoder) isPosInsideAttrExpr(attr *hclsyntax.Attribute, pos hcl.Pos) bo
return false
}

func (d *Decoder) nameTokenRangeAtPos(filename string, pos hcl.Pos) (hcl.Range, error) {
func (d *DirDecoder) nameTokenRangeAtPos(filename string, pos hcl.Pos) (hcl.Range, error) {
rng := hcl.Range{
Filename: filename,
Start: pos,
Expand Down Expand Up @@ -227,7 +224,7 @@ func nameTokenRangeAtPos(tokens hclsyntax.Tokens, pos hcl.Pos) (hcl.Range, error
return hcl.Range{}, fmt.Errorf("no token found at %s", stringPos(pos))
}

func (d *Decoder) labelTokenRangeAtPos(filename string, pos hcl.Pos) (hcl.Range, error) {
func (d *DirDecoder) labelTokenRangeAtPos(filename string, pos hcl.Pos) (hcl.Range, error) {
rng := hcl.Range{
Filename: filename,
Start: pos,
Expand Down
Loading

0 comments on commit 8d52dea

Please sign in to comment.