summaryrefslogtreecommitdiffstats
path: root/modules/neovim/init/basic.nix
blob: 85c07a530f8d844f1b49d37363d5645b1092858e (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
161
162
163
164
{
  config,
  lib,
  ...
}: let
  inherit (lib.options) mkOption mkEnableOption literalMD;
  inherit (lib.strings) optionalString;
  inherit (lib.attrsets) optionalAttrs;
  inherit (lib.types) enum bool str either;
  inherit (lib.generators) mkLuaInline;
  inherit (lib.nvim.dag) entryAfter;
  inherit (lib.nvim.binds) pushDownDefault;
  inherit (lib.nvim.types) luaInline;

  cfg = config.vim;
in {
  options.vim = {
    hideSearchHighlight = mkOption {
      type = bool;
      default = false;
      description = "Hide search highlight so it doesn't stay highlighted";
    };

    syntaxHighlighting = mkOption {
      type = bool;
      default = !config.vim.treesitter.highlight.enable;
      description = "Enable syntax highlighting";
    };

    lineNumberMode = mkOption {
      type = enum ["relative" "number" "relNumber" "none"];
      default = "relNumber";
      example = "none";
      description = "How line numbers are displayed.";
    };

    preventJunkFiles = mkOption {
      type = bool;
      default = true;
      example = false;
      description = ''
        Prevent swapfile and backupfile from being created.

        `false` is the default Neovim behaviour. If you wish to create
        backup and swapfiles, set this option to `false`.
      '';
    };

    bell = mkOption {
      type = enum ["none" "visual" "on"];
      default = "none";
      description = "Set how bells are handled. Options: on, visual or none";
    };

    searchCase = mkOption {
      type = enum ["ignore" "smart" "sensitive"];
      default = "sensitive";
      description = "Set the case sensitivity of search";
    };

    undoFile = {
      enable = mkEnableOption "undofile for persistent undo behaviour";
      path = mkOption {
        type = either str luaInline;
        default = mkLuaInline "vim.fn.stdpath('state') .. '/undo'";
        defaultText = literalMD ''
          ```nix
          mkLuaInline "vim.fn.stdpath('state') .. '/undo'"
          ```
        '';
        example = literalMD ''
          ```nix
          mkLuaInline "os.getenv('XDG_DATA_HOME') .. '/nvf/undo'"
          ```
        '';
        description = "Path to the directory in which undo history will be stored";
      };
    };
  };

  # Alias vim.options as vim.opts.
  # This is a convenience for people using frameworks like flake-parts or Den that use lib.types.deferredModule
  # and users would set `vim.options` but error when Nix confuses it with Nix Module's options-definitions.
  imports = [(lib.mkAliasOptionModule ["vim" "opts"] ["vim" "options"])];

  config.vim = {
    # Set options that were previously interpolated in 'luaConfigRC.basic' as vim.options (vim.o)
    # and 'vim.globals' (vim.g). Future options, if possible, should be added here instead of the
    # luaConfigRC section below.
    options = pushDownDefault (lib.mergeAttrsList [
      {
        # Options that are always set, with a lower priority
        encoding = "utf-8";
        hidden = true;
        expandtab = true;

        # Junkfile Behaviour
        swapfile = !cfg.preventJunkFiles;
        backup = !cfg.preventJunkFiles;
        writebackup = !cfg.preventJunkFiles;
      }

      (optionalAttrs cfg.undoFile.enable {
        undofile = true;
        undodir = cfg.undoFile.path;
      })

      (optionalAttrs (cfg.bell == "none") {
        errorbells = false;
        visualbell = false;
      })

      (optionalAttrs (cfg.bell == "on") {
        visualbell = false;
      })

      (optionalAttrs (cfg.bell == "visual") {
        visualbell = false;
      })

      (optionalAttrs (cfg.lineNumberMode == "relative") {
        relativenumber = true;
      })

      (optionalAttrs (cfg.lineNumberMode == "number") {
        number = true;
      })

      (optionalAttrs (cfg.lineNumberMode == "relNumber") {
        number = true;
        relativenumber = true;
      })
    ]);

    # Options that are more difficult to set through 'vim.options'. Namely, appending values
    # to pre-set Neovim options. Fear not, though as the Lua DAG is still as powerful as it
    # could be.
    luaConfigRC.basic = entryAfter ["globalsScript"] ''
      ${optionalString cfg.syntaxHighlighting ''
        vim.cmd("syntax on")
      ''}

      ${optionalString cfg.hideSearchHighlight ''
        vim.o.hlsearch = false
        vim.o.incsearch = true
      ''}

      ${optionalString (cfg.searchCase == "ignore") ''
        vim.o.smartcase = false
        vim.o.ignorecase = true
      ''}

      ${optionalString (cfg.searchCase == "smart") ''
        vim.o.smartcase = true
        vim.o.ignorecase = true
      ''}

      ${optionalString (cfg.searchCase == "sensitive") ''
        vim.o.smartcase = false
        vim.o.ignorecase = false
      ''}
    '';
  };
}