Skip to content

Option to inline case statements if the inline formatting only spans one line #1607

Description

@godalming123

I would like odinfmt to inline case statements more than {inline_single_stmt_case: false} does, but less then {inline_single_stmt_case: true} does.

Take the following code for example:

package main

import "core:fmt"

main :: proc() {
	switch 'a' {
	case 'b':
		if true {
			fmt.println("hi")
		}
	}
}

{inline_single_stmt_case: true} formats this example like so:

package main

import "core:fmt"

main :: proc() {
	switch 'a' {
	case 'b': if true {
				fmt.println("hi")
			}
	}
}

In this case, I would prefer odinfmt to not inline the case.

But I would like odinfmt to inline cases if the inlined case spans one line:

token_formatter :: proc(fi: ^fmt.Info, arg: any, verb: rune) -> bool {
    if verb != 'v' {
        return false
    }
    token := cast(^TokenContents)arg.data
    switch value in token {
    case Error: fmt.wprintf(fi.writer, "the tokenizer error:\n%s", value)
    case NewlineToken: fmt.wprint(fi.writer, "a newline")
    case OpenBracketToken: fmt.wprint(fi.writer, "an open bracket (`(`)")
    case CloseBracketToken: fmt.wprint(fi.writer, "a close bracket (`)`)")
    case OpenSquareBracketToken: fmt.wprint(fi.writer, "an open square bracket (`[`)")
    case CloseSquareBracketToken: fmt.wprint(fi.writer, "a close square bracket (`]`)")
    case OpenAngleBracketToken: fmt.wprint(fi.writer, "an open angle bracket (`<`)")
    case LessThanOrEqualToken: fmt.wprint(fi.writer, "a less than or equal sign (`<=`)")
    case CloseAngleBracketToken: fmt.wprint(fi.writer, "a close angle bracket (`>`)")
    case GreaterThanOrEqualToken: fmt.wprint(fi.writer, "a greater than or equal sign (`>=`)")
    case CommaToken: fmt.wprint(fi.writer, "a comma (`,`)")
    // ...
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions