Skip to content

Instantly share code, notes, and snippets.

@hotoo
Created July 21, 2010 07:01
Show Gist options
  • Select an option

  • Save hotoo/484171 to your computer and use it in GitHub Desktop.

Select an option

Save hotoo/484171 to your computer and use it in GitHub Desktop.
--- C:/Documents and Settings/wb-tianliang/My Documents/下载/csspretty.vim Wed Jul 21 14:25:29 2010
+++ D:/Vim/vimfiles/ftplugin/css/csspretty.vim Wed Jul 21 14:59:04 2010
@@ -30,7 +30,7 @@
" generates whitespace
function! Whitespace(indent)
- let tabstop = 3
+ let tabstop = &ts
return repeat(' ', a:indent * tabstop)
endfunction
@@ -99,8 +99,11 @@
let declarations = []
for o in split(contents, ';')
- let property = Trim(split(o, ':')[0])
- let value = Trim(split(o, ':')[1])
+ " let property = Trim(split(o, ':')[0])
+ " let value = Trim(split(o, ':')[1])
+ let colonIndex = stridx(o, ':')
+ let property = Trim(strpart(o, 0, colonIndex))
+ let value = Trim(strpart(o, colonIndex+1))
call add(declarations, {'property': property, 'value': value})
endfor
@@ -118,8 +121,13 @@
for x in range(len(matches))
let match = matches[x]
- call add(lines, match['selector'])
- call add(lines, '{')
+ if g:CssPrettyLeftBraceAtNewLine
+ call add(lines, match['selector'])
+ call add(lines, '{')
+ else
+ call add(lines, match['selector'].'{')
+ endif
+
for declaration in match['declarations']
call add(lines, Whitespace(1) . declaration['property'] . ": " . declaration['value'] . ';')
endfor
@@ -140,7 +148,7 @@
endfor
call cursor(lastline, 1)
for x in range(lastline - firstline + 1)
- exe 'normal ' . "a\b\e"
+ exe 'normal ' . "a\b\e"
endfor
" and appending into the buffer
@@ -151,3 +159,6 @@
endfor
endfunction
+
+let g:CssPrettyLeftBraceAtNewLine=0
+nmap <F1> :call CssPretty()<cr>
@hotoo
Copy link
Author

hotoo commented Jul 21, 2010

  1. 自动根据用户的 tabstop 设置缩进;
  2. 解决了原版将 background:#fff url(http://www.google.com/logo.gif);
    解析为 background:#fff url(http;
    的bug,丢掉了第 2 个冒号及其之后的内容。
  3. 增加了 g:CssPrettyLeftBraceAtNewLine 参数设置;
  4. 将 csspretty.vim 丢进 ftplugin/css 目录,并增加了 快捷键映射。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment