lint.vim 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. " Per-file lint entrypoint used by <leader>lf / :LintFile
  2. function! s:lint_file() abort
  3. let l:ft = &filetype
  4. let l:filepath = expand('%:p')
  5. if empty(l:filepath)
  6. echohl WarningMsg
  7. echom 'LintFile: no file to lint'
  8. echohl None
  9. return
  10. endif
  11. if index(['javascript', 'javascriptreact', 'typescript', 'typescriptreact', 'vue', 'svelte', 'astro'], l:ft) >= 0
  12. if exists('*CocAction')
  13. try
  14. call CocAction('runCommand', 'eslint.executeAutofix')
  15. echom 'LintFile: eslint autofix via CoC'
  16. return
  17. catch /.*/
  18. endtry
  19. endif
  20. let l:eslint = findfile('node_modules/.bin/eslint', '.;')
  21. if !empty(l:eslint)
  22. execute 'silent !' . fnameescape(fnamemodify(l:eslint, ':p')) . ' --fix ' . shellescape(l:filepath)
  23. checktime
  24. edit
  25. echom 'LintFile: eslint --fix (local)'
  26. return
  27. endif
  28. echohl WarningMsg
  29. echom 'LintFile: eslint not found (CoC command failed and no local node_modules/.bin/eslint)'
  30. echohl None
  31. return
  32. endif
  33. if index(['terraform', 'tf', 'hcl'], l:ft) >= 0
  34. if executable('tflint')
  35. execute 'silent !tflint --chdir ' . shellescape(expand('%:p:h'))
  36. echom 'LintFile: tflint'
  37. else
  38. echohl WarningMsg
  39. echom 'LintFile: tflint not installed'
  40. echohl None
  41. endif
  42. return
  43. endif
  44. if l:ft ==# 'java'
  45. echom 'LintFile: Java diagnostics come from coc-java (JDTLS)'
  46. return
  47. endif
  48. echom 'LintFile: no linter configured for filetype "' . l:ft . '"'
  49. endfunction
  50. command! LintFile call <SID>lint_file()