Core code editor component with tree-sitter support for syntax analysis and code highlighting. Serves as the foundation of the anycode editor and can be used as a standalone library.
anycode-base is a high-performance code editor for web applications, built on top of tree-sitter for parsing and virtual rendering. The editor supports multiple programming languages, integrates with LSP for code completion and navigation, and provides a flexible API for customization.
- Syntax Analysis: Uses web-tree-sitter for code parsing and syntax highlighting
- Virtual Rendering: Efficient rendering of large files with scroll virtualization
- Multi-language Support: JavaScript, TypeScript, Python, Rust, Go, C/C++, Java, Kotlin, C#, HTML, CSS, JSON, YAML, TOML, Lua, Zig, Bash, and more
- LSP Integration: Language Server Protocol support for code completion, go-to-definition, and hover information
- Change History: Undo/redo functionality
- Search: Built-in text search
- Text Selection: Support for multiple selections and cursor operations
- Theme Customization: Flexible theme system for appearance customization
npm install anycode-baseimport { AnycodeEditor } from 'anycode-base';
const code = `// Hello from Anycode Editor!
function greet(name) {
return \`Hello, \${name}!\`;
}
console.log(greet('World'));
`;
async function init() {
const editor = new AnycodeEditor(code, 'example.js', 'javascript');
await editor.init();
editor.render();
document.getElementById('editor')
.appendChild(editor.getContainer());
}
init();Initialization & Lifecycle:
async init(): Promise<void>- Initialize the editor (must be called before use)clean(): void- Clean up and remove the editor from DOMgetContainer(): HTMLDivElement- Get the editor's DOM container
Rendering:
render(): void- Render the editorrenderCursorOrSelection(): void- Render cursor or selection only
Text Operations:
getText(): string- Get all text contentsetText(newText: string): void- Set text contentgetTextLength(): number- Get the length of text contentapplyChange(change: Change): void- Apply a change object to the editor
Cursor & Focus:
getCursor(): { line: number, column: number }- Get current cursor positionsetCursor(line: number, column: number): void- Set cursor positionrequestFocus(line: number, column: number, center?: boolean): void- Request focus at positionrequestedFocus(): boolean- Check if focus was requested
Scroll:
hasScroll(): boolean- Check if editor has scroll positionrestoreScroll(): void- Restore previous scroll position
LSP Integration:
setCompletionProvider(provider: (request: CompletionRequest) => Promise<Completion[]>): void- Set completion providersetGoToDefinitionProvider(provider: (request: DefinitionRequest) => Promise<DefinitionResponse>): void- Set go-to-definition providersetCompletions(completions: Completion[]): void- Set completions manuallyasync toggleCompletion(): Promise<void>- Toggle completion popupasync showCompletion(): Promise<void>- Show completion popupapplyCompletion(index: number): void- Apply completion at index
Callbacks & Events:
setOnChange(func: (change: Change) => void): void- Set callback for text changessetOnCursorChange(callback: (newState: Position, oldState: Position) => void): void- Set callback on cursor change
UI Customization:
setRunButtonLines(lines: number[]): void- Set lines that show run buttonssetErrors(errors: { line: number, message: string }[]): void- Set error markers for lines
- JavaScript / TypeScript
- Python
- Rust
- Go
- C / C++
- Java
- Kotlin
- C#
- HTML / CSS
- JSON / YAML / TOML
- Lua
- Zig
- Bash
A complete list of languages can be found in the src/langs/ directory.
web-tree-sitter- Tree-sitter parser for syntax analysisvscode-textbuffer- Efficient text buffer managementtypescript- Type definitions
MIT