Skip to content

Commit 0877eb4

Browse files
committed
completion: Prompt picking type of provider/data/resource automatically
1 parent fee1028 commit 0877eb4

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
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

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)