Skip to content

Migrate to lua #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clj/async_clj_highlight.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
66 changes: 66 additions & 0 deletions lua/cljhl/init.lua
Original file line number Diff line number Diff line change
@@ -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
88 changes: 6 additions & 82 deletions plugin/async_clj_highlight.vim
Original file line number Diff line number Diff line change
@@ -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(<bang>0)

map <plug>AsyncCljDoHighlight :ClojureHighlightReferences!<CR>