-
Notifications
You must be signed in to change notification settings - Fork 0
/
firefox.vim
255 lines (204 loc) · 5.91 KB
/
firefox.vim
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
" This
"
"
"
"
"
" Build related variables
let s:build_cmd = "mach"
let s:build_args = "build"
" Run related variables
let s:run_cmd = "mach"
" Run related variables
let s:debug_cmd = "gdb"
let s:debug_args = "debug"
" This config how we make sure it is root directory
let s:root_folder_criteria = ['mach', 'GNUmakefile']
" Just a utility function
function! s:ListToPath(list)
return '/' . join(a:list, '/') . '/'
endfunction
function! s:BuildArgs(args)
return ' ' . join(a:args, ' ')
endfunction
let s:browsertest_criteria = ['browser.ini']
function! s:IsBrowsertest()
for item in s:browsertest_criteria
let find = findfile(item, s:full_dir_path)
if find == ''
return 0
endif
endfor
return 1
endfunction
let s:mochitest_criteria = ['mochitest.ini']
function! s:IsMochitest()
for item in s:mochitest_criteria
let find = findfile(item, s:full_dir_path)
if find == ''
return 0
endif
endfor
return 1
endfunction
let s:xpcshelltest_criteria = ['xpcshell.ini']
function! s:IsXpcshelltest()
for item in s:xpcshelltest_criteria
let find = findfile(item, s:full_dir_path)
if find == ''
return 0
endif
endfor
return 1
endfunction
" This function keeping going to parent folder until hit a folder
" with all 'root_folder_criteria' in it.
function! s:FindRootFolder()
let path = split(expand('%:p'), '/')
while path != []
let search_folder = '/' . join(path, '/')
for item in s:root_folder_criteria
let find = findfile(item, search_folder)
if find == ''
break
endif
endfor
if find != ''
let s:root_dir_list = split(search_folder, '/')
let s:root_dir_path = s:ListToPath(s:root_dir_list)
break
endif
let path = path[:-2]
endwhile
endfunction
let s:obj_dir_criteria = ['dist', 'build']
function! s:FindObjFolder()
let dirs = globpath(s:root_dir_path, '*/', 0, 1)
call filter(dirs, 'isdirectory(v:val)')
for dir in dirs
for item in s:obj_dir_criteria
let find = finddir(item, dir)
if find == ''
break
endif
endfor
if find != ''
let s:obj_dir_list = split(dir, '/')
let s:obj_dir_path = s:ListToPath(s:obj_dir_list)
return
endif
endfor
endfunction
" Setup current folder, current file & root folder
function! s:InitScriptVariables()
let current_path = split(expand('%:p'), '/')
if current_path == []
return
endif
" Get full path
let s:full_dir_list = split(expand('%:p:h'), '/')
let s:full_dir_path = s:ListToPath(s:full_dir_list)
" Get the folder we call VIM
let s:current_folder_list = split(expand("<sfile>:p:h"), '/')
let s:current_folder_str = s:ListToPath(s:current_folder_list)
" Get file name
let s:file_str = current_path[-1]
" Get root folder
call s:FindRootFolder()
let s:relative_path_list = s:full_dir_list[len(s:root_dir_list):]
let s:relative_path_str = '.' . s:ListToPath(s:relative_path_list)
echo s:relative_path_str
call s:FindObjFolder()
echo 'full folder :' . s:full_dir_path
echo 'current folder :' . s:current_folder_str
echo 'file :' . s:file_str
echo 'root :' . s:root_dir_path
echo 'obj :' . s:obj_dir_path
endfunction
let s:bps = []
function! s:FirefoxSetBP()
let bp_line = line('.')
let bp_file = expand('%:t')
call add(s:bps, "b " . bp_file . ":" . bp_line)
endfunction
function! s:GetBreakPoint()
let pending_on_cmd = "set breakpoint pending on"
let run_cmd = "run"
let config = [pending_on_cmd] + s:bps + [run_cmd]
let s:bp_data = join(config, "\n")
endfunction
function! s:RunOneQuickFixCmd(cmd)
tabdo ccl
tabnew
execute "AsyncRun " . a:cmd
quit
endfunction
function! s:FirefoxBuild()
call s:InitScriptVariables()
let args = s:BuildArgs(['build'])
let cmd = s:root_dir_path . s:build_cmd . args
call s:RunOneQuickFixCmd(cmd)
endfunction
function! s:FirefoxCustomRun()
call s:InitScriptVariables()
let args = s:BuildArgs(['test', '--enable-fission', s:relative_path_str . s:file_str])
let cmd = s:root_dir_path . s:run_cmd . args
echo "Run :" . cmd
call s:RunOneQuickFixCmd(cmd)
endfunction
function! s:FirefoxRun()
call s:InitScriptVariables()
if s:IsMochitest()
let args = s:BuildArgs(['mochitest', s:relative_path_str . s:file_str])
elseif s:IsBrowsertest()
let args = s:BuildArgs(['mochitest', '-f', 'browser', s:relative_path_str . s:file_str])
elseif s:IsXpcshelltest()
let args = s:BuildArgs(['xpcshell-test', s:relative_path_str . s:file_str])
else
echo "Run firefox"
let args = s:BuildArgs(['run'])
endif
let cmd = s:root_dir_path . s:run_cmd . args
echo "Run :" . cmd
call s:RunOneQuickFixCmd(cmd)
endfunction
function! s:WriteToTempFile(file, data)
new
setlocal buftype=nofile bufhidden=hide noswapfile nobuflisted
put=a:data
execute 'w ' a:file
q
endfunction
let s:bp_file = "/tmp/_firefox_bps"
function! s:FirefoxDebug()
call s:InitScriptVariables()
call s:GetBreakPoint()
call s:WriteToTempFile(s:bp_file, s:bp_data)
let firefox = s:obj_dir_path . 'dist/bin/firefox'
let profile = s:obj_dir_path . 'tmp/scratch_user'
"let args = s:BuildArgs(['-ex', 'run', '-q', '--args', firefox, '-no-remote', '-profile', profile])
let args = s:BuildArgs(['-x', s:bp_file, '-q', '--args', firefox, '-no-remote', '-profile', profile])
echo s:debug_cmd . args
execute '!' . s:debug_cmd . args
endfunction
" Configure mapping here
" Not yet support argment for build and run
command! -nargs=0 Fbuild call s:FirefoxBuild()
command! -nargs=0 Frun call s:FirefoxRun()
command! -nargs=0 Fdebug call s:FirefoxDebug()
command! -nargs=0 Fbp call s:FirefoxSetBP()
command! -nargs=0 Fc2 call s:FirefoxCustomRun()
if exists("g:EnableFirefoxBuildMapping")
if g:EnableFirefoxBuildMapping == 1
"F5 build coda
map <F5> :Fbuild<CR>
"F6 run
map <F6> :Frun<CR>
"F7 debug
map <F7> :Fdebug<CR>
"F8 set break point
map <F8> :Fbp<CR>
"F12 Custom run function 2
map <F12> :Fc2<CR>
endif
endif