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

Make sure limited label candidates are sorted #86

Merged
merged 1 commit into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions decoder/body_candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ func (d *Decoder) bodySchemaCandidates(body *hclsyntax.Body, schema *schema.Body

candidates.IsComplete = true

// TODO: sort by more metadata, such as IsRequired or IsDeprecated
sort.Slice(candidates.List, func(i, j int) bool {
return candidates.List[i].Label < candidates.List[j].Label
})
sort.Sort(candidates)

return candidates
}
Expand Down
33 changes: 22 additions & 11 deletions decoder/label_candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,30 @@ import (

func (d *Decoder) labelCandidatesFromDependentSchema(idx int, db map[schema.SchemaKey]*schema.BodySchema, prefixRng, editRng hcl.Range) (lang.Candidates, error) {
candidates := lang.NewCandidates()
candidates.IsComplete = true
count := 0

foundCandidateNames := make(map[string]bool, 0)

prefix, _ := d.bytesFromRange(prefixRng)

for schemaKey, bodySchema := range db {
for _, schemaKey := range sortedSchemaKeys(db) {
depKeys, err := decodeSchemaKey(schemaKey)
if err != nil {
// key undecodable
continue
}

if uint(count) >= d.maxCandidates {
// reached maximum no of candidates
candidates.IsComplete = false
break
}

bodySchema := db[schemaKey]

for _, label := range depKeys.Labels {
if label.Index == idx {
if uint(count) >= d.maxCandidates {
// reached maximum no of candidates
return candidates, nil
}
if len(prefix) > 0 && !strings.HasPrefix(label.Value, string(prefix)) {
continue
}
Expand Down Expand Up @@ -71,16 +76,22 @@ func (d *Decoder) labelCandidatesFromDependentSchema(idx int, db map[schema.Sche
}
}

candidates.IsComplete = true

// TODO: sort by more metadata, such as IsDeprecated
sort.Slice(candidates.List, func(i, j int) bool {
return candidates.List[i].Label < candidates.List[j].Label
})
sort.Sort(candidates)

return candidates, nil
}

func sortedSchemaKeys(m map[schema.SchemaKey]*schema.BodySchema) []schema.SchemaKey {
keys := make([]schema.SchemaKey, 0)
for k := range m {
keys = append(keys, k)
}
sort.SliceStable(keys, func(i, j int) bool {
return string(keys[i]) < string(keys[j])
})
return keys
}

func decodeSchemaKey(key schema.SchemaKey) (schema.DependencyKeys, error) {
var dk schema.DependencyKeys
err := json.Unmarshal([]byte(key), &dk)
Expand Down
13 changes: 13 additions & 0 deletions lang/candidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ type Candidates struct {
IsComplete bool
}

func (ca Candidates) Len() int {
return len(ca.List)
}

func (ca Candidates) Less(i, j int) bool {
// TODO: sort by more metadata, such as IsRequired or IsDeprecated
return ca.List[i].Label < ca.List[j].Label
}

func (ca Candidates) Swap(i, j int) {
ca.List[i], ca.List[j] = ca.List[j], ca.List[i]
}

// NewCandidates creates a new (incomplete) list of candidates
// to be appended to.
func NewCandidates() Candidates {
Expand Down