diff --git a/autoload/go/highlight_test.vim b/autoload/go/highlight_test.vim index ef0e595322..82da67eb81 100644 --- a/autoload/go/highlight_test.vim +++ b/autoload/go/highlight_test.vim @@ -101,13 +101,16 @@ 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'}, @@ -115,11 +118,32 @@ function! Test_numeric_literal_highlight() abort \ '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 @@ -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', diff --git a/syntax/go.vim b/syntax/go.vim index a7ee3b38d6..9a45a9c0b4 100644 --- a/syntax/go.vim +++ b/syntax/go.vim @@ -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()