Skip to content

Commit

Permalink
Fold import, var, and const blocks (#1339)
Browse files Browse the repository at this point in the history
* Fold import, var, and const blocks

This adds `foldmethod=syntax` folding for `import`, `var`, and `const` blocks.

I added a new `g:go_fold_enable` setting to selectively enable which regions get
folded. I find this useful personally as I only want to fold `import` blocks.

Future extension to this might be:

- `struct` and `interface`, to selectively fold `type foo struct { .. }` and
  `type foo interface { .. }` definitions;

- `package`, to fold the package-level comment.

But that's another PR for another day ;-)

Fixes: #963
  • Loading branch information
arp242 authored and fatih committed Jul 22, 2017
1 parent 3b6270e commit d5f5505
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
23 changes: 22 additions & 1 deletion doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,6 @@ default it's 60 seconds. Must be in milliseconds.
>
let g:go_statusline_duration = 60000
<

*'g:go_addtags_transform'*

Sets the `transform` option for `gomodifytags` when using |:GoAddTags| or if
Expand All @@ -1608,6 +1607,28 @@ By default "snakecase" is used. Current values are: ["snakecase",
>
let g:go_addtags_transform = 'snakecase'
<
*'g:go_fold_enable'*

Control syntax-based folding which takes effect when 'foldmethod' is set to
`syntax`.
You can enable specific fold regions by setting an array. Possible values are:

- "block" `{` .. `}` blocks.
- "import" `import` block.
- "varconst" `var` and `const` blocks.

By default they're all enabled:
>
let g:go_fold_enable = ['block', 'import', 'varconst']
<
Enable folding of only imports:
>
let g:go_fold_enable = ['import']
<
Disable everything (same as not setting 'foldmethod' to `syntax`):
>
let g:go_fold_enable = []
<

==============================================================================
DEVELOPMENT *go-development*
Expand Down
51 changes: 47 additions & 4 deletions syntax/go.vim
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,32 @@ if !exists("g:go_highlight_generate_tags")
let g:go_highlight_generate_tags = 0
endif

let s:fold_block = 1
let s:fold_import = 1
let s:fold_varconst = 1
if exists("g:go_fold_enable")
if index(g:go_fold_enable, 'block') == -1
let s:fold_block = 0
endif
if index(g:go_fold_enable, 'import') == -1
let s:fold_import = 0
endif
if index(g:go_fold_enable, 'varconst') == -1
let s:fold_varconst = 0
endif
endif

syn case match

syn keyword goDirective package import
syn keyword goDeclaration var const
syn keyword goPackage package
syn keyword goImport import contained
syn keyword goVar var contained
syn keyword goConst const contained

hi def link goDirective Statement
hi def link goPackage Statement
hi def link goImport Statement
hi def link goVar Keyword
hi def link goConst Keyword
hi def link goDeclaration Keyword

" Keywords within functions
Expand Down Expand Up @@ -189,8 +209,31 @@ syn region goCharacter start=+'+ skip=+\\\\\|\\'+ end=+'+ contains=
hi def link goCharacter Character

" Regions
syn region goBlock start="{" end="}" transparent fold
syn region goParen start='(' end=')' transparent
if s:fold_block
syn region goBlock start="{" end="}" transparent fold
else
syn region goBlock start="{" end="}" transparent
endif

" import
if s:fold_import
syn region goImport start='import (' end=')' transparent fold contains=goImport,goString
else
syn region goImport start='import (' end=')' transparent contains=goImport,goString
endif

" var, const
if s:fold_varconst
syn region goVar start='var (' end=')' transparent fold contains=ALLBUT,goParen,goBlock
syn region goConst start='const (' end=')' transparent fold contains=ALLBUT,goParen,goBlock
else
syn region goVar start='var (' end=')' transparent contains=ALLBUT,goParen,goBlock
syn region goConst start='const (' end=')' transparent contains=ALLBUT,goParen,goBlock
endif

" Single-line var, const, and import.
syn match goSingleDecl /\(import\|var\|const\) [^(]\@=/ contains=goImport,goVar,goConst

" Integers
syn match goDecimalInt "\<-\=\d\+\%([Ee][-+]\=\d\+\)\=\>"
Expand Down

3 comments on commit d5f5505

@evanphx
Copy link

Choose a reason for hiding this comment

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

One issue with this change is that it changes package and import to Statements, which almost no colorschemes have color for. I changed them back to Keyword locally to fix that.

@arp242
Copy link
Contributor Author

@arp242 arp242 commented on d5f5505 Jul 24, 2017

Choose a reason for hiding this comment

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

I'm not sure if I follow what you mean @evanphx? They were always Statement? Previously it was:

syn keyword     goDirective         package import
hi def link     goDirective         Statement

@evanphx
Copy link

Choose a reason for hiding this comment

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

Oh strange. Yes yes, I must be something elsewhere! Sorry about that!

Please sign in to comment.