npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@exercism/codemirror-lang-lean

v1.1.0

Published

Lean language support for CodeMirror

Readme

Lean language support for CodeMirror

NPM Version

This repo contains the grammar for the Lean 4 language.

Lean's syntax is user-extensible, so no fixed grammar can describe it. This one highlights Lean's tokens — keywords, sorts and core types, literals, comments and operators — and nests them inside balanced (), [], {} and ⟨⟩ nodes so that bracket matching and folding work. A catch-all token means no input, however malformed, can stop the parse.

Indentation follows the Lean 4 VS Code extension: a new line starts where the line before it starts, one level deeper after an opening bracket or under a focus dot (· tactic). Lean's layout carries meaning, so lines that already exist are never re-indented.

Two more things are taken from that extension. sorry, admit and #exit are tagged as invalid rather than as keywords, so that code left unfinished stands out. And ', ! and ? count as parts of a word, so that a double click or a word motion takes x', get! and find? whole.

The highlighting covers what that extension's grammar marks without a Lean server: the name a declaration introduces (def add, structure Point), attributes (@[simp], attribute [simp]), modifiers (private, partial), and the strings of m!, panic!, throwError, dbg_trace and reportIssue!, whose {…} hold code like those of s!. What it adds on top is a curated list of core type names (Nat, Std.HashMap, Std.Time.PlainDateTime), which VS Code leaves plain. What needs the Lean server (local variables, fields, tactics defined by the user) is out of reach.

The lexical rules follow the Lean 4 sources: src/Init/Meta/Defs.lean for what may appear in an identifier, and src/Lean/Parser/Basic.lean for comments, strings, characters and numbers. The keyword list is drawn from src/Lean/Parser/{Command,Term,Do}.lean plus the core tactics, and has every keyword the VS Code extension's grammar has; being a curated list rather than a closed one, it is the part most likely to need extending.

A tactic's name is a keyword to Lean only where a tactic can start, and an ordinary name anywhere else, which a grammar of tokens cannot tell apart. So the tactics named with everyday words (case, cases, change, clear, delta, done, ext, fail, first, focus, iterate, left, next, replace, right, skip, split) are deliberately not in the list: in (left right : Tree α), next : Option Nat or .fail they are names, and that is how programs use them far more often than proofs do.

⚠️ To ensure that the package can be integrated on the Exercism website, don't change the packages' versions:

"@codemirror/autocomplete": "^6.0.0",
"@codemirror/language": "^6.10.1",
"@codemirror/state": "^6.0.0",
"@codemirror/view": "^6.0.0",
"@lezer/highlight": "^1.2.0",
"@lezer/lr": "^1.4.0"

The @codemirror/autocomplete, @codemirror/state and @codemirror/view ranges are wide on purpose. An editor extension only works with the copy of those two packages that the editor itself was built from, so the ranges have to accept whatever version the website has installed, rather than make the package manager install a second one.

Unicode input

lean() also brings Lean's \-abbreviations: type \to and get →, \<> and get ⟨⟩ with the cursor in between, \alpha (or just \a) and get α.

They behave as in the Lean 4 VS Code extension, because they are the same: the table of abbreviations and the rules for replacing them come from @leanprover/unicode-input, the package that extension is built on. This repository contains neither, only the code that connects that package to CodeMirror (src/unicode-input.ts), so updating the dependency is all it takes to follow upstream.

  • The abbreviation being typed is underlined.
  • It is replaced when the next character can't be part of an abbreviation (\to followed by a space), or at once when nothing longer starts with it (\forall).
  • Moving the cursor away, pressing Tab or leaving the editor replaces it too. Tab is only taken while an abbreviation is being typed.
  • What was typed needn't be complete: \al gives ∀, the shortest abbreviation that starts with al.
  • \\ gives a single \.
  • Undo brings back the text as typed.
  • Resting the pointer on a symbol shows how to type it: "Type ⟨ using \< or \langle. ⟨ can be auto-closed with ⟩ using \<>."

One thing differs from VS Code. There, abbreviations are also replaced inside string and character literals, which turns the escapes \t, \r and \" into ▸, → and Ä as you type them. Here a \ typed inside a literal is left alone (the code inside an s!"…{…}…" interpolation is still code). Pass inStringLiterals: true for VS Code's behaviour.

import { lean } from "@exercism/codemirror-lang-lean";

lean(); // abbreviations on, with VS Code's defaults
lean({ unicodeInput: false }); // highlighting only
lean({
  unicodeInput: {
    leader: "\\", // lean4.input.leader
    eagerReplacement: true, // lean4.input.eagerReplacementEnabled
    customTranslations: { foo: "☺" }, // lean4.input.customTranslations
    inStringLiterals: false,
    hover: true, // the hints on symbols
  },
});

The extension is exported on its own as leanUnicodeInput(). convertLeanAbbreviations is a command that replaces what is being typed right away (what Tab runs), and abbreviationHintAt(state, pos) returns what the hover shows, for a host that wants to present it differently.

Completion and signatures

lean() also completes names, without a Lean server. It knows every public declaration of Lean's Init and Std libraries, from an index generated with Lean itself (see Updating the completion index), and what the file itself declares.

  • List. lists what is in the namespace: definitions, constructors and theorems, with the theorems last. Std. lists HashMap, TreeMap and the other namespaces inside it.
  • xs. lists List's methods when the file tells what xs is: a binder (xs : List Nat), let xs : Array Int := …, a structure field, a literal ("…", 'c', […], #[…]), a type ascription (e : T), or the result type of the previous call ((s.splitOn ",")., s.splitOn "," |>.). When the type cannot be told, nothing is listed.
  • A name on its own (two letters or more) completes to the local names, the file's declarations, names in the current and the open namespaces, and the standard library's top level.
  • Std names are only offered once the file imports a module that declares them, as Lean sees them: import Std.Data.HashMap brings in Std.HashMap, open Std makes it HashMap. After import, module names complete.
  • The list shows each signature; the panel next to it shows the docstring.
  • Resting the pointer on a name shows its signature and docstring.
  • Nothing is offered inside comments and strings, or while a \-abbreviation is being typed.
  • The list opens by itself, so Enter is careful not to take a line break for a choice. After a dot (xs.fo) it accepts a suggestion that extends the word, as in VS Code. For a name on its own it accepts only once you have moved through the list with the arrow keys or opened it with Ctrl+Space: the list cannot know every name (a syntax category, a name from another file), and would otherwise turn term into termination_by at the end of a line. At the end of a word that is already complete, it starts a new line.

The suggestions come through languageData, so they show in the editor's own autocompletion() (basicSetup includes it) and need no setup. The index is split into about a hundred files that load the first time they are needed: the list of namespaces (21 KB compressed) when the editor gets focus, and a namespace's members when its name is typed. A bundler that splits code, as esbuild does on the Exercism website, gives each file its own chunk, so none of it is in the bundle that loads with the editor.

lean({ completion: false }); // no completion, no signature hovers
lean({ completion: { hover: false } }); // completion without the hovers

leanCompletionSource is the completion source on its own, and declarationAt(state, pos) returns what the hover shows.

The names, signatures and docstrings come from Lean 4, which is licensed under the Apache License 2.0. They are in dist/data/, with a NOTICE and a copy of that licence. The rest of the package is MIT licensed.

Install

To keep the package working on the Exercism website, don't change the versions of these packages:

"@codemirror/autocomplete": "^6.0.0",
"@codemirror/language": "^6.10.1",
"@codemirror/state": "^6.0.0",
"@codemirror/view": "^6.0.0",
"@lezer/highlight": "^1.2.0",
"@lezer/lr": "^1.4.0"

A range the website's lockfile doesn't already have makes it install a second copy of that package, and the editor extensions built on the second copy then do nothing.

  • NPM: npm install @exercism/codemirror-lang-lean
  • Yarn: yarn add @exercism/codemirror-lang-lean
  • Bun: bun add @exercism/codemirror-lang-lean

Prerequisites

Resources

Structure

The repo's source files are defined in TypeScript.

The repo is structured as follows:

