Skip to content

Commit 1658954

Browse files
committed
relative file context provider working
1 parent 5184daf commit 1658954

File tree

2 files changed

+51
-16
lines changed

2 files changed

+51
-16
lines changed

file.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[
2+
{
3+
"name": "App.css",
4+
"description": "/Users/guglielmodanna/Repo/test/devtools/react-starter/src/App.css",
5+
"content": "```/Users/guglielmodanna/Repo/test/devtools/react-starter/src/App.css\n#root {\n max-width: 1280px;\n margin: 0 auto;\n padding: 2rem;\n text-align: center;\n}\n\n.logo {\n height: 6em;\n padding: 1.5em;\n will-change: filter;\n transition: filter 300ms;\n}\n.logo:hover {\n filter: drop-shadow(0 0 2em #646cffaa);\n}\n.logo.react:hover {\n filter: drop-shadow(0 0 2em #61dafbaa);\n}\n\n@keyframes logo-spin {\n from {\n transform: rotate(0deg);\n }\n to {\n transform: rotate(360deg);\n }\n}\n\n@media (prefers-reduced-motion: no-preference) {\n a:nth-of-type(2) .logo {\n animation: logo-spin infinite 20s linear;\n }\n}\n\n.card {\n padding: 2em;\n}\n\n.read-the-docs {\n color: #888;\n}\n\n```",
6+
"uri": {
7+
"type": "file",
8+
"value": "/Users/guglielmodanna/Repo/test/devtools/react-starter/src/App.css"
9+
},
10+
"id": {
11+
"providerTitle": "file",
12+
"itemId": "e2d9017e-3de5-4e4e-9f0a-8b2fc0355724"
13+
}
14+
},
15+
{
16+
"name": "App.jsx",
17+
"description": "src/App.jsx",
18+
"content": "```/src/App.jsx\nimport { useState } from \"react\";\nimport reactLogo from \"./assets/react.svg\";\nimport viteLogo from \"/vite.svg\";\nimport \"./App.css\";\n\nfunction App() {\n const [count, setCount] = useState(0);\n\n return (\n <>\n <div>\n <a href=\"https://vite.dev\" target=\"_blank\">\n <img src={viteLogo} className=\"logo\" alt=\"Vite logo\" />\n </a>\n <a href=\"https://react.dev\" target=\"_blank\">\n <img src={reactLogo} className=\"logo react\" alt=\"React logo\" />\n </a>\n </div>\n <h1>Vite + React</h1>\n <div className=\"card\">\n <button onClick={() => setCount((count) => count + 1)}>\n count is {count}\n </button>\n <p>\n Edit <code>src/App.jsx</code> and save to test HMR\n </p>\n </div>\n <p className=\"read-the-docs\">\n Click on the Vite and React logos to learn more\n </p>\n </>\n );\n}\n\nexport default App;\n\n```",
19+
"uri": {
20+
"type": "file",
21+
"value": "src/App.jsx"
22+
},
23+
"id": {
24+
"providerTitle": "relative-file",
25+
"itemId": "a15656b1-eb33-4b6d-8f02-35772283a596"
26+
}
27+
}
28+
];
Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { glob } from 'glob';
2+
import path from 'path';
23
import { BaseContextProvider } from './BaseContextProvider';
34
import {
45
ContextItem,
@@ -18,31 +19,37 @@ class RelativeFileContextProvider extends BaseContextProvider {
1819
async getContextItems(query: string, extras: ContextProviderExtras): Promise<ContextItem[]> {
1920
query = query.trim();
2021
if (!query) { return []; }
21-
22-
let firstMatch: string | undefined;
22+
console.log('will get relative files');
2323
const workspaceDirs = await extras.ide.getWorkspaceDirs();
24-
for (const rootDir of workspaceDirs) {
25-
const matches = await glob(`**/${query}`, {
26-
cwd: rootDir,
27-
signal: AbortSignal.timeout(1000),
28-
});
29-
if (matches.length > 0) {
30-
firstMatch = matches[0];
31-
break;
32-
}
33-
}
34-
if (!firstMatch) { return []; }
35-
const content = await extras.ide.readFile(firstMatch);
24+
const fullPath = await matchPathToWorkspaceDirs(query, workspaceDirs);
25+
if (!fullPath) { return []; }
26+
const content = await extras.ide.readFile(fullPath);
3627
return [{
3728
name: query.split(/[\\/]/).pop() ?? query,
38-
description: firstMatch,
29+
description: fullPath,
3930
content: `\`\`\`${query}\n${content}\n\`\`\``,
4031
uri: {
4132
type: 'file',
42-
value: firstMatch,
33+
value: fullPath,
4334
},
4435
}];
4536
}
4637
}
4738

39+
async function matchPathToWorkspaceDirs(query: string, workspaceDirs: string[]) {
40+
for (const rootDir of workspaceDirs) {
41+
const matches = await glob(`**/${query}`, {
42+
cwd: rootDir,
43+
signal: AbortSignal.timeout(1000),
44+
});
45+
if (matches.length > 0) {
46+
console.log('match', matches[0], path.join(rootDir, matches[0]));
47+
return path.join(rootDir, matches[0]); // Create full path
48+
} else {
49+
return null;
50+
}
51+
}
52+
return null;
53+
}
54+
4855
export default RelativeFileContextProvider;

0 commit comments

Comments
 (0)