Skip to content

Commit 46714dc

Browse files
committed
2 parents 8e4fee8 + ce5cc2a commit 46714dc

File tree

9 files changed

+168
-53
lines changed

9 files changed

+168
-53
lines changed

package-lock.json

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"jest": "^29.7.0",
6363
"mini-css-extract-plugin": "^2.7.6",
6464
"mocha": "^10.2.0",
65+
"node-loader": "^2.0.0",
6566
"postcss": "^8.4.31",
6667
"postcss-loader": "^7.3.3",
6768
"style-loader": "^3.3.3",

src/buildOrUpdateStack/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default async function buildOrUpdateStack(
3030
inputString,
3131
briefSkeleton,
3232
functionAndOutputSkeleton,
33-
inputValues
33+
inputValues,
3434
} = await buildStack({
3535
inputJSON: inputJSON,
3636
outputJSON: outputJSON,

src/constants.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as vscode from 'vscode';
2+
import path from 'path';
3+
4+
export const directoryPath = path.join(
5+
vscode.workspace.rootPath,
6+
// TODO: pull from config file
7+
'stacks'
8+
);

src/extension.ts

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import * as vscode from 'vscode';
2-
// import chokidar from 'chokidar';
3-
import stackRegistry from './stack/registry';
42
import findStackPositions from './findStackPositions';
53
import getHoverInformation from './hover';
64
import getStackSnippet from './getStackSnippet';
5+
import trackStackFileRenames from './trackStackFileRenames';
76
import buildOrUpdateStack from './buildOrUpdateStack';
8-
import ensureDirectoryExistence from './manageStackFolder';
97
import convertTypescriptToJson from './convertTypescriptToJson';
108
require('dotenv').config();
119

1210
// You must first call storage.init or storage.initSync
1311
// Set the storage file to be a hidden file, e.g., '.llmCache.json'
1412

1513
export function activate(context: vscode.ExtensionContext) {
14+
trackStackFileRenames(context);
1615
let disposable = vscode.workspace.onDidSaveTextDocument(async (document) => {
1716
if (
1817
document.languageId !== 'typescript' &&
@@ -71,46 +70,4 @@ export function activate(context: vscode.ExtensionContext) {
7170
context.subscriptions.push(disposable);
7271
}
7372

74-
// function trackStackFileRenames(context: vscode.ExtensionContext) {
75-
// const addedFiles = new Map();
76-
77-
// // Initialize Chokidar watcher. This tracks file renames and deletions.
78-
// const watcher = chokidar.watch(directoryPath, {
79-
// ignored: /^\./,
80-
// persistent: true,
81-
// });
82-
83-
// watcher.on('all', (event, filePath) => {
84-
// const fileName = path.basename(filePath);
85-
86-
// if (event === 'add') {
87-
// // Temporarily record added file with a timestamp
88-
// addedFiles.set(fileName, Date.now());
89-
// } else if (event === 'unlink') {
90-
// // Check if there's a recently added file that matches
91-
// for (let [newFile, time] of addedFiles) {
92-
// if (Date.now() - time < 200) {
93-
// // 0.5 seconds threshold
94-
// // stackRegistry.loadRegistry();
95-
// // if (stackRegistry.nameExists(newFile)) {
96-
// // stackRegistry.update(newFile, newFile);
97-
// // }
98-
99-
// console.log(`File renamed from ${fileName} to ${newFile}`);
100-
// addedFiles.delete(newFile);
101-
102-
// return;
103-
// }
104-
// }
105-
// console.log(`File deleted: ${filePath}`);
106-
// }
107-
// });
108-
109-
// context.subscriptions.push({
110-
// dispose: () => {
111-
// watcher.close();
112-
// },
113-
// });
114-
// }
115-
11673
export function deactivate() {}

src/stack/createStackFile/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import * as vscode from 'vscode';
22
import * as path from 'path';
33
import * as fs from 'fs';
4-
5-
const directoryPath = path.join(
6-
vscode.workspace.rootPath,
7-
// TODO: pull from config file
8-
'stacks'
9-
);
4+
import { directoryPath } from '../../constants';
105

116
export default function createStackFile(
127
skeleton: string,

src/stack/registry.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,19 @@ export default class stackRegistry {
5151
console.error('Error writing to registry file:', error);
5252
}
5353
}
54+
55+
static update(oldName, newName) {
56+
console.log(`updating ${oldName} to ${newName} in registry`);
57+
const registry = this.loadRegistry();
58+
const functionId = registry[oldName];
59+
delete registry[oldName];
60+
registry[newName] = functionId;
61+
this.saveRegistry(registry);
62+
}
63+
64+
static remove(methodName) {
65+
const registry = this.loadRegistry();
66+
delete registry[methodName];
67+
this.saveRegistry(registry);
68+
}
5469
}

src/trackStackFileRenames/index.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import * as vscode from 'vscode';
2+
import chokidar from 'chokidar';
3+
import { directoryPath } from '../constants';
4+
import path from 'path';
5+
import stackRegistry from '../stack/registry';
6+
7+
export default function trackStackFileRenames(
8+
context: vscode.ExtensionContext
9+
) {
10+
const eventBuffer = [];
11+
const bufferTime = 1000; // Buffer time in milliseconds
12+
13+
// Initialize Chokidar watcher. This tracks file renames and deletions.
14+
const watcher = chokidar.watch(directoryPath, {
15+
ignored: /^\./,
16+
persistent: true,
17+
});
18+
19+
// Function to process buffered events
20+
function processBufferedEvents() {
21+
const addedFiles = new Map();
22+
const unlinkedFiles = new Map();
23+
24+
// Categorize events
25+
eventBuffer.forEach(({ event, filePath, timestamp }) => {
26+
const fileName = path.basename(filePath);
27+
if (event === 'add') {
28+
addedFiles.set(fileName, { filePath, timestamp });
29+
} else if (event === 'unlink') {
30+
unlinkedFiles.set(fileName, { filePath, timestamp });
31+
}
32+
});
33+
34+
// Process rename and delete events
35+
unlinkedFiles.forEach((unlinkedFile, unlinkedFileName) => {
36+
let isRename = false;
37+
addedFiles.forEach((addedFile, addedFileName) => {
38+
// Check if an 'add' event closely follows an 'unlink' event
39+
if (
40+
Math.abs(addedFile.timestamp - unlinkedFile.timestamp) < bufferTime
41+
) {
42+
const oldNameWithoutExt = unlinkedFileName.replace('.ts', '');
43+
const newNameWithoutExt = addedFileName.replace('.ts', '');
44+
45+
// Handle file rename
46+
if (stackRegistry.nameExists(oldNameWithoutExt)) {
47+
stackRegistry.update(oldNameWithoutExt, newNameWithoutExt);
48+
console.log(
49+
`File renamed from ${oldNameWithoutExt} to ${newNameWithoutExt}`
50+
);
51+
}
52+
addedFiles.delete(addedFileName);
53+
isRename = true;
54+
}
55+
});
56+
57+
// Handle file deletion
58+
if (!isRename) {
59+
const nameWithoutExt = unlinkedFileName.replace('.ts', '');
60+
if (stackRegistry.nameExists(nameWithoutExt)) {
61+
stackRegistry.remove(nameWithoutExt); // Assuming you have a remove method in stackRegistry
62+
console.log(`File deleted: ${nameWithoutExt}`);
63+
}
64+
}
65+
});
66+
67+
eventBuffer.length = 0; // Clear the buffer after processing
68+
}
69+
70+
// Event handler for watcher
71+
watcher.on('all', (event, filePath) => {
72+
console.log(`File ${filePath} has been ${event}ed`);
73+
eventBuffer.push({ event, filePath, timestamp: Date.now() });
74+
75+
// Set a timeout to process events in the buffer
76+
setTimeout(processBufferedEvents, bufferTime);
77+
});
78+
79+
context.subscriptions.push({
80+
dispose: () => {
81+
watcher.close();
82+
},
83+
});
84+
}

webpack.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ const extensionConfig = {
3131
},
3232
module: {
3333
rules: [
34+
{
35+
test: /\.node$/,
36+
use: 'node-loader',
37+
},
3438
{
3539
test: /\.ts$/,
3640
exclude: /node_modules/,
@@ -51,5 +55,4 @@ const extensionConfig = {
5155
},
5256
};
5357

54-
5558
module.exports = [extensionConfig];

0 commit comments

Comments
 (0)