# Editors

The language server for `.lithe` files speaks the [Language Server Protocol](https://microsoft.github.io/language-server-protocol/), so VS Code, IntelliJ
IDEA and any other editor with an LSP client run the same server. It needs **Java 17 or newer**. The
[VS Code extension](https://marketplace.visualstudio.com/items?itemName=Ayfri.lithe) and the
[IntelliJ plugin](https://plugins.jetbrains.com/plugin/36669-lithe) bring it along: see [VS Code](#vs-code) and [IntelliJ IDEA](#intellij-idea).

![A .lithe file in VS Code: the script with the types of its state values as inlay hints, usage counts and links to the generated code above the declarations, and a background on the uses of state and derived values](/docs/editors/overview.webp)

## Features

| Feature | What it covers |
|---------|----------------|
| Errors and warnings | The ones of the Lithe compiler that a build prints, as you type. An editor that asks for the diagnostics of the workspace, such as VS Code, gets those of every `.lithe` file, the others those of the open files. |
| Completion | Components (adding their import), HTML elements, local markup, end tags, and the blocks `<script kotlin>`, `<style>`, `<style global>` and `<page />` after `<`. Props, rest attributes, attributes, events, directives and the options of `<page />` in a tag. Blocks after `@`. The names in scope in Kotlin, including the `${...}` of a style block, and the members of a value whose type the file tells. Class names, properties, at-rules and pseudo-classes in a style block. Import paths. |
| Hover | Components, props and declarations with their documentation comment, their type when the file tells it (a literal, `state` of one or with its type argument, a value of the page) and how often they are used. HTML, CSS and events, with their [Baseline](https://web.dev/baseline) status, flagging what is newer than Lithe's browser target. What `<script kotlin>`, a top-level `<style>`, `<style global>`, `<page />` and its options are for Lithe. The Kotlin standard library and browser APIs, see [Documentation](#documentation). |
| Inlay hints | The type of `state(...)` literals and of page values such as `window.innerWidth()`. The type of a value that `@if`, `@else if`, `@when` or `@await` declares without writing it. The type of the `it` or parameter of a listener, `@mount` or `window.onkeydown { }`. What a multi-line `derived` or `effect` block follows. The parameter names of literal arguments. What a long element or block closes. |
| Signature help | The parameters of `@fun`s and script functions while a call is written. |
| Rename | Components (with their file), props, script and markup declarations and the classes of a style block, in every file that names them. See [Rename](#rename). |
| Navigation | Definition, declaration, references, highlights of the uses in the file, and editing a start and end tag together (VS Code). |
| Highlighting | Semantic tokens for components, blocks and the Kotlin of the script, the markup and the style values, calls with type arguments like `apiData<Map<String, String>>("quote")` included. The `key` of a `@for` header is colored like a named argument and the `=` of a block header like an operator. HTML tags and attributes keep the colors of the editor's grammar. |
| Quick fixes | See [Quick fixes](#quick-fixes). |
| Structure view | The script with its top-level declarations, the tree of the markup and the rules of the style blocks, which breadcrumbs use too. |
| Folding | Elements, blocks, comments, imports, the Kotlin blocks of the script and style rules, the closing line staying visible. |
| Expand selection | From the word to the file, through strings, brackets, tag contents, elements and blocks. |
| Colors | A swatch next to each color of a style block or a `style` attribute, and a color picker where the editor has one for servers. |
| Code lenses | How often the component, its props and the declarations of its script are used, and links to [the generated code](#generated-code). |

The hover of a component lists its props with their types and defaults, and how often it is used:

![The hover of a Rating component in VS Code, listing its props value and max with their types and defaults, the component name and its use count](/docs/editors/hover.webp)

Completion in a tag offers the props of the component with their types, and the `{::name}` binding of a prop:

![The completion list in the tag of a Rating component in VS Code, offering the props max and value and the binding {::value}](/docs/editors/completion.webp)

## VS Code

The Lithe extension starts the server and declares the `.lithe` language.

### Install

1. Install **Lithe** from the Extensions view, or from [its Marketplace page](https://marketplace.visualstudio.com/items?itemName=Ayfri.lithe):
   `code --install-extension Ayfri.lithe`.
2. Open a folder with `.lithe` files. The server starts with the first one you open, on a JDK 17 or newer it finds itself, see `lithe.javaHome` below.
3. Install an extension with a Kotlin grammar for the colors of the Kotlin of a script, see [Colors](#colors). In a Gradle project, the
   [Kotlin extension of JetBrains](https://github.com/Kotlin/kotlin-lsp) adds the hover and the types of Kotlin, see
   [Kotlin in a Gradle project](#kotlin-in-a-gradle-project).

### Extension settings

| Setting            | Effect                                                                                  |
|--------------------|-----------------------------------------------------------------------------------------|
| `lithe.javaHome`   | The JDK 17 or newer that runs the server. Empty uses the first of `JAVA_HOME`, `java` of the `PATH`, the JDK of the Java extension and the JDKs installed on the machine that is 17 or newer. |
| `lithe.server.jar` | Another server jar than the one in the extension.                                        |

The command `Lithe: Restart Language Server` stops the server and starts it again, reading these settings again, so a changed `lithe.javaHome` or a new
server jar applies without reloading the window. It also starts the server when none could start, for example once a JDK is installed.

### Build the extension

The extension in `editors/vscode` builds with [Bun](https://bun.com):

```shell
cd editors/vscode
bun install
bun run server   # packages the server and copies it into the extension, once no server runs from it
bun run build
```

Then run `Developer: Install Extension from Location...` from the command palette and pick `editors/vscode`.

### Colors

The grammar of the extension colors the markup, and the Kotlin of the `<script kotlin>` block, of the `{...}` values and of the `${...}` of a style block
when an installed extension provides a Kotlin grammar.

Components get the semantic token type `component`, which color themes paint like classes (`support.class.component.lithe`), as they do for the components
of Vue and Svelte. Other editors get `class` for them, and IntelliJ IDEA a color of its own.

A hover colors its code blocks with a TextMate grammar only, so the semantic tokens that color the file don't reach them. The extension declares the language
`lithe-script`, the Kotlin grammar of the editor with what the grammar of Lithe adds to it: calls, types, constants, parameters and the `it` of a lambda,
infix functions, and keywords only where they act as keywords. The server writes its code blocks in that language for an editor that asks for it, and the
Kotlin language server's hovers are moved to it too. Other editors get `kotlin`.

The uses of what the page follows, state, derived values and props, get a background. `lithe.reactiveBackground` in `workbench.colorCustomizations`
changes its color, `#2B2B2B` in dark themes.

Each color of a style block, including those inside `light-dark()`, gets a swatch:

![A style block in VS Code with a color swatch next to each hex color of the light-dark() fills](/docs/editors/colors.webp)

## IntelliJ IDEA

The Lithe plugin starts the language server and brings the grammar, so nothing has to be configured. It needs IntelliJ IDEA 2026.3 or newer, or another
JetBrains IDE of that version, for its language server API: the open source builds of IntelliJ IDEA and Android Studio don't have it.

### Getting started

1. Install **Lithe** from **Settings | Plugins | Marketplace** ([its page](https://plugins.jetbrains.com/plugin/36669-lithe)).
2. Open a project with `.lithe` files: a module of the Kotlin toolchain, or a Gradle project with the [Gradle plugin](/docs/sites/kobweb) once it is synced.
   The server starts with the first `.lithe` file you open, on the runtime of the IDE, so no JDK has to be set up. Its item in the status bar shows that it runs.
3. Create a component with **New | Lithe Component** on a folder. Its name is the name of the component, so it starts with an uppercase letter.
4. Write it with the live templates (**Ctrl+J**): `prop`, `state`, `derived`, `effect`, `mount`, `script` and `style`, and the blocks `@if`, `@for`, `@when`,
   `@await`, `@try` and `@fun`.

Everything in [Features](#features) then works in the file. Where the IDE has the Kotlin plugin, as IntelliJ IDEA does, the plugin also hands the Kotlin of
the script and of the markup to it: go to declaration into the standard library and your `.kt` files, one hover page with the Kotlin declaration and what
Lithe tells about it, the types Kotlin infers, and the Kotlin errors, see [Kotlin in a Gradle project](#kotlin-in-a-gradle-project).

![A .lithe file in IntelliJ IDEA: the links to the generated code and the usage counts above the declarations of the script, the types Kotlin infers as inlay hints, a background on the uses of state and derived values, and the imported components in their own color](/docs/editors/intellij-overview.webp)

The documentation of a component lists its props with their types and defaults:

![The Quick Documentation of a TodoItem component in IntelliJ IDEA, with its props and how often it is used](/docs/editors/intellij-hover-component.webp)

If a component shows errors on its components, `{...}` values and `@` blocks, `*.lithe` is a pattern of the HTML file type: remove it in
**Settings | Editor | File Types**, since IntelliJ IDEA then reads the file as HTML.

### Plugin settings

**Settings | Languages & Frameworks | Lithe** names another JDK or another server jar, like `lithe.javaHome` and `lithe.server.jar` do in VS Code, and the
language server item of the status bar links to it. The server runs on the runtime of the IDE otherwise, which is newer than Java 17, and reads its
documentation from the installation of the IDE.

**Tools | Run Lithe Dev Server** serves the module of the open file in the Run window, with links to the address of the server and to the files of the build
errors. It needs a project of the Kotlin toolchain that lists the [dev server](/docs/sites/dev-server), like the repository of Lithe.

### Colors in IntelliJ IDEA

The plugin colors the tokens of the server with the colors of the IDE, and with colors of its own for what those can't say: a component in a tag takes the
color of a Compose component call (the IDE's Compose plugin, so it follows the scheme), and the uses of what the page follows get a background, as they do in
VS Code. **Settings | Editor | Color Scheme | Lithe** changes them. A value the page follows has the default text color, since a color scheme can't add a
background to the color of another attribute. The grammar is the one of the VS Code extension.

### Without the plugin

An IDE with the language server API takes the server from **Settings | Languages & Frameworks | Language Servers** (see
[Language servers](https://www.jetbrains.com/help/idea/language-servers.html)): the `java` executable of a JDK 17 or newer, the arguments
`-jar "<path to a copy of the executable jar>" --stdio` and the pattern `*.lithe`. The jar is the one `bun run server` writes for VS Code,
`editors/vscode/server/lithe-lsp.jar`, and the grammar colors come from adding the `editors/vscode` folder in **Settings | Editor | TextMate Bundles**.

### Build the plugin

The plugin in `editors/intellij` builds with Gradle, since the Kotlin toolchain can't build an IntelliJ plugin, with a JDK 17 or newer in `JAVA_HOME`:

```shell
cd editors/intellij
./gradlew buildPlugin   # packages the server and the grammar, then the plugin
```

Then pick `build/distributions/lithe-intellij-0.1.0.zip` in **Settings | Plugins | Install Plugin from Disk...**.

## Quick fixes

![The quick fix menu of VS Code on an img tag without alt, offering to add alt="" for a decorative image](/docs/editors/fix.webp)

| Warning or name | Fix |
|-----------------|-----|
| A declaration or a prop nothing uses | Removes it with its documentation comment. A prop a parent passes stays. |
| A `state` nothing writes | Makes it a plain `val`. |
| A value computed once | Reads it `by derived { }`. |
| A misspelled event, such as `@onClick` | Writes the event it looks like. |
| An `<img>` without `alt` | Adds `alt=""`, which marks a decorative image. |
| A selector that matches nothing | Removes it, or wraps it in `:global(...)`. |
| A component, or a top-level declaration of a `.kt` file, that isn't imported | Adds its import. |

A fix of a declaration isn't offered when another one shares its line, such as `val a = 1; val b = 2`.

## Generated code

The lenses at the top of a component open the Kotlin it compiles to for the browser and for static rendering, and its CSS, with the options its module builds
with. The files are written to the `lithe/generated` folder of the temporary folder of the system, as they are when the lens is clicked. In the CSS, a
`const val` shows as `${NAME}` because the Kotlin compiler inlines it, and the rules are indented for reading. The [playground](/playground) shows the same
outputs for a file you write in the browser.

## Documentation

Hovers and completion read the documentation a JetBrains IDE ships, from its installation: the MDN pages of HTML, SVG, CSS, DOM events and browser APIs
(CC-BY-SA 2.5, Mozilla), and the sources of the Kotlin standard library, for its KDoc.

The server finds the IDE that started it. Another editor gives the folder of an installation in the `ideHome` initialization option. Without an IDE, like in
VS Code, the server reads the sources of the newest standard library a build downloaded: from the cache of the Kotlin toolchain on Windows, the local Maven
repository or the cache of Gradle. HTML then links to MDN. The server logs where it reads from when it starts.

A member such as `name.isBlank()` is looked up on the type of the value when the file tells it: a written type, a literal, a global of the page, or the `it`
of a listener. After the `.` of such a value, completion lists what the standard library gives its type, its supertypes and any value, without what is
deprecated, and nothing when the file doesn't tell the type.

## Kotlin in a Gradle project

In a Gradle project that compiles its `.lithe` files with the [Gradle plugin](/docs/sites/kobweb), VS Code asks the Kotlin language server it has about the
Kotlin of a `.lithe` file: the [Kotlin extension of JetBrains](https://github.com/Kotlin/kotlin-lsp), which imports the Gradle project. The extension then
shows:

- The hover of Kotlin after the one of Lithe, with the type of what is under the cursor. Lithe then leaves out its guesses about the standard library,
  whose declaration Kotlin knows for sure, and keeps what Kotlin doesn't tell: what a name is for the page, its uses, and MDN for a browser API.
- The definition Kotlin finds, when Lithe finds none.
- The type Kotlin infers for a `val ... by derived { }`, a `var ... by state(...)`, or a value that `@if`, `@when` or `@await` declares, when Lithe gives no
  hint for it, as an inlay hint.

A file of a module of the Kotlin toolchain gets none of it, since the Kotlin a toolchain build generates is in no source set a Kotlin server reads.

IntelliJ IDEA analyzes the same Kotlin itself, in any module with a Kotlin plugin, a Gradle project as well as a module of the Kotlin toolchain. The plugin asks
the server (`lithe/kotlinMap`) for the Kotlin of the file as the editor has it, with where each of its places comes from, and gives it to the Kotlin plugin as a
file in memory, in the JS part of the module of the `.lithe` file, since that Kotlin is the one of the browser. The Kotlin plugin resolves it with the
dependencies of that module, so nothing waits for a build or a file watcher, and the text it analyzes is the one of the editor. In a module of the Kotlin
toolchain, whose generated Kotlin is in no source folder, the plugin declares the header of each component to the Kotlin plugin, in its package
(`lithe/componentStubs`), so a component resolves from any Kotlin the IDE analyzes, and go to declaration on it opens its `.lithe` file. On that Kotlin, the
plugin shows:

- Go to declaration of what the IDE resolves (a standard library or dependency function, or a declaration of the project), next to what Lithe finds. A symbol
  the component declares itself keeps the definition of Lithe.
- One hover page: the colored signature and the doc comment of the declaration Kotlin resolves, with its sections (`@param`, `@return`, `@sample`), then what
  Lithe tells (MDN for a browser API, the uses). For what the component declares, the text of Lithe with the type Kotlin infers. The page of the Kotlin plugin
  itself isn't reachable: its target is internal to the Kotlin plugin, and the documentation the IDE builds for Kotlin goes through internal API, which a plugin
  of the Marketplace can't use.
- The type Kotlin infers for a `derived` or `state` declaration and for a value that `@if`, `@when` or `@await` declares, as an inlay hint, where Lithe gives
  none. It shows while the type hints of Kotlin are on, **Settings | Editor | Inlay Hints | Types | Kotlin**, which a new IDE has off.
- The errors Kotlin finds at the places of the file, without the warnings and without what the compiler writes itself. Before the IDE knows the dependencies of
  the module (before an import or a sync), the runtime of Lithe doesn't resolve and no error shows, rather than one on every name.
- The Kotlin a lens opens (**Generated Kotlin/JS**, **Generated Kotlin/JVM**), read-only and analyzed in the module of the file on that platform, so the Kotlin
  plugin colors and resolves it.

![The Quick Documentation of filter in a .lithe file in IntelliJ IDEA: the colored signature of Iterable.filter, its doc comment and its samples](/docs/editors/intellij-hover-kotlin.webp)

The completion of the Kotlin isn't shown yet. Without the Kotlin plugin (WebStorm), the hover is the one of the server.

## Rename

A rename is refused when the new name isn't an identifier, when it would change what another name stands for, and for what Lithe doesn't see whole: a
declaration of a `.kt` file, a library import without `as`, a global style class, or a class whose name also appears in a Kotlin string. Renaming a
component renames its file, which needs an editor that supports file operations in edits.

## Limits

- Kotlin code isn't checked by the server: its errors come from a build, see [Compiler errors](/docs/sites/static-sites#compiler-errors). Handing it to
  [kotlin-lsp](https://github.com/Kotlin/kotlin-lsp) in a project of the Kotlin toolchain isn't possible, because kotlin-lsp imports Gradle and Maven projects
  only.
- Members of values whose type the file doesn't tell aren't completed, and navigation doesn't reach members, because that needs the types of the Kotlin
  compiler. The members of the declarations of the build aren't completed either, only the ones of the standard library.
- Formatting, refactorings other than rename, workspace symbols and call hierarchies aren't supported.
- `lab()`, `lch()`, `color()` and the colors computed from a variable or another color get no swatch of their own.
- IntelliJ IDEA paints the swatches but opens no color picker, because its client never asks a server how to write a color.
- IntelliJ IDEA doesn't ask for lenses again when another file changes, so a count there can be old until its file changes.
- Only files on disk are served. A file belongs to the nearest folder with a `module.yaml`, which gives it the components of that module and of the modules
  it depends on, or with a `build.gradle.kts`, whose `src/jsMain/kotlin` holds the `.lithe` files, without the projects it depends on. A file outside any
  module is read on its own.

[Performance](/docs/reference/performance#language-server) has the time an editor waits for each answer, and for the server to start.
