Refactoring

This commit is contained in:
Yasuhiro Matsumoto committed 2026-03-07 11:08:24 +09:00
1 parent e983971449
commit 7c44caa0e6
14 files changed
+2452 -2452

No files matched your search

+202 -202
View File
@@ -7,251 +7,251 @@ function! emmet#lang#haml#parseIntoTree(abbr, type) abort
endfunction
function! emmet#lang#haml#toString(settings, current, type, inline, filters, itemno, indent) abort
let settings = a:settings
let current = a:current
let type = a:type
let inline = a:inline
let filters = a:filters
let itemno = a:itemno
let indent = emmet#getIndentation(type)
let dollar_expr = emmet#getResource(type, 'dollar_expr', 1)
let attribute_style = emmet#getResource('haml', 'attribute_style', 'hash')
let str = ''
let l:settings = a:settings
let l:current = a:current
let l:type = a:type
let l:inline = a:inline
let l:filters = a:filters
let l:itemno = a:itemno
let l:indent = emmet#getIndentation(l:type)
let l:dollar_expr = emmet#getResource(l:type, 'dollar_expr', 1)
let l:attribute_style = emmet#getResource('haml', 'attribute_style', 'hash')
let l:str = ''
let current_name = current.name
if dollar_expr
let current_name = substitute(current.name, '\$$', itemno+1, '')
let l:current_name = l:current.name
if l:dollar_expr
let l:current_name = substitute(l:current.name, '\$$', l:itemno+1, '')
endif
if len(current.name) > 0
let str .= '%' . current_name
let tmp = ''
for attr in emmet#util#unique(current.attrs_order + keys(current.attr))
if !has_key(current.attr, attr)
if len(l:current.name) > 0
let l:str .= '%' . l:current_name
let l:tmp = ''
for l:attr in emmet#util#unique(l:current.attrs_order + keys(l:current.attr))
if !has_key(l:current.attr, l:attr)
continue
endif
let Val = current.attr[attr]
if type(Val) == 2 && Val == function('emmet#types#true')
if attribute_style ==# 'hash'
let tmp .= ' :' . attr . ' => true'
elseif attribute_style ==# 'html'
let tmp .= attr . '=true'
let l:Val = l:current.attr[l:attr]
if type(l:Val) == 2 && l:Val == function('emmet#types#true')
if l:attribute_style ==# 'hash'
let l:tmp .= ' :' . l:attr . ' => true'
elseif l:attribute_style ==# 'html'
let l:tmp .= l:attr . '=true'
end
else
if dollar_expr
while Val =~# '\$\([^#{]\|$\)'
let Val = substitute(Val, '\(\$\+\)\([^{]\|$\)', '\=printf("%0".len(submatch(1))."d", itemno+1).submatch(2)', 'g')
if l:dollar_expr
while l:Val =~# '\$\([^#{]\|$\)'
let l:Val = substitute(l:Val, '\(\$\+\)\([^{]\|$\)', '\=printf("%0".len(submatch(1))."d", l:itemno+1).submatch(2)', 'g')
endwhile
let attr = substitute(attr, '\$$', itemno+1, '')
let l:attr = substitute(l:attr, '\$$', l:itemno+1, '')
endif
let valtmp = substitute(Val, '\${cursor}', '', '')
if attr ==# 'id' && len(valtmp) > 0
let str .= '#' . Val
elseif attr ==# 'class' && len(valtmp) > 0
let str .= '.' . substitute(Val, ' ', '.', 'g')
let l:valtmp = substitute(l:Val, '\${cursor}', '', '')
if l:attr ==# 'id' && len(l:valtmp) > 0
let l:str .= '#' . l:Val
elseif l:attr ==# 'class' && len(l:valtmp) > 0
let l:str .= '.' . substitute(l:Val, ' ', '.', 'g')
else
if len(tmp) > 0
if attribute_style ==# 'hash'
let tmp .= ','
elseif attribute_style ==# 'html'
let tmp .= ' '
if len(l:tmp) > 0
if l:attribute_style ==# 'hash'
let l:tmp .= ','
elseif l:attribute_style ==# 'html'
let l:tmp .= ' '
endif
endif
if attribute_style ==# 'hash'
let tmp .= ' :' . attr . ' => "' . Val . '"'
elseif attribute_style ==# 'html'
let tmp .= attr . '="' . Val . '"'
if l:attribute_style ==# 'hash'
let l:tmp .= ' :' . l:attr . ' => "' . l:Val . '"'
elseif l:attribute_style ==# 'html'
let l:tmp .= l:attr . '="' . l:Val . '"'
end
endif
endif
endfor
if len(tmp)
if attribute_style ==# 'hash'
let str .= '{' . tmp . ' }'
elseif attribute_style ==# 'html'
let str .= '(' . tmp . ')'
if len(l:tmp)
if l:attribute_style ==# 'hash'
let l:str .= '{' . l:tmp . ' }'
elseif l:attribute_style ==# 'html'
let l:str .= '(' . l:tmp . ')'
end
endif
if stridx(','.settings.html.empty_elements.',', ','.current_name.',') != -1 && len(current.value) == 0
let str .= '/'
if stridx(','.l:settings.html.empty_elements.',', ','.l:current_name.',') != -1 && len(l:current.value) == 0
let l:str .= '/'
endif
let inner = ''
if len(current.value) > 0
let text = current.value[1:-2]
if dollar_expr
let text = substitute(text, '\%(\\\)\@\<!\(\$\+\)\([^{#]\|$\)', '\=printf("%0".len(submatch(1))."d", itemno+1).submatch(2)', 'g')
let text = substitute(text, '\${nr}', "\n", 'g')
let text = substitute(text, '\\\$', '$', 'g')
let str = substitute(str, '\$#', text, 'g')
let l:inner = ''
if len(l:current.value) > 0
let l:text = l:current.value[1:-2]
if l:dollar_expr
let l:text = substitute(l:text, '\%(\\\)\@\<!\(\$\+\)\([^{#]\|$\)', '\=printf("%0".len(submatch(1))."d", l:itemno+1).submatch(2)', 'g')
let l:text = substitute(l:text, '\${nr}', "\n", 'g')
let l:text = substitute(l:text, '\\\$', '$', 'g')
let l:str = substitute(l:str, '\$#', l:text, 'g')
endif
let lines = split(text, "\n")
if len(lines) == 1
let str .= ' ' . text
let l:lines = split(l:text, "\n")
if len(l:lines) == 1
let l:str .= ' ' . l:text
else
for line in lines
let str .= "\n" . indent . line . ' |'
for l:line in l:lines
let l:str .= "\n" . l:indent . l:line . ' |'
endfor
endif
elseif len(current.child) == 0
let str .= '${cursor}'
elseif len(l:current.child) == 0
let l:str .= '${cursor}'
endif
if len(current.child) == 1 && len(current.child[0].name) == 0
let text = current.child[0].value[1:-2]
if dollar_expr
let text = substitute(text, '\%(\\\)\@\<!\(\$\+\)\([^{#]\|$\)', '\=printf("%0".len(submatch(1))."d", itemno+1).submatch(2)', 'g')
let text = substitute(text, '\${nr}', "\n", 'g')
let text = substitute(text, '\\\$', '$', 'g')
if len(l:current.child) == 1 && len(l:current.child[0].name) == 0
let l:text = l:current.child[0].value[1:-2]
if l:dollar_expr
let l:text = substitute(l:text, '\%(\\\)\@\<!\(\$\+\)\([^{#]\|$\)', '\=printf("%0".len(submatch(1))."d", l:itemno+1).submatch(2)', 'g')
let l:text = substitute(l:text, '\${nr}', "\n", 'g')
let l:text = substitute(l:text, '\\\$', '$', 'g')
endif
let lines = split(text, "\n")
if len(lines) == 1
let str .= ' ' . text
let l:lines = split(l:text, "\n")
if len(l:lines) == 1
let l:str .= ' ' . l:text
else
for line in lines
let str .= "\n" . indent . line . ' |'
for l:line in l:lines
let l:str .= "\n" . l:indent . l:line . ' |'
endfor
endif
elseif len(current.child) > 0
for child in current.child
let inner .= emmet#toString(child, type, inline, filters, itemno, indent)
elseif len(l:current.child) > 0
for l:child in l:current.child
let l:inner .= emmet#toString(l:child, l:type, l:inline, l:filters, l:itemno, l:indent)
endfor
let inner = substitute(inner, "\n", "\n" . escape(indent, '\'), 'g')
let inner = substitute(inner, "\n" . escape(indent, '\') . '$', '', 'g')
let str .= "\n" . indent . inner
let l:inner = substitute(l:inner, "\n", "\n" . escape(l:indent, '\'), 'g')
let l:inner = substitute(l:inner, "\n" . escape(l:indent, '\') . '$', '', 'g')
let l:str .= "\n" . l:indent . l:inner
endif
else
let str = current.value[1:-2]
if dollar_expr
let str = substitute(str, '\%(\\\)\@\<!\(\$\+\)\([^{#]\|$\)', '\=printf("%0".len(submatch(1))."d", itemno+1).submatch(2)', 'g')
let str = substitute(str, '\${nr}', "\n", 'g')
let str = substitute(str, '\\\$', '$', 'g')
let l:str = l:current.value[1:-2]
if l:dollar_expr
let l:str = substitute(l:str, '\%(\\\)\@\<!\(\$\+\)\([^{#]\|$\)', '\=printf("%0".len(submatch(1))."d", l:itemno+1).submatch(2)', 'g')
let l:str = substitute(l:str, '\${nr}', "\n", 'g')
let l:str = substitute(l:str, '\\\$', '$', 'g')
endif
endif
let str .= "\n"
return str
let l:str .= "\n"
return l:str
endfunction
function! emmet#lang#haml#imageSize() abort
let line = getline('.')
let current = emmet#lang#haml#parseTag(line)
if empty(current) || !has_key(current.attr, 'src')
let l:line = getline('.')
let l:current = emmet#lang#haml#parseTag(l:line)
if empty(l:current) || !has_key(l:current.attr, 'src')
return
endif
let fn = current.attr.src
if fn =~# '^\s*$'
let l:fn = l:current.attr.src
if l:fn =~# '^\s*$'
return
elseif fn !~# '^\(/\|http\)'
let fn = simplify(expand('%:h') . '/' . fn)
elseif l:fn !~# '^\(/\|http\)'
let l:fn = simplify(expand('%:h') . '/' . l:fn)
endif
let [width, height] = emmet#util#getImageSize(fn)
if width == -1 && height == -1
let [l:width, l:height] = emmet#util#getImageSize(l:fn)
if l:width == -1 && l:height == -1
return
endif
let current.attr.width = width
let current.attr.height = height
let current.attrs_order += ['width', 'height']
let haml = emmet#toString(current, 'haml', 1)
let haml = substitute(haml, '\${cursor}', '', '')
call setline('.', substitute(matchstr(line, '^\s*') . haml, "\n", '', 'g'))
let l:current.attr.width = l:width
let l:current.attr.height = l:height
let l:current.attrs_order += ['width', 'height']
let l:haml = emmet#toString(l:current, 'haml', 1)
let l:haml = substitute(l:haml, '\${cursor}', '', '')
call setline('.', substitute(matchstr(l:line, '^\s*') . l:haml, "\n", '', 'g'))
endfunction
function! emmet#lang#haml#imageEncode() abort
endfunction
function! emmet#lang#haml#parseTag(tag) abort
let current = emmet#newNode()
let mx = '%\([a-zA-Z][a-zA-Z0-9]*\)\s*\%({\(.*\)}\)'
let match = matchstr(a:tag, mx)
let current.name = substitute(match, mx, '\1', '')
let attrs = substitute(match, mx, '\2', '')
let mx = '\([a-zA-Z0-9]\+\)\s*=>\s*\%(\([^"'' \t]\+\)\|"\([^"]\{-}\)"\|''\([^'']\{-}\)''\)'
while len(attrs) > 0
let match = matchstr(attrs, mx)
if len(match) ==# 0
let l:current = emmet#newNode()
let l:mx = '%\([a-zA-Z][a-zA-Z0-9]*\)\s*\%({\(.*\)}\)'
let l:match = matchstr(a:tag, l:mx)
let l:current.name = substitute(l:match, l:mx, '\1', '')
let l:attrs = substitute(l:match, l:mx, '\2', '')
let l:mx = '\([a-zA-Z0-9]\+\)\s*=>\s*\%(\([^"'' \t]\+\)\|"\([^"]\{-}\)"\|''\([^'']\{-}\)''\)'
while len(l:attrs) > 0
let l:match = matchstr(l:attrs, l:mx)
if len(l:match) ==# 0
break
endif
let attr_match = matchlist(match, mx)
let name = attr_match[1]
let value = len(attr_match[2]) ? attr_match[2] : attr_match[3]
let current.attr[name] = value
let current.attrs_order += [name]
let attrs = attrs[stridx(attrs, match) + len(match):]
let l:attr_match = matchlist(l:match, l:mx)
let l:name = l:attr_match[1]
let l:value = len(l:attr_match[2]) ? l:attr_match[2] : l:attr_match[3]
let l:current.attr[l:name] = l:value
let l:current.attrs_order += [l:name]
let l:attrs = l:attrs[stridx(l:attrs, l:match) + len(l:match):]
endwhile
return current
return l:current
endfunction
function! emmet#lang#haml#toggleComment() abort
let line = getline('.')
let space = matchstr(line, '^\s*')
if line =~# '^\s*-#'
call setline('.', space . matchstr(line[len(space)+2:], '^\s*\zs.*'))
elseif line =~# '^\s*%[a-z]'
call setline('.', space . '-# ' . line[len(space):])
let l:line = getline('.')
let l:space = matchstr(l:line, '^\s*')
if l:line =~# '^\s*-#'
call setline('.', l:space . matchstr(l:line[len(l:space)+2:], '^\s*\zs.*'))
elseif l:line =~# '^\s*%[a-z]'
call setline('.', l:space . '-# ' . l:line[len(l:space):])
endif
endfunction
function! emmet#lang#haml#balanceTag(flag) range abort
let block = emmet#util#getVisualBlock()
let l:block = emmet#util#getVisualBlock()
if a:flag == -2 || a:flag == 2
let curpos = [0, line("'<"), col("'<"), 0]
let l:curpos = [0, line("'<"), col("'<"), 0]
else
let curpos = emmet#util#getcurpos()
let l:curpos = emmet#util#getcurpos()
endif
let n = curpos[1]
let ml = len(matchstr(getline(n), '^\s*'))
let l:n = l:curpos[1]
let l:ml = len(matchstr(getline(l:n), '^\s*'))
if a:flag > 0
if a:flag == 1 || !emmet#util#regionIsValid(block)
let n = line('.')
if a:flag == 1 || !emmet#util#regionIsValid(l:block)
let l:n = line('.')
else
while n > 0
let l = len(matchstr(getline(n), '^\s*\ze%[a-z]'))
if l > 0 && l < ml
let ml = l
while l:n > 0
let l:l = len(matchstr(getline(l:n), '^\s*\ze%[a-z]'))
if l:l > 0 && l:l < l:ml
let l:ml = l:l
break
endif
let n -= 1
let l:n -= 1
endwhile
endif
let sn = n
if n == 0
let ml = 0
let l:sn = l:n
if l:n == 0
let l:ml = 0
endif
while n < line('$')
let l = len(matchstr(getline(n), '^\s*%[a-z]'))
if l > 0 && l <= ml
let n -= 1
while l:n < line('$')
let l:l = len(matchstr(getline(l:n), '^\s*%[a-z]'))
if l:l > 0 && l:l <= l:ml
let l:n -= 1
break
endif
let n += 1
let l:n += 1
endwhile
call setpos('.', [0, n, 1, 0])
call setpos('.', [0, l:n, 1, 0])
normal! V
call setpos('.', [0, sn, 1, 0])
call setpos('.', [0, l:sn, 1, 0])
else
while n > 0
let l = len(matchstr(getline(n), '^\s*\ze[a-z]'))
if l > 0 && l > ml
let ml = l
while l:n > 0
let l:l = len(matchstr(getline(l:n), '^\s*\ze[a-z]'))
if l:l > 0 && l:l > l:ml
let l:ml = l:l
break
endif
let n += 1
let l:n += 1
endwhile
let sn = n
if n == 0
let ml = 0
let l:sn = l:n
if l:n == 0
let l:ml = 0
endif
while n < line('$')
let l = len(matchstr(getline(n), '^\s*%[a-z]'))
if l > 0 && l <= ml
let n -= 1
while l:n < line('$')
let l:l = len(matchstr(getline(l:n), '^\s*%[a-z]'))
if l:l > 0 && l:l <= l:ml
let l:n -= 1
break
endif
let n += 1
let l:n += 1
endwhile
call setpos('.', [0, n, 1, 0])
call setpos('.', [0, l:n, 1, 0])
normal! V
call setpos('.', [0, sn, 1, 0])
call setpos('.', [0, l:sn, 1, 0])
endif
endfunction
@@ -260,76 +260,76 @@ function! emmet#lang#haml#moveNextPrevItem(flag) abort
endfunction
function! emmet#lang#haml#moveNextPrev(flag) abort
let pos = search('""', a:flag ? 'Wb' : 'W')
if pos != 0
let l:pos = search('""', a:flag ? 'Wb' : 'W')
if l:pos != 0
silent! normal! l
startinsert
endif
endfunction
function! emmet#lang#haml#splitJoinTag() abort
let n = line('.')
let sml = len(matchstr(getline(n), '^\s*%[a-z]'))
while n > 0
if getline(n) =~# '^\s*\ze%[a-z]'
if len(matchstr(getline(n), '^\s*%[a-z]')) < sml
let l:n = line('.')
let l:sml = len(matchstr(getline(l:n), '^\s*%[a-z]'))
while l:n > 0
if getline(l:n) =~# '^\s*\ze%[a-z]'
if len(matchstr(getline(l:n), '^\s*%[a-z]')) < l:sml
break
endif
let line = getline(n)
call setline(n, substitute(line, '^\s*%\w\+\%(\s*{[^}]*}\|\s\)\zs.*', '', ''))
let sn = n
let n += 1
let ml = len(matchstr(getline(n), '^\s*%[a-z]'))
if len(matchstr(getline(n), '^\s*')) > ml
while n <= line('$')
let l = len(matchstr(getline(n), '^\s*'))
if l <= ml
let l:line = getline(l:n)
call setline(l:n, substitute(l:line, '^\s*%\w\+\%(\s*{[^}]*}\|\s\)\zs.*', '', ''))
let l:sn = l:n
let l:n += 1
let l:ml = len(matchstr(getline(l:n), '^\s*%[a-z]'))
if len(matchstr(getline(l:n), '^\s*')) > l:ml
while l:n <= line('$')
let l:l = len(matchstr(getline(l:n), '^\s*'))
if l:l <= l:ml
break
endif
exe n 'delete'
exe l:n 'delete'
endwhile
call setpos('.', [0, sn, 1, 0])
call setpos('.', [0, l:sn, 1, 0])
else
let tag = matchstr(getline(sn), '^\s*%\zs\(\w\+\)')
let spaces = matchstr(getline(sn), '^\s*')
let settings = emmet#getSettings()
if stridx(','.settings.html.inline_elements.',', ','.tag.',') == -1
call append(sn, spaces . ' ')
call setpos('.', [0, sn+1, 1, 0])
let l:tag = matchstr(getline(l:sn), '^\s*%\zs\(\w\+\)')
let l:spaces = matchstr(getline(l:sn), '^\s*')
let l:settings = emmet#getSettings()
if stridx(','.l:settings.html.inline_elements.',', ','.l:tag.',') == -1
call append(l:sn, l:spaces . ' ')
call setpos('.', [0, l:sn+1, 1, 0])
else
call setpos('.', [0, sn, 1, 0])
call setpos('.', [0, l:sn, 1, 0])
endif
startinsert!
endif
break
endif
let n -= 1
let l:n -= 1
endwhile
endfunction
function! emmet#lang#haml#removeTag() abort
let n = line('.')
let ml = 0
while n > 0
if getline(n) =~# '^\s*\ze[a-z]'
let ml = len(matchstr(getline(n), '^\s*%[a-z]'))
let l:n = line('.')
let l:ml = 0
while l:n > 0
if getline(l:n) =~# '^\s*\ze[a-z]'
let l:ml = len(matchstr(getline(l:n), '^\s*%[a-z]'))
break
endif
let n -= 1
let l:n -= 1
endwhile
let sn = n
while n < line('$')
let l = len(matchstr(getline(n), '^\s*%[a-z]'))
if l > 0 && l <= ml
let n -= 1
let l:sn = l:n
while l:n < line('$')
let l:l = len(matchstr(getline(l:n), '^\s*%[a-z]'))
if l:l > 0 && l:l <= l:ml
let l:n -= 1
break
endif
let n += 1
let l:n += 1
endwhile
if sn == n
if l:sn == l:n
exe 'delete'
else
exe sn ',' (n-1) 'delete'
exe l:sn ',' (l:n-1) 'delete'
endif
endfunction