blob: 4d7d32cf193e7a1a5dc09992d73c9b018cf9222d [file] [log] [blame]
Aliaksei Budavei85f054a2024-09-30 19:40:04 +02001" Vim formatting plugin file
2" Language: Java
3" Maintainer: Aliaksei Budavei <0x000c70 AT gmail DOT com>
4" Repository: https://github.com/zzzyxwvut/java-vim.git
5" Last Change: 2024 Sep 26
6
7" Documented in ":help ft-java-plugin".
8if &cp || exists("g:loaded_javaformat") || exists("g:java_ignore_javadoc") || exists("g:java_ignore_markdown")
9 finish
10endif
11
12let g:loaded_javaformat = 1
13
14"""" STRIVE TO REMAIN COMPATIBLE FOR AT LEAST VIM 7.0.
15
16function! javaformat#RemoveCommonMarkdownWhitespace() abort
17 if mode() != 'n'
18 return 0
19 endif
20
21 let pattern = '\(^\s*///\)\(\s*\)\(.*\)'
22
23 " E121 for v:numbermax before v8.2.2388.
24 " E15 for expr-<< before v8.2.5003.
25 let common = 0x7fffffff
26 let comments = []
27
28 for n in range(v:lnum, (v:lnum + v:count - 1))
29 let parts = matchlist(getline(n), pattern)
30 let whitespace = get(parts, 2, '')
31 let nonwhitespace = get(parts, 3, '')
32
33 if !empty(whitespace)
34 let common = min([common, strlen(whitespace)])
35 elseif !empty(nonwhitespace) || empty(parts)
36 " No whitespace prefix or not a Markdown comment.
37 return 0
38 endif
39
40 call add(comments, [whitespace, parts[1], nonwhitespace])
41 endfor
42
43 let cursor = v:lnum
44
45 for line in comments
46 call setline(cursor, join(line[1 :], strpart(line[0], common)))
47 let cursor += 1
48 endfor
49
50 return 0
51endfunction
52
53" See ":help vim9-mix".
54if !has("vim9script")
55 finish
56endif
57
58def! g:javaformat#RemoveCommonMarkdownWhitespace(): number
59 if mode() != 'n'
60 return 0
61 endif
62
63 const pattern: string = '\(^\s*///\)\(\s*\)\(.*\)'
64 var common: number = v:numbermax
65 var comments: list<list<string>> = []
66
67 for n in range(v:lnum, (v:lnum + v:count - 1))
68 const parts: list<string> = matchlist(getline(n), pattern)
69 const whitespace: string = get(parts, 2, '')
70 const nonwhitespace: string = get(parts, 3, '')
71
72 if !empty(whitespace)
73 common = min([common, strlen(whitespace)])
74 elseif !empty(nonwhitespace) || empty(parts)
75 # No whitespace prefix or not a Markdown comment.
76 return 0
77 endif
78
79 add(comments, [whitespace, parts[1], nonwhitespace])
80 endfor
81
82 var cursor: number = v:lnum
83
84 for line in comments
85 setline(cursor, join(line[1 :], strpart(line[0], common)))
86 cursor += 1
87 endfor
88
89 return 0
90enddef
91
92" vim: fdm=syntax sw=4 ts=8 noet sta