-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
167 lines (148 loc) · 4.78 KB
/
extension.js
File metadata and controls
167 lines (148 loc) · 4.78 KB
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
167
const vscode = require("vscode");
const path = require("path");
const fs = require("fs");
// Import core components with better error handling
let CodeLynxDashboard, CodeLynxUsagePanel;
try {
CodeLynxDashboard = require("./src/core/dashboard");
CodeLynxUsagePanel = require("./src/core/usage");
} catch (error) {
console.error("Failed to import core components:", error);
// We'll handle this in the activate function
}
let dashboard;
let usagePanel;
let globalContext;
/**
* Simple TreeDataProvider implementation for CodeLynx Explorer view
*/
class CodeLynxTreeDataProvider {
constructor() {
this._onDidChangeTreeData = new vscode.EventEmitter();
this.onDidChangeTreeData = this._onDidChangeTreeData.event;
}
getTreeItem(element) {
return element;
}
getChildren() {
const chatItem = new vscode.TreeItem("Open AI Chat");
chatItem.command = {
command: "codelynx.openChat",
title: "Open AI Chat",
};
chatItem.iconPath = new vscode.ThemeIcon("comment-discussion");
const usageItem = new vscode.TreeItem("API Usage Statistics");
usageItem.command = {
command: "codelynx.showApiUsage",
title: "Show API Usage Statistics",
};
usageItem.iconPath = new vscode.ThemeIcon("graph");
return [chatItem, usageItem];
}
}
/**
* Initialize and validate configuration settings
* @param {vscode.ExtensionContext} context - Extension context
*/
function initializeConfiguration(context) {
// Validate API limit settings
const config = vscode.workspace.getConfiguration("codelynx");
const apiDailyLimit = config.get("apiDailyLimit");
if (!apiDailyLimit || apiDailyLimit < 1) {
config.update("apiDailyLimit", 100, vscode.ConfigurationTarget.Global);
}
// Register configuration change listener
context.subscriptions.push(
vscode.workspace.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration("codelynx")) {
// Notify panels of configuration change if needed
if (usagePanel && usagePanel._panel) {
usagePanel._refreshUsageData();
}
}
})
);
}
/**
* Deactivates the extension
*/
function deactivate() {
// Clean up resources if needed
console.log("CodeLynx deactivated");
}
// Export activate and deactivate functions directly
// This avoids potential circular reference and activation issues
module.exports = {
activate(context) {
globalContext = context;
try {
console.log("CodeLynx is being activated...");
// Initialize components with explicit error logging
try {
dashboard = new CodeLynxDashboard(context);
usagePanel = new CodeLynxUsagePanel(context);
console.log("CodeLynx components initialized successfully");
} catch (error) {
console.error("Error initializing CodeLynx components:", error);
vscode.window.showErrorMessage(
`CodeLynx initialization error: ${error.message}`
);
}
// Register tree view for explorer
const treeDataProvider = new CodeLynxTreeDataProvider();
const treeView = vscode.window.createTreeView("codelynxExplorer", {
treeDataProvider,
showCollapseAll: false,
});
context.subscriptions.push(treeView);
// Register commands
let chatCommand = vscode.commands.registerCommand(
"codelynx.openChat",
() => {
try {
dashboard.createOrShow();
} catch (error) {
console.error("Error opening chat:", error);
vscode.window.showErrorMessage(
`Error opening chat: ${error.message}`
);
}
}
);
let usageCommand = vscode.commands.registerCommand(
"codelynx.showApiUsage",
() => {
try {
if (!usagePanel) {
console.log("Reinitializing usage panel");
usagePanel = new CodeLynxUsagePanel(context);
}
usagePanel.createOrShow();
} catch (error) {
console.error("Error showing API usage panel:", error);
vscode.window.showErrorMessage(
`Error showing API usage: ${error.message}`
);
}
}
);
// Add commands to subscriptions
context.subscriptions.push(chatCommand);
context.subscriptions.push(usageCommand);
// Initialize configuration
initializeConfiguration(context);
// Show success notification
vscode.window.setStatusBarMessage(
"CodeLynx activated successfully",
3000
);
console.log("CodeLynx activation completed successfully");
} catch (error) {
console.error("Critical error during CodeLynx activation:", error);
vscode.window.showErrorMessage(
`Critical error during CodeLynx activation: ${error.message}`
);
}
},
deactivate,
};