Skip to content

Commit

Permalink
Sort module versions descending and only return maxCandidates
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Aug 8, 2022
1 parent 357bac7 commit 83a1835
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
12 changes: 11 additions & 1 deletion internal/hooks/module_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hooks
import (
"context"
"errors"
"fmt"

"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl/v2"
Expand Down Expand Up @@ -41,6 +42,10 @@ func (h *Hooks) RegistryModuleVersions(ctx context.Context, value cty.Value) ([]
if !ok {
return candidates, errors.New("missing context: filename")
}
maxCandidates, ok := decoder.MaxCandidatesFromContext(ctx)
if !ok {
return candidates, errors.New("missing context: maxCandidates")
}

module, err := h.ModStore.ModuleByPath(path.Path)
if err != nil {
Expand All @@ -62,10 +67,15 @@ func (h *Hooks) RegistryModuleVersions(ctx context.Context, value cty.Value) ([]
return candidates, err
}

for _, v := range versions {
for i, v := range versions {
if uint(i) >= maxCandidates {
return candidates, nil
}

c := decoder.ExpressionCompletionCandidate(decoder.ExpressionCandidate{
Value: cty.StringVal(v.String()),
})
c.SortText = fmt.Sprintf("%3d", i)

candidates = append(candidates, c)
}
Expand Down
1 change: 1 addition & 0 deletions internal/lsp/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func toCompletionItem(candidate lang.Candidate, caps lsp.CompletionClientCapabil
TextEdit: textEdit(candidate.TextEdit, snippetSupport),
Command: cmd,
AdditionalTextEdits: TextEdits(candidate.AdditionalTextEdits, snippetSupport),
SortText: candidate.SortText,
}

if candidate.ResolveHook != nil {
Expand Down

0 comments on commit 83a1835

Please sign in to comment.