Skip to content

Commit d1e75aa

Browse files
committed
Fixed issue with Poly not showing when no workspace is open
1 parent 8e349c4 commit d1e75aa

File tree

6 files changed

+71
-29
lines changed

6 files changed

+71
-29
lines changed

packages/vscode-extension/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 0.1.6
2+
* Fixed issue with Poly not showing when no workspace is open
3+
* Updated auto restart of TS server
4+
15
### 0.1.5
26
* Updated extension activity bar icon
37
* Only Copy action in context menu

packages/vscode-extension/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "Poly Assistant",
44
"description": "Poly AI assistant for VS Code",
55
"icon": "resources/poly-logo.png",
6-
"version": "0.1.5",
6+
"version": "0.1.6",
77
"publisher": "Poly API Corp",
88
"engines": {
99
"vscode": "^1.75.0"
@@ -16,7 +16,9 @@
1616
"url": "https://github.com/polyapi/poly-alpha",
1717
"directory": "packages/vscode-extension"
1818
},
19-
"activationEvents": [],
19+
"activationEvents": [
20+
"onStartupFinished"
21+
],
2022
"main": "./build/extension.js",
2123
"contributes": {
2224
"menus": {

packages/vscode-extension/src/extension.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,12 @@
11
import * as vscode from 'vscode';
2-
import * as fs from 'fs';
32
import ChatViewProvider from './chat-view-provider';
43

5-
const watchWorkspaceFolder = (folder: vscode.WorkspaceFolder) => {
6-
const polyIndexFilePath = `${folder.uri.fsPath}/node_modules/.poly/lib/index.js`;
7-
if (fs.existsSync(polyIndexFilePath)) {
8-
console.log('POLY: Poly index file found, watching for changes...');
9-
const changeListener = (event) => {
10-
if (event !== 'change') {
11-
return;
12-
}
13-
14-
console.log('POLY: Poly index file changed, restarting TS server...');
15-
vscode.commands.executeCommand('typescript.restartTsServer');
16-
17-
if (!fs.existsSync(polyIndexFilePath)) {
18-
console.log('POLY: Poly index file deleted, reapplying watch...');
19-
fs.unwatchFile(polyIndexFilePath, changeListener);
20-
watchWorkspaceFolder(folder);
21-
}
22-
};
23-
fs.watch(polyIndexFilePath, changeListener);
24-
} else {
25-
console.log('POLY: Poly index file not found, waiting...');
26-
setTimeout(() => watchWorkspaceFolder(folder), 5000);
27-
}
28-
};
4+
import { start as startFileWatcher } from './file-watcher';
295

306
export async function activate(context: vscode.ExtensionContext) {
317
const provider = new ChatViewProvider(context);
328

33-
vscode.workspace.workspaceFolders.forEach(watchWorkspaceFolder);
9+
startFileWatcher();
3410

3511
context.subscriptions.push(vscode.commands.registerCommand('poly.focusMessageInput', () => {
3612
provider.focusMessageInput();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import vscode from 'vscode';
2+
import fs, { Stats } from 'fs';
3+
4+
type Info = {
5+
timeoutID: NodeJS.Timeout;
6+
changeListener: (curr: Stats, prev: Stats) => void;
7+
};
8+
9+
const watchedWorkspaceInfos = new Map<vscode.WorkspaceFolder, Info>();
10+
11+
export const start = () => {
12+
vscode.workspace.onDidChangeWorkspaceFolders((event) => {
13+
event.added.forEach(watchWorkspace);
14+
event.removed.forEach(unwatchWorkspace);
15+
});
16+
vscode.workspace.workspaceFolders?.forEach(watchWorkspace);
17+
};
18+
19+
const getPolyIndexFilePath = (folder: vscode.WorkspaceFolder) => `${folder.uri.fsPath}/node_modules/.poly/lib/index.js`;
20+
21+
const watchWorkspace = (folder: vscode.WorkspaceFolder) => {
22+
const polyIndexFilePath = getPolyIndexFilePath(folder);
23+
if (fs.existsSync(polyIndexFilePath)) {
24+
console.log('POLY: Poly index file found, watching for changes...');
25+
26+
const changeListener = (event) => {
27+
if (event !== 'change') {
28+
return;
29+
}
30+
31+
console.log('POLY: Poly index file changed, restarting TS server...');
32+
vscode.commands.executeCommand('typescript.restartTsServer');
33+
34+
if (!fs.existsSync(polyIndexFilePath)) {
35+
console.log('POLY: Poly index file deleted, reapplying watch...');
36+
fs.unwatchFile(polyIndexFilePath, changeListener);
37+
watchWorkspace(folder);
38+
}
39+
};
40+
fs.watch(polyIndexFilePath, changeListener);
41+
watchedWorkspaceInfos.set(folder, { timeoutID: null, changeListener });
42+
} else {
43+
console.log('POLY: Poly index file not found, waiting...');
44+
const timeoutID = setTimeout(() => watchWorkspace(folder), 5000);
45+
watchedWorkspaceInfos.set(folder, { timeoutID, changeListener: null });
46+
}
47+
};
48+
49+
const unwatchWorkspace = (folder: vscode.WorkspaceFolder) => {
50+
const info = watchedWorkspaceInfos.get(folder);
51+
if (info) {
52+
if (info.timeoutID) {
53+
clearTimeout(info.timeoutID);
54+
}
55+
if (info.changeListener) {
56+
fs.unwatchFile(getPolyIndexFilePath(folder), info.changeListener);
57+
}
58+
watchedWorkspaceInfos.delete(folder);
59+
}
60+
};

packages/vscode-extension/yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ asynckit@^0.4.0:
273273

274274
axios@^1.3.4:
275275
version "1.3.4"
276-
resolved "https://registry.yarnpkg.com/axios/-/axios-1.3.4.tgz#f5760cefd9cfb51fd2481acf88c05f67c4523024"
276+
resolved "https://registry.npmjs.org/axios/-/axios-1.3.4.tgz"
277277
integrity sha512-toYm+Bsyl6VC5wSkfkbbNB6ROv7KY93PEBBL6xyDczaIHasAiv4wPqQ/c4RjoQzipxRD2W5g21cOqQulZ7rHwQ==
278278
dependencies:
279279
follow-redirects "^1.15.0"
878 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)