添加eslint忽略提示的vim ale快捷方式

添加eslint忽略提示的vim ale快捷方式,vim,eslint,vim-ale,Vim,Eslint,Vim Ale,我已经使用vim很多年了,但我刚刚开始通过集成eslint。我发现有时我希望能够快速添加/*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.

我已经使用vim很多年了,但我刚刚开始通过集成eslint。我发现有时我希望能够快速添加/*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脚本/函数中,有没有办法访问屏幕底部的信息?

幸运的是,ALE使用内置的位置列表来存储其lint消息,这可以通过getloclist{nr}访问,其中{nr}是寄存器。当前寄存器始终为0

下面是获取当前行的所有lint消息并将它们全部添加到eslint提示注释的方法:

function AleIgnore()
  let codes = []
  for d in getloclist(0)
    if (d.lnum==line('.'))
      let code = split(d.text,':')[0]
      call add(codes, code)
    endif
  endfor
  if len(codes)
    exe 'normal O/* eslint-disable-next-line ' . join(codes, ', ') . ' */'
  endif
endfunction
此版本仅在当前行之前添加eslint disable next line提示。扩展它,在文件顶部添加一个全局eslint disable提示也是非常容易的…最难的部分是了解getloclist

*编辑:我正在添加一个接受新行参数的更新版本。如果为0,它将在文件顶部添加一个全局提示,如果为1,它将在当前行的上方添加-next行提示。但我也保留了以前的版本,因为它是一个更简单的示例,没有所有的ternaries和东西

function AleIgnore(nl)
  let codes = []
  for d in getloclist(0)
    if (d.lnum==line('.'))
      let code = split(d.text,':')[0]
      call add(codes, code)
    endif
  endfor
  if len(codes)
    exe 'normal mq' . (a:nl?'':'1G') . 'O'
          \ . '/* eslint-disable' . (a:nl?'-next-line ':' ')
          \ . join(codes, ', ') . ' */' . "\<esc>`q"
  endif
endfunction

幸运的是,ALE使用内置的位置列表来存储其lint消息,这可以通过getloclist{nr}访问,其中{nr}是寄存器。当前寄存器始终为0

下面是获取当前行的所有lint消息并将它们全部添加到eslint提示注释的方法:

function AleIgnore()
  let codes = []
  for d in getloclist(0)
    if (d.lnum==line('.'))
      let code = split(d.text,':')[0]
      call add(codes, code)
    endif
  endfor
  if len(codes)
    exe 'normal O/* eslint-disable-next-line ' . join(codes, ', ') . ' */'
  endif
endfunction
此版本仅在当前行之前添加eslint disable next line提示。扩展它,在文件顶部添加一个全局eslint disable提示也是非常容易的…最难的部分是了解getloclist

*编辑:我正在添加一个接受新行参数的更新版本。如果为0,它将在文件顶部添加一个全局提示,如果为1,它将在当前行的上方添加-next行提示。但我也保留了以前的版本,因为它是一个更简单的示例,没有所有的ternaries和东西

function AleIgnore(nl)
  let codes = []
  for d in getloclist(0)
    if (d.lnum==line('.'))
      let code = split(d.text,':')[0]
      call add(codes, code)
    endif
  endfor
  if len(codes)
    exe 'normal mq' . (a:nl?'':'1G') . 'O'
          \ . '/* eslint-disable' . (a:nl?'-next-line ':' ')
          \ . join(codes, ', ') . ' */' . "\<esc>`q"
  endif
endfunction

尽管没有文档记录,ALE将lint信息存储在b:ALE_highlight_items变量中

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

这是:

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


尽管没有文档记录,ALE将lint信息存储在b:ALE_highlight_items变量中

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

这是:

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