From 0330ce39b440452ef9e80e68303816362b28c421 Mon Sep 17 00:00:00 2001 From: mattn Date: Wed, 30 May 2012 21:00:59 +0900 Subject: [PATCH] toggleComment. --- autoload/zencoding.vim | 76 +------------------------------- autoload/zencoding/lang/css.vim | 14 ++++++ autoload/zencoding/lang/haml.vim | 4 ++ autoload/zencoding/lang/html.vim | 61 +++++++++++++++++++++++++ autoload/zencoding/lang/slim.vim | 4 ++ 5 files changed, 85 insertions(+), 74 deletions(-) diff --git a/autoload/zencoding.vim b/autoload/zencoding.vim index bd8131f..85d8823 100644 --- a/autoload/zencoding.vim +++ b/autoload/zencoding.vim @@ -416,80 +416,8 @@ function! zencoding#imageSize() endfunction function! zencoding#toggleComment() - if zencoding#getFileType() == 'css' - let line = getline('.') - let mx = '^\(\s*\)/\*\s*\(.*\)\s*\*/\s*$' - if line =~ mx - let space = substitute(matchstr(line, mx), mx, '\1', '') - let line = substitute(matchstr(line, mx), mx, '\2', '') - let line = space . substitute(line, '^\s*\|\s*$', '\1', 'g') - else - let mx = '^\(\s*\)\(.*\)\s*$' - let line = substitute(line, mx, '\1/* \2 */', '') - endif - call setline('.', line) - return - endif - - let orgpos = getpos('.') - let curpos = getpos('.') - let mx = '<\%#[^>]*>' - while 1 - let block = zencoding#util#searchRegion('') - if zencoding#util#regionIsValid(block) - let block[1][1] += 2 - let content = zencoding#util#getContent(block) - let content = substitute(content, '^$', '\1', '') - call zencoding#util#setContent(block, content) - silent! call setpos('.', orgpos) - return - endif - let block = zencoding#util#searchRegion('<[^>]', '>') - if !zencoding#util#regionIsValid(block) - let pos1 = searchpos('<', 'bcW') - if pos1[0] == 0 && pos1[1] == 0 - return - endif - let curpos = getpos('.') - continue - endif - let pos1 = block[0] - let pos2 = block[1] - let content = zencoding#util#getContent(block) - let tag_name = matchstr(content, '^<\zs/\{0,1}[^ \r\n>]\+') - if tag_name[0] == '/' - call setpos('.', [0, pos1[0], pos1[1], 0]) - let pos2 = searchpairpos('<'. tag_name[1:] . '>', '', '', 'bnW') - let pos1 = searchpos('>', 'cneW') - let block = [pos2, pos1] - elseif tag_name =~ '/$' - if !zencoding#util#pointInRegion(orgpos[1:2], block) - " it's broken tree - call setpos('.', orgpos) - let block = zencoding#util#searchRegion('>', '<') - let content = '><' - call zencoding#util#setContent(block, content) - silent! call setpos('.', orgpos) - return - endif - else - call setpos('.', [0, pos2[0], pos2[1], 0]) - let pos2 = searchpairpos('<'. tag_name . '>', '', '', 'nW') - call setpos('.', [0, pos2[0], pos2[1], 0]) - let pos2 = searchpos('>', 'cneW') - let block = [pos1, pos2] - endif - if !zencoding#util#regionIsValid(block) - silent! call setpos('.', orgpos) - return - endif - if zencoding#util#pointInRegion(curpos[1:2], block) - let content = '' - call zencoding#util#setContent(block, content) - silent! call setpos('.', orgpos) - return - endif - endwhile + let rtype = len(globpath(&rtp, 'autoload/zencoding/lang/'.&ft.'.vim')) ? &ft : 'html' + return zencoding#lang#{rtype}#toggleComment() endfunction function! zencoding#splitJoinTag() diff --git a/autoload/zencoding/lang/css.vim b/autoload/zencoding/lang/css.vim index 011f44c..227171a 100644 --- a/autoload/zencoding/lang/css.vim +++ b/autoload/zencoding/lang/css.vim @@ -59,3 +59,17 @@ endfunction function! zencoding#lang#css#parseTag(tag) return {} endfunction + +function! zencoding#lang#css#toggleComment() + let line = getline('.') + let mx = '^\(\s*\)/\*\s*\(.*\)\s*\*/\s*$' + if line =~ mx + let space = substitute(matchstr(line, mx), mx, '\1', '') + let line = substitute(matchstr(line, mx), mx, '\2', '') + let line = space . substitute(line, '^\s*\|\s*$', '\1', 'g') + else + let mx = '^\(\s*\)\(.*\)\s*$' + let line = substitute(line, mx, '\1/* \2 */', '') + endif + call setline('.', line) +endfunction diff --git a/autoload/zencoding/lang/haml.vim b/autoload/zencoding/lang/haml.vim index f47efa7..fa8e1ec 100644 --- a/autoload/zencoding/lang/haml.vim +++ b/autoload/zencoding/lang/haml.vim @@ -113,3 +113,7 @@ function! zencoding#lang#haml#parseTag(tag) endwhile return current endfunction + +function! zencoding#lang#haml#toggleComment() + " TODO +endfunction diff --git a/autoload/zencoding/lang/html.vim b/autoload/zencoding/lang/html.vim index 239b82c..456f258 100644 --- a/autoload/zencoding/lang/html.vim +++ b/autoload/zencoding/lang/html.vim @@ -432,3 +432,64 @@ function! zencoding#lang#html#parseTag(tag) return current endfunction +function! zencoding#lang#html#toggleComment() + let orgpos = getpos('.') + let curpos = getpos('.') + let mx = '<\%#[^>]*>' + while 1 + let block = zencoding#util#searchRegion('') + if zencoding#util#regionIsValid(block) + let block[1][1] += 2 + let content = zencoding#util#getContent(block) + let content = substitute(content, '^$', '\1', '') + call zencoding#util#setContent(block, content) + silent! call setpos('.', orgpos) + return + endif + let block = zencoding#util#searchRegion('<[^>]', '>') + if !zencoding#util#regionIsValid(block) + let pos1 = searchpos('<', 'bcW') + if pos1[0] == 0 && pos1[1] == 0 + return + endif + let curpos = getpos('.') + continue + endif + let pos1 = block[0] + let pos2 = block[1] + let content = zencoding#util#getContent(block) + let tag_name = matchstr(content, '^<\zs/\{0,1}[^ \r\n>]\+') + if tag_name[0] == '/' + call setpos('.', [0, pos1[0], pos1[1], 0]) + let pos2 = searchpairpos('<'. tag_name[1:] . '>', '', '', 'bnW') + let pos1 = searchpos('>', 'cneW') + let block = [pos2, pos1] + elseif tag_name =~ '/$' + if !zencoding#util#pointInRegion(orgpos[1:2], block) + " it's broken tree + call setpos('.', orgpos) + let block = zencoding#util#searchRegion('>', '<') + let content = '><' + call zencoding#util#setContent(block, content) + silent! call setpos('.', orgpos) + return + endif + else + call setpos('.', [0, pos2[0], pos2[1], 0]) + let pos2 = searchpairpos('<'. tag_name . '>', '', '', 'nW') + call setpos('.', [0, pos2[0], pos2[1], 0]) + let pos2 = searchpos('>', 'cneW') + let block = [pos1, pos2] + endif + if !zencoding#util#regionIsValid(block) + silent! call setpos('.', orgpos) + return + endif + if zencoding#util#pointInRegion(curpos[1:2], block) + let content = '' + call zencoding#util#setContent(block, content) + silent! call setpos('.', orgpos) + return + endif + endwhile +endfunction diff --git a/autoload/zencoding/lang/slim.vim b/autoload/zencoding/lang/slim.vim index c9cc151..591688b 100644 --- a/autoload/zencoding/lang/slim.vim +++ b/autoload/zencoding/lang/slim.vim @@ -103,3 +103,7 @@ function! zencoding#lang#slim#parseTag(tag) endwhile return current endfunction + +function! zencoding#lang#slim#toggleComment() + " TODO +endfunction