javaformat.vim 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. " Documented in ":help ft-java-plugin".
  7. if &cp || exists("g:loaded_javaformat") || exists("g:java_ignore_javadoc") || exists("g:java_ignore_markdown")
  8. finish
  9. endif
  10. let g:loaded_javaformat = 1
  11. """" STRIVE TO REMAIN COMPATIBLE FOR AT LEAST VIM 7.0.
  12. function! javaformat#RemoveCommonMarkdownWhitespace() abort
  13. if mode() != 'n'
  14. return 0
  15. endif
  16. let pattern = '\(^\s*///\)\(\s*\)\(.*\)'
  17. " E121 for v:numbermax before v8.2.2388.
  18. " E15 for expr-<< before v8.2.5003.
  19. let common = 0x7fffffff
  20. let comments = []
  21. for n in range(v:lnum, (v:lnum + v:count - 1))
  22. let parts = matchlist(getline(n), pattern)
  23. let whitespace = get(parts, 2, '')
  24. let nonwhitespace = get(parts, 3, '')
  25. if !empty(whitespace)
  26. let common = min([common, strlen(whitespace)])
  27. elseif !empty(nonwhitespace) || empty(parts)
  28. " No whitespace prefix or not a Markdown comment.
  29. return 0
  30. endif
  31. call add(comments, [whitespace, parts[1], nonwhitespace])
  32. endfor
  33. let cursor = v:lnum
  34. for line in comments
  35. call setline(cursor, join(line[1 :], strpart(line[0], common)))
  36. let cursor += 1
  37. endfor
  38. return 0
  39. endfunction
  40. " See ":help vim9-mix".
  41. if !has("vim9script")
  42. finish
  43. endif
  44. def! g:javaformat#RemoveCommonMarkdownWhitespace(): number
  45. if mode() != 'n'
  46. return 0
  47. endif
  48. const pattern: string = '\(^\s*///\)\(\s*\)\(.*\)'
  49. var common: number = v:numbermax
  50. var comments: list<list<string>> = []
  51. for n in range(v:lnum, (v:lnum + v:count - 1))
  52. const parts: list<string> = matchlist(getline(n), pattern)
  53. const whitespace: string = get(parts, 2, '')
  54. const nonwhitespace: string = get(parts, 3, '')
  55. if !empty(whitespace)
  56. common = min([common, strlen(whitespace)])
  57. elseif !empty(nonwhitespace) || empty(parts)
  58. # No whitespace prefix or not a Markdown comment.
  59. return 0
  60. endif
  61. add(comments, [whitespace, parts[1], nonwhitespace])
  62. endfor
  63. var cursor: number = v:lnum
  64. for line in comments
  65. setline(cursor, join(line[1 :], strpart(line[0], common)))
  66. cursor += 1
  67. endfor
  68. return 0
  69. enddef
  70. " vim: fdm=syntax sw=4 ts=8 noet sta