Skip to content

Commit

Permalink
completion: Prompt picking type of provider/data/resource automatical…
Browse files Browse the repository at this point in the history
…ly (#300)

* Bump hcl-lang to latest

* completion: Prompt picking type of provider/data/resource automatically
  • Loading branch information
radeksimko authored Nov 16, 2020
1 parent efc9909 commit 487c81a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion commands/completion_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (c *CompletionCommand) Run(args []string) int {
}

cc := &lsp.ClientCapabilities{}
items := ilsp.CompletionList(candidates, cc.TextDocument)
items := ilsp.ToCompletionList(candidates, cc.TextDocument)

c.Ui.Output(fmt.Sprintf("%#v", items))
return 0
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/google/go-cmp v0.5.1
github.com/hashicorp/go-multierror v1.1.0
github.com/hashicorp/go-version v1.2.1
github.com/hashicorp/hcl-lang v0.0.0-20201113080530-b0668b270b47
github.com/hashicorp/hcl-lang v0.0.0-20201116081236-948e43712a65
github.com/hashicorp/hcl/v2 v2.6.0
github.com/hashicorp/terraform-exec v0.11.1-0.20201007122305-ea2094d52cb5
github.com/hashicorp/terraform-json v0.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl-lang v0.0.0-20201110071249-4e412924f52b h1:EjnMRaTQlomBMNRQfyWoLEg9IdqxeN1R2mb3ZZetCBs=
github.com/hashicorp/hcl-lang v0.0.0-20201110071249-4e412924f52b/go.mod h1:vd3BPEDWrYMAgAnB0MRlBdZknrpUXf8Jk2PNaHIbwhg=
github.com/hashicorp/hcl-lang v0.0.0-20201113080530-b0668b270b47 h1:n3STOLqEwfs4QWgzV0cjg4sZDSKVM4IwK38ZDVa+ljM=
github.com/hashicorp/hcl-lang v0.0.0-20201113080530-b0668b270b47/go.mod h1:vd3BPEDWrYMAgAnB0MRlBdZknrpUXf8Jk2PNaHIbwhg=
github.com/hashicorp/hcl-lang v0.0.0-20201116081236-948e43712a65 h1:kF6Dxt2kPNj8+Px7LyK7nxPDQjYKwGrKxxYnSu+LOXM=
github.com/hashicorp/hcl-lang v0.0.0-20201116081236-948e43712a65/go.mod h1:vd3BPEDWrYMAgAnB0MRlBdZknrpUXf8Jk2PNaHIbwhg=
github.com/hashicorp/hcl/v2 v2.0.0/go.mod h1:oVVDG71tEinNGYCxinCYadcmKU9bglqW9pV3txagJ90=
github.com/hashicorp/hcl/v2 v2.6.0 h1:3krZOfGY6SziUXa6H9PJU6TyohHn7I+ARYnhbeNBz+o=
github.com/hashicorp/hcl/v2 v2.6.0/go.mod h1:bQTN5mpo+jewjJgh8jr0JUguIi7qPHUF6yIfAEN3jqY=
Expand Down
44 changes: 32 additions & 12 deletions internal/lsp/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@ import (
lsp "github.com/sourcegraph/go-lsp"
)

func CompletionList(candidates lang.Candidates, caps lsp.TextDocumentClientCapabilities) lsp.CompletionList {
list := lsp.CompletionList{
Items: make([]lsp.CompletionItem, len(candidates.List)),
type CompletionList struct {
IsIncomplete bool `json:"isIncomplete"`
Items []CompletionItem `json:"items"`
}

type CompletionItem struct {
lsp.CompletionItem
Command *lsp.Command `json:"command,omitempty"`
}

func ToCompletionList(candidates lang.Candidates, caps lsp.TextDocumentClientCapabilities) CompletionList {
list := CompletionList{
Items: make([]CompletionItem, len(candidates.List)),
IsIncomplete: !candidates.IsComplete,
}

Expand All @@ -21,13 +31,13 @@ func CompletionList(candidates lang.Candidates, caps lsp.TextDocumentClientCapab
}

for i, c := range candidates.List {
list.Items[i] = CompletionItem(c, snippetSupport, markdown)
list.Items[i] = toCompletionItem(c, snippetSupport, markdown)
}

return list
}

func CompletionItem(candidate lang.Candidate, snippet, markdown bool) lsp.CompletionItem {
func toCompletionItem(candidate lang.Candidate, snippet, markdown bool) CompletionItem {
doc := candidate.Description.Value

// TODO: revisit once go-lsp supports markdown in CompletionItem
Expand All @@ -44,14 +54,24 @@ func CompletionItem(candidate lang.Candidate, snippet, markdown bool) lsp.Comple
}

te, format := textEdit(candidate.TextEdit, snippet)
var cmd *lsp.Command
if candidate.TriggerSuggest {
cmd = &lsp.Command{
Command: "editor.action.triggerSuggest",
Title: "Suggest",
}
}

return lsp.CompletionItem{
Label: candidate.Label,
Kind: kind,
InsertTextFormat: format,
Detail: candidate.Detail,
Documentation: doc,
TextEdit: te,
return CompletionItem{
CompletionItem: lsp.CompletionItem{
Label: candidate.Label,
Kind: kind,
InsertTextFormat: format,
Detail: candidate.Detail,
Documentation: doc,
TextEdit: te,
},
Command: cmd,
}
}

Expand Down
6 changes: 3 additions & 3 deletions langserver/handlers/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
lsp "github.com/sourcegraph/go-lsp"
)

func (h *logHandler) TextDocumentComplete(ctx context.Context, params lsp.CompletionParams) (lsp.CompletionList, error) {
var list lsp.CompletionList
func (h *logHandler) TextDocumentComplete(ctx context.Context, params lsp.CompletionParams) (ilsp.CompletionList, error) {
var list ilsp.CompletionList

fs, err := lsctx.DocumentStorage(ctx)
if err != nil {
Expand Down Expand Up @@ -54,5 +54,5 @@ func (h *logHandler) TextDocumentComplete(ctx context.Context, params lsp.Comple
h.logger.Printf("Looking for candidates at %q -> %#v", file.Filename(), fPos.Position())
candidates, err := d.CandidatesAtPos(file.Filename(), fPos.Position())
h.logger.Printf("received candidates: %#v", candidates)
return ilsp.CompletionList(candidates, cc.TextDocument), err
return ilsp.ToCompletionList(candidates, cc.TextDocument), err
}

0 comments on commit 487c81a

Please sign in to comment.