2024-04-28 21:07:41 +02:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
...
|
|
|
|
}: let
|
|
|
|
inherit (lib.options) mkEnableOption mkOption;
|
|
|
|
inherit (lib.modules) mkIf mkMerge;
|
|
|
|
inherit (lib.lists) isList;
|
2024-12-09 07:28:23 +01:00
|
|
|
inherit (lib.types) nullOr enum either attrsOf listOf package str;
|
2024-04-28 21:07:41 +02:00
|
|
|
inherit (lib.attrsets) attrNames;
|
2024-12-09 07:28:23 +01:00
|
|
|
inherit (lib.generators) mkLuaInline;
|
|
|
|
inherit (lib.meta) getExe;
|
|
|
|
inherit (lib.nvim.lua) expToLua toLuaObject;
|
|
|
|
inherit (lib.nvim.types) mkGrammarOption mkPluginSetupOption;
|
|
|
|
inherit (lib.nvim.dag) entryAnywhere;
|
2024-04-28 21:07:41 +02:00
|
|
|
|
|
|
|
cfg = config.vim.languages.typst;
|
|
|
|
|
2024-11-08 16:10:40 +01:00
|
|
|
defaultServer = "tinymist";
|
|
|
|
servers = {
|
|
|
|
typst-lsp = {
|
|
|
|
package = pkgs.typst-lsp;
|
|
|
|
lspConfig = ''
|
|
|
|
lspconfig.typst_lsp.setup {
|
|
|
|
capabilities = capabilities,
|
2024-11-26 04:49:57 +01:00
|
|
|
on_attach = function(client, bufnr)
|
2024-11-26 05:33:54 +01:00
|
|
|
-- Disable semantic tokens as a workaround for a semantic token error when using non-english characters
|
2024-11-26 04:49:57 +01:00
|
|
|
client.server_capabilities.semanticTokensProvider = nil
|
|
|
|
end,
|
2024-11-08 16:10:40 +01:00
|
|
|
cmd = ${
|
|
|
|
if isList cfg.lsp.package
|
|
|
|
then expToLua cfg.lsp.package
|
|
|
|
else ''{"${cfg.lsp.package}/bin/typst-lsp"}''
|
|
|
|
},
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
2024-12-09 07:28:23 +01:00
|
|
|
|
2024-11-08 16:10:40 +01:00
|
|
|
tinymist = {
|
|
|
|
package = pkgs.tinymist;
|
|
|
|
lspConfig = ''
|
|
|
|
lspconfig.tinymist.setup {
|
|
|
|
capabilities = capabilities,
|
2024-11-18 09:49:20 +01:00
|
|
|
single_file_support = true,
|
2024-11-26 04:49:57 +01:00
|
|
|
on_attach = function(client, bufnr)
|
2024-11-26 05:33:54 +01:00
|
|
|
-- Disable semantic tokens as a workaround for a semantic token error when using non-english characters
|
2024-11-26 04:49:57 +01:00
|
|
|
client.server_capabilities.semanticTokensProvider = nil
|
|
|
|
end,
|
2024-11-08 16:10:40 +01:00
|
|
|
cmd = ${
|
|
|
|
if isList cfg.lsp.package
|
|
|
|
then expToLua cfg.lsp.package
|
|
|
|
else ''{"${cfg.lsp.package}/bin/tinymist"}''
|
|
|
|
},
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-04-28 21:07:41 +02:00
|
|
|
defaultFormat = "typstfmt";
|
|
|
|
formats = {
|
|
|
|
typstfmt = {
|
|
|
|
package = pkgs.typstfmt;
|
|
|
|
nullConfig = ''
|
|
|
|
table.insert(
|
|
|
|
ls_sources,
|
|
|
|
null_ls.builtins.formatting.typstfmt.with({
|
|
|
|
command = "${cfg.format.package}/bin/typstfmt",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
# https://github.com/Enter-tainer/typstyle
|
|
|
|
typstyle = {
|
|
|
|
package = pkgs.typstyle;
|
|
|
|
nullConfig = ''
|
|
|
|
table.insert(
|
|
|
|
ls_sources,
|
|
|
|
null_ls.builtins.formatting.typstfmt.with({
|
|
|
|
command = "${cfg.format.package}/bin/typstyle",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
options.vim.languages.typst = {
|
|
|
|
enable = mkEnableOption "Typst language support";
|
|
|
|
|
|
|
|
treesitter = {
|
|
|
|
enable = mkEnableOption "Typst treesitter" // {default = config.vim.languages.enableTreesitter;};
|
|
|
|
package = mkGrammarOption pkgs "typst";
|
|
|
|
};
|
|
|
|
|
|
|
|
lsp = {
|
|
|
|
enable = mkEnableOption "Typst LSP support (typst-lsp)" // {default = config.vim.languages.enableLSP;};
|
|
|
|
|
2024-11-08 16:10:40 +01:00
|
|
|
server = mkOption {
|
|
|
|
description = "Typst LSP server to use";
|
|
|
|
type = enum (attrNames servers);
|
|
|
|
default = defaultServer;
|
|
|
|
};
|
|
|
|
|
2024-04-28 21:07:41 +02:00
|
|
|
package = mkOption {
|
|
|
|
description = "typst-lsp package, or the command to run as a list of strings";
|
|
|
|
example = ''[lib.getExe pkgs.jdt-language-server "-data" "~/.cache/jdtls/workspace"]'';
|
|
|
|
type = either package (listOf str);
|
2024-11-08 16:10:40 +01:00
|
|
|
default = servers.${cfg.lsp.server}.package;
|
2024-04-28 21:07:41 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
format = {
|
|
|
|
enable = mkEnableOption "Typst document formatting" // {default = config.vim.languages.enableFormat;};
|
|
|
|
|
|
|
|
type = mkOption {
|
|
|
|
description = "Typst formatter to use";
|
|
|
|
type = enum (attrNames formats);
|
|
|
|
default = defaultFormat;
|
|
|
|
};
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
description = "Typst formatter package";
|
|
|
|
type = package;
|
|
|
|
default = formats.${cfg.format.type}.package;
|
|
|
|
};
|
|
|
|
};
|
2024-12-09 07:28:23 +01:00
|
|
|
|
|
|
|
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 servers.tinymist.package;
|
|
|
|
"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";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2024-04-28 21:07:41 +02:00
|
|
|
};
|
|
|
|
config = mkIf cfg.enable (mkMerge [
|
|
|
|
(mkIf cfg.treesitter.enable {
|
|
|
|
vim.treesitter.enable = true;
|
|
|
|
vim.treesitter.grammars = [cfg.treesitter.package];
|
|
|
|
})
|
|
|
|
|
2024-11-08 16:10:40 +01:00
|
|
|
(mkIf cfg.format.enable {
|
|
|
|
vim.lsp.null-ls.enable = true;
|
|
|
|
vim.lsp.null-ls.sources.typst-format = formats.${cfg.format.type}.nullConfig;
|
|
|
|
})
|
|
|
|
|
2024-04-28 21:07:41 +02:00
|
|
|
(mkIf cfg.lsp.enable {
|
|
|
|
vim.lsp.lspconfig.enable = true;
|
2024-11-08 16:10:40 +01:00
|
|
|
vim.lsp.lspconfig.sources.typst-lsp = servers.${cfg.lsp.server}.lspConfig;
|
2024-04-28 21:07:41 +02:00
|
|
|
})
|
2024-12-09 07:28:23 +01:00
|
|
|
|
|
|
|
# Extensions
|
|
|
|
(mkIf cfg.extensions.typst-preview-nvim.enable {
|
|
|
|
vim.startPlugins = ["typst-preview-nvim"];
|
|
|
|
vim.pluginRC.typst-preview-nvim = entryAnywhere ''
|
|
|
|
require("typst-preview").setup(${toLuaObject cfg.extensions.typst-preview-nvim.setupOpts})
|
|
|
|
'';
|
|
|
|
})
|
2024-04-28 21:07:41 +02:00
|
|
|
]);
|
|
|
|
}
|