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

feat: add a first code-action quickfix (add missing attributes) #1549

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions internal/decoder/validations/missing_required_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ func (mra MissingRequiredAttribute) Visit(ctx context.Context, node hclsyntax.No
Summary: fmt.Sprintf("Required attribute %q not specified", name),
Detail: fmt.Sprintf("An attribute named %q is required here", name),
Subject: nodeType.SrcRange.Ptr(),
Extra: map[string]interface{}{
"MissingAttribute": name,
},
})

}
}
}
Expand Down
54 changes: 54 additions & 0 deletions internal/langserver/handlers/code_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,60 @@ func (svc *service) textDocumentCodeAction(ctx context.Context, params lsp.CodeA
},
},
})
case ilsp.Quickfix:

var extractedMissingAttrs []string

for _, diag := range params.Context.Diagnostics {
diagExtra, ok := diag.Data.(map[string]interface{})
if !ok {
svc.logger.Printf("Diagnostic Data does not have the expected type, skipping")
continue
}
missingAttributeRaw, found := diagExtra["MissingAttribute"]
if !found {
continue
}
missingAttribute, ok := missingAttributeRaw.(string)
if !ok {
svc.logger.Printf("MissingAttribute does not have the expected type, skipping")
continue
}
extractedMissingAttrs = append(extractedMissingAttrs, missingAttribute)
}

if len(extractedMissingAttrs) == 0 {
svc.logger.Printf("No missing attributes found")
return ca, err
}

var edits []lsp.TextEdit

rng := params.Range
rng.Start.Line = rng.End.Line
rng.Start.Character = 0
rng.End.Character = 0

newText := ""

for _, missingAttr := range extractedMissingAttrs {
newText = newText + fmt.Sprintf(" %s = null\n", missingAttr)
}

edits = append(edits, lsp.TextEdit{
Range: rng,
NewText: newText,
})

ca = append(ca, lsp.CodeAction{
Title: "Add missing attributes",
Kind: action,
Edit: lsp.WorkspaceEdit{
Changes: map[lsp.DocumentURI][]lsp.TextEdit{
lsp.DocumentURI(dh.FullURI()): edits,
},
},
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/langserver/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func initializeResponse(t *testing.T, commandPrefix string) string {
"referencesProvider": true,
"documentSymbolProvider": true,
"codeActionProvider": {
"codeActionKinds": ["source.formatAll.terraform"]
"codeActionKinds": ["quickfix", "source.formatAll.terraform"]
},
"codeLensProvider": {},
"documentLinkProvider": {},
Expand Down
2 changes: 2 additions & 0 deletions internal/lsp/code_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
const (
// SourceFormatAllTerraform is a Terraform specific format code action.
SourceFormatAllTerraform = "source.formatAll.terraform"
Quickfix = "quickfix"
)

type CodeActions map[lsp.CodeActionKind]bool
Expand All @@ -35,6 +36,7 @@ var (
// files to be formatted, but not terraform files (or vice versa).
SupportedCodeActions = CodeActions{
SourceFormatAllTerraform: true,
Quickfix: true,
}
)

Expand Down
1 change: 1 addition & 0 deletions internal/lsp/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func HCLDiagsToLSP(hclDiags hcl.Diagnostics, source string) []lsp.Diagnostic {
Severity: HCLSeverityToLSP(hclDiag.Severity),
Source: source,
Message: msg,
Data: hclDiag.Extra,
})

}
Expand Down