Skip to content

Language server ​

yuhwa-lsp brings yuhwa to any editor that speaks the Language Server Protocol: diagnostics as you type, quick fixes, completion, hover, go to definition, rename and formatting. The visual editor uses the same server, running in a Web Worker.

Quick setup ​

VS Code, Cursor or VSCodium? Install the extension. It bundles the server, so skip the rest of this section. Zed? See Zed.

For every other editor:

  1. Install the server. It comes with the yuhwa package and needs Node.js 20 or later.

    sh
    npm install -g yuhwa
    yuhwa-lsp --version   # prints the version: the server is on your PATH
    sh
    pnpm add -g yuhwa
    yuhwa-lsp --version   # prints the version: the server is on your PATH
  2. Add the snippet for your editor from below. They all say the same three things:

    whatvalue
    commandyuhwa-lsp --stdio
    file extension.yu (.yuhwa too)
    language idyuhwa
  3. Open a .yu file and type a [color: bleu]. A warning with the quick fix blue should appear. If nothing happens, see Troubleshooting.

Without a global install, use npx -y -p yuhwa yuhwa-lsp --stdio (or pnpm --package=yuhwa dlx yuhwa-lsp --stdio) as the command. It starts slower, since the package is resolved on every launch.

VS Code ​

The extension adds highlighting (also inside ```yuhwa and ```yu blocks in Markdown), brackets, comments and the bundled language server.

Install it from the VS Code Marketplace, or from the command line:

sh
code --install-extension ph1p.yuhwa-vscode

Cursor and VSCodium do not use the Marketplace. Build the .vsix from the repository and install it:

sh
git clone https://github.com/ph1p/yuhwa.git
cd yuhwa
pnpm install
pnpm --filter ./packages/vscode package
code --install-extension packages/vscode/yuhwa.vsix

Use cursor --install-extension or codium --install-extension for the last line. Or open the Extensions view, choose … › Install from VSIX… and pick the file.

settingwhat it does
yuhwa.server.patha yuhwa-lsp to run instead of the bundled one
yuhwa.trace.servermessages or verbose logs the traffic to the yuhwa output channel

yuhwa: Restart language server in the command palette restarts it.

Format Document in a Markdown file formats its ```yuhwa blocks and <!--yuhwa comments and leaves the rest of the page alone. When another extension formats Markdown too, VS Code asks which one to use. Pick Format Document With… to choose, or set it per language:

json
{
  "[markdown]": { "editor.defaultFormatter": "ph1p.yuhwa-vscode" }
}

Neovim ​

Neovim 0.11 or later:

lua
vim.filetype.add({ extension = { yu = 'yuhwa', yuhwa = 'yuhwa' } })

vim.lsp.config('yuhwa', {
  cmd = { 'yuhwa-lsp', '--stdio' },
  filetypes = { 'yuhwa' },
  root_markers = { '.git' },
})
vim.lsp.enable('yuhwa')

Neovim colours the file from the server's semantic tokens, so you get highlighting without a Tree-sitter grammar. The default 0.11 keymaps work: K hover, grn rename, gra code actions (quick fixes), grr references, Ctrl-] definition. Format with vim.lsp.buf.format().

On Neovim 0.10 or older, register the same cmd and filetypes through nvim-lspconfig's custom server config, or upgrade.

Helix ​

In ~/.config/helix/languages.toml:

toml
[language-server.yu]
command = "yuhwa-lsp"
args = ["--stdio"]

[[language]]
name = "yuhwa"
scope = "source.yuhwa"
file-types = ["yu", "yuhwa"]
comment-token = "//"
indent = { tab-width = 2, unit = "  " }
language-servers = ["yuhwa"]

Run hx --health yuhwa to check that Helix finds the server. Helix does not colour files from semantic tokens and has no yuhwa grammar, so .yu files show uncoloured. Diagnostics, completion, hover, rename and formatting work.

Zed ​

Zed only starts language servers that an extension registers, so settings alone are not enough. The repository has a minimal extension in packages/zed. It is not in the Zed extension registry yet. Install it as a dev extension:

  1. Install Rust through rustup. Zed compiles the extension with it.
  2. Run zed: install dev extension from the command palette and pick the packages/zed folder of a clone of the repository.

The extension starts the first server it finds:

  1. lsp.yuhwa-lsp.binary from your settings;
  2. yuhwa-lsp on your PATH;
  3. the yuhwa package from npm, which it installs and runs with Zed's own Node.js.
json
{
  "lsp": {
    "yuhwa-lsp": {
      "binary": { "path": "/path/to/yuhwa-lsp", "arguments": ["--stdio"] }
    }
  }
}

Highlighting comes from a small tree-sitter grammar in packages/tree-sitter. Zed fetches it from GitHub by the rev in the extension's extension.toml, so a grammar change needs a pushed commit first. Zed reads a quoted string as one token, so it highlights style spans only in unquoted text such as edge labels and card rows. Groups, themes, defs and panels show in the outline panel and the breadcrumbs.

The extension also starts the server for Markdown, where it only formats the ```yuhwa blocks. Zed formats Markdown with Prettier unless told otherwise, so pick the server in your settings:

