-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
fix(clojure) comment macro should not be comment
scope
#3395
Conversation
|
@joshgoebel I may submit more PRs for the Clojure syntax as there are still things that are highlighted not quite right. For Reference:
|
const COMMENT = hljs.COMMENT( | |
';', | |
'$', | |
{ | |
relevance: 0 | |
} | |
); |
It is common practice to use multiple ;
to signal hierarchy.
;
for margin inline comments
;;
for line comments
;;;
for top-level comments
;;; Here is a function:
(defn square
"This is a docstring."
[x]
;; Now we will square x.
(* x x)) ; Clojure uses prefix notation.
(comment)
Marco
Then there is the (comment)
macro, that just ignores the body and yields' nil. It is nothing special really. What's important though is that code inside the comment macro is still code and has to be valid Clojure code!
(comment
(defn 2abc [])) ; this will fail! Functions must not start with a number.
(comment (comment (comment "Why not?")))
;; Since it is nothing special, we could redefine it.
(defn comment [str]
(println str))
;; This is rarely used in the wild, but some libraries overwrite core functions.
(ns html)
(defn comment [body]
(str "<!-- " body " -->")
(ns my-namespace)
(html/comment "my comment")
; => <!-- my comment -->
#_
Discard
Additionally, there is a reader macro, #_
ignores the following form completely. It is used to comment out code.
#_(comment
(defn 2abc [])) ; this won't fail, the code is skipped while reading the file.
#_"You can skip any form" ; not only list forms.
#_42
;; You have no power here! #_(some-code 1 2 3)
(merge
{"a" 1}
#_{"b" 2
"c" 3}
{"d" 4})
;; => {"a" 1, "d" 4}
How do others render Clojure?
; I am a comment
(+ 1 2 3) ; I am a comment behind a form
(defn square [x] (* x x))
; (comment) is mostly used for code that the dev wants to be evaluated "by hand"
; in a REPL, but not automatically. It has to be valid clojure code!
(comment
;; sanity check my code
(= 9 (square 3)))
; #_ ignores the next form. This is used to comment out code during development.
#_(print "I will be skipped while reading")
; #_ is stackable, useful for maps.
{"a" 1
#_#_"b" 2}
This is how Cursive, the Clojure plugin for IntelliJ, renders the text:
Calva, the VSCode plugin for Clojure, renders the skipped (#_
) not like a comment, but in a lighter colour.
@joshgoebel BTW, is there a way to overrule scopes? Yesterday I experimented with highlighting everything as comment, when using (comment
(+ 1 2 3)) Would result in: <span class="hljs-comment">
(comment
(<span class="hljs-name">+</span> <span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span>))
</span> The only way I saw to overcome this is having a clone of all modes without the scopes for exclusive use in the comment mode. |
Not exactly. If your trying to not highlight them then you ONLY need modes to match things that could interfere with bracket counting... you don't need ALL the modes... so in many languages this would mean you'd need to match strings (unscoped) to avoid things like |
I'd also be willing to try this if it was reliable and you wanted to finish it up. One could handle |
In my opinion, I can open an issue with other things I noticed off with Clojure highlighting. You are right regarding
Regarding #_1
#_ 1 ; spaces between
#_ ,,,,,,,,,, ; commas are pruely decorative and treated as whitespace
1] |
Ok, we can try that for now. That would put this PR back on track. :)
Yes I realized, but those other cases may be difficult (or impossible) to deal with properly as it is not our goal to parse languages (at other than a surface level)... trying to do so very quickly leads to grammars that are difficult or impossible to maintain. |
comment
macro matching more then it shouldcomment
scope
Changes
Clojure has a macro
comment
, which evaluates to nothing. It is not a special language feature.The current grammar highlights every list beginning with "comment" (See screenshot)
I guess the intention behind that mode was at some point that some editor themes highlight the whole blocks (not just the name) like comments.
Checklist
CHANGES.md