Skip to content

Instantly share code, notes, and snippets.

@eloytoro
Last active April 16, 2017 18:17
Show Gist options
  • Save eloytoro/bed193228645672e5965f7d9b7d517b9 to your computer and use it in GitHub Desktop.
Save eloytoro/bed193228645672e5965f7d9b7d517b9 to your computer and use it in GitHub Desktop.
let s:closed_tag_regexp = '<\/\(\w\|\.\)\+>'
let s:tag_name_regexp = '<\(\w\|\.\|:\)\+'
let s:tag_regexp = s:tag_name_regexp.'\(\s\+[^>]\+\s*\)*'
function! ExpandSnippetOrCarriageReturn()
let snippet = UltiSnips#ExpandSnippetOrJump()
if g:ulti_expand_or_jump_res > 0
return snippet
else
let col = col('.') - 1
if col && strpart(getline('.'), col) =~ '^'.s:closed_tag_regexp
return "\<CR>\<Esc>".'zvO'
endif
" return delimitMate#ExpandReturn()
return "\<CR>"
endif
endfunction
inoremap <silent> <CR> <C-R>=ExpandSnippetOrCarriageReturn()<CR>
function! CloseTag()
let line = getline('.')
let col = col('.') - 1
if col && strpart(line, 0, col) =~ s:tag_regexp.'$'
return '></'.strpart(matchstr(line, s:tag_name_regexp), 1).'>'."\<Esc>F>a"
endif
return '>'
endfunction
function! EraseTag()
let n = line('.')
let line = getline(n)
let col = col('.') - 1
if col
\ && strpart(line, 0, col) =~ s:tag_regexp.'>$'
\ && strpart(line, col) =~ '^'.s:closed_tag_regexp
return "\<Esc>cf>"
endif
if n > 0
let before = getline(n - 1)
let after = getline(n + 1)
if line =~ '^\s*$'
\ && after =~ '^\s*'.s:closed_tag_regexp
\ && before =~ s:tag_regexp.'>$'
return "\<Esc>kJJhct<"
endif
endif
" return delimitMate#BS()
return "\<BS>"
endfunction
autocmd FileType javascript.jsx,html,xml inoremap <buffer> <silent> > <C-R>=CloseTag()<CR>
autocmd FileType javascript.jsx,html,xml inoremap <buffer> <silent> <BS> <C-R>=EraseTag()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment