vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

javaformat.vim (2309B)


      1 " 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".
      8 if &cp || exists("g:loaded_javaformat") || exists("g:java_ignore_javadoc") || exists("g:java_ignore_markdown")
      9     finish
     10 endif
     11 
     12 let g:loaded_javaformat = 1
     13 
     14 """" STRIVE TO REMAIN COMPATIBLE FOR AT LEAST VIM 7.0.
     15 
     16 function! 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
     51 endfunction
     52 
     53 " See ":help vim9-mix".
     54 if !has("vim9script")
     55     finish
     56 endif
     57 
     58 def! 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
     90 enddef
     91 
     92 " vim: fdm=syntax sw=4 ts=8 noet sta