-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(TF-18664) Add DecodeReferenceOrigins and DecodeReferenceTargets jobs (…
…#1786) * Add DecodeReferenceOrigins and DecodeReferenceTargets jobs This commit adds two new jobs, DecodeReferenceOrigins and DecodeReferenceTargets, and their supporting plumbing to the stacks feature. These jobs are responsible for collecting reference origins and targets, respectively. Reference origins and targets are used to determine where a reference is defined and where it is used. This information is useful for features like go-to-definition and go-to-references.
- Loading branch information
Showing
6 changed files
with
328 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: ENHANCEMENTS | ||
body: Add DecodeReferenceOrigins and DecodeReferenceTargets jobs | ||
time: 2024-08-05T14:05:26.030294-04:00 | ||
custom: | ||
Issue: "1786" | ||
Repository: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package jobs | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/hcl-lang/decoder" | ||
"github.com/hashicorp/hcl-lang/lang" | ||
"github.com/hashicorp/hcl-lang/reference" | ||
idecoder "github.com/hashicorp/terraform-ls/internal/decoder" | ||
"github.com/hashicorp/terraform-ls/internal/document" | ||
sdecoder "github.com/hashicorp/terraform-ls/internal/features/stacks/decoder" | ||
"github.com/hashicorp/terraform-ls/internal/features/stacks/state" | ||
"github.com/hashicorp/terraform-ls/internal/job" | ||
ilsp "github.com/hashicorp/terraform-ls/internal/lsp" | ||
"github.com/hashicorp/terraform-ls/internal/terraform/module/operation" | ||
) | ||
|
||
// DecodeReferenceTargets collects reference targets, | ||
// using previously parsed AST (via [ParseStackConfiguration]), | ||
// core schema of appropriate version (as obtained via [GetTerraformVersion]) | ||
// and provider schemas ([PreloadEmbeddedSchema] or [ObtainSchema]). | ||
// | ||
// For example it tells us that variable block between certain LOC | ||
// can be referred to as var.foobar. This is useful e.g. during completion, | ||
// go-to-definition or go-to-references. | ||
func DecodeReferenceTargets(ctx context.Context, stackStore *state.StackStore, moduleReader sdecoder.ModuleReader, stackPath string) error { | ||
mod, err := stackStore.StackRecordByPath(stackPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// TODO: Avoid collection if upstream jobs reported no changes | ||
|
||
// Avoid collection if it is already in progress or already done | ||
if mod.RefTargetsState != operation.OpStateUnknown && !job.IgnoreState(ctx) { | ||
return job.StateNotChangedErr{Dir: document.DirHandleFromPath(stackPath)} | ||
} | ||
|
||
err = stackStore.SetReferenceTargetsState(stackPath, operation.OpStateLoading) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d := decoder.NewDecoder(&sdecoder.PathReader{ | ||
StateReader: stackStore, | ||
ModuleReader: moduleReader, | ||
}) | ||
d.SetContext(idecoder.DecoderContext(ctx)) | ||
|
||
stackDecoder, err := d.Path(lang.Path{ | ||
Path: stackPath, | ||
LanguageID: ilsp.Stacks.String(), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
stackTargets, rErr := stackDecoder.CollectReferenceTargets() | ||
|
||
deployDecoder, err := d.Path(lang.Path{ | ||
Path: stackPath, | ||
LanguageID: ilsp.Deploy.String(), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
deployTargets, rErr := deployDecoder.CollectReferenceTargets() | ||
|
||
targets := make(reference.Targets, 0) | ||
targets = append(targets, stackTargets...) | ||
targets = append(targets, deployTargets...) | ||
|
||
sErr := stackStore.UpdateReferenceTargets(stackPath, targets, rErr) | ||
if sErr != nil { | ||
return sErr | ||
} | ||
|
||
return rErr | ||
} | ||
|
||
// DecodeReferenceOrigins collects reference origins, | ||
// using previously parsed AST (via [ParseStackConfiguration]), | ||
// core schema of appropriate version (as obtained via [GetTerraformVersion]) | ||
// and provider schemas ([PreloadEmbeddedSchema] or [ObtainSchema]). | ||
// | ||
// For example it tells us that there is a reference address var.foobar | ||
// at a particular LOC. This can be later matched with targets | ||
// (as obtained via [DecodeReferenceTargets]) during hover or go-to-definition. | ||
func DecodeReferenceOrigins(ctx context.Context, stackStore *state.StackStore, moduleReader sdecoder.ModuleReader, stackPath string) error { | ||
mod, err := stackStore.StackRecordByPath(stackPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// TODO: Avoid collection if upstream jobs reported no changes | ||
|
||
// Avoid collection if it is already in progress or already done | ||
if mod.RefOriginsState != operation.OpStateUnknown && !job.IgnoreState(ctx) { | ||
return job.StateNotChangedErr{Dir: document.DirHandleFromPath(stackPath)} | ||
} | ||
|
||
err = stackStore.SetReferenceOriginsState(stackPath, operation.OpStateLoading) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d := decoder.NewDecoder(&sdecoder.PathReader{ | ||
StateReader: stackStore, | ||
ModuleReader: moduleReader, | ||
}) | ||
d.SetContext(idecoder.DecoderContext(ctx)) | ||
|
||
stackDecoder, err := d.Path(lang.Path{ | ||
Path: stackPath, | ||
LanguageID: ilsp.Stacks.String(), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
stackOrigins, rErr := stackDecoder.CollectReferenceOrigins() | ||
|
||
deployDecoder, err := d.Path(lang.Path{ | ||
Path: stackPath, | ||
LanguageID: ilsp.Deploy.String(), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
deployOrigins, rErr := deployDecoder.CollectReferenceOrigins() | ||
|
||
origins := make(reference.Origins, 0) | ||
origins = append(origins, stackOrigins...) | ||
origins = append(origins, deployOrigins...) | ||
|
||
sErr := stackStore.UpdateReferenceOrigins(stackPath, origins, rErr) | ||
if sErr != nil { | ||
return sErr | ||
} | ||
|
||
return rErr | ||
} |
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.