@aigne/cli 1.60.0-beta → 1.74.0-beta
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.
- package/README.md +12 -12
- package/dist/_virtual/rolldown_runtime.cjs +29 -0
- package/dist/bunwrapper.cjs +22 -0
- package/dist/bunwrapper.d.cts +1 -0
- package/dist/bunwrapper.d.mts +1 -0
- package/dist/bunwrapper.mjs +23 -0
- package/dist/bunwrapper.mjs.map +1 -0
- package/dist/cli.cjs +42 -0
- package/dist/cli.d.cts +9 -0
- package/dist/cli.d.cts.map +1 -0
- package/dist/cli.d.mts +10 -0
- package/dist/cli.d.mts.map +1 -0
- package/dist/cli.mjs +41 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/commands/aigne.cjs +23 -0
- package/dist/commands/aigne.mjs +22 -0
- package/dist/commands/aigne.mjs.map +1 -0
- package/dist/commands/app/agent.cjs +117 -0
- package/dist/commands/app/agent.mjs +113 -0
- package/dist/commands/app/agent.mjs.map +1 -0
- package/dist/commands/app/app.cjs +92 -0
- package/dist/commands/app/app.mjs +90 -0
- package/dist/commands/app/app.mjs.map +1 -0
- package/dist/commands/app/cli.cjs +6 -0
- package/dist/commands/app/cli.d.cts +1 -0
- package/dist/commands/app/cli.d.mts +1 -0
- package/dist/commands/app/cli.mjs +8 -0
- package/dist/commands/app/cli.mjs.map +1 -0
- package/dist/commands/app/upgrade.cjs +243 -0
- package/dist/commands/app/upgrade.mjs +240 -0
- package/dist/commands/app/upgrade.mjs.map +1 -0
- package/dist/commands/app.cjs +53 -0
- package/dist/commands/app.mjs +53 -0
- package/dist/commands/app.mjs.map +1 -0
- package/dist/commands/create.cjs +66 -0
- package/dist/commands/create.mjs +65 -0
- package/dist/commands/create.mjs.map +1 -0
- package/dist/commands/deploy.cjs +237 -0
- package/dist/commands/deploy.mjs +237 -0
- package/dist/commands/deploy.mjs.map +1 -0
- package/dist/commands/eval.cjs +88 -0
- package/dist/commands/eval.mjs +88 -0
- package/dist/commands/eval.mjs.map +1 -0
- package/dist/commands/hub.cjs +297 -0
- package/dist/commands/hub.mjs +294 -0
- package/dist/commands/hub.mjs.map +1 -0
- package/dist/commands/observe.cjs +49 -0
- package/dist/commands/observe.mjs +46 -0
- package/dist/commands/observe.mjs.map +1 -0
- package/dist/commands/run-skill.cjs +84 -0
- package/dist/commands/run-skill.mjs +81 -0
- package/dist/commands/run-skill.mjs.map +1 -0
- package/dist/commands/run.cjs +172 -0
- package/dist/commands/run.mjs +171 -0
- package/dist/commands/run.mjs.map +1 -0
- package/dist/commands/serve-mcp.cjs +68 -0
- package/dist/commands/serve-mcp.mjs +67 -0
- package/dist/commands/serve-mcp.mjs.map +1 -0
- package/dist/commands/test.cjs +40 -0
- package/dist/commands/test.mjs +39 -0
- package/dist/commands/test.mjs.map +1 -0
- package/dist/constants.cjs +28 -0
- package/dist/constants.d.cts +9 -0
- package/dist/constants.d.cts.map +1 -0
- package/dist/constants.d.mts +9 -0
- package/dist/constants.d.mts.map +1 -0
- package/dist/constants.mjs +24 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/global.d.cjs +0 -0
- package/dist/global.d.cts +6 -0
- package/dist/global.d.cts.map +1 -0
- package/dist/global.d.mts +6 -0
- package/dist/global.d.mts.map +1 -0
- package/dist/index.cjs +0 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +1 -0
- package/dist/tracer/terminal.cjs +336 -0
- package/dist/tracer/terminal.mjs +332 -0
- package/dist/tracer/terminal.mjs.map +1 -0
- package/dist/type.cjs +0 -0
- package/dist/type.d.cts +10 -0
- package/dist/type.d.cts.map +1 -0
- package/dist/type.d.mts +10 -0
- package/dist/type.d.mts.map +1 -0
- package/dist/type.mjs +1 -0
- package/dist/ui/utils/terminal-input.cjs +145 -0
- package/dist/ui/utils/terminal-input.mjs +144 -0
- package/dist/ui/utils/terminal-input.mjs.map +1 -0
- package/dist/ui/utils/text-buffer.cjs +865 -0
- package/dist/ui/utils/text-buffer.mjs +865 -0
- package/dist/ui/utils/text-buffer.mjs.map +1 -0
- package/dist/ui/utils/text-utils.cjs +85 -0
- package/dist/ui/utils/text-utils.mjs +78 -0
- package/dist/ui/utils/text-utils.mjs.map +1 -0
- package/dist/utils/agent-v1.cjs +180 -0
- package/dist/utils/agent-v1.d.cts +138 -0
- package/dist/utils/agent-v1.d.cts.map +1 -0
- package/dist/utils/agent-v1.d.mts +138 -0
- package/dist/utils/agent-v1.d.mts.map +1 -0
- package/dist/utils/agent-v1.mjs +179 -0
- package/dist/utils/agent-v1.mjs.map +1 -0
- package/dist/utils/aigne-hub/constants.cjs +22 -0
- package/dist/utils/aigne-hub/constants.mjs +18 -0
- package/dist/utils/aigne-hub/constants.mjs.map +1 -0
- package/dist/utils/aigne-hub/credential.cjs +179 -0
- package/dist/utils/aigne-hub/credential.mjs +175 -0
- package/dist/utils/aigne-hub/credential.mjs.map +1 -0
- package/dist/utils/aigne-hub/crypto.cjs +41 -0
- package/dist/utils/aigne-hub/crypto.mjs +33 -0
- package/dist/utils/aigne-hub/crypto.mjs.map +1 -0
- package/dist/utils/aigne-hub/model.cjs +112 -0
- package/dist/utils/aigne-hub/model.d.cts +19 -0
- package/dist/utils/aigne-hub/model.d.cts.map +1 -0
- package/dist/utils/aigne-hub/model.d.mts +19 -0
- package/dist/utils/aigne-hub/model.d.mts.map +1 -0
- package/dist/utils/aigne-hub/model.mjs +106 -0
- package/dist/utils/aigne-hub/model.mjs.map +1 -0
- package/dist/utils/aigne-hub/store/file.cjs +64 -0
- package/dist/utils/aigne-hub/store/file.mjs +64 -0
- package/dist/utils/aigne-hub/store/file.mjs.map +1 -0
- package/dist/utils/aigne-hub/store/index.cjs +37 -0
- package/dist/utils/aigne-hub/store/index.mjs +37 -0
- package/dist/utils/aigne-hub/store/index.mjs.map +1 -0
- package/dist/utils/aigne-hub/store/keytar.cjs +61 -0
- package/dist/utils/aigne-hub/store/keytar.mjs +61 -0
- package/dist/utils/aigne-hub/store/keytar.mjs.map +1 -0
- package/dist/utils/aigne-hub/store/migrate.cjs +46 -0
- package/dist/utils/aigne-hub/store/migrate.mjs +45 -0
- package/dist/utils/aigne-hub/store/migrate.mjs.map +1 -0
- package/dist/utils/aigne-hub/type.d.cts +18 -0
- package/dist/utils/aigne-hub/type.d.cts.map +1 -0
- package/dist/utils/aigne-hub/type.d.mts +18 -0
- package/dist/utils/aigne-hub/type.d.mts.map +1 -0
- package/dist/utils/aigne-hub-user.cjs +11 -0
- package/dist/utils/aigne-hub-user.d.cts +23 -0
- package/dist/utils/aigne-hub-user.d.cts.map +1 -0
- package/dist/utils/aigne-hub-user.d.mts +23 -0
- package/dist/utils/aigne-hub-user.d.mts.map +1 -0
- package/dist/utils/aigne-hub-user.mjs +11 -0
- package/dist/utils/aigne-hub-user.mjs.map +1 -0
- package/dist/utils/ascii-logo.cjs +30 -0
- package/dist/utils/ascii-logo.d.cts +5 -0
- package/dist/utils/ascii-logo.d.cts.map +1 -0
- package/dist/utils/ascii-logo.d.mts +5 -0
- package/dist/utils/ascii-logo.d.mts.map +1 -0
- package/dist/utils/{ascii-logo.js → ascii-logo.mjs} +13 -3
- package/dist/utils/ascii-logo.mjs.map +1 -0
- package/dist/utils/download.cjs +25 -0
- package/dist/utils/download.d.cts +7 -0
- package/dist/utils/download.d.cts.map +1 -0
- package/dist/utils/download.d.mts +7 -0
- package/dist/utils/download.d.mts.map +1 -0
- package/dist/utils/download.mjs +25 -0
- package/dist/utils/download.mjs.map +1 -0
- package/dist/utils/evaluation/core.cjs +84 -0
- package/dist/utils/evaluation/core.mjs +84 -0
- package/dist/utils/evaluation/core.mjs.map +1 -0
- package/dist/utils/evaluation/dataset.cjs +47 -0
- package/dist/utils/evaluation/dataset.mjs +46 -0
- package/dist/utils/evaluation/dataset.mjs.map +1 -0
- package/dist/utils/evaluation/evaluator.cjs +109 -0
- package/dist/utils/evaluation/{evaluator.js → evaluator.mjs} +48 -45
- package/dist/utils/evaluation/evaluator.mjs.map +1 -0
- package/dist/utils/evaluation/reporter.cjs +225 -0
- package/dist/utils/evaluation/reporter.mjs +220 -0
- package/dist/utils/evaluation/reporter.mjs.map +1 -0
- package/dist/utils/evaluation/runner.cjs +85 -0
- package/dist/utils/evaluation/runner.mjs +85 -0
- package/dist/utils/evaluation/runner.mjs.map +1 -0
- package/dist/utils/get-url-origin.cjs +12 -0
- package/dist/utils/get-url-origin.d.cts +5 -0
- package/dist/utils/get-url-origin.d.cts.map +1 -0
- package/dist/utils/get-url-origin.d.mts +5 -0
- package/dist/utils/get-url-origin.d.mts.map +1 -0
- package/dist/utils/get-url-origin.mjs +12 -0
- package/dist/utils/get-url-origin.mjs.map +1 -0
- package/dist/utils/inquirer/checkbox.cjs +265 -0
- package/dist/utils/inquirer/checkbox.mjs +262 -0
- package/dist/utils/inquirer/checkbox.mjs.map +1 -0
- package/dist/utils/listr.cjs +226 -0
- package/dist/utils/listr.d.cts +71 -0
- package/dist/utils/listr.d.cts.map +1 -0
- package/dist/utils/listr.d.mts +71 -0
- package/dist/utils/listr.d.mts.map +1 -0
- package/dist/utils/listr.mjs +222 -0
- package/dist/utils/listr.mjs.map +1 -0
- package/dist/utils/load-aigne.cjs +77 -0
- package/dist/utils/load-aigne.d.cts +29 -0
- package/dist/utils/load-aigne.d.cts.map +1 -0
- package/dist/utils/load-aigne.d.mts +29 -0
- package/dist/utils/load-aigne.d.mts.map +1 -0
- package/dist/utils/load-aigne.mjs +74 -0
- package/dist/utils/load-aigne.mjs.map +1 -0
- package/dist/utils/run-chat-loop.cjs +90 -0
- package/dist/utils/run-chat-loop.d.cts +20 -0
- package/dist/utils/run-chat-loop.d.cts.map +1 -0
- package/dist/utils/run-chat-loop.d.mts +20 -0
- package/dist/utils/run-chat-loop.d.mts.map +1 -0
- package/dist/utils/run-chat-loop.mjs +89 -0
- package/dist/utils/run-chat-loop.mjs.map +1 -0
- package/dist/utils/run-with-aigne.cjs +131 -0
- package/dist/utils/run-with-aigne.d.cts +46 -0
- package/dist/utils/run-with-aigne.d.cts.map +1 -0
- package/dist/utils/run-with-aigne.d.mts +46 -0
- package/dist/utils/run-with-aigne.d.mts.map +1 -0
- package/dist/utils/run-with-aigne.mjs +126 -0
- package/dist/utils/run-with-aigne.mjs.map +1 -0
- package/dist/utils/serve-mcp.cjs +91 -0
- package/dist/utils/serve-mcp.d.cts +20 -0
- package/dist/utils/serve-mcp.d.cts.map +1 -0
- package/dist/utils/serve-mcp.d.mts +20 -0
- package/dist/utils/serve-mcp.d.mts.map +1 -0
- package/dist/utils/serve-mcp.mjs +89 -0
- package/dist/utils/serve-mcp.mjs.map +1 -0
- package/dist/utils/spinner.cjs +19 -0
- package/dist/utils/spinner.d.cts +5 -0
- package/dist/utils/spinner.d.cts.map +1 -0
- package/dist/utils/spinner.d.mts +5 -0
- package/dist/utils/spinner.d.mts.map +1 -0
- package/dist/utils/spinner.mjs +19 -0
- package/dist/utils/spinner.mjs.map +1 -0
- package/dist/utils/string-utils.cjs +11 -0
- package/dist/utils/string-utils.d.cts +5 -0
- package/dist/utils/string-utils.d.cts.map +1 -0
- package/dist/utils/string-utils.d.mts +5 -0
- package/dist/utils/string-utils.d.mts.map +1 -0
- package/dist/utils/string-utils.mjs +10 -0
- package/dist/utils/string-utils.mjs.map +1 -0
- package/dist/utils/time.cjs +14 -0
- package/dist/utils/time.d.cts +5 -0
- package/dist/utils/time.d.cts.map +1 -0
- package/dist/utils/time.d.mts +5 -0
- package/dist/utils/time.d.mts.map +1 -0
- package/dist/utils/time.mjs +14 -0
- package/dist/utils/time.mjs.map +1 -0
- package/dist/utils/url.cjs +8 -0
- package/dist/utils/url.d.cts +5 -0
- package/dist/utils/url.d.cts.map +1 -0
- package/dist/utils/url.d.mts +5 -0
- package/dist/utils/url.d.mts.map +1 -0
- package/dist/utils/url.mjs +8 -0
- package/dist/utils/url.mjs.map +1 -0
- package/dist/utils/yargs.cjs +191 -0
- package/dist/utils/yargs.d.cts +96 -0
- package/dist/utils/yargs.d.cts.map +1 -0
- package/dist/utils/yargs.d.mts +96 -0
- package/dist/utils/yargs.d.mts.map +1 -0
- package/dist/utils/yargs.mjs +186 -0
- package/dist/utils/yargs.mjs.map +1 -0
- package/package.json +122 -45
- package/CHANGELOG.md +0 -5019
- package/dist/bunwrapper.d.ts +0 -2
- package/dist/bunwrapper.js +0 -18
- package/dist/cli.d.ts +0 -7
- package/dist/cli.js +0 -42
- package/dist/commands/aigne.d.ts +0 -4
- package/dist/commands/aigne.js +0 -35
- package/dist/commands/app/agent.d.ts +0 -26
- package/dist/commands/app/agent.js +0 -122
- package/dist/commands/app/app.d.ts +0 -7
- package/dist/commands/app/app.js +0 -92
- package/dist/commands/app/cli.d.ts +0 -1
- package/dist/commands/app/cli.js +0 -2
- package/dist/commands/app/upgrade.d.ts +0 -54
- package/dist/commands/app/upgrade.js +0 -236
- package/dist/commands/app.d.ts +0 -4
- package/dist/commands/app.js +0 -54
- package/dist/commands/create.d.ts +0 -6
- package/dist/commands/create.js +0 -74
- package/dist/commands/deploy.d.ts +0 -11
- package/dist/commands/deploy.js +0 -255
- package/dist/commands/eval.d.ts +0 -11
- package/dist/commands/eval.js +0 -110
- package/dist/commands/hub.d.ts +0 -3
- package/dist/commands/hub.js +0 -323
- package/dist/commands/observe.d.ts +0 -7
- package/dist/commands/observe.js +0 -41
- package/dist/commands/run-skill.d.ts +0 -6
- package/dist/commands/run-skill.js +0 -102
- package/dist/commands/run.d.ts +0 -9
- package/dist/commands/run.js +0 -187
- package/dist/commands/serve-mcp.d.ts +0 -20
- package/dist/commands/serve-mcp.js +0 -67
- package/dist/commands/test.d.ts +0 -9
- package/dist/commands/test.js +0 -33
- package/dist/constants.d.ts +0 -7
- package/dist/constants.js +0 -21
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/tracer/terminal.d.ts +0 -62
- package/dist/tracer/terminal.js +0 -404
- package/dist/type.d.ts +0 -5
- package/dist/type.js +0 -1
- package/dist/ui/utils/terminal-input.d.ts +0 -19
- package/dist/ui/utils/terminal-input.js +0 -123
- package/dist/ui/utils/text-buffer.d.ts +0 -87
- package/dist/ui/utils/text-buffer.js +0 -1059
- package/dist/ui/utils/text-utils.d.ts +0 -37
- package/dist/ui/utils/text-utils.js +0 -185
- package/dist/utils/agent-v1.d.ts +0 -134
- package/dist/utils/agent-v1.js +0 -213
- package/dist/utils/aigne-hub/constants.d.ts +0 -6
- package/dist/utils/aigne-hub/constants.js +0 -12
- package/dist/utils/aigne-hub/credential.d.ts +0 -20
- package/dist/utils/aigne-hub/credential.js +0 -182
- package/dist/utils/aigne-hub/crypto.d.ts +0 -4
- package/dist/utils/aigne-hub/crypto.js +0 -30
- package/dist/utils/aigne-hub/model.d.ts +0 -13
- package/dist/utils/aigne-hub/model.js +0 -122
- package/dist/utils/aigne-hub/store/file.d.ts +0 -15
- package/dist/utils/aigne-hub/store/file.js +0 -69
- package/dist/utils/aigne-hub/store/index.d.ts +0 -5
- package/dist/utils/aigne-hub/store/index.js +0 -43
- package/dist/utils/aigne-hub/store/keytar.d.ts +0 -15
- package/dist/utils/aigne-hub/store/keytar.js +0 -67
- package/dist/utils/aigne-hub/store/migrate.d.ts +0 -2
- package/dist/utils/aigne-hub/store/migrate.js +0 -57
- package/dist/utils/aigne-hub/type.d.ts +0 -38
- package/dist/utils/aigne-hub/type.js +0 -1
- package/dist/utils/aigne-hub-user.d.ts +0 -16
- package/dist/utils/aigne-hub-user.js +0 -10
- package/dist/utils/ascii-logo.d.ts +0 -1
- package/dist/utils/download.d.ts +0 -3
- package/dist/utils/download.js +0 -19
- package/dist/utils/evaluation/core.d.ts +0 -8
- package/dist/utils/evaluation/core.js +0 -83
- package/dist/utils/evaluation/dataset.d.ts +0 -15
- package/dist/utils/evaluation/dataset.js +0 -61
- package/dist/utils/evaluation/evaluator.d.ts +0 -9
- package/dist/utils/evaluation/reporter.d.ts +0 -28
- package/dist/utils/evaluation/reporter.js +0 -221
- package/dist/utils/evaluation/runner.d.ts +0 -16
- package/dist/utils/evaluation/runner.js +0 -129
- package/dist/utils/evaluation/type.d.ts +0 -69
- package/dist/utils/evaluation/type.js +0 -1
- package/dist/utils/get-url-origin.d.ts +0 -1
- package/dist/utils/get-url-origin.js +0 -8
- package/dist/utils/inquirer/checkbox.d.ts +0 -55
- package/dist/utils/inquirer/checkbox.js +0 -319
- package/dist/utils/listr.d.ts +0 -64
- package/dist/utils/listr.js +0 -265
- package/dist/utils/load-aigne.d.ts +0 -18
- package/dist/utils/load-aigne.js +0 -80
- package/dist/utils/run-chat-loop.d.ts +0 -15
- package/dist/utils/run-chat-loop.js +0 -87
- package/dist/utils/run-with-aigne.d.ts +0 -27
- package/dist/utils/run-with-aigne.js +0 -157
- package/dist/utils/serve-mcp.d.ts +0 -9
- package/dist/utils/serve-mcp.js +0 -93
- package/dist/utils/spinner.d.ts +0 -1
- package/dist/utils/spinner.js +0 -14
- package/dist/utils/string-utils.d.ts +0 -1
- package/dist/utils/string-utils.js +0 -4
- package/dist/utils/time.d.ts +0 -1
- package/dist/utils/time.js +0 -12
- package/dist/utils/url.d.ts +0 -1
- package/dist/utils/url.js +0 -3
- package/dist/utils/yargs.d.ts +0 -94
- package/dist/utils/yargs.js +0 -210
|
@@ -0,0 +1,865 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_text_utils = require('./text-utils.cjs');
|
|
3
|
+
let react = require("react");
|
|
4
|
+
|
|
5
|
+
//#region src/ui/utils/text-buffer.ts
|
|
6
|
+
/**
|
|
7
|
+
* @license
|
|
8
|
+
* Copyright 2025 Google LLC
|
|
9
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
10
|
+
*/
|
|
11
|
+
function isWordChar(ch) {
|
|
12
|
+
if (ch === void 0) return false;
|
|
13
|
+
return !/[\s,.;!?]/.test(ch);
|
|
14
|
+
}
|
|
15
|
+
const isWordCharStrict = (char) => /[\w\p{L}\p{N}]/u.test(char);
|
|
16
|
+
const isWhitespace = (char) => /\s/.test(char);
|
|
17
|
+
const isCombiningMark = (char) => /\p{M}/u.test(char);
|
|
18
|
+
const isWordCharWithCombining = (char) => isWordCharStrict(char) || isCombiningMark(char);
|
|
19
|
+
const getCharScript = (char) => {
|
|
20
|
+
if (/[\p{Script=Latin}]/u.test(char)) return "latin";
|
|
21
|
+
if (/[\p{Script=Han}]/u.test(char)) return "han";
|
|
22
|
+
if (/[\p{Script=Arabic}]/u.test(char)) return "arabic";
|
|
23
|
+
if (/[\p{Script=Hiragana}]/u.test(char)) return "hiragana";
|
|
24
|
+
if (/[\p{Script=Katakana}]/u.test(char)) return "katakana";
|
|
25
|
+
if (/[\p{Script=Cyrillic}]/u.test(char)) return "cyrillic";
|
|
26
|
+
return "other";
|
|
27
|
+
};
|
|
28
|
+
const isDifferentScript = (char1, char2) => {
|
|
29
|
+
if (!isWordCharStrict(char1) || !isWordCharStrict(char2)) return false;
|
|
30
|
+
return getCharScript(char1) !== getCharScript(char2);
|
|
31
|
+
};
|
|
32
|
+
const findNextWordStartInLine = (line, col) => {
|
|
33
|
+
const chars = require_text_utils.toCodePoints(line);
|
|
34
|
+
let i = col;
|
|
35
|
+
if (i >= chars.length) return null;
|
|
36
|
+
const currentChar = chars[i];
|
|
37
|
+
if (isWordCharStrict(currentChar)) while (i < chars.length && isWordCharWithCombining(chars[i])) {
|
|
38
|
+
if (i + 1 < chars.length && isWordCharStrict(chars[i + 1]) && isDifferentScript(chars[i], chars[i + 1])) {
|
|
39
|
+
i++;
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
i++;
|
|
43
|
+
}
|
|
44
|
+
else if (!isWhitespace(currentChar)) while (i < chars.length && !isWordCharStrict(chars[i]) && !isWhitespace(chars[i])) i++;
|
|
45
|
+
while (i < chars.length && isWhitespace(chars[i])) i++;
|
|
46
|
+
return i < chars.length ? i : null;
|
|
47
|
+
};
|
|
48
|
+
const findPrevWordStartInLine = (line, col) => {
|
|
49
|
+
const chars = require_text_utils.toCodePoints(line);
|
|
50
|
+
let i = col;
|
|
51
|
+
if (i <= 0) return null;
|
|
52
|
+
i--;
|
|
53
|
+
while (i >= 0 && isWhitespace(chars[i])) i--;
|
|
54
|
+
if (i < 0) return null;
|
|
55
|
+
if (isWordCharStrict(chars[i])) {
|
|
56
|
+
while (i >= 0 && isWordCharStrict(chars[i])) {
|
|
57
|
+
if (i - 1 >= 0 && isWordCharStrict(chars[i - 1]) && isDifferentScript(chars[i], chars[i - 1])) return i;
|
|
58
|
+
i--;
|
|
59
|
+
}
|
|
60
|
+
return i + 1;
|
|
61
|
+
} else {
|
|
62
|
+
while (i >= 0 && !isWordCharStrict(chars[i]) && !isWhitespace(chars[i])) i--;
|
|
63
|
+
return i + 1;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const replaceRangeInternal = (state, startRow, startCol, endRow, endCol, text) => {
|
|
67
|
+
const currentLine = (row) => state.lines[row] || "";
|
|
68
|
+
const currentLineLen = (row) => require_text_utils.cpLen(currentLine(row));
|
|
69
|
+
const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
|
|
70
|
+
if (startRow > endRow || startRow === endRow && startCol > endCol || startRow < 0 || startCol < 0 || endRow >= state.lines.length || endRow < state.lines.length && endCol > currentLineLen(endRow)) return state;
|
|
71
|
+
const newLines = [...state.lines];
|
|
72
|
+
const sCol = clamp(startCol, 0, currentLineLen(startRow));
|
|
73
|
+
const eCol = clamp(endCol, 0, currentLineLen(endRow));
|
|
74
|
+
const prefix = require_text_utils.cpSlice(currentLine(startRow), 0, sCol);
|
|
75
|
+
const suffix = require_text_utils.cpSlice(currentLine(endRow), eCol);
|
|
76
|
+
const replacementParts = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n");
|
|
77
|
+
const firstLine = prefix + replacementParts[0];
|
|
78
|
+
if (replacementParts.length === 1) newLines.splice(startRow, endRow - startRow + 1, firstLine + suffix);
|
|
79
|
+
else {
|
|
80
|
+
const lastLine = replacementParts[replacementParts.length - 1] + suffix;
|
|
81
|
+
const middleLines = replacementParts.slice(1, -1);
|
|
82
|
+
newLines.splice(startRow, endRow - startRow + 1, firstLine, ...middleLines, lastLine);
|
|
83
|
+
}
|
|
84
|
+
const finalCursorRow = startRow + replacementParts.length - 1;
|
|
85
|
+
const finalCursorCol = (replacementParts.length > 1 ? 0 : sCol) + require_text_utils.cpLen(replacementParts[replacementParts.length - 1]);
|
|
86
|
+
return {
|
|
87
|
+
...state,
|
|
88
|
+
lines: newLines,
|
|
89
|
+
cursorRow: Math.min(Math.max(finalCursorRow, 0), newLines.length - 1),
|
|
90
|
+
cursorCol: Math.max(0, Math.min(finalCursorCol, require_text_utils.cpLen(newLines[finalCursorRow] || ""))),
|
|
91
|
+
preferredCol: null
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
function clamp(v, min, max) {
|
|
95
|
+
return v < min ? min : v > max ? max : v;
|
|
96
|
+
}
|
|
97
|
+
function calculateInitialCursorPosition(initialLines, offset) {
|
|
98
|
+
let remainingChars = offset;
|
|
99
|
+
let row = 0;
|
|
100
|
+
while (row < initialLines.length) {
|
|
101
|
+
const lineLength = require_text_utils.cpLen(initialLines[row]);
|
|
102
|
+
const totalCharsInLineAndNewline = lineLength + (row < initialLines.length - 1 ? 1 : 0);
|
|
103
|
+
if (remainingChars <= lineLength) return [row, remainingChars];
|
|
104
|
+
remainingChars -= totalCharsInLineAndNewline;
|
|
105
|
+
row++;
|
|
106
|
+
}
|
|
107
|
+
if (initialLines.length > 0) {
|
|
108
|
+
const lastRow = initialLines.length - 1;
|
|
109
|
+
return [lastRow, require_text_utils.cpLen(initialLines[lastRow])];
|
|
110
|
+
}
|
|
111
|
+
return [0, 0];
|
|
112
|
+
}
|
|
113
|
+
function offsetToLogicalPos(text, offset) {
|
|
114
|
+
let row = 0;
|
|
115
|
+
let col = 0;
|
|
116
|
+
let currentOffset = 0;
|
|
117
|
+
if (offset === 0) return [0, 0];
|
|
118
|
+
const lines = text.split("\n");
|
|
119
|
+
for (let i = 0; i < lines.length; i++) {
|
|
120
|
+
const line = lines[i];
|
|
121
|
+
const lineLength = require_text_utils.cpLen(line);
|
|
122
|
+
const lineLengthWithNewline = lineLength + (i < lines.length - 1 ? 1 : 0);
|
|
123
|
+
if (offset <= currentOffset + lineLength) {
|
|
124
|
+
row = i;
|
|
125
|
+
col = offset - currentOffset;
|
|
126
|
+
return [row, col];
|
|
127
|
+
} else if (offset <= currentOffset + lineLengthWithNewline) {
|
|
128
|
+
row = i;
|
|
129
|
+
col = lineLength;
|
|
130
|
+
if (offset === currentOffset + lineLengthWithNewline && i < lines.length - 1) return [i + 1, 0];
|
|
131
|
+
return [row, col];
|
|
132
|
+
}
|
|
133
|
+
currentOffset += lineLengthWithNewline;
|
|
134
|
+
}
|
|
135
|
+
if (lines.length > 0) {
|
|
136
|
+
row = lines.length - 1;
|
|
137
|
+
col = require_text_utils.cpLen(lines[row]);
|
|
138
|
+
} else {
|
|
139
|
+
row = 0;
|
|
140
|
+
col = 0;
|
|
141
|
+
}
|
|
142
|
+
return [row, col];
|
|
143
|
+
}
|
|
144
|
+
function calculateLayout(logicalLines, viewportWidth) {
|
|
145
|
+
const visualLines = [];
|
|
146
|
+
const logicalToVisualMap = [];
|
|
147
|
+
const visualToLogicalMap = [];
|
|
148
|
+
logicalLines.forEach((logLine, logIndex) => {
|
|
149
|
+
logicalToVisualMap[logIndex] = [];
|
|
150
|
+
if (logLine.length === 0) {
|
|
151
|
+
logicalToVisualMap[logIndex].push([visualLines.length, 0]);
|
|
152
|
+
visualToLogicalMap.push([logIndex, 0]);
|
|
153
|
+
visualLines.push("");
|
|
154
|
+
} else {
|
|
155
|
+
let currentPosInLogLine = 0;
|
|
156
|
+
const codePointsInLogLine = require_text_utils.toCodePoints(logLine);
|
|
157
|
+
while (currentPosInLogLine < codePointsInLogLine.length) {
|
|
158
|
+
let currentChunk = "";
|
|
159
|
+
let currentChunkVisualWidth = 0;
|
|
160
|
+
let numCodePointsInChunk = 0;
|
|
161
|
+
let lastWordBreakPoint = -1;
|
|
162
|
+
let numCodePointsAtLastWordBreak = 0;
|
|
163
|
+
for (let i = currentPosInLogLine; i < codePointsInLogLine.length; i++) {
|
|
164
|
+
const char = codePointsInLogLine[i];
|
|
165
|
+
const charVisualWidth = require_text_utils.getCachedStringWidth(char);
|
|
166
|
+
if (currentChunkVisualWidth + charVisualWidth > viewportWidth) {
|
|
167
|
+
if (lastWordBreakPoint !== -1 && numCodePointsAtLastWordBreak > 0 && currentPosInLogLine + numCodePointsAtLastWordBreak < i) {
|
|
168
|
+
currentChunk = codePointsInLogLine.slice(currentPosInLogLine, currentPosInLogLine + numCodePointsAtLastWordBreak).join("");
|
|
169
|
+
numCodePointsInChunk = numCodePointsAtLastWordBreak;
|
|
170
|
+
} else if (numCodePointsInChunk === 0 && charVisualWidth > viewportWidth) {
|
|
171
|
+
currentChunk = char;
|
|
172
|
+
numCodePointsInChunk = 1;
|
|
173
|
+
} else if (numCodePointsInChunk === 0 && charVisualWidth <= viewportWidth) {}
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
currentChunk += char;
|
|
177
|
+
currentChunkVisualWidth += charVisualWidth;
|
|
178
|
+
numCodePointsInChunk++;
|
|
179
|
+
if (char === " ") {
|
|
180
|
+
lastWordBreakPoint = i;
|
|
181
|
+
numCodePointsAtLastWordBreak = numCodePointsInChunk - 1;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (numCodePointsInChunk === 0 && currentPosInLogLine < codePointsInLogLine.length) {
|
|
185
|
+
currentChunk = codePointsInLogLine[currentPosInLogLine];
|
|
186
|
+
numCodePointsInChunk = 1;
|
|
187
|
+
}
|
|
188
|
+
if (numCodePointsInChunk === 0 && currentPosInLogLine < codePointsInLogLine.length) {
|
|
189
|
+
currentChunk = codePointsInLogLine[currentPosInLogLine];
|
|
190
|
+
numCodePointsInChunk = 1;
|
|
191
|
+
}
|
|
192
|
+
logicalToVisualMap[logIndex].push([visualLines.length, currentPosInLogLine]);
|
|
193
|
+
visualToLogicalMap.push([logIndex, currentPosInLogLine]);
|
|
194
|
+
visualLines.push(currentChunk);
|
|
195
|
+
const logicalStartOfThisChunk = currentPosInLogLine;
|
|
196
|
+
currentPosInLogLine += numCodePointsInChunk;
|
|
197
|
+
if (logicalStartOfThisChunk + numCodePointsInChunk < codePointsInLogLine.length && currentPosInLogLine < codePointsInLogLine.length && codePointsInLogLine[currentPosInLogLine] === " ") currentPosInLogLine++;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
if (logicalLines.length === 0 || logicalLines.length === 1 && logicalLines[0] === "") {
|
|
202
|
+
if (visualLines.length === 0) {
|
|
203
|
+
visualLines.push("");
|
|
204
|
+
if (!logicalToVisualMap[0]) logicalToVisualMap[0] = [];
|
|
205
|
+
logicalToVisualMap[0].push([0, 0]);
|
|
206
|
+
visualToLogicalMap.push([0, 0]);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return {
|
|
210
|
+
visualLines,
|
|
211
|
+
logicalToVisualMap,
|
|
212
|
+
visualToLogicalMap
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function calculateVisualCursorFromLayout(layout, logicalCursor) {
|
|
216
|
+
const { logicalToVisualMap, visualLines } = layout;
|
|
217
|
+
const [logicalRow, logicalCol] = logicalCursor;
|
|
218
|
+
const segmentsForLogicalLine = logicalToVisualMap[logicalRow];
|
|
219
|
+
if (!segmentsForLogicalLine || segmentsForLogicalLine.length === 0) return [0, 0];
|
|
220
|
+
let targetSegmentIndex = segmentsForLogicalLine.findIndex(([, startColInLogical$1], index) => {
|
|
221
|
+
const nextStartColInLogical = index + 1 < segmentsForLogicalLine.length ? segmentsForLogicalLine[index + 1][1] : Infinity;
|
|
222
|
+
return logicalCol >= startColInLogical$1 && logicalCol < nextStartColInLogical;
|
|
223
|
+
});
|
|
224
|
+
if (targetSegmentIndex === -1) if (logicalCol === 0) targetSegmentIndex = 0;
|
|
225
|
+
else targetSegmentIndex = segmentsForLogicalLine.length - 1;
|
|
226
|
+
const [visualRow, startColInLogical] = segmentsForLogicalLine[targetSegmentIndex];
|
|
227
|
+
const visualCol = logicalCol - startColInLogical;
|
|
228
|
+
return [visualRow, Math.min(visualCol, require_text_utils.cpLen(visualLines[visualRow] ?? ""))];
|
|
229
|
+
}
|
|
230
|
+
const historyLimit = 100;
|
|
231
|
+
const pushUndo = (currentState) => {
|
|
232
|
+
const snapshot = {
|
|
233
|
+
lines: [...currentState.lines],
|
|
234
|
+
cursorRow: currentState.cursorRow,
|
|
235
|
+
cursorCol: currentState.cursorCol
|
|
236
|
+
};
|
|
237
|
+
const newStack = [...currentState.undoStack, snapshot];
|
|
238
|
+
if (newStack.length > historyLimit) newStack.shift();
|
|
239
|
+
return {
|
|
240
|
+
...currentState,
|
|
241
|
+
undoStack: newStack,
|
|
242
|
+
redoStack: []
|
|
243
|
+
};
|
|
244
|
+
};
|
|
245
|
+
function textBufferReducerLogic(state, action) {
|
|
246
|
+
const pushUndoLocal = pushUndo;
|
|
247
|
+
const currentLine = (r) => state.lines[r] ?? "";
|
|
248
|
+
const currentLineLen = (r) => require_text_utils.cpLen(currentLine(r));
|
|
249
|
+
switch (action.type) {
|
|
250
|
+
case "set_text": {
|
|
251
|
+
let nextState = state;
|
|
252
|
+
if (action.pushToUndo !== false) nextState = pushUndoLocal(state);
|
|
253
|
+
const newContentLines = action.payload.replace(/\r\n?/g, "\n").split("\n");
|
|
254
|
+
const lines = newContentLines.length === 0 ? [""] : newContentLines;
|
|
255
|
+
const lastNewLineIndex = lines.length - 1;
|
|
256
|
+
return {
|
|
257
|
+
...nextState,
|
|
258
|
+
lines,
|
|
259
|
+
cursorRow: lastNewLineIndex,
|
|
260
|
+
cursorCol: require_text_utils.cpLen(lines[lastNewLineIndex] ?? ""),
|
|
261
|
+
preferredCol: null
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
case "insert": {
|
|
265
|
+
const nextState = pushUndoLocal(state);
|
|
266
|
+
const newLines = [...nextState.lines];
|
|
267
|
+
let newCursorRow = nextState.cursorRow;
|
|
268
|
+
let newCursorCol = nextState.cursorCol;
|
|
269
|
+
const currentLine$1 = (r) => newLines[r] ?? "";
|
|
270
|
+
const parts = require_text_utils.stripUnsafeCharacters(action.payload.replace(/\r\n/g, "\n").replace(/\r/g, "\n")).split("\n");
|
|
271
|
+
const lineContent = currentLine$1(newCursorRow);
|
|
272
|
+
const before = require_text_utils.cpSlice(lineContent, 0, newCursorCol);
|
|
273
|
+
const after = require_text_utils.cpSlice(lineContent, newCursorCol);
|
|
274
|
+
if (parts.length > 1) {
|
|
275
|
+
newLines[newCursorRow] = before + parts[0];
|
|
276
|
+
const remainingParts = parts.slice(1);
|
|
277
|
+
const lastPartOriginal = remainingParts.pop() ?? "";
|
|
278
|
+
newLines.splice(newCursorRow + 1, 0, ...remainingParts);
|
|
279
|
+
newLines.splice(newCursorRow + parts.length - 1, 0, lastPartOriginal + after);
|
|
280
|
+
newCursorRow = newCursorRow + parts.length - 1;
|
|
281
|
+
newCursorCol = require_text_utils.cpLen(lastPartOriginal);
|
|
282
|
+
} else {
|
|
283
|
+
newLines[newCursorRow] = before + parts[0] + after;
|
|
284
|
+
newCursorCol = require_text_utils.cpLen(before) + require_text_utils.cpLen(parts[0]);
|
|
285
|
+
}
|
|
286
|
+
return {
|
|
287
|
+
...nextState,
|
|
288
|
+
lines: newLines,
|
|
289
|
+
cursorRow: newCursorRow,
|
|
290
|
+
cursorCol: newCursorCol,
|
|
291
|
+
preferredCol: null
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
case "backspace": {
|
|
295
|
+
const nextState = pushUndoLocal(state);
|
|
296
|
+
const newLines = [...nextState.lines];
|
|
297
|
+
let newCursorRow = nextState.cursorRow;
|
|
298
|
+
let newCursorCol = nextState.cursorCol;
|
|
299
|
+
const currentLine$1 = (r) => newLines[r] ?? "";
|
|
300
|
+
if (newCursorCol === 0 && newCursorRow === 0) return state;
|
|
301
|
+
if (newCursorCol > 0) {
|
|
302
|
+
const lineContent = currentLine$1(newCursorRow);
|
|
303
|
+
newLines[newCursorRow] = require_text_utils.cpSlice(lineContent, 0, newCursorCol - 1) + require_text_utils.cpSlice(lineContent, newCursorCol);
|
|
304
|
+
newCursorCol--;
|
|
305
|
+
} else if (newCursorRow > 0) {
|
|
306
|
+
const prevLineContent = currentLine$1(newCursorRow - 1);
|
|
307
|
+
const currentLineContentVal = currentLine$1(newCursorRow);
|
|
308
|
+
const newCol = require_text_utils.cpLen(prevLineContent);
|
|
309
|
+
newLines[newCursorRow - 1] = prevLineContent + currentLineContentVal;
|
|
310
|
+
newLines.splice(newCursorRow, 1);
|
|
311
|
+
newCursorRow--;
|
|
312
|
+
newCursorCol = newCol;
|
|
313
|
+
}
|
|
314
|
+
return {
|
|
315
|
+
...nextState,
|
|
316
|
+
lines: newLines,
|
|
317
|
+
cursorRow: newCursorRow,
|
|
318
|
+
cursorCol: newCursorCol,
|
|
319
|
+
preferredCol: null
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
case "set_viewport": {
|
|
323
|
+
const { width, height } = action.payload;
|
|
324
|
+
if (width === state.viewportWidth && height === state.viewportHeight) return state;
|
|
325
|
+
return {
|
|
326
|
+
...state,
|
|
327
|
+
viewportWidth: width,
|
|
328
|
+
viewportHeight: height
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
case "move": {
|
|
332
|
+
const { dir } = action.payload;
|
|
333
|
+
const { cursorRow, cursorCol, lines, visualLayout, preferredCol } = state;
|
|
334
|
+
if (dir === "left" || dir === "right" || dir === "up" || dir === "down" || dir === "home" || dir === "end") {
|
|
335
|
+
const visualCursor = calculateVisualCursorFromLayout(visualLayout, [cursorRow, cursorCol]);
|
|
336
|
+
const { visualLines, visualToLogicalMap } = visualLayout;
|
|
337
|
+
let newVisualRow = visualCursor[0];
|
|
338
|
+
let newVisualCol = visualCursor[1];
|
|
339
|
+
let newPreferredCol = preferredCol;
|
|
340
|
+
const currentVisLineLen = require_text_utils.cpLen(visualLines[newVisualRow] ?? "");
|
|
341
|
+
switch (dir) {
|
|
342
|
+
case "left":
|
|
343
|
+
newPreferredCol = null;
|
|
344
|
+
if (newVisualCol > 0) newVisualCol--;
|
|
345
|
+
else if (newVisualRow > 0) {
|
|
346
|
+
newVisualRow--;
|
|
347
|
+
newVisualCol = require_text_utils.cpLen(visualLines[newVisualRow] ?? "");
|
|
348
|
+
}
|
|
349
|
+
break;
|
|
350
|
+
case "right":
|
|
351
|
+
newPreferredCol = null;
|
|
352
|
+
if (newVisualCol < currentVisLineLen) newVisualCol++;
|
|
353
|
+
else if (newVisualRow < visualLines.length - 1) {
|
|
354
|
+
newVisualRow++;
|
|
355
|
+
newVisualCol = 0;
|
|
356
|
+
}
|
|
357
|
+
break;
|
|
358
|
+
case "up":
|
|
359
|
+
if (newVisualRow > 0) {
|
|
360
|
+
if (newPreferredCol === null) newPreferredCol = newVisualCol;
|
|
361
|
+
newVisualRow--;
|
|
362
|
+
newVisualCol = clamp(newPreferredCol, 0, require_text_utils.cpLen(visualLines[newVisualRow] ?? ""));
|
|
363
|
+
}
|
|
364
|
+
break;
|
|
365
|
+
case "down":
|
|
366
|
+
if (newVisualRow < visualLines.length - 1) {
|
|
367
|
+
if (newPreferredCol === null) newPreferredCol = newVisualCol;
|
|
368
|
+
newVisualRow++;
|
|
369
|
+
newVisualCol = clamp(newPreferredCol, 0, require_text_utils.cpLen(visualLines[newVisualRow] ?? ""));
|
|
370
|
+
}
|
|
371
|
+
break;
|
|
372
|
+
case "home":
|
|
373
|
+
newPreferredCol = null;
|
|
374
|
+
newVisualCol = 0;
|
|
375
|
+
break;
|
|
376
|
+
case "end":
|
|
377
|
+
newPreferredCol = null;
|
|
378
|
+
newVisualCol = currentVisLineLen;
|
|
379
|
+
break;
|
|
380
|
+
default: {
|
|
381
|
+
const exhaustiveCheck = dir;
|
|
382
|
+
console.error(`Unknown visual movement direction: ${exhaustiveCheck}`);
|
|
383
|
+
return state;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
if (visualToLogicalMap[newVisualRow]) {
|
|
387
|
+
const [logRow, logStartCol] = visualToLogicalMap[newVisualRow];
|
|
388
|
+
return {
|
|
389
|
+
...state,
|
|
390
|
+
cursorRow: logRow,
|
|
391
|
+
cursorCol: clamp(logStartCol + newVisualCol, 0, require_text_utils.cpLen(lines[logRow] ?? "")),
|
|
392
|
+
preferredCol: newPreferredCol
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
return state;
|
|
396
|
+
}
|
|
397
|
+
switch (dir) {
|
|
398
|
+
case "wordLeft": {
|
|
399
|
+
if (cursorCol === 0 && cursorRow === 0) return state;
|
|
400
|
+
let newCursorRow = cursorRow;
|
|
401
|
+
let newCursorCol = cursorCol;
|
|
402
|
+
if (cursorCol === 0) {
|
|
403
|
+
newCursorRow--;
|
|
404
|
+
newCursorCol = require_text_utils.cpLen(lines[newCursorRow] ?? "");
|
|
405
|
+
} else {
|
|
406
|
+
const lineContent = lines[cursorRow];
|
|
407
|
+
const arr = require_text_utils.toCodePoints(lineContent);
|
|
408
|
+
let start = cursorCol;
|
|
409
|
+
let onlySpaces = true;
|
|
410
|
+
for (let i = 0; i < start; i++) if (isWordChar(arr[i])) {
|
|
411
|
+
onlySpaces = false;
|
|
412
|
+
break;
|
|
413
|
+
}
|
|
414
|
+
if (onlySpaces && start > 0) start--;
|
|
415
|
+
else {
|
|
416
|
+
while (start > 0 && !isWordChar(arr[start - 1])) start--;
|
|
417
|
+
while (start > 0 && isWordChar(arr[start - 1])) start--;
|
|
418
|
+
}
|
|
419
|
+
newCursorCol = start;
|
|
420
|
+
}
|
|
421
|
+
return {
|
|
422
|
+
...state,
|
|
423
|
+
cursorRow: newCursorRow,
|
|
424
|
+
cursorCol: newCursorCol,
|
|
425
|
+
preferredCol: null
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
case "wordRight": {
|
|
429
|
+
if (cursorRow === lines.length - 1 && cursorCol === require_text_utils.cpLen(lines[cursorRow] ?? "")) return state;
|
|
430
|
+
let newCursorRow = cursorRow;
|
|
431
|
+
let newCursorCol = cursorCol;
|
|
432
|
+
const arr = require_text_utils.toCodePoints(lines[cursorRow] ?? "");
|
|
433
|
+
if (cursorCol >= arr.length) {
|
|
434
|
+
newCursorRow++;
|
|
435
|
+
newCursorCol = 0;
|
|
436
|
+
} else {
|
|
437
|
+
let end = cursorCol;
|
|
438
|
+
while (end < arr.length && !isWordChar(arr[end])) end++;
|
|
439
|
+
while (end < arr.length && isWordChar(arr[end])) end++;
|
|
440
|
+
newCursorCol = end;
|
|
441
|
+
}
|
|
442
|
+
return {
|
|
443
|
+
...state,
|
|
444
|
+
cursorRow: newCursorRow,
|
|
445
|
+
cursorCol: newCursorCol,
|
|
446
|
+
preferredCol: null
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
default: return state;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
case "set_cursor": return {
|
|
453
|
+
...state,
|
|
454
|
+
...action.payload
|
|
455
|
+
};
|
|
456
|
+
case "delete": {
|
|
457
|
+
const { cursorRow, cursorCol, lines } = state;
|
|
458
|
+
const lineContent = currentLine(cursorRow);
|
|
459
|
+
if (cursorCol < currentLineLen(cursorRow)) {
|
|
460
|
+
const nextState = pushUndoLocal(state);
|
|
461
|
+
const newLines = [...nextState.lines];
|
|
462
|
+
newLines[cursorRow] = require_text_utils.cpSlice(lineContent, 0, cursorCol) + require_text_utils.cpSlice(lineContent, cursorCol + 1);
|
|
463
|
+
return {
|
|
464
|
+
...nextState,
|
|
465
|
+
lines: newLines,
|
|
466
|
+
preferredCol: null
|
|
467
|
+
};
|
|
468
|
+
} else if (cursorRow < lines.length - 1) {
|
|
469
|
+
const nextState = pushUndoLocal(state);
|
|
470
|
+
const nextLineContent = currentLine(cursorRow + 1);
|
|
471
|
+
const newLines = [...nextState.lines];
|
|
472
|
+
newLines[cursorRow] = lineContent + nextLineContent;
|
|
473
|
+
newLines.splice(cursorRow + 1, 1);
|
|
474
|
+
return {
|
|
475
|
+
...nextState,
|
|
476
|
+
lines: newLines,
|
|
477
|
+
preferredCol: null
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
return state;
|
|
481
|
+
}
|
|
482
|
+
case "delete_word_left": {
|
|
483
|
+
const { cursorRow, cursorCol } = state;
|
|
484
|
+
if (cursorCol === 0 && cursorRow === 0) return state;
|
|
485
|
+
const nextState = pushUndoLocal(state);
|
|
486
|
+
const newLines = [...nextState.lines];
|
|
487
|
+
let newCursorRow = cursorRow;
|
|
488
|
+
let newCursorCol = cursorCol;
|
|
489
|
+
if (newCursorCol > 0) {
|
|
490
|
+
const lineContent = currentLine(newCursorRow);
|
|
491
|
+
const prevWordStart = findPrevWordStartInLine(lineContent, newCursorCol);
|
|
492
|
+
const start = prevWordStart === null ? 0 : prevWordStart;
|
|
493
|
+
newLines[newCursorRow] = require_text_utils.cpSlice(lineContent, 0, start) + require_text_utils.cpSlice(lineContent, newCursorCol);
|
|
494
|
+
newCursorCol = start;
|
|
495
|
+
} else {
|
|
496
|
+
const prevLineContent = currentLine(cursorRow - 1);
|
|
497
|
+
const currentLineContentVal = currentLine(cursorRow);
|
|
498
|
+
const newCol = require_text_utils.cpLen(prevLineContent);
|
|
499
|
+
newLines[cursorRow - 1] = prevLineContent + currentLineContentVal;
|
|
500
|
+
newLines.splice(cursorRow, 1);
|
|
501
|
+
newCursorRow--;
|
|
502
|
+
newCursorCol = newCol;
|
|
503
|
+
}
|
|
504
|
+
return {
|
|
505
|
+
...nextState,
|
|
506
|
+
lines: newLines,
|
|
507
|
+
cursorRow: newCursorRow,
|
|
508
|
+
cursorCol: newCursorCol,
|
|
509
|
+
preferredCol: null
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
case "delete_word_right": {
|
|
513
|
+
const { cursorRow, cursorCol, lines } = state;
|
|
514
|
+
const lineContent = currentLine(cursorRow);
|
|
515
|
+
const lineLen = require_text_utils.cpLen(lineContent);
|
|
516
|
+
if (cursorCol >= lineLen && cursorRow === lines.length - 1) return state;
|
|
517
|
+
const nextState = pushUndoLocal(state);
|
|
518
|
+
const newLines = [...nextState.lines];
|
|
519
|
+
if (cursorCol >= lineLen) {
|
|
520
|
+
newLines[cursorRow] = lineContent + currentLine(cursorRow + 1);
|
|
521
|
+
newLines.splice(cursorRow + 1, 1);
|
|
522
|
+
} else {
|
|
523
|
+
const nextWordStart = findNextWordStartInLine(lineContent, cursorCol);
|
|
524
|
+
const end = nextWordStart === null ? lineLen : nextWordStart;
|
|
525
|
+
newLines[cursorRow] = require_text_utils.cpSlice(lineContent, 0, cursorCol) + require_text_utils.cpSlice(lineContent, end);
|
|
526
|
+
}
|
|
527
|
+
return {
|
|
528
|
+
...nextState,
|
|
529
|
+
lines: newLines,
|
|
530
|
+
preferredCol: null
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
case "kill_line_right": {
|
|
534
|
+
const { cursorRow, cursorCol, lines } = state;
|
|
535
|
+
const lineContent = currentLine(cursorRow);
|
|
536
|
+
if (cursorCol < currentLineLen(cursorRow)) {
|
|
537
|
+
const nextState = pushUndoLocal(state);
|
|
538
|
+
const newLines = [...nextState.lines];
|
|
539
|
+
newLines[cursorRow] = require_text_utils.cpSlice(lineContent, 0, cursorCol);
|
|
540
|
+
return {
|
|
541
|
+
...nextState,
|
|
542
|
+
lines: newLines
|
|
543
|
+
};
|
|
544
|
+
} else if (cursorRow < lines.length - 1) {
|
|
545
|
+
const nextState = pushUndoLocal(state);
|
|
546
|
+
const nextLineContent = currentLine(cursorRow + 1);
|
|
547
|
+
const newLines = [...nextState.lines];
|
|
548
|
+
newLines[cursorRow] = lineContent + nextLineContent;
|
|
549
|
+
newLines.splice(cursorRow + 1, 1);
|
|
550
|
+
return {
|
|
551
|
+
...nextState,
|
|
552
|
+
lines: newLines,
|
|
553
|
+
preferredCol: null
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
return state;
|
|
557
|
+
}
|
|
558
|
+
case "kill_line_left": {
|
|
559
|
+
const { cursorRow, cursorCol } = state;
|
|
560
|
+
if (cursorCol > 0) {
|
|
561
|
+
const nextState = pushUndoLocal(state);
|
|
562
|
+
const lineContent = currentLine(cursorRow);
|
|
563
|
+
const newLines = [...nextState.lines];
|
|
564
|
+
newLines[cursorRow] = require_text_utils.cpSlice(lineContent, cursorCol);
|
|
565
|
+
return {
|
|
566
|
+
...nextState,
|
|
567
|
+
lines: newLines,
|
|
568
|
+
cursorCol: 0,
|
|
569
|
+
preferredCol: null
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
return state;
|
|
573
|
+
}
|
|
574
|
+
case "undo": {
|
|
575
|
+
const stateToRestore = state.undoStack[state.undoStack.length - 1];
|
|
576
|
+
if (!stateToRestore) return state;
|
|
577
|
+
const currentSnapshot = {
|
|
578
|
+
lines: [...state.lines],
|
|
579
|
+
cursorRow: state.cursorRow,
|
|
580
|
+
cursorCol: state.cursorCol
|
|
581
|
+
};
|
|
582
|
+
return {
|
|
583
|
+
...state,
|
|
584
|
+
...stateToRestore,
|
|
585
|
+
undoStack: state.undoStack.slice(0, -1),
|
|
586
|
+
redoStack: [...state.redoStack, currentSnapshot]
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
case "redo": {
|
|
590
|
+
const stateToRestore = state.redoStack[state.redoStack.length - 1];
|
|
591
|
+
if (!stateToRestore) return state;
|
|
592
|
+
const currentSnapshot = {
|
|
593
|
+
lines: [...state.lines],
|
|
594
|
+
cursorRow: state.cursorRow,
|
|
595
|
+
cursorCol: state.cursorCol
|
|
596
|
+
};
|
|
597
|
+
return {
|
|
598
|
+
...state,
|
|
599
|
+
...stateToRestore,
|
|
600
|
+
redoStack: state.redoStack.slice(0, -1),
|
|
601
|
+
undoStack: [...state.undoStack, currentSnapshot]
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
case "replace_range": {
|
|
605
|
+
const { startRow, startCol, endRow, endCol, text } = action.payload;
|
|
606
|
+
return replaceRangeInternal(pushUndoLocal(state), startRow, startCol, endRow, endCol, text);
|
|
607
|
+
}
|
|
608
|
+
case "move_to_offset": {
|
|
609
|
+
const { offset } = action.payload;
|
|
610
|
+
const [newRow, newCol] = offsetToLogicalPos(state.lines.join("\n"), offset);
|
|
611
|
+
return {
|
|
612
|
+
...state,
|
|
613
|
+
cursorRow: newRow,
|
|
614
|
+
cursorCol: newCol,
|
|
615
|
+
preferredCol: null
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
case "create_undo_snapshot": return pushUndoLocal(state);
|
|
619
|
+
default: {
|
|
620
|
+
const exhaustiveCheck = action;
|
|
621
|
+
console.error(`Unknown action encountered: ${exhaustiveCheck}`);
|
|
622
|
+
return state;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
function textBufferReducer(state, action) {
|
|
627
|
+
const newState = textBufferReducerLogic(state, action);
|
|
628
|
+
if (newState.lines !== state.lines || newState.viewportWidth !== state.viewportWidth) return {
|
|
629
|
+
...newState,
|
|
630
|
+
visualLayout: calculateLayout(newState.lines, newState.viewportWidth)
|
|
631
|
+
};
|
|
632
|
+
return newState;
|
|
633
|
+
}
|
|
634
|
+
function useTextBuffer({ initialText = "", initialCursorOffset = 0, viewport, onChange, isValidPath, shellModeActive = false }) {
|
|
635
|
+
const [state, dispatch] = (0, react.useReducer)(textBufferReducer, (0, react.useMemo)(() => {
|
|
636
|
+
const lines$1 = initialText.split("\n");
|
|
637
|
+
const [initialCursorRow, initialCursorCol] = calculateInitialCursorPosition(lines$1.length === 0 ? [""] : lines$1, initialCursorOffset);
|
|
638
|
+
const visualLayout$1 = calculateLayout(lines$1.length === 0 ? [""] : lines$1, viewport.width);
|
|
639
|
+
return {
|
|
640
|
+
lines: lines$1.length === 0 ? [""] : lines$1,
|
|
641
|
+
cursorRow: initialCursorRow,
|
|
642
|
+
cursorCol: initialCursorCol,
|
|
643
|
+
preferredCol: null,
|
|
644
|
+
undoStack: [],
|
|
645
|
+
redoStack: [],
|
|
646
|
+
clipboard: null,
|
|
647
|
+
selectionAnchor: null,
|
|
648
|
+
viewportWidth: viewport.width,
|
|
649
|
+
viewportHeight: viewport.height,
|
|
650
|
+
visualLayout: visualLayout$1
|
|
651
|
+
};
|
|
652
|
+
}, [
|
|
653
|
+
initialText,
|
|
654
|
+
initialCursorOffset,
|
|
655
|
+
viewport.width,
|
|
656
|
+
viewport.height
|
|
657
|
+
]));
|
|
658
|
+
const { lines, cursorRow, cursorCol, preferredCol, selectionAnchor, visualLayout } = state;
|
|
659
|
+
const text = (0, react.useMemo)(() => lines.join("\n"), [lines]);
|
|
660
|
+
const visualCursor = (0, react.useMemo)(() => calculateVisualCursorFromLayout(visualLayout, [cursorRow, cursorCol]), [
|
|
661
|
+
visualLayout,
|
|
662
|
+
cursorRow,
|
|
663
|
+
cursorCol
|
|
664
|
+
]);
|
|
665
|
+
const { visualLines, visualToLogicalMap } = visualLayout;
|
|
666
|
+
const [visualScrollRow, setVisualScrollRow] = (0, react.useState)(0);
|
|
667
|
+
(0, react.useEffect)(() => {
|
|
668
|
+
if (onChange) onChange(text);
|
|
669
|
+
}, [text, onChange]);
|
|
670
|
+
(0, react.useEffect)(() => {
|
|
671
|
+
dispatch({
|
|
672
|
+
type: "set_viewport",
|
|
673
|
+
payload: {
|
|
674
|
+
width: viewport.width,
|
|
675
|
+
height: viewport.height
|
|
676
|
+
}
|
|
677
|
+
});
|
|
678
|
+
}, [viewport.width, viewport.height]);
|
|
679
|
+
(0, react.useEffect)(() => {
|
|
680
|
+
const { height } = viewport;
|
|
681
|
+
const totalVisualLines = visualLines.length;
|
|
682
|
+
const maxScrollStart = Math.max(0, totalVisualLines - height);
|
|
683
|
+
let newVisualScrollRow = visualScrollRow;
|
|
684
|
+
if (visualCursor[0] < visualScrollRow) newVisualScrollRow = visualCursor[0];
|
|
685
|
+
else if (visualCursor[0] >= visualScrollRow + height) newVisualScrollRow = visualCursor[0] - height + 1;
|
|
686
|
+
newVisualScrollRow = clamp(newVisualScrollRow, 0, maxScrollStart);
|
|
687
|
+
if (newVisualScrollRow !== visualScrollRow) setVisualScrollRow(newVisualScrollRow);
|
|
688
|
+
}, [
|
|
689
|
+
visualCursor,
|
|
690
|
+
visualScrollRow,
|
|
691
|
+
viewport,
|
|
692
|
+
visualLines.length
|
|
693
|
+
]);
|
|
694
|
+
const insert = (0, react.useCallback)((ch, { paste = false } = {}) => {
|
|
695
|
+
if (/[\n\r]/.test(ch)) {
|
|
696
|
+
dispatch({
|
|
697
|
+
type: "insert",
|
|
698
|
+
payload: ch
|
|
699
|
+
});
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
if (ch.length >= 3 && !shellModeActive && paste) {
|
|
703
|
+
let potentialPath = ch.trim();
|
|
704
|
+
const quoteMatch = potentialPath.match(/^'(.*)'$/);
|
|
705
|
+
if (quoteMatch) potentialPath = quoteMatch[1];
|
|
706
|
+
potentialPath = potentialPath.trim();
|
|
707
|
+
if (isValidPath(potentialPath)) ch = `@${potentialPath} `;
|
|
708
|
+
}
|
|
709
|
+
let currentText = "";
|
|
710
|
+
for (const char of require_text_utils.toCodePoints(ch)) if (char.codePointAt(0) === 127) {
|
|
711
|
+
if (currentText.length > 0) {
|
|
712
|
+
dispatch({
|
|
713
|
+
type: "insert",
|
|
714
|
+
payload: currentText
|
|
715
|
+
});
|
|
716
|
+
currentText = "";
|
|
717
|
+
}
|
|
718
|
+
dispatch({ type: "backspace" });
|
|
719
|
+
} else currentText += char;
|
|
720
|
+
if (currentText.length > 0) dispatch({
|
|
721
|
+
type: "insert",
|
|
722
|
+
payload: currentText
|
|
723
|
+
});
|
|
724
|
+
}, [isValidPath, shellModeActive]);
|
|
725
|
+
const newline = (0, react.useCallback)(() => {
|
|
726
|
+
dispatch({
|
|
727
|
+
type: "insert",
|
|
728
|
+
payload: "\n"
|
|
729
|
+
});
|
|
730
|
+
}, []);
|
|
731
|
+
const backspace = (0, react.useCallback)(() => {
|
|
732
|
+
dispatch({ type: "backspace" });
|
|
733
|
+
}, []);
|
|
734
|
+
const del = (0, react.useCallback)(() => {
|
|
735
|
+
dispatch({ type: "delete" });
|
|
736
|
+
}, []);
|
|
737
|
+
const move = (0, react.useCallback)((dir) => {
|
|
738
|
+
dispatch({
|
|
739
|
+
type: "move",
|
|
740
|
+
payload: { dir }
|
|
741
|
+
});
|
|
742
|
+
}, [dispatch]);
|
|
743
|
+
const undo = (0, react.useCallback)(() => {
|
|
744
|
+
dispatch({ type: "undo" });
|
|
745
|
+
}, []);
|
|
746
|
+
const redo = (0, react.useCallback)(() => {
|
|
747
|
+
dispatch({ type: "redo" });
|
|
748
|
+
}, []);
|
|
749
|
+
const setText = (0, react.useCallback)((newText) => {
|
|
750
|
+
dispatch({
|
|
751
|
+
type: "set_text",
|
|
752
|
+
payload: newText
|
|
753
|
+
});
|
|
754
|
+
}, []);
|
|
755
|
+
const deleteWordLeft = (0, react.useCallback)(() => {
|
|
756
|
+
dispatch({ type: "delete_word_left" });
|
|
757
|
+
}, []);
|
|
758
|
+
const deleteWordRight = (0, react.useCallback)(() => {
|
|
759
|
+
dispatch({ type: "delete_word_right" });
|
|
760
|
+
}, []);
|
|
761
|
+
const killLineRight = (0, react.useCallback)(() => {
|
|
762
|
+
dispatch({ type: "kill_line_right" });
|
|
763
|
+
}, []);
|
|
764
|
+
const killLineLeft = (0, react.useCallback)(() => {
|
|
765
|
+
dispatch({ type: "kill_line_left" });
|
|
766
|
+
}, []);
|
|
767
|
+
const handleInput = (0, react.useCallback)((key) => {
|
|
768
|
+
const { sequence: input } = key;
|
|
769
|
+
if (key.paste) {
|
|
770
|
+
insert(input, { paste: key.paste });
|
|
771
|
+
return;
|
|
772
|
+
}
|
|
773
|
+
if (key.name === "return" || input === "\r" || input === "\n" || input === "\\\r") newline();
|
|
774
|
+
else if (key.name === "left" && !key.meta && !key.ctrl) move("left");
|
|
775
|
+
else if (key.ctrl && key.name === "b") move("left");
|
|
776
|
+
else if (key.name === "right" && !key.meta && !key.ctrl) move("right");
|
|
777
|
+
else if (key.ctrl && key.name === "f") move("right");
|
|
778
|
+
else if (key.name === "up") move("up");
|
|
779
|
+
else if (key.name === "down") move("down");
|
|
780
|
+
else if ((key.ctrl || key.meta) && key.name === "left") move("wordLeft");
|
|
781
|
+
else if (key.meta && key.name === "b") move("wordLeft");
|
|
782
|
+
else if ((key.ctrl || key.meta) && key.name === "right") move("wordRight");
|
|
783
|
+
else if (key.meta && key.name === "f") move("wordRight");
|
|
784
|
+
else if (key.name === "home") move("home");
|
|
785
|
+
else if (key.ctrl && key.name === "a") move("home");
|
|
786
|
+
else if (key.name === "end") move("end");
|
|
787
|
+
else if (key.ctrl && key.name === "e") move("end");
|
|
788
|
+
else if (key.ctrl && key.name === "w") deleteWordLeft();
|
|
789
|
+
else if ((key.meta || key.ctrl) && (key.name === "backspace" || input === "")) deleteWordLeft();
|
|
790
|
+
else if ((key.meta || key.ctrl) && key.name === "delete") deleteWordRight();
|
|
791
|
+
else if (key.name === "backspace" || input === "" || key.ctrl && key.name === "h") backspace();
|
|
792
|
+
else if (key.name === "delete" || key.ctrl && key.name === "d") del();
|
|
793
|
+
else if (key.ctrl && !key.shift && key.name === "z") undo();
|
|
794
|
+
else if (key.ctrl && key.shift && key.name === "z") redo();
|
|
795
|
+
else if (input && !key.ctrl && !key.meta) insert(input, { paste: key.paste });
|
|
796
|
+
}, [
|
|
797
|
+
newline,
|
|
798
|
+
move,
|
|
799
|
+
deleteWordLeft,
|
|
800
|
+
deleteWordRight,
|
|
801
|
+
backspace,
|
|
802
|
+
del,
|
|
803
|
+
insert,
|
|
804
|
+
undo,
|
|
805
|
+
redo
|
|
806
|
+
]);
|
|
807
|
+
const renderedVisualLines = (0, react.useMemo)(() => visualLines.slice(visualScrollRow, visualScrollRow + viewport.height), [
|
|
808
|
+
visualLines,
|
|
809
|
+
visualScrollRow,
|
|
810
|
+
viewport.height
|
|
811
|
+
]);
|
|
812
|
+
return (0, react.useMemo)(() => ({
|
|
813
|
+
lines,
|
|
814
|
+
text,
|
|
815
|
+
cursor: [cursorRow, cursorCol],
|
|
816
|
+
preferredCol,
|
|
817
|
+
selectionAnchor,
|
|
818
|
+
allVisualLines: visualLines,
|
|
819
|
+
viewportVisualLines: renderedVisualLines,
|
|
820
|
+
visualCursor,
|
|
821
|
+
visualScrollRow,
|
|
822
|
+
visualToLogicalMap,
|
|
823
|
+
setText,
|
|
824
|
+
insert,
|
|
825
|
+
newline,
|
|
826
|
+
backspace,
|
|
827
|
+
del,
|
|
828
|
+
move,
|
|
829
|
+
undo,
|
|
830
|
+
redo,
|
|
831
|
+
deleteWordLeft,
|
|
832
|
+
deleteWordRight,
|
|
833
|
+
killLineRight,
|
|
834
|
+
killLineLeft,
|
|
835
|
+
handleInput
|
|
836
|
+
}), [
|
|
837
|
+
lines,
|
|
838
|
+
text,
|
|
839
|
+
cursorRow,
|
|
840
|
+
cursorCol,
|
|
841
|
+
preferredCol,
|
|
842
|
+
selectionAnchor,
|
|
843
|
+
visualLines,
|
|
844
|
+
renderedVisualLines,
|
|
845
|
+
visualCursor,
|
|
846
|
+
visualScrollRow,
|
|
847
|
+
setText,
|
|
848
|
+
insert,
|
|
849
|
+
newline,
|
|
850
|
+
backspace,
|
|
851
|
+
del,
|
|
852
|
+
move,
|
|
853
|
+
undo,
|
|
854
|
+
redo,
|
|
855
|
+
deleteWordLeft,
|
|
856
|
+
deleteWordRight,
|
|
857
|
+
killLineRight,
|
|
858
|
+
killLineLeft,
|
|
859
|
+
handleInput,
|
|
860
|
+
visualToLogicalMap
|
|
861
|
+
]);
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
//#endregion
|
|
865
|
+
exports.useTextBuffer = useTextBuffer;
|