Skip to content

Commit

Permalink
Implement #if, #else, #elseif, #endif directives.
Browse files Browse the repository at this point in the history
These are shortcuts for one-line verbatim blocks. See the test.

Fixes #14.
  • Loading branch information
lifepillar committed Jun 4, 2019
1 parent 124640a commit 53b56ba
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
21 changes: 18 additions & 3 deletions autoload/colortemplate.vim
@@ -1,9 +1,10 @@
" vim: foldmethod=marker nowrap
let s:VERSION = '2.0.0b1'
" Grammar {{{
" Informal grammar {{{
" <Template> ::= <Line>*
" <Line> ::= <EmptyLine> | <Comment> | <KeyValuePair> | <HiGroupDef> |
" <VerbatimText> | <AuxFile> | <Documentation>
" <VerbatimText> | <Command> | <AuxFile> | <Documentation>
" <Command> ::= #if <Anything> | #elseif <Anything> | #else | #endif
" <VerbatimText> ::= verbatim <Anything> endverbatim
" <AuxFile> ::= auxfile <Path> <Anything> endauxfile
" <Path> ::= .+
Expand Down Expand Up @@ -1110,7 +1111,11 @@ fun! s:token.next() dict
let [self.value, self.spos, self.pos] = matchstrpos(s:getl(), '\d\+', self.pos - 1)
let self.kind = 'NUM'
elseif l:char ==# '#'
if match(s:getl(), '^[0-9a-f]\{6}', self.pos) > -1
" Commands are recognized only at the start of a line
if match(s:getl(), '^\s*#\%(if\|else\%[if]\|endif\)\>', 0) > -1
let self.kind = 'CMD'
let self.value = matchstr(s:getl(), '^\s*#\zs\%(if\|else\%[if]\|endif\)\>', 0)
elseif match(s:getl(), '^[0-9a-f]\{6}', self.pos) > -1
let [self.value, self.spos, self.pos] = matchstrpos(s:getl(), '#[0-9a-f]\{6}', self.pos - 1)
let self.kind = 'HEX'
else
Expand Down Expand Up @@ -1429,6 +1434,8 @@ fun! s:parse_line()
else
call s:parse_hi_group_def()
endif
elseif s:token.kind ==# 'CMD'
call s:parse_command(s:token.value)
else
throw 'Unexpected token at start of line'
endif
Expand Down Expand Up @@ -1755,6 +1762,14 @@ fun! s:parse_linked_group_def()
call s:add_linked_item(l:v, s:active_section(), l:source_group, s:token.value)
endfor
endf

fun! s:parse_command(cmd)
let l:text = matchstr(s:getl(), '^\s*#\zs.\{-}\s*$')
for l:v in s:active_variants()
call s:add_verbatim_item(l:v, s:active_section(),
\ { 'line': l:text, 'linenr': s:linenr(), 'file': s:currfile() })
endfor
endf
" }}} Parser
" Init/clear parser {{{
fun! s:init_parser()
Expand Down
2 changes: 2 additions & 0 deletions syntax/colortemplate.vim
Expand Up @@ -150,6 +150,7 @@ syn include @colortemplatevim syntax/vim.vim
unlet b:current_syntax
syn region colortemplatevim matchgroup=colortemplateKeyword start=/verbatim/ end=/endverbatim/ keepend contains=@colortemplatevim
syn region auxfilevim matchgroup=colortemplateKeyword start=/auxfile.*\.vim$/ end=/endauxfile/ keepend contains=@colortemplatevim
syn region colortemplateCommand matchgroup=colortemplateCommand start=/^\s*#\%(if\|else\%[if]\|endif\)\>/ end=/$/ keepend contains=@colortemplatevim

syn include @colortemplatehelp syntax/help.vim
unlet b:current_syntax
Expand All @@ -158,6 +159,7 @@ syn region colortemplatehelp matchgroup=colortemplateKeyword start=/documentatio
hi def link colortemplateArrow Delimiter
hi def link colortemplateAttr Label
hi def link colortemplateAttrs String
hi def link colortemplateCommand PreProc
hi def link colortemplateConstant Type
hi def link colortemplateComment Comment
hi def link colortemplateFunction Function
Expand Down
28 changes: 28 additions & 0 deletions test/expected/test58.vim
@@ -0,0 +1,28 @@
" Name: Test 58
" Author: y
" Maintainer: y
" License: Vim License (see `:help license`)

set background=dark

hi clear
if exists('syntax_on')
syntax reset
endif

let g:colors_name = 'test58'

let s:t_Co = exists('&t_Co') && !empty(&t_Co) && &t_Co > 1 ? &t_Co : 2

if (has('termguicolors') && &termguicolors) || has('gui_running')
if 1 " some condition
hi Normal guifg=#ffffff guibg=#000000 guisp=NONE gui=NONE cterm=NONE
elseif '#000000' == '#ffffff' " interpolation works here
hi Normal guifg=#000000 guibg=#ffffff guisp=NONE gui=NONE cterm=NONE
else " A comment
hi Normal guifg=#000000 guibg=#000000 guisp=NONE gui=NONE cterm=NONE
endif " end some condition (255)
unlet s:t_Co
finish
endif

16 changes: 16 additions & 0 deletions test/test58.txt
@@ -0,0 +1,16 @@
# vim: ft=colortemplate
Full name:Test 58
Short name:test58
Author:y
Variant:gui
Background:dark
Color:black rgb(0,0,0) 16 Black
Color:white #ffffff 255 White
#if 1 " some condition
Normal white black #if here is treated as comment
#elseif '@guiblack' == '@guiwhite' " interpolation works here
Normal black white #else ditto
#else " A comment
Normal black black #elseif ditto
#endif " end some condition (@termwhite)

8 changes: 8 additions & 0 deletions test/test_colortemplate.vim
Expand Up @@ -681,6 +681,14 @@ fun! Test_CT_color_typo()
bwipe test57.txt
endf

fun! Test_CT_conditional_commands()
edit test58.txt
Colortemplate!
call assert_equal(0, get(g:, 'colortemplate_exit_status', 1))
call s:verify('test58')
bwipe test58.txt
endf

let s:old_warnings = get(g:, 'colortemplate_warnings', -1)
let s:old_creator = get(g:, 'colortemplate_creator', -1)
let s:old_timestamp = get(g:, 'colortemplate_timestamp', -1)
Expand Down

0 comments on commit 53b56ba

Please sign in to comment.