about summary refs log tree commit diff
path: root/pkgs/profpatsch/dhallsh/main.dhall
blob: f89f7ed1d13e06a3fa0c7b1a3e210f335c963d18 (plain) (blame)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
let Void = ./imports/Void.dhall

let Prelude = ./imports/Prelude.dhall

let Command = ./Command/type.dhall

let Argument = ./Argument/type.dhall

let FishComplete = ./Fish/Complete/type.dhall

let argCommandToList
    : Command Argument → List Text
    = ./Command/toList.dhall
      Argument
      (./Argument/toArgList.dhall ./OptionPrinter/newStyle.dhall)

let complete = ./Fish/Complete/default.dhall

let completeToCommand
    : FishComplete → Command Argument
    = ./Fish/Complete/toCommand.dhall ./OptionPrinter/newStyle.dhall

let Completion = ./Completion/package.dhall

let InSubcommand = < No | ToplevelSpecial | Subcommand : Text >

in  let fishSeenSubcommandFn = "__fish_seen_subcommand_from"

    let fishUseSubcommandFn = "__fish_use_subcommand"

    let fishCommandLineExactlyFn = "__fish_command_line_exactly"

    let subcommandCond =
            λ(programName : Text)
          → λ(inSubcommand : InSubcommand)
          → merge
            { ToplevelSpecial =
                Some
                { cmd =
                    fishCommandLineExactlyFn
                , args =
                    [ Argument.Plain programName ]
                }
            , Subcommand =
                  λ(sub : Text)
                → Some
                  { cmd = fishSeenSubcommandFn, args = [ Argument.Plain sub ] }
            , No =
                None (Command Argument)
            }
            inSubcommand
            : Optional (Command Argument)

    let optionsComplete =
            λ(programName : Text)
          → λ(inSubcommand : InSubcommand)
          → λ(options : List Completion.Option)
          → let optcompl =
                    λ(option : Completion.Option)
                  →   complete
                      { cmd = programName, description = option.description }
                    ⫽ { condition =
                          subcommandCond programName inSubcommand
                      , short-option =
                          option.short
                      , long-option =
                          Some option.long
                      }

            in  Prelude.List.map Completion.Option FishComplete optcompl options

    let mergeCommandArguments =
            λ(programName : Text)
          → λ(inSubcommand : InSubcommand)
          → λ(a : Type)
          → λ(f : List a → List FishComplete)
          → λ(arguments : Completion.Command.Arguments a)
          → let filesBlock =
                    complete { cmd = programName, description = "" }
                  ⫽ { condition =
                        subcommandCond programName inSubcommand
                    , no-files =
                        True
                    }

            in  merge
                { Subcommands =
                    λ(cmds : List a) → f cmds # [ filesBlock ]
                , Files =
                    [] : List FishComplete
                }
                arguments
                : List FishComplete

    let subcommandToFishComplete =
            λ(programName : Text)
          → λ(command : Completion.Command.Type Void)
          → let subcommandComplete =
                    [   complete
                        { cmd = programName, description = command.description }
                      ⫽ { condition =
                            Some
                            { cmd =
                                fishUseSubcommandFn
                            , args =
                                [] : List Argument
                            }
                        , argument =
                            Some command.name
                        , no-files =
                            False
                        }
                    ]
                  # optionsComplete
                    programName
                    (InSubcommand.Subcommand command.name)
                    command.options

            in    subcommandComplete
                # mergeCommandArguments
                  programName
                  (InSubcommand.Subcommand command.name)
                  Void
                  (λ(_ : List Void) → [] : List FishComplete)
                  command.arguments

    let simpleCommandToFishComplete =
            λ ( c
              : { command :
                    Completion.Command.Type (Completion.Command.Type Void)
                , toplevelCommandIsSubcommand :
                    Bool
                }
              )
          →   optionsComplete c.command.name InSubcommand.No c.command.options
            # mergeCommandArguments
              c.command.name
              (       if c.toplevelCommandIsSubcommand

                then  InSubcommand.ToplevelSpecial

                else  InSubcommand.No
              )
              (Completion.Command.Type Void)
              ( Prelude.List.concatMap
                (Completion.Command.Type Void)
                FishComplete
                (subcommandToFishComplete c.command.name)
              )
              c.command.arguments

    in    Prelude.List.map
          FishComplete
          (List Text)
          (λ(c : FishComplete) → argCommandToList (completeToCommand c))
          (simpleCommandToFishComplete ./Completion/completion.dhall)
        # [ [ "complete", "--do-complete=dhall " ] ]