Would it be possible to add an option for the indent size for the format tool?

Would it be possible to add an option for the indent size for the format tool?
with possibly a default of “2”

Reason: I’m having difficulties to read code whose indentations are smaller than 4 characters.

for instance:

crystal tool format -4 my file.cr
crystal tool format -2 my file.cr

Thanks.

Adding the ability to configure the formatter has been historically rejected. See Add configuration for Crystal format tool · Issue #8366 · crystal-lang/crystal · GitHub. So the short answer would be yes, but not likely to happen.

3 Likes

Thanks.

I have written a hack of my own (a wrapper around the formatter which doubles the indent).

So, basically, I can use it for my own use, and revert to the standard for publication on GitHub. :slight_smile:

2 Likes

I feel like this kind of visual enhancement could be provided by a text editor.

1 Like

This is what I use for the moment:
It’s a bit silly, but it works.


def doubleSpace(line : String) : String
    start = ""
    stop = ""
    beyond_first_char = false
    line.each_char do |c|
        if c.ascii_whitespace? && beyond_first_char==false
            start = start + c
        else
            beyond_first_char = true
            stop = stop + c
        end
    end
    newline = start * 2 + stop
end

# - - -
PATH = "$HOMEBREW_PREFIX/bin/"
cmd = `#{PATH}crystal tool format #{ARGV[0]}`
puts cmd

formatted = IO::Memory.new
File.each_line(ARGV[0]) do |line|
    formatted << doubleSpace(line) << "\n"
end

output = "%.cr"
# Backup original file
`cp #{ARGV[0]} %.cr`

# Write formateted file 
f_out  = File.open(ARGV[0], "w")
f_out.write(formatted.to_slice)
f_out.close