@optique/core 0.10.7 → 1.0.0-dev.1109

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 (56) hide show
  1. package/README.md +4 -6
  2. package/dist/annotations.cjs +209 -1
  3. package/dist/annotations.d.cts +78 -1
  4. package/dist/annotations.d.ts +78 -1
  5. package/dist/annotations.js +201 -1
  6. package/dist/completion.cjs +186 -50
  7. package/dist/completion.js +186 -50
  8. package/dist/constructs.cjs +310 -78
  9. package/dist/constructs.d.cts +525 -644
  10. package/dist/constructs.d.ts +525 -644
  11. package/dist/constructs.js +311 -79
  12. package/dist/context.cjs +43 -3
  13. package/dist/context.d.cts +113 -5
  14. package/dist/context.d.ts +113 -5
  15. package/dist/context.js +41 -3
  16. package/dist/dependency.cjs +172 -66
  17. package/dist/dependency.d.cts +22 -2
  18. package/dist/dependency.d.ts +22 -2
  19. package/dist/dependency.js +172 -66
  20. package/dist/doc.cjs +46 -1
  21. package/dist/doc.d.cts +24 -0
  22. package/dist/doc.d.ts +24 -0
  23. package/dist/doc.js +46 -1
  24. package/dist/facade.cjs +702 -322
  25. package/dist/facade.d.cts +124 -190
  26. package/dist/facade.d.ts +124 -190
  27. package/dist/facade.js +703 -323
  28. package/dist/index.cjs +5 -0
  29. package/dist/index.d.cts +5 -5
  30. package/dist/index.d.ts +5 -5
  31. package/dist/index.js +3 -3
  32. package/dist/message.cjs +7 -4
  33. package/dist/message.js +7 -4
  34. package/dist/mode-dispatch.cjs +23 -1
  35. package/dist/mode-dispatch.d.cts +55 -0
  36. package/dist/mode-dispatch.d.ts +55 -0
  37. package/dist/mode-dispatch.js +21 -1
  38. package/dist/modifiers.cjs +210 -55
  39. package/dist/modifiers.js +211 -56
  40. package/dist/parser.cjs +80 -47
  41. package/dist/parser.d.cts +18 -3
  42. package/dist/parser.d.ts +18 -3
  43. package/dist/parser.js +82 -50
  44. package/dist/primitives.cjs +102 -37
  45. package/dist/primitives.d.cts +81 -24
  46. package/dist/primitives.d.ts +81 -24
  47. package/dist/primitives.js +103 -39
  48. package/dist/usage.cjs +88 -6
  49. package/dist/usage.d.cts +51 -13
  50. package/dist/usage.d.ts +51 -13
  51. package/dist/usage.js +85 -7
  52. package/dist/valueparser.cjs +371 -99
  53. package/dist/valueparser.d.cts +56 -7
  54. package/dist/valueparser.d.ts +56 -7
  55. package/dist/valueparser.js +371 -99
  56. package/package.json +10 -1
@@ -1,6 +1,8 @@
1
+ const require_annotations = require('./annotations.cjs');
1
2
  const require_message = require('./message.cjs');
2
3
  const require_dependency = require('./dependency.cjs');
3
4
  const require_mode_dispatch = require('./mode-dispatch.cjs');
5
+ const require_context = require('./context.cjs');
4
6
 
5
7
  //#region src/modifiers.ts
6
8
  /**
@@ -12,7 +14,7 @@ const require_mode_dispatch = require('./mode-dispatch.cjs');
12
14
  * @internal
13
15
  */
