forked from hashicorp/terraform-ls
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add docs links to document and hover (hashicorp#402)
* Add docs links to document and hover * Bump hcl-lang and terraform-schema to latest revisions
- Loading branch information
1 parent
4da13b2
commit f9eb59f
Showing
15 changed files
with
288 additions
and
35 deletions.
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
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,35 @@ | ||
package decoder | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/hcl-lang/decoder" | ||
lsctx "github.com/hashicorp/terraform-ls/internal/context" | ||
"github.com/hashicorp/terraform-ls/internal/terraform/module" | ||
) | ||
|
||
func DecoderForModule(ctx context.Context, mod module.Module) (*decoder.Decoder, error) { | ||
d := decoder.NewDecoder() | ||
d.SetUtmSource("terraform-ls") | ||
d.UseUtmContent(true) | ||
|
||
clientName, ok := lsctx.ClientName(ctx) | ||
if ok { | ||
d.SetUtmMedium(clientName) | ||
} | ||
|
||
pf, err := mod.ParsedFiles() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for name, f := range pf { | ||
err := d.LoadFile(name, f) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to load a file: %w", err) | ||
} | ||
} | ||
|
||
return d, 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,55 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
|
||
lsctx "github.com/hashicorp/terraform-ls/internal/context" | ||
"github.com/hashicorp/terraform-ls/internal/decoder" | ||
ilsp "github.com/hashicorp/terraform-ls/internal/lsp" | ||
lsp "github.com/hashicorp/terraform-ls/internal/protocol" | ||
) | ||
|
||
func (h *logHandler) TextDocumentLink(ctx context.Context, params lsp.DocumentLinkParams) ([]lsp.DocumentLink, error) { | ||
fs, err := lsctx.DocumentStorage(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cc, err := lsctx.ClientCapabilities(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
mf, err := lsctx.ModuleFinder(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
file, err := fs.GetDocument(ilsp.FileHandlerFromDocumentURI(params.TextDocument.URI)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
mod, err := mf.ModuleByPath(file.Dir()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
schema, err := mf.SchemaForModule(file.Dir()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
d, err := decoder.DecoderForModule(ctx, mod) | ||
if err != nil { | ||
return nil, err | ||
} | ||
d.SetSchema(schema) | ||
|
||
links, err := d.LinksInFile(file.Filename()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ilsp.Links(links, cc.TextDocument.DocumentLink), 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,112 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-version" | ||
tfjson "github.com/hashicorp/terraform-json" | ||
"github.com/hashicorp/terraform-ls/internal/langserver" | ||
"github.com/hashicorp/terraform-ls/internal/terraform/exec" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestDocumentLink_withValidData(t *testing.T) { | ||
tmpDir := TempDir(t) | ||
InitPluginCache(t, tmpDir.Dir()) | ||
|
||
var testSchema tfjson.ProviderSchemas | ||
err := json.Unmarshal([]byte(testSchemaOutput), &testSchema) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{ | ||
TerraformCalls: &exec.TerraformMockCalls{ | ||
PerWorkDir: map[string][]*mock.Call{ | ||
tmpDir.Dir(): { | ||
{ | ||
Method: "Version", | ||
Repeatability: 1, | ||
Arguments: []interface{}{ | ||
mock.AnythingOfType(""), | ||
}, | ||
ReturnArguments: []interface{}{ | ||
version.Must(version.NewVersion("0.12.0")), | ||
nil, | ||
nil, | ||
}, | ||
}, | ||
{ | ||
Method: "GetExecPath", | ||
Repeatability: 1, | ||
ReturnArguments: []interface{}{ | ||
"", | ||
}, | ||
}, | ||
{ | ||
Method: "ProviderSchemas", | ||
Repeatability: 1, | ||
Arguments: []interface{}{ | ||
mock.AnythingOfType(""), | ||
}, | ||
ReturnArguments: []interface{}{ | ||
&testSchema, | ||
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.Call(t, &langserver.CallRequest{ | ||
Method: "textDocument/didOpen", | ||
ReqParams: fmt.Sprintf(`{ | ||
"textDocument": { | ||
"version": 0, | ||
"languageId": "terraform", | ||
"text": "provider \"test\" {\n\n}\n", | ||
"uri": "%s/main.tf" | ||
} | ||
}`, tmpDir.URI())}) | ||
|
||
ls.CallAndExpectResponse(t, &langserver.CallRequest{ | ||
Method: "textDocument/documentLink", | ||
ReqParams: fmt.Sprintf(`{ | ||
"textDocument": { | ||
"uri": "%s/main.tf" | ||
} | ||
}`, tmpDir.URI())}, `{ | ||
"jsonrpc": "2.0", | ||
"id": 3, | ||
"result": [ | ||
{ | ||
"range": { | ||
"start": { | ||
"line": 0, | ||
"character": 9 | ||
}, | ||
"end": { | ||
"line": 0, | ||
"character": 15 | ||
} | ||
}, | ||
"target": "https://registry.terraform.io/providers/hashicorp/test/latest/docs?utm_content=documentLink\u0026utm_source=terraform-ls" | ||
} | ||
] | ||
}`) | ||
} |
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
Oops, something went wrong.