-
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
3 changed files
with
78 additions
and
0 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,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{} | ||
} | ||
} |
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