Langium Language Engineering Tool

4.2.2 · active · verified Sun Apr 19

Langium is an open-source, TypeScript-based framework designed for language engineering, offering first-class support for the Language Server Protocol (LSP). It enables developers to create Domain-Specific Languages (DSLs) and low-code platforms with deep integration into environments like VS Code and Eclipse Theia. The current stable version is 4.2.2, with frequent minor releases occurring roughly monthly or bi-monthly, and major versions typically released annually. Key differentiators include its declarative grammar language (similar to Xtext but relying on TypeScript interfaces instead of EMF), a robust dependency injection system for extensibility, high performance powered by Chevrotain, and the ability to generate typed Abstract Syntax Trees (ASTs). Langium supports execution in both Node.js environments and web browsers, making it versatile for various language tooling needs.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a minimal Langium language server setup, showing how to establish an LSP connection and start the server using generated service factories.

import { startLanguageServer, EmptyFileSystem, LangiumSharedServices } from 'langium';
import { NodeFileSystem } from 'langium/node';
import { createConnection, ProposedFeatures } from 'vscode-languageserver/node';

// --- This part of the code is typically generated by `langium-cli` ---
// It sets up your language's grammar, parser, validator, and other services.
// For a runnable example, we'll mock a simple `createMyLanguageServices`.
interface MyLanguageServices {
    // Define your language-specific service interfaces here
    // e.g., validation: { MyLanguageValidator: MyLanguageValidator }
}

function createMyLanguageServices(context: {
    connection?: ReturnType<typeof createConnection>;
    fileSystem: EmptyFileSystem | NodeFileSystem;
}): { shared: LangiumSharedServices; MyLanguage: MyLanguageServices } {
    // In a real Langium project, this would load your grammar and hook up
    // the generated parser, linker, scope provider, etc.
    // For this quickstart, we're providing a basic mock.
    const connection = context.connection ?? createConnection(ProposedFeatures.all);
    // Mimic the creation of default shared services
    const shared = require('langium/lib/shared/shared-module').createDefaultSharedModule(context);
    // Mimic the creation of language-specific services
    const MyLanguage: MyLanguageServices = {}; // Empty mock for demonstration
    
    connection.onInitialize(() => {
        // In a real setup, capabilities would be dynamically provided by Langium
        return {
            capabilities: {
                textDocumentSync: { openClose: true, change: 1 /* TextDocumentSyncKind.Full */ },
                completionProvider: { resolveProvider: true, triggerCharacters: ['.'] },
                hoverProvider: true
            }
        };
    });
    connection.onInitialized(() => {
        connection.console.log('Mock Langium language server initialized for MyLanguage.');
    });

    return { shared, MyLanguage };
}
// ------------------------------------------------------------------

// Establish a connection for the language server.
// Langium typically uses Node's IPC to communicate with the VS Code client.
const connection = createConnection(ProposedFeatures.all);

// Create the shared and language-specific services.
// `createMyLanguageServices` would be imported from your generated module file (e.g., `./src/language-server/my-language-module.ts`).
// The NodeFileSystem is crucial for server-side file access.
const { shared, MyLanguage } = createMyLanguageServices({ connection, ...NodeFileSystem });

// Start the language server.
// This function handles the lifecycle of the LSP connection and dispatches requests
// to the services configured in `shared`.
startLanguageServer(shared);

// Listen for incoming LSP messages.
connection.listen();

view raw JSON →