首页 > 解决方案 > 添加 eslint-ignore 提示的 vim ale 快捷方式

问题描述

我已经使用 vim 多年了,但我刚刚开始集成 eslint(通过ALE)。我发现有时我希望能够快速添加一个/* eslint-ignore-next-line */. 例如:

 ...
❌    if (m = /^-l(\d+)$/.exec(args[i])) t.len = m[1];
 ...
~/some/dir/file.js [+]          
cond-assign: Expected a conditional expression and instead saw an assignment.

ALE 在窗口底部为您提供代码真的很方便,但是因为懒惰,我想自动添加注释/提示:

/* eslint-ignore-next-line cond-assign */

有没有办法在 vim 脚本/函数中访问屏幕底部的信息?

标签: vimeslintvim-ale

解决方案


虽然没有记录,但 ALE 将 lint 信息存储在b:ale_highlight_items变量中。

David784 的解决方案对我不起作用,因为我使用 ALE 的g:ale_echo_msg_format配置选项自定义了位置列表文本。所以我修改它直接从位置列表中获取信息,b:ale_highlight_items而不是从位置列表中解析出来。

这里是:

command! ALEIgnoreEslint call AleIgnoreEslint()
function! AleIgnoreEslint()
  " https://stackoverflow.com/questions/54961318/vim-ale-shortcut-to-add-eslint-ignore-hint
  let l:codes = []
  if (!exists('b:ale_highlight_items'))
    echo 'cannot ignore eslint rule without b:ale_highlight_items'
    return
  endif
  for l:item in b:ale_highlight_items
    if (l:item['lnum']==line('.') && l:item['linter_name']=='eslint')
      let l:code = l:item['code']
      call add(l:codes, l:code)
    endif
  endfor
  if len(l:codes)
    exec 'normal O/* eslint-disable-next-line ' . join(l:codes, ', ') . ' */'
  endif
endfunction


推荐阅读