-
Notifications
You must be signed in to change notification settings - Fork 27
Description
Background
Code actions is one of the major features of LSP and there is a great number of use cases anticipated in the context of the Terraform LS: https://github.com/hashicorp/terraform-ls/issues?page=2&q=is%3Aopen+is%3Aissue+label%3AtextDocument%2FcodeAction
The hcl-lang library should therefore provide an abstraction layer to enable implementations of these actions so that servers can agree on how this is done.
While code actions can be useful on their own, a significant portion of them will relate to a diagnostic ("quick fixes"). Such actions can be built on top of PathDecoder.Validate()
and PathDecoder.ValidateFile()
.
Line 18 in 8067e8d
func (d *PathDecoder) Validate(ctx context.Context) (lang.DiagnosticsMap, error) { |
Line 45 in 8067e8d
func (d *PathDecoder) ValidateFile(ctx context.Context, filename string) (hcl.Diagnostics, error) { |
Proposal
- Introduce
Code
toDiagnostic
hcl#647 - Ensure existing validators in the
validator
package produce diagnostics with appropriate uniqueCode
- New
DecoderContext
field:
CodeActions []lang.CodeActionImpl
- New
decoder.Decoder
method:
func (d *Decoder) CodeActionsForRange(ctx context.Context, path lang.Path, rng hcl.Range) []lang.CodeAction
- New
decodercontext
package:
func CodeAction(ctx context.Context) CodeActionContext {
return ctx.Value(codeActionCtxKey{}).(CodeActionContext)
}
type CodeActionContext struct {
Diagnostics []lang.Diagnostic
Only []lang.CodeActionKind
TriggerKind lang.CodeActionTriggerKind
}
- New types in
lang
:
type CodeActionImpl interface {
CodeActionKind() CodeActionKind
CodeActions(ctx context.Context, path Path, rng hcl.Range) []CodeAction
// TODO: ResolveCodeAction(CodeAction) CodeAction
}
type CodeAction struct {
Title string
Kind CodeActionKind
Diagnostics hcl.Diagnostics
Edit Edit
Command Command
}
type editSigil struct{}
type Edit interface {
isEditImpl() editSigil
}
type FileEdits []TextEdit
func (fe FileEdits) isEditImpl() editSigil {
return editSigil{}
}
type CodeActionKind string
type CodeActionTriggerKind string
Implementation Prototype
https://github.com/hashicorp/hcl-lang/compare/f-code-actions-prototype
Notes
The initial scope of this work is to enable single-file scoped code actions. The proposed abstraction should allow later expansion for multi-file and multi-folder support though.