-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
306 lines (262 loc) · 9.73 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
const vscode = require("vscode");
const { formatElevaComponent } = require("./formatter");
const { detectElevaComponents } = require("./detector");
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
console.log("Eleva.js Formatter is now active");
// Register the formatter command
let disposable = vscode.commands.registerCommand(
"elevaFormatter.format",
function () {
try {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage("No active editor found.");
return;
}
const document = editor.document;
const text = document.getText();
// Check if file contains Eleva components
if (!detectElevaComponents(text)) {
vscode.window.showInformationMessage(
"No Eleva.js components detected in this file."
);
return;
}
// Format the entire document
const formatted = formatElevaComponent(text, getFormatterConfig());
// Replace the entire document content
const fullRange = new vscode.Range(
document.positionAt(0),
document.positionAt(text.length)
);
editor
.edit((editBuilder) => {
editBuilder.replace(fullRange, formatted);
})
.then((success) => {
if (success) {
vscode.window.showInformationMessage(
"Eleva.js code formatted successfully."
);
} else {
vscode.window.showErrorMessage("Failed to format Eleva.js code.");
}
});
} catch (error) {
console.error("Error in formatter command:", error);
vscode.window.showErrorMessage(`Formatting error: ${error.message}`);
}
}
);
// Register document formatter
let documentFormatter =
vscode.languages.registerDocumentFormattingEditProvider(
{ language: "javascript" },
{
provideDocumentFormattingEdits(document) {
try {
// Get formatter config
const config = getFormatterConfig();
// Get document text
const text = document.getText();
// Check if file contains Eleva components
if (!detectElevaComponents(text)) {
return [];
}
// Format the document
const formatted = formatElevaComponent(text, config);
// Return formatting edits
return [
vscode.TextEdit.replace(
new vscode.Range(
document.positionAt(0),
document.positionAt(text.length)
),
formatted
),
];
} catch (error) {
console.error("Error in document formatter:", error);
vscode.window.showErrorMessage(
`Document formatting error: ${error.message}`
);
return [];
}
},
}
);
// Format on save
let formatOnSaveDisposable = vscode.workspace.onWillSaveTextDocument(
(event) => {
try {
const config = vscode.workspace.getConfiguration("elevaFormatter");
if (
config.get("enable") &&
config.get("formatOnSave") &&
event.document.languageId === "javascript"
) {
const text = event.document.getText();
// Check if file contains Eleva components
if (!detectElevaComponents(text)) {
return;
}
// Add formatting to list of operations to perform on save
event.waitUntil(
Promise.resolve([
vscode.TextEdit.replace(
new vscode.Range(
event.document.positionAt(0),
event.document.positionAt(text.length)
),
formatElevaComponent(text, getFormatterConfig())
),
])
);
}
} catch (error) {
console.error("Error in format on save:", error);
// Don't show error message during save to avoid interfering with the save operation
}
}
);
// Register semantic token provider for better event highlighting if enabled
if (
vscode.workspace
.getConfiguration("elevaFormatter")
.get("enableHighlighting")
) {
try {
const semanticTokensProvider = getSemanticTokensProvider();
const semanticTokensLegend = new vscode.SemanticTokensLegend(
["event", "signal", "property"],
["declaration", "readonly", "documentation"]
);
let semanticTokensRegistration =
vscode.languages.registerDocumentSemanticTokensProvider(
{ language: "javascript" },
semanticTokensProvider,
semanticTokensLegend
);
context.subscriptions.push(semanticTokensRegistration);
} catch (error) {
console.error("Error registering semantic tokens provider:", error);
// Continue without semantic highlighting if it fails
}
}
// Register all disposables
context.subscriptions.push(disposable);
context.subscriptions.push(documentFormatter);
context.subscriptions.push(formatOnSaveDisposable);
}
/**
* Gets the formatter configuration from VS Code settings
* @returns {Object} Formatter configuration object
*/
function getFormatterConfig() {
try {
const config = vscode.workspace.getConfiguration("elevaFormatter");
return {
templateStyle: config.get("templateStyle") || "backticks",
indentSize: config.get("indentSize") || 2,
};
} catch (error) {
console.error("Error getting formatter config:", error);
// Return defaults if config retrieval fails
return { templateStyle: "backticks", indentSize: 2 };
}
}
/**
* Creates a semantic tokens provider for Eleva.js template events
* @returns {vscode.DocumentSemanticTokensProvider} Semantic tokens provider
*/
function getSemanticTokensProvider() {
return {
provideDocumentSemanticTokens(document) {
try {
const tokenTypes = ["event", "signal", "property"];
const tokenModifiers = ["declaration", "readonly", "documentation"];
const builder = new vscode.SemanticTokensBuilder();
const text = document.getText();
if (!text) return builder.build();
// Find template functions
const templateRegex =
/template\s*:\s*\(\s*(?:ctx|context|\w+)\s*\)\s*=>\s*(['"`])([\s\S]*?)\1/g;
let templateMatch;
while ((templateMatch = templateRegex.exec(text)) !== null) {
if (!templateMatch[2]) continue; // Skip if template content is undefined
const templateContent = templateMatch[2];
const startQuote = templateMatch[1];
const templateStart =
templateMatch.index + templateMatch[0].indexOf(templateContent);
// Find event attributes (@click, @input, etc.)
const eventRegex = /@([a-zA-Z][a-zA-Z0-9:-]*)\s*=\s*(['"`])(.*?)\2/g;
let eventMatch;
while ((eventMatch = eventRegex.exec(templateContent)) !== null) {
if (!eventMatch[1]) continue; // Skip if event name is undefined
const eventName = eventMatch[1];
const attrStart = templateStart + eventMatch.index;
try {
// Add token for event attribute
const line = document.positionAt(attrStart).line;
const char = document.positionAt(attrStart).character;
const length = 1 + eventName.length; // Include the @ symbol
builder.push(line, char, length, 0, 0); // 0 = 'event' type
} catch (e) {
console.error("Error highlighting event:", e);
// Continue to the next event
}
}
// Find signal interpolations
const signalRegex =
/\{\{\s*([a-zA-Z][a-zA-Z0-9_\.]*(?:\.value)?)\s*\}\}/g;
let signalMatch;
while ((signalMatch = signalRegex.exec(templateContent)) !== null) {
if (!signalMatch[1]) continue; // Skip if signal name is undefined
const signalName = signalMatch[1];
const signalStart = templateStart + signalMatch.index + 2; // Skip {{
try {
// Add token for signal
const line = document.positionAt(signalStart).line;
const char = document.positionAt(signalStart).character;
const length = signalName.length;
builder.push(line, char, length, 1, 0); // 1 = 'signal' type
} catch (e) {
console.error("Error highlighting signal:", e);
// Continue to the next signal
}
}
}
// Find eleva-prop attributes
const propRegex = /eleva-prop-([a-zA-Z][a-zA-Z0-9_-]*)/g;
let propMatch;
while ((propMatch = propRegex.exec(text)) !== null) {
if (!propMatch[1]) continue; // Skip if prop name is undefined
const propName = propMatch[1];
const propStart = propMatch.index + "eleva-prop-".length;
try {
// Add token for property
const line = document.positionAt(propStart).line;
const char = document.positionAt(propStart).character;
const length = propName.length;
builder.push(line, char, length, 2, 0); // 2 = 'property' type
} catch (e) {
console.error("Error highlighting property:", e);
// Continue to the next property
}
}
return builder.build();
} catch (error) {
console.error("Error in semantic token provider:", error);
return new vscode.SemanticTokensBuilder().build(); // Return empty tokens on error
}
},
};
}
function deactivate() {}
module.exports = {
activate,
deactivate,
};