-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfmt.vim
More file actions
85 lines (77 loc) · 2.68 KB
/
fmt.vim
File metadata and controls
85 lines (77 loc) · 2.68 KB
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
if exists('g:loaded_phpfmt_fmt_autoload') || !exists('g:loaded_phpfmt_plugin')
finish
endif
let g:loaded_phpfmt_fmt_autoload = 1
function! phpfmt#fmt#autoformat() abort "{{{
if get(g:, 'phpfmt_autosave', 1)
call phpfmt#fmt#format()
endif
endfunction "}}}
function! phpfmt#fmt#format() abort "{{{
if g:phpfmt_experimental == 1
" Using winsaveview to save/restore cursor state has the problem of
" closing folds on save. One fix is to use mkview instead. Unfortunately,
" this sometimes causes other bad side effects, and still closes all folds
" if foldlevel>0.
let l:curw = {}
try
mkview!
catch
let l:curw=winsaveview()
endtry
else
" Save cursor position and many other things.
let l:curw=winsaveview()
endif
" Write current unsaved buffer to a temp file
if exists('g:phpfmt_tmp_dir')
let l:tmpdir = g:phpfmt_tmp_dir . expand('%:h')
if !isdirectory(l:tmpdir)
exe 'silent! !mkdir -p ' . shellescape(l:tmpdir, 1)
endif
let l:tmpname = g:phpfmt_tmp_dir . expand('%')
else
let l:tmpname = tempname()
endif
call writefile(getline(1, '$'), l:tmpname)
if g:phpfmt_experimental == 1
" save our undo file to be restored after we are done. This is needed to
" prevent an additional undo jump due to BufWritePre auto command and also
" restore 'redo' history because it's getting being destroyed every
" BufWritePre
let tmpundofile=tempname()
exe 'wundo! ' . tmpundofile
endif
" populate the final command with user based fmt options
let command = g:phpfmt_command . ' ' . g:phpfmt_options
" execute our system command...
call system(command . ' ' . l:tmpname)
" remove undo point caused via BufWritePre
try | silent undojoin | catch | endtry
" reload buffer
let old_fileformat = &fileformat
let formatted_content = readfile(l:tmpname)
" copy the content from formatted tempfile
call writefile(formatted_content, expand('%'))
" delete tempfile
call delete(l:tmpname)
silent edit!
let &fileformat = old_fileformat
let &syntax = &syntax
if g:phpfmt_experimental == 1
" restore our undo history
silent! exe 'rundo ' . tmpundofile
call delete(tmpundofile)
endif
if g:phpfmt_experimental == 1
" Restore our cursor/windows positions, folds, etc.
if empty(l:curw)
silent! loadview
else
call winrestview(l:curw)
endif
else
" Restore our cursor/windows positions.
call winrestview(l:curw)
endif
endfunction "}}}