aboutsummaryrefslogtreecommitdiffhomepage
path: root/nix/formatter.nix
blob: 66721c2c83141be5a321ceff01b0f609c523f872 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{
  writeShellApplication,
  deadnix,
  statix,
  alejandra,
  llvmPackages_19,
  fd,
}:
writeShellApplication {
  name = "hyprland-treewide-formatter";
  runtimeInputs = [
    deadnix
    statix
    alejandra
    llvmPackages_19.clang-tools
    fd
  ];
  text = ''
    # shellcheck disable=SC2148

    # common excludes
    excludes="subprojects"

    nix_format() {
      if [ "$*" = 0 ]; then
        fd '.*\.nix' . -E "$excludes" -x statix fix -- {} \;
        fd '.*\.nix' . -E "$excludes" -X deadnix -e -- {} \; -X alejandra {} \;
      elif [ -d "$1" ]; then
        fd '.*\.nix' "$1" -E "$excludes" -i -x statix fix -- {} \;
        fd '.*\.nix' "$1" -E "$excludes" -i -X deadnix -e -- {} \; -X alejandra {} \;
      else
        statix fix -- "$1"
        deadnix -e "$1"
        alejandra "$1"
      fi
    }

    cpp_format() {
      if [ "$*" = 0 ] || [ "$1" = "." ]; then
        fd '.*\.cpp' . -E "$excludes"  | xargs clang-format --verbose -i
      elif [ -d "$1" ]; then
        fd '.*\.cpp' "$1" -E "$excludes" | xargs clang-format --verbose -i
      else
        clang-format --verbose -i "$1"
      fi
    }

    for i in "$@"; do
      case ''${i##*.} in
        "nix")
          nix_format "$i"
          ;;
        "cpp")
          cpp_format "$i"
          ;;
        *)
          nix_format "$i"
          cpp_format "$i"
          ;;
      esac

    done
  '';
}