@adecore/editor-core 0.17.0 → 0.18.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.
@@ -110,6 +110,7 @@ export declare class DocumentModel {
110
110
  private startNewLineBefore;
111
111
  private editLines;
112
112
  private editSource;
113
+ private isUnclosedBrace;
113
114
  private bracketIndex;
114
115
  private deleteCharacter;
115
116
  private unindent;
package/dist/document.js CHANGED
@@ -1317,9 +1317,28 @@ export class DocumentModel {
1317
1317
  newline: (index) => this.newlineAt(index),
1318
1318
  context: (offset) => this.typingContext(offset, language),
1319
1319
  region: (line) => (/^vue$/i.test(language) ? vueRegionAt((index) => this.getLine(index).text, line) : null),
1320
- unmatchedBrace: (offset) => this.bracketIndex(language)?.unmatched.has(offset) ?? false
1320
+ unclosedBrace: (offset) => this.isUnclosedBrace(offset, language)
1321
1321
  };
1322
1322
  }
1323
+ /*
1324
+ * Whether the `{` at `offset` has no closer of its own: none in the document, or only the closer of a block around
1325
+ * it, which it took from that block. Such a closer sits shallower than the brace's line and leaves an opener before it unmatched.
1326
+ */
1327
+ isUnclosedBrace(offset, language) {
1328
+ const index = this.bracketIndex(language);
1329
+ if (index === undefined) {
1330
+ return false;
1331
+ }
1332
+ if (index.unmatched.has(offset)) {
1333
+ return true;
1334
+ }
1335
+ const closer = index.pairs.get(offset);
1336
+ const indentAt = (at) => this.getLine(this.rope.lineAt(at)).text.match(/^[\t ]*/)[0].length;
1337
+ if (closer === undefined || indentAt(closer) >= indentAt(offset)) {
1338
+ return false;
1339
+ }
1340
+ return [...index.unmatched].some((at) => at < offset && this.rope.charAt(at) === '{');
1341
+ }
1323
1342
  /* Undefined for a document too large to scan on every Enter. */
1324
1343
  bracketIndex(language) {
1325
1344
  if (this.rope.length > bracketScanLimit) {
@@ -11,7 +11,7 @@ export interface EditSource {
11
11
  newline(index: number): string;
12
12
  context(offset: number): TypingContext;
13
13
  region(line: number): VueRegion | null;
14
- unmatchedBrace(offset: number): boolean;
14
+ unclosedBrace(offset: number): boolean;
15
15
  }
16
16
  export interface EditPlan {
17
17
  edits: TextEdit[];
package/dist/enter.js CHANGED
@@ -120,7 +120,7 @@ class Enter {
120
120
  if (next === closers[last]) {
121
121
  return { from, to: afterWhitespace, text: newline + inner + newline + start, caret: newline.length + inner.length };
122
122
  }
123
- const closing = last === '{' && source.unmatchedBrace(context.bracket.at) ? this.closeBrace(from, afterWhitespace, line, rest, newline, start, inner) : null;
123
+ const closing = last === '{' && source.unclosedBrace(context.bracket.at) ? this.closeBrace(from, afterWhitespace, line, rest, newline, start, inner) : null;
124
124
  return closing ?? plain(inner);
125
125
  }
126
126
  const tag = this.openTag(trimmed, index);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adecore/editor-core",
3
- "version": "0.17.0",
3
+ "version": "0.18.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
package/src/document.ts CHANGED
@@ -1584,10 +1584,30 @@ export class DocumentModel {
1584
1584
  newline: (index) => this.newlineAt(index),
1585
1585
  context: (offset) => this.typingContext(offset, language),
1586
1586
  region: (line) => (/^vue$/i.test(language) ? vueRegionAt((index) => this.getLine(index).text, line) : null),
1587
- unmatchedBrace: (offset) => this.bracketIndex(language)?.unmatched.has(offset) ?? false
1587
+ unclosedBrace: (offset) => this.isUnclosedBrace(offset, language)
1588
1588
  };
1589
1589
  }
1590
1590
 
1591
+ /*
1592
+ * Whether the `{` at `offset` has no closer of its own: none in the document, or only the closer of a block around
1593
+ * it, which it took from that block. Such a closer sits shallower than the brace's line and leaves an opener before it unmatched.
1594
+ */
1595
+ private isUnclosedBrace(offset: number, language: string): boolean {
1596
+ const index = this.bracketIndex(language);
1597
+ if (index === undefined) {
1598
+ return false;
1599
+ }
1600
+ if (index.unmatched.has(offset)) {
1601
+ return true;
1602
+ }
1603
+ const closer = index.pairs.get(offset);
1604
+ const indentAt = (at: number): number => this.getLine(this.rope.lineAt(at)).text.match(/^[\t ]*/)![0].length;
1605
+ if (closer === undefined || indentAt(closer) >= indentAt(offset)) {
1606
+ return false;
1607
+ }
1608
+ return [...index.unmatched].some((at) => at < offset && this.rope.charAt(at) === '{');
1609
+ }
1610
+
1591
1611
  /* Undefined for a document too large to scan on every Enter. */
1592
1612
  private bracketIndex(language: string): BracketIndex | undefined {
1593
1613
  if (this.rope.length > bracketScanLimit) {
@@ -14,8 +14,8 @@ export interface EditSource {
14
14
  newline(index: number): string;
15
15
  context(offset: number): TypingContext;
16
16
  region(line: number): VueRegion | null;
17
- /* Whether a `{` at this offset has no closer anywhere in the document. */
18
- unmatchedBrace(offset: number): boolean;
17
+ /* Whether a `{` at this offset has no closer of its own, so Enter after it adds one. */
18
+ unclosedBrace(offset: number): boolean;
19
19
  }
20
20
 
21
21
  /* Edits to apply together and the selections that stand after them. */
package/src/enter.ts CHANGED
@@ -159,7 +159,7 @@ class Enter {
159
159
  return { from, to: afterWhitespace, text: newline + inner + newline + start, caret: newline.length + inner.length };
160
160
  }
161
161
  const closing =
162
- last === '{' && source.unmatchedBrace(context.bracket!.at) ? this.closeBrace(from, afterWhitespace, line, rest, newline, start, inner) : null;
162
+ last === '{' && source.unclosedBrace(context.bracket!.at) ? this.closeBrace(from, afterWhitespace, line, rest, newline, start, inner) : null;
163
163
  return closing ?? plain(inner);
164
164
  }
165
165