Skip to content

Commit

Permalink
Introduce go-to-variable from module input name
Browse files Browse the repository at this point in the history
Basically just reflecting all breaking changes upstream
in hcl-lang and terraform-schema.
  • Loading branch information
radeksimko committed Nov 18, 2021
1 parent 6917367 commit 548a119
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 35 deletions.
18 changes: 15 additions & 3 deletions internal/codelens/reference_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ func ReferenceCount(showReferencesCmdId string) lang.CodeLensFunc {
return func(ctx context.Context, path lang.Path, file string) ([]lang.CodeLens, error) {
lenses := make([]lang.CodeLens, 0)

pathCtx, err := decoder.PathCtx(ctx)
localCtx, err := decoder.PathCtx(ctx)
if err != nil {
return nil, err
}

refTargets := pathCtx.ReferenceTargets.OutermostInFile(file)
pathReader, err := decoder.PathReaderFromContext(ctx)
if err != nil {
return nil, err
}

refTargets := localCtx.ReferenceTargets.OutermostInFile(file)
if err != nil {
return nil, err
}
Expand All @@ -48,7 +53,14 @@ func ReferenceCount(showReferencesCmdId string) lang.CodeLensFunc {
defRange = refTarget.DefRangePtr
}

originCount += len(pathCtx.ReferenceOrigins.Targeting(refTarget))
paths := pathReader.Paths(ctx)
for _, p := range paths {
pathCtx, err := pathReader.PathContext(p)
if err != nil {
continue
}
originCount += len(pathCtx.ReferenceOrigins.Match(refTarget, path))
}
}

if originCount == 0 {
Expand Down
9 changes: 5 additions & 4 deletions internal/langserver/handlers/code_lens.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"context"

"github.com/hashicorp/hcl-lang/lang"
lsctx "github.com/hashicorp/terraform-ls/internal/context"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
Expand All @@ -22,12 +23,12 @@ func (svc *service) TextDocumentCodeLens(ctx context.Context, params lsp.CodeLen
return list, err
}

d, err := svc.decoderForDocument(ctx, doc)
if err != nil {
return nil, err
path := lang.Path{
Path: doc.Dir(),
LanguageID: doc.LanguageID(),
}

lenses, err := d.CodeLensesForFile(ctx, doc.Filename())
lenses, err := svc.decoder.CodeLensesForFile(ctx, path, doc.Filename())
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions internal/langserver/handlers/go_to_ref_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"context"

"github.com/hashicorp/hcl-lang/lang"
lsctx "github.com/hashicorp/terraform-ls/internal/context"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
Expand All @@ -24,20 +25,19 @@ func (svc *service) GoToReferenceTarget(ctx context.Context, params lsp.TextDocu
return nil, err
}

d, err := svc.decoderForDocument(ctx, doc)
if err != nil {
return nil, err
}

fPos, err := ilsp.FilePositionFromDocumentPosition(params, doc)
if err != nil {
return nil, err
}

target, err := d.ReferenceTargetForOriginAtPos(doc.Filename(), fPos.Position())
path := lang.Path{
Path: doc.Dir(),
LanguageID: doc.LanguageID(),
}
targets, err := svc.decoder.ReferenceTargetsForOriginAtPos(path, doc.Filename(), fPos.Position())
if err != nil {
return nil, err
}

return ilsp.RefTargetToLocationLink(target, cc.TextDocument.Declaration.LinkSupport), nil
return ilsp.RefTargetsToLocationLinks(targets, cc.TextDocument.Declaration.LinkSupport), nil
}
8 changes: 4 additions & 4 deletions internal/langserver/handlers/go_to_ref_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ output "foo" {
}`, tmpDir.URI())}, fmt.Sprintf(`{
"jsonrpc": "2.0",
"id": 3,
"result": {
"result": [{
"uri":"%s/main.tf",
"range": {
"start": {
Expand All @@ -99,7 +99,7 @@ output "foo" {
"character": 1
}
}
}
}]
}`, tmpDir.URI()))
}

Expand Down Expand Up @@ -180,7 +180,7 @@ output "foo" {
}`, tmpDir.URI())}, fmt.Sprintf(`{
"jsonrpc": "2.0",
"id": 3,
"result": {
"result": [{
"uri":"%s/main.tf",
"range": {
"start": {
Expand All @@ -192,6 +192,6 @@ output "foo" {
"character": 1
}
}
}
}]
}`, tmpDir.URI()))
}
11 changes: 6 additions & 5 deletions internal/langserver/handlers/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"context"

