terraform-ls icon indicating copy to clipboard operation
terraform-ls copied to clipboard

Support folding ranges

Open paultyng opened this issue 5 years ago • 18 comments
trafficstars

https://microsoft.github.io/language-server-protocol/specification#textDocument_foldingRange

There are generally three "kinds" of foldable ranges that LSP supports natively:

  • comments
  • imports
  • "regions" - to allow folding a user-labelled region via special syntax where supported - see also https://stackoverflow.com/questions/30067767/how-do-i-collapse-sections-of-code-in-visual-studio-code-for-windows/46597272#46597272

In order to support folding of comments we'd need the upstream HCL parser to return comments in the first place, these are currently ignored.

Imports could either be interpreted as required_providers block, or just not reported at all.

We can also report any additional kinds as the kind in the protocol spec is technically just string. For example gopls chooses to report all kinds of foldable ranges, such as function calls, struct declarations, switch case statements etc.: https://github.com/golang/tools/blob/8c34cc9cafff8f13c3f3ed95a11de309752c1c60/internal/lsp/source/folding_range.go#L68-L128

It is unclear however how any custom kinds would be surfaced to the user and whether a custom kind (as opposed to just empty string, like gopls uses) is even useful in practice.

paultyng avatar May 21 '20 01:05 paultyng

Apologies as I'm not too familiar with the LSP spec. Would this allow collapsing/expanding blocks, e.g., resource blocks, etc? If so, pretty cool!

amasover avatar Jun 12 '20 19:06 amasover

@amasover yep, exactly. I'd imagine the simplistic initial implementation will just be folding of root level blocks (resource, provider, data, variable, output, module, local, terraform, etc.).

paultyng avatar Jun 16 '20 13:06 paultyng

@paultyng I have try with this, But not working.

#region test
variable "test" {
  type        = bool
  default     = true
}
#endregion

PePoDev avatar Oct 01 '21 04:10 PePoDev

@PePoDev we haven't implemented region folding yet. When we do you'll see a PR and a line item in our changelog

jpogran avatar Oct 04 '21 14:10 jpogran

Hi,

I'm also interested in region folding, any news in regards of this? 🙂🤞

aleqsss avatar Jan 20 '22 12:01 aleqsss

Hi @aleqsss,

Sorry, but there hasn't been any progress on this. We are currently planning our roadmap for the first quarter, and it's already full of other issues we need to address. So at this point, it's tough for me to say when we can work on folding ranges.

dbanck avatar Jan 20 '22 13:01 dbanck

Understood, and thank you for the information!

aleqsss avatar Jan 20 '22 13:01 aleqsss

Hello, is this feature implemented?

garsiv1932 avatar Mar 25 '24 12:03 garsiv1932

@garsiv1932 Sorry, but we haven't implemented region folding yet

dbanck avatar Mar 25 '24 12:03 dbanck

Is it too difficult?

garsiv1932 avatar Mar 25 '24 13:03 garsiv1932

Hey @garsiv1932 , just to clarify region folding support was implemented in the Terraform VS Code extension in https://github.com/hashicorp/vscode-terraform/pull/924, so if you do want to use them now we do support them. If you use another editor, that has not been planned yet.

jpogran avatar Mar 25 '24 15:03 jpogran

Hey @garsiv1932 , just to clarify region folding support was implemented in the Terraform VS Code extension in hashicorp/vscode-terraform#924, so if you do want to use them now we do support them. If you use another editor, that has not been planned yet.

And how is the syntax, because i tried to put # regiion and #endRegion and didnt work.

Can You post the link of the vscode plugin too, only yo clarify if i AM using the dame one.

garsiv1932 avatar Mar 25 '24 17:03 garsiv1932

You can find our extension in the VS Code Marketplace or in the VS Code Extensions bar by typing 'hashicorp'.

The syntax is #region and #endregion:

https://github.com/hashicorp/terraform-ls/assets/272569/76881b3d-a5af-45bc-a196-28001088d5cf

jpogran avatar Mar 26 '24 13:03 jpogran

I had a conflict on my extensions, is working right now.

garsiv1932 avatar Mar 26 '24 15:03 garsiv1932