# The editor plug-in

Mote in your editor: `mote editor <name>` sets it up, `mote lsp` is the
language server, and `tools/vscode-mote` is the extension for VS Code and Cursor. Back to the [map](README.md).

## Setting an editor up

One line, for the editor you use:

```sh
mote editor vscode      # or: cursor, windsurf, vscodium, neovim, vim, helix, emacs
```

`mote editor` alone lists them and says which it finds on this machine. It
writes the editor's files from inside the `mote` binary (nothing is fetched)
and points them at that binary. Then restart the editor and open a `.mote`
file. Any other editor that speaks the Language Server Protocol runs
`mote lsp`.

## What you get

`mote lsp` is the language server: the compiler itself, answering an editor.

- **What is wrong, as you type**, before saving: the compiler's own
  diagnostics with their fix, for the whole program the file is part of (the
  nearest `app.mote` at or above the file that reads it), each said on the
  file it is in.
- **Hover**: the line that declares a name, the type the compiler found for
  it, and its comment (below). On a word of the language's own (`fs`,
  `column`), its lines of the [card](card.md).
- **Go to definition, find references, rename**, across the program's files.
  A field (`note.title`) goes to its shape's or model's line; an attribute on
  a component (`Card wide=true`) to the component's parameter.
- **Completion**: the names known where the caret is, nearest first; after a
  dot, the fields of what stands before it.
- **The outline** of a file: its states, fns, components, shapes and models.
- **Colours by meaning**, over the grammar's colours: a state (which can
  change) apart from a derive (which cannot), a `server fn` apart from a
  plain one, a parameter, a field, a component, a tag, an attribute.
- **Types beside names** (inlay hints), where the line does not say one:
  `let total` is shown as `let total: number`. `mote.hints` turns them off in
  VS Code.

A file that does not parse yet is still answered: hover, definitions and
colours are read from its words, not from the compiled program.

### Comments that document

The `//` lines directly above a declaration are its documentation, and so is
a comment after it on its own line. They are shown wherever the name is
hovered, in any file:

```mote
// How many notes there are.
// Kept between visits.
state count = 0 persist

shape Note
  // What it says.
  title: string
```

This holds for `state`, `derive`, `resource`, `live`, `let`, `fn` (of every
kind), `action`, `rule`, `component`, `shape`, `model`, and each field of a
shape or model. A blank line between the comment and the declaration ends
it: a file's opening comment is not its first declaration's.

### What it does not do yet

- It does not format a file, and offers no quick fixes: a diagnostic's fix is
  a sentence.
- Names are found by their words and the blocks they are written in, which is
  right for ordinary programs; a name the compiler makes itself (`it` in a
  handler) has no declaration to go to.
- The kit's components (`Button`, `Dialog`) hover with their parameters, and
  have no file to go to.
- Helix shows no colours for Mote (it colours by tree-sitter grammars, and
  there is none yet); Vim has colours and no server of its own.

## The files

| File | What |
|---|---|
| `package.json` | Declares the language (`mote`, files ending `.mote`) and the two grammars. |
| `language-configuration.json` | `//` comments, brackets, auto-closing pairs, and the lines after which the editor indents (a declaration with a block, `if`, `for`, `match`, a line ending in `=>`, …). |
| `syntaxes/mote.tmLanguage.json` | The grammar. A line is tried, in order, as a comment, a declaration, a conditional-style line, a statement keyword, a markup line; what is left is an expression. |
| `syntaxes/mote.markdown.json` | Injects the grammar into fenced `mote` blocks in Markdown. |
| `extension.js` | Starts `mote lsp` and passes the editor's questions to it. Written by hand: no packages. |
| `check.mjs` | Checks the grammar without an editor. |

`tools/editors/` holds the files for Vim, Neovim and Emacs. In the compiler: `lsp.rs` (the server), `index.rs` (what is declared where, from a file's words), `toks.rs` (the words), `editor.rs` (`mote editor`).

## Checking it

```sh
node tools/vscode-mote/check.mjs
```

reads every `.mote` file in the repository through the grammar's own
patterns. Each regular expression must compile and each `include` must name
a rule. It prints how many lines began with a declaration, a tag, a
component or a statement, the lines it could not place, and ends with
`the grammar's patterns compile and its includes resolve`.

## Keeping it in step with the language

The grammar has its own word lists; the compiler does not generate them.
When the language grows, add the word here too:

| New thing | Where in `mote.tmLanguage.json` |
|---|---|
| a declaration word | the `declaration` rule; and `increaseIndentPattern` in `language-configuration.json` if it opens a block |
| a namespace (`slug.…`) | the namespace list in `expression` (the pattern that begins `\\b(net|db|sql|…`) |
| a built-in fn | the built-in list in `expression` |
| a conditional-style word | the `pseudo-line` rule |

Then run `check.mjs`.

## In the source

`tools/vscode-mote/` is the extension for VS Code and the editors made from
it (its own `README.md` says the same in brief); `tools/editors/` holds
Vim's, Neovim's and Emacs's files. In the compiler: `editor.rs` (`mote
editor`), `lsp.rs` (the server), `index.rs` and `toks.rs` (what a file
declares, read from its words).
