From 83a18356d11b2367d68a2424a3fdd84a6ba1e498 Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Wed, 3 Aug 2022 17:30:54 +0200 Subject: [PATCH] Sort module versions descending and only return maxCandidates --- internal/hooks/module_version.go | 12 +++++++++++- internal/lsp/completion.go | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/internal/hooks/module_version.go b/internal/hooks/module_version.go index 79364ba23..2226afe36 100644 --- a/internal/hooks/module_version.go +++ b/internal/hooks/module_version.go @@ -3,6 +3,7 @@ package hooks import ( "context" "errors" + "fmt" "github.com/hashicorp/hcl-lang/decoder" "github.com/hashicorp/hcl/v2" @@ -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 { @@ -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) } diff --git a/internal/lsp/completion.go b/internal/lsp/completion.go index c01214516..8eee68340 100644 --- a/internal/lsp/completion.go +++ b/internal/lsp/completion.go @@ -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 {