Is there a way to highlight namespace of a keyword separately?
Invertisment opened this issue ยท 6 comments
Hey.
I use CIDER in Spacemacs and they do this:

How can I achieve that in vim/nvim?
The default behavior in nvim gives me this:

When I try to do define my own syntax for highlighting part of a keyword I can't understand how to do it (copied from this project) and I end up highlighting the whole keyword or nothing at all:
syntax match clojureKeywordMine "\v<:{1,2}([^ \n\r\t()\[\]{}";@^`~\\/]+/)*<!>"
highlight clojureKeywordMine ctermbg=darkred
There isn't a built-in way currently, This is something I would like to add at some point, but just haven't had the time.
However in the mean time, you can place something like this in your config:
syntax match clojureKeywordNs contained "\v:{1,2}[^/]+\ze/"
syntax match clojureKeywordNsSep contained "/"
syntax match clojureKeyword "\v<:{1,2}([^ \n\r\t()\[\]{}";@^`~\\/]+/)*[^ \n\r\t()\[\]{}";@^`~\\/]+:@1<!>" contains=clojureKeywordNs,clojureKeywordNsSep
" Replace these lines to pick whatever colours you want.
highlight link clojureKeywordNs Todo
highlight link clojureKeywordNsSep WarningAlright.
If you want to add it then managed to do something a little more general-purpose with it. It's not the best thing because when I started playing with it I found that I would break all of the custom themes that users have made throughout the years.
My attempt allows to highlight symbol and keyword namespaces. I didn't know which color to use so I tried to reuse what I had.
This is my attempt of fixing some of the issues that were weird to me after coming from CIDER (I didn't touch clojureFunc because it was too long):
" clojure highlighting
syntax match clojureNs contained "\v[^/ :']+\ze/"
syntax match clojureNsSep contained "/"
syntax match clojureKeywordNsColon contained "\v<:{1,2}"
syntax match clojureKeyword "\v<:{1,2}([^ \n\r\t()\[\]{}";@^`~\\/]*/)*[^ \n\r\t()\[\]{}";@^`~\\/]*:@1<!>" contains=clojureNs,clojureNsSep,clojureKeywordNsColon
syntax match clojureSymbol "\v%([a-zA-Z!$&*_+=|<.>?-]|[^\x00-\x7F])+%(:?%([a-zA-Z0-9!#$%&*_+=|'<.>/?-]|[^\x00-\x7F]))*[#:]@1<!" contains=clojureNs,clojureNsSep
highlight link clojureKeywordNsColon clojureKeyword
highlight link clojureNs clojureCharacter
highlight link clojureNsSep Normal
Also this makes :/ and :kw/ highlight as keywords so that it wouldn't flicker when I type. I use linter anyway so it's not a problem. This is how it looks:

Screenshots
Text from screenshot (if you want to test something):
:hello
:hello-there
:hello/there
:oh-hello/there
::util/hello-there
"hello/world"
(util/new-message :one :two :le/three)
'symbol/hi
(clojure.core/inc 15)
(inc 15)
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn hello/world []) ;; this is an invalid fn
(defn world [])
(world)
Should I create a PR?
Please do if you would like to. (I can see that you already have. ๐ Usually I respond much quicker, but I've been pretty busy recently, so sorry for the delay.)
I created a PR long time ago. I played around with the testing macro and I think I should remove it completely and replace it with plain clojure.test/are calls (to still be able to have table tests). The testing macro is emulating that function and I don't see much point to have it as it's a harder way to do something that already exists in standard library (actually the testing macro doesn't even detect anything, it's just a smoke test and it checks whether things exist in the outcome of the regex matcher).
In the current state of the PR I tried to play around with the macro and just make it barely working enough for my namespace highlighting (it's mergeable nevertheless but I'd like to remove the testing macro as I don't see much value from it).
From the time of my PR I used my own version of the plugin and I think I didn't find any problems with it at all.
The only issue would be that it doesn't highlight the namespace-prefixed maps if this syntax is found:
#:my-namespace{:name-one :hello}
So in this example my-namespace would not be highlighted as it would need more work.
Hi, thank you for your work on this, and sorry for the (painfully slow) amount of time it has taken me to get around to looking at it.
I've been using it for a little while and it has been working well for me. It would be nice to have namespaced maps highlighted correctly too, but this is already above and beyond what I could have hoped for.
I completely agree with replacing the testing macro, so please feel free to replace it if you like. (I had already redone the indentation tests in #26, but hadn't got around to the syntax tests.)
Once again, thank you! I will merge #27 now.
Edit: if you submit any more PRs here, I promise I won't make you wait as long. ๐



