comparison runtime/indent/cucumber.vim @ 34042:9ece16460769

runtime(cucumber): Updates to indent and syntax Commit: https://github.com/vim/vim/commit/715a8058895f5908f44ee243fdafa431b6483e47 Author: Tim Pope <code@tpope.net> Date: Thu Dec 28 13:03:39 2023 -0500 runtime(cucumber): Updates to indent and syntax
author Christian Brabandt <cb@256bit.org>
date Thu, 28 Dec 2023 23:30:08 +0100
parents 63b0b7b79b25
children
comparison
equal deleted inserted replaced
34041:4a67df32683d 34042:9ece16460769
1 " Vim indent file 1 " Vim indent file
2 " Language: Cucumber 2 " Language: Cucumber
3 " Maintainer: Tim Pope <vimNOSPAM@tpope.org> 3 " Maintainer: Tim Pope <vimNOSPAM@tpope.org>
4 " Last Change: 2017 Jun 13 4 " Last Change: 2023 Dec 28
5 5
6 if exists("b:did_indent") 6 if exists("b:did_indent")
7 finish 7 finish
8 endif 8 endif
9 let b:did_indent = 1 9 let b:did_indent = 1
17 " Only define the function once. 17 " Only define the function once.
18 if exists("*GetCucumberIndent") 18 if exists("*GetCucumberIndent")
19 finish 19 finish
20 endif 20 endif
21 21
22 function! s:syn(lnum) 22 let s:headings = {
23 return synIDattr(synID(a:lnum,1+indent(a:lnum),1),'name') 23 \ 'Feature': 'feature',
24 \ 'Rule': 'rule',
25 \ 'Background': 'bg_or_scenario',
26 \ 'Scenario': 'bg_or_scenario',
27 \ 'ScenarioOutline': 'bg_or_scenario',
28 \ 'Examples': 'examples',
29 \ 'Scenarios': 'examples'}
30
31 function! s:Line(lnum) abort
32 if getline(a:lnum) =~# ':'
33 let group = matchstr(synIDattr(synID(a:lnum,1+indent(a:lnum), 1), 'name'), '^cucumber\zs.*')
34 if !has_key(s:headings, group)
35 let group = substitute(matchstr(getline(a:lnum), '^\s*\zs\%([^:]\+\)\ze:\S\@!'), '\s\+', '', 'g')
36 endif
37 else
38 let group = ''
39 endif
40 let char = matchstr(getline(a:lnum), '^\s*\zs[[:punct:]]')
41 return {
42 \ 'lnum': a:lnum,
43 \ 'indent': indent(a:lnum),
44 \ 'heading': get(s:headings, group, ''),
45 \ 'tag': char ==# '@',
46 \ 'table': char ==# '|',
47 \ 'comment': char ==# '#',
48 \ }
24 endfunction 49 endfunction
25 50
26 function! GetCucumberIndent() 51 function! GetCucumberIndent(...) abort
27 let line = getline(prevnonblank(v:lnum-1)) 52 let lnum = a:0 ? a:1 : v:lnum
28 let cline = getline(v:lnum) 53 let sw = shiftwidth()
29 let nline = getline(nextnonblank(v:lnum+1)) 54 let prev = s:Line(prevnonblank(lnum-1))
30 let sw = exists('*shiftwidth') ? shiftwidth() : shiftwidth() 55 let curr = s:Line(lnum)
31 let syn = s:syn(prevnonblank(v:lnum-1)) 56 let next = s:Line(nextnonblank(lnum+1))
32 let csyn = s:syn(v:lnum) 57 if curr.heading ==# 'feature'
33 let nsyn = s:syn(nextnonblank(v:lnum+1))
34 if csyn ==# 'cucumberFeature' || cline =~# '^\s*Feature:'
35 " feature heading 58 " feature heading
36 return 0 59 return 0
37 elseif csyn ==# 'cucumberExamples' || cline =~# '^\s*\%(Examples\|Scenarios\):' 60 elseif curr.heading ==# 'examples'
38 " examples heading 61 " examples heading
39 return 2 * sw 62 return 2 * sw
40 elseif csyn =~# '^cucumber\%(Background\|Scenario\|ScenarioOutline\)$' || cline =~# '^\s*\%(Background\|Scenario\|Scenario Outline\):' 63 elseif curr.heading ==# 'bg_or_scenario'
41 " background, scenario or outline heading 64 " background, scenario or outline heading
42 return sw 65 return sw
43 elseif syn ==# 'cucumberFeature' || line =~# '^\s*Feature:' 66 elseif prev.heading ==# 'feature'
44 " line after feature heading 67 " line after feature heading
45 return sw 68 return sw
46 elseif syn ==# 'cucumberExamples' || line =~# '^\s*\%(Examples\|Scenarios\):' 69 elseif prev.heading ==# 'examples'
47 " line after examples heading 70 " line after examples heading
48 return 3 * sw 71 return 3 * sw
49 elseif syn =~# '^cucumber\%(Background\|Scenario\|ScenarioOutline\)$' || line =~# '^\s*\%(Background\|Scenario\|Scenario Outline\):' 72 elseif prev.heading ==# 'bg_or_scenario'
50 " line after background, scenario or outline heading 73 " line after background, scenario or outline heading
51 return 2 * sw 74 return 2 * sw
52 elseif cline =~# '^\s*[@#]' && (nsyn == 'cucumberFeature' || nline =~# '^\s*Feature:' || indent(prevnonblank(v:lnum-1)) <= 0) 75 elseif (curr.tag || curr.comment) && (next.heading ==# 'feature' || prev.indent <= 0)
53 " tag or comment before a feature heading 76 " tag or comment before a feature heading
54 return 0 77 return 0
55 elseif cline =~# '^\s*@' 78 elseif curr.tag
56 " other tags 79 " other tags
57 return sw 80 return sw
58 elseif cline =~# '^\s*[#|]' && line =~# '^\s*|' 81 elseif (curr.table || curr.comment) && prev.table
59 " mid-table 82 " mid-table
60 " preserve indent 83 " preserve indent
61 return indent(prevnonblank(v:lnum-1)) 84 return prev.indent
62 elseif cline =~# '^\s*|' && line =~# '^\s*[^|]' 85 elseif curr.table && !prev.table
63 " first line of a table, relative indent 86 " first line of a table, relative indent
64 return indent(prevnonblank(v:lnum-1)) + sw 87 return prev.indent + sw
65 elseif cline =~# '^\s*[^|]' && line =~# '^\s*|' 88 elseif !curr.table && prev.table
66 " line after a table, relative unindent 89 " line after a table, relative unindent
67 return indent(prevnonblank(v:lnum-1)) - sw 90 return prev.indent - sw
68 elseif cline =~# '^\s*#' && getline(v:lnum-1) =~ '^\s*$' && (nsyn =~# '^cucumber\%(Background\|Scenario\|ScenarioOutline\)$' || nline =~# '^\s*\%(Background\|Scenario\|Scenario Outline\):') 91 elseif curr.comment && getline(v:lnum-1) =~# '^\s*$' && next.heading ==# 'bg_or_scenario'
69 " comments on scenarios 92 " comments on scenarios
70 return sw 93 return sw
71 endif 94 endif
72 return indent(prevnonblank(v:lnum-1)) 95 return prev.indent < 0 ? 0 : prev.indent
73 endfunction 96 endfunction
74 97
75 " vim:set sts=2 sw=2: 98 " vim:set sts=2 sw=2: