Skip to content

Commit

Permalink
(GH-327) Format on save code action
Browse files Browse the repository at this point in the history
This adds a code action to format a file based on the `editor.codeActionsOnSave` setting. This can either be a global setting or one specific to the terraform language.
  • Loading branch information
jpogran committed Aug 18, 2021
1 parent 0059475 commit 52902b5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
57 changes: 57 additions & 0 deletions internal/langserver/handlers/code_action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package handlers

import (
"context"

lsp "github.com/hashicorp/terraform-ls/internal/protocol"
)

func (h *logHandler) TextDocumentCodeAction(ctx context.Context, params lsp.CodeActionParams) ([]lsp.CodeAction, error) {
ca := make([]lsp.CodeAction, 0)

h.logger.Printf("got %+v", params)

for _, action := range params.Context.Only {
c := getCodeAction(action)
ca = append(ca, c)
}

return ca, nil
}

func getCodeAction(action lsp.CodeActionKind) lsp.CodeAction {
switch action {
case lsp.Source:
return lsp.CodeAction{
Title: "Format Document",
Kind: lsp.SourceFixAll,
IsPreferred: false,
Command: &lsp.Command{
Title: "Format Document",
Command: "editor.action.formatDocument",
},
}
case lsp.SourceFixAll:
return lsp.CodeAction{
Title: "Format Document",
Kind: lsp.SourceFixAll,
IsPreferred: false,
Command: &lsp.Command{
Title: "Format Document",
Command: "editor.action.formatDocument",
},
}
// case "formatModified":
// return lsp.CodeAction{
// Title: "Format Modified",
// Kind: "formatModified",
// IsPreferred: false,
// Command: &lsp.Command{
// Title: "Format Modified",
// Command: "editor.action.formatChanges",
// },
// }
default:
return lsp.CodeAction{}
}
}
9 changes: 9 additions & 0 deletions internal/langserver/handlers/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ func (svc *service) Initialize(ctx context.Context, params lsp.InitializeParams)
ResolveProvider: false,
TriggerCharacters: []string{".", "["},
},
CodeActionProvider: lsp.CodeActionOptions{
CodeActionKinds: []lsp.CodeActionKind{
lsp.Source,
lsp.SourceFixAll,
},

ResolveProvider: false,
WorkDoneProgressOptions: lsp.WorkDoneProgressOptions{},
},
DeclarationProvider: lsp.DeclarationOptions{},
DefinitionProvider: true,
CodeLensProvider: lsp.CodeLensOptions{},
Expand Down
12 changes: 12 additions & 0 deletions internal/langserver/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {

return handle(ctx, req, lh.TextDocumentHover)
},
"textDocument/codeAction": func(ctx context.Context, req *jrpc2.Request) (interface{}, error) {
err := session.CheckInitializationIsConfirmed()
if err != nil {
return nil, err
}

ctx = lsctx.WithClientCapabilities(ctx, cc)
ctx = lsctx.WithDocumentStorage(ctx, svc.fs)
ctx = lsctx.WithModuleFinder(ctx, svc.modMgr)

return handle(ctx, req, lh.TextDocumentCodeAction)
},
"textDocument/codeLens": func(ctx context.Context, req *jrpc2.Request) (interface{}, error) {
err := session.CheckInitializationIsConfirmed()
if err != nil {
Expand Down

0 comments on commit 52902b5

Please sign in to comment.