@singapore-editor/tree-sitter
v0.2.17
Published
Worker-based Tree-sitter parsing for Singapore syntax colors, folds, and brackets.
Maintainers
Readme
@singapore-editor/tree-sitter
Worker-based Tree-sitter parsing for Singapore syntax colors, folds, and brackets.
Part of Singapore. A code editor for the browser that keeps every version.
Install
npm install @singapore-editor/core @singapore-editor/tree-sitter @singapore-editor/tree-sitter-languagesUsage
import { Editor } from '@singapore-editor/core/editor'
import { createTreeSitterLanguagePlugin } from '@singapore-editor/tree-sitter'
import { TREE_SITTER_LANGUAGE_CONTRIBUTIONS } from '@singapore-editor/tree-sitter-languages/catalog'
const host = document.createElement('div')
host.style.height = '400px'
document.body.append(host)
const editor = new Editor(host, {
plugins: [createTreeSitterLanguagePlugin(TREE_SITTER_LANGUAGE_CONTRIBUTIONS)],
})
editor.setText('const value = 1\n', { languageId: 'typescript' })Requirements
Tree-sitter and native Markdown parsing need Chromium 144, Firefox 145, Safari 26.2, or newer, and Node.js 26.0.0 or Bun 1.2.22 or newer for parser calls outside a browser. The parser uses WebAssembly.Memory.prototype.toResizableBuffer(), and those are the first versions that support it.
API highlights
createTreeSitterLanguagePlugin()installs language contributions for structural parsing and editor palette colors.createTreeSitterHighlighterPlugin()andcreateTreeSitterHighlighterProvider()paint VS Code themes through@shaulavo/tree-sitter-scope-compaton the same worker.createTreeSitterSyntaxProvider()registers custom grammars.expandTreeSitterSelection()selects a larger syntax node.
Merge units
TreeSitterWorkerOwner.mergeUnit() queries an already-parsed snapshot using its document ID,
runtime session ID, snapshot version, language ID and UTF-16 range. It returns status: 'ok'
with the smallest enclosing syntax unit, its source-spelling signature and its parent's range
and commutativity flag. A retired or unavailable snapshot returns status: 'stale' and
unit: null.
Language contributions supply mergeUnitQuerySource. Queries capture @merge.unit,
@merge.signature and @merge.commutative. An optional repeated @_merge.member capture
limits commutativity to parents whose named semantic children all match that pattern. Comments
leave eligibility unchanged. The worker compiles this query only when requested. Languages
without a query, unmatched ranges and units with syntax errors use the enclosing complete
lines with source: 'line'. Fallback includes requested line separators and keeps CRLF together.
Named JavaScript imports use the local alias as their signature when present. Go field names are individual units whose parent range identifies the owning declaration, including grouped fields. Ordered parents expose source-name hints; duplicate-signature comparisons apply to commutative parents. JavaScript class bodies and objects, Go and Rust field lists, and TypeScript interfaces containing methods or call signatures stay ordered. Property-only TypeScript interfaces and JSON objects allow unordered children.
Generic languages reuse their retained parse. Markdown creates a structural block tree on its first merge-unit request because its native renderer keeps its tree private. Injected code uses the deepest containing registered language's query. Successful results retain that selected language ID, including line fallback.
In the Singapore family
@singapore-editor/core owns the editor view. This optional package adds syntax parsing. Choose the other plugins your app needs.
Use a bundler that serves the package worker and data assets. See the hosting guide.
Singapore README · Documentation
License
MIT. License
