Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
tools: sort imports on pre-commit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Sep 17, 2024
1 parent d9f924c commit b95beda
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ repos:
entry: tools/additional_lint
language: python
stages: [commit]

- id: imports
name: imports
entry: tools/sort_imports
language: system
files: \.[ch](pp)?$
3 changes: 1 addition & 2 deletions include/libtrx/config/config_map.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "config_option.h"

#include "../enum_str.h"
#include "../utils.h"
#include "config_option.h"

#include <stdbool.h>
#include <stddef.h>
Expand Down
6 changes: 3 additions & 3 deletions include/libtrx/game/objects/common.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include "ids.h"
#include "../math.h"
#include "../items.h"
#include "../collision.h"
#include "../items.h"
#include "../math.h"
#include "ids.h"

#include <stdint.h>

Expand Down
2 changes: 1 addition & 1 deletion include/libtrx/gfx/2d/2d_renderer.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#pragma once

#include "2d_surface.h"
#include "../gl/buffer.h"
#include "../gl/program.h"
#include "../gl/sampler.h"
#include "../gl/texture.h"
#include "../gl/vertex_array.h"
#include "2d_surface.h"

#include <stdbool.h>
#include <stdint.h>
Expand Down
3 changes: 1 addition & 2 deletions include/libtrx/gfx/2d/2d_surface.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#pragma once

#include "../gl/gl_core_3_3.h"

#include "../../engine/image.h"
#include "../gl/gl_core_3_3.h"

#include <stdbool.h>
#include <stdint.h>
Expand Down
2 changes: 1 addition & 1 deletion include/libtrx/gfx/3d/3d_renderer.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once

#include "vertex_stream.h"
#include "../common.h"
#include "../config.h"
#include "../gl/gl_core_3_3.h"
#include "../gl/program.h"
#include "../gl/sampler.h"
#include "../gl/texture.h"
#include "vertex_stream.h"

#define GFX_MAX_TEXTURES 128
#define GFX_NO_TEXTURE (-1)
Expand Down
3 changes: 1 addition & 2 deletions include/libtrx/gfx/gl/utils.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#pragma once

#include "../gl/gl_core_3_3.h"

#include "../../log.h"
#include "../gl/gl_core_3_3.h"

#define GFX_GL_CheckError() \
{ \
Expand Down
2 changes: 1 addition & 1 deletion include/libtrx/virtual_file.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <stdint.h>
#include <stddef.h>
#include <stdint.h>

typedef struct {
char *content;
Expand Down
15 changes: 14 additions & 1 deletion tools/libtrx/cli/sort_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,21 @@ def sort_imports(
own_include_map: dict[str, str],
fix_map: dict[str, str],
forced_order: list[str],
include_dirs: list[Path],
) -> None:
source = path.read_text()
rel_path = path.relative_to(root_dir)
try:
rel_path = path.relative_to(root_dir)
except ValueError:
matches = []
for include_dir in include_dirs:
try:
rel_path = path.relative_to(include_dir)
except ValueError:
pass
matches.append(rel_path)
rel_path = sorted(matches, key=lambda path: len(str(path)))[0]

own_include = str(rel_path.with_suffix(".h"))
own_include = own_include_map.get(str(rel_path), own_include)

Expand Down Expand Up @@ -106,4 +118,5 @@ def run_script(
own_include_map=own_include_map,
fix_map=fix_map,
forced_order=forced_order,
include_dirs=include_dirs,
)

0 comments on commit b95beda

Please sign in to comment.