/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
import { TextDocument } from 'vscode-languageserver-textdocument';
/**
 * Represents a temporary version of a document.
 *
 * This indicates that the document should not be cached or reuse cached results.
 */
export const tempDocVersion = -1;
export class InMemoryDocument {
    version;
    #doc;
    $uri;
    uri;
    constructor(uri, contents, version = 0) {
        this.version = version;
        this.$uri = uri;
        this.uri = uri.toString();
        this.#doc = TextDocument.create(this.uri, 'markdown', version, contents);
    }
    get lineCount() {
        return this.#doc.lineCount;
    }
    positionAt(offset) {
        return this.#doc.positionAt(offset);
    }
    offsetAt(position) {
        return this.#doc.offsetAt(position);
    }
    getText(range) {
        return this.#doc.getText(range);
    }
    replaceContents(newContent) {
        this.#update([{ text: newContent }]);
        return this;
    }
    applyEdits(textEdits) {
        this.#update(textEdits.map(x => ({ range: x.range, text: x.newText })));
        return this;
    }
    previewEdits(textEdits) {
        return TextDocument.applyEdits(this.#doc, textEdits);
    }
    #update(changes) {
        // Temp docs always share the same version
        const newVersion = this.version < 0 ? this.version : this.version + 1;
        this.version = newVersion;
        TextDocument.update(this.#doc, changes, newVersion);
    }
}
//# sourceMappingURL=inMemoryDocument.js.map