Fork me on GitHub
#calva
<
2022-12-03
>
p-himik09:12:12

I'm writing an app that heavily relies on some constants that can easily be assigned colors. E.g. I have a "high risk" warning specification assigned to a high-risk name within a let block. Is there any way (or could there be a way) to highlight just that high-risk in just that let block in a specific color, both the text and its background? Such highlighting should also be a part of the project definition so it could easily be shared with other developers. In particular it's useful with DSLs. In this particular case, I'm in the process of writing a table matching macro that would be used something like this:

(table-match value1 | value2
               ;; @formatter:off
                       | hom-mut         | hz           | hom-wt          | nil
               hom-mut | high-risk       | high-risk    | low-risk-spread | unknown-risk
               hz      | high-risk       | high-risk    | low-risk        | unknown-risk
               hom-wt  | low-risk-spread | low-risk     | low-risk        | low-risk
               nil     | unknown-risk    | unknown-risk | low-risk        | unknown-risk
               ;; @formatter:on
               )
In such a table, it would be incredibly useful to immediately see the overall structure by just color, especially if a table gets larger. If there's no easy way of achieving it, I suppose I should probably take a closer look at Joyride, right?

pez09:12:50

Do you think you could achieve it with regular expressions? If so this extension probably is the answer: https://github.com/fabiospampinato/vscode-highlight

pez09:12:08

Otherwise Joyride is the best bet, I think. We don't ship rewrite-clj with it yet, but you might get away with including it in your source, I don't remember what dependencies it has...

pez09:12:14

Maybe there is a way we can get edamame in there, @U04V15CAJ? Would that be useful for things like this?

p-himik09:12:16

The vscode-highlight extension applies regexes globally, right? It's a great start already, especially given that I can use some naming convention to group items together. But ideally coloring would be limited to the scope where a particular colored name exists. For example:

(let [;; @color:red
      high-risk (create-high-risk-warning)]
  ;; This value is colored in red because of the @ comment above.
  high-risk)

;; But this isn't colored.
(def high-risk "high risk")
Feels like it also ties to an ability to have function/macro-specific formatting settings. But I don't recall if there's any tool for that. Which makes me think that coloring can be done not only on a comment basis but also on a factory function basis.

pez09:12:30

It'll not really possible to express scope with a regex, I think... In lack of rewrite-clj or edamame, Calva has API for figuring out some common ranges. https://calva.io/api/#ranges making this at least a bit easier to do. So a Joyride script could use regexes for figuring out where you have those annotations. Then Calva's ranges API for figuring out the scopes. Then VS Code API for coloring things.

borkdude09:12:15

@U0ETXRFEW Yes, we can get edamame in there, it costs virtually nothing extra since it's already a dependency

pez09:12:34

It would help, wouldn't it?

borkdude09:12:01

@U0ETXRFEW Also we could include rewrite-clj - maybe figure out how to lazily load stuff using modules

borkdude09:12:05

Not sure if edamame helps for the above problem, I've only skimmed it. edamame returns s-expressions, doesn't preserve whitespace and comments

pez09:12:50

Ah, maybe that could trip this up. But if #_@color:red is used instead, maybe not...

borkdude09:12:16

unevals are also not parsed: s-expressions. but rewrite-clj could help here

borkdude09:12:51

rewrite-clj has been requested before

borkdude09:12:06

it could also work if you expose rewrite-clj via calva's api, not sure

pez09:12:27

Hmmm, that's an interesting take!

pez09:12:53

If that would work, it would make a lot of sense because this is Clojure specific.

borkdude09:12:15

but you would have to serialize the arguments back and forth

borkdude09:12:21

which could make that awkward

pez09:12:01

I'll make some experiments.