diff --git a/autoload/vim_clojure_highlight.cljs b/autoload/vim_clojure_highlight.cljs new file mode 100644 index 0000000..99afb03 --- /dev/null +++ b/autoload/vim_clojure_highlight.cljs @@ -0,0 +1,47 @@ +(ns vim-clojure-highlight) + +;;;;;;;;;;;;;;;;;;;; Copied from vim-clojure-static.generate ;;;;;;;;;;;;;;;;;;; + +(defn- fn-var? [v] + (let [f @v] + (or (when (seq (:arglists (meta v))) + ; in clojurescript, all vars have :arglists, but it may be empty + true) + (fn? f) + (instance? MultiFn f)))) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(defn clojure-core? + "Is this var from clojure.core?" + [var] + (= "cljs.core" (-> var meta :ns str))) + +(defn var-type [v] + (let [f @v + m (meta v)] + (cond (:macro m) "clojureMacro" ;; NOTE: this doesn't actually work, sadly + (fn-var? v) "clojureFunc" + :else "clojureVariable"))) + +(defn syntax-keyword-dictionary [ns-refs] + (->> ns-refs + (group-by (comp var-type peek)) + (mapv (fn [[type sym->var]] + (->> sym->var + (mapv (comp pr-str str first)) + (clojure.string/join \,) + ((fn [values] + (str "'" type "': [" values "]")))))) + (clojure.string/join \,))) + +(defn ns-syntax-command [ns publics & opts] + ; NOTE: (ns-publics) is a macro in clojurescript! + (let [{:keys [local-vars] :or {local-vars true}} (apply hash-map opts) + dict (syntax-keyword-dictionary + ; NOTE: ns-refers and ns-aliases don't exist in clojurescript + (when local-vars + publics))] + ; NOTE: don't disable core keywords just yet + (str "let b:clojure_syntax_keywords = {" dict "}"))) + diff --git a/autoload/vim_clojure_highlight.vim b/autoload/vim_clojure_highlight.vim index 95df568..bc0bdd6 100644 --- a/autoload/vim_clojure_highlight.vim +++ b/autoload/vim_clojure_highlight.vim @@ -1,14 +1,51 @@ " vim-clojure-highlight +function! s:is_cljs() + return expand('%:e') == 'cljs' +endfunction + function! s:session_exists() return exists('g:fireplace_nrepl_sessions') && len(g:fireplace_nrepl_sessions) endfunction -function! s:require() - if fireplace#evalparse("(find-ns 'vim-clojure-highlight)") ==# '' - let buf = join(readfile(globpath(&runtimepath, 'autoload/vim_clojure_highlight.clj')), "\n") +function! s:evalparse(cmd) + if !s:is_cljs() + return fireplace#evalparse(a:cmd) + endif + + " NOTE: evalparse doesn't seem to work in cljs + " due to the {'session': 0} opts (weird). + let cmd = printf(g:fireplace#reader, a:cmd) + let resp = fireplace#session_eval(cmd) + if !empty(resp) + return eval(resp) + else + return '' + endif +endfunction + +function! s:require(force) + if !a:force && s:evalparse("(nil? (find-ns 'vim-clojure-highlight))") == 0 + return 0 + endif + + let ext = expand('%:e') + if ext != 'cljs' && ext != 'clj' + let ext = 'clj' + endif + let file = globpath(&runtimepath, 'autoload/vim_clojure_highlight.' . ext) + let buf = join(readfile(file), "\n") + if s:is_cljs() + call fireplace#session_eval('(ns vim-clojure-highlight) (do ' . buf . ')') + else call fireplace#session_eval('(do ' . buf . ')') endif + return 1 +endfunction + +" temporary, to force-reload the vim-clojure-highlight ns +function! vim_clojure_highlight#require() + return s:require(1) endfunction " Pass zero explicitly to prevent highlighting local vars @@ -16,14 +53,17 @@ function! vim_clojure_highlight#syntax_match_references(...) if !s:session_exists() | return | endif try - call s:require() + call s:require(0) let ns = "'" . fireplace#ns() let opts = (a:0 > 0 && !a:1) ? ' :local-vars false' : '' + if s:is_cljs() + let ns = ns . ' (ns-publics ' . ns . ')' + endif + execute s:evalparse("(vim-clojure-highlight/ns-syntax-command " . ns . opts . ")") - execute fireplace#evalparse("(vim-clojure-highlight/ns-syntax-command " . ns . opts . ")") let &syntax = &syntax - catch /./ + catch /.*/ endtry endfunction diff --git a/plugin/vim_clojure_highlight.vim b/plugin/vim_clojure_highlight.vim index 589ba99..7baa5f6 100644 --- a/plugin/vim_clojure_highlight.vim +++ b/plugin/vim_clojure_highlight.vim @@ -28,6 +28,7 @@ endfunction augroup vim_clojure_highlight autocmd! autocmd BufRead *.clj ClojureHighlightReferences + autocmd BufRead *.cljs silent! ClojureHighlightReferences augroup END command! -bar ToggleClojureHighlightReferences call s:toggle_clojure_highlight_references()