cursive icon indicating copy to clipboard operation
cursive copied to clipboard

Discard (#_) used before metadata results in incorrect comment highlighting

Open imrekoszo opened this issue 3 years ago • 0 comments

From https://clojurians.slack.com/archives/C0744GXCJ/p1644931688839219

Currently when discard (#_) is used before a metadata literal, it results in only the metadata literal being highlighted as a comment:

Screenshot 2022-02-15 at 14 22 19

This is incorrect in Clojure source files as Clojure discards the form the metadata applies to (which is the form after the metadata literal), so in the example in the screenshot, [chan] should also be highlighted as a comment.

Colin: Does that mean that #_ ^:foo bar baz would return baz without meta, or that the meta would apply to bar and would get dropped?

Alex Miller:

#_ says read the next thing and drop it
  ^:foo says here's some meta, read the next thing and give it that meta
    read bar symbol
  ... meta is applied to bar
... #_ drops bar w/meta
read baz symbol

For edn files the matter is somewhat more complex. The edn format documentation does not mention metadata at all and does not appear to allow any tokens to begin with ^. The Clojure implementation of edn read however has an extension which handles metadata literals just like Clojure itself:

(clojure.edn/read-string "[^asdf foo]")
=> [foo]
(clojure.edn/read-string "[#_^asdf foo]")
=> []
(clojure.edn/read-string "^asdf foo")
=> foo
(meta (clojure.edn/read-string "^asdf foo"))
=> {:tag asdf}
(prn-str (clojure.edn/read-string "^asdf foo"))
=> "foo\n"
(pr-str (clojure.edn/read-string "^asdf foo"))
=> "foo"

imrekoszo avatar Feb 15 '22 20:02 imrekoszo