Skip to content

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

Closed

Description

@Invertisment

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

Activity

  1. axvr commented on Aug 4, 2022

    @axvr
    Member

    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
  2. Invertisment commented on Aug 5, 2022

    @Invertisment
    ContributorAuthor

    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)
    
  3. Invertisment commented on Aug 30, 2022

    @Invertisment
    ContributorAuthor

    Should I create a PR?

  4. axvr commented on Sep 1, 2022

    @axvr
    Member

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

  5. Invertisment commented on Dec 6, 2022

    @Invertisment
    ContributorAuthor

    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.

  6. axvr commented on Jan 7, 2023

    @axvr
    Member

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

  7. linked a pull request that will close this issueKeyword and Symbol prefix matching #27on Jun 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    highlightingAffects syntax highlighting

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions