diff --git a/clj/async_clj_highlight.clj b/clj/async_clj_highlight.clj index a97c4c6..60ff836 100644 --- a/clj/async_clj_highlight.clj +++ b/clj/async_clj_highlight.clj @@ -68,7 +68,7 @@ (ns-aliases ns))) (defn var-type [v] - (let [f @v m (meta v)] + (let [_ @v m (meta v)] (cond (clojure-core? v) (core-symbol->syntax-group (:name m)) (:macro m) "clojureMacro" (fn-var? v) "clojureFunc" diff --git a/lua/cljhl/init.lua b/lua/cljhl/init.lua new file mode 100644 index 0000000..f941dcf --- /dev/null +++ b/lua/cljhl/init.lua @@ -0,0 +1,66 @@ +-- luacheck: globals vim +local acid = require("acid") +local log = require("acid.log").msg +local connections = require("acid.connections") +local commands = require("acid.commands") +local eval = require("acid.ops").eval + +local cljhl = {} + +local conn_to_key = function(conn) + return tostring(conn[1]) .. ":" .. tostring(conn[2]) +end + +cljhl.cache = {} + +cljhl.apply = function(msg) + if msg.status ~= nil then + return + elseif msg.err ~= nil then + log("Can't apply highlight.", msg.err) + return + end + vim.api.nvim_call_function("AsyncCljHighlightExec", {msg.value}) +end + +cljhl.highlight = function(ns) + ns = ns or vim.api.nvim_call_function("AcidGetNs", {}) + if ns == nil then + return + end + + local opts = "" + if vim.api.nvim_get_var("clojure_highlight_local_vars") == 0 then + opts = " :local-vars false" + end + + local payload = eval{code = "(ns-syntax-command '" .. ns .. opts ..")", ns = "async-clj-highlight"} + acid.run(payload:with_handler(cljhl.apply)) +end + +cljhl.preload = function(ns) + ns = ns or vim.api.nvim_call_function("AcidGetNs", {}) + + if ns == nil then + return + end + + local pwd = vim.api.nvim_call_function("getcwd", {}) + local conn = connections.attempt_get(pwd) + local key = conn_to_key(conn) + + if cljhl.cache[key] ~= nil then + cljhl.highlight(ns) + else + local cmd = commands.preload{files = {"clj/async_clj_highlight.clj"}}[1] + acid.run(cmd:with_handler(function(data) + if data.status then + return + end + cljhl.cache[key] = true + cljhl.highlight(ns) + end, conn)) + end +end + +return cljhl diff --git a/plugin/async_clj_highlight.vim b/plugin/async_clj_highlight.vim index c32c82a..3633f1d 100644 --- a/plugin/async_clj_highlight.vim +++ b/plugin/async_clj_highlight.vim @@ -1,95 +1,19 @@ " vim-clojure-highlight -if !exists('g:clojure_highlight_references') - let g:clojure_highlight_references = 1 -endif - if !exists('g:clojure_highlight_local_vars') let g:clojure_highlight_local_vars = 1 endif -function! s:disable_acid_log() - if exists('b:acid_log_messages') - let b:acid_old_log_value = b:acid_log_messages - else - let b:acid_log_messages = 0 - endif -endfunction - -function! s:restore_acid_log() - if exists('b:acid_old_log_value') - let b:acid_log_messages = b:acid_old_log_value - unlet b:acid_old_log_value - else - unlet b:acid_log_messages - endif -endfunction - -function! s:silent_acid_send(data, handler_fn) - call s:disable_acid_log() - echom "Disabled log: ".b:acid_log_messages - call AcidSendNrepl(a:data, 'VimFn', a:handler_fn) - call s:restore_acid_log() -endfunction - -function! AsyncCljHighlightExec(msg) - let fst = a:msg[0] - if get(fst, 'value', '') !=# '' - exec eval(fst.value) - let &syntax = &syntax - let b:async_clj_updated_highlight = 1 - elseif get(fst, 'err', '') !=# '' - echohl ErrorMSG - echo fst.err - echohl NONE - endif -endfunction - -function! AsyncCljRequestHighlight(...) - if a:0 > 0 && get(a:1[0], 'err', 0) - echohl ErrorMSG - echo a:1[0].err - echohl NONE - return - endif - - let ns = AcidGetNs() - let opts = g:clojure_highlight_local_vars ? '' : ' :local-vars false' - call s:silent_acid_send({"op": "eval", "code": "(async-clj-highlight/ns-syntax-command '" . ns . opts . ")"}, 'AsyncCljHighlightExec') +function! AsyncCljHighlightExec(value) + exec eval(a:value) + let &syntax = &syntax endfunction -function! AsyncCljHighlightPrepare(msg) - let exists = a:msg[0].value - if exists =~ 'nil' - let buf = join(readfile(globpath(&runtimepath, 'clj/async_clj_highlight.clj')), "\n") - call s:silent_acid_send({'op': 'eval', 'code': "(do ". buf . ")"}, 'AsyncCljRequestHighlight') - endif - call AsyncCljRequestHighlight() -endfunction - -function! s:syntax_match_references(bang) - if g:clojure_highlight_references && (a:bang || !exists('b:b:async_clj_updated_highlight')) - call s:silent_acid_send({'op': 'eval', 'code': "(find-ns 'async-clj-highlight)"}, 'AsyncCljHighlightPrepare') - endif -endfunction - -function! s:toggle_clojure_highlight_references() - let g:clojure_highlight_references = !g:clojure_highlight_references - - if g:clojure_highlight_references - call s:syntax_match_references(0) - else - unlet! b:clojure_syntax_keywords b:clojure_syntax_without_core_keywords - let &syntax = &syntax - endif -endfunction +command! -bar -bang ClojureAsyncHighlight call luaeval("require('cljhl').preload()") augroup async_clj_highlight autocmd! - autocmd User AcidRequired ClojureHighlightReferences + autocmd User AcidLoadedAllNSs ClojureAsyncHighlight + autocmd User AcidRequired ClojureAsyncHighlight augroup END -command! -bar ToggleClojureHighlightReferences call s:toggle_clojure_highlight_references() -command! -bar -bang ClojureHighlightReferences call s:syntax_match_references(0) - -map AsyncCljDoHighlight :ClojureHighlightReferences!