Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for require-pragma and depracting previous naive implemenation #206

Merged
merged 2 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ let g:prettier#config#prose_wrap = 'preserve'
" css|strict|ignore
" default: 'css'
let g:prettier#config#html_whitespace_sensitivity = 'css'

" false|true
" default: 'false'
let g:prettier#config#require_pragma = 'false'
```

### REQUIREMENT(S)
Expand Down
33 changes: 6 additions & 27 deletions autoload/prettier.vim
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,9 @@ function! prettier#PrettierCli(user_input) abort
endif
endfunction

" Allows @format pragma support
" Allows @format and @prettier pragma support upon saving
function! prettier#Autoformat(...) abort
let l:curPos = getpos('.')
let l:maxLineLookup = 50
let l:maxTimeLookupMs = 500
let l:pattern = '@format'
let l:search = @/
let l:winview = winsaveview()

" we need to move selection to the top before looking up to avoid
" scanning a very long file
call cursor(1, 1)

" Search starting at the start of the document
if search(l:pattern, 'n', l:maxLineLookup, l:maxTimeLookupMs) > 0
" autoformat async
call prettier#Prettier(1)
endif

" Restore the selection and if greater then before it defaults to end
call cursor(l:curPos[1], l:curPos[2])

" Restore view
call winrestview(l:winview)

" Restore search
let @/=l:search
call prettier#Prettier(1, 1, line('$'), 0, { 'requirePragma': 'true'})
endfunction

" Main prettier command
Expand All @@ -71,9 +47,12 @@ function! prettier#Prettier(...) abort
let l:endSelection = a:0 > 2 ? a:3 : line('$')
let l:hasSelection = a:0 > 2 ? 1 : 0
let l:partialFormat = a:0 > 3 && a:4 ? a:4 : 0
let l:config = getbufvar(bufnr('%'), 'prettier_ft_default_args', {})
let l:partialFormatEnabled = l:hasSelection && l:partialFormat

let l:overWrite = a:0 > 4 ? a:5 : {}
let l:bufferConfig = getbufvar(bufnr('%'), 'prettier_ft_default_args', {})
let l:config = extend(l:bufferConfig, l:overWrite)

if l:execCmd != -1
" TODO
" => we should make sure we can resolve --range-start and --range-end when required
Expand Down
2 changes: 2 additions & 0 deletions autoload/prettier/resolver/config.vim
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ function! prettier#resolver#config#resolve(config, hasSelection, start, end) abo
\ ' --html-whitespace-sensitivity ' .
\ get(a:config, 'htmlWhitespaceSensitivity', g:prettier#config#html_whitespace_sensitivity) .
\ ' --stdin-filepath="'.simplify(expand('%:p')).'"' .
\ ' --require-pragma=' .
\ get(a:config, 'requirePragma', g:prettier#config#require_pragma) .
\ ' --loglevel error '.
\ ' --stdin '
return l:cmd
Expand Down
4 changes: 4 additions & 0 deletions doc/prettier.txt
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ However they can be configured by:

" css|strict|ignore
let g:prettier#config#html_whitespace_sensitivity = 'css'

" false|true
" default: 'false'
let g:prettier#config#require_pragma = 'false'
<
==============================================================================
REQUIREMENT(S) *vim-prettier-requirements*
Expand Down
7 changes: 0 additions & 7 deletions ftplugin/html.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,3 @@ if &filetype !~# 'markdown'
\ 'parser': 'html',
\ }
endif

augroup Prettier
autocmd!
if g:prettier#autoformat
autocmd BufWritePre *.html call prettier#Autoformat()
endif
augroup end
10 changes: 10 additions & 0 deletions plugin/prettier.vim
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ let g:prettier#config#arrow_parens = get(g:,'prettier#config#arrow_parens', 'avo
" default: 'none'
let g:prettier#config#trailing_comma = get(g:,'prettier#config#trailing_comma', 'none')

" restrict itself to only format files that contain a special comment @prettier or @format
let g:prettier#config#require_pragma= get(g:, 'prettier#config#require_pragma', 'false')

" synchronous by default
command! -nargs=? -range=% Prettier call prettier#Prettier(g:prettier#exec_cmd_async, <line1>, <line2>, g:prettier#partial_format)

Expand Down Expand Up @@ -133,3 +136,10 @@ nnoremap <silent> <Plug>(PrettierVersion) :PrettierVersion<CR>
nnoremap <silent> <Plug>(PrettierCli) :PrettierCli<CR>
nnoremap <silent> <Plug>(PrettierCliVersion) :PrettierCliVersion<CR>
nnoremap <silent> <Plug>(PrettierCliPath) :PrettierCliPath<CR>

augroup Prettier
autocmd!
if g:prettier#autoformat
autocmd BufWritePre *.js,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.json,*.graphql,*.md,*.vue,*.yaml,*.html call prettier#Autoformat()
endif
augroup end