Skip to content

Commit

Permalink
Merge pull request #3637 from matthewhughes934/fix-package-comment-to…
Browse files Browse the repository at this point in the history
…o-broad

Fix `goPackageComment` highlighting too broad
  • Loading branch information
bhcleek authored Jan 29, 2024
2 parents 535355d + d1c36cc commit c63ed98
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
71 changes: 71 additions & 0 deletions autoload/go/highlight_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,77 @@ function! Test_gomodGoVersion() abort
endtry
endfunc


function! Test_goPackageComment_highlight() abort
try
syntax on

let g:go_gopls_enabled = 0
let l:wd = getcwd()
let l:dir = gotest#write_file('packageTest.go', [
\ '// this is a package comment',
\ 'package somepkg',
\ '',
\ '/*',
\ 'this is also a package comment',
\ '*/',
\ 'package somepkg',
\ '',
\ 'var (',
\ '\t// this is a regular comment',
\ '\tpackages []string',
\ ')',
\ ''])

let l:package_comment_lines = [1, 4, 5, 6]
for l:lineno in package_comment_lines
let l:line = getline(l:lineno)
let l:idx = 0
let l:end = len(l:line) - 1
let l:col = 1

while l:idx <= l:end
call cursor(l:lineno, l:col)
let l:synname = synIDattr(synID(l:lineno, l:col, 1), 'name')
let l:errlen = len(v:errors)

call assert_equal('goPackageComment', l:synname, 'version on line ' . l:lineno . ' and col ' . l:col)

if l:errlen < len(v:errors)
break
endif

let l:col += 1
let l:idx += 1
endwhile
endfor

let l:lineno = 10
let l:col = col([l:lineno, '$']) - 1
let l:end_idx = stridx(l:line, '\t')
let l:idx = len(l:line - 1)

while l:idx > l:end_idx
call cursor(l:lineno, l:col)
let l:synname = synIDattr(synID(l:lineno, l:col, 1), 'name')
let l:errlen = len(v:errors)

call assert_equal('goComment', l:synname, 'version on line ' . l:lineno . ' and col ' . l:col)

if l:errlen < len(v:errors)
break
endif

let l:col -= 1
let l:idx -= 1
endwhile

finally
call go#util#Chdir(l:wd)
call delete(l:dir, 'rf')
endtry
endfunc

" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
Expand Down
8 changes: 4 additions & 4 deletions syntax/go.vim
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,12 @@ if go#config#HighlightBuildConstraints() || go#config#FoldEnable('package_commen
" matched as comments to avoid looking like working build constraints.
" The he, me, and re options let the "package" itself be highlighted by
" the usual rules.
exe 'syn region goPackageComment start=/\v(\/\/.*\n)+\s*package/'
\ . ' end=/\v\n\s*package/he=e-7,me=e-7,re=e-7'
exe 'syn region goPackageComment start=/\v(\/\/.*\n)+\s*package\s/'
\ . ' end=/\v\n\s*package\s/he=e-8,me=e-8,re=e-8'
\ . ' contains=@goCommentGroup,@Spell'
\ . (go#config#FoldEnable('package_comment') ? ' fold' : '')
exe 'syn region goPackageComment start=/\v^\s*\/\*.*\n(.*\n)*\s*\*\/\npackage/'
\ . ' end=/\v\*\/\n\s*package/he=e-7,me=e-7,re=e-7'
exe 'syn region goPackageComment start=/\v^\s*\/\*.*\n(.*\n)*\s*\*\/\npackage\s/'
\ . ' end=/\v\*\/\n\s*package\s/he=e-8,me=e-8,re=e-8'
\ . ' contains=@goCommentGroup,@Spell'
\ . (go#config#FoldEnable('package_comment') ? ' fold' : '')
hi def link goPackageComment Comment
Expand Down

0 comments on commit c63ed98

Please sign in to comment.