Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompt/node/testExample.tsx
13399 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 { PromptElement, PromptElementProps, PromptReference, PromptSizing } from '@vscode/prompt-tsx';
7
import type { Progress } from 'vscode';
8
import { TextDocumentSnapshot } from '../../../platform/editing/common/textDocumentSnapshot';
9
import { IParserService } from '../../../platform/parser/node/parserService';
10
import { IWorkspaceService } from '../../../platform/workspace/common/workspaceService';
11
import { CancellationToken } from '../../../util/vs/base/common/cancellation';
12
import * as path from '../../../util/vs/base/common/path';
13
import { URI } from '../../../util/vs/base/common/uri';
14
import { ChatResponseProgressPart, Range } from '../../../vscodeTypes';
15
import { Tag } from '../../prompts/node/base/tag';
16
import { summarizeDocument } from '../../prompts/node/inline/summarizedDocument/summarizeDocumentHelpers';
17
import { CodeBlock } from '../../prompts/node/panel/safeElements';
18
19
// Finds the file that is testing a given file. Uses the findFiles API and file name
20
// heuristics for this.
21
22
export type TestExampleFile = {
23
kind: 'candidateTestFile' | 'anyTestFile';
24
testExampleFile: URI;
25
};
26
27
type Props = PromptElementProps<TestExampleFile>;
28
29
/**
30
* @remark Does NOT respected copilot-ignore. Parent element must make sure the URI is not copilot-ignored.
31
*/
32
export class TestExample extends PromptElement<Props> {
33
34
constructor(
35
props: Props,
36
@IParserService private readonly parserService: IParserService,
37
@IWorkspaceService private readonly workspaceService: IWorkspaceService
38
) {
39
super(props);
40
}
41
42
override async render(state: void, sizing: PromptSizing, progress?: Progress<ChatResponseProgressPart> | undefined, token?: CancellationToken | undefined) {
43
44
const { kind, testExampleFile } = this.props;
45
46
let testDocument: TextDocumentSnapshot;
47
try {
48
testDocument = await this.workspaceService.openTextDocumentAndSnapshot(testExampleFile);
49
} catch (e) {
50
return undefined;
51
}
52
53
const codeExcerpt = await summarizeDocument(
54
this.parserService,
55
testDocument,
56
undefined,
57
new Range(0, 0, 0, 0),
58
sizing.tokenBudget
59
);
60
61
const references = [new PromptReference(testExampleFile)];
62
63
const workspaceOfTestFile = this.workspaceService.getWorkspaceFolders().find(folder => testExampleFile.path.startsWith(folder.path));
64
let pathToTestFile: string = testExampleFile.path;
65
if (workspaceOfTestFile !== undefined) {
66
pathToTestFile = path.relative(workspaceOfTestFile.path, testExampleFile.path);
67
// Convert the path separator to be platform-independent
68
pathToTestFile = pathToTestFile.split(path.sep).join('/');
69
}
70
71
switch (kind) {
72
case 'candidateTestFile': {
73
return (
74
<Tag name='testExample' priority={this.props.priority}>
75
<references value={references} />
76
Excerpt of the existing test file at `{pathToTestFile}`:<br />
77
<CodeBlock uri={testExampleFile} code={codeExcerpt.text} languageId={codeExcerpt.languageId} /><br />
78
Because a test file exists: <br />
79
- Do not generate preambles, like imports, copyright headers etc.<br />
80
- Do generate code that can be appended to the existing test file.
81
</Tag>
82
);
83
}
84
case 'anyTestFile': {
85
return (
86
<Tag name='testExample' priority={this.props.priority}>
87
This is a sample test file:<br />
88
<CodeBlock uri={testExampleFile} code={codeExcerpt.text} languageId={codeExcerpt.languageId} />
89
</Tag>
90
);
91
}
92
}
93
}
94
}
95
96