简体   繁体   English

如何将jscs autofix功能集成到vim中?

[英]How can I integrate jscs autofix feature into vim?

I'm trying to get a command I can run within vim to get jscs auto correct formatting issues in my code. 我正在尝试获取一个命令,我可以在vim中运行,以便在我的代码中使jscs自动纠正格式化问题。 So far I've come up with : 到目前为止,我想出了:

:nmap <F5> :!jscs -x .<CR>

which is ok, but it runs it on the entire directory and I need to confirm to vim that I want to reload the buffer. 这是好的,但它在整个目录上运行它,我需要确认vim我想重新加载缓冲区。 Is there a way to get vim to fix the current file only and didsplay the changes without reloading? 有没有办法让vim只修复当前文件并在没有重新加载的情况下显示更改?

This will pipe the current file through jscs's fix mode whenever you save the file (your mileage may vary using this in practice!): 每当您保存文件时,这将通过jscs的修复模式管道当前文件(在实践中您的里程可能会有所不同!):

function! JscsFix()
    "Save current cursor position"
    let l:winview = winsaveview()
    "Pipe the current buffer (%) through the jscs -x command"
    % ! jscs -x
    "Restore cursor position - this is needed as piping the file"
    "through jscs jumps the cursor to the top"
    call winrestview(l:winview)
endfunction
command! JscsFix :call JscsFix()

"Run the JscsFix command just before the buffer is written for *.js files"
autocmd BufWritePre *.js JscsFix

It also creates a command JscsFix which you can run whenever you want with :JscsFix . 它还会创建一个命令JscsFix ,您可以JscsFix它运行:JscsFix To bind it to a key (in this case <leader>g ) use noremap <leader>g :JscsFix<cr> . 要将其绑定到键(在本例中为<leader>g ),请使用noremap <leader>g :JscsFix<cr>

vim-autoformat supports JSCS out of the box. vim-autoformat支持JSCS开箱即用。 Invoke its :Autoformat command to fix only the current file. 调用其:Autoformat命令仅修复当前文件。 Note it edits the file in the current buffer, so the changes will just appear; 请注意,它会编辑当前缓冲区中的文件,因此只会出现更改; you will not be prompted to reload. 系统不会提示您重新加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM