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

Add support for syntax based folding #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions syntax/hcl.vim
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ syn match hclNumber /\<0[xX]\x\+\>/

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
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.


syn region hclComment start=/\/\// end=/$/ contains=hclTodo
syn region hclComment start=/\#/ end=/$/ contains=hclTodo
Expand All @@ -41,11 +41,13 @@ syn region hclComment start=/\/\*/ end=/\*\// contains=hclTodo
syn match hclAttributeName /\w\+/ contained
syn match hclAttribute /^.*=/ contains=hclAttributeName,hclComment,hclString

syn match hclBlockName /\w\+/ contained
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
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
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.


syn keyword hclTodo TODO FIXME XXX DEBUG NOTE contained

hi def link hclBlockName Statement
hi def link hclVariable PreProc
hi def link hclFunction Function
hi def link hclKeyword Keyword
Expand All @@ -57,4 +59,11 @@ hi def link hclInterpolation PreProc
hi def link hclComment Comment
hi def link hclTodo Todo

syn sync fromstart

let b:current_syntax = 'hcl'
set tabstop=2
set softtabstop=2
set shiftwidth=2
set expandtab
set smarttab
Copy link
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.