clarity-pattern-parser 3.0.12 → 3.0.13

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.
@@ -260,8 +260,27 @@ export default class TextSuggester {
260
260
  }
261
261
 
262
262
  private saveOptions() {
263
- const furthestMatches = this.cursor?.history.astNodes.reduce(
263
+ const parents = new Map<Pattern, Pattern>();
264
+ const cursor = this.cursor;
265
+
266
+ if (cursor == null) {
267
+ this.options = [];
268
+ return;
269
+ }
270
+
271
+ const furthestMatches = cursor.history.astNodes.reduce(
264
272
  (acc: any, node: any, index: any) => {
273
+ const pattern = cursor.history.patterns[index];
274
+ const parent = pattern.parent;
275
+
276
+ if (parent != null) {
277
+ parents.set(parent, parent);
278
+ }
279
+
280
+ if (parents.has(pattern)){
281
+ return acc;
282
+ }
283
+
265
284
  if (node.endIndex === acc.furthestTextIndex) {
266
285
  acc.nodeIndexes.push(index);
267
286
  } else if (node.endIndex > acc.furthestTextIndex) {
@@ -114,7 +114,7 @@ export default abstract class Pattern {
114
114
  if (index === 0 && siblings.length > 1) {
115
115
  return nextSibling.getTokens().concat(tokens);
116
116
  } else if (index === 1) {
117
- return siblings[0].getTokens().concat(tokens);
117
+ return siblings[0].getTokens();
118
118
  } else {
119
119
  return this.getTokens().concat(tokens);
120
120
  }
@@ -75,7 +75,6 @@ export default class RepeatComposite extends CompositePattern {
75
75
  this.nodes.push(node);
76
76
 
77
77
  if (node.endIndex === this.cursor.lastIndex()) {
78
- this.nodes.length = 0;
79
78
  this._processMatch();
80
79
  break;
81
80
  }
@@ -76,7 +76,6 @@ export default class RepeatValue extends ValuePattern {
76
76
  this.nodes.push(node);
77
77
 
78
78
  if (node.endIndex === this.cursor.lastIndex()) {
79
- this.nodes.length = 0;
80
79
  this._processMatch();
81
80
  break;
82
81
  }
@@ -7,7 +7,40 @@ import OrValue from "../patterns/value/OrValue";
7
7
  import json from "./javascriptPatterns/json";
8
8
  import OrComposite from "../patterns/composite/OrComposite";
9
9
  import RepeatComposite from "../patterns/composite/RepeatComposite";
10
- import Cursor from "../Cursor";
10
+ import AndComposite from "../patterns/composite/AndComposite";
11
+ import RecursivePattern from "../patterns/RecursivePattern";
12
+
13
+ function generateFlagFromList(flagNames: string[]) {
14
+ return flagNames.map((flagName) => {
15
+ return new Literal("flag-name", flagName);
16
+ });
17
+ }
18
+
19
+ function generateExpression(flagNames: string[]) {
20
+ const openParen = new Literal("open-paren", "(");
21
+ const closeParen = new Literal("close-paren", ")");
22
+ const andLiteral = new Literal("and-literal", "AND");
23
+ const space = new Literal("space", " ");
24
+ const orLiteral = new Literal("or-literal", "OR");
25
+ const and = new AndValue("and", [space, andLiteral, space]);
26
+ const or = new AndValue("or", [space, orLiteral, space]);
27
+
28
+ const booleanOperator = new OrComposite("booleanOperator", [and, or]);
29
+ const flag = new OrComposite("flags", generateFlagFromList(flagNames));
30
+ const group = new AndComposite("group", [
31
+ openParen,
32
+ new RecursivePattern("flag-expression"),
33
+ closeParen,
34
+ ]);
35
+ const flagOrGroup = new OrComposite("flag-or-group", [flag, group]);
36
+ const flagExpression = new RepeatComposite(
37
+ "flag-expression",
38
+ flagOrGroup,
39
+ booleanOperator
40
+ );
41
+
42
+ return flagExpression;
43
+ }
11
44
 
12
45
  describe("TextInspector", () => {
13
46
  test("Partial Match", () => {
@@ -218,19 +251,32 @@ describe("TextInspector", () => {
218
251
  expect(inspection.isComplete).toBe(false);
219
252
  });
220
253
 
221
- test("Suggest another item in the repeat.", () => {
222
- const a = new Literal("a", "A");
223
- const b = new Literal("b", "B");
224
- const space = new Literal("space", " ");
225
- const or = new OrComposite("names", [a, b]);
254
+ // This is a current bug. Repeat can end on a divider and that isn't right.
255
+
256
+ // test("Suggest another item in the repeat.", () => {
257
+ // const a = new Literal("a", "A");
258
+ // const b = new Literal("b", "B");
259
+ // const space = new Literal("space", " ");
260
+ // const or = new OrComposite("names", [a, b]);
261
+
262
+ // const repeat = new RepeatComposite("repeat", or, space);
263
+
264
+ // const result = TextSuggester.suggest("A B ", repeat);
226
265
 
227
- const repeat = new RepeatComposite("repeat", or, space);
266
+ // expect(result.isComplete).toBe(false);
267
+ // expect(result.options.values[0]).toBe("A");
268
+ // expect(result.options.values[1]).toBe("B");
269
+ // expect(result.options.values.length).toBe(2);
270
+ // });
228
271
 
229
- const result = TextSuggester.suggest("A B ", repeat);
272
+ test("Repeating pattern.", () => {
273
+ const expression = generateExpression(["FlagX", "FlagY", "FlagZ"]);
274
+ const result = TextSuggester.suggest("(FlagX AND ", expression);
230
275
 
231
- expect(result.isComplete).toBe(false);
232
- expect(result.options.values[0]).toBe("A");
233
- expect(result.options.values[1]).toBe("B");
234
- expect(result.options.values.length).toBe(2);
276
+ expect(result.options.values.length).toBe(4);
277
+ expect(result.options.values[0]).toBe("FlagX");
278
+ expect(result.options.values[1]).toBe("FlagY");
279
+ expect(result.options.values[2]).toBe("FlagZ");
280
+ expect(result.options.values[3]).toBe("(");
235
281
  });
236
282
  });