Skip to content

Commit 281a04b

Browse files
committed
add support to java
- support, e.g. auto add lib import statements, auto fold the statements, auto comment the statements before submission, etc.
1 parent 9141df0 commit 281a04b

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

autoload/leetcode/lang/java.vim

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,91 @@
11
let s:lang = 'java'
22
let s:ext = 'java'
3+
let s:dependencies = [
4+
\'/*** [' .g:leetcode_name .'] For Local Syntax Checking ***/',
5+
\'import java.util.*;',
6+
\'import java.util.stream.*;',
7+
\'import javafx.util.*;',
8+
\'import java.util.Map.Entry;', '']
9+
let s:depend_location = '/\mclass\s\+solution\c\s\+{/-'
10+
let s:code_begin_location = '?\m)\s*{?+'
11+
12+
"" TODO: refactor to reuse the duplicated logic
313

414
fu! leetcode#lang#java#locateLeetcodeCliComment(topmost_line)
15+
exe 'keepj norm! ' .(a:topmost_line - 1) .'G'
16+
let comment_first_line = search('\/\*')
17+
let comment_last_line = searchpair('\/\*', '', '\*\/', 'W')
18+
return [comment_first_line, comment_last_line]
519
endfu
620

721
fu! leetcode#lang#java#getCustomDependencies()
22+
keepj norm! gg
23+
let lc_code_start_line = search('@\s*lc\s*code\s*=\s*start')
24+
let [comment_first_line, comment_last_line] = leetcode#lang#java#locateLeetcodeCliComment(lc_code_start_line + 1)
25+
exe 'keepj norm! ' .comment_first_line .'G'
26+
let definition_comment_line = search('\cdefinition for')
27+
if definition_comment_line == 0
28+
retu []
29+
endif
30+
let custom_depend = []
31+
let custom_depend_line = definition_comment_line + 1
32+
while custom_depend_line < comment_last_line
33+
exe 'keepj norm! ' .custom_depend_line .'G'
34+
cal add(custom_depend, getline(custom_depend_line))
35+
let custom_depend_line += 1
36+
endwhile
37+
cal map(custom_depend, {key, val -> matchstr(val, '\s*\*\+\s*\zs.*')})
38+
retu custom_depend
839
endfu
940

1041
fu! leetcode#lang#java#addDependencies()
42+
keepj norm! gg
43+
try | exe 'keepp sil ' .s:depend_location
44+
cat /E486/
45+
throw 'Error in locating the position to add dependencies'
46+
endt
47+
"" Add dependencies and make it non-undoable
48+
let old_ul = &ul
49+
setl ul=-1
50+
keepj cal append(line('.'), s:dependencies)
51+
let custom_depend = leetcode#lang#java#getCustomDependencies()
52+
let custom_depend_begin_line = search(s:dependencies[len(s:dependencies) - 3])
53+
keepj cal append(custom_depend_begin_line, custom_depend)
54+
exe 'setlocal ul='.old_ul
55+
retu 1
1156
endfu
1257

1358
fu! leetcode#lang#java#foldDependencies()
59+
setl foldmethod=manual
60+
try
61+
keepj norm! gg
62+
let first_fold_line = search(escape(s:dependencies[0], '/*[]'))
63+
if foldclosed(first_fold_line) == -1
64+
exe 'keepj keepp sil /\m\C\s*\/\{-,2}\s*' .escape(s:dependencies[1], '.*') .'\s*$'
65+
let last_fold_line = search(escape(s:dependencies[-2], '.*'))
66+
exe 'keepj norm! ' .first_fold_line .'G' .(last_fold_line - first_fold_line + 1) .'zF'
67+
en
68+
retu 1
69+
cat /E486/
70+
throw 'Error in locating the dependencies to fold'
71+
endt
1472
endfu
1573

1674
fu! leetcode#lang#java#goToWhereCodeBegins()
75+
keepj keepp sil /\m\%$/
76+
try
77+
exe 'sil ' .s:code_begin_location
78+
cat /E486/
79+
throw 'Error in locating where to start code writing.'
80+
endt
1781
endfu
1882

1983
fu! leetcode#lang#java#commentDependencies()
84+
exe 'keepj keepp sil %sm@\C^\(.*' .s:dependencies[1] .'.*\)$@//\1@'
2085
endfu
2186

2287
fu! leetcode#lang#java#uncommentDependencies()
88+
exe 'keepj keepp sil %sm@\C\s*\zs/*\ze.*' .s:dependencies[1] .'.*$@@'
2389
endfu
2490

2591
fu! leetcode#lang#java#getExt()

plugin/leetcode.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"" Version: 0.5.0
1+
"" Version: 0.6.0
22
"" Description:
33
"" This is a leetcode question and code file manager. It allows a quick
44
"" download, load, test and submission of leetcode questions. The file

0 commit comments

Comments
 (0)