@rse/ink-editor 0.0.0-stage → 0.9.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 (63) hide show
  1. package/.claude/CLAUDE.md +1 -0
  2. package/AGENTS.md +99 -0
  3. package/CHANGELOG.md +9 -0
  4. package/README.md +116 -2
  5. package/dst-stage1/ink-editor-cli.d.ts +2 -0
  6. package/dst-stage1/ink-editor-cli.js +174 -0
  7. package/dst-stage1/ink-editor-engine-emacs.d.ts +103 -0
  8. package/dst-stage1/ink-editor-engine-emacs.js +1530 -0
  9. package/dst-stage1/ink-editor-engine-nano.d.ts +78 -0
  10. package/dst-stage1/ink-editor-engine-nano.js +884 -0
  11. package/dst-stage1/ink-editor-engine-vim.d.ts +124 -0
  12. package/dst-stage1/ink-editor-engine-vim.js +2220 -0
  13. package/dst-stage1/ink-editor-engine-vscode.d.ts +79 -0
  14. package/dst-stage1/ink-editor-engine-vscode.js +850 -0
  15. package/dst-stage1/ink-editor-mode-emacs.d.ts +12 -0
  16. package/dst-stage1/ink-editor-mode-emacs.js +143 -0
  17. package/dst-stage1/ink-editor-mode-nano.d.ts +12 -0
  18. package/dst-stage1/ink-editor-mode-nano.js +140 -0
  19. package/dst-stage1/ink-editor-mode-vim.d.ts +12 -0
  20. package/dst-stage1/ink-editor-mode-vim.js +230 -0
  21. package/dst-stage1/ink-editor-mode-vscode.d.ts +12 -0
  22. package/dst-stage1/ink-editor-mode-vscode.js +133 -0
  23. package/dst-stage1/ink-editor-mode.d.ts +72 -0
  24. package/dst-stage1/ink-editor-mode.js +24 -0
  25. package/dst-stage1/ink-editor-syntax.d.ts +14 -0
  26. package/dst-stage1/ink-editor-syntax.js +149 -0
  27. package/dst-stage1/ink-editor-text.d.ts +38 -0
  28. package/dst-stage1/ink-editor-text.js +177 -0
  29. package/dst-stage1/ink-editor-widget.d.ts +15 -0
  30. package/dst-stage1/ink-editor-widget.js +132 -0
  31. package/dst-stage1/ink-editor.d.ts +13 -0
  32. package/dst-stage1/ink-editor.js +37 -0
  33. package/dst-stage1/tsc.tsbuildinfo +1 -0
  34. package/dst-stage2/ink-editor-cli.esm.js +177 -0
  35. package/dst-stage2/ink-editor.esm.js +5972 -0
  36. package/etc/c8.json +17 -0
  37. package/etc/eslint.mts +84 -0
  38. package/etc/knip.jsonc +51 -0
  39. package/etc/stx.conf +103 -0
  40. package/etc/tsc.json +29 -0
  41. package/etc/vite.mts +79 -0
  42. package/package.d/@typescript-eslint+typescript-estree+8.74.0.patch +12 -0
  43. package/package.d/vite+8.4.1.patch +12 -0
  44. package/package.json +79 -5
  45. package/src/ink-editor-cli.ts +200 -0
  46. package/src/ink-editor-engine-emacs.ts +1648 -0
  47. package/src/ink-editor-engine-nano.ts +978 -0
  48. package/src/ink-editor-engine-vim.ts +2203 -0
  49. package/src/ink-editor-engine-vscode.ts +938 -0
  50. package/src/ink-editor-mode-emacs.ts +137 -0
  51. package/src/ink-editor-mode-nano.ts +135 -0
  52. package/src/ink-editor-mode-vim.ts +223 -0
  53. package/src/ink-editor-mode-vscode.ts +129 -0
  54. package/src/ink-editor-mode.ts +137 -0
  55. package/src/ink-editor-syntax.ts +176 -0
  56. package/src/ink-editor-text.ts +205 -0
  57. package/src/ink-editor-widget.ts +182 -0
  58. package/src/ink-editor.ts +48 -0
  59. package/tst/ink-editor-1-vim.spec.ts +177 -0
  60. package/tst/ink-editor-2-emacs.spec.ts +211 -0
  61. package/tst/ink-editor-3-nano.spec.ts +187 -0
  62. package/tst/ink-editor-4-vscode.spec.ts +187 -0
  63. package/tst/tsc.json +27 -0
