Skip to content
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

Is there a way to highlight namespace of a keyword separately? #24

Closed
Invertisment opened this issue Aug 3, 2022 · 6 comments · Fixed by #27
Closed

Is there a way to highlight namespace of a keyword separately? #24

Invertisment opened this issue Aug 3, 2022 · 6 comments · Fixed by #27
Labels
highlighting Affects syntax highlighting

Comments

@Invertisment
Copy link
Contributor

Invertisment commented Aug 3, 2022

Hey.
I use CIDER in Spacemacs and they do this:
image

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

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
@axvr
Copy link
Member

axvr commented Aug 4, 2022

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 Warning

@Invertisment
Copy link
Contributor Author

Invertisment commented Aug 5, 2022

Alright.
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:
image

Screenshots

srcery:
image

kanagawa:
image

Atelier_ForestDark:
image

birds-of-paradise:
image

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)

@Invertisment
Copy link
Contributor Author

Should I create a PR?

@axvr
Copy link
Member

axvr commented Sep 1, 2022

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.)

@Invertisment
Copy link
Contributor Author

Invertisment commented Dec 6, 2022

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.

@axvr
Copy link
Member

axvr commented Jan 7, 2023

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. 😉

@axvr axvr closed this as completed Jan 7, 2023
@axvr axvr added the highlighting Affects syntax highlighting label Jun 23, 2023
@axvr axvr linked a pull request Jun 23, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
highlighting Affects syntax highlighting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants