Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty-print output code #25

Merged
merged 3 commits into from
Aug 11, 2023
Merged

Pretty-print output code #25

merged 3 commits into from
Aug 11, 2023

Conversation

holly-hacker
Copy link
Contributor

@holly-hacker holly-hacker commented Aug 9, 2023

Closes #23

This PR introduces pretty-printed output formatting. It does so by keeping an internal indent value to keep track of the current indentation level and simply printing additional newlines and whitespace in addition to the normal output.

This technique is fairly limited compared to something AST-based or using a custom writer, but for auto-generated code the output is quite good and this is less complex. I've also added additional whitespace around some symbols which makes the code very close to what a human would write.

The built-in test cases serve as good examples for the new output. I've also switched my personal project to this branch and the output looks quite nice.

@holly-hacker
Copy link
Contributor Author

As another example, here's my real-world code as output by this branch:

Code sample
// AUTO-GENERATED by typescript-type-def

export default types;
export namespace types {
    export type I32 = number;
    export type U32 = number;
    export type F32 = number;
    export type Item = {

        /**
         * The item tag. This is the closest we get to a string id but it does not exist for all items.
         */
        "tag": (string | null);
        "library_note": (string | null);
        "name": (string | null);
        "temp_name": (string | null);
        "temp_end_event": (string | null);
        "sort": types.I32;
        "img_no": types.I32;
        "price": types.U32;
        "lv": types.U32;
        "element": (types.U32 | null);
        "element_value": (types.U32 | null);
        "elem_fire": boolean;
        "elem_ice": boolean;
        "elem_thunder": boolean;
        "elem_air": boolean;
        "pc": (types.I32)[];
        "hp": (types.I32 | null);
        "atk": (types.I32 | null);
        "def": (types.I32 | null);
        "spd": (types.I32 | null);
        "w_hp": (types.F32 | null);
        "w_mp": (types.F32 | null);
        "w_atk": (types.F32 | null);
        "w_def": (types.F32 | null);
        "w_spd": (types.F32 | null);

        /**
         * The DLC required for this item. In practice, this is always a single item.
         */
        "dlc": (string)[];
        "use_tag": string;
        "kind_tag": string;
        "bme": (string | null);
        "bmee": (string | null);
        "cat": (string)[];
    };
    export type EnemyDrop = {
        "item_tag": string;
        "rate": types.U32;
        "num": types.U32;
        "quality_min": types.F32;
        "quality_max": types.F32;
        "potential_min": types.F32;
        "potential_max": types.F32;
        "potential_num_min": types.F32;
        "potential_num_max": types.F32;
        "potential_lv_min": types.F32;
        "potential_lv_max": types.F32;
        "quality_min_adj": types.F32;
        "quality_max_adj": types.F32;
        "potential_min_adj": types.F32;
        "potential_max_adj": types.F32;
        "potential_num_min_adj": types.U32;
        "potential_num_max_adj": types.U32;
        "potential_lv_min_adj": types.U32;
        "potential_lv_max_adj": types.U32;
        "super_pot_rate": types.U32;
        "factor": string;
        "eff": (string | null);
    };
    export type EnemyStatus = {
        "exp": types.U32;
        "money": types.U32;
        "exp_rosca": types.U32;
        "money_rosca": types.U32;
        "gold_coin": types.U32;
        "gold_coin_rate": types.U32;
        "drop_tag": string;
        "skill_tag": string;
        "extra_skill_tag": string;
        "lv": types.U32;
        "stun": types.U32;
        "key_make": types.U32;
        "atk_num": types.U32;
        "burst_up": types.U32;
        "burst_max": types.U32;
        "hp": types.I32;
        "atk": types.I32;
        "def": types.I32;
        "spd": types.I32;
        "bad_resist": (types.U32)[];
        "resist_non": types.U32;
        "key_create_tag": string;
        "att": (string)[];

        /**
         * `sp_item_tag` from drop data
         */
        "sp_item_tag": string;
        "drops": (types.EnemyDrop)[];
    };
    export type Enemy = {
        "name": string;
        "library_note": (string | null);
        "is_big": boolean;
        "img_no": types.I32;
        "wait_action": boolean;
        "library_rank_health": types.U32;
        "library_rank_attack": types.U32;
        "library_rank_speed": types.U32;
        "library_rank_defense": types.U32;
        "dlc": (string)[];
        "shoot_up": boolean;
        "monster_tag": string;
        "chara_tag": string;
        "race_tag": string;
        "size": string;
        "division": string;
        "statusses": (types.EnemyStatus)[];
    };
    export type Ryza3Data = {
        "item_data": (types.Item)[];
        "enemy_data": (types.Enemy)[];
    };
}

Copy link
Owner

@dbeckwith dbeckwith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Thanks for the contribution. Definitely makes the emitted code more readable, and a pretty simple approach.

Please just fix the failing test and then I'll merge.

@holly-hacker
Copy link
Contributor Author

Ah apologies, I didn't see this test because I didn't run cargo test with all-features. I've pushed a fix for that test.

@dbeckwith
Copy link
Owner

Thanks! I'll push a new release with this change.

@dbeckwith dbeckwith closed this Aug 11, 2023
@dbeckwith dbeckwith reopened this Aug 11, 2023
@dbeckwith dbeckwith merged commit 394ec08 into dbeckwith:master Aug 11, 2023
@holly-hacker holly-hacker deleted the feature/pretty-printed-output branch August 11, 2023 23:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve formatting of output
2 participants