@caartzee/theme-sdk 1.0.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +208 -20
- package/dist/bin/caartzee-theme.js +2272 -87
- package/dist/lib/index.js +2274 -87
- package/dist/types/src/blocks/block-contract.d.ts +10 -10
- package/dist/types/src/cli/candy-mcp.d.ts +2 -0
- package/dist/types/src/cli/candy-tool-server.d.ts +39 -0
- package/dist/types/src/cli/mcp/component-tools.d.ts +2 -0
- package/dist/types/src/cli/mcp/docs-tools.d.ts +89 -0
- package/dist/types/src/cli/mcp/editor-tools.d.ts +22 -0
- package/dist/types/src/cli/mcp/resources-and-prompts.d.ts +2 -0
- package/dist/types/src/cli/mcp/testing-tools.d.ts +54 -0
- package/dist/types/src/cli/mcp/ui-patterns-tools.d.ts +26 -0
- package/dist/types/src/cli/mcp/v2-commerce-tools.d.ts +16 -0
- package/dist/types/src/contracts/manifest.d.ts +77 -77
- package/dist/types/src/dev-server/dev-server.d.ts +3 -0
- package/dist/types/src/dev-server/local-commerce-runtime.d.ts +76 -0
- package/dist/types/src/dev-server/local-editor-server.d.ts +22 -0
- package/dist/types/src/dev-server/theme-preview.d.ts +6 -2
- package/dist/types/src/scaffold/design-presets.d.ts +41 -0
- package/dist/types/src/scaffold/template-generator.d.ts +2 -0
- package/dist/types/src/sections/section-contract.d.ts +12 -12
- package/package.json +2 -1
- package/templates/theme-candy-boilerplate/.editorconfig +14 -0
- package/templates/theme-candy-boilerplate/.gitattributes +1 -0
- package/templates/theme-candy-boilerplate/.vscode/extensions.json +7 -0
- package/templates/theme-candy-boilerplate/.vscode/settings.json +17 -0
- package/templates/theme-candy-boilerplate/assets/theme.css +78 -0
- package/templates/theme-candy-boilerplate/assets/theme.js +7 -0
- package/templates/theme-candy-boilerplate/candy.config.json +40 -0
- package/templates/theme-candy-boilerplate/index.ts +39 -0
- package/templates/theme-candy-boilerplate/layout/theme.candy +25 -0
- package/templates/theme-candy-boilerplate/locales/en.default.json +37 -0
- package/templates/theme-candy-boilerplate/manifest.json +123 -0
- package/templates/theme-candy-boilerplate/package.json +16 -0
- package/templates/theme-candy-boilerplate/schemas/settings_schema.json +36 -0
- package/templates/theme-candy-boilerplate/sections/featured-products.candy +89 -0
- package/templates/theme-candy-boilerplate/sections/footer.candy +50 -0
- package/templates/theme-candy-boilerplate/sections/header.candy +116 -0
- package/templates/theme-candy-boilerplate/sections/hero-banner.candy +135 -0
- package/templates/theme-candy-boilerplate/sections/image-with-text.candy +85 -0
- package/templates/theme-candy-boilerplate/snippets/price.candy +19 -0
- package/templates/theme-candy-boilerplate/snippets/product-card.candy +43 -0
- package/templates/theme-candy-boilerplate/templates/404.candy +12 -0
- package/templates/theme-candy-boilerplate/templates/cart.candy +51 -0
- package/templates/theme-candy-boilerplate/templates/collection.candy +51 -0
- package/templates/theme-candy-boilerplate/templates/index.candy +7 -0
- package/templates/theme-candy-boilerplate/templates/page.candy +10 -0
- package/templates/theme-candy-boilerplate/templates/product.candy +59 -0
- package/templates/theme-candy-boilerplate/templates/search.candy +40 -0
- package/templates/vscode-candy/LICENSE +21 -0
- package/templates/vscode-candy/extension.js +689 -338
- package/templates/vscode-candy/icons/candy-logo.png +0 -0
- package/templates/vscode-candy/package.json +18 -3
- package/templates/vscode-candy/snippets/candy.code-snippets +91 -0
|
@@ -1,338 +1,689 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
const
|
|
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
|
-
{ name: '
|
|
37
|
-
{ name: '
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
{ name: '
|
|
42
|
-
{ name: '
|
|
43
|
-
{ name: '
|
|
44
|
-
{ name: '
|
|
45
|
-
{ name: '
|
|
46
|
-
{ name: '
|
|
47
|
-
{ name: '
|
|
48
|
-
{ name: '
|
|
49
|
-
{ name: '
|
|
50
|
-
{ name: '
|
|
51
|
-
{ name: '
|
|
52
|
-
{ name: '
|
|
53
|
-
{ name: '
|
|
54
|
-
{ name: '
|
|
55
|
-
{ name: '
|
|
56
|
-
{ name: '
|
|
57
|
-
{ name: '
|
|
58
|
-
{ name: '
|
|
59
|
-
{ name: '
|
|
60
|
-
{ name: '
|
|
61
|
-
{ name: '
|
|
62
|
-
{ name: '
|
|
63
|
-
{ name: '
|
|
64
|
-
{ name: '
|
|
65
|
-
{ name: '
|
|
66
|
-
{ name: '
|
|
67
|
-
{ name: '
|
|
68
|
-
{ name: '
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
{ name: '
|
|
73
|
-
{ name: '
|
|
74
|
-
{ name: '
|
|
75
|
-
{ name: '
|
|
76
|
-
{ name: '
|
|
77
|
-
{ name: '
|
|
78
|
-
{ name: '
|
|
79
|
-
{ name: '
|
|
80
|
-
{ name: '
|
|
81
|
-
{ name: '
|
|
82
|
-
{ name: '
|
|
83
|
-
{ name: '
|
|
84
|
-
{ name: '
|
|
85
|
-
{ name: '
|
|
86
|
-
{ name: '
|
|
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
|
-
for (
|
|
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
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const vscode = require('vscode');
|
|
6
|
+
|
|
7
|
+
const THEME_HOOK_NAMES = [
|
|
8
|
+
'head_scripts',
|
|
9
|
+
'body_start',
|
|
10
|
+
'body_end',
|
|
11
|
+
'header_actions',
|
|
12
|
+
'header_announcement_before',
|
|
13
|
+
'header_announcement_after',
|
|
14
|
+
'footer_widgets_before',
|
|
15
|
+
'footer_widgets_after',
|
|
16
|
+
'footer_bottom',
|
|
17
|
+
'login_form_top',
|
|
18
|
+
'login_form_bottom',
|
|
19
|
+
'signup_form_top',
|
|
20
|
+
'signup_form_bottom',
|
|
21
|
+
'product_price_after',
|
|
22
|
+
'product_add_to_cart_before',
|
|
23
|
+
'product_add_to_cart_after',
|
|
24
|
+
'product_description_after',
|
|
25
|
+
'product_reviews',
|
|
26
|
+
'cart_items_after',
|
|
27
|
+
'cart_drawer_bottom',
|
|
28
|
+
'cart_summary_after',
|
|
29
|
+
'checkout_payment_methods',
|
|
30
|
+
'checkout_summary_after',
|
|
31
|
+
'account_dashboard_top',
|
|
32
|
+
'account_dashboard_bottom'
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
const CANDY_TAGS = [
|
|
36
|
+
{ name: 'if', snippet: 'if ${1:condition} %}\n\t$0\n{% endif', detail: 'Conditional block ({% if %} ... {% endif %})' },
|
|
37
|
+
{ name: 'elsif', snippet: 'elsif ${1:condition}', detail: 'Else-if branch inside {% if %}' },
|
|
38
|
+
{ name: 'else', snippet: 'else', detail: 'Fallback branch inside {% if %}, {% unless %}, {% case %}, or {% for %}' },
|
|
39
|
+
{ name: 'unless', snippet: 'unless ${1:condition} %}\n\t$0\n{% endunless', detail: 'Negated conditional block ({% unless %} ... {% endunless %})' },
|
|
40
|
+
{ name: 'case', snippet: 'case ${1:variable} %}\n\t{% when ${2:value} %}\n\t\t$0\n{% endcase', detail: 'Multi-branch switch block ({% case %} ... {% endcase %})' },
|
|
41
|
+
{ name: 'when', snippet: 'when ${1:value}', detail: 'Match clause inside {% case %}' },
|
|
42
|
+
{ name: 'for', snippet: 'for ${1:item} in ${2:collection} %}\n\t$0\n{% endfor', detail: 'Loop over an array or range ({% for %} ... {% endfor %})' },
|
|
43
|
+
{ name: 'break', snippet: 'break', detail: 'Exit the current {% for %} or {% tablerow %} loop' },
|
|
44
|
+
{ name: 'continue', snippet: 'continue', detail: 'Skip to the next iteration of {% for %}' },
|
|
45
|
+
{ name: 'cycle', snippet: "cycle '${1:odd}', '${2:even}'", detail: 'Cycle through a list of strings on each loop iteration' },
|
|
46
|
+
{ name: 'tablerow', snippet: 'tablerow ${1:item} in ${2:collection} cols:${3:3} %}\n\t$0\n{% endtablerow', detail: 'Render an HTML <tr>/<td> table row loop' },
|
|
47
|
+
{ name: 'assign', snippet: 'assign ${1:name} = ${2:value}', detail: 'Assign a variable in template scope' },
|
|
48
|
+
{ name: 'capture', snippet: 'capture ${1:name} %}\n\t$0\n{% endcapture', detail: 'Capture rendered block content into a variable' },
|
|
49
|
+
{ name: 'increment', snippet: 'increment ${1:counter}', detail: 'Output and increment an independent counter' },
|
|
50
|
+
{ name: 'decrement', snippet: 'decrement ${1:counter}', detail: 'Decrement and output an independent counter' },
|
|
51
|
+
{ name: 'echo', snippet: 'echo ${1:expression}', detail: 'Output an expression inside {% liquid %} or standalone' },
|
|
52
|
+
{ name: 'liquid', snippet: 'liquid\n\t$0', detail: 'Multi-line tag-less Candy block' },
|
|
53
|
+
{ name: 'render', snippet: "render '${1:product-card}', product: ${2:item}, shop: shop, currency_info: currency_info", detail: 'Render an isolated snippet from snippets/<name>.candy' },
|
|
54
|
+
{ name: 'include', snippet: "include '${1:snippet-name}'", detail: 'Include a snippet sharing parent scope' },
|
|
55
|
+
{ name: 'hook', snippet: `hook '\${1|${THEME_HOOK_NAMES.join(',')}|}'`, detail: 'Render a Caartzee dynamic plugin hook slot (e.g. product_reviews, login_form_bottom)' },
|
|
56
|
+
{ name: 'layout', snippet: "layout '${1:theme}'", detail: 'Wrap template output in layout/<name>.candy (omit in storefront-shell mode)' },
|
|
57
|
+
{ name: 'section', snippet: "section '${1:section-name}'", detail: 'Render a section from sections/<name>.candy' },
|
|
58
|
+
{ name: 'sections', snippet: "sections '${1:header-group}'", detail: 'Render a section group' },
|
|
59
|
+
{ name: 'content_for', snippet: "content_for '${1|layout,header|}'", detail: 'Inject layout body or header scripts' },
|
|
60
|
+
{ name: 'paginate', snippet: 'paginate ${1:collection.products} by ${2:12} %}\n\t$0\n{% endpaginate', detail: 'Paginate a collection array' },
|
|
61
|
+
{ name: 'form', snippet: "form '${1|product,cart,search,contact,customer_login|}' %}\n\t$0\n{% endform", detail: 'Render a Caartzee storefront form' },
|
|
62
|
+
{ name: 'schema', snippet: 'schema %}\n{\n\t"name": "${1:Section Name}",\n\t"type": "${2:section-type}",\n\t"settings": []\n}\n{% endschema', detail: 'Define Theme Builder JSON schema for this section' },
|
|
63
|
+
{ name: 'style', snippet: 'style %}\n\t$0\n{% endstyle', detail: 'Emit an HTML <style data-candy-style> block' },
|
|
64
|
+
{ name: 'stylesheet', snippet: 'stylesheet %}\n\t$0\n{% endstylesheet', detail: 'Emit a section stylesheet block' },
|
|
65
|
+
{ name: 'javascript', snippet: 'javascript %}\n\t$0\n{% endjavascript', detail: 'Emit a <script data-candy-script> block' },
|
|
66
|
+
{ name: 'comment', snippet: 'comment %}\n\t$0\n{% endcomment', detail: 'Multi-line unrendered comment block' },
|
|
67
|
+
{ name: 'doc', snippet: 'doc %}\n\t$0\n{% enddoc', detail: 'Template documentation block (stripped from output)' },
|
|
68
|
+
{ name: 'raw', snippet: 'raw %}\n\t$0\n{% endraw', detail: 'Output raw text without evaluating Candy tags' }
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
const CANDY_FILTERS = [
|
|
72
|
+
{ name: 'escape', snippet: 'escape', detail: 'HTML-escape special characters (&, <, >, ", \')' },
|
|
73
|
+
{ name: 'escape_once', snippet: 'escape_once', detail: 'HTML-escape without double-escaping existing entities' },
|
|
74
|
+
{ name: 'safe_url', snippet: 'safe_url', detail: 'Sanitize and validate a URL for safe use in href/src attributes' },
|
|
75
|
+
{ name: 'raw', snippet: 'raw', detail: 'Mark output as trusted HTML (bypass auto-escaping)' },
|
|
76
|
+
{ name: 'money', snippet: 'money', detail: 'Format numeric price using tenant currency settings (e.g. ₹22.00, $29.99)' },
|
|
77
|
+
{ name: 'money_with_currency', snippet: 'money_with_currency', detail: 'Format price with currency code (e.g. ₹22.00 INR)' },
|
|
78
|
+
{ name: 'money_without_currency', snippet: 'money_without_currency', detail: 'Format numeric price without currency symbol (e.g. 29.99)' },
|
|
79
|
+
{ name: 'money_without_trailing_zeros', snippet: 'money_without_trailing_zeros', detail: 'Format price and strip .00 suffix' },
|
|
80
|
+
{ name: 'image_url', snippet: 'image_url: width: ${1:800}', detail: 'Generate responsive CDN image URL with width/height/crop/format' },
|
|
81
|
+
{ name: 'image_tag', snippet: "image_tag: alt: '${1:Image}'", detail: 'Render an HTML <img> tag with lazy loading' },
|
|
82
|
+
{ name: 'asset_url', snippet: 'asset_url', detail: 'Resolve theme static asset path to /_assets/<themeSlug>/<file>' },
|
|
83
|
+
{ name: 'stylesheet_tag', snippet: 'stylesheet_tag', detail: 'Wrap CSS URL in <link rel="stylesheet"> tag' },
|
|
84
|
+
{ name: 'script_tag', snippet: 'script_tag', detail: 'Wrap JS URL in <script defer> tag' },
|
|
85
|
+
{ name: 'default', snippet: "default: '${1:fallback}'", detail: 'Return fallback value when input is nil, false, or empty' },
|
|
86
|
+
{ name: 't', snippet: 't', detail: 'Translate locale key from locales/*.json' },
|
|
87
|
+
{ name: 'default_pagination', snippet: 'default_pagination', detail: 'Render accessible pagination HTML links for paginate object' },
|
|
88
|
+
{ name: 'json', snippet: 'json', detail: 'Serialize value as script-safe JSON' },
|
|
89
|
+
{ name: 'upcase', snippet: 'upcase', detail: 'Convert string to uppercase' },
|
|
90
|
+
{ name: 'downcase', snippet: 'downcase', detail: 'Convert string to lowercase' },
|
|
91
|
+
{ name: 'capitalize', snippet: 'capitalize', detail: 'Capitalize the first character of a string' },
|
|
92
|
+
{ name: 'strip', snippet: 'strip', detail: 'Trim leading and trailing whitespace' },
|
|
93
|
+
{ name: 'strip_html', snippet: 'strip_html', detail: 'Remove all HTML tags from string' },
|
|
94
|
+
{ name: 'newline_to_br', snippet: 'newline_to_br', detail: 'Convert newline characters to <br /> tags' },
|
|
95
|
+
{ name: 'truncate', snippet: 'truncate: ${1:50}', detail: 'Truncate string to N characters' },
|
|
96
|
+
{ name: 'truncatewords', snippet: 'truncatewords: ${1:15}', detail: 'Truncate string to N words' },
|
|
97
|
+
{ name: 'split', snippet: "split: '${1:,}'", detail: 'Split string into array by delimiter' },
|
|
98
|
+
{ name: 'join', snippet: "join: '${1:, }'", detail: 'Join array elements into a string' },
|
|
99
|
+
{ name: 'replace', snippet: "replace: '${1:search}', '${2:replace}'", detail: 'Replace all occurrences of a substring' },
|
|
100
|
+
{ name: 'append', snippet: "append: '${1:suffix}'", detail: 'Append string to the end' },
|
|
101
|
+
{ name: 'prepend', snippet: "prepend: '${1:prefix}'", detail: 'Prepend string to the beginning' },
|
|
102
|
+
{ name: 'handleize', snippet: 'handleize', detail: 'Convert string into URL-safe kebab-case handle' },
|
|
103
|
+
{ name: 'slugify', snippet: 'slugify', detail: 'Convert string into URL-safe slug' },
|
|
104
|
+
{ name: 'url_encode', snippet: 'url_encode', detail: 'Percent-encode URL component' },
|
|
105
|
+
{ name: 'plus', snippet: 'plus: ${1:1}', detail: 'Add number' },
|
|
106
|
+
{ name: 'minus', snippet: 'minus: ${1:1}', detail: 'Subtract number' },
|
|
107
|
+
{ name: 'times', snippet: 'times: ${1:2}', detail: 'Multiply number' },
|
|
108
|
+
{ name: 'divided_by', snippet: 'divided_by: ${1:2}', detail: 'Divide number' },
|
|
109
|
+
{ name: 'round', snippet: 'round: ${1:2}', detail: 'Round number to N decimal places' },
|
|
110
|
+
{ name: 'where', snippet: "where: '${1:property}', ${2:value}", detail: 'Filter array of objects by property value' },
|
|
111
|
+
{ name: 'where_exp', snippet: "where_exp: '${1:item}', '${1:item}.${2:available} == true'", detail: 'Filter array using a safe comparison expression' },
|
|
112
|
+
{ name: 'map', snippet: "map: '${1:property}'", detail: 'Extract property from each object in an array' },
|
|
113
|
+
{ name: 'sort', snippet: "sort: '${1:property}'", detail: 'Sort array of primitives or objects' },
|
|
114
|
+
{ name: 'size', snippet: 'size', detail: 'Return length of string or array' },
|
|
115
|
+
{ name: 'first', snippet: 'first', detail: 'Return first item of array' },
|
|
116
|
+
{ name: 'last', snippet: 'last', detail: 'Return last item of array' },
|
|
117
|
+
{ name: 'date', snippet: "date: '${1:%B %d, %Y}'", detail: 'Format date/timestamp with strftime format' },
|
|
118
|
+
{ name: 'pluralize', snippet: "pluralize: '${1:item}', '${2:items}'", detail: 'Return singular or plural label based on numeric count' }
|
|
119
|
+
];
|
|
120
|
+
|
|
121
|
+
const GLOBAL_OBJECTS = {
|
|
122
|
+
shop: ['name', 'description', 'currency', 'currency_symbol', 'currency_position', 'domain', 'url', 'logo', 'logo_url', 'email'],
|
|
123
|
+
product: ['id', 'title', 'handle', 'slug', 'description', 'price', 'compare_at_price', 'available', 'in_stock', 'inventory_quantity', 'has_inventory_variants', 'selected_or_first_available_variant', 'featured_image', 'images', 'variants', 'options', 'vendor', 'type', 'tags', 'url'],
|
|
124
|
+
collection: ['id', 'title', 'handle', 'slug', 'description', 'image', 'products', 'products_count', 'all_products_count', 'url'],
|
|
125
|
+
cart: ['item_count', 'total_price', 'subtotal_price', 'original_total_price', 'total_discount', 'currency', 'items', 'empty'],
|
|
126
|
+
section: ['id', 'type', 'settings', 'blocks', 'block_order'],
|
|
127
|
+
block: ['id', 'type', 'settings', 'candy_attributes', 'shopify_attributes'],
|
|
128
|
+
hooks: THEME_HOOK_NAMES,
|
|
129
|
+
currency_info: ['symbol', 'currency', 'currencyPosition'],
|
|
130
|
+
linklists: ["'main-menu'"],
|
|
131
|
+
mobile_sliders: ['size', 'first'],
|
|
132
|
+
wishlist: ['size', 'first'],
|
|
133
|
+
customer: ['id', 'name', 'email', 'phone'],
|
|
134
|
+
forloop: ['index', 'index0', 'rindex', 'rindex0', 'first', 'last', 'length', 'parentloop'],
|
|
135
|
+
paginate: ['current_page', 'current_offset', 'items', 'page_size', 'pages', 'parts', 'previous', 'next'],
|
|
136
|
+
request: ['path', 'page_type', 'host', 'locale', 'design_mode'],
|
|
137
|
+
routes: ['root_url', 'all_products_collection_url', 'collections_url', 'cart_url', 'cart_add_url', 'checkout_url', 'search_url', 'account_url', 'account_login_url', 'account_register_url']
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const GLOBAL_SCALARS = [
|
|
141
|
+
{ name: 'google_auth_enabled', detail: 'True when Firebase Google Sign-In is enabled for the merchant store' },
|
|
142
|
+
{ name: 'firebase_auth_enabled', detail: 'True when Firebase Auth integration is enabled for the merchant store' },
|
|
143
|
+
{ name: 'redirect_url', detail: 'Post-login/signup redirect path (default: /account)' },
|
|
144
|
+
{ name: 'is_preview', detail: 'True when rendering inside Theme Editor preview mode' }
|
|
145
|
+
];
|
|
146
|
+
|
|
147
|
+
function parseDocumentSchema(documentText) {
|
|
148
|
+
const match = /\{%-?\s*schema\s*-?%\}([\s\S]*?)\{%-?\s*endschema\s*-?%\}/.exec(documentText);
|
|
149
|
+
if (!match) {
|
|
150
|
+
return { found: false, schema: null, error: null, startOffset: 0 };
|
|
151
|
+
}
|
|
152
|
+
const jsonText = match[1];
|
|
153
|
+
const startOffset = match.index + match[0].indexOf(jsonText);
|
|
154
|
+
try {
|
|
155
|
+
const parsed = JSON.parse(jsonText);
|
|
156
|
+
return { found: true, schema: parsed, error: null, startOffset };
|
|
157
|
+
} catch (err) {
|
|
158
|
+
return { found: true, schema: null, error: err, startOffset };
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function findThemeRoot(filePath) {
|
|
163
|
+
let dir = path.dirname(filePath);
|
|
164
|
+
for (let i = 0; i < 6; i++) {
|
|
165
|
+
if (
|
|
166
|
+
fs.existsSync(path.join(dir, 'candy.config.json')) ||
|
|
167
|
+
fs.existsSync(path.join(dir, 'manifest.json'))
|
|
168
|
+
) {
|
|
169
|
+
return dir;
|
|
170
|
+
}
|
|
171
|
+
const parent = path.dirname(dir);
|
|
172
|
+
if (parent === dir) break;
|
|
173
|
+
dir = parent;
|
|
174
|
+
}
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function validateCandyDocument(document, diagnosticCollection) {
|
|
179
|
+
if (!document || !document.fileName.endsWith('.candy')) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const text = document.getText();
|
|
184
|
+
const diagnostics = [];
|
|
185
|
+
|
|
186
|
+
// 1. Validate {% schema %} JSON syntax if present
|
|
187
|
+
const schemaResult = parseDocumentSchema(text);
|
|
188
|
+
if (schemaResult.found && schemaResult.error) {
|
|
189
|
+
const pos = document.positionAt(schemaResult.startOffset);
|
|
190
|
+
const range = new vscode.Range(pos, pos.translate(0, 20));
|
|
191
|
+
diagnostics.push(
|
|
192
|
+
new vscode.Diagnostic(
|
|
193
|
+
range,
|
|
194
|
+
`Invalid Candy {% schema %} JSON: ${schemaResult.error.message}`,
|
|
195
|
+
vscode.DiagnosticSeverity.Error
|
|
196
|
+
)
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// 2. Validate balanced block tags (ignoring comment/raw/schema interiors)
|
|
201
|
+
const stripped = text
|
|
202
|
+
.replace(/\{%-?\s*comment\s*-?%\}[\s\S]*?\{%-?\s*endcomment\s*-?%\}/g, (m) => ' '.repeat(m.length))
|
|
203
|
+
.replace(/\{%-?\s*raw\s*-?%\}[\s\S]*?\{%-?\s*endraw\s*-?%\}/g, (m) => ' '.repeat(m.length))
|
|
204
|
+
.replace(/\{%-?\s*doc\s*-?%\}[\s\S]*?\{%-?\s*enddoc\s*-?%\}/g, (m) => ' '.repeat(m.length))
|
|
205
|
+
.replace(/\{%-?\s*schema\s*-?%\}[\s\S]*?\{%-?\s*endschema\s*-?%\}/g, (m) => ' '.repeat(m.length));
|
|
206
|
+
|
|
207
|
+
const blockPairs = {
|
|
208
|
+
if: 'endif',
|
|
209
|
+
unless: 'endunless',
|
|
210
|
+
case: 'endcase',
|
|
211
|
+
for: 'endfor',
|
|
212
|
+
tablerow: 'endtablerow',
|
|
213
|
+
capture: 'endcapture',
|
|
214
|
+
paginate: 'endpaginate',
|
|
215
|
+
form: 'endform',
|
|
216
|
+
style: 'endstyle',
|
|
217
|
+
stylesheet: 'endstylesheet',
|
|
218
|
+
javascript: 'endjavascript'
|
|
219
|
+
};
|
|
220
|
+
const closeToOpen = {};
|
|
221
|
+
for (const [open, close] of Object.entries(blockPairs)) {
|
|
222
|
+
closeToOpen[close] = open;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const stack = [];
|
|
226
|
+
const tagRegex = /\{%-?\s*([a-zA-Z_][a-zA-Z0-9_]*)\b[\s\S]*?-?%\}/g;
|
|
227
|
+
let match;
|
|
228
|
+
while ((match = tagRegex.exec(stripped)) !== null) {
|
|
229
|
+
const tagName = match[1];
|
|
230
|
+
if (blockPairs[tagName]) {
|
|
231
|
+
stack.push({ tagName, index: match.index, length: match[0].length });
|
|
232
|
+
} else if (closeToOpen[tagName]) {
|
|
233
|
+
const expectedOpen = closeToOpen[tagName];
|
|
234
|
+
const top = stack[stack.length - 1];
|
|
235
|
+
if (!top || top.tagName !== expectedOpen) {
|
|
236
|
+
const startPos = document.positionAt(match.index);
|
|
237
|
+
const endPos = document.positionAt(match.index + match[0].length);
|
|
238
|
+
diagnostics.push(
|
|
239
|
+
new vscode.Diagnostic(
|
|
240
|
+
new vscode.Range(startPos, endPos),
|
|
241
|
+
`Unexpected {% ${tagName} %} without matching {% ${expectedOpen} %}.`,
|
|
242
|
+
vscode.DiagnosticSeverity.Error
|
|
243
|
+
)
|
|
244
|
+
);
|
|
245
|
+
} else {
|
|
246
|
+
stack.pop();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
for (const unclosed of stack) {
|
|
252
|
+
const startPos = document.positionAt(unclosed.index);
|
|
253
|
+
const endPos = document.positionAt(unclosed.index + unclosed.length);
|
|
254
|
+
diagnostics.push(
|
|
255
|
+
new vscode.Diagnostic(
|
|
256
|
+
new vscode.Range(startPos, endPos),
|
|
257
|
+
`Unclosed {% ${unclosed.tagName} %} block (expected {% ${blockPairs[unclosed.tagName]} %}).`,
|
|
258
|
+
vscode.DiagnosticSeverity.Error
|
|
259
|
+
)
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// 3. Caartzee Storefront Readiness Diagnostics
|
|
264
|
+
// 3a. Flag synthetic "-default" variant IDs that cause POST /_api/cart 422 errors
|
|
265
|
+
const badVariantRegex = /data-variant-id\s*=\s*["'][^"']*-default["']/gi;
|
|
266
|
+
while ((match = badVariantRegex.exec(stripped)) !== null) {
|
|
267
|
+
diagnostics.push(
|
|
268
|
+
new vscode.Diagnostic(
|
|
269
|
+
new vscode.Range(document.positionAt(match.index), document.positionAt(match.index + match[0].length)),
|
|
270
|
+
'Avoid synthetic "-default" variant IDs — POST /_api/cart rejects non-numeric strings with 422. Guard data-variant-id with {% if product.has_inventory_variants %}.',
|
|
271
|
+
vscode.DiagnosticSeverity.Error
|
|
272
|
+
)
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// 3b. Warn if templates/*.candy uses {% layout %} when storefront-shell is enabled
|
|
277
|
+
const normalizedFile = document.fileName.replace(/\\/g, '/');
|
|
278
|
+
if (/\/templates\/[^/]+\.candy$/i.test(normalizedFile)) {
|
|
279
|
+
const layoutMatch = /\{%-?\s*layout\s+[^%]+-?%\}/i.exec(stripped);
|
|
280
|
+
if (layoutMatch) {
|
|
281
|
+
diagnostics.push(
|
|
282
|
+
new vscode.Diagnostic(
|
|
283
|
+
new vscode.Range(document.positionAt(layoutMatch.index), document.positionAt(layoutMatch.index + layoutMatch[0].length)),
|
|
284
|
+
'Storefront Shell themes must keep templates/*.candy as body fragments (omit {% layout %} to avoid duplicate headers/footers).',
|
|
285
|
+
vscode.DiagnosticSeverity.Warning
|
|
286
|
+
)
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// 3c. Warn if {% render 'product-card' %} or {% render 'price' %} omits currency_info / shop
|
|
292
|
+
const renderPriceRegex = /\{%-?\s*render\s+['"](?:product-card|price)['"]([^%]*)-?%\}/gi;
|
|
293
|
+
while ((match = renderPriceRegex.exec(stripped)) !== null) {
|
|
294
|
+
const argsStr = match[1] || '';
|
|
295
|
+
if (!/\bcurrency_info\b/.test(argsStr) && !/\bshop\b/.test(argsStr)) {
|
|
296
|
+
diagnostics.push(
|
|
297
|
+
new vscode.Diagnostic(
|
|
298
|
+
new vscode.Range(document.positionAt(match.index), document.positionAt(match.index + match[0].length)),
|
|
299
|
+
'Pass "shop: shop, currency_info: currency_info" into {% render %} snippets so {{ price | money }} uses the merchant\'s live currency.',
|
|
300
|
+
vscode.DiagnosticSeverity.Warning
|
|
301
|
+
)
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
diagnosticCollection.set(document.uri, diagnostics);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function runCandyCliCommand(subcommand) {
|
|
310
|
+
const editor = vscode.window.activeTextEditor;
|
|
311
|
+
const activeFile = editor?.document?.fileName;
|
|
312
|
+
const themeRoot =
|
|
313
|
+
(activeFile ? findThemeRoot(activeFile) : null) ||
|
|
314
|
+
vscode.workspace.workspaceFolders?.[0]?.uri?.fsPath ||
|
|
315
|
+
process.cwd();
|
|
316
|
+
|
|
317
|
+
const terminal = vscode.window.createTerminal({
|
|
318
|
+
name: 'Caartzee Candy CLI',
|
|
319
|
+
cwd: themeRoot
|
|
320
|
+
});
|
|
321
|
+
terminal.show(true);
|
|
322
|
+
terminal.sendText(`npx caartzee-theme ${subcommand}`);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const HTML_VOID_TAGS = new Set([
|
|
326
|
+
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
|
|
327
|
+
'link', 'meta', 'param', 'source', 'track', 'wbr', 'path',
|
|
328
|
+
'circle', 'line', 'rect', 'polygon', 'polyline', 'ellipse'
|
|
329
|
+
]);
|
|
330
|
+
|
|
331
|
+
const CANDY_OPEN_TAGS = new Set([
|
|
332
|
+
'if', 'unless', 'case', 'for', 'tablerow', 'capture',
|
|
333
|
+
'paginate', 'form', 'style', 'stylesheet', 'javascript'
|
|
334
|
+
]);
|
|
335
|
+
|
|
336
|
+
const CANDY_MID_TAGS = new Set(['else', 'elsif', 'when']);
|
|
337
|
+
|
|
338
|
+
const CANDY_CLOSE_TAGS = new Set([
|
|
339
|
+
'endif', 'endunless', 'endcase', 'endfor', 'endtablerow',
|
|
340
|
+
'endcapture', 'endpaginate', 'endform', 'endstyle', 'endstylesheet', 'endjavascript'
|
|
341
|
+
]);
|
|
342
|
+
|
|
343
|
+
function formatCandyDocument(text, options = {}) {
|
|
344
|
+
const indentUnit = options.insertSpaces === false
|
|
345
|
+
? '\t'
|
|
346
|
+
: ' '.repeat(typeof options.tabSize === 'number' && options.tabSize > 0 ? options.tabSize : 2);
|
|
347
|
+
const eol = text.includes('\r\n') ? '\r\n' : '\n';
|
|
348
|
+
|
|
349
|
+
// Extract {% schema %}...{% endschema %} and pretty-print valid JSON
|
|
350
|
+
const preservedBlocks = [];
|
|
351
|
+
const withPlaceholders = text.replace(
|
|
352
|
+
/(\{%-?\s*(schema|raw|comment|doc)\s*-?%\})([\s\S]*?)(\{%-?\s*end\2\s*-?%\})/gi,
|
|
353
|
+
(full, openTag, tagKind, inner, closeTag) => {
|
|
354
|
+
let formattedBlock = full;
|
|
355
|
+
if (tagKind.toLowerCase() === 'schema') {
|
|
356
|
+
try {
|
|
357
|
+
const parsed = JSON.parse(inner);
|
|
358
|
+
const pretty = JSON.stringify(parsed, null, indentUnit).replace(/\n/g, eol);
|
|
359
|
+
formattedBlock = `${openTag}${eol}${pretty}${eol}${closeTag}`;
|
|
360
|
+
} catch {
|
|
361
|
+
formattedBlock = full;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
const token = `__CANDY_PRESERVED_BLOCK_${preservedBlocks.length}__`;
|
|
365
|
+
preservedBlocks.push(formattedBlock);
|
|
366
|
+
return token;
|
|
367
|
+
}
|
|
368
|
+
);
|
|
369
|
+
|
|
370
|
+
const rawLines = withPlaceholders.split(/\r?\n/);
|
|
371
|
+
const outputLines = [];
|
|
372
|
+
let indentLevel = 0;
|
|
373
|
+
let consecutiveBlanks = 0;
|
|
374
|
+
let inPreformattedTag = false;
|
|
375
|
+
|
|
376
|
+
for (let i = 0; i < rawLines.length; i++) {
|
|
377
|
+
const rawLine = rawLines[i];
|
|
378
|
+
const trimmed = rawLine.trim();
|
|
379
|
+
|
|
380
|
+
if (trimmed === '') {
|
|
381
|
+
consecutiveBlanks++;
|
|
382
|
+
if (consecutiveBlanks <= 1 && outputLines.length > 0) {
|
|
383
|
+
outputLines.push('');
|
|
384
|
+
}
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
consecutiveBlanks = 0;
|
|
388
|
+
|
|
389
|
+
if (/^__CANDY_PRESERVED_BLOCK_\d+__$/.test(trimmed)) {
|
|
390
|
+
outputLines.push(trimmed);
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (inPreformattedTag) {
|
|
395
|
+
outputLines.push(rawLine.replace(/[ \t]+$/, ''));
|
|
396
|
+
if (/<\/(?:pre|textarea|script|style)\b/i.test(trimmed)) {
|
|
397
|
+
inPreformattedTag = false;
|
|
398
|
+
}
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// Check leading Candy tag for dedent
|
|
403
|
+
const leadingCandyMatch = /^\{%-?\s*([a-zA-Z_][a-zA-Z0-9_]*)\b/.exec(trimmed);
|
|
404
|
+
const leadingCandyTag = leadingCandyMatch ? leadingCandyMatch[1].toLowerCase() : null;
|
|
405
|
+
const isLeadingHtmlClose = /^<\//.test(trimmed);
|
|
406
|
+
|
|
407
|
+
if (
|
|
408
|
+
(leadingCandyTag && (CANDY_CLOSE_TAGS.has(leadingCandyTag) || CANDY_MID_TAGS.has(leadingCandyTag))) ||
|
|
409
|
+
isLeadingHtmlClose
|
|
410
|
+
) {
|
|
411
|
+
indentLevel = Math.max(0, indentLevel - 1);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// Normalize spacing inside simple {% tag %} and {{ expr }} delimiters
|
|
415
|
+
const normalized = trimmed
|
|
416
|
+
.replace(/\{%(-?)\s*([^%\s][^%]*?)\s*(-?)%\}/g, '{%$1 $2 $3%}')
|
|
417
|
+
.replace(/\{\{(-?)\s*([^}\s][^}]*?)\s*(-?)\}\}/g, '{{$1 $2 $3}}');
|
|
418
|
+
|
|
419
|
+
outputLines.push(indentUnit.repeat(indentLevel) + normalized);
|
|
420
|
+
|
|
421
|
+
// Track preformatted HTML tags that open without closing on the same line
|
|
422
|
+
const openPre = /<(pre|textarea|script|style)\b[^>]*>/i.exec(trimmed);
|
|
423
|
+
if (openPre && !new RegExp(`</${openPre[1]}\\b`, 'i').test(trimmed)) {
|
|
424
|
+
inPreformattedTag = true;
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// Calculate net indent delta for subsequent lines
|
|
429
|
+
let delta = 0;
|
|
430
|
+
const candyTagRe = /\{%-?\s*([a-zA-Z_][a-zA-Z0-9_]*)\b[\s\S]*?-?%\}/g;
|
|
431
|
+
let m;
|
|
432
|
+
while ((m = candyTagRe.exec(trimmed)) !== null) {
|
|
433
|
+
const t = m[1].toLowerCase();
|
|
434
|
+
if (CANDY_OPEN_TAGS.has(t)) delta++;
|
|
435
|
+
else if (CANDY_CLOSE_TAGS.has(t)) delta--;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// Strip Candy tags before counting HTML open/close tags on this line
|
|
439
|
+
const htmlOnly = trimmed.replace(/\{%[\s\S]*?%\}/g, '').replace(/\{\{[\s\S]*?\}\}/g, '');
|
|
440
|
+
const htmlTagRe = /<(\/?)([a-zA-Z][a-zA-Z0-9:-]*)\b([^>]*)>/g;
|
|
441
|
+
while ((m = htmlTagRe.exec(htmlOnly)) !== null) {
|
|
442
|
+
const isClose = m[1] === '/';
|
|
443
|
+
const tagName = m[2].toLowerCase();
|
|
444
|
+
const attrs = m[3] || '';
|
|
445
|
+
const isSelfClosing = attrs.trim().endsWith('/') || HTML_VOID_TAGS.has(tagName);
|
|
446
|
+
if (isSelfClosing) continue;
|
|
447
|
+
if (isClose) delta--;
|
|
448
|
+
else delta++;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// Adjust because we already dedented leading mid/close tags above
|
|
452
|
+
if (leadingCandyTag && CANDY_MID_TAGS.has(leadingCandyTag)) {
|
|
453
|
+
delta += 1;
|
|
454
|
+
} else if ((leadingCandyTag && CANDY_CLOSE_TAGS.has(leadingCandyTag)) || isLeadingHtmlClose) {
|
|
455
|
+
delta += 1;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
indentLevel = Math.max(0, indentLevel + delta);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// Trim trailing blank lines
|
|
462
|
+
while (outputLines.length > 0 && outputLines[outputLines.length - 1] === '') {
|
|
463
|
+
outputLines.pop();
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
let result = outputLines.join(eol) + eol;
|
|
467
|
+
for (let i = 0; i < preservedBlocks.length; i++) {
|
|
468
|
+
result = result.replace(`__CANDY_PRESERVED_BLOCK_${i}__`, preservedBlocks[i]);
|
|
469
|
+
}
|
|
470
|
+
return result;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function activate(context) {
|
|
474
|
+
const diagnosticCollection = vscode.languages.createDiagnosticCollection('candy');
|
|
475
|
+
context.subscriptions.push(diagnosticCollection);
|
|
476
|
+
|
|
477
|
+
if (vscode.window.activeTextEditor) {
|
|
478
|
+
validateCandyDocument(vscode.window.activeTextEditor.document, diagnosticCollection);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
context.subscriptions.push(
|
|
482
|
+
vscode.workspace.onDidOpenTextDocument((doc) => validateCandyDocument(doc, diagnosticCollection)),
|
|
483
|
+
vscode.workspace.onDidChangeTextDocument((e) => validateCandyDocument(e.document, diagnosticCollection)),
|
|
484
|
+
vscode.workspace.onDidCloseTextDocument((doc) => diagnosticCollection.delete(doc.uri))
|
|
485
|
+
);
|
|
486
|
+
|
|
487
|
+
// 1. Completion Provider for Candy tags, filters, hooks, objects, and dynamic {% schema %} settings
|
|
488
|
+
const completionProvider = vscode.languages.registerCompletionItemProvider(
|
|
489
|
+
'candy',
|
|
490
|
+
{
|
|
491
|
+
provideCompletionItems(document, position) {
|
|
492
|
+
const linePrefix = document.lineAt(position).text.slice(0, position.character);
|
|
493
|
+
|
|
494
|
+
// A. After `|` -> Suggest Candy Filters
|
|
495
|
+
if (/\|\s*[a-zA-Z_]*$/.test(linePrefix)) {
|
|
496
|
+
return CANDY_FILTERS.map((f) => {
|
|
497
|
+
const item = new vscode.CompletionItem(f.name, vscode.CompletionItemKind.Function);
|
|
498
|
+
item.insertText = new vscode.SnippetString(f.snippet);
|
|
499
|
+
item.detail = `(Candy Filter) ${f.detail}`;
|
|
500
|
+
item.documentation = new vscode.MarkdownString(`**\`| ${f.name}\`**\n\n${f.detail}`);
|
|
501
|
+
return item;
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// B. Inside `{% hook '` -> Suggest ThemeHookName slots
|
|
506
|
+
if (/\{%-?\s*hook\s+['"][a-zA-Z0-9_]*$/.test(linePrefix)) {
|
|
507
|
+
return THEME_HOOK_NAMES.map((hookName) => {
|
|
508
|
+
const item = new vscode.CompletionItem(hookName, vscode.CompletionItemKind.Event);
|
|
509
|
+
item.detail = `(Caartzee Hook) Dynamic plugin slot "${hookName}"`;
|
|
510
|
+
return item;
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// C. After `{%` or `{%-` -> Suggest Candy Tags
|
|
515
|
+
if (/\{%-?\s*[a-zA-Z_]*$/.test(linePrefix)) {
|
|
516
|
+
return CANDY_TAGS.map((t) => {
|
|
517
|
+
const item = new vscode.CompletionItem(t.name, vscode.CompletionItemKind.Keyword);
|
|
518
|
+
item.insertText = new vscode.SnippetString(t.snippet);
|
|
519
|
+
item.detail = `(Candy Tag) ${t.detail}`;
|
|
520
|
+
item.documentation = new vscode.MarkdownString(`**\`{% ${t.name} %}\`**\n\n${t.detail}`);
|
|
521
|
+
return item;
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// D. After `section.settings.` -> Suggest settings from current file's {% schema %}
|
|
526
|
+
if (/section\.settings\.[a-zA-Z0-9_-]*$/.test(linePrefix)) {
|
|
527
|
+
const { schema } = parseDocumentSchema(document.getText());
|
|
528
|
+
const settings = Array.isArray(schema?.settings) ? schema.settings : [];
|
|
529
|
+
return settings
|
|
530
|
+
.filter((s) => s && typeof s.id === 'string')
|
|
531
|
+
.map((s) => {
|
|
532
|
+
const item = new vscode.CompletionItem(s.id, vscode.CompletionItemKind.Property);
|
|
533
|
+
item.detail = `(${s.type || 'setting'}) ${s.label || s.id}`;
|
|
534
|
+
item.documentation = new vscode.MarkdownString(
|
|
535
|
+
`**Section Setting \`${s.id}\`**\n- **Type**: \`${s.type}\`\n- **Label**: ${s.label || s.id}\n- **Default**: \`${JSON.stringify(s.default ?? null)}\``
|
|
536
|
+
);
|
|
537
|
+
return item;
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// E. After `<object>.` -> Suggest known properties
|
|
542
|
+
const objMatch = /\b(shop|product|collection|cart|section|block|hooks|currency_info|linklists|mobile_sliders|wishlist|customer|forloop|paginate|request|routes)\.[a-zA-Z0-9_]*$/.exec(
|
|
543
|
+
linePrefix
|
|
544
|
+
);
|
|
545
|
+
if (objMatch) {
|
|
546
|
+
const objName = objMatch[1];
|
|
547
|
+
const props = GLOBAL_OBJECTS[objName] || [];
|
|
548
|
+
return props.map((prop) => {
|
|
549
|
+
const item = new vscode.CompletionItem(prop, vscode.CompletionItemKind.Field);
|
|
550
|
+
item.detail = `${objName}.${prop}`;
|
|
551
|
+
return item;
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
// F. Inside `{{ ...` or `{% ...` -> Suggest global objects & scalar flags
|
|
556
|
+
if (/(\{\{-?|\{%-?)[^}%]*$/.test(linePrefix)) {
|
|
557
|
+
const objectItems = Object.keys(GLOBAL_OBJECTS).map((objName) => {
|
|
558
|
+
const item = new vscode.CompletionItem(objName, vscode.CompletionItemKind.Variable);
|
|
559
|
+
item.detail = `Caartzee Candy global object (${objName})`;
|
|
560
|
+
return item;
|
|
561
|
+
});
|
|
562
|
+
const scalarItems = GLOBAL_SCALARS.map((s) => {
|
|
563
|
+
const item = new vscode.CompletionItem(s.name, vscode.CompletionItemKind.Constant);
|
|
564
|
+
item.detail = s.detail;
|
|
565
|
+
return item;
|
|
566
|
+
});
|
|
567
|
+
return [...objectItems, ...scalarItems];
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
return undefined;
|
|
571
|
+
}
|
|
572
|
+
},
|
|
573
|
+
'|',
|
|
574
|
+
'.',
|
|
575
|
+
'%',
|
|
576
|
+
"'",
|
|
577
|
+
'"',
|
|
578
|
+
' '
|
|
579
|
+
);
|
|
580
|
+
|
|
581
|
+
// 2. Hover Provider for Candy tags, filters, hooks, and objects
|
|
582
|
+
const hoverProvider = vscode.languages.registerHoverProvider('candy', {
|
|
583
|
+
provideHover(document, position) {
|
|
584
|
+
const range = document.getWordRangeAtPosition(position, /[a-zA-Z_][a-zA-Z0-9_-]*/);
|
|
585
|
+
if (!range) return null;
|
|
586
|
+
const word = document.getText(range);
|
|
587
|
+
|
|
588
|
+
if (THEME_HOOK_NAMES.includes(word)) {
|
|
589
|
+
return new vscode.Hover(
|
|
590
|
+
new vscode.MarkdownString(
|
|
591
|
+
`### Caartzee Dynamic Hook \`${word}\`\n\nRenders installed merchant plugins targeting \`${word}\`. Use \`{{ hooks.${word} }}\` or \`{% hook '${word}' %}\`.`
|
|
592
|
+
)
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
const scalar = GLOBAL_SCALARS.find((s) => s.name === word);
|
|
597
|
+
if (scalar) {
|
|
598
|
+
return new vscode.Hover(
|
|
599
|
+
new vscode.MarkdownString(`### Candy Context \`${scalar.name}\`\n\n${scalar.detail}`)
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
const filter = CANDY_FILTERS.find((f) => f.name === word);
|
|
604
|
+
if (filter) {
|
|
605
|
+
return new vscode.Hover(
|
|
606
|
+
new vscode.MarkdownString(`### Candy Filter \`| ${filter.name}\`\n\n${filter.detail}`)
|
|
607
|
+
);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
const tag = CANDY_TAGS.find((t) => t.name === word);
|
|
611
|
+
if (tag) {
|
|
612
|
+
return new vscode.Hover(
|
|
613
|
+
new vscode.MarkdownString(`### Candy Tag \`{% ${tag.name} %}\`\n\n${tag.detail}`)
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
if (GLOBAL_OBJECTS[word]) {
|
|
618
|
+
return new vscode.Hover(
|
|
619
|
+
new vscode.MarkdownString(
|
|
620
|
+
`### Candy Object \`${word}\`\n\nAvailable properties: \`${GLOBAL_OBJECTS[word].join('`, `')}\``
|
|
621
|
+
)
|
|
622
|
+
);
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
return null;
|
|
626
|
+
}
|
|
627
|
+
});
|
|
628
|
+
|
|
629
|
+
// 3. Go-to-Definition for {% render 'snippet' %}, {% include 'snippet' %}, and {% section 'section' %}
|
|
630
|
+
const definitionProvider = vscode.languages.registerDefinitionProvider('candy', {
|
|
631
|
+
provideDefinition(document, position) {
|
|
632
|
+
const lineText = document.lineAt(position.line).text;
|
|
633
|
+
const refRegex = /\{%-?\s*(render|include|section)\s+['"]([a-zA-Z0-9/_-]+)['"]/g;
|
|
634
|
+
let m;
|
|
635
|
+
while ((m = refRegex.exec(lineText)) !== null) {
|
|
636
|
+
const startCol = m.index;
|
|
637
|
+
const endCol = m.index + m[0].length;
|
|
638
|
+
if (position.character >= startCol && position.character <= endCol) {
|
|
639
|
+
const kind = m[1];
|
|
640
|
+
const targetName = m[2];
|
|
641
|
+
const themeRoot = findThemeRoot(document.fileName);
|
|
642
|
+
if (!themeRoot) return null;
|
|
643
|
+
const folder = kind === 'section' ? 'sections' : 'snippets';
|
|
644
|
+
const candidate = path.join(themeRoot, folder, targetName.endsWith('.candy') ? targetName : `${targetName}.candy`);
|
|
645
|
+
if (fs.existsSync(candidate)) {
|
|
646
|
+
return new vscode.Location(vscode.Uri.file(candidate), new vscode.Position(0, 0));
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
return null;
|
|
651
|
+
}
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
// 4. Document Formatter (Shift+Alt+F / Format on Save)
|
|
655
|
+
const formattingProvider = vscode.languages.registerDocumentFormattingEditProvider('candy', {
|
|
656
|
+
provideDocumentFormattingEdits(document, options) {
|
|
657
|
+
const original = document.getText();
|
|
658
|
+
const formatted = formatCandyDocument(original, options);
|
|
659
|
+
if (formatted === original) return [];
|
|
660
|
+
const fullRange = new vscode.Range(
|
|
661
|
+
document.positionAt(0),
|
|
662
|
+
document.positionAt(original.length)
|
|
663
|
+
);
|
|
664
|
+
return [vscode.TextEdit.replace(fullRange, formatted)];
|
|
665
|
+
}
|
|
666
|
+
});
|
|
667
|
+
|
|
668
|
+
// 5. Command Palette CLI Shortcuts
|
|
669
|
+
const commands = [
|
|
670
|
+
vscode.commands.registerCommand('candy.dev', () => runCandyCliCommand('dev --port 3333')),
|
|
671
|
+
vscode.commands.registerCommand('candy.doctor', () => runCandyCliCommand('doctor .')),
|
|
672
|
+
vscode.commands.registerCommand('candy.syncManifest', () => runCandyCliCommand('sync-manifest .')),
|
|
673
|
+
vscode.commands.registerCommand('candy.validate', () => runCandyCliCommand('validate .')),
|
|
674
|
+
vscode.commands.registerCommand('candy.build', () => runCandyCliCommand('build .')),
|
|
675
|
+
vscode.commands.registerCommand('candy.push', () => runCandyCliCommand('push .'))
|
|
676
|
+
];
|
|
677
|
+
|
|
678
|
+
context.subscriptions.push(
|
|
679
|
+
completionProvider,
|
|
680
|
+
hoverProvider,
|
|
681
|
+
definitionProvider,
|
|
682
|
+
formattingProvider,
|
|
683
|
+
...commands
|
|
684
|
+
);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
function deactivate() {}
|
|
688
|
+
|
|
689
|
+
module.exports = { activate, deactivate, formatCandyDocument };
|