Skip to content

Add support for syntax based folding#7

Open
droustchev wants to merge 3 commits into
jvirtanen:mainfrom
droustchev:add-support-for-syntax-based-folding
Open

Add support for syntax based folding#7
droustchev wants to merge 3 commits into
jvirtanen:mainfrom
droustchev:add-support-for-syntax-based-folding

Conversation

@droustchev

Copy link
Copy Markdown

Hi there,

Thanks for this great plugin. I've been working in long Nomad HCLs and missed being able to fold sections based on syntax highlighting. I took the liberty to add this to your syntax file, and also made some other minor changes. Let me know if you'd be interested to incorporate them. I've tested my changes manually against a few of the fixtures in the official hcl repo. Here are some screenshots of the folding in action as well as the changes to the highlighting, albeit with my colorscheme:

Screen Shot 2020-07-10 at 2 58 18 PM

Screen Shot 2020-07-10 at 2 58 36 PM

@mbillow

mbillow commented Aug 23, 2020

Copy link
Copy Markdown

I was just about to implement and submit my own MR for block names and variables, awesome to see you beat me to it @droustchev! It looks good.

@jvirtanen Any ideas when you will have some time to review and merge this?

@jvirtanen

Copy link
Copy Markdown
Owner

Hi @droustchev and @mbillow! My apologies, I missed this pull request when it was initially opened. I will take a look at this today.

@jvirtanen jvirtanen left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thank you for the contribution, @droustchev! I think the folding itself looks good, especially is it is by default off as I believe it is. 👍

I have some comments below regarding the syntax highlighting changes as well as the indentation settings. I feel that as-is, they actually slightly degrade the user experience, and I would prefer to see a patch that only implements the folding with minimal changes, keeping the existing syntax highlighting intact. 🙂

Comment thread syntax/hcl.vim
set softtabstop=2
set shiftwidth=2
set expandtab
set smarttab

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Aren't these more of a personal preference than part of Vim runtime files for a language? 🤔

One could argue that HCL has a canonical form at least in Terraform with terraform fmt, but so does Go with go fmt yet the Vim runtime files for Go, I just checked, do not specify tabstop etc.

Comment thread syntax/hcl.vim
syn match hclBlock /^[^=]\+{/ contains=hclBlockName,hclString
syn match hclBlockName /\<[A-Za-z0-9_.\[\]*]\+\>/ nextgroup=hclString,hclBlock
syn region hclBlock start="{" end="}" fold transparent contains=hclBlock,hclVariable,hclString,hclInterpolation,hclComment
syn region hclBlock start="\[" end="\]" fold transparent contains=hclBlock,hclVariable,hclString,hclInterpolation,hclComment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think the change in hclBlock breaks the highlighting of hclAttributeName, which used to be unadorned (i.e. light gray) but after this change is highlighted the same way as hclVariable (i.e. yellow). This, I feel, makes the syntax highlighting worse, as a typical attribute name as well as a typical attribute value end up both being highlighted the same way.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think this change also breaks the highlighting of hclNumber, which is not included in hclBlock.

Comment thread syntax/hcl.vim
syn keyword hclConstant true false null

syn region hclInterpolation start=/\${/ end=/}/ contained contains=hclInterpolation
syn region hclInterpolation start=/\${/ end=/}/ contained contains=hclString,hclInterpolation,hclAttribute,hclAttributeName

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Do you have an example that has a string inside an interpolation? I admit I work almost exclusively with Terraform, but I have never seen such a construct in the wild. 🙂

In similar vein, is there a use case, which utilizes an hclAttribute (/^.*=/) inside an interpolation? 🤔

Finally, I think possibly the addition of hclAttributeName here breaks highlighting of variables (i.e. identifiers) inside interpolations, as they now become unadorned (i.e. light gray) instead of being highlighted the same way hclVariable (i.e. yellow). I think this is confusing, because it changes how variables are highlighted inside and outside interpolations.

@droustchev

Copy link
Copy Markdown
Author

Thanks for the feedback @jvirtanen. I have been a bit swamped at work lately, so haven't gotten around to look into making the requested changes. I hope to get around to them this weekend.

@antoineco

Copy link
Copy Markdown
Contributor

I just stumbled upon this and would also be interested in seeing syntax-based folding land in this plugin. @droustchev do you think you could push the requested changes?

@jvirtanen

Copy link
Copy Markdown
Owner

I could possibly try to look into this myself as well. This was my first real Vim plugin, and I just released another, which forced me to up my Vimscript game. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants