From 0d9958caa60d9ce644949e016487e3e9c56e0e6b Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Thu, 9 Jul 2026 17:23:56 +0200 Subject: [PATCH] feat: initial commit --- .envrc | 7 ++++ .gitignore | 52 +++++++++++++++++++++++ AGENTS.md | 103 +++++++++++++++++++++++++++++++++++++++++++++ Eask | 21 ++++++++++ flake.lock | 61 +++++++++++++++++++++++++++ flake.nix | 26 ++++++++++++ lsp-typst.el | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 385 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 AGENTS.md create mode 100644 Eask create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 lsp-typst.el diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..752574d --- /dev/null +++ b/.envrc @@ -0,0 +1,7 @@ +# -*- mode: sh; -*- +watch_file flake.nix +watch_file flake.lock +if ! use flake . --no-pure-eval +then + echo "dev shell could not be built. The environment was not loaded. Make the necessary changes to flake.nix and hit enter to try again." >&2 +fi diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba58331 --- /dev/null +++ b/.gitignore @@ -0,0 +1,52 @@ +# ---> Emacs +# -*- mode: gitignore; -*- +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# Org-mode +.org-id-locations +*_archive + +# flymake-mode +*_flymake.* + +# eshell files +/eshell/history +/eshell/lastdir + +# elpa packages +/elpa/ + +# reftex files +*.rel + +# AUCTeX auto folder +/auto/ + +# cask packages +.cask/ +dist/ + +# Flycheck +flycheck_*.el + +# server auth directory +/server/ + +# projectiles files +.projectile + +# directory configuration +.dir-locals.el + +# network security +/network-security.data + +# Eask +.eask/ \ No newline at end of file diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..10a9d93 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,103 @@ +# AGENTS.md — lsp-typst + +## Project Overview + +`lsp-typst` is an Emacs Lisp package that provides **LSP support for Typst** (the typesetting language) via `lsp-mode`. It connects to the **tinymist** LSP server to provide completion, diagnostics, formatting, and other LSP features in buffers using `typst-ts-mode`. + +- **Homepage:** https://labs.phundrak.com/phundrak/lsp-typst +- **License:** GPLv3+ + +## Quick Start + +```sh +# Enter the reproducible dev shell (Nix) +nix develop + +# Run tests (currently a placeholder — see Testing Status below) +eask test +``` + +No build step is required — the package is pure Elisp. + +## Dependencies + +| Dependency | Min. Version | Purpose | +|-------------------|--------------|--------------------------------------| +| Emacs | 30.1 | Runtime | +| `lsp-mode` | 10 | LSP client infrastructure | +| `f` | 0.20 | File path manipulation | +| `typst-ts-mode` | 0.12 | Typst major mode (recommended) | + +The package can also **auto-download** the `tinymist` binary from GitHub releases. + +## Project Structure + +| Path | Purpose | +|-------------------------------------|--------------------------------------| +| `lsp-typst.el` | Single-file package (all source) | +| `Eask` | Eask build/test configuration | +| `flake.nix` | Nix flake for reproducible dev shell | +| `flake.lock` | Locked Nix inputs (auto-generated) | + +Everything lives in one Elisp file; keep it that way unless complexity warrants splitting. + +## Key Conventions + +- **Lexical binding** is enabled (`lexical-binding: t`). +- **Colon-double-space** is enforced (`colon-double-space: t`). +- **Naming:** All public symbols are prefixed `lsp-typst-*`. +- **Docstrings:** Present on all `defcustom` and `defgroup` forms. First line is a complete sentence. +- **Comment style:** Section headers use `;;;` with standard Emacs conventions (`;;; Code:`, `;;; Commentary:`, `;;; lsp-typst.el ends here`). +- **Single-file package:** Keep source in `lsp-typst.el` unless the package grows significantly. +- **Indentation:** Standard Emacs Lisp indentation. + +## Public API (User-Facing Customization) + +| Variable | Default | Description | +|-----------------------------------|------------------------------------------|-------------------------------------| +| `lsp-typst-server-command` | `"tinymist"` | LSP server binary path | +| `lsp-typst-server-command-args` | `'("lsp")` | CLI arguments for the server | +| `lsp-typst-download-url` | Auto-detected per platform | URL to download tinymist | +| `lsp-typst-store-path` | Inside `lsp-server-install-dir` | Where the downloaded binary is kept | + +There are no interactive commands defined — all configuration is via `defcustom`. + +## Architecture Notes + +The LSP client is registered in three steps inside `lsp-typst.el`: + +1. **Language-ID mapping** — `typst-ts-mode` is mapped to the language id `"typst"` via `lsp-language-id-configuration`. +2. **Dependency definition** — The `typst` dependency is defined, pointing to either a system binary or a downloadable `tinymist` binary. +3. **Client registration** — `lsp-register-client` creates the client with: + - `:language-id` → `"typst"` + - `:major-modes` → `(nix-ts-mode)` (see **Known Quirks** below) + - `:activation-modes` → `lsp-activate-on "typst"` + - `:server-id` → `'typst` + - Connection via `lsp-stdio-connection` calling `lsp-typst-server-command` + args + - Initialization sends configuration via `lsp--set-configuration` + +## Known Quirks + +- **Suspicious `:major-modes` value:** The client registration lists `(nix-ts-mode)` as the major mode, which looks like a copy-paste artifact. The actual activation gate is `lsp-activate-on "typst"`, but if you're cleaning this up, it should probably be `(typst-ts-mode)`. +- **Only tinymist is supported:** The package is written specifically for **tinymist** (the successor to `typst-lsp`). Do not add support for the older `typst-lsp` server without explicit discussion. + +## What Not to Touch + +- **`flake.lock`** — Auto-generated by Nix. Regenerate with `nix flake update`. Never edit manually. + +## Testing Status + +**No tests exist.** The `Eask` test script is a placeholder: + +```shell +echo "Error: no test specified" && exit 1 +``` + +Any testing setup would need to be created from scratch (likely a `test/` directory with ERT tests). + +## Decision Records + +- **tinymist** is the only supported LSP server. The package was written after `typst-lsp` was deprecated in favor of tinymist. +- **No CI is configured** (no `.github/`, no `.gitlab-ci.yml`). +- **No README** exists — this `AGENTS.md` and the package commentary in `lsp-typst.el` serve as documentation. +- **Self-hosted git:** The canonical repository is on a Gitea/Forgejo instance at `labs.phundrak.com`, not GitHub. diff --git a/Eask b/Eask new file mode 100644 index 0000000..0b7981b --- /dev/null +++ b/Eask @@ -0,0 +1,21 @@ +;; -*- mode: eask; lexical-binding: t -*- + +(package "lsp-typst" + "1.0.0" + "LSP support for Typst with lsp-mode") + +(website-url "https://labs.phundrak.com/phundrak/lsp-typst") +(keywords "typst" "lsp" "tools") + +(package-file "lsp-typst.el") + +(script "test" "echo \"Error: no test specified\" && exit 1") + +(source "gnu") +(source "nongnu") +(source "melpa") + +(depends-on "emacs" "30.1") +(depends-on "f" "0.20") +(depends-on "lsp-mode" "10.0") +(depends-on "typst-ts-mode") diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..bcd9f41 --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1783224372, + "narHash": "sha256-8i/87eeoqiGE4yOTjwSA3Eh/ziJRQEmd/unYU+K27sk=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "d407951447dcd00442e97087bf374aad70c04cea", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..dd32932 --- /dev/null +++ b/flake.nix @@ -0,0 +1,26 @@ +{ + description = "LSP support for Typst with lsp-mode"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { + self, + nixpkgs, + flake-utils, + }: + flake-utils.lib.eachSystem ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"] (system: let + pkgs = import nixpkgs {inherit system;}; + in { + formatter = pkgs.alejandra; + devShells.default = pkgs.mkShell { + name = "lsp-typst"; + buildInputs = [pkgs.eask-cli]; + shellHook = '' + echo "lsp-typst devshell — Eask version: $(eask --version 2>/dev/null || echo 'not found')" + ''; + }; + }); +} diff --git a/lsp-typst.el b/lsp-typst.el new file mode 100644 index 0000000..d97f87f --- /dev/null +++ b/lsp-typst.el @@ -0,0 +1,115 @@ +;;; lsp-typst.el --- LSP support for Typst with lsp-mode -*- lexical-binding: t; colon-double-space: t; -*- + +;; Author: Lucien Cartier-Tilet +;; Maintainer: Lucien Cartier-Tilet +;; Version: 1.0.0 +;; Package-Requires: ((emacs "30.1") (lsp-mode "10") (f "0.20") (typst-ts-mode "0.12")) +;; Homepage: https://labs.phundrak.com/phundrak/lsp-typst +;; Keywords: lsp typst tools + + +;; This file is not part of GNU Emacs + +;; This program is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + + +;;; Commentary: + +;; LSP support for Typst with lsp-mode. +;; +;; I recommend `typst-ts-mode' +;; (https://codeberg.org/meow_king/typst-ts-mode/), available on +;; Nongnu Elpa, if you need a major mode for Typst, but this package +;; does not require it. + +;;; Code: + +(require 'lsp-mode) +(require 'f) + +(defgroup lsp-typst nil + "Settings for the tinymist language server client for Typst." + :group 'lsp-mode + :link '(url "https://github.com/Myriad-Dreamin/tinymist") + :package-version '(lsp-typst . "1.0.0")) + +(defcustom lsp-typst-server-command "tinymist" + "The binary or full path to binary which launches the server." + :type 'string + :group 'lsp-typst + :package-version '(lsp-typst . "1.0.0")) + +(defcustom lsp-typst-server-command-args '("lsp") + "Command-line arguments for the tinymist LSP server." + :type '(repeat string) + :group 'lsp-typst + :package-version '(lsp-typst . "1.0.0")) + +(defcustom lsp-typst-download-url + (format "https://github.com/Myriad-Dreamin/tinymist/releases/latest/download/tinymist-%s" + (let ((aarch64-p (string-match "^aarch64-.*" system-configuration))) + (pcase system-type + ('gnu/linux (if aarch64-p "linux-arm64" "linux-x64")) + ('darwin (if aarch64-p "darwin-arm64" "darwin-x64")) + ('windows-nt (if aarch64-p "win32-arm64.exe" "win32-x64.exe"))))) + "Automatic download URL for tinymist. + +Out of the box, supports x86-64 GNU/Linux, Windows, and macOS. If you +are using Linux with musl, a non-aarch64 ARM architecture, or if you +want tinymist-preview, you’ll probably want to change this value." + :type 'string + :group 'lsp-typst + :package-version '(lsp-typst . "1.0.0")) + +(defcustom lsp-typst-store-path (f-join lsp-server-install-dir + "tinymist" + (if (eq system-type 'windows-nt) + "tinymist.exe" + "tinymist")) + "The path to the file in which `tinymist' will be stored." + :type 'string + :group 'lsp-typst + :package-version '(lsp-typst . "1.0.0")) + +;;;###autoload +(with-eval-after-load 'lsp-mode + (add-to-list 'lsp-language-id-configuration '(typst-ts-mode . "typst")) + + (lsp-dependency 'typst + '(:system "typst") + `(:download :url lsp-typst-download-url + :store-path lsp-typst-store-path + :set-executable? t)) + + (lsp-register-client + (make-lsp-client :new-connection (lsp-stdio-connection + (lambda () + (cons (or (executable-find lsp-typst-server-command) + (lsp-package-path 'typst) + "tinymist") + lsp-typst-server-command-args))) + :activation-fn (lsp-activate-on "typst") + :initialized-fn (lambda (workspace) + (with-lsp-workspace workspace + (lsp--set-configuration (lsp-configuration-section "typst")))) + :priority -1 + :server-id 'typst + :download-server-fn (lambda (_client callback error-callback _update?) + (lsp-package-ensure 'typst callback error-callback))))) + +(lsp-consistency-check lsp-typst) + +(provide 'lsp-typst) + +;;; lsp-typst.el ends here