summaryrefslogtreecommitdiffstats
path: root/modules/plugins/debugger/nvim-dap/config.nix
blob: 03ff18cb1165ddf69ae2bb0d056269f2a9e4270b (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
{
  options,
  config,
  lib,
  ...
}: let
  inherit (builtins) concatStringsSep filter;
  inherit (lib.strings) optionalString;
  inherit (lib.modules) mkIf mkMerge;
  inherit (lib.attrsets) mapAttrs mapAttrsToList;
  inherit (lib.nvim.binds) mkKeymap;
  inherit (lib.nvim.dag) entryAnywhere entryAfter;
  inherit (lib.nvim.lua) toLuaObject isLuaInline;

  cfg = config.vim.debugger.nvim-dap;
  opt = {
    silent = true;
    lua = true;
  };
  inherit (options.vim.debugger.nvim-dap) mappings;

  checkAdapter = name: adapter:
    if isLuaInline adapter
    then null
    else if adapter.type == "executable"
    then {
      assertion = adapter.command != null;
      message = ''
        `vim.debugger.nvim-dap.adapters.${name}.command` must be set if type="executable"
      '';
    }
    else if adapter.type == "server"
    then {
      assertion = adapter.port != null;
      message = ''
        `vim.debugger.nvim-dap.adapters.${name}.port` must be set if type="server"
      '';
    }
    # type = "pipe"
    else {
      assertion = adapter.pipe != null;
      message = ''
        `vim.debugger.nvim-dap.adapters.${name}.pipe` must be set if type="pipe"
      '';
    };
in {
  config = mkMerge [
    (mkIf cfg.enable {
      vim = {
        startPlugins = ["nvim-dap"];

        pluginRC =
          {
            # TODO customizable keymaps
            nvim-dap = entryAnywhere ''
              local dap = require("dap")
              vim.fn.sign_define("DapBreakpoint", { text = "🛑", texthl = "ErrorMsg", linehl = "", numhl = "" })

              local nvf_dap_input_cache= {}
              local function nvf_dap_cached_input(cache_key, prompt, default, completion)
                default = nvf_dap_input_cache[cache_key] or default
                nvf_dap_input_cache[cache_key] =
                  vim.fn.input(prompt, default, completion)
                return nvf_dap_input_cache[cache_key]
              end

              ${
                concatStringsSep "\n"
                (mapAttrsToList (name: opts: ''
                    dap.adapters[${toLuaObject name}] = ${toLuaObject opts}
                  '')
                  cfg.adapters)
              }

              ${
                concatStringsSep "\n"
                (mapAttrsToList (ft: opts: ''
                    dap.configurations[${toLuaObject ft}] = ${toLuaObject opts}
                  '')
                  cfg.configurations)
              }
            '';
          }
          // mapAttrs (_: v: (entryAfter ["nvim-dap"] v)) cfg.sources;

        keymaps = [
          (mkKeymap "n" cfg.mappings.continue "require('dap').continue" (opt // {desc = mappings.continue.description;}))
          (mkKeymap "n" cfg.mappings.restart "require('dap').restart" (opt // {desc = mappings.restart.description;}))
          (mkKeymap "n" cfg.mappings.terminate "require('dap').terminate" (opt // {desc = mappings.terminate.description;}))
          (mkKeymap "n" cfg.mappings.runLast "require('dap').run_last" (opt // {desc = mappings.runLast.description;}))

          (mkKeymap "n" cfg.mappings.toggleRepl "require('dap').repl.toggle" (opt // {desc = mappings.toggleRepl.description;}))
          (mkKeymap "n" cfg.mappings.hover "require('dap.ui.widgets').hover" (opt // {desc = mappings.hover.description;}))
          (mkKeymap "n" cfg.mappings.toggleBreakpoint "require('dap').toggle_breakpoint" (opt // {desc = mappings.toggleBreakpoint.description;}))

          (mkKeymap "n" cfg.mappings.runToCursor "require('dap').run_to_cursor" (opt // {desc = mappings.runToCursor.description;}))
          (mkKeymap "n" cfg.mappings.stepInto "require('dap').step_into" (opt // {desc = mappings.stepInto.description;}))
          (mkKeymap "n" cfg.mappings.stepOut "require('dap').step_out" (opt // {desc = mappings.stepOut.description;}))
          (mkKeymap "n" cfg.mappings.stepOver "require('dap').step_over" (opt // {desc = mappings.stepOver.description;}))
          (mkKeymap "n" cfg.mappings.stepBack "require('dap').step_back" (opt // {desc = mappings.stepBack.description;}))

          (mkKeymap "n" cfg.mappings.goUp "require('dap').up" (opt // {desc = mappings.goUp.description;}))
          (mkKeymap "n" cfg.mappings.goDown "require('dap').down" (opt // {desc = mappings.goDown.description;}))
        ];
      };

      assertions = filter (x: x != null) (mapAttrsToList checkAdapter cfg.adapters);
    })
    (mkIf (cfg.enable && cfg.ui.enable) {
      vim = {
        startPlugins = ["nvim-nio"];

        lazy.plugins.nvim-dap-ui = {
          package = "nvim-dap-ui";
          setupModule = "dapui";
          inherit (cfg.ui) setupOpts;

          keys = [
            (mkKeymap "n" cfg.mappings.toggleDapUI "function() require('dapui').toggle() end" (opt // {desc = mappings.toggleDapUI.description;}))
          ];
        };

        pluginRC.nvim-dap-ui = entryAfter ["nvim-dap"] (
          optionalString cfg.ui.autoStart ''
            dap.listeners.after.event_initialized["dapui_config"] = function()
              require("dapui").open()
            end
            dap.listeners.before.event_terminated["dapui_config"] = function()
              require("dapui").close()
            end
            dap.listeners.before.event_exited["dapui_config"] = function()
              require("dapui").close()
            end
          ''
        );
      };
    })
  ];
}