summaryrefslogtreecommitdiffstats
path: root/modules/plugins/languages/http.nix
blob: 8d6cc11ff3f6ffa7540b75b328d1b4093df6dba2 (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
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
158
159
160
{
  config,
  lib,
  pkgs,
  options,
  ...
}: let
  inherit (builtins) attrNames;
  inherit (lib.options) literalExpression mkEnableOption mkOption;
  inherit (lib.modules) mkIf mkMerge;
  inherit (lib.meta) getExe;
  inherit (lib.types) enum listOf nullOr str bool;
  inherit (lib.nvim.attrsets) mapListToAttrs;
  inherit (lib.nvim.types) mkGrammarOption mkPluginSetupOption;
  inherit (config.vim.lib) mkMappingOption;
  inherit (lib.nvim.binds) mkKeymap;

  cfg = config.vim.languages.http;

  defaultFormat = ["kulala-fmt"];
  formats = {
    kulala-fmt = {
      command = getExe pkgs.kulala-fmt;
    };
  };
in {
  options.vim.languages.http = {
    enable = mkEnableOption "HTTP request file support";

    treesitter = {
      enable =
        mkEnableOption "HTTP request file treesitter"
        // {
          default = config.vim.languages.enableTreesitter;
          defaultText = literalExpression "config.vim.languages.enableTreesitter";
        };
      package = mkGrammarOption pkgs "http";
    };

    format = {
      enable =
        mkEnableOption "HTTP request file formatting"
        // {
          default = config.vim.languages.enableFormat;
          defaultText = literalExpression "config.vim.languages.enableFormat";
        };

      type = mkOption {
        description = "HTTP request file formatter to use";
        type = listOf (enum (attrNames formats));
        default = defaultFormat;
      };
    };

    extensions = {
      kulala-nvim = {
        enable = mkEnableOption ''
          A fully-featured ⚡️ HTTP/GraphQL/gRPC/Websocket-client 🐼 interface 🖥️ for Neovim ❤️,
          that supports the Jetbrains .http spec (with full scripting support).
        '';
        setupOpts = mkPluginSetupOption "kulala.nvim" {
          kulala_core = {
            path = mkOption {
              type = nullOr str;
              default = getExe pkgs.kulala-core;
              defaultText = literalExpression "getExe pkgs.kulala-fmt";
              description = ''
                Optional path to the [kulala-core executable](https://github.com/mistweaverco/kulala-core).
                When set, this path is used exclusively.
                When null (default), auto-download and
                use kulala-core from GitHub releases based on the user's OS and architecture.
              '';
            };
            download_tool = mkOption {
              type = str;
              default = "${pkgs.curlMinimal}/bin/curl";
              defaultText = literalExpression "$${pkgs.curlMinimal}/bin/curl";
              description = "`curl` or `wget` or full path to `curl` or `wget` executable.";
            };
          };
          lsp = {
            enable = mkOption {
              type = bool;
              default = true;
              description = "Enable plugin managed LSP";
            };
          };
        };

        mappings = {
          openScratchpad = mkMappingOption "Open scratchpad" "<leader>Rb";
          sendRequest = mkMappingOption "Send request" "<leader>Rs";
          sendAllRequests = mkMappingOption "Send all requests" "<leader>Ra";
          replayRequest = mkMappingOption "Replay the last request" "<leader>Rr";
        };
      };
    };
  };

  config = mkIf cfg.enable (mkMerge [
    (mkIf cfg.treesitter.enable {
      vim.treesitter = {
        enable = true;
        grammars = [cfg.treesitter.package];
      };
    })

    (mkIf cfg.format.enable {
      vim.formatter.conform-nvim = {
        enable = true;
        setupOpts = {
          formatters_by_ft.http = cfg.format.type;
          formatters =
            mapListToAttrs (name: {
              inherit name;
              value = formats.${name};
            })
            cfg.format.type;
        };
      };
    })

    (mkIf cfg.extensions.kulala-nvim.enable {
      vim.lazy.plugins.kulala-nvim = let
        extCfg = cfg.extensions.kulala-nvim;
        extOpts = options.vim.languages.http.extensions.kulala-nvim;
      in {
        package = "kulala-nvim";
        ft = ["http" "rest"];
        event = ["SessionLoadPost" "VimLeavePre"];
        setupModule = "kulala";
        setupOpts = {treesitter.enable = false;} // cfg.extensions.kulala-nvim.setupOpts;

        keys = let
          mkAction = action: "function() require('kulala').${action}() end";
        in [
          (mkKeymap ["n"] extCfg.mappings.openScratchpad (mkAction "scratchpad") {
            lua = true;
            desc = extOpts.mappings.openScratchpad.description;
          })
          (mkKeymap ["n" "v"] extCfg.mappings.sendRequest (mkAction "run") {
            lua = true;
            ft = ["http" "rest"];
            desc = extOpts.mappings.sendRequest.description;
          })
          (mkKeymap ["n" "v"] extCfg.mappings.sendAllRequests (mkAction "run_all") {
            lua = true;
            ft = ["http" "rest"];
            desc = extOpts.mappings.sendAllRequests.description;
          })
          (mkKeymap ["n" "v"] extCfg.mappings.replayRequest (mkAction "replay") {
            lua = true;
            ft = ["http" "rest"];
            desc = extOpts.mappings.replayRequest.description;
          })
        ];
      };
    })
  ]);
}