-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
166 lines (138 loc) · 4.96 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const vscode = require('vscode');
const http = require('http');
const crypto = require('crypto');
// Generate a unique ID for this window
const windowId = crypto.randomBytes(16).toString('hex');
// Keep track of when this window was last focused
let lastFocusTime = 0;
let activeEditor = null;
let server = null;
let serverPort = null;
function updateActiveEditor() {
activeEditor = vscode.window.activeTextEditor;
if (vscode.window.state.focused) {
lastFocusTime = Date.now();
}
}
async function startServer() {
if (server) {
return;
}
// Try ports in range 54321-54330
for (let port = 54321; port <= 54330; port++) {
try {
server = http.createServer((req, res) => {
if (req.method === 'GET') {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Content-Type', 'application/json');
// Only return content if this window/server is still active
if (!server) {
res.statusCode = 404;
res.end(JSON.stringify({ error: 'Window closed' }));
return;
}
res.end(JSON.stringify({
windowId: windowId,
lastFocusTime: lastFocusTime,
content: activeEditor?.document.getText() || '',
language: activeEditor?.document.languageId,
fileName: activeEditor?.document.fileName,
selectedText: activeEditor?.selection ? activeEditor.document.getText(activeEditor.selection) : '',
timestamp: new Date().toISOString(),
isFocused: vscode.window.state.focused,
port: serverPort // Include port for debugging
}));
}
});
await new Promise((resolve, reject) => {
server.listen(port, '127.0.0.1', () => {
serverPort = port;
resolve();
});
server.on('error', reject);
});
console.log(`Editor content server started on port ${port} for window ${windowId}`);
// Set up error handling
server.on('error', (error) => {
console.error(`Server error on port ${serverPort}: ${error}`);
stopServer();
});
return;
} catch (error) {
console.log(`Port ${port} in use, trying next port...`);
continue;
}
}
vscode.window.showErrorMessage('Failed to start server: all ports in use');
}
function stopServer() {
if (!server) {
return;
}
const port = serverPort;
server.close(() => {
console.log(`Editor content server stopped on port ${port} for window ${windowId}`);
});
server = null;
serverPort = null;
}
function activate(context) {
console.log(`Activating extension for window ${windowId}`);
updateActiveEditor();
// Register commands
let startCommand = vscode.commands.registerCommand('huggingchat-helper.start', () => {
if (server) {
vscode.window.showInformationMessage('HuggingChat Helper is already running');
return;
}
startServer();
});
let stopCommand = vscode.commands.registerCommand('huggingchat-helper.stop', () => {
if (!server) {
vscode.window.showInformationMessage('HuggingChat Helper is not running');
return;
}
stopServer();
});
context.subscriptions.push(startCommand);
context.subscriptions.push(stopCommand);
// Track window focus and editor changes
context.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor(() => {
updateActiveEditor();
})
);
context.subscriptions.push(
vscode.window.onDidChangeWindowState(e => {
if (e.focused) {
updateActiveEditor();
}
})
);
startServer();
// Ensure server is stopped when this specific window is closed
context.subscriptions.push({
dispose: () => {
console.log(`Disposing extension for window ${windowId}`);
stopServer();
}
});
// Additional cleanup for window close events
context.subscriptions.push(
vscode.workspace.onDidCloseTextDocument(() => {
// If there are no more editors in this window, stop the server
if (!vscode.window.visibleTextEditors.length) {
console.log(`No more editors in window ${windowId}, stopping server`);
stopServer();
}
})
);
}
function deactivate() {
console.log(`Deactivating extension for window ${windowId}`);
stopServer();
}
module.exports = {
activate,
deactivate
}