Skip to content

Commit b843d4d

Browse files
tenfyzhongianding1
authored andcommitted
Support difficulty and status filter.
close ianding1#14
1 parent c9c9982 commit b843d4d

File tree

1 file changed

+119
-11
lines changed

1 file changed

+119
-11
lines changed

autoload/leetcode.vim

Lines changed: 119 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,20 @@ function! s:PrintProblemList() abort
205205
let title_width = s:MaxWidthOfKey(sorted_problems, 'title', 1) + 4
206206
let max_frequency = s:Max(map(copy(sorted_problems), 'v:val["frequency"]'))
207207

208+
call append('$', [printf('## Difficulty [%s]', b:leetcode_difficulty), ''])
209+
let b:leetcode_difficulty_start_line = line('$')
210+
let difficulty_line = <SID>FormatIntoColumns(<SID>difficulty_tags())
211+
call append('$', difficulty_line)
212+
let b:leetcode_difficulty_end_line = line('$')
213+
call append('$', '')
214+
215+
call append('$', [printf('## Status [%s]', <SID>state2status(b:leetcode_state)), ''])
216+
let b:leetcode_status_start_line = line('$')
217+
let status_line = <SID>FormatIntoColumns(<SID>status_tags())
218+
call append('$', status_line)
219+
let b:leetcode_status_end_line = line('$')
220+
call append('$', '')
221+
208222
let sort_column_name = s:sort_column_to_name_map[b:leetcode_sort_column]
209223
let sort_order_name = s:sort_order_to_name_map[b:leetcode_sort_order]
210224
call append('$', ['## Problem List',
@@ -235,6 +249,10 @@ function! s:PrintProblemList() abort
235249

236250
let problem_lines = []
237251
for problem in sorted_problems
252+
if b:leetcode_difficulty != 'All' && b:leetcode_difficulty != problem['level'] ||
253+
\ b:leetcode_state != 'All' && b:leetcode_state != problem['state']
254+
continue
255+
endif
238256
let title = substitute(problem['title'], '`', "'", 'g')
239257
if problem['paid_only']
240258
let title .= ' [P]'
@@ -250,6 +268,32 @@ function! s:PrintProblemList() abort
250268
let b:leetcode_problem_end_line = line('$')
251269
endfunction
252270

271+
function! s:difficulty_tags()
272+
let tags = {'All':0, 'Easy':0, 'Medium': 0, 'Hard':0}
273+
for problem in b:leetcode_problems
274+
let tags['All'] += 1
275+
let tags[problem['level']] += 1
276+
endfor
277+
return [
278+
\ printf("All:%d", tags['All']),
279+
\ printf("Easy:%d", tags['Easy']),
280+
\ printf("Medium:%d", tags['Medium']),
281+
\ printf("Hard:%d", tags['Hard'])]
282+
endfunction
283+
284+
function! s:status_tags()
285+
let tags = {'All':0, 'Todo':0, 'Solved':0, 'Attempted':0}
286+
for problem in b:leetcode_problems
287+
let tags['All'] += 1
288+
let tags[<SID>state2status(problem['state'])] += 1
289+
endfor
290+
return [
291+
\ printf("All:%d", tags['All']),
292+
\ printf("Todo:%d", tags['Todo']),
293+
\ printf("Solved:%d", tags['Solved']),
294+
\ printf("Attempted:%d", tags['Attempted'])]
295+
endfunction
296+
253297
function! s:ListProblemsOfTopic(topic_slug, refresh) abort
254298
let buf_name = 'leetcode:///problems/topic/' . a:topic_slug
255299
if buflisted(buf_name)
@@ -271,6 +315,8 @@ function! s:ListProblemsOfTopic(topic_slug, refresh) abort
271315
let b:leetcode_buffer_topic = a:topic_slug
272316
let expr = printf('leetcode.get_problems_of_topic("%s")', a:topic_slug)
273317
let b:leetcode_downloaded_problems = py3eval(expr)['problems']
318+
let b:leetcode_difficulty = 'All'
319+
let b:leetcode_state = 'All'
274320
let b:leetcode_sort_column = 'id'
275321
let b:leetcode_sort_order = 'asc'
276322
endif
@@ -340,6 +386,8 @@ function! s:ListProblemsOfCompany(company_slug, refresh) abort
340386
let expr = printf('leetcode.get_problems_of_company("%s")',
341387
\ a:company_slug)
342388
let b:leetcode_downloaded_problems = py3eval(expr)['problems']
389+
let b:leetcode_difficulty = 'All'
390+
let b:leetcode_state = 'All'
343391
let b:leetcode_sort_column = 'id'
344392
let b:leetcode_sort_order = 'asc'
345393
endif
@@ -391,6 +439,8 @@ function! leetcode#ListProblems(refresh) abort
391439
let b:leetcode_buffer_type = 'all'
392440
let expr = printf('leetcode.get_problems(["all"])')
393441
let b:leetcode_downloaded_problems = py3eval(expr)
442+
let b:leetcode_difficulty = 'All'
443+
let b:leetcode_state = 'All'
394444
let b:leetcode_sort_column = 'id'
395445
let b:leetcode_sort_order = 'asc'
396446
endif
@@ -462,10 +512,36 @@ function! s:HandleProblemListCR() abort
462512
return
463513
endif
464514

515+
if line_nr >= b:leetcode_difficulty_start_line &&
516+
\ line_nr < b:leetcode_difficulty_end_line
517+
let difficulty_slug = expand('<cWORD>')
518+
let difficulty_slug = <SID>TagName(difficulty_slug)
519+
if difficulty_slug != ''
520+
if b:leetcode_difficulty != difficulty_slug
521+
let b:leetcode_difficulty = difficulty_slug
522+
call <SID>redraw()
523+
endif
524+
endif
525+
endif
526+
527+
if line_nr >= b:leetcode_status_start_line &&
528+
\ line_nr < b:leetcode_status_end_line
529+
let status_slug = expand('<cWORD>')
530+
let status_slug = <SID>TagName(status_slug)
531+
if status_slug != ''
532+
let new_state = <SID>status2state(status_slug)
533+
if b:leetcode_state != new_state
534+
let b:leetcode_state = new_state
535+
call <SID>redraw()
536+
endif
537+
endif
538+
endif
539+
465540
if line_nr >= b:leetcode_problem_start_line &&
466541
\ line_nr < b:leetcode_problem_end_line
467-
let problem_nr = line_nr - b:leetcode_problem_start_line
468-
let problem_slug = b:leetcode_problems[problem_nr]['slug']
542+
let problem_id = <SID>ProblemIdFromNr(line_nr)
543+
let problem = <SID>GetProblem(problem_id)
544+
let problem_slug = problem['slug']
469545
let problem_ext = s:SolutionFileExt(g:leetcode_solution_filetype)
470546
let problem_file_name = printf('%s.%s', s:SlugToFileName(problem_slug),
471547
\ problem_ext)
@@ -480,6 +556,40 @@ function! s:HandleProblemListCR() abort
480556
endif
481557
endfunction
482558

559+
function! s:status2state(status)
560+
if a:status == 'Todo'
561+
return ' '
562+
elseif a:status == 'Solved'
563+
return 'X'
564+
elseif a:status == 'Attempted'
565+
return '?'
566+
else
567+
return 'All'
568+
endif
569+
endfunction
570+
571+
function! s:state2status(state)
572+
if a:state == ' '
573+
return 'Todo'
574+
elseif a:state == 'X'
575+
return 'Solved'
576+
elseif a:state == '?'
577+
return 'Attempted'
578+
else
579+
return 'All'
580+
endif
581+
endfunction
582+
583+
function! s:redraw()
584+
if b:leetcode_buffer_type ==# 'all'
585+
call leetcode#ListProblems('redraw')
586+
elseif b:leetcode_buffer_type ==# 'topic'
587+
call s:ListProblemsOfTopic(b:leetcode_buffer_topic, 'redraw')
588+
elseif b:leetcode_buffer_type ==# 'company'
589+
call s:ListProblemsOfCompany(b:leetcode_buffer_company, 'redraw')
590+
endif
591+
endfunction
592+
483593
function! s:TagName(tag)
484594
return substitute(a:tag, ':\d*$', '', 'g')
485595
endfunction
@@ -547,13 +657,7 @@ function! s:HandleProblemListSort() abort
547657
let b:leetcode_sort_column = s:column_choice_map[column_choice]
548658
let b:leetcode_sort_order = s:order_choice_map[order_choice]
549659

550-
if b:leetcode_buffer_type ==# 'all'
551-
call leetcode#ListProblems('redraw')
552-
elseif b:leetcode_buffer_type ==# 'topic'
553-
call s:ListProblemsOfTopic(b:leetcode_buffer_topic, 'redraw')
554-
elseif b:leetcode_buffer_type ==# 'company'
555-
call s:ListProblemsOfCompany(b:leetcode_buffer_company, 'redraw')
556-
endif
660+
call <SID>redraw()
557661
endfunction
558662

559663
let s:file_type_to_ext = {
@@ -997,8 +1101,9 @@ function! s:HandleProblemListS() abort
9971101
let line_nr = line('.')
9981102
if line_nr >= b:leetcode_problem_start_line &&
9991103
\ line_nr < b:leetcode_problem_end_line
1000-
let problem_nr = line_nr - b:leetcode_problem_start_line
1001-
let problem_slug = b:leetcode_problems[problem_nr]['slug']
1104+
let problem_id = <SID>ProblemIdFromNr(line_nr)
1105+
let problem = <SID>GetProblem(problem_id)
1106+
let problem_slug = problem['slug']
10021107
call s:ListSubmissions(problem_slug, 0)
10031108
endif
10041109
endfunction
@@ -1169,6 +1274,9 @@ function! s:FormatIntoColumns(words) abort
11691274
endif
11701275

11711276
let num_rows = float2nr(ceil(len(a:words) / num_columns))
1277+
if num_rows == 0
1278+
let num_rows = 1
1279+
endif
11721280
let lines = []
11731281

11741282
for i in range(num_rows)

0 commit comments

Comments
 (0)