summaryrefslogtreecommitdiffstats
path: root/modules/plugins/utility/csvview/config.nix
blob: 2fcb2866a964ce052fa9c367e19aeefeecd88264 (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
{
  options,
  config,
  lib,
  ...
}: let
  inherit (lib.generators) mkLuaInline;
  inherit (lib.modules) mkIf;
  inherit (lib.nvim.binds) mkKeymap;

  cfg = config.vim.utility.csvview;

  keys = cfg.mappings;
  inherit (options.vim.utility.csvview) mappings;
in {
  config = mkIf cfg.enable {
    vim = {
      lazy.plugins.csvview-nvim = {
        package = "csvview-nvim";
        setupModule = "csvview";
        inherit (cfg) setupOpts;

        cmd = ["CsvViewEnable" "CsvViewDisable" "CsvViewToggle" "CsvViewInfo"];

        keys = [
          (mkKeymap "n" keys.toggle "<cmd>CsvViewToggle<CR>" {desc = mappings.toggle.description;})
        ];
      };

      # csvview.nvim has no built-in auto-enable, so drive it with a FileType
      # autocommand. The `is_enabled` guard keeps this idempotent so
      # re-firing FileType (e.g. `:e!`, `:set ft=csv`) doesn't warn about an
      # already-attached buffer.
      autocmds = mkIf cfg.autoEnable [
        {
          event = ["FileType"];
          pattern = ["csv" "tsv"];
          callback = mkLuaInline ''
            function(args)
              if not require("csvview").is_enabled(args.buf) then
                require("csvview").enable()
              end
            end
          '';
          desc = "Automatically enable CSV view for CSV/TSV files";
        }
      ];
    };
  };
}