Color parens of different levels differently

This commit is contained in:
Dan Holmsand 2014-01-05 15:52:51 +01:00
parent 5031284e81
commit d16d0b2405
1 changed files with 24 additions and 13 deletions

View File

@ -25,7 +25,7 @@
(def nssrc (def nssrc
"(ns example "(ns example
(:require [cloact.core :as cloact :refer [atom]]) (:require [cloact.core :as cloact :refer [atom]]))
") ")
(defn src-for-names [names] (defn src-for-names [names]
@ -46,7 +46,7 @@
open-p (str "[" open "]") open-p (str "[" open "]")
close-p (str "[" close "]") close-p (str "[" close "]")
iden-p (str "[^" sep "]+") iden-p (str "[^" sep "]+")
any-p ".|\\n" any-p (str "[" ws "]+" "|.")
patt (re-pattern (str "(" patt (re-pattern (str "("
(string/join ")|(" [str-p open-p close-p (string/join ")|(" [str-p open-p close-p
iden-p any-p]) iden-p any-p])
@ -64,25 +64,36 @@
any [:other s])))) any [:other s]))))
(defn syntaxify [src] (defn syntaxify [src]
(let [def-re #"^def|^ns\b"] (let [def-re #"^def|^ns\b"
parcol ["#9a3" "#c83" "#4a8"]
ncol (count parcol)
paren-style (fn [level]
{:style {:color (nth parcol (mod level ncol))}})]
(loop [tokens (tokenize src) (loop [tokens (tokenize src)
prev nil prev nil
level 0
res []] res []]
(let [[kind val] (first tokens) (let [[kind val] (first tokens)
part (case kind level' (case kind
:str-litt [:span {:style {:color "green"}} val] :open (inc level)
:keyw [:span {:style {:color "blue"}} val] :close (dec level)
:builtin [:span {:style {:font-weight "bold"}} val] level)
:iden (if (and prev (re-find def-re prev)) style (case kind
[:span {:style {:color "#55c" :str-litt {:style {:color "green"}}
:font-weight "bold"}} val] :keyw {:style {:color "blue"}}
val) :builtin {:style {:font-weight "bold"}}
val) :iden (when (and prev (re-find def-re prev))
{:style {:color "#55c"
:font-weight "bold"}})
:open (paren-style level)
:close (paren-style level')
nil)
remain (rest tokens)] remain (rest tokens)]
(if-not (empty? remain) (if-not (empty? remain)
(recur remain (recur remain
(if (= kind :other) prev val) (if (= kind :other) prev val)
(conj res part)) level'
(conj res [:span style val]))
(apply vector :pre res)))))) (apply vector :pre res))))))
(defn src-for [defs] (defn src-for [defs]