powershell.vim 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. " Vim compiler file
  2. " Compiler: powershell
  3. " URL: https://github.com/PProvost/vim-ps1
  4. " Contributors: Enno Nagel
  5. " Last Change: 2024 Mar 29
  6. " 2024 Apr 03 by the Vim Project (removed :CompilerSet definition)
  7. " 2024 Apr 05 by the Vim Project (avoid leaving behind g:makeprg)
  8. " 2024 Nov 19 by the Vim Project (properly escape makeprg setting)
  9. " 2025 Mar 11 by the Vim Project (add comment for Dispatch)
  10. if exists("current_compiler")
  11. finish
  12. endif
  13. let current_compiler = "powershell"
  14. let s:cpo_save = &cpo
  15. set cpo-=C
  16. if !exists("g:ps1_makeprg_cmd")
  17. if executable('pwsh')
  18. " pwsh is the future
  19. let g:ps1_makeprg_cmd = 'pwsh'
  20. elseif executable('pwsh.exe')
  21. let g:ps1_makeprg_cmd = 'pwsh.exe'
  22. elseif executable('powershell.exe')
  23. let g:ps1_makeprg_cmd = 'powershell.exe'
  24. else
  25. let g:ps1_makeprg_cmd = ''
  26. endif
  27. endif
  28. if !executable(g:ps1_makeprg_cmd)
  29. echoerr "To use the powershell compiler, please set g:ps1_makeprg_cmd to the powershell executable!"
  30. endif
  31. " Show CategoryInfo, FullyQualifiedErrorId, etc?
  32. let g:ps1_efm_show_error_categories = get(g:, 'ps1_efm_show_error_categories', 0)
  33. " Use absolute path because powershell requires explicit relative paths
  34. " (./file.ps1 is okay, but # expands to file.ps1)
  35. let s:makeprg = g:ps1_makeprg_cmd .. ' %:p:S'
  36. " Parse file, line, char from callstacks:
  37. " Write-Ouput : The term 'Write-Ouput' is not recognized as the name of a
  38. " cmdlet, function, script file, or operable program. Check the spelling
  39. " of the name, or if a path was included, verify that the path is correct
  40. " and try again.
  41. " At C:\script.ps1:11 char:5
  42. " + Write-Ouput $content
  43. " + ~~~~~~~~~~~
  44. " + CategoryInfo : ObjectNotFound: (Write-Ouput:String) [], CommandNotFoundException
  45. " + FullyQualifiedErrorId : CommandNotFoundException
  46. " CompilerSet makeprg=pwsh
  47. " CompilerSet makeprg=powershell
  48. execute 'CompilerSet makeprg=' .. escape(s:makeprg, ' \|"')
  49. " Showing error in context with underlining.
  50. CompilerSet errorformat=%+G+%m
  51. " Error summary.
  52. CompilerSet errorformat+=%E%*\\S\ :\ %m
  53. " Error location.
  54. CompilerSet errorformat+=%CAt\ %f:%l\ char:%c
  55. " Errors that span multiple lines (may be wrapped to width of terminal).
  56. CompilerSet errorformat+=%C%m
  57. " Ignore blank/whitespace-only lines.
  58. CompilerSet errorformat+=%Z\\s%#
  59. if g:ps1_efm_show_error_categories
  60. CompilerSet errorformat^=%+G\ \ \ \ +\ %.%#\\s%#:\ %m
  61. else
  62. CompilerSet errorformat^=%-G\ \ \ \ +\ %.%#\\s%#:\ %m
  63. endif
  64. " Parse file, line, char from of parse errors:
  65. " At C:\script.ps1:22 char:16
  66. " + Stop-Process -Name "invalidprocess
  67. " + ~~~~~~~~~~~~~~~
  68. " The string is missing the terminator: ".
  69. " + CategoryInfo : ParserError: (:) [], ParseException
  70. " + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
  71. CompilerSet errorformat+=At\ %f:%l\ char:%c
  72. let &cpo = s:cpo_save
  73. unlet s:cpo_save
  74. " vim:set sw=2 sts=2: