LSP Text Document Implementation

1.0.12 · active · verified Sun Apr 19

The `vscode-languageserver-textdocument` package provides a lightweight and immutable in-memory representation of a text document, specifically designed for use within Node.js-based Language Server Protocol (LSP) servers. It abstracts away the complexities of file system interactions, offering a consistent model for document content, versioning, and change tracking. This is a foundational utility within the larger `vscode-languageserver-node` monorepo, which is actively maintained by Microsoft. While the monorepo sees frequent `next` releases (e.g., `10.0.0-next.x` for server components), `vscode-languageserver-textdocument` itself is currently stable at version `1.0.12`, indicating a mature and less frequently changing API for its core document management functions. It is written in TypeScript and ships with comprehensive type definitions, making it well-suited for TypeScript-first development environments. Its primary differentiator is its focus on being a simple, efficient, and reliable document model optimized for LSP server operations like diagnostics, completions, and refactoring.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates creating a `TextDocument`, applying simulated text edits to generate a new document version, and retrieving document content and position/offset information. Note that `vscode-languageserver-types` is a peer dependency for `Position`, `Range`, `TextEdit`.

import { TextDocument } from 'vscode-languageserver-textdocument';
import { Position, Range, TextEdit } from 'vscode-languageserver-types';

// 1. Create an initial document
const uri = 'file:///path/to/example.ts';
const languageId = 'typescript';
const version = 1;
const initialText = 'console.log("Hello");\nconst x = 10;';
let document = TextDocument.create(uri, languageId, version, initialText);

console.log(`Initial Document (v${document.version}):\n${document.getText()}`);
console.log(`Line Count: ${document.lineCount}`);

// 2. Simulate an edit: Change 'Hello' to 'World'
const changeRange: Range = {
  start: Position.create(0, 13),
  end: Position.create(0, 18)
};
const newText = 'World';

const edits: TextEdit[] = [
  TextEdit.replace(changeRange, newText)
];

// 3. Apply the edits to get a new document instance
const newVersion = document.version + 1;
const updatedDocument = TextDocument.applyEdits(document, edits);

// Important: The original document is unchanged
console.log(`\nOriginal Document (still v${document.version}):\n${document.getText()}`);

// The updated document reflects the changes
console.log(`\nUpdated Document (v${updatedDocument.version}):\n${updatedDocument.getText()}`);

// Example of getting position from offset and vice-versa
const offset = updatedDocument.offsetAt(Position.create(1, 6)); // 'x' in 'const x = 10;'
const position = updatedDocument.positionAt(offset);
console.log(`\nOffset of 'x': ${offset}, Position: { line: ${position.line}, character: ${position.character} }`);

view raw JSON →