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