Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/simulation/inlineEdit/fileLoading.ts
13394 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 { readFileSync } from 'fs';
7
import { join } from 'path/posix';
8
import { getFixturesDir } from '../stestUtil';
9
10
/**
11
* Rewrites fileName.[ext].txt to fileName.[ext] if no virtualFileName is given!
12
*
13
* This function allows [tools](https://github.com/microsoft/vscode-ts-file-path-support/tree/main) to inline/extract the file content.
14
*/
15
16
type FileInfo = { filePath: string | { fullPath: string; pathWithinFixturesDir?: string } } | {/** Relative */ fileName: string; fileContents: string };
17
18
export interface ILoadedFile {
19
fileContents: string;
20
fileName?: string; // relative
21
filePath?: string; // absolute
22
pathWithinFixturesDir?: string; // relative
23
}
24
25
/**
26
* This function allows [tools](https://github.com/microsoft/vscode-ts-file-path-support/tree/main) to inline/extract the file content.
27
*/
28
export function loadFile(data: FileInfo): ILoadedFile {
29
let fileName: string | undefined = undefined;
30
let filePath: string | undefined = undefined;
31
let pathWithinFixturesDir: string | undefined = undefined;
32
let fileContents: string;
33
34
if ('fileContents' in data) {
35
fileName = data.fileName;
36
fileContents = data.fileContents;
37
} else {
38
if (typeof data.filePath === 'string') {
39
filePath = data.filePath;
40
filePath = filePath;
41
} else {
42
filePath = data.filePath.fullPath;
43
pathWithinFixturesDir = data.filePath.pathWithinFixturesDir;
44
}
45
46
fileContents = readFileSync(filePath, 'utf8');
47
}
48
49
return { fileContents, fileName, filePath, pathWithinFixturesDir };
50
}
51
52
export function inlineEditsFixture(pathWithinFixturesDir: RelativeFilePath<'$dir/../fixtures/inlineEdits'>): { fullPath: string; pathWithinFixturesDir: string } {
53
const fullPath = join(getFixturesDir(), 'inlineEdits', pathWithinFixturesDir);
54
return {
55
fullPath,
56
pathWithinFixturesDir,
57
};
58
}
59
60
/** See https://github.com/microsoft/vscode-ts-file-path-support */
61
type RelativeFilePath<T extends string> = string & { baseDir?: T }; export interface IInlineEditBaseFile {
62
fileContents: string;
63
64
/** Relative or absolute */
65
virtualFileName: string;
66
}
67
68