qml.vim 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. " Vim indent file
  2. " Language: QML
  3. " Maintainer: Chase Knowlden <haroldknowlden@gmail.com>
  4. " Last Change: 2024 Jan 24 by Vim Project
  5. "
  6. " Improved JavaScript indent script.
  7. " Indent script in place for this already?
  8. if exists("b:did_indent")
  9. finish
  10. endif
  11. let b:did_indent = 1
  12. let b:undo_indent = "setlocal indentexpr< indentkeys<"
  13. setlocal indentexpr=s:GetQmlIndent()
  14. setlocal indentkeys=0{,0},0),0],:,!^F,o,O,e,*<Return>,=*/
  15. " Only define functions once per session
  16. if exists("*s:GetQmlIndent")
  17. finish
  18. endif
  19. " Clean up a line of code by removing trailing '//' and '/* */' comments, and trimming
  20. " whitespace
  21. function! s:Trim(line)
  22. return substitute(substitute(substitute(a:line, '// .*', '', ''), '/\* .* \*/', '', ''), '^\s*\|\s*$', '', 'g')
  23. endfunction
  24. function! s:GetQmlIndent()
  25. let num = v:lnum
  26. let line = s:Trim(getline(num))
  27. let pnum = prevnonblank(num - 1)
  28. if pnum == 0
  29. return 0
  30. endif
  31. let pline = s:Trim(getline(pnum))
  32. let ind = indent(pnum)
  33. " bracket/brace/paren blocks
  34. if pline =~ '[{[(]$'
  35. let ind += shiftwidth()
  36. endif
  37. if line =~ '^[}\])]'
  38. let ind -= shiftwidth()
  39. endif
  40. " '/*' comments
  41. if pline =~ '^/\*.*\*/'
  42. " no indent for single-line form
  43. elseif pline =~ '^/\*'
  44. let ind += 1
  45. elseif pline =~ '^\*/'
  46. let ind -= 1
  47. endif
  48. return ind
  49. endfunction