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

Fold import, var, and const blocks #1339

Merged
merged 3 commits into from
Jul 22, 2017
Merged
Show file tree
Hide file tree
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
27 changes: 24 additions & 3 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,8 @@ CTRL-t
:GoAddTags xml db
<
If [option] is passed it'll either add a new tag with an option or will
modify existing tags. An example of adding `omitempty` to all `json` fields
would be:
modify existing tags. An example of adding `omitempty` to all `json`
fields would be:
>
:GoAddTags json,omitempty
<
Expand Down Expand Up @@ -1565,7 +1565,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 @@ -1589,6 +1588,28 @@ By default "snakecase" is used. Current values are: ["snakecase",
>
let g:go_addtags_transform = 'snakecase'
<
*'g:go_fold_enable'*
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 we should add a line explaining that they get this when they also add set foldmethod=syntax to their vimrc


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