-
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.
refactor: Decouple walker & indexing logic (#981)
* refactor: Decouple walker & indexing logic This refactoring solves two problems: - reduce dependencies of the walker and make it focused on _walking_ by moving out the indexing logic - move all indexing logic to a new dedicated indexer package, so that it’s easier deduplicate and reuse code within the package * move terraform/module/testdata -> walker/testdata * avoid error catching in Exists to aid readability
- Loading branch information
1 parent
b93fcac
commit 8fba5f0
Showing
831 changed files
with
726 additions
and
626 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package indexer | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-ls/internal/document" | ||
"github.com/hashicorp/terraform-ls/internal/job" | ||
"github.com/hashicorp/terraform-ls/internal/terraform/module" | ||
op "github.com/hashicorp/terraform-ls/internal/terraform/module/operation" | ||
) | ||
|
||
func (idx *Indexer) DocumentChanged(modHandle document.DirHandle) (job.IDs, error) { | ||
ids := make(job.IDs, 0) | ||
|
||
id, err := idx.jobStore.EnqueueJob(job.Job{ | ||
Dir: modHandle, | ||
Func: func(ctx context.Context) error { | ||
return module.ParseModuleConfiguration(idx.fs, idx.modStore, modHandle.Path()) | ||
}, | ||
Type: op.OpTypeParseModuleConfiguration.String(), | ||
Defer: func(ctx context.Context, jobErr error) job.IDs { | ||
ids, err := idx.decodeModule(ctx, modHandle) | ||
if err != nil { | ||
idx.logger.Printf("error: %s", err) | ||
} | ||
return ids | ||
}, | ||
}) | ||
if err != nil { | ||
return ids, err | ||
} | ||
ids = append(ids, id) | ||
|
||
id, err = idx.jobStore.EnqueueJob(job.Job{ | ||
Dir: modHandle, | ||
Func: func(ctx context.Context) error { | ||
return module.ParseVariables(idx.fs, idx.modStore, modHandle.Path()) | ||
}, | ||
Type: op.OpTypeParseVariables.String(), | ||
Defer: func(ctx context.Context, jobErr error) (ids job.IDs) { | ||
id, err := idx.jobStore.EnqueueJob(job.Job{ | ||
Dir: modHandle, | ||
Func: func(ctx context.Context) error { | ||
return module.DecodeVarsReferences(ctx, idx.modStore, idx.schemaStore, modHandle.Path()) | ||
}, | ||
Type: op.OpTypeDecodeVarsReferences.String(), | ||
}) | ||
if err != nil { | ||
return | ||
} | ||
ids = append(ids, id) | ||
return | ||
}, | ||
}) | ||
if err != nil { | ||
return ids, err | ||
} | ||
ids = append(ids, id) | ||
|
||
return ids, nil | ||
} | ||
|
||
func (idx *Indexer) decodeModule(ctx context.Context, modHandle document.DirHandle) (job.IDs, error) { | ||
ids := make(job.IDs, 0) | ||
|
||
id, err := idx.jobStore.EnqueueJob(job.Job{ | ||
Dir: modHandle, | ||
Func: func(ctx context.Context) error { | ||
return module.LoadModuleMetadata(idx.modStore, modHandle.Path()) | ||
}, | ||
Type: op.OpTypeLoadModuleMetadata.String(), | ||
Defer: func(ctx context.Context, jobErr error) (ids job.IDs) { | ||
id, err := idx.jobStore.EnqueueJob(job.Job{ | ||
Dir: modHandle, | ||
Func: func(ctx context.Context) error { | ||
return module.DecodeReferenceTargets(ctx, idx.modStore, idx.schemaStore, modHandle.Path()) | ||
}, | ||
Type: op.OpTypeDecodeReferenceTargets.String(), | ||
}) | ||
if err != nil { | ||
return | ||
} | ||
ids = append(ids, id) | ||
|
||
id, err = idx.jobStore.EnqueueJob(job.Job{ | ||
Dir: modHandle, | ||
Func: func(ctx context.Context) error { | ||
return module.DecodeReferenceOrigins(ctx, idx.modStore, idx.schemaStore, modHandle.Path()) | ||
}, | ||
Type: op.OpTypeDecodeReferenceOrigins.String(), | ||
}) | ||
if err != nil { | ||
return | ||
} | ||
ids = append(ids, id) | ||
|
||
id, err = idx.jobStore.EnqueueJob(job.Job{ | ||
Dir: modHandle, | ||
Func: func(ctx context.Context) error { | ||
return module.GetModuleDataFromRegistry(ctx, idx.registryClient, | ||
idx.modStore, idx.registryModStore, modHandle.Path()) | ||
}, | ||
Priority: job.LowPriority, | ||
Type: op.OpTypeGetModuleDataFromRegistry.String(), | ||
}) | ||
if err != nil { | ||
return | ||
} | ||
ids = append(ids, id) | ||
|
||
return | ||
}, | ||
}) | ||
if err != nil { | ||
return ids, err | ||
} | ||
ids = append(ids, id) | ||
|
||
return ids, 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,10 @@ | ||
package indexer | ||
|
||
import "io/fs" | ||
|
||
type ReadOnlyFS interface { | ||
fs.FS | ||
ReadDir(name string) ([]fs.DirEntry, error) | ||
ReadFile(name string) ([]byte, error) | ||
Stat(name string) (fs.FileInfo, error) | ||
} |
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.