"github.com/hashicorp/hcl-lang/lang"
lsctx "github.com/hashicorp/terraform-ls/internal/context"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
Expand All @@ -21,17 +22,17 @@ func (svc *service) References(ctx context.Context, params lsp.ReferenceParams)
return list, err
}

d, err := svc.decoderForDocument(ctx, doc)
fPos, err := ilsp.FilePositionFromDocumentPosition(params.TextDocumentPositionParams, doc)
if err != nil {
return list, err
}

fPos, err := ilsp.FilePositionFromDocumentPosition(params.TextDocumentPositionParams, doc)
if err != nil {
return list, err
path := lang.Path{
Path: doc.Dir(),
LanguageID: doc.LanguageID(),
}

origins := d.ReferenceOriginsTargetingPos(doc.Filename(), fPos.Position())
origins := svc.decoder.ReferenceOriginsTargetingPos(path, doc.Filename(), fPos.Position())

return ilsp.RefOriginsToLocations(origins), nil
}
40 changes: 29 additions & 11 deletions internal/lsp/location_links.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,41 @@ import (
"github.com/hashicorp/terraform-ls/internal/uri"
)

func RefTargetToLocationLink(target *decoder.ReferenceTarget, linkSupport bool) interface{} {
targetUri := uri.FromPath(filepath.Join(target.Path.Path, target.Range.Filename))

func RefTargetsToLocationLinks(targets decoder.ReferenceTargets, linkSupport bool) interface{} {
if linkSupport {
locLink := lsp.LocationLink{
OriginSelectionRange: HCLRangeToLSP(target.OriginRange),
TargetURI: lsp.DocumentURI(targetUri),
TargetRange: HCLRangeToLSP(target.Range),
links := make([]lsp.LocationLink, 0)
for _, target := range targets {
links = append(links, refTargetToLocationLink(target))
}
return links
}

if target.DefRangePtr != nil {
locLink.TargetSelectionRange = HCLRangeToLSP(*target.DefRangePtr)
}
locations := make([]lsp.Location, 0)
for _, target := range targets {
locations = append(locations, refTargetToLocation(target))
}
return locations
}

func refTargetToLocationLink(target *decoder.ReferenceTarget) lsp.LocationLink {
targetUri := uri.FromPath(filepath.Join(target.Path.Path, target.Range.Filename))

locLink := lsp.LocationLink{
OriginSelectionRange: HCLRangeToLSP(target.OriginRange),
TargetURI: lsp.DocumentURI(targetUri),
TargetRange: HCLRangeToLSP(target.Range),
}

return locLink
if target.DefRangePtr != nil {
locLink.TargetSelectionRange = HCLRangeToLSP(*target.DefRangePtr)
}

return locLink
}

func refTargetToLocation(target *decoder.ReferenceTarget) lsp.Location {
targetUri := uri.FromPath(filepath.Join(target.Path.Path, target.Range.Filename))

return lsp.Location{
URI: lsp.DocumentURI(targetUri),
Range: HCLRangeToLSP(target.Range),
Expand Down
2 changes: 1 addition & 1 deletion internal/terraform/module/module_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestModuleLoader_referenceCollection(t *testing.T) {
}

expectedOrigins := reference.Origins{
{
reference.LocalOrigin{
Addr: lang.Address{lang.RootStep{Name: "var"}, lang.AttrStep{Name: "count"}},
Range: hcl.Range{
Filename: "main.tf",
Expand Down

0 comments on commit 548a119

Please sign in to comment.