Skip to content

Commit 487c81a

Browse files
authored
completion: Prompt picking type of provider/data/resource automatically (#300)
* Bump hcl-lang to latest * completion: Prompt picking type of provider/data/resource automatically
1 parent efc9909 commit 487c81a

File tree

5 files changed

+39
-19
lines changed

5 files changed

+39
-19
lines changed

commands/completion_command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (c *CompletionCommand) Run(args []string) int {
127127
}
128128

129129
cc := &lsp.ClientCapabilities{}
130-
items := ilsp.CompletionList(candidates, cc.TextDocument)
130+
items := ilsp.ToCompletionList(candidates, cc.TextDocument)
131131

132132
c.Ui.Output(fmt.Sprintf("%#v", items))
133133
return 0

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/google/go-cmp v0.5.1
1111
github.com/hashicorp/go-multierror v1.1.0
1212
github.com/hashicorp/go-version v1.2.1
13-
github.com/hashicorp/hcl-lang v0.0.0-20201113080530-b0668b270b47
13+
github.com/hashicorp/hcl-lang v0.0.0-20201116081236-948e43712a65
1414
github.com/hashicorp/hcl/v2 v2.6.0
1515
github.com/hashicorp/terraform-exec v0.11.1-0.20201007122305-ea2094d52cb5
1616
github.com/hashicorp/terraform-json v0.6.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
183183
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
184184
github.com/hashicorp/hcl-lang v0.0.0-20201110071249-4e412924f52b h1:EjnMRaTQlomBMNRQfyWoLEg9IdqxeN1R2mb3ZZetCBs=
185185
github.com/hashicorp/hcl-lang v0.0.0-20201110071249-4e412924f52b/go.mod h1:vd3BPEDWrYMAgAnB0MRlBdZknrpUXf8Jk2PNaHIbwhg=
186-
github.com/hashicorp/hcl-lang v0.0.0-20201113080530-b0668b270b47 h1:n3STOLqEwfs4QWgzV0cjg4sZDSKVM4IwK38ZDVa+ljM=
187-
github.com/hashicorp/hcl-lang v0.0.0-20201113080530-b0668b270b47/go.mod h1:vd3BPEDWrYMAgAnB0MRlBdZknrpUXf8Jk2PNaHIbwhg=
186+
github.com/hashicorp/hcl-lang v0.0.0-20201116081236-948e43712a65 h1:kF6Dxt2kPNj8+Px7LyK7nxPDQjYKwGrKxxYnSu+LOXM=
187+
github.com/hashicorp/hcl-lang v0.0.0-20201116081236-948e43712a65/go.mod h1:vd3BPEDWrYMAgAnB0MRlBdZknrpUXf8Jk2PNaHIbwhg=
188188
github.com/hashicorp/hcl/v2 v2.0.0/go.mod h1:oVVDG71tEinNGYCxinCYadcmKU9bglqW9pV3txagJ90=
189189
github.com/hashicorp/hcl/v2 v2.6.0 h1:3krZOfGY6SziUXa6H9PJU6TyohHn7I+ARYnhbeNBz+o=
190190
github.com/hashicorp/hcl/v2 v2.6.0/go.mod h1:bQTN5mpo+jewjJgh8jr0JUguIi7qPHUF6yIfAEN3jqY=

internal/lsp/completion.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@ import (
66
lsp "github.com/sourcegraph/go-lsp"
77
)
88

9-
func CompletionList(candidates lang.Candidates, caps lsp.TextDocumentClientCapabilities) lsp.CompletionList {
10-
list := lsp.CompletionList{
11-
Items: make([]lsp.CompletionItem, len(candidates.List)),
9+
type CompletionList struct {
10+
IsIncomplete bool `json:"isIncomplete"`
11+
Items []CompletionItem `json:"items"`
12+
}
13+
14+
type CompletionItem struct {
15+
lsp.CompletionItem
16+
Command *lsp.Command `json:"command,omitempty"`
17+
}
18+
19+
func ToCompletionList(candidates lang.Candidates, caps lsp.TextDocumentClientCapabilities) CompletionList {
20+
list := CompletionList{
21+
Items: make([]CompletionItem, len(candidates.List)),
1222
IsIncomplete: !candidates.IsComplete,
1323
}
1424

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

2333
for i, c := range candidates.List {
24-
list.Items[i] = CompletionItem(c, snippetSupport, markdown)
34+
list.Items[i] = toCompletionItem(c, snippetSupport, markdown)
2535
}
2636

2737
return list
2838
}
2939

30-
func CompletionItem(candidate lang.Candidate, snippet, markdown bool) lsp.CompletionItem {
40+
func toCompletionItem(candidate lang.Candidate, snippet, markdown bool) CompletionItem {
3141
doc := candidate.Description.Value
3242

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

4656
te, format := textEdit(candidate.TextEdit, snippet)
57+
var cmd *lsp.Command
58+
if candidate.TriggerSuggest {
59+
cmd = &lsp.Command{
60+
Command: "editor.action.triggerSuggest",
61+
Title: "Suggest",
62+
}
63+
}
4764

48-
return lsp.CompletionItem{
49-
Label: candidate.Label,
50-
Kind: kind,
51-
InsertTextFormat: format,
52-
Detail: candidate.Detail,
53-
Documentation: doc,
54-
TextEdit: te,
65+
return CompletionItem{
66+
CompletionItem: lsp.CompletionItem{
67+
Label: candidate.Label,
68+
Kind: kind,
69+
InsertTextFormat: format,
70+
Detail: candidate.Detail,
71+
Documentation: doc,
72+
TextEdit: te,
73+
},
74+
Command: cmd,
5575
}
5676
}
5777

langserver/handlers/complete.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
lsp "github.com/sourcegraph/go-lsp"
99
)
1010

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

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

0 commit comments

Comments
 (0)