{
  config,
  lib,
  ...
}: {
  imports = [../telescope ../trouble];

  options.dotfiles.inputs.nixvim.programs.nixvim.plugins.todo-comments.enable =
    lib.dotfiles.mkEnableOption;

  config = let
    cfgNixvimPlugins = config.dotfiles.inputs.nixvim.programs.nixvim.plugins;
  in
    lib.mkIf cfgNixvimPlugins.todo-comments.enable {
      programs.nixvim = {
        keymaps = let
          requireTodocomments = "require('todo-comments')";
        in
          map
          (
            keymaps:
              {
                lua = true;
                mode = "n";
                options.silent = true;
              }
              // keymaps
          )
          [
            {
              action = "function() ${requireTodocomments}.jump_next() end";
              key = "]t";
            }

            {
              action = "function() ${requireTodocomments}.jump_prev() end";
              key = "[t";
            }
          ];

        plugins.todo-comments = {
          enable = true;

          keymaps = let
            leader = {
              telescope = "<leader>t";
              trouble = "<leader>l";
            };
          in
            # The
            #
            #     <ACTION> = lib.mkIf <CONDITION> { key = <VALUE>; };
            #
            # pattern is used instead of the simpler
            #
            #     <ACTION>.key = lib.mkIf <CONDITION> <VALUE>;
            #
            # expression because NixVim's assertion logic does not allow the key
            # attribute to be null.
            {
              todoTelescope = lib.mkIf cfgNixvimPlugins.telescope.enable {
                key = "${leader.telescope}t";
              };

              todoTrouble = lib.mkIf cfgNixvimPlugins.trouble.enable {
                key = "${leader.trouble}q";
              };
            };

          settings = {
            highlight = {
              comments_only = false;
              pattern = [''.*<(KEYWORDS)\s*:'' ''.*<(KEYWORDS)\s*!\(''];
            };

            keywords.TODO.alt = ["todo" "unimplemented"];
            search.pattern = ''\b(KEYWORDS)(:|!\()'';
          };
        };
      };
    };
}
