Syntax: Add highlithing for YAML metadata block (#1287)

This commit is contained in:
Tinmarino
2023-03-13 16:52:57 -03:00
parent 34ceee8aaa
commit 8bf4d6363c
5 changed files with 160 additions and 7 deletions
+95
View File
@@ -0,0 +1,95 @@
# Non regression tests for issue: #1287
Before(Define function for yaml inspection):
function! Issue1287Yaml(line)
" The line where the yaml delimiter is: 1 if at top
let l = a:line
AssertEqual 'textSnipYAML', GetSyntaxStack(l + 0, 2)[0]
AssertEqual 'VimwikiPre', GetSyntaxStack(l + 0, 2)[1]
AssertEqual 'textSnipYAML', GetSyntaxStack(l + 1, 2)[0]
AssertEqual 'textSnipYAML', GetSyntaxStack(l + 2, 2)[0]
AssertEqual 'textSnipYAML', GetSyntaxStack(l + 2, 20)[0]
AssertEqual 'VimwikiPre', GetSyntaxStack(l + 3, 2)[-1]
endfunction
Given vimwiki (Yaml with --- at top):
---
title: my title
description: my description
---
Execute (Assert delimiter with --- at top):
call Issue1287Yaml(1)
Given vimwiki (Yaml with ... at top):
...
title: my title
description: my description
...
Execute (Assert delimiter with ... at top):
call Issue1287Yaml(1)
Given vimwiki (Yaml with --- after empty line):
A stupid block
of 2 lines
---
title: my title
description: my description
---
Execute (Assert delimiter with --- after empty line):
call Issue1287Yaml(4)
Given vimwiki (Yaml with --- mixed with ...):
---
title: my title
...
comment: my comment
description: my description
---
And a text follows
Execute (Assert all is yaml except after the closing ---):
AssertEqual 'textSnipYAML1', GetSyntaxStack(1, 2)[0] . 1
AssertEqual 'VimwikiPre1', GetSyntaxStack(1, 2)[1] . 1
AssertEqual 'textSnipYAML1', GetSyntaxStack(1, 2)[0] . 1
AssertEqual 'textSnipYAML2', GetSyntaxStack(2, 2)[0] . 2
AssertEqual 'textSnipYAML3', GetSyntaxStack(3, 2)[0] . 3
AssertEqual 'textSnipYAML4', GetSyntaxStack(4, 2)[0] . 4
AssertEqual 'textSnipYAML5', GetSyntaxStack(5, 2)[0] . 5
AssertEqual 'textSnipYAML6', GetSyntaxStack(5, 2)[0] . 6
AssertEqual 0, len(GetSyntaxStack(7, 2))
AssertEqual 0, len(GetSyntaxStack(8, 2))
Given vimwiki (Yaml with --- with a --- not a start of line):
---
title: my title
comment: my comment ---
description: my description
---
And a text follows
Execute (Assert all is yaml except after the closing ---):
AssertEqual 'textSnipYAML1', GetSyntaxStack(1, 2)[0] . 1
AssertEqual 'VimwikiPre1', GetSyntaxStack(1, 2)[1] . 1
AssertEqual 'textSnipYAML1', GetSyntaxStack(1, 2)[0] . 1
AssertEqual 'textSnipYAML2', GetSyntaxStack(2, 2)[0] . 2
AssertEqual 'textSnipYAML3', GetSyntaxStack(3, 2)[0] . 3
AssertEqual 'textSnipYAML4', GetSyntaxStack(4, 2)[0] . 4
AssertEqual 'VimwikiPre5', GetSyntaxStack(5, 2)[-1] . 5
AssertEqual 0, len(GetSyntaxStack(6, 2))
AssertEqual 0, len(GetSyntaxStack(7, 2))
+22 -6
View File
@@ -343,21 +343,37 @@
0d
endfunction
function! GetSyntaxGroup(line, col)
function! GetSyntaxGroup(...)
" Get normalized syntax group: usefull for boldItalic Vs italicBold
" Arg1: line
" Arg2: column
" -- Here, Vader's SyntaxAt is not enough
" From: https://stackoverflow.com/questions/9464844
let l:s = synID(a:line, a:col, 1)
let line = a:0 >= 1 ? a:1 : '.'
let col = a:0 >= 2 ? a:2 : '.'
let l:s = synID(line, col, 1)
return synIDattr(synIDtrans(l:s), 'name')
endfun
function! GetSyntaxStack()
function! GetSyntaxStack(...)
" Debug helper
" Arg1: line
" Arg2: column
let line = a:0 >= 1 ? a:1 : '.'
let col = a:0 >= 2 ? a:2 : '.'
if !exists('*synstack')
return
return []
endif
return map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
endfunc
let res = map(synstack(line, col), 'synIDattr(v:val, "name")')
" For old vim version returning 0
if type(res) == type(0) && res == 0
return []
endif
return res
endfunction
function! AssertIfVersion(version, one, two)
" Run Assert only if vim version is high enough