Skip to content

Commit

Permalink
Update twig source
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerun committed Sep 11, 2016
1 parent 7575da2 commit 74652b4
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ If you need full functionality of any plugin, please use it directly with your p
- [tmux](https://github.com/keith/tmux.vim) (syntax, ftplugin, ftdetect)
- [tomdoc](https://github.com/wellbredgrapefruit/tomdoc.vim) (syntax)
- [toml](https://github.com/cespare/vim-toml) (syntax, ftplugin, ftdetect)
- [twig](https://github.com/evidens/vim-twig) (syntax, ftplugin)
- [twig](https://github.com/lumiliet/vim-twig) (syntax, indent, ftplugin)
- [typescript](https://github.com/leafgarland/typescript-vim) (syntax, indent, compiler, ftplugin, ftdetect)
- [vala](https://github.com/tkztmk/vim-vala) (syntax, indent, ftdetect)
- [vbnet](https://github.com/vim-scripts/vbnet.vim) (syntax)
Expand Down
2 changes: 1 addition & 1 deletion build
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ PACKS="
tmux:keith/tmux.vim
tomdoc:wellbredgrapefruit/tomdoc.vim
toml:cespare/vim-toml
twig:evidens/vim-twig
twig:lumiliet/vim-twig
typescript:leafgarland/typescript-vim
vala:tkztmk/vim-vala
vbnet:vim-scripts/vbnet.vim
Expand Down
80 changes: 80 additions & 0 deletions indent/twig.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'twig') == -1

if exists("b:ran_once")
finish
endif

let b:ran_once = 1

let s:baseIndentExpr=&indentexpr
setlocal indentexpr=GetTwigIndent(v:lnum)

fun! GetTwigIndent(currentLineNumber)
let currentLine = getline(a:currentLineNumber)
let previousLineNumber = prevnonblank(a:currentLineNumber - 1)
let previousLine = getline(previousLineNumber)

if (previousLine =~ s:startStructures || previousLine =~ s:middleStructures) && (currentLine !~ s:endStructures && currentLine !~ s:middleStructures)
return indent(previousLineNumber) + &shiftwidth
elseif currentLine =~ s:endStructures || currentLine =~ s:middleStructures
let previousOpenStructureNumber = s:FindPreviousOpenStructure(a:currentLineNumber)
let previousOpenStructureLine = getline(previousOpenStructureNumber)
return indent(previousOpenStructureNumber)
endif

return s:CallBaseIndent()
endf

function! s:CallBaseIndent()
exe 'redir => s:outputOfBaseIndent'
exe 'silent echo ' . s:baseIndentExpr
exe 'redir END'
return split(s:outputOfBaseIndent)[0]
endf


function! s:FindPreviousOpenStructure(lineNumber)
let countOpen = 0
let countClosed = 0
let lineNumber = a:lineNumber
while lineNumber != 1 && countOpen <= countClosed
let lineNumber -= 1
let currentLine = getline(lineNumber)

if currentLine =~ s:startStructures
let countOpen += 1
elseif currentLine =~ s:endStructures
let countClosed += 1
endif
endwhile

return lineNumber
endfunction

function! s:StartStructure(name)
return '^\s*{%\s*' . a:name . '.*%}'
endfunction

function! s:EndStructure(name)
return '^\s*{%\s*end' . a:name . '.*%}'
endfunction

function! s:Map(Fun, list)
if len(a:list) == 0
return []
else
return [a:Fun(a:list[0])] + s:Map(a:Fun, a:list[1:])
endif
endfunction

fun! s:BuildStructures()
let structures = ['if','for','block']
let mStructures = ['elseif','else']
let s:startStructures = join(s:Map(function('s:StartStructure'), structures), '\|')
let s:endStructures = join(s:Map(function('s:EndStructure'), structures), '\|')
let s:middleStructures = join(s:Map(function('s:StartStructure'), mStructures), '\|')
endfun

call s:BuildStructures()

endif

0 comments on commit 74652b4

Please sign in to comment.