Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/simulation/workbench/components/openInVSCode.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 * as cp from 'child_process';
7
import { promises as fs } from 'fs';
8
import * as mobxlite from 'mobx-react-lite';
9
import * as path from 'path';
10
import * as React from 'react';
11
import { ISimulationTest } from '../stores/simulationTestsProvider';
12
13
14
export const OpenInVSCodeButton = mobxlite.observer(({ test }: { test: ISimulationTest }) => {
15
const simulationInputPath = test.simulationInputPath;
16
const matchResult = test.name.match(/case-\d+/);
17
if (!simulationInputPath || !matchResult) {
18
return null;
19
}
20
return (
21
<button
22
className='test-open-in-vscode'
23
onClick={() => { openInVSCode(simulationInputPath, matchResult[0]); }}>
24
Open In VS Code Insiders
25
</button>
26
);
27
});
28
29
30
async function openInVSCode(simulationInputPath: string, caseName: string) {
31
try {
32
const conversation = JSON.parse(await fs.readFile(path.join(simulationInputPath, `${caseName}.conversation.json`), 'utf8'));
33
if (!Array.isArray(conversation) || conversation.length === 0 || !conversation[0].repo_folder) {
34
return;
35
}
36
const repoPath = path.join(simulationInputPath, 'repos', conversation[0].repo_folder);
37
cp.execFileSync('code-insiders', [repoPath]);
38
39
const state = JSON.parse(await fs.readFile(path.join(simulationInputPath, conversation[0].stateFile), 'utf8'));
40
const documentFilePath = state?.activeTextEditor?.documentFilePath;
41
if (documentFilePath) {
42
const selection = state?.activeTextEditor?.selections?.[0];
43
const line = selection?.active?.line;
44
cp.execFileSync('code-insiders', ['-g', path.join(simulationInputPath, documentFilePath + (line ? `:${line + 1}` : ''))]);
45
}
46
47
} catch (e) {
48
console.error(e);
49
}
50
}
51
52