aboutsummaryrefslogtreecommitdiff
path: root/autoload/prettier.vim
blob: 4eb4a24ad58ccc8597303f681fcf1cccfc7d3796 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
" vim-prettier: A vim plugin wrapper for prettier, pre-configured with custom default prettier settings.
"
" Script Info  {{{
"==========================================================================================================
" Name Of File: prettier.vim
"  Description: A vim plugin wrapper for prettier, pre-configured with custom default prettier settings.
"   Maintainer: Mitermayer Reis <mitermayer.reis at gmail.com>
"      Version: 1.0.0-beta
"        Usage: Use :help vim-prettier-usage, or visit https://github.com/prettier/vim-prettier
"
"==========================================================================================================
" }}}

" Displays the resolve prettier CLI path
function! prettier#PrettierCliPath() abort
  let l:execCmd = prettier#resolver#executable#getPath()

  if l:execCmd != -1
    echom l:execCmd
  else
    call prettier#logging#error#log('EXECUTABLE_NOT_FOUND_ERROR')
  endif
endfunction

" Allows user commands to be passed straight to the prettier CLI
function! prettier#PrettierCli(user_input) abort
  let l:execCmd = prettier#resolver#executable#getPath()

  if l:execCmd != -1
    let l:out = system(l:execCmd. ' ' . a:user_input)
    echom l:out
  else
    call prettier#logging#error#log('EXECUTABLE_NOT_FOUND_ERROR')
  endif
endfunction

" Allows @format and @prettier pragma support upon saving
function! prettier#Autoformat(...) abort
  let l:autoformat = g:prettier#autoformat_config_present ?
        \ prettier#IsConfigPresent(g:prettier#autoformat_config_files) :
        \ g:prettier#autoformat

  if l:autoformat
    call prettier#Prettier(1, 1, line('$'), 0, {
      \ 'requirePragma': g:prettier#autoformat_require_pragma ? 'true' : 'false'
      \ })
  endif
endfunction

" Main prettier command
function! prettier#Prettier(...) abort
  let l:execCmd = prettier#resolver#executable#getPath()
  let l:async = a:0 > 0 ? a:1 : 0
  let l:startSelection = a:0 > 1 ? a:2 : 1
  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: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
    "    => when the above is required we should also update l:startSelection to '1' and l:endSelection to line('$')
    let l:cmd = l:execCmd . prettier#resolver#config#resolve(
          \ prettier#resolver#preset#resolve(l:config),
          \ l:partialFormatEnabled,
          \ l:startSelection, 
          \ l:endSelection)

    " close quickfix if it is opened
    call prettier#utils#quickfix#close()

    " we will be using portion formatting, so we need to send entire buffer to prettier
    if l:partialFormatEnabled
      let l:startSelection = 1
      let l:endSelection = line('$')
    endif

    " format buffer
    call prettier#job#runner#run(l:cmd, l:startSelection, l:endSelection, l:async)
  else
    call prettier#logging#error#log('EXECUTABLE_NOT_FOUND_ERROR')
  endif
endfunction

" Set autoformat toggle based on whether config file was found.
function! prettier#IsConfigPresent(config_files) abort
  for config_file in a:config_files
    if filereadable(findfile(config_file, '.;'))
      return 1
    endif
  endfor
  return 0
endfunction