@@ -0,0 +1,133 @@
1
+ /*
2
+ ** Ink-Editor -- Text Editor with Exchangeable Editing Modes for Ink
3
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ **
5
+ ** Permission is hereby granted, free of charge, to any person obtaining
6
+ ** a copy of this software and associated documentation files (the
7
+ ** "Software"), to deal in the Software without restriction, including
8
+ ** without limitation the rights to use, copy, modify, merge, publish,
9
+ ** distribute, sublicense, and/or sell copies of the Software, and to
10
+ ** permit persons to whom the Software is furnished to do so, subject to
11
+ ** the following conditions:
12
+ **
13
+ ** The above copyright notice and this permission notice shall be included
14
+ ** in all copies or substantial portions of the Software.
15
+ **
16
+ ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+ /* VS Code-style editing mode, adapting the VS Code-style editing engine to the editor
25
+ (dependency-free: it knows nothing about React, Ink, or any terminal) */
26
+ /* internal dependencies */
27
+ import { literalRegExp } from "./ink-editor-text";
28
+ import { Code } from "./ink-editor-engine-vscode";
29
+ /* mapping of a key press onto VS Code key tokens like "x", "ctrl+s", or "shift+alt+down" */
30
+ const keyOf = (input, key) => {
31
+ const mods = (key.ctrl ? "ctrl+" : "") + (key.shift ? "shift+" : "") + (key.meta ? "alt+" : "");
32
+ if (key.escape)
33
+ return ["escape"];
34
+ if (key.return)
35
+ return [`${mods}enter`];
36
+ if (key.tab)
37
+ return [`${mods}tab`];
38
+ if (key.backspace)
39
+ return [`${mods}backspace`];
40
+ if (key.delete)
41
+ return [`${mods}delete`];
42
+ if (key.upArrow)
43
+ return [`${mods}up`];
44
+ if (key.downArrow)
45
+ return [`${mods}down`];
46
+ if (key.leftArrow)
47
+ return [`${mods}left`];
48
+ if (key.rightArrow)
49
+ return [`${mods}right`];
50
+ if (key.pageUp)
51
+ return [`${mods}pageup`];
52
+ if (key.pageDown)
53
+ return [`${mods}pagedown`];
54
+ if (key.home)
55
+ return [`${mods}home`];
56
+ if (key.end)
57
+ return [`${mods}end`];
58
+ if ((key.ctrl || key.meta) && input.length === 1)
59
+ return [`${mods}${input.toLowerCase()}`];
60
+ return [...input];
61
+ };
62
+ /* the outcome of a file operation of the host, with the messages of VS Code */
63
+ const outcome = (r) => {
64
+ if ("error" in r) {
65
+ if (r.error === "noname")
66
+ return { message: "No file name", error: true };
67
+ if (r.error === "exists")
68
+ return { message: `File ${r.detail} exists`, error: true, exists: true };
69
+ if (r.error === "read")
70
+ return { message: `Failed to open ${r.detail}`, error: true };
71
+ return { message: `Failed to save: ${r.detail}`, error: true };
72
+ }
73
+ return { message: `Saved ${r.name}`, error: false };
74
+ };
75
+ /* the VS Code-style editing mode, on top of the VS Code-style editing engine */
76
+ export class CodeMode extends Code {
77
+ constructor() {
78
+ super(...arguments);
79
+ /* the highlighting of the searched text (keeping its identity while unchanged) */
80
+ this.hl = { text: null, re: null };
81
+ }
82
+ /* re-bind the mode to the widget and the host */
83
+ bind(binding) {
84
+ const host = binding.host;
85
+ this.ops = binding.ops;
86
+ this.height = binding.height;
87
+ this.emit = binding.emit;
88
+ this.clip = binding.clip;
89
+ this.files = host === null ? null : {
90
+ name: () => host.name(),
91
+ modified: () => host.modified(),
92
+ write: (file, force, rename) => outcome(host.write(file, force, rename)),
93
+ quit: (failed) => { host.quit(failed); }
94
+ };
95
+ }
96
+ /* show the message of an opened file */
97
+ opened(result) {
98
+ if ("error" in result)
99
+ this.fail(outcome(result).message);
100
+ else if (result.isNew)
101
+ this.say("New file");
102
+ }
103
+ /* the cursor blinks, the searched text is highlighted, and the rows below the text end stay empty */
104
+ blink() {
105
+ return true;
106
+ }
107
+ highlight() {
108
+ const text = this.searched();
109
+ if (this.hl.text !== text)
110
+ this.hl = { text, re: text !== null ? literalRegExp(text, true) : null };
111
+ return this.hl.re;
112
+ }
113
+ filler() {
114
+ return "";
115
+ }
116
+ /* the status bar with the prompt or message, else the file, the position, and the main shortcuts */
117
+ status([l, c]) {
118
+ const sel = this.selection();
119
+ return {
120
+ prompt: this.prompt(),
121
+ message: this.message,
122
+ error: this.error,
123
+ label: "",
124
+ note: `${this.files?.name() || "Untitled"}${this.files?.modified() ? " ●" : ""}`,
125
+ info: `Ln ${l + 1}, Col ${c + 1}${sel ? ` (${sel[1] - sel[0]} selected)` : ""} `,
126
+ hints: "^S Save ^Q Quit ^F Find "
127
+ };
128
+ }
129
+ /* handle a key press (all keys are taken from the Textarea) */
130
+ key(input, key) {
131
+ return this.keys(keyOf(input, key));
132
+ }
133
+ }
@@ -0,0 +1,72 @@
1
+ import type { TextOps } from "./ink-editor-text";
2
+ export interface EditorKey {
3
+ escape: boolean;
4
+ return: boolean;
5
+ tab: boolean;
6
+ backspace: boolean;
7
+ delete: boolean;
8
+ upArrow: boolean;
9
+ downArrow: boolean;
10
+ leftArrow: boolean;
11
+ rightArrow: boolean;
12
+ pageUp: boolean;
13
+ pageDown: boolean;
14
+ home: boolean;
15
+ end: boolean;
16
+ ctrl: boolean;
17
+ meta: boolean;
18
+ shift: boolean;
19
+ }
20
+ export type EditorFile = {
21
+ name: string;
22
+ isNew: boolean;
23
+ crlf: boolean;
24
+ lines: number;
25
+ bytes: number;
26
+ };
27
+ export type EditorError = {
28
+ error: "noname" | "exists" | "read" | "write";
29
+ detail: string;
30
+ };
31
+ export type EditorResult = EditorFile | EditorError;
32
+ export interface EditorHost {
33
+ name(): string;
34
+ modified(): boolean;
35
+ write(file: string, force: boolean, rename?: boolean): EditorResult;
36
+ reload(): EditorResult;
37
+ quit(failed?: boolean): void;
38
+ }
39
+ export interface EditorBinding {
40
+ ops: TextOps;
41
+ height: number;
42
+ emit: (value: string) => void;
43
+ clip: (text: string) => void;
44
+ host: EditorHost | null;
45
+ }
46
+ export interface EditorStatus {
47
+ prompt: string | null;
48
+ message: string;
49
+ error: boolean;
50
+ label: string;
51
+ note: string;
52
+ info: string;
53
+ hints: string;
54
+ }
55
+ export interface EditorMode {
56
+ bind(binding: EditorBinding): void;
57
+ sync(value: string): void;
58
+ opened(result: EditorResult): void;
59
+ readonly value: string;
60
+ readonly off: number;
61
+ selection(): [number, number] | null | undefined;
62
+ blink(): boolean;
63
+ highlight(): RegExp | null;
64
+ filler(): string;
65
+ status(cursor: [number, number]): EditorStatus;
66
+ key(input: string, key: EditorKey): boolean;
67
+ paste?(text: string): void;
68
+ onText(value: string): void;
69
+ onCursor(cursor: [number, number]): void;
70
+ onSelect(selection: [number, number] | null): boolean;
71
+ }
72
+ export type EditorModeFactory = (value: string, ops: TextOps) => EditorMode;
@@ -0,0 +1,24 @@
1
+ /*
2
+ ** Ink-Editor -- Text Editor with Exchangeable Editing Modes for Ink
3
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ **
5
+ ** Permission is hereby granted, free of charge, to any person obtaining
6
+ ** a copy of this software and associated documentation files (the
7
+ ** "Software"), to deal in the Software without restriction, including
8
+ ** without limitation the rights to use, copy, modify, merge, publish,
9
+ ** distribute, sublicense, and/or sell copies of the Software, and to
10
+ ** permit persons to whom the Software is furnished to do so, subject to
11
+ ** the following conditions:
12
+ **
13
+ ** The above copyright notice and this permission notice shall be included
14
+ ** in all copies or substantial portions of the Software.
15
+ **
16
+ ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+ export {};
@@ -0,0 +1,14 @@
1
+ export type SyntaxStyle = {
2
+ color?: string;
3
+ bold?: boolean;
4
+ italic?: boolean;
5
+ };
6
+ export type SyntaxRange = {
7
+ start: number;
8
+ end: number;
9
+ label: string;
10
+ style: SyntaxStyle;
11
+ };
12
+ export type Syntax = (value: string) => SyntaxRange[];
13
+ export declare const syntaxLanguage: (file: string) => string | null;
14
+ export declare const syntaxLoad: (language: string | null) => Promise<Syntax | null>;
@@ -0,0 +1,149 @@
1
+ /*
2
+ ** Ink-Editor -- Text Editor with Exchangeable Editing Modes for Ink
3
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ **
5
+ ** Permission is hereby granted, free of charge, to any person obtaining
6
+ ** a copy of this software and associated documentation files (the
7
+ ** "Software"), to deal in the Software without restriction, including
8
+ ** without limitation the rights to use, copy, modify, merge, publish,
9
+ ** distribute, sublicense, and/or sell copies of the Software, and to
10
+ ** permit persons to whom the Software is furnished to do so, subject to
11
+ ** the following conditions:
12
+ **
13
+ ** The above copyright notice and this permission notice shall be included
14
+ ** in all copies or substantial portions of the Software.
15
+ **
16
+ ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+ /* syntax highlighting of a text via Shiki, as styled ranges for the Textarea */
25
+ /* external dependencies */
26
+ import path from "node:path";
27
+ import { createHighlighterCore } from "shiki/core";
28
+ import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
29
+ import { bundledLanguages, bundledLanguagesInfo } from "shiki/langs";
30
+ /* the highlight groups (a restrained theme of just the named terminal colors
31
+ gray, blue, and red, so they follow the terminal theme, plus bold and italic),
32
+ each with the TextMate scopes it covers and a unique pseudo color, by which the
33
+ group of a token is recognized again after Shiki resolved the theme */
34
+ const groups = [
35
+ { marker: "#000001", style: { color: "gray", italic: true },
36
+ scopes: ["comment", "punctuation.definition.comment"] },
37
+ { marker: "#000002", style: { color: "red" },
38
+ scopes: ["string", "constant", "markup.inline.raw", "markup.fenced_code", "punctuation.definition.string"] },
39
+ { marker: "#000003", style: { color: "blue", bold: true },
40
+ scopes: ["keyword", "storage", "variable.language"] },
41
+ { marker: "#000004", style: { color: "blue", bold: true },
42
+ scopes: ["entity.name.type", "entity.name.class", "support.type", "support.class"] },
43
+ { marker: "#000005", style: { color: "blue" },
44
+ scopes: ["entity.name.function", "support.function", "entity.name.tag",
45
+ "entity.other.attribute-name", "support.type.property-name", "meta.object-literal.key",
46
+ "markup.underline.link"] },
47
+ { marker: "#000006", style: { color: "red", bold: true },
48
+ scopes: ["constant.character.escape", "punctuation.definition.template-expression",
49
+ "punctuation.section.embedded", "meta.preprocessor", "keyword.control.directive"] },
50
+ { marker: "#000007", style: { color: "blue", bold: true },
51
+ scopes: ["markup.heading", "entity.name.section", "punctuation.definition.heading"] },
52
+ { marker: "#000008", style: { bold: true },
53
+ scopes: ["markup.bold"] },
54
+ { marker: "#000009", style: { italic: true },
55
+ scopes: ["markup.italic"] }
56
+ ];
57
+ /* the style of each highlight group, by its pseudo color */
58
+ const styleOf = new Map(groups.map((g) => [g.marker, g.style]));
59
+ /* the scopes left uncolored (more specific scopes above still win) */
60
+ const plain = ["keyword.operator", "punctuation"];
61
+ /* the language of a file, by its name, its extension, or a Shiki language id or alias */
62
+ export const syntaxLanguage = (file) => {
63
+ const known = new Map();
64
+ for (const info of bundledLanguagesInfo)
65
+ for (const name of [info.id, ...(info.aliases ?? [])])
66
+ known.set(name.toLowerCase(), info.id);
67
+ const base = path.basename(file).toLowerCase();
68
+ const ext = path.extname(base).slice(1);
69
+ return known.get(base) ?? known.get(ext) ?? null;
70
+ };
71
+ /* load the highlighting of a language, or null where there is none */
72
+ export const syntaxLoad = async (language) => {
73
+ if (language === null || !Object.hasOwn(bundledLanguages, language))
74
+ return null;
75
+ const highlighter = await createHighlighterCore({
76
+ themes: [{
77
+ name: "editor", type: "dark", fg: "#000000", bg: "#ffffff",
78
+ settings: [
79
+ ...groups.map((g) => ({ scope: g.scopes, settings: { foreground: g.marker } })),
80
+ { scope: plain, settings: { foreground: "#000000" } },
81
+ /* uncolored scopes nested in a group (like a quantifier in a regex) keep its color */
82
+ ...groups.map((g) => ({ scope: g.scopes.flatMap((s) => plain.map((p) => `${s} ${p}`)),
83
+ settings: { foreground: g.marker } }))
84
+ ]
85
+ }],
86
+ langs: [bundledLanguages[language]],
87
+ engine: createJavaScriptRegexEngine({ forgiving: true })
88
+ });
89
+ /* tokenize a single line, continuing from the grammar state at its start */
90
+ const tokenize = (line, state) => {
91
+ const result = highlighter.codeToTokensBase(line, { lang: language, theme: "editor", grammarState: state });
92
+ const tokens = [];
93
+ for (const token of result[0] ?? []) {
94
+ const style = styleOf.get((token.color ?? "").toLowerCase());
95
+ if (style !== undefined && token.content !== "")
96
+ tokens.push({ start: token.offset, end: token.offset + token.content.length, style });
97
+ }
98
+ return { tokens, end: highlighter.getLastGrammarState(result) };
99
+ };
100
+ /* whether two grammar states are equal (by their internal grammar stacks) */
101
+ const same = (a, b) => {
102
+ const sa = a.getInternalStack();
103
+ const sb = b.getInternalStack();
104
+ return sa !== undefined && sb !== undefined && sa.equals(sb);
105
+ };
106
+ /* the lines, their tokens, and their grammar states at their ends, of the last highlighting:
107
+ an edit re-tokenizes only from its first changed line on, until a line of the unchanged
108
+ rest ends in the same grammar state as before, so a keystroke costs only a few lines */
109
+ let cache = { lines: [], tokens: [], ends: [] };
110
+ return (value) => {
111
+ /* determine the unchanged leading and trailing lines */
112
+ const lines = value.split("\n");
113
+ const old = cache;
114
+ let d = 0;
115
+ while (d < lines.length && d < old.lines.length && lines[d] === old.lines[d])
116
+ d++;
117
+ let s = 0;
118
+ while (s < lines.length - d && s < old.lines.length - d
119
+ && lines[lines.length - 1 - s] === old.lines[old.lines.length - 1 - s])
120
+ s++;
121
+ const delta = lines.length - old.lines.length;
122
+ const tokens = old.tokens.slice(0, d);
123
+ const ends = old.ends.slice(0, d);
124
+ /* re-tokenize from the first changed line on, until the grammar state converges */
125
+ for (let i = d; i < lines.length; i++) {
126
+ const r = tokenize(lines[i], i > 0 ? ends[i - 1] : undefined);
127
+ tokens.push(r.tokens);
128
+ ends.push(r.end);
129
+ const j = i - delta;
130
+ if (i >= lines.length - s && j >= 0 && j < old.ends.length && same(r.end, old.ends[j])) {
131
+ for (let k = j + 1; k < old.ends.length; k++) {
132
+ tokens.push(old.tokens[k]);
133
+ ends.push(old.ends[k]);
134
+ }
135
+ break;
136
+ }
137
+ }
138
+ cache = { lines, tokens, ends };
139
+ /* the ranges with offsets relative to the text start */
140
+ const ranges = [];
141
+ let off = 0;
142
+ for (let l = 0; l < lines.length; l++) {
143
+ for (const t of tokens[l])
144
+ ranges.push({ start: off + t.start, end: off + t.end, label: "syntax", style: t.style });
145
+ off += lines[l].length + 1;
146
+ }
147
+ return ranges;
148
+ };
149
+ };
@@ -0,0 +1,38 @@
1
+ export declare const lineStart: (v: string, o: number) => number;
2
+ export declare const lineEnd: (v: string, o: number) => number;
3
+ export declare const lineNo: (v: string, o: number) => number;
4
+ export declare const lineAt: (v: string, l: number) => number;
5
+ export declare const toCursor: (v: string, o: number) => [number, number];
6
+ export declare const toOffset: (v: string, [l, c]: [number, number]) => number;
7
+ export interface TextOps {
8
+ previous(v: string, o: number): number;
9
+ next(v: string, o: number): number;
10
+ wordStart(v: string, o: number): number;
11
+ vertical(v: string, o: number, direction: 1 | -1): number;
12
+ }
13
+ export declare const isWord: (c: string | undefined) => boolean;
14
+ export declare const isIdent: (c: string | undefined) => boolean;
15
+ export declare const isSpace: (c: string | undefined) => c is " " | "\t";
16
+ export declare const isBlank: (v: string, o: number) => boolean;
17
+ export declare const wordEnd: (v: string, o: number, word?: (c: string | undefined) => boolean) => number;
18
+ export declare const wordBegin: (v: string, o: number, word?: (c: string | undefined) => boolean) => number;
19
+ export declare const paragraphEnd: (v: string, o: number) => number;
20
+ export declare const paragraphBegin: (v: string, o: number) => number;
21
+ export declare const escapeRegExp: (s: string) => string;
22
+ export declare const literalRegExp: (text: string, icase?: boolean) => RegExp;
23
+ export declare const findNext: (v: string, text: string, from: number, icase?: boolean) => {
24
+ match: [number, number];
25
+ wrapped: boolean;
26
+ } | null;
27
+ export declare const findPrevious: (v: string, text: string, before: number, icase?: boolean) => {
28
+ match: [number, number];
29
+ wrapped: boolean;
30
+ } | null;
31
+ export declare const goalColumn: (ops: TextOps, v: string, o: number) => number | null;
32
+ export declare const rowMove: (ops: TextOps, v: string, o: number, direction: 1 | -1, goal: number | null) => number;
33
+ export declare const fillParagraph: (v: string, o: number, column: number) => {
34
+ start: number;
35
+ end: number;
36
+ text: string;
37
+ off: number;
38
+ } | null;
@@ -0,0 +1,177 @@
1
+ /*
2
+ ** Ink-Editor -- Text Editor with Exchangeable Editing Modes for Ink
3
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ **
5
+ ** Permission is hereby granted, free of charge, to any person obtaining
6
+ ** a copy of this software and associated documentation files (the
7
+ ** "Software"), to deal in the Software without restriction, including
8
+ ** without limitation the rights to use, copy, modify, merge, publish,
9
+ ** distribute, sublicense, and/or sell copies of the Software, and to
10
+ ** permit persons to whom the Software is furnished to do so, subject to
11
+ ** the following conditions:
12
+ **
13
+ ** The above copyright notice and this permission notice shall be included
14
+ ** in all copies or substantial portions of the Software.
15
+ **
16
+ ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+ /* the line geometry of a text and the text operations of the front end
25
+ (dependency-free: it knows nothing about React, Ink, or any terminal) */
26
+ /* line geometry of a text */
27
+ export const lineStart = (v, o) => o <= 0 ? 0 : v.lastIndexOf("\n", o - 1) + 1;
28
+ export const lineEnd = (v, o) => { const e = v.indexOf("\n", o); return e < 0 ? v.length : e; };
29
+ export const lineNo = (v, o) => {
30
+ let n = 0;
31
+ const to = Math.min(o, v.length);
32
+ for (let i = v.indexOf("\n"); i >= 0 && i < to; i = v.indexOf("\n", i + 1))
33
+ n++;
34
+ return n;
35
+ };
36
+ export const lineAt = (v, l) => {
37
+ let s = 0;
38
+ for (let i = 0; i < l; i++) {
39
+ const e = v.indexOf("\n", s);
40
+ if (e < 0)
41
+ break;
42
+ s = e + 1;
43
+ }
44
+ return s;
45
+ };
46
+ /* conversion between an offset and a [ line, column ] cursor (column clamped to the line) */
47
+ export const toCursor = (v, o) => {
48
+ const p = Math.min(o, v.length);
49
+ return [lineNo(v, p), p - lineStart(v, p)];
50
+ };
51
+ export const toOffset = (v, [l, c]) => {
52
+ const s = lineAt(v, l);
53
+ return s + Math.max(0, Math.min(c, lineEnd(v, s) - s));
54
+ };
55
+ /* character classes (words of letters and digits, identifiers also of underscores),
56
+ and whether the line of an offset is blank */
57
+ export const isWord = (c) => c !== undefined && /[\p{L}\p{N}]/u.test(c);
58
+ export const isIdent = (c) => c !== undefined && /[\p{L}\p{N}_]/u.test(c);
59
+ export const isSpace = (c) => c === " " || c === "\t";
60
+ export const isBlank = (v, o) => /^[ \t]*$/.test(v.slice(lineStart(v, o), lineEnd(v, o)));
61
+ /* the end of the word at or after an offset, and the start of the word at or before it */
62
+ export const wordEnd = (v, o, word = isWord) => {
63
+ let i = o;
64
+ while (i < v.length && !word(v[i]))
65
+ i++;
66
+ while (i < v.length && word(v[i]))
67
+ i++;
68
+ return i;
69
+ };
70
+ export const wordBegin = (v, o, word = isWord) => {
71
+ let i = o;
72
+ while (i > 0 && !word(v[i - 1]))
73
+ i--;
74
+ while (i > 0 && word(v[i - 1]))
75
+ i--;
76
+ return i;
77
+ };
78
+ /* the blank line after resp. before the paragraph of an offset (paragraphs are separated by blank lines) */
79
+ export const paragraphEnd = (v, o) => {
80
+ let p = lineStart(v, o);
81
+ while (p < v.length && isBlank(v, p))
82
+ p = Math.min(lineEnd(v, p) + 1, v.length);
83
+ while (p < v.length && !isBlank(v, p))
84
+ p = Math.min(lineEnd(v, p) + 1, v.length);
85
+ return p;
86
+ };
87
+ export const paragraphBegin = (v, o) => {
88
+ let p = lineStart(v, o);
89
+ while (p > 0 && isBlank(v, p))
90
+ p = lineStart(v, p - 1);
91
+ while (p > 0 && !isBlank(v, p))
92
+ p = lineStart(v, p - 1);
93
+ return p;
94
+ };
95
+ /* a literal text as a regular expression, matched ignoring case without upper case letters */
96
+ export const escapeRegExp = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
97
+ export const literalRegExp = (text, icase = text === text.toLowerCase()) => new RegExp(escapeRegExp(text), icase ? "gi" : "g");
98
+ /* the matches of a literal text, the next one from an offset (wrapping around), and the previous
99
+ one starting before an offset (wrapping around), with whether wrapping was necessary */
100
+ export const findNext = (v, text, from, icase) => {
101
+ const re = literalRegExp(text, icase);
102
+ re.lastIndex = Math.max(0, from);
103
+ let m = re.exec(v);
104
+ let wrapped = false;
105
+ if (m === null) {
106
+ re.lastIndex = 0;
107
+ m = re.exec(v);
108
+ wrapped = true;
109
+ }
110
+ return m === null ? null : { match: [m.index, m.index + m[0].length], wrapped };
111
+ };
112
+ export const findPrevious = (v, text, before, icase) => {
113
+ const all = [...v.matchAll(literalRegExp(text, icase))].map((m) => [m.index, m.index + m[0].length]);
114
+ const hit = all.findLast((m) => m[0] < before);
115
+ if (hit !== undefined)
116
+ return { match: hit, wrapped: false };
117
+ const last = all.at(-1);
118
+ return last === undefined ? null : { match: last, wrapped: true };
119
+ };
120
+ /* whether the line of an offset wraps into multiple rows */
121
+ const isWrapped = (ops, v, o) => {
122
+ const s = lineStart(v, o);
123
+ const e = lineEnd(v, o);
124
+ const t = ops.vertical(v, s, 1);
125
+ return t > s && t < e;
126
+ };
127
+ /* the goal column of a sequence of row moves (null for a wrapped line) */
128
+ export const goalColumn = (ops, v, o) => isWrapped(ops, v, o) ? null : o - lineStart(v, o);
129
+ /* the offset one row above or below, keeping the goal column between lines which are not wrapped */
130
+ export const rowMove = (ops, v, o, direction, goal) => {
131
+ let t = ops.vertical(v, o, direction);
132
+ if (t !== o && goal !== null && lineNo(v, t) !== lineNo(v, o) && !isWrapped(ops, v, t)) {
133
+ const s = lineStart(v, t);
134
+ t = s + Math.min(goal, lineEnd(v, t) - s);
135
+ if (ops.next(v, ops.previous(v, t)) !== t)
136
+ t = ops.previous(v, t);
137
+ }
138
+ return t;
139
+ };
140
+ /* fill the paragraph of an offset to a column, keeping the offset at the same non-blank character
141
+ (the replaced range and its new text, or null on a blank line) */
142
+ export const fillParagraph = (v, o, column) => {
143
+ if (isBlank(v, o))
144
+ return null;
145
+ let start = lineStart(v, o);
146
+ while (start > 0 && !isBlank(v, start - 1))
147
+ start = lineStart(v, start - 1);
148
+ let end = lineEnd(v, o);
149
+ while (end < v.length && !isBlank(v, end + 1))
150
+ end = lineEnd(v, end + 1);
151
+ const old = v.slice(start, end);
152
+ const indent = /^[ \t]*/.exec(old)?.[0] ?? "";
153
+ const lines = [];
154
+ let line = "";
155
+ for (const w of old.split(/[ \t\n]+/).filter((x) => x !== "")) {
156
+ if (line !== "" && indent.length + line.length + 1 + w.length > column) {
157
+ lines.push(indent + line);
158
+ line = w;
159
+ }
160
+ else
161
+ line = line === "" ? w : line + " " + w;
162
+ }
163
+ if (line !== "")
164
+ lines.push(indent + line);
165
+ const text = lines.join("\n");
166
+ let k = v.slice(start, o).replace(/[ \t\n]/g, "").length;
167
+ let p = 0;
168
+ while (p < text.length && k > 0) {
169
+ if (!/[ \t\n]/.test(text[p]))
170
+ k--;
171
+ p++;
172
+ }
173
+ if (o < end && !/[ \t\n]/.test(v[o]))
174
+ while (p < text.length && /[ \t\n]/.test(text[p]))
175
+ p++;
176
+ return { start, end, text, off: start + p };
177
+ };
@@ -0,0 +1,15 @@
1
+ import React from "react";
2
+ import type { EditorModeFactory, EditorHost, EditorResult } from "./ink-editor-mode";
3
+ import type { Syntax } from "./ink-editor-syntax";
4
+ export interface EditorProps {
5
+ value: string;
6
+ onChange: (value: string) => void;
7
+ height: number;
8
+ width?: number;
9
+ focus?: boolean;
10
+ mode: EditorModeFactory;
11
+ host?: EditorHost;
12
+ file?: EditorResult | null;
13
+ syntax?: Syntax | null;
14
+ }
15
+ export declare function Editor(props: EditorProps): React.ReactElement;