vim 添加 eslint-ignore 提示的 ale 快捷方式
vim ale shortcut to add eslint-ignore hint
我已经使用 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 为您提供 window 底部的代码真的很方便,但由于懒惰,我想自动添加 comment/hint:
/* eslint-ignore-next-line cond-assign */
有没有办法在 vim script/function 中访问屏幕底部的信息?
幸运的是,ALE 使用内置 location-list
来存储其 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()
.
*编辑:我正在添加接受 new-line
参数的更新版本。如果是 0
,它将在文件顶部添加一个全局提示,如果是 1
,它将在当前行上方添加 -next-line
提示。但我也保留了以前的版本,因为它是一个更简单的示例,没有所有的三元组和东西。
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_items
获取信息,而不是从位置列表中解析它。
这里是:
command! ALEIgnoreEslint call AleIgnoreEslint()
function! AleIgnoreEslint()
"
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
我已经使用 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 为您提供 window 底部的代码真的很方便,但由于懒惰,我想自动添加 comment/hint:
/* eslint-ignore-next-line cond-assign */
有没有办法在 vim script/function 中访问屏幕底部的信息?
幸运的是,ALE 使用内置 location-list
来存储其 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()
.
*编辑:我正在添加接受 new-line
参数的更新版本。如果是 0
,它将在文件顶部添加一个全局提示,如果是 1
,它将在当前行上方添加 -next-line
提示。但我也保留了以前的版本,因为它是一个更简单的示例,没有所有的三元组和东西。
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_items
获取信息,而不是从位置列表中解析它。
这里是:
command! ALEIgnoreEslint call AleIgnoreEslint()
function! AleIgnoreEslint()
"
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