aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitermayer Reis <mitermayer.reis@gmail.com>2017-06-06 12:27:50 -0700
committerGitHub <noreply@github.com>2017-06-06 12:27:50 -0700
commitc28004d9c616a3599e8d0622c87fa071aa2342e8 (patch)
treefb0924bf23922badf6f025b1e645189a75977d1c
parentb394b4c23622fe3aae78333d7830bc5dabf11e50 (diff)
parenta3df2e771010f9c70e74fadda32dd81a28b88f4f (diff)
downloadvim-prettier-c28004d9c616a3599e8d0622c87fa071aa2342e8.tar.xz
Merge pull request #11 from mitermayer/feature/adding-support-for-new-parsers-bump-prettier0.0.5
Bumping support to latest prettier ^1.4.X, enabling support for css,scss,less,typescript formatting and allowing user configuration to enable async as the default for configuration for `:Prettier` command
-rw-r--r--README.md33
-rw-r--r--autoload/prettier.vim29
-rw-r--r--doc/prettier.txt29
-rw-r--r--ftplugin/css.vim10
-rw-r--r--ftplugin/less.vim10
-rw-r--r--ftplugin/scss.vim10
-rw-r--r--ftplugin/typescript.vim10
-rw-r--r--package.json4
-rw-r--r--plugin/prettier.vim10
9 files changed, 103 insertions, 42 deletions
diff --git a/README.md b/README.md
index 7f010f3..37fc501 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,7 @@
A vim plugin wrapper for prettier, pre-configured with custom default prettier settings.
-By default it will auto format javascript files that have "@format" annotation in the header of the file.
+By default it will auto format **javascript**, **typescript**, **less**, **scss** and **css** files that have "@format" annotation in the header of the file.
![vim-prettier](/media/vim-prettier.gif?raw=true "vim-prettier")
@@ -11,8 +11,10 @@ By default it will auto format javascript files that have "@format" annotation i
Install with [vim-plug](https://github.com/junegunn/vim-plug), assumes node and yarn|npm installed globally.
```
-" yarn install | npm install
-Plug 'mitermayer/vim-prettier', { 'do': 'yarn install', 'for': 'javascript' }
+" post install (yarn install | npm install) then load plugin only for editing supported files
+plug 'mitermayer/vim-prettier', {
+ \ 'do': 'yarn install',
+ \ 'for': ['javascript', 'typescript', 'css', 'less', 'scss'] }
```
If using other vim plugin managers or doing manual setup make sure to have `prettier` installed globally or go to your vim-prettier directory and either do `npm install` or `yarn install`
@@ -29,32 +31,35 @@ vim-prettier executable resolution:
## USAGE
-Formats the entire buffer
-
-### Commands
-
-Prettier can be manualy triggered by:
+Prettier by default will run on auto save but can also be manualy triggered by:
```
<Leader>p
```
-
-### Configuration
+or
```
:Prettier
```
-Disable auto formatting of javascript files that have "@format" tag
+## Configuration
+
+Disable auto formatting of files that have "@format" tag
```
let g:prettier#autoformat = 0
```
-Enable vim-prettier to run in javascript files without the "@format" doc tag
+The command `:Prettier` by default is synchronous but can be forced to be async
+
+```
+let g:prettier#exec_cmd_async = 1
+```
+
+Enable vim-prettier to run in files without requiring the "@format" doc tag
```
-autocmd BufWritePre *.js call prettier#Prettier()
+ autocmd BufWritePre *.js,*.css,*.scss,*.less call prettier#Prettier()
```
Overwrite default configuration
@@ -84,7 +89,7 @@ g:prettier#config#jsx_bracket_same_line = 'true'
" none|es5|all
g:prettier#config#trailing_comma = 'all'
-" flow|babylon
+" flow|babylon|typescript|postcss
g:prettier#config#parser = 'flow'
```
diff --git a/autoload/prettier.vim b/autoload/prettier.vim
index 249ffb1..66166f4 100644
--- a/autoload/prettier.vim
+++ b/autoload/prettier.vim
@@ -3,13 +3,10 @@ let s:root_dir = fnamemodify(resolve(expand('<sfile>:p')), ':h')
function! prettier#Prettier(...) abort
let l:execCmd = s:Get_Prettier_Exec()
let l:async = a:0 > 0 ? a:1 : 0
-
- if &ft !~ 'javascript'
- return
- endif
+ let l:config = getbufvar(bufnr('%'), 'prettier_ft_default_args', {})
if l:execCmd != -1
- let l:cmd = l:execCmd . s:Get_Prettier_Exec_Args()
+ let l:cmd = l:execCmd . s:Get_Prettier_Exec_Args(l:config)
" close quickfix
call setqflist([])
@@ -124,25 +121,27 @@ endfunction
" By default we will default to our internal
" configuration settings for prettier
-function! s:Get_Prettier_Exec_Args() abort
+function! s:Get_Prettier_Exec_Args(config) abort
+ " Allow params to be passed as json format
+ " convert bellow usage of globals to a get function o the params defaulting to global
let l:cmd = ' --print-width ' .
- \ g:prettier#config#print_width .
+ \ get(a:config, 'printWidth', g:prettier#config#print_width) .
\ ' --tab-width ' .
- \ g:prettier#config#tab_width .
+ \ get(a:config, 'tabWidth', g:prettier#config#tab_width) .
\ ' --use-tabs ' .
- \ g:prettier#config#use_tabs .
+ \ get(a:config, 'useTabs', g:prettier#config#use_tabs) .
\ ' --semi ' .
- \ g:prettier#config#semi .
+ \ get(a:config, 'semi', g:prettier#config#semi) .
\ ' --single-quote ' .
- \ g:prettier#config#single_quote .
+ \ get(a:config, 'singleQuote', g:prettier#config#single_quote) .
\ ' --bracket-spacing ' .
- \ g:prettier#config#bracket_spacing .
+ \ get(a:config, 'bracketSpacing', g:prettier#config#bracket_spacing) .
\ ' --jsx-bracket-same-line ' .
- \ g:prettier#config#jsx_bracket_same_line .
+ \ get(a:config, 'jsxBracketSameLine', g:prettier#config#jsx_bracket_same_line) .
\ ' --trailing-comma ' .
- \ g:prettier#config#trailing_comma .
+ \ get(a:config, 'trailingComma', g:prettier#config#trailing_comma) .
\ ' --parser ' .
- \ g:prettier#config#parser .
+ \ get(a:config, 'parser', g:prettier#config#parser) .
\ ' --stdin '
return cmd
endfunction
diff --git a/doc/prettier.txt b/doc/prettier.txt
index 584595a..08550ed 100644
--- a/doc/prettier.txt
+++ b/doc/prettier.txt
@@ -5,6 +5,7 @@ CONTENTS *vim-prettier-contents*
Introduction |vim-prettier-introduction|
Install |vim-prettier-install|
Usage |vim-prettier-usage|
+Configuration |vim-prettier-configuration|
Requirements |vim-prettier-requirements|
==============================================================================
@@ -13,8 +14,8 @@ INTRODUCTION *vim-prettier-introduction*
A vim plugin wrapper for prettier, pre-configured with
custom default prettier settings.
-By default it will auto format javascript files that have "@format"
-annotation in the header of the file.
+By default it will auto format javascript, typescript, less, scss and
+css files that have '@format' annotation in the header of the file.
When installed via vim-plug, a default prettier executable is installed inside
vim-prettier.
@@ -31,7 +32,9 @@ INSTALL *vim-prettier-install*
Install with [vim-plug](https://github.com/junegunn/vim-plug), assumes
node and yarn|npm installed globally.
>
- Plug 'mitermayer/vim-prettier', { 'do': 'yarn install', 'for': 'javascript' }
+ plug 'mitermayer/vim-prettier', {
+ \ 'do': 'yarn install',
+ \ 'for': ['javascript', 'typescript', 'css', 'less', 'scss'] }
<
If using other vim plugin managers or doing manual setup make sure to have
`prettier` installed globally or go to your vim-prettier directory and
@@ -40,21 +43,29 @@ either do `npm install` or `yarn install`
==============================================================================
USAGE *vim-prettier-usage*
-Prettier can be manualy triggered by:
+Prettier by default will run on auto save but can also
+be manualy triggered by:
>
<Leader>p
<
-Formats the entire buffer
+or
>
:Prettier
<
-Disable auto formatting of javascript files that have "@format" tag
+==============================================================================
+CONFIGURATION *vim-prettier-configuration*
+
+Disable auto formatting of files that have "@format" tag
>
let g:prettier#autoformat = 0
<
-Enable vim-prettier to run in javascript files without the "@format" doc tag
+The command `:Prettier` by default is synchronous but can be forced to be async
+>
+ let g:prettier#exec_cmd_async = 1
+<
+Enable vim-prettier to run in files without requiring the "@format" doc tag
>
- autocmd BufWritePre *.js call prettier#Prettier()
+ autocmd BufWritePre *.js,*.css,*.scss,*.less call prettier#Prettier()
<
Overwrite default configuration
>
@@ -82,7 +93,7 @@ Overwrite default configuration
" none|es5|all
g:prettier#config#trailing_comma = 'all'
- " flow|babylon
+ " flow|babylon|typescript|postcss
g:prettier#config#parser = 'flow'
<
==============================================================================
diff --git a/ftplugin/css.vim b/ftplugin/css.vim
new file mode 100644
index 0000000..92bc628
--- /dev/null
+++ b/ftplugin/css.vim
@@ -0,0 +1,10 @@
+let b:prettier_ft_default_args = {
+ \ 'parser': 'postcss'
+ \ }
+
+augroup Prettier
+ autocmd!
+ if g:prettier#autoformat
+ autocmd BufWritePre <buffer> call prettier#Autoformat()
+ endif
+augroup end
diff --git a/ftplugin/less.vim b/ftplugin/less.vim
new file mode 100644
index 0000000..92bc628
--- /dev/null
+++ b/ftplugin/less.vim
@@ -0,0 +1,10 @@
+let b:prettier_ft_default_args = {
+ \ 'parser': 'postcss'
+ \ }
+
+augroup Prettier
+ autocmd!
+ if g:prettier#autoformat
+ autocmd BufWritePre <buffer> call prettier#Autoformat()
+ endif
+augroup end
diff --git a/ftplugin/scss.vim b/ftplugin/scss.vim
new file mode 100644
index 0000000..92bc628
--- /dev/null
+++ b/ftplugin/scss.vim
@@ -0,0 +1,10 @@
+let b:prettier_ft_default_args = {
+ \ 'parser': 'postcss'
+ \ }
+
+augroup Prettier
+ autocmd!
+ if g:prettier#autoformat
+ autocmd BufWritePre <buffer> call prettier#Autoformat()
+ endif
+augroup end
diff --git a/ftplugin/typescript.vim b/ftplugin/typescript.vim
new file mode 100644
index 0000000..b654a48
--- /dev/null
+++ b/ftplugin/typescript.vim
@@ -0,0 +1,10 @@
+let b:prettier_ft_default_args = {
+ \ 'parser': 'typescript'
+ \ }
+
+augroup Prettier
+ autocmd!
+ if g:prettier#autoformat
+ autocmd BufWritePre <buffer> call prettier#Autoformat()
+ endif
+augroup end
diff --git a/package.json b/package.json
index b3cfd9d..aac0aea 100644
--- a/package.json
+++ b/package.json
@@ -1,13 +1,13 @@
{
"name": "vim-prettier",
"author": "Mitermayer Reis <mitermayer.reis@gmail.com>",
- "version": "0.0.1",
+ "version": "0.0.5",
"description": "Vim plugin for prettier",
"repository": {
"type": "git",
"url": "git://github.com/mitermayer/vim-prettier.git"
},
"dependencies": {
- "prettier": "^1.3.1"
+ "prettier": "^1.4.2"
}
}
diff --git a/plugin/prettier.vim b/plugin/prettier.vim
index 6b5fee4..729ec2e 100644
--- a/plugin/prettier.vim
+++ b/plugin/prettier.vim
@@ -4,8 +4,12 @@ endif
let g:loaded_prettier = 1
" => Plugin config
+" autoformating enabled by default upon saving
let g:prettier#autoformat = get(g:, 'g:prettier#autoformat', 1)
+" calling :Prettier by default runs synchronous
+let g:prettier#exec_cmd_async = get(g:, 'g:prettier#exec_cmd_async', 0)
+
" => Prettier CLI config
" max line lengh that prettier will wrap on
let g:prettier#config#print_width = get(g:, 'g:prettier#config#print_width', 80)
@@ -31,11 +35,13 @@ let g:prettier#config#jsx_bracket_same_line = get(g:,'g:prettier#config#jsx_brac
" none|es5|all
let g:prettier#config#trailing_comma = get(g:,'g:prettier#config#trailing_comma', 'all')
-" flow|babylon
+" flow|babylon|typescript|postcss
let g:prettier#config#parser = get(g:,'g:prettier#config#parser', 'flow')
-command! Prettier call prettier#Prettier()
+" synchronous by default
+command! Prettier call prettier#Prettier(g:prettier#exec_cmd_async)
+" map command
if !hasmapto('<Plug>(Prettier)') && maparg('<Leader>p', 'n') ==# ''
nmap <unique> <Leader>p <Plug>(Prettier)
endif