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

Various improvements to syntax highlighting. #917

Merged
merged 4 commits into from
Jun 29, 2016
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ To change it:
let g:go_highlight_functions = 1
let g:go_highlight_methods = 1
let g:go_highlight_fields = 1
let g:go_highlight_structs = 1
let g:go_highlight_interfaces = 1
let g:go_highlight_types = 1
Copy link
Owner

Choose a reason for hiding this comment

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

This is a breaking change. Is there a way to not break this? I don't want people change their vimrc for no reason if possible :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that is quite unfortunate. I don't see an efficient way to separate parsing of type Name struct from type Name interface. Let me think about it a bit more. Maybe it can be done using syn-region syntax.

On the other hand, why do we need to distinguish these two cases?

Copy link
Owner

Choose a reason for hiding this comment

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

It was like that previously. We don't need to distinguish at all, it is just for backwards compatibility. But if you believe that making it backwards compatibility comes with increased complexity, I'm in favor of breaking it, so it means this change would be ok.

let g:go_highlight_operators = 1
let g:go_highlight_build_constraints = 1
```
Expand Down
12 changes: 3 additions & 9 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1118,23 +1118,17 @@ Highlights method names. By default it's disabled. >

let g:go_highlight_methods = 0
<
*'g:go_highlight_structs'*
*'g:go_highlight_types'*

Highlights struct names. By default it's disabled. >
Highlights struct and interface names. By default it's disabled. >

let g:go_highlight_structs = 0
let g:go_highlight_types = 0
<
*'g:go_highlight_fields'*

Highlights field names. By default it's disabled. >

let g:go_highlight_fields = 0
<
*'g:go_highlight_interfaces'*

Highlights interface names. By default it's disabled. >

let g:go_highlight_interfaces = 0
<
*'g:go_highlight_build_constraints'*

Expand Down
60 changes: 27 additions & 33 deletions syntax/go.vim
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,8 @@ if !exists("g:go_highlight_fields")
let g:go_highlight_fields = 0
endif

if !exists("g:go_highlight_structs")
let g:go_highlight_structs = 0
endif

if !exists("g:go_highlight_interfaces")
let g:go_highlight_interfaces = 0
if !exists("g:go_highlight_types")
let g:go_highlight_types = 0
endif

if !exists("g:go_highlight_build_constraints")
Expand All @@ -98,12 +94,10 @@ endif
syn case match

syn keyword goDirective package import
syn keyword goDeclaration var const type
syn keyword goDeclType struct interface
syn keyword goDeclaration var const

hi def link goDirective Statement
hi def link goDeclaration Keyword
hi def link goDeclType Keyword

" Keywords within functions
syn keyword goStatement defer go goto return break continue fallthrough
Expand All @@ -129,10 +123,6 @@ hi def link goUnsignedInts Type
hi def link goFloats Type
hi def link goComplexes Type

" Treat func specially: it's a declaration at the start of a line, but a type
" elsewhere. Order matters here.
syn match goDeclaration /\<func\>/


" Predefined functions and values
syn match goBuiltins /\<\v(append|cap|close|complex|copy|delete|imag|len)\ze\(/
Expand Down Expand Up @@ -301,38 +291,42 @@ hi def link goOperator Operator

" Functions;
if g:go_highlight_functions != 0
syn match goFunction /\(func\s\+\)\@<=\w\+\((\)\@=/
syn match goFunction /\()\s\+\)\@<=\w\+\((\)\@=/
syn match goDeclaration /\<func\>/ nextgroup=goReceiver,goFunction skipwhite skipnl
syn match goReceiver /([^),]\+)/ contained nextgroup=goFunction contains=goReceiverType skipwhite skipnl
syn match goReceiverType /\(\s\|*\)\w\+)/hs=s+1,he=e-1 contained
syn match goFunction /\w\+/ contained
else
Copy link
Owner

Choose a reason for hiding this comment

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

Why do we have the else clause here? If it's not enabled we should not highlight anything at all

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to pull down syn match goDeclaration /\<func\>/ from the top of the script, since this condition if true adds extra parsing rules to it.

syn match goDeclaration /\<func\>/
endif
hi def link goReceiverType Type
hi def link goFunction Function

" Methods;
if g:go_highlight_methods != 0
syn match goMethod /\(\.\)\@<=\w\+\((\)\@=/
syn match goMethod /\.\w\+(/hs=s+1,he=e-1
endif
hi def link goMethod Type

" Fields;
if g:go_highlight_fields != 0
syn match goField /\(\.\)\@<=\a\+\([\ \n\r\:\)]\)\@=/
syn match goVarArgs /\.\.\.\w\+\>/
syn match goField /\.\a\+\([\ \n\r\:\)\[]\)\@=/hs=s+1
endif
hi def link goField Type

" Structs;
if g:go_highlight_structs != 0
syn match goStruct /\(.\)\@<=\w\+\({\)\@=/
syn match goStructDef /\(type\s\+\)\@<=\w\+\(\s\+struct\s\+{\)\@=/
endif
hi def link goStruct Function
hi def link goStructDef Function

" Interfaces;
if g:go_highlight_interfaces != 0
syn match goInterface /\(.\)\@<=\w\+\({\)\@=/
syn match goInterfaceDef /\(type\s\+\)\@<=\w\+\(\s\+interface\s\+{\)\@=/
hi def link goField Identifier

" Structs & Interfaces;
if g:go_highlight_types != 0
syn match goTypeConstructor /\<\w\+{/he=e-1
syn match goTypeDecl /\<type\>/ nextgroup=goTypeName skipwhite skipnl
syn match goTypeName /\w\+/ contained nextgroup=goDeclType skipwhite skipnl
syn match goDeclType /\<interface\|struct\>/ contained skipwhite skipnl
else
syn keyword goDeclType struct interface
endif
hi def link goInterface Function
hi def link goInterfaceDef Function
hi def link goTypeConstructor Type
hi def link goTypeName Type
hi def link goTypeDecl Keyword
hi def link goDeclType Keyword

" Build Constraints
if g:go_highlight_build_constraints != 0
Expand Down