summaryrefslogtreecommitdiffstats
path: root/modules/plugins/languages/typst.nix
blob: 7ca6649f51b557b29a84dcc00bfe6868b767920e (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
{
  config,
  pkgs,
  lib,
  ...
}: let
  inherit (lib.options) mkOption mkEnableOption literalExpression;
  inherit (lib.modules) mkIf mkMerge;
  inherit (lib.types) nullOr enum attrsOf listOf str bool int;
  inherit (lib) genAttrs;
  inherit (lib.meta) getExe;
  inherit (lib.nvim.types) mkGrammarOption mkPluginSetupOption;
  inherit (lib.nvim.binds) mkKeymap;
  inherit (config.vim.lib) mkMappingOption;

  cfg = config.vim.languages.typst;

  defaultServers = ["tinymist"];
  servers = ["tinymist"];

  defaultFormat = ["typstyle"];
  formats = ["typstyle" "injected"];
in {
  options.vim.languages.typst = {
    enable = mkEnableOption "Typst language support";

    treesitter = {
      enable =
        mkEnableOption "Typst treesitter"
        // {
          default = config.vim.languages.enableTreesitter;
          defaultText = literalExpression "config.vim.languages.enableTreesitter";
        };
      package = mkGrammarOption pkgs "typst";
    };

    lsp = {
      enable =
        mkEnableOption "Typst LSP support (typst-lsp)"
        // {
          default = config.vim.lsp.enable;
          defaultText = literalExpression "config.vim.lsp.enable";
        };

      servers = mkOption {
        type = listOf (enum servers);
        default = defaultServers;
        description = "Typst LSP server to use";
      };
    };

    format = {
      enable =
        mkEnableOption "Typst document formatting"
        // {
          default = config.vim.languages.enableFormat;
          defaultText = literalExpression "config.vim.languages.enableFormat";
        };

      type = mkOption {
        type = listOf (enum formats);
        default = defaultFormat;
        description = "Typst formatter to use";
      };
    };

    extensions = {
      typst-preview-nvim = {
        enable =
          mkEnableOption ''
            [typst-preview.nvim]: https://github.com/chomosuke/typst-preview.nvim

            Low latency typst preview for Neovim via [typst-preview.nvim]
          ''
          // {default = true;};

        setupOpts = mkPluginSetupOption "typst-preview-nvim" {
          open_cmd = mkOption {
            type = nullOr str;
            default = null;
            example = "firefox %s -P typst-preview --class typst-preview";
            description = ''
              Custom format string to open the output link provided with `%s`
            '';
          };

          dependencies_bin = mkOption {
            type = attrsOf str;
            default = {
              "tinymist" = getExe pkgs.tinymist;
              "websocat" = getExe pkgs.websocat;
            };

            description = ''
              Provide the path to binaries for dependencies. Setting this
              to a non-null value will skip the download of the binary by
              the plugin.
            '';
          };

          extra_args = mkOption {
            type = nullOr (listOf str);
            default = null;
            example = ["--input=ver=draft" "--ignore-system-fonts"];
            description = "A list of extra arguments (or `null`) to be passed to previewer";
          };
        };
      };
      typst-concealer = {
        enable = mkEnableOption ''
          [typst-concealer]: https://github.com/PartyWumpus/typst-concealer

          Inline typst preview for Neovim via [typst-concealer]
        '';

        mappings = {
          toggleConcealing = mkMappingOption "Enable typst-concealer in buffer" "<leader>TT";
        };

        setupOpts = mkPluginSetupOption "typst-concealer" {
          do_diagnostics = mkOption {
            type = nullOr bool;
            default = !cfg.lsp.enable;
            description = "Should typst-concealer provide diagnostics on error?";
          };
          color = mkOption {
            type = nullOr str;
            default = null;
            example = "rgb(\"#f012be\")";
            description = "What color should typst-concealer render text/stroke with? (only applies when styling_type is 'colorscheme')";
          };
          enabled_by_default = mkOption {
            type = nullOr bool;
            default = null;
            description = "Should typst-concealer conceal newly opened buffers by default?";
          };
          styling_type = mkOption {
            type = nullOr (enum ["simple" "none" "colorscheme"]);
            default = null;
            description = "What kind of styling should typst-concealer apply to your typst?";
          };
          ppi = mkOption {
            type = nullOr int;
            default = null;
            description = "What PPI should typst render at. Plugin default is 300, typst's normal default is 144.";
          };
          typst_location = mkOption {
            type = str;
            default = getExe pkgs.typst;
            description = "Where should typst-concealer look for your typst binary?";
            example = ''lib.getExe pkgs.typst'';
          };
          conceal_in_normal = mkOption {
            type = nullOr bool;
            default = null;
            description = "Should typst-concealer still conceal when the normal mode cursor goes over a line.";
          };
        };
      };
    };
  };
  config = mkIf cfg.enable (mkMerge [
    (mkIf cfg.treesitter.enable {
      vim.treesitter.enable = true;
      vim.treesitter.grammars = [cfg.treesitter.package];
    })

    (mkIf cfg.format.enable {
      vim.formatter.conform-nvim = {
        enable = true;
        presets = genAttrs cfg.format.type (_: {enable = true;});
        setupOpts.formatters_by_ft.typst = cfg.format.type;
      };
    })

    (mkIf cfg.lsp.enable {
      vim.lsp = {
        presets = genAttrs cfg.lsp.servers (_: {enable = true;});
        servers = genAttrs cfg.lsp.servers (_: {
          filetypes = ["typst"];
        });
      };
    })

    # Extensions
    (mkIf cfg.extensions.typst-preview-nvim.enable {
      vim.lazy.plugins.typst-preview-nvim = {
        package = "typst-preview-nvim";
        setupModule = "typst-preview";
        setupOpts = cfg.extensions.typst-preview-nvim.setupOpts;
        cmd = [
          "TypstPreviewUpdate"
          "TypstPreview"
          "TypstPreviewStop"
          "TypstPreviewToggle"
          "TypstPreviewFollowCursor"
          "TypstPreviewNoFollowCursor"
          "TypstPreviewFollowCursorToggle"
          "TypstPreviewSyncCursor"
        ];
      };
    })

    (mkIf cfg.extensions.typst-concealer.enable {
      vim.lazy.plugins.typst-concealer = {
        event = "BufRead *.typ";
        package = "typst-concealer";
        setupModule = "typst-concealer";
        setupOpts = cfg.extensions.typst-concealer.setupOpts;

        keys = [
          (mkKeymap "n" cfg.extensions.typst-concealer.mappings.toggleConcealing "<cmd>lua require('typst-concealer').toggle_buf()<CR>" {desc = "Toggle typst-concealer in buffer";})
        ];
      };
    })
  ]);
}