@moondraft/import 0.0.0-stage → 0.2.0

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.
Files changed (337) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/LICENSE +34 -0
  3. package/README.md +36 -2
  4. package/SOURCE.md +394 -0
  5. package/dist/adapter/convert.d.ts +144 -0
  6. package/dist/adapter/convert.js +2811 -0
  7. package/dist/adapter/figma-descriptions.d.ts +7 -0
  8. package/dist/adapter/figma-descriptions.js +34 -0
  9. package/dist/adapter/figma-grids.d.ts +12 -0
  10. package/dist/adapter/figma-grids.js +175 -0
  11. package/dist/adapter/figma-handoff.d.ts +12 -0
  12. package/dist/adapter/figma-handoff.js +167 -0
  13. package/dist/adapter/figma-hyperlinks.d.ts +13 -0
  14. package/dist/adapter/figma-hyperlinks.js +77 -0
  15. package/dist/adapter/figma.d.ts +12 -0
  16. package/dist/adapter/figma.js +40 -0
  17. package/dist/adapter/file-components.d.ts +20 -0
  18. package/dist/adapter/file-components.js +352 -0
  19. package/dist/adapter/file-resources.d.ts +29 -0
  20. package/dist/adapter/file-resources.js +851 -0
  21. package/dist/adapter/image-crop.d.ts +18 -0
  22. package/dist/adapter/image-crop.js +30 -0
  23. package/dist/adapter/index.d.ts +6 -0
  24. package/dist/adapter/index.js +9 -0
  25. package/dist/adapter/link.d.ts +49 -0
  26. package/dist/adapter/link.js +892 -0
  27. package/dist/adapter/payload.d.ts +31 -0
  28. package/dist/adapter/payload.js +78 -0
  29. package/dist/adapter/shapes.d.ts +31 -0
  30. package/dist/adapter/shapes.js +75 -0
  31. package/dist/adapter/variables.d.ts +45 -0
  32. package/dist/adapter/variables.js +106 -0
  33. package/dist/figma-file.d.ts +38 -0
  34. package/dist/figma-file.js +97 -0
  35. package/dist/figma-worker.d.ts +11 -0
  36. package/dist/figma-worker.js +12 -0
  37. package/dist/figma.d.ts +26 -0
  38. package/dist/figma.js +230 -0
  39. package/dist/index.d.ts +29 -0
  40. package/dist/index.js +50 -0
  41. package/dist/report.d.ts +42 -0
  42. package/dist/report.js +142 -0
  43. package/dist/serialize.d.ts +7 -0
  44. package/dist/serialize.js +79 -0
  45. package/dist/svg.d.ts +33 -0
  46. package/dist/svg.js +44 -0
  47. package/dist/syntax.d.ts +8 -0
  48. package/dist/syntax.js +19 -0
  49. package/dist/taco/figma-import/clipboard.d.ts +25 -0
  50. package/dist/taco/figma-import/clipboard.js +87 -0
  51. package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
  52. package/dist/taco/figma-import/clipboardSize.js +107 -0
  53. package/dist/taco/figma-import/container/compression.d.ts +9 -0
  54. package/dist/taco/figma-import/container/compression.js +174 -0
  55. package/dist/taco/figma-import/container/error.d.ts +15 -0
  56. package/dist/taco/figma-import/container/error.js +19 -0
  57. package/dist/taco/figma-import/container/kiwiContainer.d.ts +41 -0
  58. package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
  59. package/dist/taco/figma-import/container/limits.d.ts +22 -0
  60. package/dist/taco/figma-import/container/limits.js +22 -0
  61. package/dist/taco/figma-import/container/zip.d.ts +14 -0
  62. package/dist/taco/figma-import/container/zip.js +154 -0
  63. package/dist/taco/figma-import/figFile.d.ts +34 -0
  64. package/dist/taco/figma-import/figFile.js +91 -0
  65. package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
  66. package/dist/taco/figma-import/figFileDocument.js +100 -0
  67. package/dist/taco/figma-import/import/blobs.d.ts +10 -0
  68. package/dist/taco/figma-import/import/blobs.js +23 -0
  69. package/dist/taco/figma-import/import/carry.d.ts +24 -0
  70. package/dist/taco/figma-import/import/carry.js +66 -0
  71. package/dist/taco/figma-import/import/componentProps.d.ts +90 -0
  72. package/dist/taco/figma-import/import/componentProps.js +329 -0
  73. package/dist/taco/figma-import/import/componentSets.d.ts +85 -0
  74. package/dist/taco/figma-import/import/componentSets.js +647 -0
  75. package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
  76. package/dist/taco/figma-import/import/converters/effects.js +127 -0
  77. package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
  78. package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
  79. package/dist/taco/figma-import/import/converters/exportSettings.d.ts +4 -0
  80. package/dist/taco/figma-import/import/converters/exportSettings.js +49 -0
  81. package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
  82. package/dist/taco/figma-import/import/converters/frame.js +63 -0
  83. package/dist/taco/figma-import/import/converters/gradient.d.ts +74 -0
  84. package/dist/taco/figma-import/import/converters/gradient.js +108 -0
  85. package/dist/taco/figma-import/import/converters/group.d.ts +19 -0
  86. package/dist/taco/figma-import/import/converters/group.js +30 -0
  87. package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
  88. package/dist/taco/figma-import/import/converters/image.js +94 -0
  89. package/dist/taco/figma-import/import/converters/layout.d.ts +33 -0
  90. package/dist/taco/figma-import/import/converters/layout.js +187 -0
  91. package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
  92. package/dist/taco/figma-import/import/converters/line.js +40 -0
  93. package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
  94. package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
  95. package/dist/taco/figma-import/import/converters/section.d.ts +8 -0
  96. package/dist/taco/figma-import/import/converters/section.js +29 -0
  97. package/dist/taco/figma-import/import/converters/shared.d.ts +71 -0
  98. package/dist/taco/figma-import/import/converters/shared.js +372 -0
  99. package/dist/taco/figma-import/import/converters/text.d.ts +54 -0
  100. package/dist/taco/figma-import/import/converters/text.js +707 -0
  101. package/dist/taco/figma-import/import/converters/vector.d.ts +105 -0
  102. package/dist/taco/figma-import/import/converters/vector.js +609 -0
  103. package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +114 -0
  104. package/dist/taco/figma-import/import/converters/vectorNetwork.js +503 -0
  105. package/dist/taco/figma-import/import/fields.d.ts +15 -0
  106. package/dist/taco/figma-import/import/fields.js +29 -0
  107. package/dist/taco/figma-import/import/instances.d.ts +184 -0
  108. package/dist/taco/figma-import/import/instances.js +1530 -0
  109. package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
  110. package/dist/taco/figma-import/import/nestingCycles.js +488 -0
  111. package/dist/taco/figma-import/import/slots.d.ts +56 -0
  112. package/dist/taco/figma-import/import/slots.js +96 -0
  113. package/dist/taco/figma-import/import/styles.d.ts +48 -0
  114. package/dist/taco/figma-import/import/styles.js +48 -0
  115. package/dist/taco/figma-import/import/tree.d.ts +66 -0
  116. package/dist/taco/figma-import/import/tree.js +194 -0
  117. package/dist/taco/figma-import/import/types.d.ts +232 -0
  118. package/dist/taco/figma-import/import/types.js +33 -0
  119. package/dist/taco/figma-import/import/variables.d.ts +137 -0
  120. package/dist/taco/figma-import/import/variables.js +769 -0
  121. package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
  122. package/dist/taco/figma-import/import/variantTrees.js +520 -0
  123. package/dist/taco/figma-import/import/walk.d.ts +207 -0
  124. package/dist/taco/figma-import/import/walk.js +1464 -0
  125. package/dist/taco/figma-import/index.d.ts +22 -0
  126. package/dist/taco/figma-import/index.js +13 -0
  127. package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
  128. package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
  129. package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
  130. package/dist/taco/figma-import/kiwi/error.js +29 -0
  131. package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
  132. package/dist/taco/figma-import/kiwi/limits.js +18 -0
  133. package/dist/taco/figma-import/kiwi/message.d.ts +12 -0
  134. package/dist/taco/figma-import/kiwi/message.js +147 -0
  135. package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
  136. package/dist/taco/figma-import/kiwi/schema.js +206 -0
  137. package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
  138. package/dist/taco/figma-import/kiwi/types.js +18 -0
  139. package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
  140. package/dist/taco/model/agent-hidden-fields.js +29 -0
  141. package/dist/taco/model/asset-users.d.ts +23 -0
  142. package/dist/taco/model/asset-users.js +128 -0
  143. package/dist/taco/model/auto-layout-lines.d.ts +32 -0
  144. package/dist/taco/model/auto-layout-lines.js +70 -0
  145. package/dist/taco/model/codec.d.ts +444 -0
  146. package/dist/taco/model/codec.js +5665 -0
  147. package/dist/taco/model/color.d.ts +11 -0
  148. package/dist/taco/model/color.js +61 -0
  149. package/dist/taco/model/comment-operations.d.ts +102 -0
  150. package/dist/taco/model/comment-operations.js +227 -0
  151. package/dist/taco/model/component-evaluation.d.ts +96 -0
  152. package/dist/taco/model/component-evaluation.js +555 -0
  153. package/dist/taco/model/component-expressions.d.ts +242 -0
  154. package/dist/taco/model/component-expressions.js +1082 -0
  155. package/dist/taco/model/component-properties.d.ts +111 -0
  156. package/dist/taco/model/component-properties.js +339 -0
  157. package/dist/taco/model/component-samples.d.ts +83 -0
  158. package/dist/taco/model/component-samples.js +290 -0
  159. package/dist/taco/model/component-validity.d.ts +85 -0
  160. package/dist/taco/model/component-validity.js +397 -0
  161. package/dist/taco/model/components.d.ts +155 -0
  162. package/dist/taco/model/components.js +505 -0
  163. package/dist/taco/model/connector-ends.d.ts +20 -0
  164. package/dist/taco/model/connector-ends.js +188 -0
  165. package/dist/taco/model/connector-geometry.d.ts +74 -0
  166. package/dist/taco/model/connector-geometry.js +293 -0
  167. package/dist/taco/model/corner-radii.d.ts +26 -0
  168. package/dist/taco/model/corner-radii.js +47 -0
  169. package/dist/taco/model/derivations.d.ts +81 -0
  170. package/dist/taco/model/derivations.js +151 -0
  171. package/dist/taco/model/export-settings.d.ts +53 -0
  172. package/dist/taco/model/export-settings.js +156 -0
  173. package/dist/taco/model/expressions/ast.d.ts +153 -0
  174. package/dist/taco/model/expressions/ast.js +6 -0
  175. package/dist/taco/model/expressions/cache.d.ts +23 -0
  176. package/dist/taco/model/expressions/cache.js +43 -0
  177. package/dist/taco/model/expressions/check.d.ts +72 -0
  178. package/dist/taco/model/expressions/check.js +517 -0
  179. package/dist/taco/model/expressions/completion.d.ts +61 -0
  180. package/dist/taco/model/expressions/completion.js +421 -0
  181. package/dist/taco/model/expressions/evaluate.d.ts +32 -0
  182. package/dist/taco/model/expressions/evaluate.js +519 -0
  183. package/dist/taco/model/expressions/forms.d.ts +84 -0
  184. package/dist/taco/model/expressions/forms.js +200 -0
  185. package/dist/taco/model/expressions/index.d.ts +14 -0
  186. package/dist/taco/model/expressions/index.js +17 -0
  187. package/dist/taco/model/expressions/lexer.d.ts +42 -0
  188. package/dist/taco/model/expressions/lexer.js +213 -0
  189. package/dist/taco/model/expressions/library.d.ts +33 -0
  190. package/dist/taco/model/expressions/library.js +117 -0
  191. package/dist/taco/model/expressions/names.d.ts +43 -0
  192. package/dist/taco/model/expressions/names.js +99 -0
  193. package/dist/taco/model/expressions/parser.d.ts +15 -0
  194. package/dist/taco/model/expressions/parser.js +438 -0
  195. package/dist/taco/model/expressions/rename.d.ts +34 -0
  196. package/dist/taco/model/expressions/rename.js +46 -0
  197. package/dist/taco/model/expressions/source.d.ts +15 -0
  198. package/dist/taco/model/expressions/source.js +161 -0
  199. package/dist/taco/model/expressions/types.d.ts +119 -0
  200. package/dist/taco/model/expressions/types.js +102 -0
  201. package/dist/taco/model/expressions/values.d.ts +97 -0
  202. package/dist/taco/model/expressions/values.js +160 -0
  203. package/dist/taco/model/geometry-limits.d.ts +16 -0
  204. package/dist/taco/model/geometry-limits.js +67 -0
  205. package/dist/taco/model/geometry.d.ts +47 -0
  206. package/dist/taco/model/geometry.js +193 -0
  207. package/dist/taco/model/group-bounds.d.ts +38 -0
  208. package/dist/taco/model/group-bounds.js +144 -0
  209. package/dist/taco/model/id-table.d.ts +37 -0
  210. package/dist/taco/model/id-table.js +80 -0
  211. package/dist/taco/model/index.d.ts +25 -0
  212. package/dist/taco/model/index.js +27 -0
  213. package/dist/taco/model/instance-resolver.d.ts +376 -0
  214. package/dist/taco/model/instance-resolver.js +1778 -0
  215. package/dist/taco/model/layer-syntax.d.ts +340 -0
  216. package/dist/taco/model/layer-syntax.js +3506 -0
  217. package/dist/taco/model/layout/constraints.d.ts +84 -0
  218. package/dist/taco/model/layout/constraints.js +445 -0
  219. package/dist/taco/model/layout/frame-layout.d.ts +46 -0
  220. package/dist/taco/model/layout/frame-layout.js +618 -0
  221. package/dist/taco/model/layout/node-sizing.d.ts +163 -0
  222. package/dist/taco/model/layout/node-sizing.js +486 -0
  223. package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
  224. package/dist/taco/model/legacy-connector-caps.js +70 -0
  225. package/dist/taco/model/linear-call.d.ts +48 -0
  226. package/dist/taco/model/linear-call.js +58 -0
  227. package/dist/taco/model/new-nodes.d.ts +145 -0
  228. package/dist/taco/model/new-nodes.js +277 -0
  229. package/dist/taco/model/node-builder.d.ts +449 -0
  230. package/dist/taco/model/node-builder.js +2294 -0
  231. package/dist/taco/model/node-table-watch.d.ts +36 -0
  232. package/dist/taco/model/node-table-watch.js +63 -0
  233. package/dist/taco/model/node-transform.d.ts +39 -0
  234. package/dist/taco/model/node-transform.js +79 -0
  235. package/dist/taco/model/node-users-index.d.ts +10 -0
  236. package/dist/taco/model/node-users-index.js +82 -0
  237. package/dist/taco/model/operation-references.d.ts +80 -0
  238. package/dist/taco/model/operation-references.js +399 -0
  239. package/dist/taco/model/operations.d.ts +610 -0
  240. package/dist/taco/model/operations.js +2882 -0
  241. package/dist/taco/model/overlay-table.d.ts +71 -0
  242. package/dist/taco/model/overlay-table.js +171 -0
  243. package/dist/taco/model/override-remap.d.ts +69 -0
  244. package/dist/taco/model/override-remap.js +230 -0
  245. package/dist/taco/model/parent-table.d.ts +53 -0
  246. package/dist/taco/model/parent-table.js +193 -0
  247. package/dist/taco/model/problem-collector.d.ts +27 -0
  248. package/dist/taco/model/problem-collector.js +101 -0
  249. package/dist/taco/model/sha256.d.ts +10 -0
  250. package/dist/taco/model/sha256.js +79 -0
  251. package/dist/taco/model/shape-geometry.d.ts +96 -0
  252. package/dist/taco/model/shape-geometry.js +368 -0
  253. package/dist/taco/model/shared-node-table.d.ts +97 -0
  254. package/dist/taco/model/shared-node-table.js +546 -0
  255. package/dist/taco/model/slots.d.ts +113 -0
  256. package/dist/taco/model/slots.js +281 -0
  257. package/dist/taco/model/stroke-settings.d.ts +37 -0
  258. package/dist/taco/model/stroke-settings.js +90 -0
  259. package/dist/taco/model/svg-path-import.d.ts +89 -0
  260. package/dist/taco/model/svg-path-import.js +219 -0
  261. package/dist/taco/model/text-measurement.d.ts +142 -0
  262. package/dist/taco/model/text-measurement.js +846 -0
  263. package/dist/taco/model/text-reads.d.ts +66 -0
  264. package/dist/taco/model/text-reads.js +128 -0
  265. package/dist/taco/model/text-sizer.d.ts +358 -0
  266. package/dist/taco/model/text-sizer.js +996 -0
  267. package/dist/taco/model/theme-users.d.ts +59 -0
  268. package/dist/taco/model/theme-users.js +312 -0
  269. package/dist/taco/model/thumbnail.d.ts +15 -0
  270. package/dist/taco/model/thumbnail.js +34 -0
  271. package/dist/taco/model/types.d.ts +2369 -0
  272. package/dist/taco/model/types.js +366 -0
  273. package/dist/taco/model/unknown-property.d.ts +5 -0
  274. package/dist/taco/model/unknown-property.js +7 -0
  275. package/dist/taco/model/validated-documents.d.ts +22 -0
  276. package/dist/taco/model/validated-documents.js +22 -0
  277. package/dist/taco/model/variable-modes.d.ts +61 -0
  278. package/dist/taco/model/variable-modes.js +438 -0
  279. package/dist/taco/model/variable-references.d.ts +54 -0
  280. package/dist/taco/model/variable-references.js +183 -0
  281. package/dist/taco/model/variables.d.ts +24 -0
  282. package/dist/taco/model/variables.js +151 -0
  283. package/dist/taco/model/vector-network.d.ts +34 -0
  284. package/dist/taco/model/vector-network.js +411 -0
  285. package/dist/taco/svg-import/import-svg.d.ts +36 -0
  286. package/dist/taco/svg-import/import-svg.js +729 -0
  287. package/dist/taco/svg-import/index.d.ts +3 -0
  288. package/dist/taco/svg-import/index.js +3 -0
  289. package/dist/taco/svg-import/limits/limits.d.ts +79 -0
  290. package/dist/taco/svg-import/limits/limits.js +160 -0
  291. package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
  292. package/dist/taco/svg-import/paint/gradients.js +429 -0
  293. package/dist/taco/svg-import/paint/paint.d.ts +32 -0
  294. package/dist/taco/svg-import/paint/paint.js +162 -0
  295. package/dist/taco/svg-import/paths/paths.d.ts +58 -0
  296. package/dist/taco/svg-import/paths/paths.js +205 -0
  297. package/dist/taco/svg-import/public-types.d.ts +279 -0
  298. package/dist/taco/svg-import/public-types.js +56 -0
  299. package/dist/taco/svg-import/report/report.d.ts +44 -0
  300. package/dist/taco/svg-import/report/report.js +79 -0
  301. package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
  302. package/dist/taco/svg-import/reuse/reuse.js +840 -0
  303. package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
  304. package/dist/taco/svg-import/shapes/shapes.js +386 -0
  305. package/dist/taco/svg-import/structure/effects.d.ts +26 -0
  306. package/dist/taco/svg-import/structure/effects.js +618 -0
  307. package/dist/taco/svg-import/structure/image.d.ts +24 -0
  308. package/dist/taco/svg-import/structure/image.js +495 -0
  309. package/dist/taco/svg-import/structure/walk.d.ts +85 -0
  310. package/dist/taco/svg-import/structure/walk.js +544 -0
  311. package/dist/taco/svg-import/style/color.d.ts +40 -0
  312. package/dist/taco/svg-import/style/color.js +275 -0
  313. package/dist/taco/svg-import/style/computed.d.ts +214 -0
  314. package/dist/taco/svg-import/style/computed.js +1534 -0
  315. package/dist/taco/svg-import/style/css.d.ts +127 -0
  316. package/dist/taco/svg-import/style/css.js +541 -0
  317. package/dist/taco/svg-import/style/media.d.ts +25 -0
  318. package/dist/taco/svg-import/style/media.js +307 -0
  319. package/dist/taco/svg-import/style/properties.d.ts +43 -0
  320. package/dist/taco/svg-import/style/properties.js +135 -0
  321. package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
  322. package/dist/taco/svg-import/style/selector-reach.js +169 -0
  323. package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
  324. package/dist/taco/svg-import/style/selector-validity.js +307 -0
  325. package/dist/taco/svg-import/text/text.d.ts +29 -0
  326. package/dist/taco/svg-import/text/text.js +545 -0
  327. package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
  328. package/dist/taco/svg-import/transform/matrix.js +229 -0
  329. package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
  330. package/dist/taco/svg-import/tree/element-data.js +155 -0
  331. package/dist/taco/svg-import/tree/elements.d.ts +36 -0
  332. package/dist/taco/svg-import/tree/elements.js +121 -0
  333. package/dist/taco/svg-import/units/length.d.ts +43 -0
  334. package/dist/taco/svg-import/units/length.js +125 -0
  335. package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
  336. package/dist/taco/svg-import/units/viewbox.js +77 -0
  337. package/package.json +55 -4
@@ -0,0 +1,421 @@
1
+ // Editing help for the expression editor (docs/components/design.md 8.8):
2
+ // syntax colours and completion that knows the type expected at the caret.
3
+ // Both work on unfinished text, which the parser rejects.
4
+ import { expressionTypeNotes } from "./check.js";
5
+ import { EXPRESSION_LIBRARY, formatExpressionSignature } from "./library.js";
6
+ import { EXPRESSION_KEYWORDS } from "./lexer.js";
7
+ import { formatExpressionName, isExpressionNamePart, isExpressionNameStart, normalizeExpressionName } from "./names.js";
8
+ import { parseExpression } from "./parser.js";
9
+ import { expressionRecordFields, expressionTypesEqual, formatExpressionType } from "./types.js";
10
+ import { formatExpressionString } from "./source.js";
11
+ const KEYWORDS = new Set(EXPRESSION_KEYWORDS);
12
+ const OPERATOR_WORDS = new Set(["and", "or", "not"]);
13
+ const isNameStart = (ch) => ch !== "" && isExpressionNameStart(ch);
14
+ const isNamePart = (ch) => ch !== "" && isExpressionNamePart(ch);
15
+ const isDigit = (ch) => ch !== undefined && ch >= "0" && ch <= "9";
16
+ /** The code point that starts at `i`, or "" at the end. */
17
+ function pointAt(source, i) {
18
+ const code = source.codePointAt(i);
19
+ return code === undefined ? "" : String.fromCodePoint(code);
20
+ }
21
+ /** The code point that ends at `i`, or "" at the start. */
22
+ function pointBefore(source, i) {
23
+ if (i <= 0)
24
+ return "";
25
+ const low = source.charCodeAt(i - 1);
26
+ if (low >= 0xdc00 && low <= 0xdfff && i >= 2) {
27
+ const high = source.charCodeAt(i - 2);
28
+ if (high >= 0xd800 && high <= 0xdbff)
29
+ return source.slice(i - 2, i);
30
+ }
31
+ return source[i - 1];
32
+ }
33
+ /** The end of the bare name part from `i`. */
34
+ function namePartEnd(source, i) {
35
+ let end = i;
36
+ for (let ch = pointAt(source, end); isNamePart(ch); ch = pointAt(source, end))
37
+ end += ch.length;
38
+ return end;
39
+ }
40
+ /**
41
+ * Reads a name in backticks from the backtick at `start`, as the lexer
42
+ * does: `end` is past the closing backtick, or the end of the source when
43
+ * it never closes; `value` is the name so far, unescaped, in NFC.
44
+ */
45
+ function quotedName(source, start) {
46
+ let value = "";
47
+ let i = start + 1;
48
+ while (i < source.length) {
49
+ const c = source[i];
50
+ if (c === "`")
51
+ return { end: i + 1, closed: true, value: normalizeExpressionName(value) };
52
+ if (c === "\\" && i + 1 < source.length) {
53
+ value += source[i + 1];
54
+ i += 2;
55
+ continue;
56
+ }
57
+ value += c;
58
+ i += 1;
59
+ }
60
+ return { end: source.length, closed: false, value: normalizeExpressionName(value) };
61
+ }
62
+ /** True when a `.` at `dot` reads a field (`pad.top`, `f(x).y`) rather than starting an option literal (`.large`). */
63
+ function dotReadsField(source, dot) {
64
+ const before = pointBefore(source, dot);
65
+ return isNamePart(before) || before === ")" || before === "]" || before === "`";
66
+ }
67
+ /**
68
+ * Splits source text into coloured stretches. It never fails: text the
69
+ * lexer would reject (an unknown character, an unfinished string) comes out
70
+ * as `invalid` or as far as it goes. Names are `property` when
71
+ * `isProperty` says so, `function` before a `(`, and `name` otherwise
72
+ * (`let` names and unknown names).
73
+ */
74
+ export function highlightExpression(source, isProperty) {
75
+ const out = [];
76
+ const push = (from, to, kind) => out.push({ from, to, kind });
77
+ let i = 0;
78
+ while (i < source.length) {
79
+ const ch = source[i];
80
+ const start = i;
81
+ if (/\s/.test(ch)) {
82
+ i += 1;
83
+ continue;
84
+ }
85
+ if (ch === "/" && source[i + 1] === "/") {
86
+ while (i < source.length && source[i] !== "\n")
87
+ i += 1;
88
+ push(start, i, "comment");
89
+ continue;
90
+ }
91
+ if (isDigit(ch)) {
92
+ while (isDigit(source[i]))
93
+ i += 1;
94
+ if (source[i] === "." && isDigit(source[i + 1])) {
95
+ i += 1;
96
+ while (isDigit(source[i]))
97
+ i += 1;
98
+ }
99
+ while (isNamePart(pointAt(source, i)) || source[i] === "%")
100
+ i += pointAt(source, i).length;
101
+ const unit = source.slice(start, i).replace(/^[0-9.]+/, "");
102
+ push(start, i, unit === "" || unit === "px" || unit === "%" || unit === "deg" ? "number" : "invalid");
103
+ continue;
104
+ }
105
+ if (ch === '"') {
106
+ i += 1;
107
+ while (i < source.length && source[i] !== '"' && source[i] !== "\n")
108
+ i += source[i] === "\\" ? 2 : 1;
109
+ const closed = source[i] === '"';
110
+ if (closed)
111
+ i += 1;
112
+ push(start, Math.min(i, source.length), closed ? "string" : "invalid");
113
+ continue;
114
+ }
115
+ if (ch === "#") {
116
+ i += 1;
117
+ while (/^[0-9A-Za-z]$/.test(source[i] ?? ""))
118
+ i += 1;
119
+ const digits = i - start - 1;
120
+ const valid = [3, 4, 6, 8].includes(digits) && /^#[0-9A-Fa-f]+$/.test(source.slice(start, i));
121
+ push(start, i, valid ? "color" : "invalid");
122
+ continue;
123
+ }
124
+ if (ch === "." && (isNameStart(pointAt(source, i + 1)) || source[i + 1] === "`")) {
125
+ const field = dotReadsField(source, i);
126
+ let closed = true;
127
+ if (source[i + 1] === "`") {
128
+ const quoted = quotedName(source, i + 1);
129
+ i = quoted.end;
130
+ closed = quoted.closed;
131
+ }
132
+ else {
133
+ i = namePartEnd(source, i + 1 + pointAt(source, i + 1).length);
134
+ }
135
+ if (!closed) {
136
+ push(start, i, "invalid");
137
+ }
138
+ else if (field) {
139
+ push(start, start + 1, "punctuation");
140
+ push(start + 1, i, "field");
141
+ }
142
+ else {
143
+ push(start, i, "option");
144
+ }
145
+ continue;
146
+ }
147
+ const point = pointAt(source, i);
148
+ if (isNameStart(point) || ch === "`") {
149
+ let word;
150
+ if (ch === "`") {
151
+ const quoted = quotedName(source, i);
152
+ i = quoted.end;
153
+ if (!quoted.closed) {
154
+ push(start, i, "invalid");
155
+ continue;
156
+ }
157
+ word = quoted.value;
158
+ }
159
+ else {
160
+ i = namePartEnd(source, i + point.length);
161
+ word = source.slice(start, i);
162
+ }
163
+ let kind;
164
+ if (ch !== "`" && OPERATOR_WORDS.has(word))
165
+ kind = "operator";
166
+ else if (ch !== "`" && KEYWORDS.has(word))
167
+ kind = "keyword";
168
+ else if (isProperty(normalizeExpressionName(word)))
169
+ kind = "property";
170
+ else if (/^\s*\(/.test(source.slice(i)))
171
+ kind = "function";
172
+ else
173
+ kind = "name";
174
+ push(start, i, kind);
175
+ continue;
176
+ }
177
+ const two = source.slice(i, i + 2);
178
+ if (["=>", "==", "!=", "<=", ">=", "++"].includes(two)) {
179
+ push(start, i + 2, two === "=>" ? "punctuation" : "operator");
180
+ i += 2;
181
+ continue;
182
+ }
183
+ if ("+-*/<>".includes(ch)) {
184
+ push(start, i + 1, "operator");
185
+ i += 1;
186
+ continue;
187
+ }
188
+ push(start, i + point.length, "()[]{},:.=".includes(ch) ? "punctuation" : "invalid");
189
+ i += point.length;
190
+ }
191
+ return out;
192
+ }
193
+ /** A name the checker sees in place of the word being typed. */
194
+ const HOLE = "zz_completion_hole";
195
+ /** The brackets left open in `text`, closed again, innermost first. */
196
+ function closers(text) {
197
+ const stack = [];
198
+ const pairs = { "(": ")", "[": "]", "{": "}" };
199
+ for (let i = 0; i < text.length; i += 1) {
200
+ const ch = text[i];
201
+ if (ch === "/" && text[i + 1] === "/") {
202
+ while (i < text.length && text[i] !== "\n")
203
+ i += 1;
204
+ }
205
+ else if (ch === '"') {
206
+ i += 1;
207
+ while (i < text.length && text[i] !== '"')
208
+ i += text[i] === "\\" ? 2 : 1;
209
+ }
210
+ else if (ch in pairs) {
211
+ stack.push(pairs[ch]);
212
+ }
213
+ else if (ch === ")" || ch === "]" || ch === "}") {
214
+ if (stack[stack.length - 1] === ch)
215
+ stack.pop();
216
+ }
217
+ }
218
+ return stack.reverse().join(" ");
219
+ }
220
+ /**
221
+ * Checks the text with the word being typed replaced by a placeholder,
222
+ * trying a few endings so that unfinished text parses (closing brackets,
223
+ * the rest of a `match` arm, an `else`, an `in`). Returns the checker's
224
+ * notes, or undefined when no ending parses.
225
+ */
226
+ function probe(before, after, context) {
227
+ const open = (tail) => closers(before + tail);
228
+ const endings = [after, open(""), ` => stored ${open(" =>")}`, ` else stored ${open("")}`, ` in stored ${open("")}`, ` then stored else stored ${open("")}`];
229
+ for (const ending of endings) {
230
+ const parsed = parseExpression(before + ending);
231
+ if (parsed.ok)
232
+ return expressionTypeNotes(parsed.expression, context.expected, context.environment);
233
+ }
234
+ return undefined;
235
+ }
236
+ /** How well a name matches the typed text: 0 when it starts with it, 1 when it only contains it, undefined when neither. */
237
+ function matchRank(label, typed) {
238
+ const at = label.toLowerCase().indexOf(typed.toLowerCase());
239
+ return at === 0 ? 0 : at > 0 ? 1 : undefined;
240
+ }
241
+ function matches(label, typed) {
242
+ return matchRank(label, typed) !== undefined;
243
+ }
244
+ /**
245
+ * Stable order: names that start with the typed text before names that only
246
+ * contain it, and in each of the two groups the items that fit first.
247
+ * `typed` is matched against the label without a leading `.`.
248
+ */
249
+ function sortItems(items, typed) {
250
+ // A variable offered as `var("Name")` starts with the typed text when its name does, as well as when `var` does.
251
+ const starts = (item) => [item.label.replace(/^\./, ""), item.label.replace(/^var\("/, "")].some((text) => matchRank(text, typed) === 0);
252
+ const rank = (item) => (starts(item) ? 0 : 2) + (item.fits ? 0 : 1);
253
+ return [0, 1, 2, 3].flatMap((wanted) => items.filter((item) => rank(item) === wanted));
254
+ }
255
+ function variableItems(context, expected, partial, wrap) {
256
+ return context.variables
257
+ .filter((name) => partial === "" || name.toLowerCase().includes(partial.toLowerCase()))
258
+ .map((name) => {
259
+ const type = context.environment.variableType(name);
260
+ const literal = formatExpressionString(name);
261
+ return {
262
+ kind: "variable",
263
+ label: wrap ? `var(${literal})` : name,
264
+ insert: wrap ? `var(${literal})` : literal,
265
+ ...(type === undefined ? {} : { detail: formatExpressionType(type) }),
266
+ fits: type !== undefined && expected !== undefined && expressionTypesEqual(type, expected),
267
+ };
268
+ });
269
+ }
270
+ function optionItems(type, partial, withDot) {
271
+ if (type?.kind !== "enum")
272
+ return [];
273
+ const items = type.options
274
+ .filter((option) => matches(option, partial))
275
+ .map((option) => {
276
+ const written = formatExpressionName(option);
277
+ return { kind: "option", label: `.${option}`, insert: withDot ? `.${written}` : written, detail: type.name, fits: true };
278
+ });
279
+ return sortItems(items, partial);
280
+ }
281
+ /** The last note at this span start that has an expected type, or the last one at all. */
282
+ function noteAt(notes, start) {
283
+ const here = notes.filter((note) => note.span.start === start);
284
+ return [...here].reverse().find((note) => note.expected !== undefined) ?? here[here.length - 1];
285
+ }
286
+ /**
287
+ * The completions at `caret`. After a `.` that starts an option literal
288
+ * or pattern it offers the options of the enum expected there; after a
289
+ * `.` that reads a field, the fields of the value's type; inside
290
+ * `var("...")`, the variables; in a word, properties, variables written as
291
+ * `var("Collection/Name")`, functions with their signature and keywords,
292
+ * the ones whose type fits the place first. Names that start with the typed
293
+ * text come before names that only contain it. Returns undefined inside a
294
+ * comment, a colour or another string, or at an empty word unless it
295
+ * follows `match` or `explicit` is set (the user asked with Ctrl+Space).
296
+ */
297
+ export function completeExpression(source, caret, context, explicit = false) {
298
+ const highlight = highlightExpression(source, (name) => context.properties.includes(name));
299
+ const token = highlight.find((h) => h.from < caret && caret <= h.to);
300
+ if (token?.kind === "comment" && caret > token.from)
301
+ return undefined;
302
+ // Inside a colour such as `#8a8a92`, finished or not, there is nothing to offer.
303
+ if (token !== undefined && source[token.from] === "#")
304
+ return undefined;
305
+ if (token !== undefined && (token.kind === "string" || (token.kind === "invalid" && source[token.from] === '"'))) {
306
+ const closed = token.kind === "string";
307
+ if (closed && caret === token.to)
308
+ return undefined;
309
+ if (!/var\s*\(\s*$/.test(source.slice(0, token.from)))
310
+ return undefined;
311
+ const partial = source.slice(token.from + 1, caret);
312
+ const notes = probe(source.slice(0, token.from) + formatExpressionString(HOLE), closed ? source.slice(token.to) : "", context);
313
+ const expected = notes === undefined ? undefined : noteAt(notes, previousVarStart(source, token.from))?.expected;
314
+ // An unfinished `var("` gets its closing quote and bracket.
315
+ const items = sortItems(variableItems(context, expected, partial, false), partial).map((item) => (closed ? item : { ...item, insert: `${item.insert})` }));
316
+ return { from: token.from, to: token.to, ...(expected === undefined ? {} : { expected }), items };
317
+ }
318
+ let from = caret;
319
+ let to = caret;
320
+ let partial;
321
+ // In a name in backticks, finished or not, the word is the whole name from its opening backtick.
322
+ const quoteAt = token === undefined ? -1 : source[token.from] === "`" ? token.from : source.startsWith(".`", token.from) ? token.from + 1 : -1;
323
+ if (token !== undefined && quoteAt >= 0 && caret > quoteAt) {
324
+ from = quoteAt;
325
+ to = token.to;
326
+ partial = quotedName(source.slice(0, caret), quoteAt).value;
327
+ }
328
+ else {
329
+ while (from > 0 && isNamePart(pointBefore(source, from)))
330
+ from -= pointBefore(source, from).length;
331
+ to = namePartEnd(source, caret);
332
+ if (isDigit(source[from]))
333
+ return undefined;
334
+ partial = normalizeExpressionName(source.slice(from, caret));
335
+ }
336
+ const after = source.slice(to);
337
+ if (source[from - 1] === ".") {
338
+ const dot = from - 1;
339
+ if (dotReadsField(source, dot)) {
340
+ const notes = probe(`${source.slice(0, dot)}.${HOLE}`, after, context) ?? [];
341
+ const target = [...notes].reverse().find((note) => note.span.end === dot && note.type !== undefined);
342
+ const fields = target?.type === undefined ? undefined : expressionRecordFields(target.type);
343
+ if (fields === undefined)
344
+ return undefined;
345
+ const items = Object.entries(fields)
346
+ .filter(([name]) => matches(name, partial))
347
+ .map(([name, type]) => ({ kind: "field", label: name, insert: name, detail: formatExpressionType(type), fits: false }));
348
+ return { from, to, items: sortItems(items, partial) };
349
+ }
350
+ const notes = probe(`${source.slice(0, dot)}.${HOLE}`, after, context);
351
+ let expected = notes === undefined ? undefined : noteAt(notes, dot)?.expected;
352
+ if (notes === undefined) {
353
+ // The text has an error somewhere, so it cannot be checked as a whole.
354
+ // The nearest `match` before the dot, checked on its own, still tells
355
+ // which options its arms take.
356
+ const match = highlight.filter((h) => h.kind === "keyword" && h.to <= dot && source.slice(h.from, h.to) === "match").pop();
357
+ const alone = match === undefined ? undefined : probe(`${source.slice(match.from, dot)}.${HOLE}`, "", context);
358
+ if (match !== undefined && alone !== undefined)
359
+ expected = noteAt(alone, dot - match.from)?.expected;
360
+ }
361
+ return {
362
+ from: dot,
363
+ to,
364
+ ...(expected === undefined ? {} : { expected }),
365
+ items: optionItems(expected, partial, true),
366
+ };
367
+ }
368
+ // Right after `match` a property is expected, so the properties are
369
+ // offered without being asked for; elsewhere an empty word waits for
370
+ // Ctrl+Space.
371
+ const afterMatch = partial === "" && /(^|[^A-Za-z0-9_.])match[ \t]+$/.test(source.slice(0, from));
372
+ if (partial === "" && !explicit && !afterMatch)
373
+ return undefined;
374
+ const notes = probe(source.slice(0, from) + HOLE, after, context);
375
+ const expected = notes === undefined ? undefined : noteAt(notes, from)?.expected;
376
+ const fits = (type) => type !== undefined && expected !== undefined && expressionTypesEqual(type, expected);
377
+ const items = [];
378
+ if (partial === "")
379
+ items.push(...optionItems(expected, "", true));
380
+ for (const name of context.properties) {
381
+ if (!matches(name, partial))
382
+ continue;
383
+ const type = context.environment.propertyType(name);
384
+ // A `match` picks by a property with options (or on and off), so those fit after `match`.
385
+ const fitsHere = afterMatch ? type?.kind === "enum" || type?.kind === "boolean" : fits(type);
386
+ items.push({ kind: "property", label: name, insert: formatExpressionName(name), ...(type === undefined ? {} : { detail: formatExpressionType(type) }), fits: fitsHere });
387
+ }
388
+ if (afterMatch && !explicit)
389
+ return { from, to, items: sortItems(items, partial) };
390
+ if ("var".startsWith(partial.toLowerCase()) || partial.length >= 2) {
391
+ const matching = "var".startsWith(partial.toLowerCase()) ? "" : partial;
392
+ items.push(...variableItems(context, expected, matching, true));
393
+ }
394
+ for (const fn of Object.values(EXPRESSION_LIBRARY)) {
395
+ if (!matches(fn.name, partial))
396
+ continue;
397
+ const overload = fn.overloads.find((o) => fits(o.result)) ?? fn.overloads[0];
398
+ items.push({
399
+ kind: "function",
400
+ label: fn.name,
401
+ insert: `${fn.name}()`,
402
+ caret: fn.name.length + 1,
403
+ detail: formatExpressionSignature(fn.name, overload),
404
+ fits: fits(overload.result),
405
+ });
406
+ }
407
+ for (const keyword of ["if", "match", "let", "not", "true", "false", "stored"]) {
408
+ if (!matches(keyword, partial) || keyword === partial)
409
+ continue;
410
+ if (keyword === "stored" && context.environment.storedType === undefined)
411
+ continue;
412
+ const type = keyword === "true" || keyword === "false" || keyword === "not" ? { kind: "boolean" } : keyword === "stored" ? context.environment.storedType : undefined;
413
+ items.push({ kind: "keyword", label: keyword, insert: keyword, ...(type === undefined ? {} : { detail: formatExpressionType(type) }), fits: fits(type) });
414
+ }
415
+ return { from, to, ...(expected === undefined ? {} : { expected }), items: sortItems(items, partial) };
416
+ }
417
+ /** Where the `var` before a string literal at `quote` starts. */
418
+ function previousVarStart(source, quote) {
419
+ const match = /var\s*\(\s*$/.exec(source.slice(0, quote));
420
+ return match === null ? quote : match.index;
421
+ }
@@ -0,0 +1,32 @@
1
+ import type { Expression } from "./ast.ts";
2
+ import { type ExpressionValue } from "./values.ts";
3
+ /**
4
+ * What an expression reads while it runs: the counterpart of the checking
5
+ * `ExpressionEnvironment`, with values in place of types. Every name the
6
+ * checking environment typed must have a value here.
7
+ */
8
+ export interface ExpressionValueEnvironment {
9
+ /** Local dimensions for a linear gradient authored on a layer. */
10
+ readonly paintBox?: () => {
11
+ width: number;
12
+ height: number;
13
+ };
14
+ /** The value of the component property with this name, or undefined when there is none. */
15
+ propertyValue(name: string): ExpressionValue | undefined;
16
+ /** The value of the document variable `"Collection/Name"` in the active mode, or undefined when there is none. */
17
+ variableValue(name: string): ExpressionValue | undefined;
18
+ /** The component's base value of the field being computed, which `stored` reads. */
19
+ readonly stored?: ExpressionValue;
20
+ }
21
+ /**
22
+ * Evaluates an expression that has passed the check. It never throws for
23
+ * such an expression when the environment holds every property, variable
24
+ * and stored value the checking environment typed; a missing one is a
25
+ * programming error and throws, since a wrong value would be a silent
26
+ * failure.
27
+ */
28
+ export declare function evaluateExpression(expression: Expression, environment: ExpressionValueEnvironment): ExpressionValue;
29
+ /** 0 for a number that is not finite (from very large values or 0 / 0); negative zero is plain 0. */
30
+ export declare function finiteExpressionNumber(value: number): number;
31
+ /** A number held between 0 and 1: what amounts and colour channels become. */
32
+ export declare function clampExpressionAmount(value: number): number;