Skip to content

Commit 4cffb70

Browse files
authored
Merge pull request #2 from clojure-vim/lua
Migrate to lua
2 parents c91fb58 + d69aa1e commit 4cffb70

File tree

3 files changed

+73
-83
lines changed

3 files changed

+73
-83
lines changed

clj/async_clj_highlight.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
(ns-aliases ns)))
6969

7070
(defn var-type [v]
71-
(let [f @v m (meta v)]
71+
(let [_ @v m (meta v)]
7272
(cond (clojure-core? v) (core-symbol->syntax-group (:name m))
7373
(:macro m) "clojureMacro"
7474
(fn-var? v) "clojureFunc"

lua/cljhl/init.lua

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
-- luacheck: globals vim
2+
local acid = require("acid")
3+
local log = require("acid.log").msg
4+
local connections = require("acid.connections")
5+
local commands = require("acid.commands")
6+
local eval = require("acid.ops").eval
7+
8+
local cljhl = {}
9+
10+
local conn_to_key = function(conn)
11+
return tostring(conn[1]) .. ":" .. tostring(conn[2])
12+
end
13+
14+
cljhl.cache = {}
15+
16+
cljhl.apply = function(msg)
17+
if msg.status ~= nil then
18+
return
19+
elseif msg.err ~= nil then
20+
log("Can't apply highlight.", msg.err)
21+
return
22+
end
23+
vim.api.nvim_call_function("AsyncCljHighlightExec", {msg.value})
24+
end
25+
26+
cljhl.highlight = function(ns)
27+
ns = ns or vim.api.nvim_call_function("AcidGetNs", {})
28+
if ns == nil then
29+
return
30+
end
31+
32+
local opts = ""
33+
if vim.api.nvim_get_var("clojure_highlight_local_vars") == 0 then
34+
opts = " :local-vars false"
35+
end
36+
37+
local payload = eval{code = "(ns-syntax-command '" .. ns .. opts ..")", ns = "async-clj-highlight"}
38+
acid.run(payload:with_handler(cljhl.apply))
39+
end
40+
41+
cljhl.preload = function(ns)
42+
ns = ns or vim.api.nvim_call_function("AcidGetNs", {})
43+
44+
if ns == nil then
45+
return
46+
end
47+
48+
local pwd = vim.api.nvim_call_function("getcwd", {})
49+
local conn = connections.attempt_get(pwd)
50+
local key = conn_to_key(conn)
51+
52+
if cljhl.cache[key] ~= nil then
53+
cljhl.highlight(ns)
54+
else
55+
local cmd = commands.preload{files = {"clj/async_clj_highlight.clj"}}[1]
56+
acid.run(cmd:with_handler(function(data)
57+
if data.status then
58+
return
59+
end
60+
cljhl.cache[key] = true
61+
cljhl.highlight(ns)
62+
end, conn))
63+
end
64+
end
65+
66+
return cljhl

plugin/async_clj_highlight.vim

+6-82
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,19 @@
11
" vim-clojure-highlight
22

3-
if !exists('g:clojure_highlight_references')
4-
let g:clojure_highlight_references = 1
5-
endif
6-
73
if !exists('g:clojure_highlight_local_vars')
84
let g:clojure_highlight_local_vars = 1
95
endif
106

11-
function! s:disable_acid_log()
12-
if exists('b:acid_log_messages')
13-
let b:acid_old_log_value = b:acid_log_messages
14-
else
15-
let b:acid_log_messages = 0
16-
endif
17-
endfunction
18-
19-
function! s:restore_acid_log()
20-
if exists('b:acid_old_log_value')
21-
let b:acid_log_messages = b:acid_old_log_value
22-
unlet b:acid_old_log_value
23-
else
24-
unlet b:acid_log_messages
25-
endif
26-
endfunction
27-
28-
function! s:silent_acid_send(data, handler_fn)
29-
call s:disable_acid_log()
30-
echom "Disabled log: ".b:acid_log_messages
31-
call AcidSendNrepl(a:data, 'VimFn', a:handler_fn)
32-
call s:restore_acid_log()
33-
endfunction
34-
35-
function! AsyncCljHighlightExec(msg)
36-
let fst = a:msg[0]
37-
if get(fst, 'value', '') !=# ''
38-
exec eval(fst.value)
39-
let &syntax = &syntax
40-
let b:async_clj_updated_highlight = 1
41-
elseif get(fst, 'err', '') !=# ''
42-
echohl ErrorMSG
43-
echo fst.err
44-
echohl NONE
45-
endif
46-
endfunction
47-
48-
function! AsyncCljRequestHighlight(...)
49-
if a:0 > 0 && get(a:1[0], 'err', 0)
50-
echohl ErrorMSG
51-
echo a:1[0].err
52-
echohl NONE
53-
return
54-
endif
55-
56-
let ns = AcidGetNs()
57-
let opts = g:clojure_highlight_local_vars ? '' : ' :local-vars false'
58-
call s:silent_acid_send({"op": "eval", "code": "(async-clj-highlight/ns-syntax-command '" . ns . opts . ")"}, 'AsyncCljHighlightExec')
7+
function! AsyncCljHighlightExec(value)
8+
exec eval(a:value)
9+
let &syntax = &syntax
5910
endfunction
6011

61-
function! AsyncCljHighlightPrepare(msg)
62-
let exists = a:msg[0].value
63-
if exists =~ 'nil'
64-
let buf = join(readfile(globpath(&runtimepath, 'clj/async_clj_highlight.clj')), "\n")
65-
call s:silent_acid_send({'op': 'eval', 'code': "(do ". buf . ")"}, 'AsyncCljRequestHighlight')
66-
endif
67-
call AsyncCljRequestHighlight()
68-
endfunction
69-
70-
function! s:syntax_match_references(bang)
71-
if g:clojure_highlight_references && (a:bang || !exists('b:b:async_clj_updated_highlight'))
72-
call s:silent_acid_send({'op': 'eval', 'code': "(find-ns 'async-clj-highlight)"}, 'AsyncCljHighlightPrepare')
73-
endif
74-
endfunction
75-
76-
function! s:toggle_clojure_highlight_references()
77-
let g:clojure_highlight_references = !g:clojure_highlight_references
78-
79-
if g:clojure_highlight_references
80-
call s:syntax_match_references(0)
81-
else
82-
unlet! b:clojure_syntax_keywords b:clojure_syntax_without_core_keywords
83-
let &syntax = &syntax
84-
endif
85-
endfunction
12+
command! -bar -bang ClojureAsyncHighlight call luaeval("require('cljhl').preload()")
8613

8714
augroup async_clj_highlight
8815
autocmd!
89-
autocmd User AcidRequired ClojureHighlightReferences
16+
autocmd User AcidLoadedAllNSs ClojureAsyncHighlight
17+
autocmd User AcidRequired ClojureAsyncHighlight
9018
augroup END
9119

92-
command! -bar ToggleClojureHighlightReferences call s:toggle_clojure_highlight_references()
93-
command! -bar -bang ClojureHighlightReferences call s:syntax_match_references(<bang>0)
94-
95-
map <plug>AsyncCljDoHighlight :ClojureHighlightReferences!<CR>

0 commit comments

Comments
 (0)