-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
langserver: Implement textDocument/hover (#294)
* Bump hcl-lang to latest revision * langserver: Implement textDocument/hover
- Loading branch information
1 parent
bf37e29
commit c477bf9
Showing
9 changed files
with
234 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package lsp | ||
|
||
import ( | ||
"github.com/hashicorp/hcl-lang/lang" | ||
"github.com/hashicorp/terraform-ls/internal/mdplain" | ||
"github.com/sourcegraph/go-lsp" | ||
) | ||
|
||
func HoverData(data *lang.HoverData, cc lsp.TextDocumentClientCapabilities) lsp.Hover { | ||
mdSupported := cc.Hover != nil && | ||
len(cc.Hover.ContentFormat) > 0 && | ||
cc.Hover.ContentFormat[0] == "markdown" | ||
|
||
value := data.Content.Value | ||
if data.Content.Kind == lang.MarkdownKind && !mdSupported { | ||
value = mdplain.Clean(value) | ||
} | ||
|
||
content := lsp.RawMarkedString(value) | ||
rng := HCLRangeToLSP(data.Range) | ||
|
||
return lsp.Hover{ | ||
Contents: []lsp.MarkedString{content}, | ||
Range: &rng, | ||
} | ||
} |
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,62 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
|
||
lsctx "github.com/hashicorp/terraform-ls/internal/context" | ||
ilsp "github.com/hashicorp/terraform-ls/internal/lsp" | ||
lsp "github.com/sourcegraph/go-lsp" | ||
) | ||
|
||
func (h *logHandler) TextDocumentHover(ctx context.Context, params lsp.TextDocumentPositionParams) (lsp.Hover, error) { | ||
var data lsp.Hover | ||
|
||
fs, err := lsctx.DocumentStorage(ctx) | ||
if err != nil { | ||
return data, err | ||
} | ||
|
||
cc, err := lsctx.ClientCapabilities(ctx) | ||
if err != nil { | ||
return data, err | ||
} | ||
|
||
rmf, err := lsctx.RootModuleFinder(ctx) | ||
if err != nil { | ||
return data, err | ||
} | ||
|
||
file, err := fs.GetDocument(ilsp.FileHandlerFromDocumentURI(params.TextDocument.URI)) | ||
if err != nil { | ||
return data, err | ||
} | ||
|
||
rm, err := rmf.RootModuleByPath(file.Dir()) | ||
if err != nil { | ||
return data, err | ||
} | ||
|
||
schema, err := rmf.SchemaForPath(file.Dir()) | ||
if err != nil { | ||
return data, err | ||
} | ||
|
||
d, err := rm.DecoderWithSchema(schema) | ||
if err != nil { | ||
return data, err | ||
} | ||
|
||
fPos, err := ilsp.FilePositionFromDocumentPosition(params, file) | ||
if err != nil { | ||
return data, err | ||
} | ||
|
||
h.logger.Printf("Looking for hover data at %q -> %#v", file.Filename(), fPos.Position()) | ||
hoverData, err := d.HoverAtPos(file.Filename(), fPos.Position()) | ||
h.logger.Printf("received hover data: %#v", data) | ||
if err != nil { | ||
return data, err | ||
} | ||
|
||
return ilsp.HoverData(hoverData, cc.TextDocument), 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,129 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-version" | ||
tfjson "github.com/hashicorp/terraform-json" | ||
"github.com/hashicorp/terraform-ls/internal/terraform/exec" | ||
"github.com/hashicorp/terraform-ls/internal/terraform/rootmodule" | ||
"github.com/hashicorp/terraform-ls/langserver" | ||
"github.com/hashicorp/terraform-ls/langserver/session" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestHover_withoutInitialization(t *testing.T) { | ||
ls := langserver.NewLangServerMock(t, NewMockSession(nil)) | ||
stop := ls.Start(t) | ||
defer stop() | ||
|
||
ls.CallAndExpectError(t, &langserver.CallRequest{ | ||
Method: "textDocument/hover", | ||
ReqParams: fmt.Sprintf(`{ | ||
"textDocument": { | ||
"uri": "%s/main.tf" | ||
}, | ||
"position": { | ||
"character": 0, | ||
"line": 1 | ||
} | ||
}`, TempDir(t).URI())}, session.SessionNotInitialized.Err()) | ||
} | ||
|
||
func TestHover_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{ | ||
RootModules: map[string]*rootmodule.RootModuleMock{ | ||
tmpDir.Dir(): { | ||
TfExecFactory: exec.NewMockExecutor([]*mock.Call{ | ||
{ | ||
Method: "Version", | ||
Repeatability: 1, | ||
Arguments: []interface{}{ | ||
mock.AnythingOfType(""), | ||
}, | ||
ReturnArguments: []interface{}{ | ||
version.Must(version.NewVersion("0.12.0")), | ||
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 | ||
}`, TempDir(t).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" | ||
} | ||
}`, TempDir(t).URI())}) | ||
|
||
ls.CallAndExpectResponse(t, &langserver.CallRequest{ | ||
Method: "textDocument/hover", | ||
ReqParams: fmt.Sprintf(`{ | ||
"textDocument": { | ||
"uri": "%s/main.tf" | ||
}, | ||
"position": { | ||
"character": 3, | ||
"line": 0 | ||
} | ||
}`, TempDir(t).URI())}, `{ | ||
"jsonrpc": "2.0", | ||
"id": 3, | ||
"result": { | ||
"contents": [ | ||
"provider Block\n\nA provider block is used to specify a provider configuration" | ||
], | ||
"range": { | ||
"start": { "line":0, "character":0 }, | ||
"end": { "line":0, "character":8 } | ||
} | ||
} | ||
}`) | ||
} |
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