14
16
  function parseOptionalStyleSync(context, parser) {
15
- const innerState = typeof context.state === "undefined" ? parser.initialState : context.state[0];
17
+ const innerState = Array.isArray(context.state) ? context.state[0] : parser.initialState;
16
18
  const result = parser.parse({
17
19
  ...context,
18
20
  state: innerState
@@ -24,7 +26,7 @@ function parseOptionalStyleSync(context, parser) {
24
26
  * @internal
25
27
  */
26
28
  async function parseOptionalStyleAsync(context, parser) {
27
- const innerState = typeof context.state === "undefined" ? parser.initialState : context.state[0];
29
+ const innerState = Array.isArray(context.state) ? context.state[0] : parser.initialState;
28
30
  const result = await parser.parse({
29
31
  ...context,
30
32
  state: innerState
@@ -62,6 +64,28 @@ function processOptionalStyleResult(result, innerState, context) {
62
64
  return result;
63
65
  }
64
66
  /**
67
+ * Creates a `shouldDeferCompletion` adapter that unwraps the outer state
68
+ * shape (`[TState] | undefined`) used by {@link optional} and
69
+ * {@link withDefault} before delegating to the inner parser's hook.
70
+ *
71
+ * When state is an array, the adapter unwraps `state[0]` and propagates
72
+ * annotations from the outer array. Non-array objects (e.g., PromptBindState
73
+ * from `prompt()`) are passed through directly. `undefined` returns `false`
74
+ * without calling the inner hook.
75
+ *
76
+ * @internal
77
+ */
78
+ function adaptShouldDeferCompletion(innerCheck) {
79
+ return (state) => {
80
+ if (Array.isArray(state)) {
81
+ const inner = require_annotations.getAnnotations(state) != null && state[0] != null && typeof state[0] === "object" ? require_annotations.inheritAnnotations(state, state[0]) : state[0];
82
+ return innerCheck(inner);
83
+ }
84
+ if (state != null && typeof state === "object") return innerCheck(state);
85
+ return false;
86
+ };
87
+ }
88
+ /**
65
89
  * Creates a parser that makes another parser optional, allowing it to succeed
66
90
  * without consuming input if the wrapped parser fails to match.
67
91
  * If the wrapped parser succeeds, this returns its value.
@@ -76,14 +100,14 @@ function processOptionalStyleResult(result, innerState, context) {
76
100
  function optional(parser) {
77
101
  const syncParser = parser;
78
102
  function* suggestSync(context, prefix) {
79
- const innerState = typeof context.state === "undefined" ? syncParser.initialState : context.state[0];
103
+ const innerState = Array.isArray(context.state) ? context.state[0] : syncParser.initialState;
80
104
  yield* syncParser.suggest({
81
105
  ...context,
82
106
  state: innerState
83
107
  }, prefix);
84
108
  }
85
109
  async function* suggestAsync(context, prefix) {
86
- const innerState = typeof context.state === "undefined" ? syncParser.initialState : context.state[0];
110
+ const innerState = Array.isArray(context.state) ? context.state[0] : syncParser.initialState;
87
111
  const suggestions = parser.suggest({
88
112
  ...context,
89
113
  state: innerState
@@ -106,12 +130,23 @@ function optional(parser) {
106
130
  }],
107
131
  initialState: void 0,
108
132
  ...wrappedDependencyMarker,
133
+ ...typeof parser.shouldDeferCompletion === "function" ? { shouldDeferCompletion: adaptShouldDeferCompletion(parser.shouldDeferCompletion.bind(parser)) } : {},
109
134
  parse(context) {
110
135
  return require_mode_dispatch.dispatchByMode(parser.$mode, () => parseOptionalStyleSync(context, syncParser), () => parseOptionalStyleAsync(context, parser));
111
136
  },
112
137
  complete(state) {
113
- if (typeof state === "undefined") {
138
+ if (!Array.isArray(state)) {
114
139
  if (innerHasWrappedDependency && wrappedPendingState) return require_mode_dispatch.dispatchByMode(parser.$mode, () => syncParser.complete([wrappedPendingState]), () => parser.complete([wrappedPendingState]));
140
+ if (typeof parser.shouldDeferCompletion === "function" && state != null && typeof state === "object") {
141
+ const innerComplete = () => {
142
+ const innerResult = require_mode_dispatch.dispatchByMode(parser.$mode, () => syncParser.complete(state), () => parser.complete(state));
143
+ return require_mode_dispatch.mapModeValue(parser.$mode, innerResult, (result) => result.success ? result : {
144
+ success: true,
145
+ value: void 0
146
+ });
147
+ };
148
+ return innerComplete();
149
+ }
115
150
  return {
116
151
  success: true,
117
152
  value: void 0
@@ -124,7 +159,8 @@ function optional(parser) {
124
159
  value: void 0
125
160
  };
126
161
  }
127
- return require_mode_dispatch.dispatchByMode(parser.$mode, () => syncParser.complete(state[0]), () => parser.complete(state[0]));
162
+ const innerElement = require_annotations.getAnnotations(state) != null && state[0] != null && typeof state[0] === "object" ? require_annotations.inheritAnnotations(state, state[0]) : state[0];
163
+ return require_mode_dispatch.dispatchByMode(parser.$mode, () => syncParser.complete(innerElement), () => parser.complete(innerElement));
128
164
  },
129
165
  suggest(context, prefix) {
130
166
  return require_mode_dispatch.dispatchIterableByMode(parser.$mode, () => suggestSync(context, prefix), () => suggestAsync(context, prefix));
@@ -174,15 +210,25 @@ var WithDefaultError = class extends Error {
174
210
  };
175
211
  function withDefault(parser, defaultValue, options) {
176
212
  const syncParser = parser;
213
+ const getDocDefaultValue = (upperDefaultValue) => {
214
+ if (upperDefaultValue != null) return upperDefaultValue;
215
+ if (options?.message) return void 0;
216
+ if (typeof defaultValue !== "function") return defaultValue;
217
+ try {
218
+ return defaultValue();
219
+ } catch {
220
+ return void 0;
221
+ }
222
+ };
177
223
  function* suggestSync(context, prefix) {
178
- const innerState = typeof context.state === "undefined" ? syncParser.initialState : context.state[0];
224
+ const innerState = Array.isArray(context.state) ? context.state[0] : syncParser.initialState;
179
225
  yield* syncParser.suggest({
180
226
  ...context,
181
227
  state: innerState
182
228
  }, prefix);
183
229
  }
184
230
  async function* suggestAsync(context, prefix) {
185
- const innerState = typeof context.state === "undefined" ? syncParser.initialState : context.state[0];
231
+ const innerState = Array.isArray(context.state) ? context.state[0] : syncParser.initialState;
186
232
  const suggestions = parser.suggest({
187
233
  ...context,
188
234
  state: innerState
@@ -202,11 +248,12 @@ function withDefault(parser, defaultValue, options) {
202
248
  }],
203
249
  initialState: void 0,
204
250
  ...wrappedDependencyMarker,
251
+ ...typeof parser.shouldDeferCompletion === "function" ? { shouldDeferCompletion: adaptShouldDeferCompletion(parser.shouldDeferCompletion.bind(parser)) } : {},
205
252
  parse(context) {
206
253
  return require_mode_dispatch.dispatchByMode(parser.$mode, () => parseOptionalStyleSync(context, syncParser), () => parseOptionalStyleAsync(context, parser));
207
254
  },
208
255
  complete(state) {
209
- if (typeof state === "undefined") {
256
+ if (!Array.isArray(state)) {
210
257
  if (require_dependency.transformsDependencyValue(parser)) {
211
258
  const innerResult = require_mode_dispatch.dispatchByMode(parser.$mode, () => syncParser.complete(void 0), () => parser.complete(void 0));
212
259
  const handleInnerResult = (res) => {
@@ -235,8 +282,7 @@ function withDefault(parser, defaultValue, options) {
235
282
  };
236
283
  }
237
284
  };
238
- if (innerResult instanceof Promise) return innerResult.then(handleInnerResult);
239
- return handleInnerResult(innerResult);
285
+ return require_mode_dispatch.mapModeValue(parser.$mode, innerResult, handleInnerResult);
240
286
  }
241
287
  if (require_dependency.isWrappedDependencySource(parser)) try {
242
288
  const value = typeof defaultValue === "function" ? defaultValue() : defaultValue;
@@ -251,6 +297,10 @@ function withDefault(parser, defaultValue, options) {
251
297
  error: error instanceof WithDefaultError ? error.errorMessage : require_message.message`${require_message.text(String(error))}`
252
298
  };
253
299
  }
300
+ if (typeof parser.shouldDeferCompletion === "function" && state != null && typeof state === "object") {
301
+ const innerResult = require_mode_dispatch.dispatchByMode(parser.$mode, () => syncParser.complete(state), () => parser.complete(state));
302
+ return innerResult;
303
+ }
254
304
  try {
255
305
  const value = typeof defaultValue === "function" ? defaultValue() : defaultValue;
256
306
  return {
@@ -293,8 +343,7 @@ function withDefault(parser, defaultValue, options) {
293
343
  };
294
344
  }
295
345
  };
296
- if (innerResult instanceof Promise) return innerResult.then(handleInnerResult);
297
- return handleInnerResult(innerResult);
346
+ return require_mode_dispatch.mapModeValue(parser.$mode, innerResult, handleInnerResult);
298
347
  }
299
348
  try {
300
349
  const value = typeof defaultValue === "function" ? defaultValue() : defaultValue;
@@ -309,7 +358,8 @@ function withDefault(parser, defaultValue, options) {
309
358
  };
310
359
  }
311
360
  }
312
- return require_mode_dispatch.dispatchByMode(parser.$mode, () => syncParser.complete(state[0]), () => parser.complete(state[0]));
361
+ const innerElement = require_annotations.getAnnotations(state) != null && state[0] != null && typeof state[0] === "object" ? require_annotations.inheritAnnotations(state, state[0]) : state[0];
362
+ return require_mode_dispatch.dispatchByMode(parser.$mode, () => syncParser.complete(innerElement), () => parser.complete(innerElement));
313
363
  },
314
364
  suggest(context, prefix) {
315
365
  return require_mode_dispatch.dispatchIterableByMode(parser.$mode, () => suggestSync(context, prefix), () => suggestAsync(context, prefix));
@@ -319,8 +369,7 @@ function withDefault(parser, defaultValue, options) {
319
369
  kind: "available",
320
370
  state: state.state[0]
321
371
  };
322
- const actualDefaultValue = upperDefaultValue != null ? upperDefaultValue : typeof defaultValue === "function" ? defaultValue() : defaultValue;
323
- const fragments = syncParser.getDocFragments(innerState, actualDefaultValue);
372
+ const fragments = syncParser.getDocFragments(innerState, getDocDefaultValue(upperDefaultValue));
324
373
  if (options?.message) {
325
374
  const modifiedFragments = fragments.fragments.map((fragment) => {
326
375
  if (fragment.type === "entry") return {
@@ -374,19 +423,10 @@ function withDefault(parser, defaultValue, options) {
374
423
  */
375
424
  function map(parser, transform) {
376
425
  const complete = (state) => {
377
- const res = parser.complete(state);
378
- if (res instanceof Promise) return res.then((r) => {
379
- if (r.success) return {
380
- success: true,
381
- value: transform(r.value)
382
- };
383
- return r;
384
- });
385
- if (res.success) return {
426
+ return require_mode_dispatch.mapModeValue(parser.$mode, parser.complete(state), (result) => result.success ? require_context.isPlaceholderValue(result.value) ? result : {
386
427
  success: true,
387
- value: transform(res.value)
388
- };
389
- return res;
428
+ value: transform(result.value)
429
+ } : result);
390
430
  };
391
431
  const dependencyMarkers = require_dependency.isWrappedDependencySource(parser) ? {
392
432
  [require_dependency.wrappedDependencySourceMarker]: parser[require_dependency.wrappedDependencySourceMarker],
@@ -420,50 +460,105 @@ function map(parser, transform) {
420
460
  function multiple(parser, options = {}) {
421
461
  const syncParser = parser;
422
462
  const { min = 0, max = Infinity } = options;
463
+ const unwrapInjectedWrapper = require_annotations.unwrapInjectedAnnotationWrapper;
464
+ const completeSyncWithUnwrappedFallback = (state) => {
465
+ try {
466
+ return syncParser.complete(state);
467
+ } catch (error) {
468
+ if (!require_annotations.isInjectedAnnotationWrapper(state)) throw error;
469
+ return syncParser.complete(unwrapInjectedWrapper(state));
470
+ }
471
+ };
472
+ const parseSyncWithUnwrappedFallback = (context) => {
473
+ try {
474
+ const result = syncParser.parse(context);
475
+ if (result.success || result.consumed !== 0 || !require_annotations.isInjectedAnnotationWrapper(context.state)) return result;
476
+ return syncParser.parse({
477
+ ...context,
478
+ state: unwrapInjectedWrapper(context.state)
479
+ });
480
+ } catch (error) {
481
+ if (!require_annotations.isInjectedAnnotationWrapper(context.state)) throw error;
482
+ return syncParser.parse({
483
+ ...context,
484
+ state: unwrapInjectedWrapper(context.state)
485
+ });
486
+ }
487
+ };
488
+ const completeAsyncWithUnwrappedFallback = async (state) => {
489
+ try {
490
+ return await parser.complete(state);
491
+ } catch (error) {
492
+ if (!require_annotations.isInjectedAnnotationWrapper(state)) throw error;
493
+ return await parser.complete(unwrapInjectedWrapper(state));
494
+ }
495
+ };
496
+ const parseAsyncWithUnwrappedFallback = async (context) => {
497
+ try {
498
+ const result = await parser.parse(context);
499
+ if (result.success || result.consumed !== 0 || !require_annotations.isInjectedAnnotationWrapper(context.state)) return result;
500
+ return await parser.parse({
501
+ ...context,
502
+ state: unwrapInjectedWrapper(context.state)
503
+ });
504
+ } catch (error) {
505
+ if (!require_annotations.isInjectedAnnotationWrapper(context.state)) throw error;
506
+ return await parser.parse({
507
+ ...context,
508
+ state: unwrapInjectedWrapper(context.state)
509
+ });
510
+ }
511
+ };
423
512
  const parseSync = (context) => {
424
513
  let added = context.state.length < 1;
425
- let result = syncParser.parse({
514
+ const currentItemStateWithAnnotations = context.state.at(-1) ?? require_annotations.inheritAnnotations(context.state, syncParser.initialState);
515
+ let result = parseSyncWithUnwrappedFallback({
426
516
  ...context,
427
- state: context.state.at(-1) ?? syncParser.initialState
517
+ state: currentItemStateWithAnnotations
428
518
  });
429
519
  if (!result.success) if (!added) {
430
- result = syncParser.parse({
520
+ const nextInitialState = require_annotations.inheritAnnotations(context.state, syncParser.initialState);
521
+ result = parseSyncWithUnwrappedFallback({
431
522
  ...context,
432
- state: syncParser.initialState
523
+ state: nextInitialState
433
524
  });
434
525
  if (!result.success) return result;
435
526
  added = true;
436
527
  } else return result;
528
+ const itemAnnotationSource = added ? context.state : currentItemStateWithAnnotations;
529
+ const nextItemState = require_annotations.inheritAnnotations(itemAnnotationSource, result.next.state);
437
530
  return {
438
531
  success: true,
439
532
  next: {
440
533
  ...result.next,
441
- state: [...added ? context.state : context.state.slice(0, -1), result.next.state]
534
+ state: require_annotations.annotateFreshArray(context.state, [...added ? context.state : context.state.slice(0, -1), nextItemState])
442
535
  },
443
536
  consumed: result.consumed
444
537
  };
445
538
  };
446
539
  const parseAsync = async (context) => {
447
540
  let added = context.state.length < 1;
448
- let resultOrPromise = parser.parse({
541
+ const currentItemStateWithAnnotations = context.state.at(-1) ?? require_annotations.inheritAnnotations(context.state, parser.initialState);
542
+ let result = await parseAsyncWithUnwrappedFallback({
449
543
  ...context,
450
- state: context.state.at(-1) ?? parser.initialState
544
+ state: currentItemStateWithAnnotations
451
545
  });
452
- let result = await resultOrPromise;
453
546
  if (!result.success) if (!added) {
454
- resultOrPromise = parser.parse({
547
+ const nextInitialState = require_annotations.inheritAnnotations(context.state, parser.initialState);
548
+ result = await parseAsyncWithUnwrappedFallback({
455
549
  ...context,
456
- state: parser.initialState
550
+ state: nextInitialState
457
551
  });
458
- result = await resultOrPromise;
459
552
  if (!result.success) return result;
460
553
  added = true;
461
554
  } else return result;
555
+ const itemAnnotationSource = added ? context.state : currentItemStateWithAnnotations;
556
+ const nextItemState = require_annotations.inheritAnnotations(itemAnnotationSource, result.next.state);
462
557
  return {
463
558
  success: true,
464
559
  next: {
465
560
  ...result.next,
466
- state: [...added ? context.state : context.state.slice(0, -1), result.next.state]
561
+ state: require_annotations.annotateFreshArray(context.state, [...added ? context.state : context.state.slice(0, -1), nextItemState])
467
562
  },
468
563
  consumed: result.consumed
469
564
  };
@@ -486,8 +581,8 @@ function multiple(parser, options = {}) {
486
581
  return require_mode_dispatch.dispatchByMode(parser.$mode, () => {
487
582
  const result = [];
488
583
  for (const s of state) {
489
- const valueResult = syncParser.complete(s);
490
- if (valueResult.success) result.push(valueResult.value);
584
+ const valueResult = completeSyncWithUnwrappedFallback(s);
585
+ if (valueResult.success) result.push(unwrapInjectedWrapper(valueResult.value));
491
586
  else return {
492
587
  success: false,
493
588
  error: valueResult.error
@@ -495,9 +590,9 @@ function multiple(parser, options = {}) {
495
590
  }
496
591
  return validateMultipleResult(result);
497
592
  }, async () => {
498
- const results = await Promise.all(state.map((s) => parser.complete(s)));
593
+ const results = await Promise.all(state.map((s) => completeAsyncWithUnwrappedFallback(s)));
499
594
  const values = [];
500
- for (const valueResult of results) if (valueResult.success) values.push(valueResult.value);
595
+ for (const valueResult of results) if (valueResult.success) values.push(unwrapInjectedWrapper(valueResult.value));
501
596
  else return {
502
597
  success: false,
503
598
  error: valueResult.error
@@ -507,10 +602,13 @@ function multiple(parser, options = {}) {
507
602
  },
508
603
  suggest(context, prefix) {
509
604
  const selectedValues = /* @__PURE__ */ new Set();
605
+ const suggestInitialState = require_annotations.inheritAnnotations(context.state, parser.initialState);
606
+ const suggestFallbackState = unwrapInjectedWrapper(suggestInitialState);
607
+ const hasSuggestFallbackState = suggestFallbackState !== suggestInitialState;
510
608
  for (const s of context.state) {
511
- const completed = syncParser.complete(s);
609
+ const completed = completeSyncWithUnwrappedFallback(s);
512
610
  if (completed.success) {
513
- const valueStr = String(completed.value);
611
+ const valueStr = String(unwrapInjectedWrapper(completed.value));
514
612
  selectedValues.add(valueStr);
515
613
  }
516
614
  }
@@ -518,23 +616,80 @@ function multiple(parser, options = {}) {
518
616
  if (suggestion.kind === "literal") return !selectedValues.has(suggestion.text);
519
617
  return true;
520
618
  };
619
+ const suggestionKey = (suggestion) => {
620
+ const description = suggestion.description == null ? "" : require_message.formatMessage(suggestion.description);
621
+ if (suggestion.kind === "literal") return JSON.stringify([
622
+ "literal",
623
+ suggestion.text,
624
+ description
625
+ ]);
626
+ return JSON.stringify([
627
+ "file",
628
+ suggestion.type,
629
+ suggestion.pattern ?? "",
630
+ suggestion.includeHidden === true,
631
+ suggestion.extensions == null ? "" : suggestion.extensions.join("\0"),
632
+ description
633
+ ]);
634
+ };
521
635
  return require_mode_dispatch.dispatchIterableByMode(parser.$mode, function* () {
522
- for (const s of syncParser.suggest({
636
+ const emitted = /* @__PURE__ */ new Set();
637
+ const yieldUnique = function* (suggestions) {
638
+ for (const s of suggestions) {
639
+ const key = suggestionKey(s);
640
+ if (shouldInclude(s) && !emitted.has(key)) {
641
+ emitted.add(key);
642
+ yield s;
643
+ }
644
+ }
645
+ };
646
+ let shouldTryFallback = false;
647
+ try {
648
+ yield* yieldUnique(syncParser.suggest({
649
+ ...context,
650
+ state: suggestInitialState
651
+ }, prefix));
652
+ } catch (error) {
653
+ if (!hasSuggestFallbackState) throw error;
654
+ shouldTryFallback = true;
655
+ }
656
+ if (shouldTryFallback) yield* yieldUnique(syncParser.suggest({
523
657
  ...context,
524
- state: parser.initialState
525
- }, prefix)) if (shouldInclude(s)) yield s;
658
+ state: suggestFallbackState
659
+ }, prefix));
526
660
  }, async function* () {
527
- const suggestions = parser.suggest({
661
+ const emitted = /* @__PURE__ */ new Set();
662
+ const yieldUnique = async function* (suggestions) {
663
+ for await (const s of suggestions) {
664
+ const key = suggestionKey(s);
665
+ if (shouldInclude(s) && !emitted.has(key)) {
666
+ emitted.add(key);
667
+ yield s;
668
+ }
669
+ }
670
+ };
671
+ let shouldTryFallback = false;
672
+ try {
673
+ yield* yieldUnique(parser.suggest({
674
+ ...context,
675
+ state: suggestInitialState
676
+ }, prefix));
677
+ } catch (error) {
678
+ if (!hasSuggestFallbackState) throw error;
679
+ shouldTryFallback = true;
680
+ }
681
+ if (shouldTryFallback) yield* yieldUnique(parser.suggest({
528
682
  ...context,
529
- state: parser.initialState
530
- }, prefix);
531
- for await (const s of suggestions) if (shouldInclude(s)) yield s;
683
+ state: suggestFallbackState
684
+ }, prefix));
532
685
  });
533
686
  },
534
687
  getDocFragments(state, defaultValue) {
535
- const innerState = state.kind === "unavailable" ? { kind: "unavailable" } : state.state.length > 0 ? {
688
+ const latestState = state.kind === "available" && state.state.length > 0 ? state.state.at(-1) : void 0;
689
+ const latestInnerState = latestState != null && require_annotations.isInjectedAnnotationWrapper(latestState) ? unwrapInjectedWrapper(latestState) : latestState;
690
+ const innerState = state.kind === "unavailable" ? { kind: "unavailable" } : latestInnerState !== void 0 ? {
536
691
  kind: "available",
537
- state: state.state.at(-1)
692
+ state: latestInnerState
538
693
  } : { kind: "unavailable" };
539
694
  return syncParser.getDocFragments(innerState, defaultValue != null && defaultValue.length > 0 ? defaultValue[0] : void 0);
540
695
  }