.
├── .github/workflows
│   ├── test.yml (builds and tests every pull request and push to main)
│   ├── publish.yml (publishes to npm when a v* tag is pushed)
│   └── sync-labels.yml (keeps the issue labels in step with Exercism's)
├── src
│   ├── index.ts (the main plugin: language, highlighting, `lean()`)
│   ├── syntax.grammar (the Lezer grammar)
│   ├── syntax.grammar.d.ts, syntax.grammar.terms.d.ts (the grammar typings)
│   ├── type-names.ts (the curated type names, kept in step with the grammar)
│   ├── ident-utils.ts (what may appear in an identifier)
│   ├── interp-prefix.ts, std-namespace.ts, qualified-type.ts (hand-written tokenizers)
│   ├── tokens.ts, std-tokens.ts, qualified-type-tokens.ts (what the grammar imports them through)
│   ├── indent.ts (indentation on Enter)
│   ├── unicode-input.ts (the `\`-abbreviations)
│   ├── completion.ts (completion and signature hovers)
│   ├── lean-index.ts (loads the index, decides what an import makes visible)
│   ├── lean-scope.ts (reads the file: imports, declarations, open namespaces, locals)
│   ├── completion-types.ts (the index's format)
│   └── data (the generated index, with NOTICE and LICENSE-LEAN)
├── test
│   ├── cases (test cases)
│   │   └ *.txt (test case)
│   ├── grammar.test.ts (test file)
│   ├── indent.test.ts (test file)
│   ├── completion.test.ts (test file)
│   ├── parser.ts (the parser the tests share)
│   └── unicode-input.test.ts (test file)
├── tools
│   ├── parse-corpus.mjs (parses a tree of .lean files, dev only)
│   └── index (extract.lean and build.mjs, which generate src/data)
├── index.html (dev server file)
├── rollup.config.js (bundling config)
├── tsconfig.json (typescript config)
└── vite.config.js (vite dev server config)

Setup

Run bun install to install all dependencies.

Developing

To help with development, run bun run dev. This will start a Vite dev server (usually at http://localhost:5173/) that renders the index.html file. The #editor element gets populated with some sample source code of your choosing and then it will get transformed by the grammar defined in src/syntax.grammar. Any changes to the grammar will auto-refresh the dev server's rendered contents.

Testing

The test/cases directory contains the tests files. Run bun run test to build the package and then run these tests (bun test alone runs them without building; the tests build their own parser from the grammar). The Test workflow runs the same command on every pull request and on every push to main.

Note: test (case) files should be relatively small and focus on a single aspect of a grammar.

For wider coverage, tools/parse-corpus.mjs parses every .lean file under the directories you give it and reports both error nodes and Unknown tokens (a character the tokenizer has no rule for):

bun tools/parse-corpus.mjs ../lean/exercises

Point it at a checkout of the Lean track, or at Lean's own src/Init and src/Std.

Updating the completion index

The index in src/data/ is generated, and committed so that building the package needs no Lean. Regenerate it when the Lean track moves to a new Lean version (all its exercises pin the same one in lean-toolchain):

  1. Set tools/index/lean-toolchain to the track's version.

  2. With that toolchain installed (elan toolchain install <version>), run, from tools/index/:

    lean --run extract.lean /tmp/decls.jsonl /tmp/modules.jsonl
    node build.mjs /tmp/decls.jsonl /tmp/modules.jsonl

    The first step takes about a minute.

  3. Update the version in tools/index/NOTICE, run the tests, and release.

extract.lean writes every public declaration of Init and Std (leaving out deprecated ones and what Lean generates: match_, proof_, equation lemmas, recursors, instances), with its signature, docstring and module. build.mjs groups them by namespace into files of about 100 KB.

Building

Run bun run prepare to build the package into dist/. bun install runs the same command, so a fresh install also builds the package. Only dist/ is published, along with this README and the licence.

Publishing

To publish a new version, set version in package.json and commit it. Then push a tag made of v followed by that version, for example v1.0.0. The tag starts the Publish workflow, which builds and tests the package and publishes it to npm. If the tag differs from version, the workflow stops before it publishes.

The workflow calls Exercism's shared npm-publish.yml, which publishes through npm's trusted publishing, so the repository needs no npm token. The package on npmjs.com needs a trusted publisher that names this repository and publish.yml.