Skip to content

Commit c44a1be

Browse files
committed
1st
0 parents  commit c44a1be

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

.jscsrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"preset": "airbnb"
3+
}

plugin/jscs.vim

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
let s:dirname = expand('<sfile>:h')
2+
3+
if (exists ('g:jscs_config'))
4+
let s:configfile = g:jscs_config
5+
else
6+
let s:configfile = s:dirname . '/../.jscsrc'
7+
endif
8+
9+
function! s:JSCSFormat(line1, line2, file)
10+
let cmd = 'sed -n "' . a:line1 . ',' . a:line2 . 'p" ' . a:file . '| jscs -x -c ' . s:configfile
11+
let out = system(cmd)
12+
let ls = split(out, '\n', 1)
13+
14+
call setline(a:line1, ls)
15+
endfunction
16+
17+
function! s:initCommands()
18+
command! -buffer -range=% Format call s:JSCSFormat(<line1>, <line2>, expand('%'))
19+
endfunction
20+
21+
autocmd! BufEnter *.js call s:initCommands()

readme.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## jscs.vim
2+
3+
A vim plugin that wraps `jscs -x` into a `:Format` command, for use within Vim.
4+
Can accept range or operate on the whole file.
5+
6+
### Installation
7+
8+
- First, you need jscs installed globally: npm install jscs -g
9+
- Extract the files and put them in your VIM directory (usually `~/.vim/`)
10+
11+
If you don't have a preferred installation method, I recommend installing
12+
[vim-plug](https://github.com/junegunn/vim-plug) and using the following configuration:
13+
14+
Plug 'mklabs/jscs.vim', { 'do': 'npm i jscs -g' }
15+
16+
so that vim-plug takes care of installing jscs for you.
17+
18+
### :Format
19+
20+
The single command exposed by this plugin.
21+
22+
Accepts a range and is usable in Visual mode.
23+
24+
Ex.
25+
26+
plugin.commandSync('Cmd', {
27+
range: '',
28+
nargs: '*',
29+
}, function( nvim, args, range, cb ) {
30+
try {
31+
incrementCalls()
32+
nvim.setCurrentLine(
33+
fmt('Command: Called', numCalls, 'times, args:', args, 'range:', range),
34+
cb )
35+
} catch ( err ) {
36+
cb( err )
37+
}
38+
})
39+
40+
Running `:Format` while run jscs in fix mode on the whole file and replace the current buffer with the result.
41+
42+
### Configuration
43+
44+
The default configuration uses airbnb styleguide, you can change the configuration used by jscs with:
45+
46+
let g:jscs_config = "/path/to/.jscsrc"

0 commit comments

Comments
 (0)