Skip to content

Commit be14b26

Browse files
author
xdcn4066
committed
添加Problem命令,用于获取当前文件对应的Problem问题描述,方便查看
1 parent 4246351 commit be14b26

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

autoload/leetcode.vim

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function! leetcode#Submission() abort
6868
endif
6969
try
7070
let problem_slug = s:ProblemSlugFromFileName()
71-
call s:ListSubmissions(problem_slug, 1)
71+
call s:ListSubmissions(problem_slug, 0)
7272
catch
7373
echo "invalid filename, not found submission"
7474
endtry
@@ -728,6 +728,58 @@ function! s:SolutionFileExt(filetype) abort
728728
return s:file_type_to_ext[a:filetype]
729729
endfunction
730730

731+
function! leetcode#Problem() abort
732+
if s:CheckSignIn() == v:false
733+
return
734+
endif
735+
736+
let problem_slug = s:ProblemSlugFromFileName()
737+
let expr = printf('leetcode.get_problem("%s")', problem_slug)
738+
let problem = py3eval(expr)
739+
if type(problem) != v:t_dict
740+
return
741+
endif
742+
743+
let filetype = s:GuessFileType()
744+
if !has_key(problem['templates'], filetype)
745+
echo 'the file type is not supported: ' . filetype
746+
return
747+
endif
748+
749+
let problem_desc_file_name = printf('[DESCRIPTION] %s.%s.%s', problem['fid'], problem_slug, filetype)
750+
if buflisted(problem_desc_file_name)
751+
execute bufnr(problem_desc_file_name) . 'buffer'
752+
return
753+
endif
754+
755+
let output = []
756+
call add(output, s:CommentStart(filetype, problem['title']))
757+
let meta_format = '[%s] [AC:%s %s of %s] [filetype:%s]'
758+
let meta = printf(meta_format, problem['level'], problem['ac_rate'],
759+
\ problem['total_accepted'], problem['total_submission'],
760+
\ filetype)
761+
call add(output, s:CommentLine(filetype, ''))
762+
call add(output, s:CommentLine(filetype, meta))
763+
call add(output, s:CommentLine(filetype, ''))
764+
for line in problem['desc']
765+
call add(output, s:CommentLine(filetype, line))
766+
endfor
767+
call add(output, s:CommentEnd(filetype))
768+
769+
execute 'vertical '. 'new ' . problem_desc_file_name
770+
"execute 'leftabove '. len(output). 'new ' . problem_desc_file_name
771+
call append('$', output)
772+
silent! normal! ggdd
773+
silent! normal! gggqG
774+
775+
setlocal nomodifiable
776+
setlocal buftype=nofile
777+
setlocal nospell
778+
setlocal bufhidden=delete
779+
780+
execute 'wincmd j'
781+
endfunction
782+
731783
function! leetcode#ResetSolution(with_latest_submission, select_filetype) abort
732784
if s:CheckSignIn() == v:false
733785
return
@@ -1212,6 +1264,7 @@ function! s:ListSubmissions(slug, refresh) abort
12121264
hi! lcNA ctermfg=gray guifg=gray
12131265
endif
12141266

1267+
12151268
let b:leetcode_problem_slug = a:slug
12161269

12171270
let expr = printf('leetcode.get_submissions("%s")', a:slug)

plugin/leetcode.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ command! -nargs=0 LeetCodeTest call leetcode#TestSolution()
4242
command! -nargs=0 LeetCodeSubmit call leetcode#SubmitSolution()
4343
command! -nargs=0 LeetCodeSignIn call leetcode#SignIn(1)
4444
command! -nargs=0 LeetCodeSubmission call leetcode#Submission()
45+
command! -nargs=0 LeetCodeProblem call leetcode#Problem()
4546

0 commit comments

Comments
 (0)