Skip to content

Commit 60c9f07

Browse files
committed
Add refresh problem list
1 parent a339d0d commit 60c9f07

File tree

3 files changed

+54
-23
lines changed

3 files changed

+54
-23
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ Default value is `''`.
7070

7171
## Updates
7272

73-
- 2019/07/27: Support LeetCode China
73+
- 2019/07/27:
74+
+ Support LeetCode China accounts
75+
+ Refresh the problem list with `r`
7476
- 2019/07/23: Support topics and companies
7577

7678
## FAQ

autoload/leetcode.vim

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ function! s:SetupProblemListBuffer() abort
7272
setlocal bufhidden=hide
7373
nnoremap <silent> <buffer> <return> :call <SID>HandleProblemListCR()<cr>
7474
nnoremap <silent> <buffer> s :call <SID>HandleProblemListS()<cr>
75+
nnoremap <silent> <buffer> r :call <SID>HandleProblemListR()<cr>
7576
7677
syn match lcEasy /| Easy /hs=s+2
7778
syn match lcMedium /| Medium /hs=s+2
@@ -107,6 +108,8 @@ function! s:PrintProblemList(problems) abort
107108
\ '### Keys',
108109
\ ' - <cr> open the problem/go to the topic or company',
109110
\ ' - s view the submissions',
111+
\ ' - r refresh',
112+
\ '',
110113
\ '### Indicators',
111114
\ ' - [P] = paid-only problems',
112115
\ ''])
@@ -135,16 +138,23 @@ function! s:PrintProblemList(problems) abort
135138
let b:leetcode_problem_end_line = line('$')
136139
endfunction
137140

138-
function! s:ListProblemsOfTopic(topic_slug) abort
141+
function! s:ListProblemsOfTopic(topic_slug, refresh) abort
139142
let buf_name = 'leetcode:///problems/topic/' . a:topic_slug
140143
if buflisted(buf_name)
141144
execute bufnr(buf_name) . 'buffer'
142-
return
145+
if a:refresh
146+
setlocal modifiable
147+
silent! normal! ggdG
148+
else
149+
return
150+
endif
151+
else
152+
execute 'rightbelow new ' . buf_name
153+
call s:SetupProblemListBuffer()
154+
let b:leetcode_buffer_type = 'topic'
155+
let b:leetcode_buffer_topic = a:topic_slug
143156
endif
144157

145-
execute 'rightbelow new ' . buf_name
146-
call s:SetupProblemListBuffer()
147-
148158
let expr = printf('leetcode.get_problems_of_topic("%s")', a:topic_slug)
149159
let problems = py3eval(expr)['problems']
150160

@@ -164,16 +174,23 @@ function! s:ListProblemsOfTopic(topic_slug) abort
164174
silent! only
165175
endfunction
166176

167-
function! s:ListProblemsOfCompany(company_slug) abort
177+
function! s:ListProblemsOfCompany(company_slug, refresh) abort
168178
let bufname = 'leetcode:///problems/company/' . a:company_slug
169179
if buflisted(bufname)
170180
execute bufnr(bufname) . 'buffer'
171-
return
181+
if a:refresh
182+
setlocal modifiable
183+
silent! normal! ggdG
184+
else
185+
return
186+
endif
187+
else
188+
execute 'rightbelow new ' . bufname
189+
call s:SetupProblemListBuffer()
190+
let b:leetcode_buffer_type = 'company'
191+
let b:leetcode_buffer_company = a:company_slug
172192
endif
173193

174-
execute 'rightbelow new ' . bufname
175-
call s:SetupProblemListBuffer()
176-
177194
let expr = printf('leetcode.get_problems_of_company("%s")', a:company_slug)
178195
let problems = py3eval(expr)['problems']
179196

@@ -182,8 +199,6 @@ function! s:ListProblemsOfCompany(company_slug) abort
182199
let b:leetcode_company_start_line = 0
183200
let b:leetcode_company_end_line = 0
184201

185-
setlocal modifiable
186-
187202
call append('$', ['# LeetCode [company:' . a:company_slug . ']', ''])
188203

189204
call s:PrintProblemList(problems)
@@ -193,15 +208,24 @@ function! s:ListProblemsOfCompany(company_slug) abort
193208
silent! only
194209
endfunction
195210

196-
function! leetcode#ListProblems() abort
211+
function! leetcode#ListProblems(refresh) abort
197212
if s:CheckSignIn() == v:false
198213
return
199214
endif
200215

201216
let buf_name = 'leetcode:///problems/all'
202217
if buflisted(buf_name)
203218
execute bufnr(buf_name) . 'buffer'
204-
return
219+
if a:refresh
220+
setlocal modifiable
221+
silent! normal! ggdG
222+
else
223+
return
224+
endif
225+
else
226+
execute 'rightbelow new ' . buf_name
227+
call s:SetupProblemListBuffer()
228+
let b:leetcode_buffer_type = 'all'
205229
endif
206230

207231
let expr = printf('leetcode.get_problems(["all"])')
@@ -211,11 +235,6 @@ function! leetcode#ListProblems() abort
211235
let topics = topics_and_companies['topics']
212236
let companies = topics_and_companies['companies']
213237

214-
execute 'rightbelow new ' . buf_name
215-
call s:SetupProblemListBuffer()
216-
217-
set modifiable
218-
219238
" concatenate the topics into a string
220239
let topic_slugs = map(topics, 'v:val["topic_slug"]')
221240
let topic_lines = s:FormatIntoColumns(topic_slugs)
@@ -251,7 +270,7 @@ function! s:HandleProblemListCR() abort
251270
\ line_nr < b:leetcode_topic_end_line
252271
let topic_slug = expand('<cWORD>')
253272
if topic_slug != ''
254-
call s:ListProblemsOfTopic(topic_slug)
273+
call s:ListProblemsOfTopic(topic_slug, 0)
255274
endif
256275
return
257276
endif
@@ -260,7 +279,7 @@ function! s:HandleProblemListCR() abort
260279
\ line_nr < b:leetcode_company_end_line
261280
let company_slug = expand('<cWORD>')
262281
if company_slug != ''
263-
call s:ListProblemsOfCompany(company_slug)
282+
call s:ListProblemsOfCompany(company_slug, 0)
264283
endif
265284
return
266285
endif
@@ -282,6 +301,16 @@ function! s:HandleProblemListCR() abort
282301
endif
283302
endfunction
284303

304+
function! s:HandleProblemListR() abort
305+
if b:leetcode_buffer_type ==# 'all'
306+
call leetcode#ListProblems(1)
307+
elseif b:leetcode_buffer_type ==# 'topic'
308+
call s:ListProblemsOfTopic(b:leetcode_buffer_topic, 1)
309+
elseif b:leetcode_buffer_type ==# 'company'
310+
call s:ListProblemsOfCompany(b:leetcode_buffer_company, 1)
311+
endif
312+
endfunction
313+
285314
let s:file_type_to_ext = {
286315
\ 'cpp': 'cpp',
287316
\ 'java': 'java',

plugin/leetcode.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ if !exists('g:leetcode_debug')
2424
let g:leetcode_debug = 0
2525
endif
2626

27-
command! -nargs=0 LeetCodeList call leetcode#ListProblems()
27+
command! -nargs=0 LeetCodeList call leetcode#ListProblems(0)
2828
command! -nargs=0 LeetCodeReset call leetcode#ResetSolution(0)
2929
command! -nargs=0 LeetCodeTest call leetcode#TestSolution()
3030
command! -nargs=0 LeetCodeSubmit call leetcode#SubmitSolution()

0 commit comments

Comments
 (0)