Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide basic IntelliSense (except for diagnostics) for hidden *.tf files #971

Merged
merged 1 commit into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/langserver/handlers/command/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (h *CmdHandler) TerraformValidateHandler(ctx context.Context, args cmd.Comm
validateDiags := diagnostics.HCLDiagsFromJSON(jsonDiags)
diags.EmptyRootDiagnostic()
diags.Append("terraform validate", validateDiags)
diags.Append("HCL", mod.ModuleDiagnostics.AsMap())
diags.Append("HCL", mod.ModuleDiagnostics.AutoloadedOnly().AsMap())
diags.Append("HCL", mod.VarsDiagnostics.AutoloadedOnly().AsMap())

notifier.PublishHCLDiags(ctx, mod.Path, diags)
Expand Down
2 changes: 1 addition & 1 deletion internal/langserver/handlers/hooks_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func updateDiagnostics(dNotifier *diagnostics.Notifier) notifier.Hook {
defer dNotifier.PublishHCLDiags(ctx, mod.Path, diags)

if mod != nil {
diags.Append("HCL", mod.ModuleDiagnostics.AsMap())
diags.Append("HCL", mod.ModuleDiagnostics.AutoloadedOnly().AsMap())
diags.Append("HCL", mod.VarsDiagnostics.AutoloadedOnly().AsMap())
}
}
Expand Down
29 changes: 29 additions & 0 deletions internal/terraform/ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,32 @@ func TestVarsDiags_autoloadedOnly(t *testing.T) {
t.Fatalf("unexpected diagnostics: %s", diff)
}
}

func TestModuleDiags_autoloadedOnly(t *testing.T) {
md := ModDiagsFromMap(map[string]hcl.Diagnostics{
"alpha.tf": {},
"beta.tf": {
{
Severity: hcl.DiagError,
Summary: "Test error",
Detail: "Test description",
},
},
".hidden.tf": {},
})
diags := md.AutoloadedOnly().AsMap()
expectedDiags := map[string]hcl.Diagnostics{
"alpha.tf": {},
"beta.tf": {
{
Severity: hcl.DiagError,
Summary: "Test error",
Detail: "Test description",
},
},
}

if diff := cmp.Diff(expectedDiags, diags, ctydebug.CmpOptions); diff != "" {
t.Fatalf("unexpected diagnostics: %s", diff)
}
}
20 changes: 15 additions & 5 deletions internal/terraform/ast/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ func (mf ModFilename) IsJSON() bool {
return strings.HasSuffix(string(mf), ".json")
}

func IsModuleFilename(name string) bool {
if isIgnoredFile(name) {
// See https://github.com/hashicorp/terraform/blob/d35bc05/internal/configs/parser_config_dir.go#L107
return false
}
func (mf ModFilename) IsIgnored() bool {
return isIgnoredFile(string(mf))
}

func IsModuleFilename(name string) bool {
return strings.HasSuffix(name, ".tf") ||
strings.HasSuffix(name, ".tf.json")
}

// isIgnoredFile returns true if the given filename (which must not have a
// directory path ahead of it) should be ignored as e.g. an editor swap file.
// See https://github.com/hashicorp/terraform/blob/d35bc05/internal/configs/parser_config_dir.go#L107
func isIgnoredFile(name string) bool {
return strings.HasPrefix(name, ".") || // Unix-like hidden files
strings.HasSuffix(name, "~") || // vim
Expand Down Expand Up @@ -62,6 +62,16 @@ func ModDiagsFromMap(m map[string]hcl.Diagnostics) ModDiags {
return mf
}

func (md ModDiags) AutoloadedOnly() ModDiags {
diags := make(ModDiags)
for name, f := range md {
if !name.IsIgnored() {
diags[name] = f
}
}
return diags
}

func (md ModDiags) AsMap() map[string]hcl.Diagnostics {
m := make(map[string]hcl.Diagnostics, len(md))
for name, diags := range md {
Expand Down
10 changes: 0 additions & 10 deletions internal/terraform/ast/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,6 @@ func (vd VarsDiags) AutoloadedOnly() VarsDiags {
return diags
}

func (vd VarsDiags) ForFile(name VarsFilename) VarsDiags {
diags := make(VarsDiags)
for fName, f := range vd {
if fName == name {
diags[fName] = f
}
}
return diags
}

func (vd VarsDiags) AsMap() map[string]hcl.Diagnostics {
m := make(map[string]hcl.Diagnostics, len(vd))
for name, diags := range vd {
Expand Down
6 changes: 4 additions & 2 deletions internal/terraform/parser/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ func TestParseModuleFiles(t *testing.T) {
{
"valid-mod-files-with-extra-items",
map[string]struct{}{
"main.tf": {},
".hidden.tf": {},
"main.tf": {},
},
map[string]hcl.Diagnostics{
"main.tf": nil,
".hidden.tf": nil,
"main.tf": nil,
},
},
{
Expand Down