-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add hooks for dynamic completion #1017
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
77b8c9d
Pass context to CandidatesAtPos
dbanck be6fc6a
Add optional data to completion item response
dbanck ea3e03d
Refactor `DecoderContext` creation
dbanck 893ddaf
Introduce hooks package and register first completion hook
dbanck 3f2276d
Add `completionItem/resolve` handler
dbanck 0834145
Update hcl-lang to 118ac45
dbanck 0485ef1
Avoid exposing CompletionItem data as null
dbanck 5191397
Add completionItem/resolve test
dbanck 7189da8
Review feedback
dbanck 41f2bbf
Rename `CompletionItemR` to `CompletionItemWithResolveHook`
dbanck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Package hooks enables the implementation of hooks for dynamic | ||
// autocompletion. Hooks should be added to this package and | ||
// registered via AppendCompletionHooks in completion_hooks.go. | ||
package hooks | ||
|
||
import "github.com/hashicorp/terraform-ls/internal/state" | ||
|
||
type Hooks struct { | ||
ModStore *state.ModuleStore | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package hooks | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/hcl-lang/decoder" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
func (h *Hooks) LocalModuleSources(ctx context.Context, value cty.Value) ([]decoder.Candidate, error) { | ||
candidates := make([]decoder.Candidate, 0) | ||
|
||
// Obtain indexed modules via h.modStore.List() | ||
// TODO filter modules inside .terraform | ||
// TODO build candidates | ||
|
||
return candidates, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package handlers | ||
|
||
import ( | ||
"github.com/hashicorp/hcl-lang/decoder" | ||
"github.com/hashicorp/terraform-ls/internal/hooks" | ||
) | ||
|
||
func (s *service) AppendCompletionHooks(ctx decoder.DecoderContext) { | ||
h := hooks.Hooks{ | ||
ModStore: s.modStore, | ||
} | ||
|
||
ctx.CompletionHooks["CompleteLocalModuleSources"] = h.LocalModuleSources | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/hcl-lang/decoder" | ||
ilsp "github.com/hashicorp/terraform-ls/internal/lsp" | ||
"github.com/hashicorp/terraform-ls/internal/mdplain" | ||
lsp "github.com/hashicorp/terraform-ls/internal/protocol" | ||
) | ||
|
||
func (svc *service) CompletionItemResolve(ctx context.Context, params lsp.CompletionItemWithResolveHook) (lsp.CompletionItemWithResolveHook, 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
tfjson "github.com/hashicorp/terraform-json" | ||
"github.com/hashicorp/terraform-ls/internal/langserver" | ||
"github.com/hashicorp/terraform-ls/internal/langserver/session" | ||
) | ||
|
||
func TestCompletionResolve_withoutInitialization(t *testing.T) { | ||
ls := langserver.NewLangServerMock(t, NewMockSession(nil)) | ||
stop := ls.Start(t) | ||
defer stop() | ||
|
||
ls.CallAndExpectError(t, &langserver.CallRequest{ | ||
Method: "completionItem/resolve", | ||
ReqParams: "{}"}, session.SessionNotInitialized.Err()) | ||
} | ||
|
||
func TestCompletionResolve_withoutHook(t *testing.T) { | ||
tmpDir := TempDir(t) | ||
InitPluginCache(t, tmpDir.Path()) | ||
|
||
var testSchema tfjson.ProviderSchemas | ||
err := json.Unmarshal([]byte(testModuleSchemaOutput), &testSchema) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
ls := langserver.NewLangServerMock(t, NewMockSession(nil)) | ||
stop := ls.Start(t) | ||
defer stop() | ||
|
||
ls.Call(t, &langserver.CallRequest{ | ||
Method: "initialize", | ||
ReqParams: fmt.Sprintf(`{ | ||
"capabilities": {}, | ||
"rootUri": %q, | ||
"processId": 12345 | ||
}`, tmpDir.URI)}) | ||
ls.Notify(t, &langserver.CallRequest{ | ||
Method: "initialized", | ||
ReqParams: "{}", | ||
}) | ||
|
||
ls.CallAndExpectResponse(t, &langserver.CallRequest{ | ||
Method: "completionItem/resolve", | ||
ReqParams: fmt.Sprintf(`{ | ||
"label": "\"test\"", | ||
"kind": 1, | ||
"data": { | ||
"resolve_hook": "test", | ||
"path": "%s/main.tf" | ||
} | ||
}`, TempDir(t).URI), | ||
}, fmt.Sprintf(`{ | ||
"jsonrpc": "2.0", | ||
"id": 2, | ||
"result": { | ||
"label": "\"test\"", | ||
"labelDetails": {}, | ||
"kind": 1, | ||
"data": { | ||
"resolve_hook": "test", | ||
"path": "%s/main.tf" | ||
} | ||
} | ||
}`, TempDir(t).URI)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package protocol | ||
|
||
import "github.com/hashicorp/hcl-lang/lang" | ||
|
||
type CompletionItemWithResolveHook struct { | ||
CompletionItem | ||
|
||
ResolveHook *lang.ResolveHook `json:"data,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming this would be eventually part of a separate PR? 👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this as a placeholder to showcase the registration of hooks in
AppendCompletionHooks
. Without any hooks we wouldn't be able to createhooks.Hooks{}
.But I can remove it if you want? And defer everything to the next PR