├── LICENSE.txt ├── README.md ├── doc └── cssfmt.txt └── plugin └── stylefmt.vim /LICENSE.txt: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) 2004 Sam Hocevar 5 | 6 | Everyone is permitted to copy and distribute verbatim or modified 7 | copies of this license document, and changing it is allowed as long 8 | as the name is changed. 9 | 10 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 11 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 12 | 13 | 0. You just DO WHAT THE FUCK YOU WANT TO. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vim-stylefmt 2 | 3 | Format your stylesheets using [stylefmt](https://github.com/morishitter/stylefmt) inside Vim. 4 | Stylefmt supports the latest CSS syntax and understands CSS-like syntax such as SCSS, Stylus and Less. 5 | 6 | This plugin is heavily inspired by [vim-esformatter](https://github.com/millermedeiros/vim-esformatter). 7 | 8 | ## Installation 9 | 10 | First you need to install stylefmt (make sure you have [Node.js](https://nodejs.org/) 11 | installed): 12 | 13 | ``` 14 | npm install -g stylefmt 15 | ``` 16 | 17 | Then install the plugin: 18 | 19 | * Manual installation: 20 | - Copy the files to your `.vim/plugin` directory 21 | * Pathogen 22 | - `cd ~/.vim/bundle && git clone git://github.com/kewah/vim-stylefmt.git` 23 | * Vundle 24 | - Add `Plugin 'kewah/vim-stylefmt'` to `.vimrc` 25 | - Run `:PluginInstall` 26 | * NeoBundle 27 | - Add `NeoBundle 'kewah/vim-stylefmt'` to `.vimrc` 28 | - Run `:NeoBundleInstall` 29 | * vim-plug 30 | - Add `Plug 'kewah/vim-stylefmt'` to `.vimrc` 31 | - Run `:PlugInstall` 32 | 33 | 34 | ## Usage 35 | 36 | In normal mode: 37 | * `:Stylefmt`: format the whole buffer. 38 | 39 | In Visual mode: 40 | * `:'<,'>StylefmtVisual`: format the selected block. 41 | 42 | Or by mapping the commands in your `.vimrc`: 43 | ``` 44 | nnoremap cs :Stylefmt 45 | vnoremap cs :StylefmtVisual 46 | ``` 47 | 48 | 49 | ## License 50 | 51 | Released under the [wtfpl](http://sam.zoy.org/wtfpl/COPYING) license 52 | -------------------------------------------------------------------------------- /doc/cssfmt.txt: -------------------------------------------------------------------------------- 1 | *stylefmt.txt* Automatically format your stylesheets. 2 | 3 | =============================================================================== 4 | USAGE *stylefmt* 5 | 6 | In normal mode: 7 | `:Stylefmt`: format the whole buffer. 8 | 9 | In Visual mode: 10 | `:'<,'>StylefmtVisual`: format the selected block. 11 | 12 | Or by mapping the commands in your `.vimrc`: 13 | ``` 14 | nnoremap cs :Stylefmt 15 | vnoremapap cs :StylefmtVisual 16 | ``` 17 | 18 | 19 | =============================================================================== 20 | REPOSITORY 21 | 22 | https://github.com/kewah/vim-stylefmt 23 | 24 | 25 | =============================================================================== 26 | LICENSE 27 | 28 | released under the wtfpl http://sam.zoy.org/wtfpl/COPYING 29 | 30 | 31 | -------------------------------------------------------------------------------- /plugin/stylefmt.vim: -------------------------------------------------------------------------------- 1 | " ============================================================================ 2 | " File: stylefmt.vim 3 | " Maintainer: Antoine Lehurt 4 | " Description: Expose commands to execute the stylefmt binary on normal and 5 | " visual modes. 6 | " Last Change: 2015-08-14 7 | " License: This program is free software. It comes without any warranty, 8 | " to the extent permitted by applicable law. You can redistribute 9 | " it and/or modify it under the terms of the Do What The Fuck You 10 | " Want To Public License, Version 2, as published by Sam Hocevar. 11 | " See http://sam.zoy.org/wtfpl/COPYING for more details. 12 | " Forked: https://github.com/millermedeiros/vim-esformatter 13 | " ============================================================================ 14 | 15 | " avoid installing twice 16 | if exists('g:loaded_stylefmt') || &compatible 17 | finish 18 | endif 19 | 20 | " check if debugging is turned off 21 | if !exists('g:stylefmt_debug') 22 | let g:loaded_stylefmt = 1 23 | end 24 | 25 | function! s:StylefmtNormal() 26 | " store current cursor position and change the working directory 27 | let win_view = winsaveview() 28 | let file_wd = expand('%:p:h') 29 | let current_wd = getcwd() 30 | execute ':lcd' . file_wd 31 | 32 | " pass whole buffer content to stylefmt 33 | let output = system('stylefmt', join(getline(1,'$'), "\n")) 34 | 35 | if v:shell_error 36 | echom "Error while executing stylefmt! no changes made." 37 | echo output 38 | else 39 | " delete whole buffer content and append the formatted code 40 | normal! ggdG 41 | let list = split(output, "\n") 42 | call setline(1, list[0]) 43 | call append(1, list[1:]) 44 | endif 45 | 46 | " Clean up: restore previous cursor position and working directory 47 | call winrestview(win_view) 48 | execute ':lcd' . current_wd 49 | endfunction 50 | 51 | function! s:StylefmtVisual() range 52 | " store current cursor position and change the working directory 53 | let win_view = winsaveview() 54 | let file_wd = expand('%:p:h') 55 | let current_wd = getcwd() 56 | execute ':lcd' . file_wd 57 | 58 | " get lines from the current selection and store the first line number 59 | let range_start = line("'<") 60 | let input = getline("'<", "'>") 61 | 62 | let output = system('stylefmt', join(input, "\n")) 63 | 64 | if v:shell_error 65 | echom 'Error while executing stylefmt! no changes made.' 66 | echo output 67 | else 68 | " delete the old lines 69 | normal! gvd 70 | 71 | let new_lines = split(l:output, '\n') 72 | 73 | " add new lines to the buffer 74 | call append(range_start - 1, new_lines) 75 | 76 | " Clean up: restore previous cursor position 77 | call winrestview(win_view) 78 | " recreate the visual selection and cancel it, so that the formatted code 79 | " can be reselected using gv 80 | execute "normal! V" . (len(new_lines)-1) . "j\" 81 | endif 82 | 83 | " Clean up: restore working directory 84 | execute ':lcd' . current_wd 85 | endfunction 86 | 87 | command! -range=0 -complete=shellcmd Stylefmt call s:StylefmtNormal() 88 | command! -range=% -complete=shellcmd StylefmtVisual call s:StylefmtVisual() 89 | --------------------------------------------------------------------------------