Skip to content

Commit

Permalink
Add completionItem/resolve handler
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Jul 27, 2022
1 parent c4a57cb commit 94222ea
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
55 changes: 55 additions & 0 deletions internal/langserver/handlers/completion_resolve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package handlers

import (
"context"

"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/lang"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
"github.com/hashicorp/terraform-ls/internal/mdplain"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
)

type CompletionItem struct {
lsp.CompletionItem

ResolveHook *lang.ResolveHook `json:"data,omitempty"`
}

func (svc *service) CompletionItemResolve(ctx context.Context, params CompletionItem) (CompletionItem, error) {
cc, err := ilsp.ClientCapabilities(ctx)
if err != nil {
return params, err
}

if params.ResolveHook == nil {
return params, nil
}

unresolvedCandidate := decoder.UnresolvedCandidate{
ResolveHook: params.ResolveHook,
}

resolvedCandidate, err := svc.decoder.ResolveCandidate(ctx, unresolvedCandidate)
if err != nil || resolvedCandidate == nil {
return params, err
}

if resolvedCandidate.Description.Value != "" {
doc := resolvedCandidate.Description.Value

// TODO: Revisit when MarkupContent is allowed as Documentation
// https://github.com/golang/tools/blob/4783bc9b/internal/lsp/protocol/tsprotocol.go#L753
doc = mdplain.Clean(doc)
params.Documentation = doc
}
if resolvedCandidate.Detail != "" {
params.Detail = resolvedCandidate.Detail
}
if len(resolvedCandidate.AdditionalTextEdits) > 0 {
snippetSupport := cc.TextDocument.Completion.CompletionItem.SnippetSupport
params.AdditionalTextEdits = ilsp.TextEdits(resolvedCandidate.AdditionalTextEdits, snippetSupport)
}

return params, nil
}
2 changes: 1 addition & 1 deletion internal/langserver/handlers/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func initializeResult(ctx context.Context) lsp.InitializeResult {
Change: lsp.Incremental,
},
CompletionProvider: lsp.CompletionOptions{
ResolveProvider: false,
ResolveProvider: true,
TriggerCharacters: []string{".", "["},
},
CodeActionProvider: lsp.CodeActionOptions{
Expand Down
11 changes: 11 additions & 0 deletions internal/langserver/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,17 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {

return handle(ctx, req, svc.TextDocumentComplete)
},
"completionItem/resolve": func(ctx context.Context, req *jrpc2.Request) (interface{}, error) {
err := session.CheckInitializationIsConfirmed()
if err != nil {
return nil, err
}

ctx = ilsp.WithClientCapabilities(ctx, cc)
ctx = lsctx.WithExperimentalFeatures(ctx, &expFeatures)

return handle(ctx, req, svc.CompletionItemResolve)
},
"textDocument/hover": func(ctx context.Context, req *jrpc2.Request) (interface{}, error) {
err := session.CheckInitializationIsConfirmed()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func toCompletionItem(candidate lang.Candidate, caps lsp.CompletionClientCapabil
Documentation: doc,
TextEdit: textEdit(candidate.TextEdit, snippetSupport),
Command: cmd,
AdditionalTextEdits: textEdits(candidate.AdditionalTextEdits, snippetSupport),
AdditionalTextEdits: TextEdits(candidate.AdditionalTextEdits, snippetSupport),
Data: candidate.ResolveHook,
}

Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/text_edits.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TextEditsFromDocumentChanges(changes document.Changes) []lsp.TextEdit {
return edits
}

func textEdits(tes []lang.TextEdit, snippetSupport bool) []lsp.TextEdit {
func TextEdits(tes []lang.TextEdit, snippetSupport bool) []lsp.TextEdit {
edits := make([]lsp.TextEdit, len(tes))

for i, te := range tes {
Expand Down

0 comments on commit 94222ea

Please sign in to comment.