Skip to content

Commit

Permalink
syntax: refactor float matches
Browse files Browse the repository at this point in the history
* Add tests for float and imaginary matches.
* Match floats that start with or end with a decimal point with no
  digits preceding or following, respectively.
* Match hexadecimal floats.
* Match imaginary binary, octal, and hexadecimal numbers.

Fixes fatih#3430
  • Loading branch information
bhcleek committed Aug 8, 2022
1 parent 4336105 commit 8fc3568
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 16 deletions.
54 changes: 47 additions & 7 deletions autoload/go/highlight_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,49 @@ endfunc
function! Test_numeric_literal_highlight() abort
syntax on

let tests = {
let l:tests = {
\ 'lone zero': {'group': 'goDecimalInt', 'value': '0'},
\ 'float loan zero': {'group': 'goFloat', 'value': '0.'},
\ 'integer': {'group': 'goDecimalInt', 'value': '1234567890'},
\ 'integerGrouped': {'group': 'goDecimalInt', 'value': '1_234_567_890'},
\ 'hexadecimal': {'group': 'goHexadecimalInt', 'value': '0x0123456789abdef'},
\ 'hexadecimalGrouped': {'group': 'goHexadecimalInt', 'value': '0x012_345_678_9ab_def'},
\ 'heXadecimal': {'group': 'goHexadecimalInt', 'value': '0X0123456789abdef'},
\ 'hexadecimal': {'group': 'goHexadecimalInt', 'value': '0x0123456789abcdef'},
\ 'hexadecimalGrouped': {'group': 'goHexadecimalInt', 'value': '0x012_345_678_9abc_def'},
\ 'heXadecimal': {'group': 'goHexadecimalInt', 'value': '0X0123456789abcdef'},
\ 'hexadecimalFloatUp': {'group': 'goHexadecimalFloat', 'value': '0x0123456789abcdef.0123456789abcdefp2'},
\ 'hexadecimalFloatDown': {'group': 'goHexadecimalFloat', 'value': '0x0123456789abcdef.0123456789abcdefp-2'},
\ 'octal': {'group': 'goOctalInt', 'value': '01234567'},
\ 'octalPrefix': {'group': 'goOctalInt', 'value': '0o1234567'},
\ 'octalGrouped': {'group': 'goOctalInt', 'value': '0o1_234_567'},
\ 'OctalPrefix': {'group': 'goOctalInt', 'value': '0O1234567'},
\ 'binaryInt': {'group': 'goBinaryInt', 'value': '0b0101'},
\ 'binaryIntGrouped': {'group': 'goBinaryInt', 'value': '0b_01_01'},
\ 'BinaryInt': {'group': 'goBinaryInt', 'value': '0B0101'},
\ 'floatFractionalOnly': {'group': 'goFloat', 'value': '.1'},
\ 'hexadecimalFloatFractionalOnly': {'group': 'goHexadecimalFloat', 'value': '0x.1'},
\ 'floatIntegerOnly': {'group': 'goFloat', 'value': '1e6'},
\ }

for kv in items(tests)
let l:actual = s:numericHighlightGroupInAssignment(kv[0], kv[1].value)
call assert_equal(kv[1].group, l:actual, kv[0])
for l:kv in items(copy(l:tests))
let l:value = deepcopy(l:kv[1])
let l:value.group = substitute(l:value.group, 'go', 'goImaginary', '')
let l:value.group = substitute(l:value.group, 'Int$', '', '')
let l:value.value = printf('%si', l:value.value)
let l:tests[printf('imaginary %s', l:kv[0])] = l:value
endfor

for l:kv in items(copy(l:tests))
let l:value = deepcopy(l:kv[1])
let l:value.value = printf('-%s', l:value.value)
let l:tests[printf('negative %s', l:kv[0])] = l:value
endfor

for l:kv in items(tests)
let l:actual = s:numericHighlightGroupInAssignment(l:kv[0], l:kv[1].value)
call assert_equal(l:kv[1].group, l:actual, l:kv[1].value)

let l:groupName = 'goString'
let l:actual = s:stringHighlightGroupInAssignment(l:kv[0], l:kv[1].value)
call assert_equal('goString', l:actual, printf('"%s"', l:kv[1].value))
endfor
endfunction

Expand Down Expand Up @@ -165,6 +189,22 @@ function! s:numericHighlightGroupInAssignment(testname, value)
endtry
endfunction

function! s:stringHighlightGroupInAssignment(testname, value)
let l:dir = gotest#write_file(printf('numeric/%s.go', a:testname), [
\ 'package numeric',
\ '',
\ printf("var v = \"%s\x1f\"", a:value),
\ ])

