aboutsummaryrefslogtreecommitdiff
path: root/runtime/autoload/health.vim
blob: d2f36b2f65a224b9b87a5ad66eed7f95f59b9ad9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
" Dictionary of all health check functions we have found.
" They will only be run if the value is true
let g:health_checkers = get(g:, 'health_checkers', {})
let s:current_checker = get(s:, 'current_checker', '')

function! s:enhance_syntax() abort
  syntax keyword healthError ERROR
  highlight link healthError Error

  syntax keyword healthWarning WARNING
  highlight link healthWarning WarningMsg

  syntax keyword healthInfo INFO
  highlight link healthInfo ModeMsg

  syntax keyword healthSuccess SUCCESS
  highlight link healthSuccess Function

  syntax keyword healthSuggestion SUGGESTION
  highlight link healthSuggestion String
endfunction

" Runs the specified healthchecks.
" Runs all discovered healthchecks if a:plugin_names is empty.
function! health#check(plugin_names) abort
  let report = ''

  let healthchecks = empty(a:plugin_names)
        \ ? s:discover_health_checks()
        \ : s:to_fn_names(a:plugin_names)

  if empty(healthchecks)
    let report = "ERROR: No healthchecks found."
  else
    for c in healthchecks
      let report .= printf("\n%s\n%s", c, repeat('=',80))
      try
        let report .= execute('call '.c.'()')
      catch /^Vim\%((\a\+)\)\=:E117/
        let report .= execute(
              \ 'call health#report_error(''No healthcheck found for "'
              \ .s:to_plugin_name(c)
              \ .'" plugin.'')')
      catch
        let report .= execute(
              \ 'call health#report_error(''Failed to run healthcheck for "'
              \ .s:to_plugin_name(c)
              \ .'" plugin. Exception:''."\n".v:exception)')
      endtry
      let report .= "\n"
    endfor
  endif

  new
  setlocal bufhidden=wipe
  set filetype=markdown
  call s:enhance_syntax()
  call setline(1, split(report, "\n"))
  setlocal nomodified
endfunction

" Starts a new report.
function! health#report_start(name) abort
  echo "\n## " . a:name
endfunction

" Indents lines *except* line 1 of a string if it contains newlines.
function! s:indent_after_line1(s, columns) abort
  let lines = split(a:s, "\n", 0)
  if len(lines) < 2  " We do not indent line 1, so nothing to do.
    return a:s
  endif
  for i in range(1, len(lines)-1)  " Indent lines after the first.
    let lines[i] = substitute(lines[i], '^\s*', repeat(' ', a:columns), 'g')
  endfor
  return join(lines, "\n")
endfunction

" Format a message for a specific report item
function! s:format_report_message(status, msg, ...) abort " {{{
  let output = '  - ' . a:status . ': ' . s:indent_after_line1(a:msg, 4)
  let suggestions = []

  " Optional parameters
  if a:0 > 0
    let suggestions = type(a:1) == type("") ? [a:1] : a:1
    if type(suggestions) != type([])
      echoerr "Expected String or List"
    endif
  endif

  " Report each suggestion
  if len(suggestions) > 0
    let output .= "\n      - SUGGESTIONS:"
  endif
  for suggestion in suggestions
    let output .= "\n        - " . s:indent_after_line1(suggestion, 10)
  endfor

  return output
endfunction " }}}

" Use {msg} to report information in the current section
function! health#report_info(msg) abort " {{{
  echo s:format_report_message('INFO', a:msg)
endfunction " }}}

" Reports a successful healthcheck.
function! health#report_ok(msg) abort " {{{
  echo s:format_report_message('SUCCESS', a:msg)
endfunction " }}}

" Reports a health warning.
function! health#report_warn(msg, ...) abort " {{{
  if a:0 > 0
    echo s:format_report_message('WARNING', a:msg, a:1)
  else
    echo s:format_report_message('WARNING', a:msg)
  endif
endfunction " }}}

" Reports a failed healthcheck.
function! health#report_error(msg, ...) abort " {{{
  if a:0 > 0
    echo s:format_report_message('ERROR', a:msg, a:1)
  else
    echo s:format_report_message('ERROR', a:msg)
  endif
endfunction " }}}

" Adds a health checker. Does nothing if the checker already exists.
function! s:add_single_checker(checker_name) abort " {{{
  if has_key(g:health_checkers, a:checker_name)
    return
  else
      let g:health_checkers[a:checker_name] = v:true
  endif
endfunction " }}}

" Enables a health checker.
function! s:enable_single_checker(checker_name) abort " {{{
  let g:health_checkers[a:checker_name] = v:true
endfunction " }}}

" Disables a health checker.
function! s:disable_single_checker(checker_name) abort " {{{
  let g:health_checkers[a:checker_name] = v:false
endfunction " }}}


" Adds a health checker. `checker_name` can be a list of strings or
" a single string. Does nothing if the checker already exists.
function! health#add_checker(checker_name) abort " {{{
  if type(a:checker_name) == type('')
    call s:add_single_checker(a:checker_name)
  elseif type(a:checker_name) == type([])
    for checker in a:checker_name
      call s:add_single_checker(checker)
    endfor
  endif
endfunction " }}}

" Enables a health checker. `checker_name` can be a list of strings or
" a single string.
function! health#enable_checker(checker_name) abort " {{{
  if type(a:checker_name) == type('')
    call s:enable_single_checker(a:checker_name)
  elseif type(a:checker_name) == type([])
    for checker in a:checker_name
      call s:enable_single_checker(checker)
    endfor
  endif
endfunction " }}}

" Disables a health checker. `checker_name` can be a list of strings or
" a single string.
function! health#disable_checker(checker_name) abort " {{{
  if type(a:checker_name) == type('')
    call s:disable_single_checker(a:checker_name)
  elseif type(a:checker_name) == type([])
    for checker in a:checker_name
      call s:disable_single_checker(checker)
    endfor
  endif
endfunction " }}}

function! s:filepath_to_function(name) abort
  return substitute(substitute(substitute(a:name, ".*autoload/", "", ""),
        \ "\\.vim", "#check", ""), "/", "#", "g")
endfunction

function! s:discover_health_checks() abort
  let healthchecks = globpath(&runtimepath, 'autoload/health/*.vim', 1, 1)
  let healthchecks = map(healthchecks, '<SID>filepath_to_function(v:val)')
  return healthchecks
endfunction

" Translates a list of plugin names to healthcheck function names.
function! s:to_fn_names(plugin_names) abort
  let healthchecks = []
  for p in a:plugin_names
    call add(healthchecks, 'health#'.p.'#check')
  endfor
  return healthchecks
endfunction

" Extracts 'foo' from 'health#foo#check'.
function! s:to_plugin_name(fn_name) abort
  return substitute(a:fn_name,
        \ '\v.*health\#(.+)\#check.*', '\1', '')
endfunction