Fortunately, ALE uses the built-in location-list
to store its lint messages, and this is accessible via getloclist({nr})
, where {nr}
is the register. Current register is always 0
.
So here's a method of getting all the lint messages for the current line, and adding them all to an eslint hint comment:
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
This version will only add the eslint-disable-next-line
hint immediately before the current line. It would also be pretty easy to expand this out to add a global eslint-disable
hint at the top of the file...the hard part was figuring out about getloclist()
.
*EDIT: I'm adding an updated version that accepts a new-line
parameter. If this is 0
, it will add a global hint at the top of the file, and if it's 1
it will add the -next-line
hint above the current line. But I'm keeping the previous version as well since it's a simpler example, without all the ternaries and stuff.
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