-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
lsp "github.com/hashicorp/terraform-ls/internal/protocol" | ||
) | ||
|
||
func (h *logHandler) TextDocumentCodeAction(ctx context.Context, params lsp.CodeActionParams) ([]lsp.CodeAction, error) { | ||
var ca []lsp.CodeAction | ||
var wanted map[lsp.CodeActionKind]bool | ||
|
||
// TODO: get this from initialize | ||
supported := map[lsp.CodeActionKind]bool{ | ||
`source`: true, | ||
`source.fixAll`: true, | ||
`source.formatAll`: true, | ||
`source.formatAll.terraform-ls`: true, | ||
} | ||
|
||
// return early if we don't support any code actions | ||
if len(supported) == 0 { | ||
return nil, nil | ||
} | ||
|
||
// if Only is empty, assume that the client wants all code actions | ||
// else build mapping of requested and determine if supported | ||
if len(params.Context.Only) == 0 { | ||
wanted = supported | ||
} else { | ||
wanted = make(map[lsp.CodeActionKind]bool) | ||
for _, only := range params.Context.Only { | ||
wanted[only] = supported[only] | ||
} | ||
} | ||
|
||
if len(wanted) == 0 { | ||
return nil, fmt.Errorf("could not find a supported code action to execute for %s, wanted %v", | ||
params.TextDocument.URI, params.Context.Only) | ||
} | ||
|
||
for action, _ := range wanted { | ||
switch action { | ||
case lsp.Source, lsp.SourceFixAll, `source.formatAll`, `source.formatAll.terraform-ls`: | ||
c, err := terraformFmt(ctx, h, params.TextDocument.URI) | ||
if err != nil { | ||
continue | ||
} | ||
ca = append(ca, c) | ||
} | ||
} | ||
|
||
return ca, nil | ||
} | ||
|
||
func terraformFmt(ctx context.Context, h *logHandler, uri lsp.DocumentURI) (lsp.CodeAction, error) { | ||
var ca lsp.CodeAction | ||
|
||
edits, err := formatDocument(ctx, h, uri) | ||
if err != nil { | ||
return ca, err | ||
} | ||
|
||
we := lsp.WorkspaceEdit{ | ||
Changes: map[string][]lsp.TextEdit{ | ||
string(uri): edits, | ||
}, | ||
} | ||
|
||
x := lsp.CodeAction{ | ||
Title: "Format Document", | ||
Kind: lsp.SourceFixAll, | ||
IsPreferred: false, | ||
Edit: we, | ||
} | ||
return x, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters