mattn / emmet-vim

emmet for vim: http://emmet.io/
http://mattn.github.io/emmet-vim
MIT License
6.39k stars 411 forks source link

Make updating tag works everywhere #530

Open eight04 opened 2 years ago

eight04 commented 2 years ago

With the following content:

<div class="foo">
  foobar
</div>

<C-Y>u only works when the cursor is on the first line:

[<div class="foo"]>
  foobar
</div>

It does nothing from the > character:

<div class="foo"[>
  foobar]
</div>

When the cursor is on the closing tag, the closing tag will be deleted:

<div class="foo">
  foobar
[</div>]

Currently I use a mapping to make it work like regular emmet:

nnoremap <C-e> :execute "normal vato\e\<plug>(emmet-update-tag)"<CR>
eight04 commented 2 years ago

The vato\e trick doesn't work on broken tags e,g, <input> without a closing tag.

eight04 commented 2 years ago

Now I use this:

nnoremap <C-y>u :call EmmetUpdateTag()<CR>

function! EmmetUpdateTag()
  let old_pos = getpos(".")
  let old_search = @/

  let @/ = '\v\<\w[^>]+\>'
  execute "normal! gN\e"
  let pos2 = getpos("'>")
  if CmpList(old_pos, pos2) > 0
    call setpos(".", old_pos)
    execute "normal! vato\e"
  endif
  execute "normal \<plug>(emmet-update-tag)"
  let @/ = old_search
endfunction

function CmpList(a, b)
  let i = 0
  let length = len(a:a)
  while i < length
    if a:a[i] > a:b[i]
      return 1
    elseif a:a[i] < a:b[i]
      return -1
    endif
    let i += 1
  endwhile
  return 0
endfunction