@adecore/editor-core 0.0.1 → 0.17.0-beta.2

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 (80) hide show
  1. package/LICENSE +110 -0
  2. package/LICENSE.apache-2.0.txt +201 -0
  3. package/NOTICE +11 -0
  4. package/README.md +40 -0
  5. package/dist/block-folds.d.ts +3 -0
  6. package/dist/block-folds.js +112 -0
  7. package/dist/brackets.d.ts +5 -0
  8. package/dist/brackets.js +136 -0
  9. package/dist/closer-tracker.d.ts +9 -0
  10. package/dist/closer-tracker.js +46 -0
  11. package/dist/comments.d.ts +14 -0
  12. package/dist/comments.js +344 -0
  13. package/dist/document.d.ts +148 -0
  14. package/dist/document.js +1729 -0
  15. package/dist/edit-source.d.ts +19 -0
  16. package/dist/edit-source.js +1 -0
  17. package/dist/enter.d.ts +17 -0
  18. package/dist/enter.js +387 -0
  19. package/dist/fold-roles.d.ts +6 -0
  20. package/dist/fold-roles.js +142 -0
  21. package/dist/index.d.ts +10 -0
  22. package/dist/index.js +7 -0
  23. package/dist/languages.d.ts +12 -0
  24. package/dist/languages.js +51 -0
  25. package/dist/lexical-folds.d.ts +10 -0
  26. package/dist/lexical-folds.js +116 -0
  27. package/dist/lexical.d.ts +16 -0
  28. package/dist/lexical.js +74 -0
  29. package/dist/line-commands.d.ts +9 -0
  30. package/dist/line-commands.js +306 -0
  31. package/dist/markup-regions.d.ts +8 -0
  32. package/dist/markup-regions.js +331 -0
  33. package/dist/offsets.d.ts +2 -0
  34. package/dist/offsets.js +14 -0
  35. package/dist/paste.d.ts +9 -0
  36. package/dist/paste.js +108 -0
  37. package/dist/preserve-case.d.ts +1 -0
  38. package/dist/preserve-case.js +61 -0
  39. package/dist/rope.d.ts +22 -0
  40. package/dist/rope.js +246 -0
  41. package/dist/search.d.ts +26 -0
  42. package/dist/search.js +85 -0
  43. package/dist/structure.d.ts +50 -0
  44. package/dist/structure.js +124 -0
  45. package/dist/text-span.d.ts +7 -0
  46. package/dist/text-span.js +211 -0
  47. package/dist/types.d.ts +70 -0
  48. package/dist/types.js +1 -0
  49. package/dist/typing-context.d.ts +30 -0
  50. package/dist/typing-context.js +270 -0
  51. package/dist/typing-handlers.d.ts +15 -0
  52. package/dist/typing-handlers.js +125 -0
  53. package/dist/words.d.ts +7 -0
  54. package/dist/words.js +72 -0
  55. package/package.json +34 -3
  56. package/src/block-folds.ts +117 -0
  57. package/src/brackets.ts +159 -0
  58. package/src/closer-tracker.ts +60 -0
  59. package/src/comments.ts +411 -0
  60. package/src/document.ts +2021 -0
  61. package/src/edit-source.ts +25 -0
  62. package/src/enter.ts +452 -0
  63. package/src/fold-roles.ts +166 -0
  64. package/src/index.ts +22 -0
  65. package/src/languages.ts +77 -0
  66. package/src/lexical-folds.ts +138 -0
  67. package/src/lexical.ts +113 -0
  68. package/src/line-commands.ts +356 -0
  69. package/src/markup-regions.ts +333 -0
  70. package/src/offsets.ts +16 -0
  71. package/src/paste.ts +126 -0
  72. package/src/preserve-case.ts +62 -0
  73. package/src/rope.ts +278 -0
  74. package/src/search.ts +119 -0
  75. package/src/structure.ts +223 -0
  76. package/src/text-span.ts +230 -0
  77. package/src/types.ts +141 -0
  78. package/src/typing-context.ts +329 -0
  79. package/src/typing-handlers.ts +143 -0
  80. package/src/words.ts +83 -0
package/LICENSE ADDED
@@ -0,0 +1,110 @@
1
+ # Functional Source License, Version 1.1, MIT Future License
2
+
3
+ ## Abbreviation
4
+
5
+ FSL-1.1-MIT
6
+
7
+ ## Notice
8
+
9
+ Copyright 2026 - present Bas Milius
10
+
11
+ ## Terms and Conditions
12
+
13
+ ### Licensor ("We")
14
+
15
+ The party offering the Software under these Terms and Conditions.
16
+
17
+ ### The Software
18
+
19
+ The "Software" is each version of the software that we make available under
20
+ these Terms and Conditions, as indicated by our inclusion of these Terms and
21
+ Conditions with the Software.
22
+
23
+ ### License Grant
24
+
25
+ Subject to your compliance with this License Grant and the Patents,
26
+ Redistribution and Trademark clauses below, we hereby grant you the right to
27
+ use, copy, modify, create derivative works, publicly perform, publicly display
28
+ and redistribute the Software for any Permitted Purpose identified below.
29
+
30
+ ### Permitted Purpose
31
+
32
+ A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
33
+ means making the Software available to others in a commercial product or
34
+ service that:
35
+
36
+ 1. substitutes for the Software;
37
+
38
+ 2. substitutes for any other product or service we offer using the Software
39
+ that exists as of the date we make the Software available; or
40
+
41
+ 3. offers the same or substantially similar functionality as the Software.
42
+
43
+ Permitted Purposes specifically include using the Software:
44
+
45
+ 1. for your internal use and access;
46
+
47
+ 2. for non-commercial education;
48
+
49
+ 3. for non-commercial research; and
50
+
51
+ 4. in connection with professional services that you provide to a licensee
52
+ using the Software in accordance with these Terms and Conditions.
53
+
54
+ ### Patents
55
+
56
+ To the extent your use for a Permitted Purpose would necessarily infringe our
57
+ patents, the license grant above includes a license under our patents. If you
58
+ make a claim against any party that the Software infringes or contributes to
59
+ the infringement of any patent, then your patent license to the Software ends
60
+ immediately.
61
+
62
+ ### Redistribution
63
+
64
+ The Terms and Conditions apply to all copies, modifications and derivatives of
65
+ the Software.
66
+
67
+ If you redistribute any copies, modifications or derivatives of the Software,
68
+ you must include a copy of or a link to these Terms and Conditions and not
69
+ remove any copyright notices provided in or with the Software.
70
+
71
+ ### Disclaimer
72
+
73
+ THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
74
+ IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
75
+ PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
76
+
77
+ IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
78
+ SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
79
+ EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
80
+
81
+ ### Trademarks
82
+
83
+ Except for displaying the License Details and identifying us as the origin of
84
+ the Software, you have no right under these Terms and Conditions to use our
85
+ trademarks, trade names, service marks or product names.
86
+
87
+ ## Grant of Future License
88
+
89
+ We hereby irrevocably grant you an additional license to use the Software under
90
+ the MIT license that is effective on the second anniversary of the date we make
91
+ the Software available. On or after that date, you may use the Software under
92
+ the MIT license, in which case the following will apply:
93
+
94
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
95
+ this software and associated documentation files (the "Software"), to deal in
96
+ the Software without restriction, including without limitation the rights to
97
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
98
+ of the Software, and to permit persons to whom the Software is furnished to do
99
+ so, subject to the following conditions:
100
+
101
+ The above copyright notice and this permission notice shall be included in all
102
+ copies or substantial portions of the Software.
103
+
104
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
105
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
106
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
107
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
108
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
109
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
110
+ SOFTWARE.
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,11 @@
1
+ @adecore/editor-core
2
+
3
+ Original project sources remain under FSL-1.1-MIT (see LICENSE), except what is named below.
4
+
5
+ The word-boundary predicates in src/words.ts are adapted from the IntelliJ Platform, Copyright
6
+ 2000-2023 JetBrains s.r.o. and contributors, under the Apache License, Version 2.0 (see
7
+ LICENSE.apache-2.0.txt). The TypeScript translation is a modified version. README.md names the
8
+ upstream file and revision.
9
+
10
+ This package is independent and is not affiliated with JetBrains or Microsoft. It does not bundle
11
+ Monaco, CodeMirror, or the IntelliJ Platform runtime.
package/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # @adecore/editor-core
2
+
3
+ [![npm](https://img.shields.io/npm/v/@adecore/editor-core)](https://www.npmjs.com/package/@adecore/editor-core)
4
+ [![Docs](https://img.shields.io/badge/docs-adecore.dev-blue)](https://adecore.dev/editor-core/)
5
+
6
+ The document model of a code editor, with no DOM and no dependencies. `DocumentModel` holds the text, the selections, an undo history and the editing commands: typing with bracket pairing, smart Enter, comment toggles, line moves, multiple carets, search and folding. It runs in a browser, Bun and Node. [`@adecore/editor`](https://adecore.dev/editor/) draws it on screen.
7
+
8
+ ## Install
9
+
10
+ ```sh
11
+ bun add @adecore/editor-core
12
+ ```
13
+
14
+ ## Use
15
+
16
+ ```ts
17
+ import { DocumentModel } from '@adecore/editor-core';
18
+
19
+ const document = new DocumentModel('const greeting = "hello";\n');
20
+
21
+ document.setSelections([{ anchor: 26, head: 26 }]);
22
+ document.typeText('(', { language: 'typescript' }); // the closer comes along
23
+ document.undo();
24
+ ```
25
+
26
+ Offsets are UTF-16 code units, lines and columns zero-based. A batch of edits is simultaneous, in the coordinates of the text before it.
27
+
28
+ ## Documentation
29
+
30
+ | Page | What it covers |
31
+ |---|---|
32
+ | [Overview](https://adecore.dev/editor-core/) | Coordinates, limits and provenance |
33
+ | [Documents and edits](https://adecore.dev/editor-core/documents) | Text, selections, edit batches, revisions, history and change events |
34
+ | [Typing and commands](https://adecore.dev/editor-core/commands) | `typeText`, `paste`, `execute` and their options |
35
+ | [Search and folding](https://adecore.dev/editor-core/search-folding) | Find and replace, folding ranges and roles |
36
+ | [API reference](https://adecore.dev/editor-core/api) | The helpers and types next to the model |
37
+
38
+ ## License
39
+
40
+ FSL-1.1-MIT, except the word-boundary predicates in `src/words.ts`, which are adapted from the IntelliJ Platform (`EditorActionUtil.java` of JetBrains/intellij-community at `7184b03af6c5370e79a01ded0df68cf56d7669da`, lines 934 through 982) under Apache 2.0. `NOTICE` and `LICENSE.apache-2.0.txt` ship with the package.
@@ -0,0 +1,3 @@
1
+ import type { DocumentLine } from './rope.ts';
2
+ import type { FoldingRange } from './structure.ts';
3
+ export declare function blockFolds(lineCount: number, getLine: (line: number) => DocumentLine, language: string): FoldingRange[];
@@ -0,0 +1,112 @@
1
+ import { rangeOf } from './lexical-folds.js';
2
+ const MARKDOWN = /^(markdown|md|mdx)$/i;
3
+ const HEADING = /^(#{1,6})\s+\S/;
4
+ const FENCE = /^\s*(`{3,}|~{3,})/;
5
+ const TABLE_ROW = /^\s*\|/;
6
+ const HEREDOC = /<<<[ \t]*(["']?)([A-Za-z_]\w*)\1\s*$/;
7
+ /*
8
+ * Front matter, code fences, tables and the sections under headings. A fence closes on a line of the same
9
+ * character and at least as long, and what a fence holds is never read as a heading or a table.
10
+ */
11
+ function markdownFolds(lineCount, getLine) {
12
+ const result = [];
13
+ const headings = [];
14
+ let first = 0;
15
+ if (lineCount > 0 && getLine(0).text.trim() === '---') {
16
+ for (let line = 1; line < lineCount; line++) {
17
+ const text = getLine(line).text.trim();
18
+ if (text === '---' || text === '...') {
19
+ result.push(rangeOf(0, line, getLine, 'block', 'front-matter'));
20
+ first = line + 1;
21
+ break;
22
+ }
23
+ }
24
+ }
25
+ let tableStart = -1;
26
+ const flushTable = (end) => {
27
+ if (tableStart !== -1 && end > tableStart) {
28
+ result.push(rangeOf(tableStart, end, getLine, 'block', 'table'));
29
+ }
30
+ tableStart = -1;
31
+ };
32
+ for (let line = first; line < lineCount; line++) {
33
+ const text = getLine(line).text;
34
+ const fence = FENCE.exec(text);
35
+ if (fence !== null) {
36
+ flushTable(line - 1);
37
+ const marker = fence[1];
38
+ let end = -1;
39
+ for (let next = line + 1; next < lineCount && end === -1; next++) {
40
+ const closer = FENCE.exec(getLine(next).text);
41
+ if (closer !== null && closer[1][0] === marker[0] && closer[1].length >= marker.length && getLine(next).text.trim() === closer[1]) {
42
+ end = next;
43
+ }
44
+ }
45
+ if (end !== -1) {
46
+ result.push(rangeOf(line, end, getLine, 'block', 'code-fence'));
47
+ line = end;
48
+ }
49
+ continue;
50
+ }
51
+ if (TABLE_ROW.test(text)) {
52
+ tableStart = tableStart === -1 ? line : tableStart;
53
+ continue;
54
+ }
55
+ flushTable(line - 1);
56
+ const heading = HEADING.exec(text);
57
+ if (heading !== null) {
58
+ headings.push({ line, level: heading[1].length });
59
+ }
60
+ }
61
+ flushTable(lineCount - 1);
62
+ for (const [index, heading] of headings.entries()) {
63
+ const next = headings.slice(index + 1).find((candidate) => candidate.level <= heading.level);
64
+ let end = (next?.line ?? lineCount) - 1;
65
+ while (end > heading.line && getLine(end).text.trim() === '') {
66
+ end--;
67
+ }
68
+ if (end > heading.line) {
69
+ result.push(rangeOf(heading.line, end, getLine, 'section'));
70
+ }
71
+ }
72
+ return result;
73
+ }
74
+ /* `<?php ... ?>` blocks that sit between markup, and heredocs and nowdocs. A block that opens the file is the file, not a fold. */
75
+ function phpFolds(lineCount, getLine) {
76
+ const result = [];
77
+ let open = -1;
78
+ for (let line = 0; line < lineCount; line++) {
79
+ const text = getLine(line).text;
80
+ const heredoc = HEREDOC.exec(text);
81
+ if (heredoc !== null) {
82
+ const closer = new RegExp(`^\\s*${heredoc[2]}\\b`);
83
+ for (let next = line + 1; next < lineCount; next++) {
84
+ if (closer.test(getLine(next).text)) {
85
+ result.push(rangeOf(line, next, getLine, 'block', 'heredoc'));
86
+ line = next;
87
+ break;
88
+ }
89
+ }
90
+ continue;
91
+ }
92
+ const opened = open === -1 ? text.search(/<\?(?:php\b|=)/) : -1;
93
+ const closed = text.indexOf('?>', Math.max(0, opened));
94
+ if (opened !== -1 && closed === -1) {
95
+ open = line === 0 ? -1 : line;
96
+ }
97
+ else if (open !== -1 && closed !== -1) {
98
+ if (line > open) {
99
+ result.push(rangeOf(open, line, getLine, 'block', 'php-tag'));
100
+ }
101
+ open = -1;
102
+ }
103
+ }
104
+ return result;
105
+ }
106
+ /* The folds of whole lines that a language has beyond brackets and indentation. */
107
+ export function blockFolds(lineCount, getLine, language) {
108
+ if (MARKDOWN.test(language)) {
109
+ return markdownFolds(lineCount, getLine);
110
+ }
111
+ return language.toLowerCase() === 'php' ? phpFolds(lineCount, getLine) : [];
112
+ }
@@ -0,0 +1,5 @@
1
+ export interface BracketIndex {
2
+ pairs: ReadonlyMap<number, number>;
3
+ unmatched: ReadonlySet<number>;
4
+ }
5
+ export declare function scanBrackets(text: string, language?: string): BracketIndex;
@@ -0,0 +1,136 @@
1
+ import { hasBlockComments, hasDashComments, hasHashComments, hashNeedsWordStart, hasMarkupComments, hasSlashComments, isPhp, isScript, isWordCharacter, openers, startsExpression, startsExpressionAfter, startsWord } from './lexical.js';
2
+ function skipLine(text, at) {
3
+ const end = text.indexOf('\n', at + 1);
4
+ return end < 0 ? text.length : end;
5
+ }
6
+ function skipComment(text, from, terminator) {
7
+ const end = text.indexOf(terminator, from);
8
+ return end < 0 ? text.length : end + terminator.length - 1;
9
+ }
10
+ function skipString(text, at, quote, endsAtLineBreak) {
11
+ let cursor = at + 1;
12
+ for (; cursor < text.length; cursor++) {
13
+ if (text[cursor] === '\\') {
14
+ cursor++;
15
+ }
16
+ else if (text[cursor] === quote || (endsAtLineBreak && /[\r\n]/.test(text[cursor]))) {
17
+ break;
18
+ }
19
+ }
20
+ return cursor;
21
+ }
22
+ function skipRegex(text, at) {
23
+ let inClass = false;
24
+ let cursor = at + 1;
25
+ for (; cursor < text.length; cursor++) {
26
+ const character = text[cursor];
27
+ if (character === '\\') {
28
+ cursor++;
29
+ }
30
+ else if (character === '[') {
31
+ inClass = true;
32
+ }
33
+ else if (character === ']') {
34
+ inClass = false;
35
+ }
36
+ else if ((character === '/' && !inClass) || /[\r\n]/.test(character)) {
37
+ break;
38
+ }
39
+ }
40
+ return cursor;
41
+ }
42
+ /* Lexical pairing for incomplete code; malformed nesting never produces crossing pairs. */
43
+ export function scanBrackets(text, language = 'typescript') {
44
+ const pairs = new Map();
45
+ const unmatched = new Set();
46
+ const stack = [];
47
+ const script = isScript(language);
48
+ const hashComments = hasHashComments(language);
49
+ const hashAtWordStart = hashNeedsWordStart(language);
50
+ const dashComments = hasDashComments(language);
51
+ const phpAttributes = isPhp(language);
52
+ const slashComments = hasSlashComments(language);
53
+ const blockComments = hasBlockComments(language);
54
+ const markupComments = hasMarkupComments(language);
55
+ let inTemplate = false;
56
+ let expectsExpression = true;
57
+ for (let at = 0; at < text.length; at++) {
58
+ const char = text[at];
59
+ const next = text[at + 1];
60
+ if (inTemplate) {
61
+ if (char === '\\') {
62
+ at++;
63
+ }
64
+ else if (char === '`') {
65
+ inTemplate = false;
66
+ expectsExpression = false;
67
+ }
68
+ else if (char === '$' && next === '{') {
69
+ stack.push({ at: ++at, close: '}', template: true });
70
+ inTemplate = false;
71
+ expectsExpression = true;
72
+ }
73
+ continue;
74
+ }
75
+ if (/\s/.test(char)) {
76
+ continue;
77
+ }
78
+ if (markupComments && text.startsWith('<!--', at)) {
79
+ at = skipComment(text, at + 4, '-->');
80
+ }
81
+ else if ((slashComments && char === '/' && next === '/') ||
82
+ (dashComments && char === '-' && next === '-') ||
83
+ (hashComments && char === '#' && !(phpAttributes && next === '[') && (!hashAtWordStart || startsWord(text, at)))) {
84
+ at = skipLine(text, at);
85
+ }
86
+ else if (blockComments && char === '/' && next === '*') {
87
+ at = skipComment(text, at + 2, '*/');
88
+ }
89
+ else if (char === '"' || char === "'") {
90
+ at = skipString(text, at, char, script);
91
+ expectsExpression = false;
92
+ }
93
+ else if (script && char === '`') {
94
+ inTemplate = true;
95
+ }
96
+ else if (script && char === '/' && expectsExpression) {
97
+ at = skipRegex(text, at);
98
+ expectsExpression = false;
99
+ }
100
+ else if (openers[char]) {
101
+ stack.push({ at, close: openers[char] });
102
+ expectsExpression = true;
103
+ }
104
+ else if (/[)\]}]/.test(char)) {
105
+ const open = stack.at(-1);
106
+ if (open?.close === char) {
107
+ stack.pop();
108
+ pairs.set(open.at, at);
109
+ pairs.set(at, open.at);
110
+ inTemplate = open.template === true;
111
+ }
112
+ else {
113
+ unmatched.add(at);
114
+ // A mismatched closer keeps a distant opener from claiming it later.
115
+ for (const abandoned of stack.splice(0)) {
116
+ unmatched.add(abandoned.at);
117
+ }
118
+ }
119
+ expectsExpression = false;
120
+ }
121
+ else if (isWordCharacter(char)) {
122
+ const start = at;
123
+ while (at + 1 < text.length && isWordCharacter(text[at + 1])) {
124
+ at++;
125
+ }
126
+ expectsExpression = startsExpression(text.slice(start, at + 1));
127
+ }
128
+ else {
129
+ expectsExpression = startsExpressionAfter(char);
130
+ }
131
+ }
132
+ for (const open of stack) {
133
+ unmatched.add(open.at);
134
+ }
135
+ return { pairs, unmatched };
136
+ }
@@ -0,0 +1,9 @@
1
+ import type { Selection, TextEdit } from './types.ts';
2
+ export declare class CloserTracker {
3
+ private marks;
4
+ add(open: number, close: number): void;
5
+ isCloserAt(offset: number): boolean;
6
+ map(edits: readonly TextEdit[]): void;
7
+ prune(selections: readonly Selection[]): void;
8
+ clear(): void;
9
+ }