11let s: sourced_script_type = 0
22let s: other_lines_type = 1
33
4+ " *************************************************
5+ " * Globals
6+ " *************************************************
7+
48" 's:tfields' contains the time fields.
59let s: tfields = [' elapsed' , ' self+sourced' , ' self' ]
610
@@ -23,6 +27,10 @@ function! s:ColBounds()
2327endfunction
2428let s: col_bounds = s: ColBounds ()
2529
30+ " *************************************************
31+ " * Utils
32+ " *************************************************
33+
2634function ! s: Contains (list , element)
2735 return index (a: list , a: element ) !=# -1
2836endfunction
@@ -91,6 +99,10 @@ function! s:Echo(echo_list)
9199 echohl None
92100endfunction
93101
102+ " *************************************************
103+ " * Core
104+ " *************************************************
105+
94106function ! s: SetFile ()
95107 try
96108 silent file [startuptime]
@@ -161,8 +173,8 @@ function! s:Profile(callback, tries, file)
161173 \ ' exit_cb' : function (l: tmp .exit , l: env ),
162174 \ ' hidden' : 1
163175 \ }
164- " XXX: A new buffer is created each time this is run. Running many times will
165- " result in large buffer numbers.
176+ " XXX: A new buffer is created each time this is run. Running many times
177+ " will result in large buffer numbers.
166178 let l: env .bufnr = term_start (l: command , l: options )
167179 call term_sendkeys (l: env .bufnr , l: exit_keys )
168180 endif
@@ -171,7 +183,7 @@ endfunction
171183" Returns a nested list. The top-level list entries correspond to different
172184" profiling sessions. The next level lists contain the parsed lines for each
173185" profiling session. Each line is represented with a dict.
174- function ! s: Extract (file )
186+ function ! s: Extract (file , options )
175187 let l: result = []
176188 let l: lines = readfile (a: file )
177189 for l: line in l: lines
@@ -195,7 +207,16 @@ function! s:Extract(file)
195207 else
196208 let l: item .elapsed = str2float (l: times [1 ])
197209 endif
198- call add (l: result [-1 ], l: item )
210+ let l: types = []
211+ if a: options .sourced_events
212+ call add (l: types , s: sourced_script_type )
213+ endif
214+ if a: options .other_events
215+ call add (l: types , s: other_lines_type )
216+ endif
217+ if s: Contains (l: types , l: item .type )
218+ call add (l: result [-1 ], l: item )
219+ endif
199220 endfor
200221 return l: result
201222endfunction
@@ -447,12 +468,9 @@ function! startuptime#Main(file, winid, bufnr, options)
447468 if winbufnr (a: winid ) !=# a: bufnr | return | endif
448469 call win_gotoid (a: winid )
449470 normal ! ggdG
450- let l: items = s: Extract (a: file )
471+ let l: items = s: Extract (a: file, a: options )
451472 let l: items = s: Consolidate (l: items )
452473 let l: items = s: Augment (l: items , a: options )
453- if ! a: options .all
454- call filter (l: items , ' v:val.type !=# s:other_lines_type' )
455- endif
456474 let l: Compare = {i1, i2 - > i1.time == # i2.time ? 0 : (i1.time <# i2.time ? 1 : -1 )}
457475 if a: options .sort
458476 call sort (l: items , l: Compare )
@@ -498,29 +516,45 @@ function! s:New(mods)
498516 return 1
499517endfunction
500518
501- " Usage:
502- " :StartupTime [--nosort] [--all] [--self] [--tries INT]
503- function ! startuptime#StartupTime (mods, ... )
504- " TODO: implement this with a loop
505- " TODO: throw error for unknown options
519+ function ! s: Options (args )
506520 let l: options = {
507- \ ' sort' : 1 ,
508- \ ' all' : 0 ,
509- \ ' self' : 0 ,
510- \ ' tries' : 1
521+ \ ' sort' : g: startuptime_sort ,
522+ \ ' tries' : g: startuptime_tries ,
523+ \ ' sourced_events' : g: startuptime_sourced_events ,
524+ \ ' other_events' : g: startuptime_other_events ,
525+ \ ' self' : g: startuptime_self
511526 \ }
512- let l: mods = split (a: mods )
513- let l: args = a: 000
514- let l: options .sort = ! s: Contains (l: args , ' --nosort' )
515- let l: options .all = s: Contains (l: args , ' --all' )
516- let l: options .self = s: Contains (l: args , ' --self' )
517- try
518- let l: _tries = str2nr (l: args [index (l: args , ' --tries' ) + 1 ])
519- if l: _tries ># l: options .tries
520- let l: options .tries = l: _tries
527+ let l: idx = 0
528+ while l: idx <# len (a: args )
529+ let l: arg = a: args [l: idx ]
530+ if l: arg == # ' --sort' || l: arg == # ' --no-sort'
531+ let l: options .sort = l: arg == # ' --sort'
532+ elseif l: arg == # ' --tries'
533+ let l: idx += 1
534+ let l: arg = a: args [l: idx ]
535+ let l: options .tries = str2nr (l: arg )
536+ elseif l: arg == # ' --sourced-events' || l: arg == # ' --no-sourced-events'
537+ let l: options .sourced_events = l: arg == # ' --sourced-events'
538+ elseif l: arg == # ' --other-events' || l: arg == # ' --no-other-events'
539+ let l: options .other_events = l: arg == # ' --other-events'
540+ else
541+ throw ' vim-startuptime: unknown argument (' . l: arg . ' )'
521542 endif
522- catch
523- endtry
543+ let l: idx += 1
544+ endwhile
545+ return l: options
546+ endfunction
547+
548+ " Usage:
549+ " :StartupTime
550+ " \ [--sort] [--nosort]
551+ " \ [--sourced-events] [--no-sourced-events]
552+ " \ [--other-events] [--no-other-events]
553+ " \ [--self] [--no-self]
554+ " \ [--tries INT]
555+ function ! startuptime#StartupTime (mods, ... )
556+ let l: mods = split (a: mods )
557+ let l: options = s: Options (a: 000 )
524558 if ! s: New (l: mods )
525559 throw ' vim-startuptime: couldn'' t create new buffer'
526560 endif
0 commit comments