astro.vim 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. " Vim indent file (experimental).
  2. " Language: Astro
  3. " Author: Wuelner Martínez <wuelner.martinez@outlook.com>
  4. " Maintainer: Wuelner Martínez <wuelner.martinez@outlook.com>
  5. " URL: https://github.com/wuelnerdotexe/vim-astro
  6. " Last Change: 2022 Aug 07
  7. " Based On: Evan Lecklider's vim-svelte
  8. " Changes: See https://github.com/evanleck/vim-svelte
  9. " Credits: See vim-svelte on github
  10. " Only load this indent file when no other was loaded yet.
  11. if exists('b:did_indent')
  12. finish
  13. endif
  14. let b:html_indent_script1 = 'inc'
  15. let b:html_indent_style1 = 'inc'
  16. " Embedded HTML indent.
  17. runtime! indent/html.vim
  18. let s:html_indent = &l:indentexpr
  19. unlet b:did_indent
  20. let b:did_indent = 1
  21. setlocal indentexpr=GetAstroIndent()
  22. setlocal indentkeys=<>>,/,0{,{,},0},0),0],0\,<<>,,!^F,*<Return>,o,O,e,;
  23. let b:undo_indent = 'setl inde< indk<'
  24. " Only define the function once.
  25. if exists('*GetAstroIndent')
  26. finish
  27. endif
  28. let s:cpoptions_save = &cpoptions
  29. setlocal cpoptions&vim
  30. function! GetAstroIndent()
  31. let l:current_line_number = v:lnum
  32. if l:current_line_number == 0
  33. return 0
  34. endif
  35. let l:current_line = getline(l:current_line_number)
  36. if l:current_line =~ '^\s*</\?\(script\|style\)'
  37. return 0
  38. endif
  39. let l:previous_line_number = prevnonblank(l:current_line_number - 1)
  40. let l:previous_line = getline(l:previous_line_number)
  41. let l:previous_line_indent = indent(l:previous_line_number)
  42. if l:previous_line =~ '^\s*</\?\(script\|style\)'
  43. return l:previous_line_indent + shiftwidth()
  44. endif
  45. execute 'let l:indent = ' . s:html_indent
  46. if searchpair('<style>', '', '</style>', 'bW') &&
  47. \ l:previous_line =~ ';$' && l:current_line !~ '}'
  48. return l:previous_line_indent
  49. endif
  50. if synID(l:previous_line_number, match(
  51. \ l:previous_line, '\S'
  52. \ ) + 1, 0) == hlID('htmlTag') && synID(l:current_line_number, match(
  53. \ l:current_line, '\S'
  54. \ ) + 1, 0) != hlID('htmlEndTag')
  55. let l:indents_match = l:indent == l:previous_line_indent
  56. let l:previous_closes = l:previous_line =~ '/>$'
  57. if l:indents_match &&
  58. \ !l:previous_closes && l:previous_line =~ '<\(\u\|\l\+:\l\+\)'
  59. return l:previous_line_indent + shiftwidth()
  60. elseif !l:indents_match && l:previous_closes
  61. return l:previous_line_indent
  62. endif
  63. endif
  64. return l:indent
  65. endfunction
  66. let &cpoptions = s:cpoptions_save
  67. unlet s:cpoptions_save
  68. " vim: ts=8