try
let l:pos = getcurpos()
let l:actual = synIDattr(synID(l:pos[1], l:pos[2], 1), 'name')
return l:actual
finally
call delete(l:dir, 'rf')
endtry
endfunction

function! s:numericHighlightGroupInSliceElement(testname, value)
let l:dir = gotest#write_file(printf('numeric/slice-element/%s.go', a:testname), [
\ 'package numeric',
Expand Down
54 changes: 45 additions & 9 deletions syntax/go.vim
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,55 @@ hi def link goBinaryError Error
hi def link Integer Number

" Floating point
syn match goFloat "\<-\=\d\+\.\d*\%([Ee][-+]\=\d\+\)\=\>"
syn match goFloat "\<-\=\.\d\+\%([Ee][-+]\=\d\+\)\=\>"
"float_lit = decimal_float_lit | hex_float_lit .
"
"decimal_float_lit = decimal_digits "." [ decimal_digits ] [ decimal_exponent ] |
" decimal_digits decimal_exponent |
" "." decimal_digits [ decimal_exponent ] .
"decimal_exponent = ( "e" | "E" ) [ "+" | "-" ] decimal_digits .
"
"hex_float_lit = "0" ( "x" | "X" ) hex_mantissa hex_exponent .
"hex_mantissa = [ "_" ] hex_digits "." [ hex_digits ] |
" [ "_" ] hex_digits |
" "." hex_digits .
"hex_exponent = ( "p" | "P" ) [ "+" | "-" ] decimal_digits .
" decimal floats with a decimal point
syn match goFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)\.\%(\%(\%(\d\|\d_\d\)\+\)\=\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\=\>\)\="
syn match goFloat "\s\zs-\=\.\%(\d\|\d_\d\)\+\%(\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\>\)\="
" decimal floats without a decimal point
syn match goFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)[Ee][-+]\=\%(\d\|\d_\d\)\+\>"
" hexadecimal floats with a decimal point
syn match goHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+\.\%(\%(\x\|\x_\x\)\+\)\=\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=\>"
syn match goHexadecimalFloat "\<-\=0[xX]\.\%(\x\|\x_\x\)\+\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=\>"
" hexadecimal floats without a decimal point
syn match goHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+[Pp][-+]\=\%(\d\|\d_\d\)\+\>"

hi def link goFloat Float
hi def link goHexidecimalFloat Float

" Imaginary literals
syn match goImaginary "\<-\=\d\+i\>"
syn match goImaginary "\<-\=\d\+[Ee][-+]\=\d\+i\>"
syn match goImaginaryFloat "\<-\=\d\+\.\d*\%([Ee][-+]\=\d\+\)\=i\>"
syn match goImaginaryFloat "\<-\=\.\d\+\%([Ee][-+]\=\d\+\)\=i\>"

hi def link goImaginary Number
hi def link goImaginaryFloat Float
syn match goImaginaryDecimal "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)i\>"
syn match goImaginaryHexadecimal "\<-\=0[xX]_\?\%(\x\|\x_\x\)\+i\>"
syn match goImaginaryOctal "\<-\=0[oO]\?_\?\%(\o\|\o_\o\)\+i\>"
syn match goImaginaryBinary "\<-\=0[bB]_\?\%([01]\|[01]_[01]\)\+i\>"

" imaginary decimal floats with a decimal point
syn match goImaginaryFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)\.\%(\%(\%(\d\|\d_\d\)\+\)\=\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\=\)\=i\>"
syn match goImaginaryFloat "\s\zs-\=\.\%(\d\|\d_\d\)\+\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\=i\>"
" imaginary decimal floats without a decimal point
syn match goImaginaryFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)[Ee][-+]\=\%(\d\|\d_\d\)\+i\>"
" imaginary hexadecimal floats with a decimal point
syn match goImaginaryHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+\.\%(\%(\x\|\x_\x\)\+\)\=\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=i\>"
syn match goImaginaryHexadecimalFloat "\<-\=0[xX]\.\%(\x\|\x_\x\)\+\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=i\>"
" imaginary hexadecimal floats without a decimal point
syn match goImaginaryHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+[Pp][-+]\=\%(\d\|\d_\d\)\+i\>"

hi def link goImaginaryDecimal Number
hi def link goImaginaryHexaecimal Number
hi def link goImaginaryOctal Number
hi def link goImaginaryBinary Number
hi def link goImaginaryFloat Float
hi def link goImaginaryHexadecimalFloat Float

" Spaces after "[]"
if go#config#HighlightArrayWhitespaceError()
Expand Down

0 comments on commit 8fc3568

Please sign in to comment.