Skip to content

Commit d581656

Browse files
committed
syntax/go.vim: Various fixes
1 parent f18dae0 commit d581656

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

changelog.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
# Changelog
22

3+
4+
## Unreleased
5+
6+
### Changed
7+
8+
- Generate comments are now contained correctly
9+
10+
### Fixed
11+
12+
- Generic parentheses, brackets, and braces are no longer contained thus
13+
allowing them to match at the top-level, fixing some odd highlighting issues
14+
- Fixed `make()` of slice not highlighting other arguments correctly
15+
- Fixed `make()` of slice of functions not highlighting multi-value returns
16+
correctly
17+
- Fixed slice literals whose item type is a function with a return type
18+
- Fixed nested slice literals
19+
320
## Version 0.2.0 - 2022/12/31
421

522
### Changed

syntax/go.vim

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ syntax region goComment start=+//+ end=+$+ contains=@goCommentSpell,goCommen
179179
syntax region goComment start=+/\*+ end=+\*/+ contains=@goCommentSpell,goCommentTodo keepend
180180

181181
syntax keyword goCommentTodo contained TODO FIXME XXX TBD NOTE
182-
syntax region goGenerateComment start=+//go:generate+ end=+$+ containedin=goComment
182+
syntax region goGenerateComment start=+//go:generate+ end=+$+ contained containedin=goComment
183183

184184
hi link goCommentTodo Todo
185185
hi link goComment Comment
@@ -277,9 +277,9 @@ call s:HiConfig('goInvalidRuneLiteral', ['go_highlight_rune_literal_error'], #{o
277277

278278
" Simple Blocks {{{
279279

280-
syntax region goBracketBlock matchgroup=goBrackets start='\[' end='\]' contained transparent extend
281-
syntax region goParenBlock matchgroup=goParens start='(' end=')' contained transparent extend
282-
syntax region goBraceBlock matchgroup=goBraces start='{' end='}' contained transparent extend
280+
syntax region goBracketBlock matchgroup=goBrackets start='\[' end='\]' transparent extend
281+
syntax region goParenBlock matchgroup=goParens start='(' end=')' transparent extend
282+
syntax region goBraceBlock matchgroup=goBraces start='{' end='}' transparent extend
283283

284284
hi link goBraces Delimiter
285285
hi link goBrackets Delimiter
@@ -427,12 +427,16 @@ syntax region goMapKeyType matchgroup=goMapBrackets start='\[' end='\]' contain
427427
syntax match goSliceOrArrayType /\[\%(\d\+\|\.\.\.\)\?\]/ contained contains=goNumber,goDot skipwhite nextgroup=@goType
428428

429429
" A lookbehind is used to distinguish a slice/array literal with slice indexing
430-
syntax match goSliceOrArrayLiteral /\k\@1<!\[[0-9.]*\]\ze\%(\*\|\K\|\[\|(\)/ contained contains=goNumber,goDot skipwhite nextgroup=goSliceItemType
430+
syntax match goSliceOrArrayLiteral /\k\@1<!\[[0-9.]*\]\ze\%(\*\|\K\|\[\|(\)/ contained contains=goNumber,goDot skipwhite nextgroup=goSliceLiteralType
431431

432-
" Only look to the end of the line for the item type, and let slices etc. extend
433-
" across lines as necessary. Note the first '(' is to match the first paren
434-
" around the type, which is then extended by goTypeParens.
435-
syntax match goSliceItemType /(\|\%(\%(interface\|struct\)\s*{\|[^{()]\)\+/ contained contains=@goType skipwhite nextgroup=goSliceItems
432+
" goSliceOrArrayLiteralType allows matching complex types for slice literals
433+
" such as named return parameters when using a slice of functions without
434+
" parentheses, e.g. "[]func() (foo, bar int) { f1, f2, f3 }", which is
435+
" technically valid, albeit hard to read. The use of a region allows the
436+
" contained matches (goSliceLiteralTypeMatch) to extend the region as necessary,
437+
" allowing the type to contain braces, such as "[]struct{X, Y int}{ ... }"
438+
syntax region goSliceLiteralType start='\S' end='\ze{\|$' contained contains=goSliceLiteralTypeMatch skipwhite skipnl nextgroup=goSliceItems
439+
syntax match goSliceLiteralTypeMatch /\%(\%(interface\|struct\)\s*{\|[^{]\)\+/ contained contains=@goType
436440

437441
syntax region goSliceItems matchgroup=goSliceBraces start='{' end='}' contained contains=goStructLiteralBlock,@goExpr,goComment
438442

@@ -493,7 +497,6 @@ syntax match goFuncName /\K\k*/ contained skipwhite nextgroup=goFuncTypeParams,g
493497

494498
syntax region goFuncTypeParams matchgroup=goTypeParamBrackets start='\[' end='\]' contained contains=goTypeParam,goComma,goComment nextgroup=goFuncParams
495499

496-
" TODO: is skipempty needed?
497500
syntax match goTypeParam /\K\k*/ contained skipwhite skipempty nextgroup=goTypeParamComma,goTypeConstraint
498501
syntax match goTypeParamComma /,/ contained skipwhite skipempty nextgroup=goTypeParam
499502

@@ -503,7 +506,10 @@ syntax match goTypeParamComma /,/ contained skipwhite skipempty nextgroup=go
503506
syntax region goTypeConstraint start='\s'ms=e+1 end=/[,\]]/me=s-1 contained contains=@goType,goTypeConstraintSymbols
504507
syntax match goTypeConstraintSymbols /[~|]/ contained
505508

506-
syntax match goFuncReturnType /\s*\zs(\@1<!\%(\%(interface\|struct\)\s*{\|[^{]\)\+{\@1<!/ contained contains=@goType skipwhite skipempty nextgroup=goFuncBlock
509+
" This is odd, but the \s*\zs at the start seems to ensure that the (\@1<!
510+
" negative lookbehind works as desired (i.e. to not steal a match from
511+
" goFuncMultiReturn); look into this further and try to remove this.
512+
syntax match goFuncReturnType /\s*\zs(\@1<!\%(\%(interface\|struct\)\s*{\|[^{]\)\+/ contained contains=@goType skipwhite skipempty nextgroup=goFuncBlock
507513

508514
syntax region goFuncParams matchgroup=goFuncParens start='(' end=')' contained contains=goParam,goComma,goComment skipwhite nextgroup=goFuncReturnType,goFuncMultiReturn,goFuncBlock
509515
syntax region goFuncMultiReturn matchgroup=goFuncMultiReturnParens start='(' end=')' contained contains=goNamedReturnValue,goComma,goComment skipwhite skipempty nextgroup=goFuncBlock
@@ -569,7 +575,10 @@ syntax match goEmbeddedType /\*\?\K\k*\%(\.\K\k*\)\?\%#\@1<!$/ containe
569575
syntax match goStructLiteral /\v\K\k*\ze%(\{|\[\s*\n?%(,\n|[^\[\]]|\[\s*\n?%(,\n|[^\[\]]|\[[^\[\]]*\])*\])*\]\{)/ contained nextgroup=goStructLiteralTypeArgs,goStructLiteralBlock
570576
syntax region goStructLiteralTypeArgs matchgroup=goTypeParamBrackets start='\[' end='\]' contained contains=@goType,goUnderscore,goComma,goComment nextgroup=goStructLiteralBlock
571577

572-
GoFoldStruct syntax region goStructLiteralBlock matchgroup=goStructBraces start='{' end='}' contained contains=goStructLiteralField,goComma,@goExpr,goComment
578+
" goStructLiteralBlock contains itself to 1) prevent weird highlighting while
579+
" typing, and 2) allow slice literals of slices of structs to highlight
580+
" correctly
581+
GoFoldStruct syntax region goStructLiteralBlock matchgroup=goStructBraces start='{' end='}' contained contains=goStructLiteralField,goComma,@goExpr,goComment,goStructLiteralBlock
573582

574583
syntax match goStructLiteralField /\<\K\k*\ze:/ contained nextgroup=goStructLiteralColon
575584
syntax match goStructLiteralColon /:/ contained
@@ -609,10 +618,11 @@ call s:HiConfig('goStructTypeField', ['go_highlight_struct_type_fields'], #{d
609618
syntax keyword goBuiltins append cap close complex copy delete imag len panic print println real recover contained skipwhite nextgroup=goFuncCallArgs
610619

611620
syntax keyword goMakeBuiltin make contained skipwhite nextgroup=goMakeBlock
612-
syntax region goMakeBlock matchgroup=goFuncCallParens start='(' end=')' contained contains=@goType,@goExpr,goComment
621+
syntax region goMakeBlock matchgroup=goFuncCallParens start='(' end=')' contained contains=@goType,goMakeArguments,goComment
622+
syntax region goMakeArguments start=',' end='\ze)' contained contains=@goExpr,gComment
613623

614624
syntax keyword goNewBuiltin new contained skipwhite nextgroup=goNewBlock
615-
syntax region goNewBlock matchgroup=goFuncCallParens start='(' end=')' contained contains=@goType,@goExpr,goComment
625+
syntax region goNewBlock matchgroup=goFuncCallParens start='(' end=')' contained contains=@goType,goComment
616626

617627
hi link goBuiltins Special
618628
hi link goMakeBuiltin goBuiltins

0 commit comments

Comments
 (0)