Path: blob/main/extensions/copilot/src/util/common/test/shims/textDocument.ts
13405 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import type * as vscode from 'vscode';6import { splitLines } from '../../../vs/base/common/strings';7import { URI as Uri, UriComponents } from '../../../vs/base/common/uri';8import { IModelChangedEvent } from '../../../vs/editor/common/model/mirrorTextModel';9import { ExtHostDocumentData, IExtHostDocumentSaveDelegate } from '../../../vs/workbench/api/common/extHostDocumentData';10import { EndOfLine } from '../../../vs/workbench/api/common/extHostTypes/textEdit';1112export interface IExtHostDocumentData {13readonly document: vscode.TextDocument;14getText(): string;15onEvents(e: IModelChangedEvent): void;16}1718export function createTextDocumentData(uri: Uri, contents: string, languageId: string, eol: '\r\n' | '\n' | undefined = undefined): IExtHostDocumentData {19const lines = splitLines(contents);20eol = eol ?? (contents.indexOf('\r\n') !== -1 ? '\r\n' : '\n');21const delegate: IExtHostDocumentSaveDelegate = {22$trySaveDocument: function (uri: UriComponents): Promise<boolean> {23throw new Error('Not implemented.');24}25};26return new ExtHostDocumentData(delegate, uri, lines, eol, 1, languageId, false, 'utf8', false);27}2829export function setDocText(doc: IExtHostDocumentData, text: string): void {30doc.onEvents({31changes: [32{33range: {34startLineNumber: 1,35startColumn: 1,36endLineNumber: doc.document.lineCount,37endColumn: doc.document.lineAt(doc.document.lineCount - 1).text.length + 1,38},39rangeOffset: 0,40rangeLength: doc.document.getText().length,41text: text,42},43],44versionId: doc.document.version + 1,45eol: (doc.document.eol === EndOfLine.LF ? '\n' : '\r\n'),46isUndoing: false,47isRedoing: false,48});49}505152