Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompt/node/test2Impl.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, PromptSizing } from '@vscode/prompt-tsx';
7
import assert from 'assert';
8
import type * as vscode from 'vscode';
9
import { IIgnoreService } from '../../../platform/ignore/common/ignoreService';
10
import { IWorkspaceService } from '../../../platform/workspace/common/workspaceService';
11
import { CancellationToken } from '../../../util/vs/base/common/cancellation';
12
import { URI } from '../../../util/vs/base/common/uri';
13
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
14
import { ITestGenInfo } from '../../intents/node/testIntent/testInfoStorage';
15
import { Tag } from '../../prompts/node/base/tag';
16
import { DocumentSummarizer } from '../../prompts/node/inline/summarizedDocument/summarizeDocumentHelpers';
17
import { CodeBlock } from '../../prompts/node/panel/safeElements';
18
import { PromptReference } from '../common/conversation';
19
import { IDocumentContext } from './documentContext';
20
import { TestFileFinder, isTestFile } from './testFiles';
21
22
type Props = PromptElementProps<{
23
/**
24
* Document here is expected to be a test file.
25
*/
26
documentContext: IDocumentContext;
27
/**
28
* Src (ie impl) file to include if already known.
29
*/
30
srcFile?: ITestGenInfo;
31
}>;
32
33
/**
34
* @remark Respects copilot-ignore.
35
*/
36
export class Test2Impl extends PromptElement<Props> {
37
38
constructor(
39
props: Props,
40
@IInstantiationService private readonly instaService: IInstantiationService,
41
@IIgnoreService private readonly ignoreService: IIgnoreService,
42
@IWorkspaceService private readonly workspaceService: IWorkspaceService,
43
) {
44
super(props);
45
}
46
47
override async render(state: void, sizing: PromptSizing) {
48
49
const { documentContext, srcFile, } = this.props;
50
51
assert(isTestFile(documentContext.document), 'Test2Impl must be invoked on a test file.');
52
53
let candidateFile: URI | undefined;
54
let selection: vscode.Range | undefined;
55
56
if (srcFile) {
57
candidateFile = srcFile.uri;
58
selection = srcFile.target;
59
} else {
60
// @ulugbekna: find file that this test file corresponds to
61
const finder = this.instaService.createInstance(TestFileFinder);
62
candidateFile = await finder.findFileForTestFile(documentContext.document, CancellationToken.None);
63
}
64
65
if (candidateFile === undefined || await this.ignoreService.isCopilotIgnored(candidateFile)) {
66
return undefined;
67
}
68
69
const doc = await this.workspaceService.openTextDocumentAndSnapshot(candidateFile);
70
71
const docSummarizer = this.instaService.createInstance(DocumentSummarizer);
72
73
const summarizedDoc = await docSummarizer.summarizeDocument(
74
doc,
75
documentContext.fileIndentInfo,
76
selection,
77
sizing.tokenBudget,
78
);
79
80
const references = [new PromptReference(candidateFile)];
81
82
return (
83
<Tag name='codeToTest' priority={this.props.priority}>
84
<references value={references} />
85
Below is the file located at {candidateFile.path}:<br />
86
<CodeBlock
87
code={summarizedDoc.text}
88
uri={candidateFile}
89
languageId={documentContext.document.languageId}
90
/>
91
</Tag>
92
);
93
}
94
95
}
96
97