Minimalistic Odin formatter

I made a odin formatter for myself. Indentation is mostly what I need. I might be adding more features in the future.

Current Features:

  • Indents lines with 1 tab per scope level
  • Leaves the contents like multi line strings and multi line comments untouched
  • Adds one indent level to parameters if they were broken up into multiple lines.

I thought it might be useful to others. I’d love feedback and if you’d like more features or customization.

Thanks for sharing. I did a quick test, not very extensive. I can see how this could useful for when cloning someone else’s code that is all indented with spaces or when your editor decides to default to spaces instead of tabs without telling you (happened to me several times, grrr)

I know it’s a simple thing, but there was no help usage output, and when I ran the first time without a file, it just waited for input. Maybe at least output a message a file is expected and quit-out?

Input

package badcode

main :: proc() {
hello:="hello"
stuff:int
switch stuff {
case 1:
case 2:
}
}

Output

package badcode

main :: proc() {
  hello:="hello"
  stuff:int
  switch stuff {
    case 1:
    case 2:
  }
}

Should Be

package badcode

main :: proc() {
  hello := "hello"
  stuff: int
  switch stuff {
  case 1:
  case 2:
  }
}

Cannot seem to find the link at the moment, but I’ve see somewhere that case statements are meant to be on the same column as the switch.

Edit: This is the best example for switches I could find. switch-statement Nothing that suggests to use this format, but it seems to be common for Odin code.

1 Like

I played around a little more. You might also find using a memory tracker useful for when working with Odin.

Original Tracker
tracking-memory-leaks

I’ve also made a colorized and formatted tracker here:
xuul tracker

When I added a tracker, I found the following leaks. It’s not a big deal for a program that runs for a short time and then quits. The memory is released at that time anyway. I found it still to be good practice to manage memory properly in all programs to gain better habits. It took me a little while, but after forcing myself to do it, now it feels second nature and initiative. Never-mind the line numbers for main.odin, they reflect my file which I added the tracker to, which shifted everything down about 8 lines.

 Leaked Bytes    3/5 Allocations Not Freed                                      
 93              lucyfmt/main.odin:49:16                                    main
 160             lucyfmt/formatter.odin:163:11                     format_source
 102             lucyfmt/formatter.odin:230:12                     format_source
1 Like

noted. will do

case lines skip an indentation level now.

I purposely didn’t free long standing memory, as it’s a short lived program and it literally has no impact over anything.

might do it at some point later if this project gets big, so it can pass a tracking report if i decide to add one.

I’ll check out those trackers. That’s interesting. I have custom tracking logs in my other projects. I use the Tracking Allocator.

Thank you for all your input! :slight_smile: