From a76a9754e1a3c97ecdfccf8d7eaa0a7dd32dfc0e Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Tue, 18 Aug 2020 16:57:06 +0200 Subject: [PATCH] Add asynchronous linting plugin for vim --- vim/plugin/async_lint.vim | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 vim/plugin/async_lint.vim diff --git a/vim/plugin/async_lint.vim b/vim/plugin/async_lint.vim new file mode 100644 index 0000000..b5e22d6 --- /dev/null +++ b/vim/plugin/async_lint.vim @@ -0,0 +1,41 @@ +function s:async_lint_done(bufnr, buffer) + if getbufvar(a:bufnr, "lint_job")!="" + call getbufvar(a:bufnr, "") + let l:pos = getcurpos() + call deletebufline(bufname(a:bufnr), 1, "$") + for line in a:buffer + call appendbufline(bufname(a:bufnr), "$", line) + endfor + call deletebufline(bufname(a:bufnr), 1) + call setpos(".", l:pos) + call setbufvar(a:bufnr, "lint_job", "") + end +endfun + +function s:async_lint_abort(bufnr) + let l:job = getbufvar(a:bufnr, "lint_job") + if l:job!="" + call job_stop(l:job) + end + call setbufvar(a:bufnr, "lint_job", "") +endfun + +function s:async_lint(bufnr, command) + if getbufvar(a:bufnr, "lint_job")=="" + let l:buffer = [] + let l:job = job_start(a:command, { + \ "in_io": "buffer", "in_name": bufname(a:bufnr), + \ "out_io": "pipe", + \ "out_cb": { pipe, text -> add(l:buffer, text) }, + \ "close_cb": { pipe -> s:async_lint_done(a:bufnr, l:buffer) } + \ }) + call setbufvar(a:bufnr, "lint_job", l:job) + end +endfun + +augroup ASYNC_LINT + au TextChanged * call s:async_lint_abort(bufnr("%")) + au TextChangedI * call s:async_lint_abort(bufnr("%")) + au TextChangedP * call s:async_lint_abort(bufnr("%")) +augroup END +