Skip to content

Commit 904c0d7

Browse files
authored
Typst (#81)
* Working on adding kernel and LSP for typst * Working on typst "kernel" for exporter * typst: add package searcher * typst: add test of tinymist diagnostics * ci: add typst * Working on typst packages Closes #80
1 parent b0c1b7b commit 904c0d7

File tree

11 files changed

+229
-2
lines changed

11 files changed

+229
-2
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ jobs:
120120
- name: rust
121121
- name: zsh
122122
- name: spellchecker
123+
- name: typst
123124

124125
- name: exporters
125126
- name: sample-environments

codedown.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ rec {
5757
};
5858

5959
kernels = everythingEnv.config.builtKernels;
60+
exporters = everythingEnv.config.builtExporters;
6061

6162
makeEnvironment = callPackage ./nix/makeEnvironment.nix {
6263
inherit pkgsStable pkgsMaster;

modules/exporters/typst/default.nix

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{ callPackage
2+
, lib
23
, pandoc
34
, symlinkJoin
45
, typst
@@ -7,7 +8,12 @@
78
, settingsSchema
89
}:
910

11+
with { inherit (settings) packages; };
12+
with { inherit (settings.interface) attrs extensions; };
13+
1014
let
15+
kernelName = "typst";
16+
1117
common = callPackage ../../kernels/common.nix {};
1218

1319
script = common.writeShellScriptBinWithAttrs {} "typst-export" ''
@@ -16,14 +22,27 @@ let
1622
echo_and_run ${typst}/bin/typst compile "$1" "$2"
1723
'';
1824

25+
typstToUse = typst.withPackages (ps: (map (x: ps.${x}) packages));
26+
27+
languageServers = lib.optionals settings.lsp.tinymist.enable
28+
[(callPackage ./language_server_tinymist { inherit kernelName typstToUse; })];
29+
30+
packageOptions = typst.packages;
31+
packageSearch = common.searcher packageOptions;
32+
1933
icon = ./typst.png;
2034
iconMonochrome = ./typst.svg;
2135

2236
in
2337

2438
symlinkJoin {
2539
name = "codedown-exporter-typst";
26-
paths = [script];
40+
paths = [
41+
(callPackage ./kernel.nix { inherit attrs extensions typstToUse; })
42+
script
43+
]
44+
++ languageServers
45+
;
2746

2847
passthru = {
2948
meta = {
@@ -46,12 +65,23 @@ symlinkJoin {
4665
input_extensions = ["typ"];
4766
pandoc = "${pandoc}/bin/pandoc";
4867
}];
68+
69+
hasPackages = packageOptions != {};
4970
};
5071

5172
versions = {
5273
typst = typst.version;
5374
};
5475

76+
inherit packageOptions packageSearch;
77+
5578
inherit settingsSchema settings;
79+
80+
modes = {
81+
inherit attrs extensions;
82+
code_mirror_mode = "typst";
83+
};
84+
85+
languageServerNames = map (x: x.languageServerName) languageServers;
5686
};
5787
}

modules/exporters/typst/kernel.nix

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{ lib
2+
, callPackage
3+
, typst
4+
, typstToUse # TODO: should this kernel actually do something?
5+
6+
, attrs
7+
, extensions
8+
}:
9+
10+
let
11+
common = callPackage ../../kernels/common.nix {};
12+
13+
in
14+
15+
common.makeJupyterKernel {
16+
bash = {
17+
displayName = "Typst";
18+
argv = [
19+
"echo"
20+
"hi"
21+
];
22+
language = lib.head attrs;
23+
logo32 = ./typst.png; # TODO: resize to proper size
24+
logo64 = ./typst.png; # TODO: resize to proper size
25+
metadata = {
26+
codedown = {
27+
inherit attrs extensions;
28+
language_version = typst.version;
29+
priority = 10;
30+
};
31+
};
32+
};
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{ lib
2+
, callPackage
3+
4+
, tinymist
5+
6+
# TODO: how to make the typstToUse (i.e. Typst with some packages) available to tinymist?
7+
, typstToUse
8+
9+
, kernelName
10+
}:
11+
12+
let
13+
common = callPackage ../../../kernels/common.nix {};
14+
15+
languageServerName = "tinymist";
16+
17+
passthru = {
18+
inherit languageServerName;
19+
};
20+
21+
in
22+
23+
common.writeTextDirWithMetaAndPassthru tinymist.meta passthru "lib/codedown/language-servers/typst-${kernelName}-tinymist-language-server.yaml" (lib.generators.toYAML {} [{
24+
name = languageServerName;
25+
version = tinymist.version;
26+
extensions = ["typ"];
27+
notebook_suffix = ".typ";
28+
attrs = ["typst"];
29+
type = "stream";
30+
primary = true;
31+
args = [
32+
"${tinymist}/bin/tinymist"
33+
];
34+
}])

modules/exporters/typst/module.nix

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1-
{ config, options, lib, nixosOptionsToSettingsSchema, ... }:
1+
{ config, options, lib, nixosOptionsToSettingsSchema, boilerplate, ... }:
22

33
with lib;
44

5+
let
6+
subPackage = types.submodule {
7+
options = {
8+
name = mkOption {
9+
description = "Package name";
10+
type = types.str;
11+
};
12+
outputs = mkOption {
13+
example = "Package outputs to include";
14+
type = types.listOf types.str;
15+
};
16+
};
17+
};
18+
19+
in
20+
521
{
622
options = {
723
exporters.typst = {
@@ -12,6 +28,32 @@ with lib;
1228
default = false;
1329
visible = false;
1430
};
31+
32+
packages = mkOption {
33+
example = "List of packages";
34+
type = types.listOf (types.either types.str subPackage);
35+
default = [];
36+
visible = false;
37+
};
38+
39+
interface.attrs = mkOption {
40+
example = boilerplate.attrsTitle;
41+
description = boilerplate.attrsDescription;
42+
type = types.listOf types.str;
43+
default = ["typst"];
44+
};
45+
interface.extensions = mkOption {
46+
example = boilerplate.extensionsTitle;
47+
description = boilerplate.extensionsDescription;
48+
type = types.listOf types.str;
49+
default = ["typ"];
50+
};
51+
52+
lsp.tinymist.enable = mkOption {
53+
example = "Enable tinymist language server";
54+
type = types.bool;
55+
default = true;
56+
};
1557
};
1658
};
1759

nix/makeEnvironment.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ symlinkJoin {
8282
paths =
8383
attrValues evaluated.config.builtKernels
8484
++ attrValues evaluated.config.builtLanguageServers
85+
++ attrValues evaluated.config.builtExporters
8586
++ lib.optionals (builtins.length exporters > 0) [(writeTextDir "lib/codedown/exporters.yaml" (lib.generators.toYAML {} exporters))]
8687
++ attrValues evaluated.config.packages
8788
++ lib.mapAttrsToList linkBinaries evaluated.config.extraBinDirs

sample_environments/exporters-typst.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ codedown.makeEnvironment {
66
name = "exporters-typst";
77

88
exporters.typst.enable = true;
9+
exporters.typst.packages = [
10+
"aero-check"
11+
];
912
}

tests/app/Spec/Tests/Typst.hs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{-# LANGUAGE RankNTypes #-}
2+
{-# OPTIONS_GHC -fno-warn-unused-top-binds #-}
3+
{-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-}
4+
5+
module Spec.Tests.Typst (tests) where
6+
7+
import Control.Lens hiding (List)
8+
import Control.Monad
9+
import Data.String.Interpolate
10+
import Data.Text as T
11+
import Language.LSP.Protocol.Lens hiding (diagnostics, hover, text)
12+
import Language.LSP.Protocol.Types
13+
import Language.LSP.Test hiding (message)
14+
import Test.Sandwich as Sandwich
15+
import TestLib.JupyterRunnerContext
16+
import TestLib.LSP
17+
import TestLib.NixEnvironmentContext
18+
import TestLib.NixTypes
19+
import TestLib.TestBuilding
20+
import TestLib.TestSearchers
21+
import TestLib.Types
22+
23+
24+
kernelName :: Text
25+
kernelName = "typst"
26+
27+
tests :: LanguageSpec
28+
tests = describe [i|Typst|] $ introduceNixEnvironment [] config [i|Typst|] $ introduceJupyterRunner $ do
29+
it "has expected fields" $ do
30+
testEval [i|exporters.typst.settingsSchema|]
31+
testEval [i|exporters.typst.modes|]
32+
testEval [i|exporters.typst.settings|]
33+
testEval [i|exporters.typst.args|]
34+
testEval [i|exporters.typst.meta|]
35+
36+
-- Used to view all versions in codedown-languages
37+
testEval [i|exporters.typst.versions|]
38+
39+
it "searcher builds" $ do
40+
void $ testBuild [i|exporters.typst.packageSearch|]
41+
42+
describe "LSP" $ do
43+
testDiagnosticsLabelDesired "simple" lsName "test.typ" (Just "typst")
44+
[__i|\#loremz(45)|]
45+
((== [(Range (Position 0 1) (Position 0 7), Nothing, "unknown variable: loremz")]) . getDiagnosticRanges')
46+
47+
48+
documentHighlightCode :: Text
49+
documentHighlightCode = [__i|foo = "hello"
50+
println(foo)|]
51+
52+
documentHighlightResults :: [DocumentHighlight]
53+
documentHighlightResults = [
54+
DocumentHighlight (Range (Position 0 0) (Position 0 3)) (Just DocumentHighlightKind_Write)
55+
, DocumentHighlight (Range (Position 1 8) (Position 1 11)) (Just DocumentHighlightKind_Read)
56+
]
57+
58+
lsName :: Text
59+
lsName = "tinymist"
60+
61+
-- kernelSpec :: NixKernelSpec
62+
-- kernelSpec = NixKernelSpec {
63+
-- nixKernelName = "typst"
64+
-- , nixKernelChannel = "codedown"
65+
-- , nixKernelDisplayName = Just [i|Typst|]
66+
-- , nixKernelPackages = []
67+
-- , nixKernelMeta = Nothing
68+
-- , nixKernelIcon = Nothing
69+
-- , nixKernelExtraConfig = Just [
70+
-- "lsp.tinymist.enable = true"
71+
-- ]
72+
-- }
73+
74+
config = [
75+
"exporters.typst.enable = true;"
76+
, "exporters.typst.lsp.tinymist.enable = true;"
77+
]
78+
79+
main :: IO ()
80+
main = jupyterMain tests

tests/src/TestLib/LSP.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ withLspSession' handleFn name filename codeToTest extraFiles session = do
256256
& set (workspace . _Just . configuration) Nothing
257257
& set (workspace . _Just . didChangeWatchedFiles . _Just . dynamicRegistration) (Just False)
258258
& set (workspace . _Just . didChangeConfiguration . _Just . dynamicRegistration) (Just False)
259+
& set (textDocument . _Just . semanticTokens . _Just . dynamicRegistration) (Just False)
259260

260261
handleFn $ runSessionWithConfigCustomProcess modifyCp sessionConfig cp caps homeDir (session homeDir)
261262

0 commit comments

Comments
 (0)