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

Format on save code action #625

Merged
merged 1 commit into from
Aug 23, 2021
Merged

Format on save code action #625

merged 1 commit into from
Aug 23, 2021

Conversation

jpogran
Copy link
Contributor

@jpogran jpogran commented Aug 18, 2021

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.

Fixes #327

@hashicorp-cla
Copy link

hashicorp-cla commented Aug 18, 2021

CLA assistant check
All committers have signed the CLA.

@jpogran jpogran force-pushed the f-format-code-action branch from 52902b5 to 45a349b Compare August 18, 2021 19:11
@jpogran jpogran changed the title (GH-327) Format on save code action (wip)(GH-327) Format on save code action Aug 18, 2021
@jpogran jpogran force-pushed the f-format-code-action branch from 45a349b to 161328c Compare August 18, 2021 19:36
@jpogran jpogran changed the title (wip)(GH-327) Format on save code action Format on save code action Aug 19, 2021
@jpogran jpogran force-pushed the f-format-code-action branch 3 times, most recently from 514ef3b to 25d7038 Compare August 19, 2021 18:08
@jpogran jpogran self-assigned this Aug 19, 2021
Copy link
Member

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave it spin and it worked nicely in both VS Code and Sublime Text. 👍🏻

Aside from my inline comments:

We should also add an RPC-level test for this new handler, similar to the one we have in

func TestLangServer_formatting_basic(t *testing.T) {

and perhaps one for tfvars
func TestLangServer_formatting_variables(t *testing.T) {

I'd probably omit the version check as it's already heavily tested in multiple places, including terraform-exec itself.

Lastly it would be great to get this new feature documented. The best way to do so is probably adding Code Actions section similar to Code Lens we have here https://github.com/hashicorp/terraform-ls/blob/main/docs/language-clients.md#code-lens and also briefly explaining the expected use case for the two kinds (source.formatAll and source.formatAll.terraform-ls) - one serving the needs of a client which has languageID-based configuration and the other one client which has global configuration (such as Sublime Text).

internal/langserver/handlers/initialize.go Outdated Show resolved Hide resolved
internal/langserver/handlers/formatting.go Outdated Show resolved Hide resolved
internal/langserver/handlers/code_action.go Outdated Show resolved Hide resolved
internal/langserver/handlers/code_action.go Outdated Show resolved Hide resolved
@jpogran jpogran force-pushed the f-format-code-action branch from a9a71d4 to 378f4a5 Compare August 20, 2021 17:34
docs/language-clients.md Outdated Show resolved Hide resolved
@jpogran jpogran marked this pull request as ready for review August 20, 2021 17:47
@radeksimko radeksimko added this to the v0.21.0 milestone Aug 23, 2021
Copy link
Member

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left you just some very minor inline suggestions, but the PR looks pretty good in general and ready for merging.

docs/language-clients.md Outdated Show resolved Hide resolved
docs/language-clients.md Outdated Show resolved Hide resolved
docs/language-clients.md Outdated Show resolved Hide resolved
internal/langserver/handlers/code_action.go Outdated Show resolved Hide resolved
internal/langserver/handlers/code_action_test.go Outdated Show resolved Hide resolved
internal/langserver/handlers/formatting.go Outdated Show resolved Hide resolved
internal/lsp/code_actions.go Outdated Show resolved Hide resolved
@jpogran jpogran force-pushed the f-format-code-action branch from 8f9941e to 2a359ff Compare August 23, 2021 13:24
This enables in the language server to perform one or more code actions on a range of text or a full document.

This adds a code action to format a file based on the `editor.codeActionsOnSave` setting or when a request has `source.formatAll` or `source.formatAll.terraform-ls`. This can either be a global setting or one specific to the terraform language.
@jpogran jpogran force-pushed the f-format-code-action branch from 2a359ff to a21c3ee Compare August 23, 2021 13:32
@jpogran jpogran merged commit 95fa734 into main Aug 23, 2021
@jpogran jpogran deleted the f-format-code-action branch August 23, 2021 13:52
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support code-action-on-save for triggering formatting
3 participants