Skip to content

lspeasy / core/src / DocumentVersionTracker

Class: DocumentVersionTracker ​

Defined in: packages/core/src/utils/document.ts:43

Tracks monotonically increasing version numbers for open text documents.

Remarks ​

The LSP spec requires DidChangeTextDocumentParams.textDocument.version to increment with every change notification. Use DocumentVersionTracker to manage version numbers centrally rather than maintaining ad-hoc counters in each document model.

Use When ​

You are building an LSP client that sends textDocument/didChange notifications and need to track per-document version counters.

Never ​

NEVER send a textDocument/didChange with the same version number as a previous change for the same document. The server may reject the change as a no-op or apply it out of order, causing text state desync.

Example ​

ts
import { DocumentVersionTracker, createIncrementalDidChangeParams } from '@lspeasy/core';

const tracker = new DocumentVersionTracker();
tracker.open('file:///src/main.ts');

const params = createIncrementalDidChangeParams(
  'file:///src/main.ts',
  [{ range: { start: { line: 0, character: 0 }, end: { line: 0, character: 5 } }, text: 'hello' }],
  { tracker }
);
await client.sendNotification('textDocument/didChange', params);

Constructors ​

Constructor ​

ts
new DocumentVersionTracker(): DocumentVersionTracker;

Returns ​

DocumentVersionTracker

Methods ​

close() ​

ts
close(uri): void;

Defined in: packages/core/src/utils/document.ts:84

Stops tracking a document URI.

Parameters ​

ParameterTypeDescription
uristringThe document URI to stop tracking.

Returns ​

void


currentVersion() ​

ts
currentVersion(uri): number | undefined;

Defined in: packages/core/src/utils/document.ts:75

Returns the current tracked version, if any.

Parameters ​

ParameterTypeDescription
uristringThe document URI to query.

Returns ​

number | undefined

The current version number, or undefined if the document is not tracked.


nextVersion() ​

ts
nextVersion(uri): number;

Defined in: packages/core/src/utils/document.ts:62

Increments and returns the next document version.

Parameters ​

ParameterTypeDescription
uristringThe document URI to increment.

Returns ​

number

The incremented version number.


open() ​

ts
open(uri, initialVersion?): void;

Defined in: packages/core/src/utils/document.ts:52

Starts tracking a document URI with an optional initial version.

Parameters ​

ParameterTypeDefault valueDescription
uristringundefinedThe document URI to start tracking.
initialVersionnumber0Starting version number (defaults to 0).

Returns ​

void

Released under the MIT License.