json
{
  "languages": {
    "Markdown": { "formatter": { "language_server": { "name": "yuhwa-lsp" } } }
  }
}

The rest of the page is then left as it is.

Sublime Text ​

Install the LSP package. Sublime needs a syntax that claims .yu files; save this as Packages/User/yuhwa.sublime-syntax:

yaml
%YAML 1.2
---
name: yuhwa
file_extensions: [yu, yuhwa]
scope: source.yuhwa
contexts:
  main:
    - match: '(^|\s)//.*$'
      scope: comment.line.yu

Then in Preferences › Package Settings › LSP › Settings:

json
{
  "clients": {
    "yuhwa": {
      "enabled": true,
      "command": ["yuhwa-lsp", "--stdio"],
      "selector": "source.yuhwa"
    }
  }
}

Emacs ​

With eglot (built in since Emacs 29):

elisp
(define-derived-mode yuhwa-mode prog-mode "yuhwa"
  (setq-local comment-start "// "))
(add-to-list 'auto-mode-alist '("\\.yu\\'" . yuhwa-mode))

(with-eval-after-load 'eglot
  (add-to-list 'eglot-server-programs '(yuhwa-mode "yuhwa-lsp" "--stdio")))

With lsp-mode, keep yuhwa-mode from above and add:

elisp
(with-eval-after-load 'lsp-mode
  (add-to-list 'lsp-language-id-configuration '(yuhwa-mode . "yuhwa"))
  (lsp-register-client
   (make-lsp-client :new-connection (lsp-stdio-connection '("yuhwa-lsp" "--stdio"))
                    :activation-fn (lsp-activate-on "yuhwa")
                    :server-id 'yuhwa-lsp)))

Kate ​

First give .yu files a mode: Settings › Configure Kate › Open/Save › Modes & Filetypes, add a filetype yuhwa with the extension *.yu. Then add a server in LSP Client › User Server Settings:

json
{
  "servers": {
    "yuhwa": {
      "command": ["yuhwa-lsp", "--stdio"],
      "highlightingModeRegex": "^yuhwa$"
    }
  }
}

JetBrains IDEs ​

Install the LSP4IJ plugin. In Settings › Languages & Frameworks › Language Servers, add a server:

  • Command: yuhwa-lsp --stdio
  • Mappings › File name patterns: *.yu, language id yuhwa

Vim ​

With vim-lsp:

vim
autocmd BufRead,BufNewFile *.yu setfiletype yuhwa

if executable('yuhwa-lsp')
  autocmd User lsp_setup call lsp#register_server({
        \ 'name': 'yuhwa-lsp',
        \ 'cmd': {server_info -> ['yuhwa-lsp', '--stdio']},
        \ 'allowlist': ['yuhwa'],
        \ })
endif

With coc.nvim, in coc-settings.json (and the setfiletype line above):

json
{
  "languageserver": {
    "yuhwa": {
      "command": "yuhwa-lsp",
      "args": ["--stdio"],
      "filetypes": ["yuhwa"]
    }
  }
}

Other editors ​

Any client that can start a server over stdio works the same way: the command is yuhwa-lsp --stdio, the file extension is .yu, the language id is yuhwa.

Features ​

featurewhat you get
diagnosticsthe same errors and warnings as yuhwa check, as you type
quick fixesone click for did you mean "…" suggestions
completionnode names, classes, shapes, colours, prop keys and values, settings, style span keys in text
hoverwhat a name is, its label, group and classes; what a style span sets
go to definition, referencesacross imported files
highlightsevery use of the name under the caret
renamea node, group, class or def, everywhere it is used
document symbols, foldinggroups, panels and blocks
semantic tokenscolouring that knows names from keywords and values
formattingtwo-space indents, trimmed lines, one blank line at most
on-type formattingindent after Enter and }, continue - / * outline bullets

Files are matched by the .yu extension. Imports are read from disk relative to the importing file; open files win over what is on disk.

Markdown documents (.md, .mdx, .markdown) only get formatting: the server formats their diagram blocks and offers no other feature for them. Clients that do not open Markdown files with the server can send the page text with the yuhwa/formatMarkdown request ({ text }); it returns the text edits.

Formatting only changes whitespace. The lines inside a panel or node body (timeline t { … }, api [card] { … }) keep their own indentation, since a plugin may read it; the block moves as a whole.

Protocol details ​

The server works with simple and full clients alike:

  • Transports: --stdio (the default), --node-ipc, --socket=<port>, --pipe=<name>.
  • Positions are UTF-16, as the protocol defaults to.
  • Sync is incremental. Clients that send the whole text work too.
  • Diagnostics are pulled (textDocument/diagnostic) when the client says it can, and pushed (publishDiagnostics) otherwise. Never both.
  • Settings: the server needs none. workspace/didChangeConfiguration is accepted and ignored.

Troubleshooting ​

yuhwa-lsp: command not found. npm's global folder is not on your PATH. npm prefix -g prints it; the binary is in its bin folder. GUI editors started from the Dock or a launcher often do not see your shell's PATH either. Use the absolute path from which yuhwa-lsp as the command.

No diagnostics, no completion. The editor has not mapped the file to the yuhwa language. Check the language shown in the status bar (:set ft? in Vim and Neovim). The file must end in .yu.

The server starts and stops. Run yuhwa-lsp --stdio in a terminal. It should wait silently for input; Ctrl-C quits. An error there is the error your editor sees. Check node --version too: 20 or later.

See the traffic. VS Code: set yuhwa.trace.server to verbose and open the yuhwa output channel. Neovim: :checkhealth vim.lsp shows the attached clients and the log file. Helix: hx --health yuhwa, then :log-open.

An import is not found. Imports are read from disk relative to the importing file and must end in .yu. An open, unsaved file wins over the copy on disk.

In the browser ​

The server also runs in a Web Worker, with a small client for the page. This is how the visual editor gets its diagnostics, completion and colouring.

ts
// yuhwa.worker.ts
import { startWorkerServer } from 'yuhwa/lsp/browser';

startWorkerServer({
  // optional: source of an imported file that is not open
  readFile: (path) => undefined,
});
ts
// page
import { LspClient } from 'yuhwa/lsp/client';

const worker = new Worker(new URL('./yuhwa.worker.ts', import.meta.url), { type: 'module' });
const lsp = new LspClient(worker);
await lsp.initialize();

lsp.on('textDocument/publishDiagnostics', ({ uri, diagnostics }) => {
  console.log(uri, diagnostics);
});

lsp.open('file:///main.yu', source);
lsp.change('file:///main.yu', newSource);

const items = await lsp.request('textDocument/completion', {
  textDocument: { uri: 'file:///main.yu' },
  position: { line: 3, character: 7 },
});

LspClient is plain JSON-RPC over postMessage with no dependencies. Use request for any LSP method the server lists in its capabilities.

Without a protocol ​

YuhwaService from yuhwa/lsp is the language service itself: plain methods that take the source text and a position and return LSP-shaped results. Use it to build editor features without a connection.

ts
import { YuhwaService } from 'yuhwa/lsp';

const service = new YuhwaService();
service.diagnostics(source, '/main.yu');
service.completion(source, { line: 3, character: 7 }, '/main.yu');

Released under the MIT License.