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

Introduce Code Actions #363

Open
6 tasks
Tracked by #1575
radeksimko opened this issue Jan 9, 2024 · 0 comments
Open
6 tasks
Tracked by #1575

Introduce Code Actions #363

radeksimko opened this issue Jan 9, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@radeksimko
Copy link
Member

radeksimko commented Jan 9, 2024

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().

func (d *PathDecoder) Validate(ctx context.Context) (lang.DiagnosticsMap, error) {

func (d *PathDecoder) ValidateFile(ctx context.Context, filename string) (hcl.Diagnostics, error) {

Proposal

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant