-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython_autoload.vim
More file actions
139 lines (127 loc) · 5.11 KB
/
python_autoload.vim
File metadata and controls
139 lines (127 loc) · 5.11 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" GET THE PYTHON PROJECT NAME
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! ftplugin#python#ProjectName()
let name = system("grep 'name=' setup.py | head -n1 | cut -d '=' -f 2")
if name =~ 'No such file or directory'
let name = system("grep 'name =' pyproject.toml | head -n1 | cut -d '=' -f 2")
endif
" Check if name includes 'No such file or directory' and try pyproject.py
let name = substitute(name, '-', '_', 'g')
let name = substitute(name, ' ', '', 'g')
let name = substitute(name, '"', '', 'g')
let name = substitute(name, ',', '', 'g')
let name = substitute(name, '\n', '', 'g')
return name
endfunction
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" CTRLP + project dirs
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! ftplugin#python#CtrlpProject(...)
if a:0
let prefix = a:1
else
let prefix = ""
endif
exec ":CtrlP " . ftplugin#python#ProjectName() . "/" . prefix
endfunction
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" SWITCH BETWEEN TEST AND PRODUCTION CODE
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! ftplugin#python#OpenTestAlternate()
let new_file = ftplugin#python#AlternateForCurrentFile()
exec ':e ' . new_file
endfunction
function! ftplugin#python#AlternateForCurrentFile()
let project_name = ftplugin#python#ProjectName()
let current_file = expand("%")
let new_file = current_file
let in_test = match(current_file, '^tests/') != -1
let going_to_test = !in_test
if going_to_test
let new_file = substitute(new_file, '\/\(\w\{-}\.py$\)', '/test_\1', '')
let new_file = substitute(new_file, '^' . project_name . '/', 'tests/', '')
else
let new_file = substitute(new_file, '\/test_\(\w\{-}\.py$\)', '/\1', '')
let new_file = substitute(new_file, '^tests/', project_name . '/', '')
endif
return new_file
endfunction
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" RUN BIN FILE
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! ftplugin#python#RunBin(filename)
" Write the file and exec it using node
:w
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
exec ":!python " . a:filename
endfunction
function! ftplugin#python#SetBinFile()
" Set the spec file that tests will be run for.
let t:grb_bin_file=@%
endfunction
function! ftplugin#python#RunBinFile(...)
if a:0
let command_suffix = a:1
else
let command_suffix = ""
endif
" Run the tests for the previously-marked file.
let is_bin_file = match(expand("%"), '^bin\/') != -1
if is_bin_file
call ftplugin#python#SetBinFile()
elseif !exists("t:grb_bin_file")
return
end
call ftplugin#python#RunBin(t:grb_bin_file . command_suffix)
endfunction
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" RUNNING TESTS
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! ftplugin#python#RunTests(test_name)
" Write the file and run tests for the given filename
:w
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
let project_name = ftplugin#python#ProjectName()
exec ":!poetry run test " . a:test_name
endfunction
function! ftplugin#python#SetTestFile()
" Set the spec file that tests will be run for.
let t:grb_test_file=@%
endfunction
function! ftplugin#python#RunTestFile(...)
if a:0
let command_suffix = a:1
else
let command_suffix = ""
endif
" Run the tests for the previously-marked file.
let in_test_file = match(expand("%"), 'test_') != -1
if in_test_file
call ftplugin#python#SetTestFile()
elseif !exists("t:grb_test_file")
return
end
call ftplugin#python#RunTests(t:grb_test_file . command_suffix)
endfunction
function! ftplugin#python#TranslateTestFile(filename)
let test_name = substitute(a:filename, '/', '.', 'g')
let test_name = substitute(test_name, '.py', '', '')
return test_name
endfunction
function! ftplugin#python#RunNearestTest()
let test_line_number = line('.')
let module_details = system('line_to_path ' . expand("%") . " " . test_line_number)
let module_details = substitute(module_details, '\n', '', '')
call ftplugin#python#RunTestFile(' -k ' . module_details)
endfunction