zmkfirmware / zmk

ZMK Firmware Repository
https://zmk.dev/
MIT License
2.62k stars 2.69k forks source link

DeviceTree Formatter #583

Open innovaker opened 3 years ago

innovaker commented 3 years ago

We're increasingly in need of a DeviceTree formatter. We're looking for a volunteer to implement one.

It must ...

It should ...

It may ...

See relevant conversations:

innovaker commented 3 years ago

Please reply to this issue if you start work on it so that we can assign the issue to you (avoids duplicate efforts).

joelspadin commented 3 years ago

Given https://github.com/zmkfirmware/zmk/pull/568, a Prettier plugin seems like a convenient way to implement this.

Ignoring bindings properties could be done by implementing a // prettier-ignore comment and just prefixing bindings in keymaps with it. You could additionally try to automatically ignore bindings, but you'd want to still support the ignore comment for future features like per-key RGB color maps, etc. that the formatter doesn't yet know about.

I've written a mostly complete DT parser that runs in NodeJS at https://github.com/joelspadin/tree-sitter-devicetree. It is built on https://tree-sitter.github.io/tree-sitter/. Here is a project that uses it as an example: https://github.com/joelspadin/zmk-tools/blob/main/src/Parser.ts. (To my knowledge the only thing not yet supported in the parser is labels inside property values.)

innovaker commented 3 years ago

To anyone who reads this, we're still looking for a volunteer to spearhead this issue.

If you're a developer wishing to contribute to ZMK without touching C/embedded, this might be just the ticket.

nickcoutsos commented 2 years ago

Hi!

I've been doing a little work on this for my own project with the help of tree-sitter-devicetree and I don't know if I can satisfy the requirements describe above, but I'd be interested in some discussion and knowing if the ZMK maintainers have any kind of preferred style for formatting devicetree code.

Here are the basic rules I came up with for myself.

Indentation

I picked 4-space tabs because... if I have to force something on people it seems like a safe bet. But it's gotten me thinking about the ways people make these configurations portable. I'm not a vim user but it might be simple enough to look out for a modeline to get the preferred tab size (I'm reading the file already) or even to check for an .editorconfig.

Spacing

As is frequently the case, this is what I came up with by experimentation and it's more than likely I'm missing some more critical edge cases due to my ignorance of devicetree and maybe more complex keymaps. I haven't even looked at applying it to board/shield definitions yet.

jakenvac commented 1 year ago

I understand that it makes sense to structure keymaps as DeviceTree files under the hood, but is this really a good way for the end user to create their keymaps? Ive built a preprocessor that allows you to create your keymaps in typescript programmatically and outputs the relevant DTS. Would this be something the zmk project is interested in? Right now its very tailored to my use but I'd be happy to make it more generic and make the repo public/contribute the code to zmk

Sorry for bad formatting/grammar. Currently on mobile.

ELLIOTTCABLE commented 1 year ago

@jakehl Personally, I prefer declarative formats (see Dhall's homepage and documentation for some good arguments there), but I wouldn't mind seeing your work published. Sounds like it'd be useful in some cases. (=

joebonneau commented 3 months ago

I landed here from a search for a devicetree formatter and came across dtsfmt. I don't know if the maintainer would be interested in contributing to ZMK directly, but I wanted to bring it to the group's attention.