pi-agent-ide 0.6.3 → 0.7.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.
- package/CHANGELOG.md +97 -0
- package/README.md +10 -3
- package/dist/{chunk-AWXVJ5AC.js → chunk-2RTCSZB3.js} +1 -1
- package/dist/chunk-3HYXQ4YA.js +28 -0
- package/dist/chunk-3JO7QFIL.js +1 -0
- package/dist/chunk-4NIFBFFR.js +11 -0
- package/dist/{chunk-VCTK4JSN.js → chunk-64M3SQPG.js} +1 -1
- package/dist/{chunk-5QQABP47.js → chunk-7LD2OQH3.js} +1 -1
- package/dist/chunk-7LZKOHH2.js +1 -0
- package/dist/chunk-AJRIASPQ.js +2 -0
- package/dist/chunk-EAZGN5ND.js +7 -0
- package/dist/{chunk-ZM4L5AGN.js → chunk-EDURPQAV.js} +3 -2
- package/dist/{chunk-GIRAEVUJ.js → chunk-F5YZG77W.js} +1 -1
- package/dist/chunk-FK42RSF4.js +1 -0
- package/dist/{chunk-O6LE42GK.js → chunk-FXNKMNSJ.js} +1 -1
- package/dist/chunk-GBLKIB6O.js +2 -0
- package/dist/chunk-KBP3QUDT.js +2 -0
- package/dist/chunk-KR36BT5E.js +1 -0
- package/dist/{chunk-NTYXZ3EL.js → chunk-KVHDMIZS.js} +1 -1
- package/dist/{chunk-QDST6VIU.js → chunk-KZC7TI5M.js} +1 -1
- package/dist/chunk-OE7QVAI2.js +5 -0
- package/dist/{chunk-GUTQ3PWL.js → chunk-PLKYHUNT.js} +1 -1
- package/dist/chunk-PNU3E7IO.js +3 -0
- package/dist/chunk-PS2B6THP.js +3 -0
- package/dist/chunk-PYPPV2F5.js +25 -0
- package/dist/{chunk-25CZ5ZIU.js → chunk-QFTBY67N.js} +3 -3
- package/dist/chunk-S5LXX32D.js +28 -0
- package/dist/chunk-SILJ3OL4.js +9 -0
- package/dist/chunk-SQKAXYIR.js +2 -0
- package/dist/chunk-VCGNVTXQ.js +1 -0
- package/dist/chunk-XYS4FSCC.js +12 -0
- package/dist/chunk-YGGKGZO2.js +1 -0
- package/dist/{chunk-L7JMDDSD.js → chunk-YRF7OATP.js} +1 -1
- package/dist/chunk-ZPT5D4VP.js +2 -0
- package/dist/entry-0.js +1 -1
- package/dist/entry-11.js +1 -1
- package/dist/entry-13.js +1 -1
- package/dist/entry-14.js +1 -1
- package/dist/entry-15.js +1 -1
- package/dist/entry-16.js +1 -1
- package/dist/entry-17.js +1 -1
- package/dist/entry-25.js +2 -2
- package/dist/entry-26.js +1 -1
- package/dist/entry-27.js +1 -1
- package/dist/entry-28.js +1 -1
- package/dist/entry-29.js +1 -1
- package/dist/entry-30.js +1 -1
- package/dist/entry-31.js +11 -9
- package/dist/entry-33.js +1 -1
- package/dist/entry-37.js +6 -6
- package/dist/entry-42.js +1 -1
- package/dist/entry-44.js +1 -1
- package/dist/entry-45.js +12 -11
- package/dist/entry-46.js +1 -1
- package/dist/entry-49.js +1 -1
- package/dist/entry-50.js +3 -6
- package/dist/entry-51.js +11 -9
- package/dist/entry-52.js +1 -1
- package/dist/entry-53.js +1 -1
- package/dist/entry-54.js +1 -1
- package/dist/entry-55.js +1 -1
- package/dist/entry-56.js +1 -1
- package/dist/entry-57.js +1 -1
- package/dist/entry-58.js +1 -1
- package/dist/entry-59.js +1 -1
- package/dist/entry-60.js +1 -1
- package/dist/entry-61.js +0 -1
- package/dist/entry-62.js +1 -0
- package/dist/entry-63.js +0 -1
- package/dist/entry-64.js +1 -0
- package/dist/entry-65.js +1 -1
- package/dist/entry-66.js +1 -1
- package/dist/entry-67.js +1 -1
- package/dist/entry-68.js +1 -1
- package/dist/entry-69.js +1 -1
- package/dist/entry-70.js +1 -1
- package/dist/entry-72.js +1 -1
- package/dist/entry-75.js +1 -1
- package/dist/entry-77.js +50 -307
- package/dist/entry-78.js +14 -26
- package/dist/entry-79.js +2 -2
- package/dist/entry-80.js +1 -1
- package/dist/entry-81.js +1 -1
- package/dist/entry-82.js +5 -3
- package/dist/entry-83.js +1 -1
- package/dist/entry-84.js +1 -1
- package/dist/entry-85.js +1 -1
- package/dist/entry-86.js +1 -1
- package/dist/pi-agent-ide.js +15 -3
- package/docs/agent-guides/editing.md +31 -3
- package/docs/agent-guides/git-changes.md +1 -1
- package/docs/agent-guides/json-reading.md +19 -3
- package/docs/agent-guides/read-resources.md +15 -1
- package/docs/agent-guides/search-code.md +43 -2
- package/docs/agent-guides/select-code.md +194 -0
- package/docs/agent-guides/terminal.md +1 -1
- package/docs/code-review.md +99 -0
- package/docs/configuration.md +36 -7
- package/docs/development.md +16 -19
- package/docs/nightly.md +7 -0
- package/docs/releases.md +42 -0
- package/docs/research/apply-native-capability-matrix.md +174 -0
- package/docs/research/apply-read-latency.md +2 -0
- package/docs/research/lpt-393-fuzzy-results/file.json +152 -0
- package/docs/research/lpt-393-fuzzy-results/repo.json +122 -0
- package/docs/research/lpt-393-search-fuzzy-fallback.md +97 -0
- package/docs/research/native-codemode-batch-decision.md +29 -0
- package/docs/research/native-codemode-batching.md +65 -0
- package/docs/structured-results.md +86 -0
- package/docs/tools.md +24 -4
- package/docs/user-hooks.md +1 -1
- package/node_modules/pi-agent-doctor/package.json +1 -1
- package/node_modules/pi-agent-documentation/package.json +1 -1
- package/node_modules/pi-agent-documentation/src/index.ts +1 -1
- package/node_modules/pi-agent-filesystem/index.ts +6 -1
- package/node_modules/pi-agent-filesystem/package.json +7 -6
- package/node_modules/pi-agent-filesystem/src/path-recovery.ts +109 -0
- package/node_modules/pi-agent-filesystem-image/package.json +3 -3
- package/node_modules/pi-agent-filesystem-jq/package.json +5 -5
- package/node_modules/pi-agent-filesystem-pdf/package.json +3 -3
- package/node_modules/pi-agent-filesystem-text/package.json +3 -3
- package/node_modules/pi-agent-ide-ast/package.json +15 -10
- package/node_modules/pi-agent-ide-ast/src/ast/constructs.ts +193 -0
- package/node_modules/pi-agent-ide-ast/src/ast/element-extent.ts +59 -0
- package/node_modules/pi-agent-ide-ast/src/ast/manager.ts +5 -1
- package/node_modules/pi-agent-ide-ast/src/ast/selection.ts +204 -0
- package/node_modules/pi-agent-ide-ast/src/ast/syntax-tree.ts +2 -0
- package/node_modules/pi-agent-ide-ast/src/extension.ts +4 -2
- package/node_modules/pi-agent-ide-ast/src/geometry-selection.ts +184 -0
- package/node_modules/pi-agent-ide-ast/src/overflow-handler.ts +6 -1
- package/node_modules/pi-agent-ide-ast/src/scope-handler.ts +6 -2
- package/node_modules/pi-agent-ide-ast/src/search-resolver.ts +110 -7
- package/node_modules/pi-agent-ide-ast/src/select-presentation.ts +104 -0
- package/node_modules/pi-agent-ide-ast/src/select-schema.ts +384 -0
- package/node_modules/pi-agent-ide-ast/src/selection-region.ts +41 -0
- package/node_modules/pi-agent-ide-ast/src/source-text.ts +92 -0
- package/node_modules/pi-agent-ide-ast/src/text-selection.ts +214 -0
- package/node_modules/pi-agent-ide-ast/src/tool-select.ts +251 -0
- package/node_modules/pi-agent-ide-changes/index.ts +2 -24
- package/node_modules/pi-agent-ide-changes/package.json +8 -7
- package/node_modules/pi-agent-ide-changes/src/current-change-presenter.ts +5 -0
- package/node_modules/pi-agent-ide-changes/src/tool-index-change.ts +126 -48
- package/node_modules/pi-agent-ide-changes/src/tool-text-undo.ts +11 -40
- package/node_modules/pi-agent-ide-formatter/package.json +3 -3
- package/node_modules/pi-agent-ide-languages/package.json +2 -2
- package/node_modules/pi-agent-ide-lint/package.json +7 -7
- package/node_modules/pi-agent-ide-lsp/index.ts +2 -2
- package/node_modules/pi-agent-ide-lsp/package.json +9 -9
- package/node_modules/pi-agent-ide-lsp/src/lsp/client.ts +5 -0
- package/node_modules/pi-agent-ide-lsp/src/lsp/manager.ts +8 -2
- package/node_modules/pi-agent-ide-lsp/src/lsp/project.ts +19 -0
- package/node_modules/pi-agent-ide-lsp/src/lsp/symbol-search.ts +204 -118
- package/node_modules/pi-agent-ide-lsp/src/rename-plan.ts +2 -17
- package/node_modules/pi-agent-ide-lsp/src/search-resolver.ts +92 -30
- package/node_modules/pi-agent-image/package.json +2 -2
- package/node_modules/pi-agent-invariant/package.json +1 -1
- package/node_modules/pi-agent-pdf/package.json +2 -2
- package/node_modules/pi-agent-read/package.json +7 -7
- package/node_modules/pi-agent-read/src/api/read-parameters.ts +2 -2
- package/node_modules/pi-agent-read/src/api/tools/read.ts +35 -2
- package/node_modules/pi-agent-read/src/core/extension.ts +11 -0
- package/node_modules/pi-agent-read/src/core/tools/read/raw-read.ts +9 -10
- package/node_modules/pi-agent-read/src/core/tools/read/read-result.ts +28 -15
- package/node_modules/pi-agent-read/src/core/tools/read/result-target.ts +93 -0
- package/node_modules/pi-agent-read/src/core/tools/read/structured-result.ts +218 -0
- package/node_modules/pi-agent-read/src/core/tools/tool-read.ts +127 -73
- package/node_modules/pi-agent-resource/package.json +1 -1
- package/node_modules/pi-agent-resource/src/index.ts +22 -0
- package/node_modules/pi-agent-resource/src/resource-access.ts +55 -0
- package/node_modules/pi-agent-resource/src/resource-scheduler.ts +204 -0
- package/node_modules/pi-agent-resource/src/result-targets.ts +233 -0
- package/node_modules/pi-agent-resource/src/tool-result.ts +125 -0
- package/node_modules/pi-agent-search/package.json +5 -4
- package/node_modules/pi-agent-search/src/api/fuzzy.ts +208 -0
- package/node_modules/pi-agent-search/src/api/presentation.ts +68 -0
- package/node_modules/pi-agent-search/src/api/search-parameters.ts +13 -6
- package/node_modules/pi-agent-search/src/api/search-scope.ts +22 -0
- package/node_modules/pi-agent-search/src/api/search.ts +39 -0
- package/node_modules/pi-agent-search/src/api/structured-result.ts +148 -0
- package/node_modules/pi-agent-search/src/core/extension.ts +90 -24
- package/node_modules/pi-agent-search/src/core/search-core.ts +145 -78
- package/node_modules/pi-agent-search-text/index.ts +6 -1
- package/node_modules/pi-agent-search-text/package.json +7 -6
- package/node_modules/pi-agent-search-text/src/fuzzy-search.ts +164 -0
- package/node_modules/pi-agent-search-text/src/resolvers.ts +174 -26
- package/node_modules/pi-agent-search-text/src/scoped-search.ts +103 -0
- package/node_modules/pi-agent-search-text/src/search-backend.ts +201 -18
- package/node_modules/pi-agent-search-text/src/search-query.ts +42 -34
- package/node_modules/pi-agent-search-text/src/search-recipe.ts +18 -4
- package/node_modules/pi-agent-search-text/src/search-renderer.ts +90 -259
- package/node_modules/pi-agent-search-text/src/search-result.ts +77 -0
- package/node_modules/pi-agent-search-text/src/search-session.ts +110 -17
- package/node_modules/pi-agent-text/package.json +2 -2
- package/node_modules/pi-agent-text/src/text-document.ts +15 -10
- package/node_modules/pi-agent-text-anchor-constant/package.json +3 -3
- package/node_modules/pi-agent-text-anchor-exact/package.json +3 -3
- package/node_modules/pi-agent-text-anchor-exact/src/anchor.ts +4 -3
- package/node_modules/pi-agent-text-anchor-line-hash/package.json +5 -5
- package/node_modules/pi-agent-text-editor/index.ts +4 -7
- package/node_modules/pi-agent-text-editor/package.json +20 -21
- package/node_modules/pi-agent-text-editor/src/api/mutation-preview.ts +2 -0
- package/node_modules/pi-agent-text-editor/src/api/mutation-result.ts +6 -0
- package/node_modules/pi-agent-text-editor/src/api/mutation-tool.ts +3 -1
- package/node_modules/pi-agent-text-editor/src/api/native-edit-batch-event.ts +12 -0
- package/node_modules/pi-agent-text-editor/src/api/plugin-protocol.ts +1 -20
- package/node_modules/pi-agent-text-editor/src/core/builtin-edit.ts +16 -0
- package/node_modules/pi-agent-text-editor/src/core/diff-tool.ts +78 -41
- package/node_modules/pi-agent-text-editor/src/core/editing-guidelines.ts +8 -0
- package/node_modules/pi-agent-text-editor/src/core/extension.ts +12 -2
- package/node_modules/pi-agent-text-editor/src/core/file-operation-tools.ts +35 -4
- package/node_modules/pi-agent-text-editor/src/core/file-operations.ts +1 -1
- package/node_modules/pi-agent-text-editor/src/core/file-result-targets.ts +76 -0
- package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +2 -0
- package/node_modules/pi-agent-text-editor/src/core/mutation-result-targets.ts +168 -0
- package/node_modules/pi-agent-text-editor/src/core/native-text-edit-batch.ts +820 -0
- package/node_modules/pi-agent-text-editor/src/core/post-edit-scope.ts +20 -6
- package/node_modules/pi-agent-text-editor/src/core/result-input.ts +22 -0
- package/node_modules/pi-agent-text-editor/src/core/result-target-anchors.ts +78 -0
- package/node_modules/pi-agent-text-editor/src/core/result-transfer.ts +175 -0
- package/node_modules/pi-agent-text-editor/src/core/structured-result.ts +412 -0
- package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +149 -139
- package/node_modules/pi-agent-text-editor/src/core/text-edit-batch.ts +2 -0
- package/node_modules/pi-agent-text-editor/src/core/text-editor-core.ts +236 -185
- package/node_modules/pi-agent-text-editor/src/core/text-mutation.ts +253 -203
- package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/coordinator.ts +25 -5
- package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/streaming-provider.ts +8 -4
- package/node_modules/pi-agent-text-editor/src/tools/text-tool-schema-properties.ts +9 -6
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-copy.ts +5 -5
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-delete.ts +5 -5
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-move.ts +9 -7
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-replace.ts +8 -8
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-write.ts +8 -3
- package/node_modules/pi-agent-text-editor-renderer/index.ts +9 -73
- package/node_modules/pi-agent-text-editor-renderer/package.json +6 -5
- package/node_modules/pi-agent-text-editor-renderer/src/diff-model.ts +276 -129
- package/node_modules/pi-agent-text-editor-renderer/src/diff-renderer.ts +36 -2
- package/node_modules/pi-agent-text-editor-renderer/src/mutation-panel.ts +28 -2
- package/node_modules/pi-agent-text-editor-renderer/src/mutation-projection.ts +40 -10
- package/node_modules/pi-agent-text-editor-renderer/src/renderer.ts +19 -45
- package/node_modules/pi-agent-text-editor-stale-anchor/index.ts +16 -1
- package/node_modules/pi-agent-text-editor-stale-anchor/package.json +4 -4
- package/node_modules/pi-agent-tool-call-interception/package.json +2 -2
- package/node_modules/pi-agent-tool-ui/package.json +1 -1
- package/node_modules/pi-agent-tool-ui/src/index.ts +7 -0
- package/node_modules/pi-agent-tool-ui/src/result-panel.ts +298 -0
- package/node_modules/pi-agent-web/index.ts +23 -0
- package/node_modules/pi-agent-web/package.json +7 -6
- package/node_modules/pi-agent-web/src/search.ts +221 -0
- package/node_modules/pi-agent-web-html/package.json +2 -2
- package/node_modules/pi-agent-web-image/package.json +3 -3
- package/node_modules/pi-agent-web-pdf/package.json +3 -3
- package/node_modules/pi-agent-web-text/package.json +3 -3
- package/package.json +42 -37
- package/skills/capture-code-review-rule/SKILL.md +60 -0
- package/src/code-review/config.ts +68 -0
- package/src/code-review/extension.ts +105 -0
- package/src/code-review/fragments.ts +26 -0
- package/src/code-review/reviewer.ts +198 -0
- package/src/composite/host-version.ts +13 -0
- package/src/composite/module-labels.ts +1 -1
- package/src/composite/nested-tool-rendering.ts +298 -0
- package/src/composite/preferences.ts +0 -5
- package/src/composite/tool-availability.ts +90 -0
- package/src/core/extension.ts +2 -0
- package/src/documentation/extension.ts +81 -145
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/index.ts +6 -1
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/package.json +7 -6
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-jq/package.json +5 -5
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/src/path-recovery.ts +109 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/index.ts +23 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/package.json +7 -6
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/package.json +2 -2
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/src/search.ts +221 -0
- package/src/extensions/pi-agent-read/package.json +7 -7
- package/src/extensions/pi-agent-read/src/api/read-parameters.ts +2 -2
- package/src/extensions/pi-agent-read/src/api/tools/read.ts +35 -2
- package/src/extensions/pi-agent-read/src/core/extension.ts +11 -0
- package/src/extensions/pi-agent-read/src/core/tools/read/raw-read.ts +9 -10
- package/src/extensions/pi-agent-read/src/core/tools/read/read-result.ts +28 -15
- package/src/extensions/pi-agent-read/src/core/tools/read/result-target.ts +93 -0
- package/src/extensions/pi-agent-read/src/core/tools/read/structured-result.ts +218 -0
- package/src/extensions/pi-agent-read/src/core/tools/tool-read.ts +127 -73
- package/src/extensions/pi-agent-search/package.json +5 -4
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/index.ts +6 -1
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/package.json +7 -6
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/fuzzy-search.ts +164 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/resolvers.ts +174 -26
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/scoped-search.ts +103 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-backend.ts +201 -18
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-query.ts +42 -34
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-recipe.ts +18 -4
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-renderer.ts +90 -259
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-result.ts +77 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-session.ts +110 -17
- package/src/extensions/pi-agent-search/src/api/fuzzy.ts +208 -0
- package/src/extensions/pi-agent-search/src/api/presentation.ts +68 -0
- package/src/extensions/pi-agent-search/src/api/search-parameters.ts +13 -6
- package/src/extensions/pi-agent-search/src/api/search-scope.ts +22 -0
- package/src/extensions/pi-agent-search/src/api/search.ts +39 -0
- package/src/extensions/pi-agent-search/src/api/structured-result.ts +148 -0
- package/src/extensions/pi-agent-search/src/core/extension.ts +90 -24
- package/src/extensions/pi-agent-search/src/core/search-core.ts +145 -78
- package/src/extensions/pi-agent-text-editor/index.ts +4 -7
- package/src/extensions/pi-agent-text-editor/package.json +20 -21
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/package.json +3 -3
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/package.json +3 -3
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/src/anchor.ts +4 -3
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/package.json +5 -5
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/index.ts +9 -73
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/package.json +6 -5
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-model.ts +276 -129
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-renderer.ts +36 -2
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-panel.ts +28 -2
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-projection.ts +40 -10
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/renderer.ts +19 -45
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/index.ts +16 -1
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/package.json +4 -4
- package/src/extensions/pi-agent-text-editor/src/api/mutation-preview.ts +2 -0
- package/src/extensions/pi-agent-text-editor/src/api/mutation-result.ts +6 -0
- package/src/extensions/pi-agent-text-editor/src/api/mutation-tool.ts +3 -1
- package/src/extensions/pi-agent-text-editor/src/api/native-edit-batch-event.ts +12 -0
- package/src/extensions/pi-agent-text-editor/src/api/plugin-protocol.ts +1 -20
- package/src/extensions/pi-agent-text-editor/src/core/builtin-edit.ts +16 -0
- package/src/extensions/pi-agent-text-editor/src/core/diff-tool.ts +78 -41
- package/src/extensions/pi-agent-text-editor/src/core/editing-guidelines.ts +8 -0
- package/src/extensions/pi-agent-text-editor/src/core/extension.ts +12 -2
- package/src/extensions/pi-agent-text-editor/src/core/file-operation-tools.ts +35 -4
- package/src/extensions/pi-agent-text-editor/src/core/file-operations.ts +1 -1
- package/src/extensions/pi-agent-text-editor/src/core/file-result-targets.ts +76 -0
- package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +2 -0
- package/src/extensions/pi-agent-text-editor/src/core/mutation-result-targets.ts +168 -0
- package/src/extensions/pi-agent-text-editor/src/core/native-text-edit-batch.ts +820 -0
- package/src/extensions/pi-agent-text-editor/src/core/post-edit-scope.ts +20 -6
- package/src/extensions/pi-agent-text-editor/src/core/result-input.ts +22 -0
- package/src/extensions/pi-agent-text-editor/src/core/result-target-anchors.ts +78 -0
- package/src/extensions/pi-agent-text-editor/src/core/result-transfer.ts +175 -0
- package/src/extensions/pi-agent-text-editor/src/core/structured-result.ts +412 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +149 -139
- package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch.ts +2 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-editor-core.ts +236 -185
- package/src/extensions/pi-agent-text-editor/src/core/text-mutation.ts +253 -203
- package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/coordinator.ts +25 -5
- package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/streaming-provider.ts +8 -4
- package/src/extensions/pi-agent-text-editor/src/tools/text-tool-schema-properties.ts +9 -6
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-copy.ts +5 -5
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-delete.ts +5 -5
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-move.ts +9 -7
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-replace.ts +8 -8
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-write.ts +8 -3
- package/src/pi-agent-ide.ts +61 -8
- package/src/plugins/pi-agent-ide-ast/package.json +15 -10
- package/src/plugins/pi-agent-ide-ast/src/ast/constructs.ts +193 -0
- package/src/plugins/pi-agent-ide-ast/src/ast/element-extent.ts +59 -0
- package/src/plugins/pi-agent-ide-ast/src/ast/manager.ts +5 -1
- package/src/plugins/pi-agent-ide-ast/src/ast/selection.ts +204 -0
- package/src/plugins/pi-agent-ide-ast/src/ast/syntax-tree.ts +2 -0
- package/src/plugins/pi-agent-ide-ast/src/extension.ts +4 -2
- package/src/plugins/pi-agent-ide-ast/src/geometry-selection.ts +184 -0
- package/src/plugins/pi-agent-ide-ast/src/overflow-handler.ts +6 -1
- package/src/plugins/pi-agent-ide-ast/src/scope-handler.ts +6 -2
- package/src/plugins/pi-agent-ide-ast/src/search-resolver.ts +110 -7
- package/src/plugins/pi-agent-ide-ast/src/select-presentation.ts +104 -0
- package/src/plugins/pi-agent-ide-ast/src/select-schema.ts +384 -0
- package/src/plugins/pi-agent-ide-ast/src/selection-region.ts +41 -0
- package/src/plugins/pi-agent-ide-ast/src/source-text.ts +92 -0
- package/src/plugins/pi-agent-ide-ast/src/text-selection.ts +214 -0
- package/src/plugins/pi-agent-ide-ast/src/tool-select.ts +251 -0
- package/src/plugins/pi-agent-ide-changes/index.ts +2 -24
- package/src/plugins/pi-agent-ide-changes/package.json +8 -7
- package/src/plugins/pi-agent-ide-changes/src/current-change-presenter.ts +5 -0
- package/src/plugins/pi-agent-ide-changes/src/tool-index-change.ts +126 -48
- package/src/plugins/pi-agent-ide-changes/src/tool-text-undo.ts +11 -40
- package/src/plugins/pi-agent-ide-debugger/index.ts +63 -17
- package/src/plugins/pi-agent-ide-debugger/src/catalog.ts +18 -2
- package/src/plugins/pi-agent-ide-debugger/src/dap-client.ts +24 -4
- package/src/plugins/pi-agent-ide-debugger/src/doctor-plugin.ts +68 -2
- package/src/plugins/pi-agent-ide-debugger/src/java-runtime.ts +357 -0
- package/src/plugins/pi-agent-ide-debugger/src/session-manager.ts +55 -3
- package/src/plugins/pi-agent-ide-diagnostics/src/extension.ts +13 -13
- package/src/plugins/pi-agent-ide-formatter/package.json +3 -3
- package/src/plugins/pi-agent-ide-languages/package.json +2 -2
- package/src/plugins/pi-agent-ide-lint/package.json +7 -7
- package/src/plugins/pi-agent-ide-lsp/index.ts +2 -2
- package/src/plugins/pi-agent-ide-lsp/package.json +9 -9
- package/src/plugins/pi-agent-ide-lsp/src/lsp/client.ts +5 -0
- package/src/plugins/pi-agent-ide-lsp/src/lsp/manager.ts +8 -2
- package/src/plugins/pi-agent-ide-lsp/src/lsp/project.ts +19 -0
- package/src/plugins/pi-agent-ide-lsp/src/lsp/symbol-search.ts +204 -118
- package/src/plugins/pi-agent-ide-lsp/src/rename-plan.ts +2 -17
- package/src/plugins/pi-agent-ide-lsp/src/search-resolver.ts +92 -30
- package/src/plugins/pi-agent-ide-terminal/index.ts +12 -2
- package/src/plugins/pi-agent-ide-terminal/src/output-limits.ts +1 -1
- package/src/plugins/pi-agent-ide-terminal/src/resources.ts +65 -30
- package/src/plugins/pi-agent-ide-terminal/src/search.ts +16 -37
- package/src/plugins/pi-agent-ide-terminal/src/session-manager.ts +1 -0
- package/src/plugins/pi-agent-ide-terminal/src/shell-result.ts +100 -0
- package/src/plugins/pi-agent-ide-terminal/src/steering.ts +62 -0
- package/src/plugins/pi-agent-ide-terminal/src/tools.ts +98 -46
- package/src/plugins/pi-agent-ide-terminal/src/types.ts +3 -1
- package/src/plugins/pi-agent-ide-terminal/src/ui.ts +15 -6
- package/src/plugins/pi-agent-ide-vision/index.ts +35 -1
- package/dist/chunk-3E2NTWCN.js +0 -2
- package/dist/chunk-4LUW26BX.js +0 -10
- package/dist/chunk-4U2GUKNJ.js +0 -0
- package/dist/chunk-5VSE5X5R.js +0 -2
- package/dist/chunk-6EYWFBNG.js +0 -0
- package/dist/chunk-7WCZOO5I.js +0 -1
- package/dist/chunk-CEX3VOJ7.js +0 -2
- package/dist/chunk-FA6BKC5Y.js +0 -1
- package/dist/chunk-FEDVTZJG.js +0 -1
- package/dist/chunk-HMLH43ZS.js +0 -4
- package/dist/chunk-ITSNK5YG.js +0 -7
- package/dist/chunk-KII2JQC5.js +0 -1
- package/dist/chunk-LVWMWWWF.js +0 -28
- package/dist/chunk-P7XO5CIP.js +0 -2
- package/dist/chunk-PZFOOHY2.js +0 -15
- package/dist/chunk-ROXXK6TW.js +0 -0
- package/dist/chunk-S3ZPXWX7.js +0 -2
- package/dist/chunk-T7CZIACQ.js +0 -27
- package/dist/chunk-VXD6FZP5.js +0 -5
- package/dist/chunk-YHUTTZC6.js +0 -2
- package/dist/chunk-YT42LKFV.js +0 -16
- package/docs/agent-guides/apply.md +0 -333
- package/node_modules/pi-agent-text-editor/src/api/apply-renderer.ts +0 -3
- package/node_modules/pi-agent-text-editor/src/core/apply/apply-undo-store.ts +0 -155
- package/node_modules/pi-agent-text-editor/src/core/apply/execution.ts +0 -344
- package/node_modules/pi-agent-text-editor/src/core/apply/final-mutations.ts +0 -50
- package/node_modules/pi-agent-text-editor/src/core/apply/format-source.ts +0 -11
- package/node_modules/pi-agent-text-editor/src/core/apply/mixed-source.ts +0 -203
- package/node_modules/pi-agent-text-editor/src/core/apply/mutation-outcome.ts +0 -55
- package/node_modules/pi-agent-text-editor/src/core/apply/output.ts +0 -276
- package/node_modules/pi-agent-text-editor/src/core/apply/prompt.ts +0 -13
- package/node_modules/pi-agent-text-editor/src/core/apply/queue.ts +0 -25
- package/node_modules/pi-agent-text-editor/src/core/apply/renderer.ts +0 -405
- package/node_modules/pi-agent-text-editor/src/core/apply/results.ts +0 -133
- package/node_modules/pi-agent-text-editor/src/core/apply/runtime.ts +0 -369
- package/node_modules/pi-agent-text-editor/src/core/apply/tool.ts +0 -148
- package/node_modules/pi-agent-text-editor/src/core/apply/transaction.ts +0 -920
- package/node_modules/pi-agent-text-editor/src/core/builtin-edit-filter.ts +0 -52
- package/src/extensions/pi-agent-text-editor/src/api/apply-renderer.ts +0 -3
- package/src/extensions/pi-agent-text-editor/src/core/apply/apply-undo-store.ts +0 -155
- package/src/extensions/pi-agent-text-editor/src/core/apply/execution.ts +0 -344
- package/src/extensions/pi-agent-text-editor/src/core/apply/final-mutations.ts +0 -50
- package/src/extensions/pi-agent-text-editor/src/core/apply/format-source.ts +0 -11
- package/src/extensions/pi-agent-text-editor/src/core/apply/mixed-source.ts +0 -203
- package/src/extensions/pi-agent-text-editor/src/core/apply/mutation-outcome.ts +0 -55
- package/src/extensions/pi-agent-text-editor/src/core/apply/output.ts +0 -276
- package/src/extensions/pi-agent-text-editor/src/core/apply/prompt.ts +0 -13
- package/src/extensions/pi-agent-text-editor/src/core/apply/queue.ts +0 -25
- package/src/extensions/pi-agent-text-editor/src/core/apply/renderer.ts +0 -405
- package/src/extensions/pi-agent-text-editor/src/core/apply/results.ts +0 -133
- package/src/extensions/pi-agent-text-editor/src/core/apply/runtime.ts +0 -369
- package/src/extensions/pi-agent-text-editor/src/core/apply/tool.ts +0 -148
- package/src/extensions/pi-agent-text-editor/src/core/apply/transaction.ts +0 -920
- package/src/extensions/pi-agent-text-editor/src/core/builtin-edit-filter.ts +0 -52
|
@@ -1,920 +0,0 @@
|
|
|
1
|
-
import { lstat, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import fs from "fs-extra";
|
|
4
|
-
import { requiredValue } from "pi-agent-invariant";
|
|
5
|
-
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
6
|
-
import { applyTextChanges, type TextChange } from "#src/core/text-change-engine.js";
|
|
7
|
-
import type { TextEditorCore } from "#src/core/text-editor-core.js";
|
|
8
|
-
|
|
9
|
-
import { forgetDeferredPostEdit } from "#src/core/post-edit-scope.js";
|
|
10
|
-
import type {
|
|
11
|
-
ScriptMutationFile,
|
|
12
|
-
ScriptMutationOutcome,
|
|
13
|
-
} from "#src/core/apply/mutation-outcome.js";
|
|
14
|
-
|
|
15
|
-
export interface EditorSnapshot {
|
|
16
|
-
readonly id: string;
|
|
17
|
-
readonly source: string;
|
|
18
|
-
readonly content: string;
|
|
19
|
-
}
|
|
20
|
-
interface Selection {
|
|
21
|
-
readonly document: string;
|
|
22
|
-
readonly from: number;
|
|
23
|
-
readonly to: number;
|
|
24
|
-
readonly text: string;
|
|
25
|
-
readonly linewise?: boolean;
|
|
26
|
-
}
|
|
27
|
-
interface TextOperation {
|
|
28
|
-
readonly kind: "replace";
|
|
29
|
-
readonly selection: Selection;
|
|
30
|
-
readonly text: string;
|
|
31
|
-
}
|
|
32
|
-
type TextTransferOperation = {
|
|
33
|
-
readonly sources: readonly Selection[];
|
|
34
|
-
readonly destinations: readonly Selection[];
|
|
35
|
-
readonly text: string;
|
|
36
|
-
} & ({ readonly kind: "text-copy" } | { readonly kind: "text-move" });
|
|
37
|
-
interface WarningOperation {
|
|
38
|
-
readonly kind: "warning";
|
|
39
|
-
readonly document: string;
|
|
40
|
-
readonly query?: string;
|
|
41
|
-
readonly warning?: unknown;
|
|
42
|
-
}
|
|
43
|
-
interface CreateOperation {
|
|
44
|
-
readonly kind: "create";
|
|
45
|
-
readonly path: string;
|
|
46
|
-
readonly content: string;
|
|
47
|
-
}
|
|
48
|
-
interface FileOperation {
|
|
49
|
-
readonly kind: "delete" | "copy" | "move";
|
|
50
|
-
readonly path: string;
|
|
51
|
-
readonly target?: string;
|
|
52
|
-
readonly overwrite?: boolean;
|
|
53
|
-
}
|
|
54
|
-
type Operation =
|
|
55
|
-
| TextOperation
|
|
56
|
-
| TextTransferOperation
|
|
57
|
-
| WarningOperation
|
|
58
|
-
| CreateOperation
|
|
59
|
-
| FileOperation;
|
|
60
|
-
export interface EditorTransactionRequest {
|
|
61
|
-
readonly snapshots: readonly EditorSnapshot[];
|
|
62
|
-
readonly operations: readonly Operation[];
|
|
63
|
-
}
|
|
64
|
-
/** Captured path state used to restore one failed operation. */
|
|
65
|
-
export interface SavedPath {
|
|
66
|
-
readonly path: string;
|
|
67
|
-
readonly existed: boolean;
|
|
68
|
-
readonly bytes?: Uint8Array;
|
|
69
|
-
}
|
|
70
|
-
export interface ApplyOperationOutcome {
|
|
71
|
-
readonly index: number;
|
|
72
|
-
readonly kind: Operation["kind"];
|
|
73
|
-
readonly status: "applied" | "warning" | "failed" | "unknown" | "blocked";
|
|
74
|
-
readonly effect: "applied" | "not-applied" | "unknown";
|
|
75
|
-
readonly resources: readonly string[];
|
|
76
|
-
readonly error?: { readonly code: string; readonly message: string };
|
|
77
|
-
readonly warning?: {
|
|
78
|
-
readonly code: string;
|
|
79
|
-
readonly message: string;
|
|
80
|
-
readonly presentation?: unknown;
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export type TransactionEditor = Pick<
|
|
85
|
-
TextEditorCore,
|
|
86
|
-
"editTexts" | "postProcessFile" | "recordApplyUndo"
|
|
87
|
-
>;
|
|
88
|
-
|
|
89
|
-
/** Optional transaction execution dependencies for deterministic failure handling. */
|
|
90
|
-
export interface TransactionExecutionOptions {
|
|
91
|
-
/** Restore one captured path after an operation fails. */
|
|
92
|
-
readonly restorePath?: (state: SavedPath) => Promise<void>;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/** Commit staged operations independently and return every outcome in staging order. */
|
|
96
|
-
export async function executeEditorTransaction(
|
|
97
|
-
editor: TransactionEditor,
|
|
98
|
-
input: unknown,
|
|
99
|
-
signal: AbortSignal,
|
|
100
|
-
context: Pick<ExtensionContext, "cwd">,
|
|
101
|
-
options: TransactionExecutionOptions = {},
|
|
102
|
-
): Promise<ScriptMutationOutcome> {
|
|
103
|
-
const request = parseRequest(input);
|
|
104
|
-
if (request.operations.length === 0) throw invalid("flush() requires at least one staged change");
|
|
105
|
-
const snapshots = new Map(request.snapshots.map((snapshot) => [snapshot.id, snapshot]));
|
|
106
|
-
if (snapshots.size !== request.snapshots.length) throw invalid("Duplicate editor snapshot id");
|
|
107
|
-
|
|
108
|
-
const accepted = new Map<string, TextChange[]>();
|
|
109
|
-
const unknown = new Set<string>();
|
|
110
|
-
const journal = new Map<string, SavedPath>();
|
|
111
|
-
const files: ScriptMutationFile[] = [];
|
|
112
|
-
const operations: ApplyOperationOutcome[] = [];
|
|
113
|
-
const cwd = context.cwd;
|
|
114
|
-
const batchedReplacements = new Set<number>();
|
|
115
|
-
|
|
116
|
-
for (const [index, operation] of request.operations.entries()) {
|
|
117
|
-
const resources = operationResources(operation, snapshots, cwd);
|
|
118
|
-
if (batchedReplacements.has(index)) continue;
|
|
119
|
-
if (operation.kind === "replace") {
|
|
120
|
-
const replacements: Array<{ index: number; operation: TextOperation }> = [];
|
|
121
|
-
for (
|
|
122
|
-
let candidateIndex = index;
|
|
123
|
-
candidateIndex < request.operations.length;
|
|
124
|
-
candidateIndex += 1
|
|
125
|
-
) {
|
|
126
|
-
const candidate = request.operations[candidateIndex];
|
|
127
|
-
if (
|
|
128
|
-
candidate?.kind !== "replace" ||
|
|
129
|
-
candidate.selection.document !== operation.selection.document
|
|
130
|
-
)
|
|
131
|
-
break;
|
|
132
|
-
replacements.push({ index: candidateIndex, operation: candidate });
|
|
133
|
-
}
|
|
134
|
-
if (replacements.length > 1) {
|
|
135
|
-
for (const replacement of replacements) batchedReplacements.add(replacement.index);
|
|
136
|
-
const outcome = await executeReplacementBatch(
|
|
137
|
-
replacements,
|
|
138
|
-
snapshots,
|
|
139
|
-
accepted,
|
|
140
|
-
editor,
|
|
141
|
-
cwd,
|
|
142
|
-
signal,
|
|
143
|
-
options,
|
|
144
|
-
);
|
|
145
|
-
for (const state of outcome.saved)
|
|
146
|
-
if (!journal.has(state.path)) journal.set(state.path, state);
|
|
147
|
-
files.push(...outcome.files);
|
|
148
|
-
operations.push(...outcome.operations);
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
if (operation.kind === "warning") {
|
|
153
|
-
operations.push({
|
|
154
|
-
index,
|
|
155
|
-
kind: "warning",
|
|
156
|
-
status: "warning",
|
|
157
|
-
effect: "not-applied",
|
|
158
|
-
resources,
|
|
159
|
-
warning: warningDetails(operation),
|
|
160
|
-
});
|
|
161
|
-
continue;
|
|
162
|
-
}
|
|
163
|
-
if (resources.some((resource) => unknown.has(resource))) {
|
|
164
|
-
operations.push({
|
|
165
|
-
index,
|
|
166
|
-
kind: operation.kind,
|
|
167
|
-
status: "blocked",
|
|
168
|
-
effect: "unknown",
|
|
169
|
-
resources,
|
|
170
|
-
error: {
|
|
171
|
-
code: "DEPENDENCY_UNKNOWN",
|
|
172
|
-
message: "A prior operation left a required resource in an unknown state.",
|
|
173
|
-
},
|
|
174
|
-
});
|
|
175
|
-
continue;
|
|
176
|
-
}
|
|
177
|
-
if (operation.kind === "replace") {
|
|
178
|
-
const snapshot = snapshots.get(operation.selection.document);
|
|
179
|
-
const prior = snapshot === undefined ? [] : (accepted.get(snapshot.source) ?? []);
|
|
180
|
-
if (prior.some((change) => overlaps(change, operation.selection))) {
|
|
181
|
-
operations.push(
|
|
182
|
-
failed(
|
|
183
|
-
index,
|
|
184
|
-
operation,
|
|
185
|
-
resources,
|
|
186
|
-
invalid(`Overlapping selection in ${snapshot?.source ?? "unknown snapshot"}`),
|
|
187
|
-
),
|
|
188
|
-
);
|
|
189
|
-
continue;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
let saved: readonly SavedPath[] = [];
|
|
194
|
-
try {
|
|
195
|
-
await validateOperation(operation, snapshots, accepted, cwd);
|
|
196
|
-
saved = await Promise.all(resources.map(savePath));
|
|
197
|
-
const produced = await executeOperation(operation, snapshots, accepted, editor, cwd, signal);
|
|
198
|
-
for (const state of saved) if (!journal.has(state.path)) journal.set(state.path, state);
|
|
199
|
-
files.push(...produced);
|
|
200
|
-
operations.push({
|
|
201
|
-
index,
|
|
202
|
-
kind: operation.kind,
|
|
203
|
-
status: "applied",
|
|
204
|
-
effect: "applied",
|
|
205
|
-
resources,
|
|
206
|
-
});
|
|
207
|
-
} catch (error) {
|
|
208
|
-
const rollbackErrors = await rollback(saved, options.restorePath);
|
|
209
|
-
if (rollbackErrors.length > 0) {
|
|
210
|
-
for (const resource of resources) unknown.add(resource);
|
|
211
|
-
operations.push({
|
|
212
|
-
index,
|
|
213
|
-
kind: operation.kind,
|
|
214
|
-
status: "unknown",
|
|
215
|
-
effect: "unknown",
|
|
216
|
-
resources,
|
|
217
|
-
error: codedError(error, rollbackErrors),
|
|
218
|
-
});
|
|
219
|
-
} else operations.push(failed(index, operation, resources, error));
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
const successful = operations.some(({ effect }) => effect === "applied");
|
|
224
|
-
const transaction = successful ? await editor.recordApplyUndo([...journal.values()]) : undefined;
|
|
225
|
-
return {
|
|
226
|
-
operation: "apply",
|
|
227
|
-
ok: operations.length > 0 && operations.every(({ status }) => status === "applied"),
|
|
228
|
-
effect: unknown.size > 0 ? "unknown" : successful ? "applied" : "not-applied",
|
|
229
|
-
...(transaction === undefined ? {} : { transaction }),
|
|
230
|
-
files,
|
|
231
|
-
completed: [
|
|
232
|
-
...new Set(
|
|
233
|
-
operations
|
|
234
|
-
.filter(({ effect }) => effect === "applied")
|
|
235
|
-
.flatMap(({ resources }) => resources),
|
|
236
|
-
),
|
|
237
|
-
],
|
|
238
|
-
errors: operations.flatMap((item) =>
|
|
239
|
-
item.status === "warning" || item.error === undefined
|
|
240
|
-
? []
|
|
241
|
-
: [{ source: item.resources.join(", "), ...item.error }],
|
|
242
|
-
),
|
|
243
|
-
operations,
|
|
244
|
-
};
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
async function executeReplacementBatch(
|
|
248
|
-
replacements: readonly { readonly index: number; readonly operation: TextOperation }[],
|
|
249
|
-
snapshots: ReadonlyMap<string, EditorSnapshot>,
|
|
250
|
-
accepted: Map<string, TextChange[]>,
|
|
251
|
-
editor: TransactionEditor,
|
|
252
|
-
cwd: string,
|
|
253
|
-
signal: AbortSignal,
|
|
254
|
-
options: TransactionExecutionOptions,
|
|
255
|
-
): Promise<{
|
|
256
|
-
readonly saved: readonly SavedPath[];
|
|
257
|
-
readonly files: readonly ScriptMutationFile[];
|
|
258
|
-
readonly operations: readonly ApplyOperationOutcome[];
|
|
259
|
-
}> {
|
|
260
|
-
const first = requiredValue(replacements[0]);
|
|
261
|
-
const snapshot = snapshots.get(first.operation.selection.document);
|
|
262
|
-
const resources = operationResources(first.operation, snapshots, cwd);
|
|
263
|
-
if (snapshot === undefined)
|
|
264
|
-
return {
|
|
265
|
-
saved: [],
|
|
266
|
-
files: [],
|
|
267
|
-
operations: replacements.map(({ index, operation }) =>
|
|
268
|
-
failed(
|
|
269
|
-
index,
|
|
270
|
-
operation,
|
|
271
|
-
resources,
|
|
272
|
-
invalid("Selection belongs to an unknown editor snapshot"),
|
|
273
|
-
),
|
|
274
|
-
),
|
|
275
|
-
};
|
|
276
|
-
|
|
277
|
-
const prior = accepted.get(snapshot.source) ?? [];
|
|
278
|
-
const additions: TextChange[] = [];
|
|
279
|
-
const operationResults = new Map<number, ApplyOperationOutcome>();
|
|
280
|
-
for (const { index, operation } of replacements) {
|
|
281
|
-
try {
|
|
282
|
-
validateSelection(operation, snapshot);
|
|
283
|
-
if (
|
|
284
|
-
prior.some((change) => overlaps(change, operation.selection)) ||
|
|
285
|
-
additions.some((change) => overlaps(change, operation.selection))
|
|
286
|
-
)
|
|
287
|
-
throw invalid(`Overlapping selection in ${snapshot.source}`);
|
|
288
|
-
additions.push({
|
|
289
|
-
from: operation.selection.from,
|
|
290
|
-
to: operation.selection.to,
|
|
291
|
-
insert: preserveLineEnding(operation.selection, operation.text),
|
|
292
|
-
});
|
|
293
|
-
} catch (error) {
|
|
294
|
-
operationResults.set(index, failed(index, operation, resources, error));
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
if (additions.length === 0)
|
|
298
|
-
return {
|
|
299
|
-
saved: [],
|
|
300
|
-
files: [],
|
|
301
|
-
operations: replacements.map(({ index }) => requiredValue(operationResults.get(index))),
|
|
302
|
-
};
|
|
303
|
-
|
|
304
|
-
const before = applyTextChanges(snapshot.content, prior).content;
|
|
305
|
-
let current: string;
|
|
306
|
-
try {
|
|
307
|
-
current = await readFile(snapshot.source, "utf8");
|
|
308
|
-
if (current !== before) throw stale(snapshot.source);
|
|
309
|
-
} catch (error) {
|
|
310
|
-
for (const { index, operation } of replacements)
|
|
311
|
-
if (!operationResults.has(index))
|
|
312
|
-
operationResults.set(index, failed(index, operation, resources, error));
|
|
313
|
-
return {
|
|
314
|
-
saved: [],
|
|
315
|
-
files: [],
|
|
316
|
-
operations: replacements.map(({ index }) => requiredValue(operationResults.get(index))),
|
|
317
|
-
};
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
const saved = [await savePath(snapshot.source)];
|
|
321
|
-
const combined = [...prior, ...additions];
|
|
322
|
-
const applied = applyTextChanges(snapshot.content, combined);
|
|
323
|
-
try {
|
|
324
|
-
await editOne(editor, snapshot.source, before, applied.content, cwd, signal, false);
|
|
325
|
-
accepted.set(snapshot.source, combined);
|
|
326
|
-
for (const { index, operation } of replacements)
|
|
327
|
-
if (!operationResults.has(index))
|
|
328
|
-
operationResults.set(index, {
|
|
329
|
-
index,
|
|
330
|
-
kind: operation.kind,
|
|
331
|
-
status: "applied",
|
|
332
|
-
effect: "applied",
|
|
333
|
-
resources,
|
|
334
|
-
});
|
|
335
|
-
return {
|
|
336
|
-
saved,
|
|
337
|
-
files: [
|
|
338
|
-
{
|
|
339
|
-
source: snapshot.source,
|
|
340
|
-
before,
|
|
341
|
-
after: applied.content,
|
|
342
|
-
action: "edited",
|
|
343
|
-
changes: applied.changes.map((change, editIndex) => ({
|
|
344
|
-
editIndex,
|
|
345
|
-
fromA: change.fromBefore,
|
|
346
|
-
toA: change.toBefore,
|
|
347
|
-
fromB: change.fromAfter,
|
|
348
|
-
toB: change.toAfter,
|
|
349
|
-
removedText: change.removedText,
|
|
350
|
-
insertedText: change.insertedText,
|
|
351
|
-
})),
|
|
352
|
-
formatting: { status: "not-reported" },
|
|
353
|
-
},
|
|
354
|
-
],
|
|
355
|
-
operations: replacements.map(({ index }) => requiredValue(operationResults.get(index))),
|
|
356
|
-
};
|
|
357
|
-
} catch (error) {
|
|
358
|
-
const rollbackErrors = await rollback(saved, options.restorePath);
|
|
359
|
-
for (const { index, operation } of replacements)
|
|
360
|
-
if (!operationResults.has(index))
|
|
361
|
-
operationResults.set(
|
|
362
|
-
index,
|
|
363
|
-
rollbackErrors.length === 0
|
|
364
|
-
? failed(index, operation, resources, error)
|
|
365
|
-
: {
|
|
366
|
-
index,
|
|
367
|
-
kind: operation.kind,
|
|
368
|
-
status: "unknown",
|
|
369
|
-
effect: "unknown",
|
|
370
|
-
resources,
|
|
371
|
-
error: codedError(error, rollbackErrors),
|
|
372
|
-
},
|
|
373
|
-
);
|
|
374
|
-
return {
|
|
375
|
-
saved: rollbackErrors.length === 0 ? [] : saved,
|
|
376
|
-
files: [],
|
|
377
|
-
operations: replacements.map(({ index }) => requiredValue(operationResults.get(index))),
|
|
378
|
-
};
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
async function validateOperation(
|
|
383
|
-
operation: Operation,
|
|
384
|
-
snapshots: ReadonlyMap<string, EditorSnapshot>,
|
|
385
|
-
accepted: ReadonlyMap<string, readonly TextChange[]>,
|
|
386
|
-
cwd: string,
|
|
387
|
-
): Promise<void> {
|
|
388
|
-
if (operation.kind === "warning") return;
|
|
389
|
-
if (operation.kind === "text-copy" || operation.kind === "text-move") {
|
|
390
|
-
const selected =
|
|
391
|
-
operation.kind === "text-move"
|
|
392
|
-
? [...operation.destinations, ...operation.sources]
|
|
393
|
-
: [...operation.destinations];
|
|
394
|
-
for (const selection of [...operation.sources, ...operation.destinations])
|
|
395
|
-
validateSelection(
|
|
396
|
-
{ kind: "replace", selection, text: "" },
|
|
397
|
-
snapshots.get(selection.document),
|
|
398
|
-
);
|
|
399
|
-
for (const [index, selection] of selected.entries()) {
|
|
400
|
-
const snapshot = requiredValue(snapshots.get(selection.document));
|
|
401
|
-
const prior = accepted.get(snapshot.source) ?? [];
|
|
402
|
-
if (
|
|
403
|
-
prior.some((change) => overlaps(change, selection)) ||
|
|
404
|
-
selected
|
|
405
|
-
.slice(0, index)
|
|
406
|
-
.some((other) => other.document === selection.document && rangesOverlap(other, selection))
|
|
407
|
-
)
|
|
408
|
-
throw invalid(`Overlapping selection in ${snapshot.source}`);
|
|
409
|
-
}
|
|
410
|
-
for (const document of new Set(
|
|
411
|
-
[...operation.sources, ...operation.destinations].map(({ document }) => document),
|
|
412
|
-
)) {
|
|
413
|
-
const snapshot = requiredValue(snapshots.get(document));
|
|
414
|
-
const expected = applyTextChanges(
|
|
415
|
-
snapshot.content,
|
|
416
|
-
accepted.get(snapshot.source) ?? [],
|
|
417
|
-
).content;
|
|
418
|
-
let current;
|
|
419
|
-
try {
|
|
420
|
-
current = await readFile(snapshot.source, "utf8");
|
|
421
|
-
} catch {
|
|
422
|
-
throw stale(snapshot.source);
|
|
423
|
-
}
|
|
424
|
-
if (current !== expected) throw stale(snapshot.source);
|
|
425
|
-
}
|
|
426
|
-
return;
|
|
427
|
-
}
|
|
428
|
-
if (operation.kind === "replace") {
|
|
429
|
-
const snapshot = snapshots.get(operation.selection.document);
|
|
430
|
-
validateSelection(operation, snapshot);
|
|
431
|
-
const expected = applyTextChanges(
|
|
432
|
-
snapshot.content,
|
|
433
|
-
accepted.get(snapshot.source) ?? [],
|
|
434
|
-
).content;
|
|
435
|
-
let current: string;
|
|
436
|
-
try {
|
|
437
|
-
current = await readFile(snapshot.source, "utf8");
|
|
438
|
-
} catch {
|
|
439
|
-
throw stale(snapshot.source);
|
|
440
|
-
}
|
|
441
|
-
if (current !== expected) throw stale(snapshot.source);
|
|
442
|
-
return;
|
|
443
|
-
}
|
|
444
|
-
const source = path.resolve(cwd, operation.path);
|
|
445
|
-
const sourceStat = await optionalStat(source);
|
|
446
|
-
if (operation.kind === "create") {
|
|
447
|
-
if (sourceStat !== undefined) throw invalid(`Create target exists: ${operation.path}`);
|
|
448
|
-
return;
|
|
449
|
-
}
|
|
450
|
-
if (sourceStat === undefined) throw invalid(`Source does not exist: ${operation.path}`);
|
|
451
|
-
if (!sourceStat.isFile() || sourceStat.isSymbolicLink())
|
|
452
|
-
throw invalid(`${source} is not a regular file`);
|
|
453
|
-
if (operation.kind === "delete") return;
|
|
454
|
-
const target = path.resolve(cwd, operation.target ?? "");
|
|
455
|
-
if (source === target) throw invalid("Source and target are the same file");
|
|
456
|
-
const targetStat = await optionalStat(target);
|
|
457
|
-
if (targetStat !== undefined && (!targetStat.isFile() || targetStat.isSymbolicLink()))
|
|
458
|
-
throw invalid(`${target} is not a regular file`);
|
|
459
|
-
if (targetStat !== undefined && operation.overwrite !== true)
|
|
460
|
-
throw invalid(`Target exists: ${operation.target}`);
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
async function executeOperation(
|
|
464
|
-
operation: Operation,
|
|
465
|
-
snapshots: ReadonlyMap<string, EditorSnapshot>,
|
|
466
|
-
accepted: Map<string, TextChange[]>,
|
|
467
|
-
editor: TransactionEditor,
|
|
468
|
-
cwd: string,
|
|
469
|
-
signal: AbortSignal,
|
|
470
|
-
): Promise<ScriptMutationFile[]> {
|
|
471
|
-
if (operation.kind === "warning") return [];
|
|
472
|
-
if (operation.kind === "text-copy" || operation.kind === "text-move")
|
|
473
|
-
return executeTextTransfer(operation, snapshots, accepted, editor, cwd, signal);
|
|
474
|
-
if (operation.kind === "replace") {
|
|
475
|
-
const snapshot = snapshots.get(operation.selection.document);
|
|
476
|
-
if (snapshot === undefined) throw invalid("Selection belongs to an unknown editor snapshot");
|
|
477
|
-
const prior = accepted.get(snapshot.source) ?? [];
|
|
478
|
-
const next = [
|
|
479
|
-
...prior,
|
|
480
|
-
{
|
|
481
|
-
from: operation.selection.from,
|
|
482
|
-
to: operation.selection.to,
|
|
483
|
-
insert: preserveLineEnding(operation.selection, operation.text),
|
|
484
|
-
},
|
|
485
|
-
];
|
|
486
|
-
const before = applyTextChanges(snapshot.content, prior).content;
|
|
487
|
-
const applied = applyTextChanges(snapshot.content, next);
|
|
488
|
-
await editOne(editor, snapshot.source, before, applied.content, cwd, signal, false);
|
|
489
|
-
accepted.set(snapshot.source, next);
|
|
490
|
-
return [
|
|
491
|
-
{
|
|
492
|
-
source: snapshot.source,
|
|
493
|
-
before,
|
|
494
|
-
after: applied.content,
|
|
495
|
-
action: "edited",
|
|
496
|
-
changes: applied.changes.map((change, editIndex) => ({
|
|
497
|
-
editIndex,
|
|
498
|
-
fromA: change.fromBefore,
|
|
499
|
-
toA: change.toBefore,
|
|
500
|
-
fromB: change.fromAfter,
|
|
501
|
-
toB: change.toAfter,
|
|
502
|
-
removedText: change.removedText,
|
|
503
|
-
insertedText: change.insertedText,
|
|
504
|
-
})),
|
|
505
|
-
formatting: { status: "not-reported" },
|
|
506
|
-
},
|
|
507
|
-
];
|
|
508
|
-
}
|
|
509
|
-
if (operation.kind === "create") {
|
|
510
|
-
const source = path.resolve(cwd, operation.path);
|
|
511
|
-
await editOne(editor, source, "", operation.content, cwd, signal, true);
|
|
512
|
-
return [
|
|
513
|
-
{
|
|
514
|
-
source,
|
|
515
|
-
before: null,
|
|
516
|
-
after: operation.content,
|
|
517
|
-
action: "created",
|
|
518
|
-
changes: [],
|
|
519
|
-
formatting: { status: "not-reported" },
|
|
520
|
-
},
|
|
521
|
-
];
|
|
522
|
-
}
|
|
523
|
-
await performFileOperation(operation, cwd);
|
|
524
|
-
if (operation.kind === "delete" || operation.kind === "move") {
|
|
525
|
-
forgetDeferredPostEdit(path.resolve(cwd, operation.path));
|
|
526
|
-
}
|
|
527
|
-
if (operation.target !== undefined) {
|
|
528
|
-
const target = path.resolve(cwd, operation.target);
|
|
529
|
-
if ((await optionalStat(target)) !== undefined)
|
|
530
|
-
await editor.postProcessFile(target, { cwd, signal });
|
|
531
|
-
}
|
|
532
|
-
return [];
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
function preserveLineEnding(selection: Selection, insert: string): string {
|
|
536
|
-
if (insert.length === 0 || selection.linewise !== true || /(?:\r\n|\r|\n)$/u.test(insert))
|
|
537
|
-
return insert;
|
|
538
|
-
return insert + (/(?:\r\n|\r|\n)$/u.exec(selection.text)?.[0] ?? "");
|
|
539
|
-
}
|
|
540
|
-
function rangesOverlap(left: Selection, right: Selection): boolean {
|
|
541
|
-
return left.from < right.to && right.from < left.to;
|
|
542
|
-
}
|
|
543
|
-
function transferInsert(snapshot: EditorSnapshot, destination: Selection, text: string): string {
|
|
544
|
-
if (destination.from !== destination.to || destination.linewise !== true || text.length === 0)
|
|
545
|
-
return text;
|
|
546
|
-
const separator = snapshot.content.includes("\r\n") ? "\r\n" : "\n";
|
|
547
|
-
const before = snapshot.content.slice(0, destination.from);
|
|
548
|
-
const after = snapshot.content.slice(destination.to);
|
|
549
|
-
const prefix = before.length > 0 && !/(?:\r\n|\r|\n)$/u.test(before) ? separator : "";
|
|
550
|
-
const suffix = after.length > 0 && !/(?:\r\n|\r|\n)$/u.test(text) ? separator : "";
|
|
551
|
-
return prefix + text + suffix;
|
|
552
|
-
}
|
|
553
|
-
async function executeTextTransfer(
|
|
554
|
-
operation: TextTransferOperation,
|
|
555
|
-
snapshots: ReadonlyMap<string, EditorSnapshot>,
|
|
556
|
-
accepted: Map<string, TextChange[]>,
|
|
557
|
-
editor: TransactionEditor,
|
|
558
|
-
cwd: string,
|
|
559
|
-
signal: AbortSignal,
|
|
560
|
-
): Promise<ScriptMutationFile[]> {
|
|
561
|
-
const additions = new Map<string, TextChange[]>();
|
|
562
|
-
for (const destination of operation.destinations) {
|
|
563
|
-
const snapshot = requiredValue(snapshots.get(destination.document));
|
|
564
|
-
const changes = additions.get(snapshot.source) ?? [];
|
|
565
|
-
changes.push({
|
|
566
|
-
from: destination.from,
|
|
567
|
-
to: destination.to,
|
|
568
|
-
insert: transferInsert(snapshot, destination, operation.text),
|
|
569
|
-
});
|
|
570
|
-
additions.set(snapshot.source, changes);
|
|
571
|
-
}
|
|
572
|
-
if (operation.kind === "text-move")
|
|
573
|
-
for (const source of operation.sources) {
|
|
574
|
-
const snapshot = requiredValue(snapshots.get(source.document));
|
|
575
|
-
const changes = additions.get(snapshot.source) ?? [];
|
|
576
|
-
changes.push({ from: source.from, to: source.to, insert: "" });
|
|
577
|
-
additions.set(snapshot.source, changes);
|
|
578
|
-
}
|
|
579
|
-
const sourcePaths = [...additions.keys()];
|
|
580
|
-
const before = new Map(
|
|
581
|
-
sourcePaths.map((source) => {
|
|
582
|
-
const snapshot = requiredValue(
|
|
583
|
-
[...snapshots.values()].find((item) => item.source === source),
|
|
584
|
-
);
|
|
585
|
-
return [
|
|
586
|
-
source,
|
|
587
|
-
applyTextChanges(snapshot.content, accepted.get(source) ?? []).content,
|
|
588
|
-
] as const;
|
|
589
|
-
}),
|
|
590
|
-
);
|
|
591
|
-
const after = new Map<string, string>();
|
|
592
|
-
const outcome = await editor.editTexts(
|
|
593
|
-
sourcePaths.map((source) => ({ source, read: true })),
|
|
594
|
-
{ cwd, signal, intent: "edit" },
|
|
595
|
-
(texts) => {
|
|
596
|
-
const changes = new Map<string, TextChange[]>();
|
|
597
|
-
for (const source of sourcePaths) {
|
|
598
|
-
if (texts.get(source) !== before.get(source)) throw stale(source);
|
|
599
|
-
const combined = [...(accepted.get(source) ?? []), ...(additions.get(source) ?? [])];
|
|
600
|
-
const snapshot = requiredValue(
|
|
601
|
-
[...snapshots.values()].find((item) => item.source === source),
|
|
602
|
-
);
|
|
603
|
-
after.set(source, applyTextChanges(snapshot.content, combined).content);
|
|
604
|
-
changes.set(source, additions.get(source) ?? []);
|
|
605
|
-
}
|
|
606
|
-
return { changes, result: undefined };
|
|
607
|
-
},
|
|
608
|
-
);
|
|
609
|
-
if (outcome.kind === "failed")
|
|
610
|
-
throw outcome.failure.cause instanceof Error
|
|
611
|
-
? outcome.failure.cause
|
|
612
|
-
: new Error(outcome.failure.message);
|
|
613
|
-
const files: ScriptMutationFile[] = [];
|
|
614
|
-
for (const source of sourcePaths) {
|
|
615
|
-
const snapshot = requiredValue([...snapshots.values()].find((item) => item.source === source));
|
|
616
|
-
const combined = [...(accepted.get(source) ?? []), ...(additions.get(source) ?? [])];
|
|
617
|
-
accepted.set(source, combined);
|
|
618
|
-
const applied = applyTextChanges(snapshot.content, combined);
|
|
619
|
-
files.push({
|
|
620
|
-
source,
|
|
621
|
-
before: requiredValue(before.get(source)),
|
|
622
|
-
after: requiredValue(after.get(source)),
|
|
623
|
-
action: "edited",
|
|
624
|
-
changes: applied.changes.map((change, editIndex) => ({
|
|
625
|
-
editIndex,
|
|
626
|
-
fromA: change.fromBefore,
|
|
627
|
-
toA: change.toBefore,
|
|
628
|
-
fromB: change.fromAfter,
|
|
629
|
-
toB: change.toAfter,
|
|
630
|
-
removedText: change.removedText,
|
|
631
|
-
insertedText: change.insertedText,
|
|
632
|
-
})),
|
|
633
|
-
formatting: { status: "not-reported" },
|
|
634
|
-
});
|
|
635
|
-
}
|
|
636
|
-
return files;
|
|
637
|
-
}
|
|
638
|
-
async function editOne(
|
|
639
|
-
editor: TransactionEditor,
|
|
640
|
-
source: string,
|
|
641
|
-
before: string,
|
|
642
|
-
after: string,
|
|
643
|
-
cwd: string,
|
|
644
|
-
signal: AbortSignal,
|
|
645
|
-
create: boolean,
|
|
646
|
-
): Promise<void> {
|
|
647
|
-
const outcome = await editor.editTexts(
|
|
648
|
-
[{ source, read: true, ...(create && { allowReadFailure: true }) }],
|
|
649
|
-
{ cwd, signal, intent: "edit" },
|
|
650
|
-
(texts) => {
|
|
651
|
-
if ((texts.get(source) ?? "") !== before) throw stale(source);
|
|
652
|
-
return {
|
|
653
|
-
changes: new Map([[source, [{ from: 0, to: before.length, insert: after }]]]),
|
|
654
|
-
result: undefined,
|
|
655
|
-
};
|
|
656
|
-
},
|
|
657
|
-
);
|
|
658
|
-
if (outcome.kind === "failed")
|
|
659
|
-
throw outcome.failure.cause instanceof Error
|
|
660
|
-
? outcome.failure.cause
|
|
661
|
-
: new Error(outcome.failure.message);
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
function validateSelection(
|
|
665
|
-
operation: TextOperation,
|
|
666
|
-
snapshot: EditorSnapshot | undefined,
|
|
667
|
-
): asserts snapshot is EditorSnapshot {
|
|
668
|
-
if (snapshot === undefined) throw invalid("Selection belongs to an unknown editor snapshot");
|
|
669
|
-
const { from, to, text } = operation.selection;
|
|
670
|
-
if (
|
|
671
|
-
!Number.isInteger(from) ||
|
|
672
|
-
!Number.isInteger(to) ||
|
|
673
|
-
from < 0 ||
|
|
674
|
-
to < from ||
|
|
675
|
-
to > snapshot.content.length
|
|
676
|
-
)
|
|
677
|
-
throw invalid(`Selection ${from}-${to} is outside ${snapshot.source}`);
|
|
678
|
-
if (snapshot.content.slice(from, to) !== text)
|
|
679
|
-
throw invalid(`Selection text does not match ${snapshot.source}`);
|
|
680
|
-
}
|
|
681
|
-
function overlaps(change: TextChange, selection: Selection): boolean {
|
|
682
|
-
return selection.from < change.to && change.from < selection.to;
|
|
683
|
-
}
|
|
684
|
-
function operationResources(
|
|
685
|
-
operation: Operation,
|
|
686
|
-
snapshots: ReadonlyMap<string, EditorSnapshot>,
|
|
687
|
-
cwd: string,
|
|
688
|
-
): string[] {
|
|
689
|
-
if (operation.kind === "text-copy" || operation.kind === "text-move")
|
|
690
|
-
return [
|
|
691
|
-
...new Set(
|
|
692
|
-
[...operation.sources, ...operation.destinations].flatMap((selection) => {
|
|
693
|
-
const snapshot = snapshots.get(selection.document);
|
|
694
|
-
return snapshot === undefined ? [] : [path.resolve(cwd, snapshot.source)];
|
|
695
|
-
}),
|
|
696
|
-
),
|
|
697
|
-
];
|
|
698
|
-
if (operation.kind === "warning") {
|
|
699
|
-
const snapshot = snapshots.get(operation.document);
|
|
700
|
-
return snapshot === undefined ? [] : [path.resolve(cwd, snapshot.source)];
|
|
701
|
-
}
|
|
702
|
-
if (operation.kind === "replace") {
|
|
703
|
-
const snapshot = snapshots.get(operation.selection.document);
|
|
704
|
-
return snapshot === undefined ? [] : [path.resolve(cwd, snapshot.source)];
|
|
705
|
-
}
|
|
706
|
-
return [
|
|
707
|
-
path.resolve(cwd, operation.path),
|
|
708
|
-
...("target" in operation && operation.target !== undefined
|
|
709
|
-
? [path.resolve(cwd, operation.target)]
|
|
710
|
-
: []),
|
|
711
|
-
];
|
|
712
|
-
}
|
|
713
|
-
function warningDetails(operation: WarningOperation): {
|
|
714
|
-
code: string;
|
|
715
|
-
message: string;
|
|
716
|
-
presentation?: unknown;
|
|
717
|
-
} {
|
|
718
|
-
if (operation.warning !== null && typeof operation.warning === "object") {
|
|
719
|
-
const value = operation.warning as Record<string, unknown>;
|
|
720
|
-
return {
|
|
721
|
-
code: typeof value.code === "string" ? value.code : "EMPTY_SELECTION",
|
|
722
|
-
message:
|
|
723
|
-
typeof value.message === "string"
|
|
724
|
-
? value.message
|
|
725
|
-
: "SelectionSet is empty; no edit was applied.",
|
|
726
|
-
...(value.presentation !== undefined && { presentation: value.presentation }),
|
|
727
|
-
};
|
|
728
|
-
}
|
|
729
|
-
return {
|
|
730
|
-
code: "EMPTY_SELECTION",
|
|
731
|
-
message:
|
|
732
|
-
operation.query === undefined
|
|
733
|
-
? "SelectionSet is empty; no edit was applied."
|
|
734
|
-
: `SelectionSet for "${operation.query}" is empty; no edit was applied.`,
|
|
735
|
-
};
|
|
736
|
-
}
|
|
737
|
-
function failed(
|
|
738
|
-
index: number,
|
|
739
|
-
operation: Operation,
|
|
740
|
-
resources: readonly string[],
|
|
741
|
-
error: unknown,
|
|
742
|
-
): ApplyOperationOutcome {
|
|
743
|
-
return {
|
|
744
|
-
index,
|
|
745
|
-
kind: operation.kind,
|
|
746
|
-
status: "failed",
|
|
747
|
-
effect: "not-applied",
|
|
748
|
-
resources,
|
|
749
|
-
error: codedError(error),
|
|
750
|
-
};
|
|
751
|
-
}
|
|
752
|
-
function codedError(
|
|
753
|
-
error: unknown,
|
|
754
|
-
rollbackErrors: readonly string[] = [],
|
|
755
|
-
): { code: string; message: string } {
|
|
756
|
-
const code =
|
|
757
|
-
error !== null && typeof error === "object" && "code" in error
|
|
758
|
-
? String(error.code)
|
|
759
|
-
: "OPERATION_FAILED";
|
|
760
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
761
|
-
return {
|
|
762
|
-
code: rollbackErrors.length ? "ROLLBACK_INCOMPLETE" : code,
|
|
763
|
-
message: rollbackErrors.length
|
|
764
|
-
? `${message}; rollback failed: ${rollbackErrors.join("; ")}`
|
|
765
|
-
: message,
|
|
766
|
-
};
|
|
767
|
-
}
|
|
768
|
-
|
|
769
|
-
function parseRequest(value: unknown): EditorTransactionRequest {
|
|
770
|
-
const request = record(value, "flush() transaction");
|
|
771
|
-
const snapshots = array(request.snapshots, "snapshots").map((value, index) => {
|
|
772
|
-
const item = record(value, `snapshot ${index + 1}`);
|
|
773
|
-
return {
|
|
774
|
-
id: string(item.id, "snapshot id"),
|
|
775
|
-
source: string(item.source, "snapshot source"),
|
|
776
|
-
content: text(item.content, "snapshot content"),
|
|
777
|
-
};
|
|
778
|
-
});
|
|
779
|
-
const operations = array(request.operations, "operations").map((value, index): Operation => {
|
|
780
|
-
const item = record(value, `operation ${index + 1}`);
|
|
781
|
-
const kind = string(item.kind, "operation kind");
|
|
782
|
-
if (kind === "replace") {
|
|
783
|
-
const selected = record(item.selection, "selection");
|
|
784
|
-
return {
|
|
785
|
-
kind,
|
|
786
|
-
selection: {
|
|
787
|
-
document: string(selected.document, "selection document"),
|
|
788
|
-
from: integer(selected.from, "selection from"),
|
|
789
|
-
to: integer(selected.to, "selection to"),
|
|
790
|
-
text: text(selected.text, "selection text"),
|
|
791
|
-
...(selected.linewise === true && { linewise: true }),
|
|
792
|
-
},
|
|
793
|
-
text: text(item.text, "replacement text"),
|
|
794
|
-
};
|
|
795
|
-
}
|
|
796
|
-
if (kind === "text-copy" || kind === "text-move")
|
|
797
|
-
return {
|
|
798
|
-
kind,
|
|
799
|
-
sources: selections(item.sources, "transfer sources"),
|
|
800
|
-
destinations: selections(item.destinations, "transfer destinations"),
|
|
801
|
-
text: text(item.text, "transfer text"),
|
|
802
|
-
};
|
|
803
|
-
if (kind === "warning")
|
|
804
|
-
return {
|
|
805
|
-
kind,
|
|
806
|
-
document: string(item.document, "warning document"),
|
|
807
|
-
...(typeof item.query === "string" && { query: item.query }),
|
|
808
|
-
...(item.warning !== undefined && { warning: item.warning }),
|
|
809
|
-
};
|
|
810
|
-
if (kind === "create")
|
|
811
|
-
return {
|
|
812
|
-
kind,
|
|
813
|
-
path: string(item.path, "create path"),
|
|
814
|
-
content: text(item.content, "create content"),
|
|
815
|
-
};
|
|
816
|
-
if (kind === "delete") return { kind, path: string(item.path, "delete path") };
|
|
817
|
-
if (kind === "copy" || kind === "move")
|
|
818
|
-
return {
|
|
819
|
-
kind,
|
|
820
|
-
path: string(item.path, `${kind} path`),
|
|
821
|
-
target: string(item.target, `${kind} target`),
|
|
822
|
-
...(item.overwrite === true && { overwrite: true }),
|
|
823
|
-
};
|
|
824
|
-
throw invalid(`Unknown transaction operation: ${kind}`);
|
|
825
|
-
});
|
|
826
|
-
return { snapshots, operations };
|
|
827
|
-
}
|
|
828
|
-
function selections(value: unknown, label: string): Selection[] {
|
|
829
|
-
return array(value, label).map((entry, index) => {
|
|
830
|
-
const item = record(entry, `${label} ${index + 1}`);
|
|
831
|
-
return {
|
|
832
|
-
document: string(item.document, "selection document"),
|
|
833
|
-
from: integer(item.from, "selection from"),
|
|
834
|
-
to: integer(item.to, "selection to"),
|
|
835
|
-
text: text(item.text, "selection text"),
|
|
836
|
-
...(item.linewise === true && { linewise: true }),
|
|
837
|
-
};
|
|
838
|
-
});
|
|
839
|
-
}
|
|
840
|
-
function record(value: unknown, label: string): Record<string, unknown> {
|
|
841
|
-
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
842
|
-
throw invalid(`${label} must be an object`);
|
|
843
|
-
return value as Record<string, unknown>;
|
|
844
|
-
}
|
|
845
|
-
function array(value: unknown, label: string): readonly unknown[] {
|
|
846
|
-
if (!Array.isArray(value)) throw invalid(`${label} must be an array`);
|
|
847
|
-
return value;
|
|
848
|
-
}
|
|
849
|
-
function string(value: unknown, label: string): string {
|
|
850
|
-
if (typeof value !== "string" || value.length === 0)
|
|
851
|
-
throw invalid(`${label} must be a non-empty string`);
|
|
852
|
-
return value;
|
|
853
|
-
}
|
|
854
|
-
function text(value: unknown, label: string): string {
|
|
855
|
-
if (typeof value !== "string") throw invalid(`${label} must be a string`);
|
|
856
|
-
return value;
|
|
857
|
-
}
|
|
858
|
-
function integer(value: unknown, label: string): number {
|
|
859
|
-
if (!Number.isInteger(value)) throw invalid(`${label} must be an integer`);
|
|
860
|
-
return value as number;
|
|
861
|
-
}
|
|
862
|
-
async function performFileOperation(operation: FileOperation, cwd: string): Promise<void> {
|
|
863
|
-
const source = path.resolve(cwd, operation.path);
|
|
864
|
-
if (operation.kind === "delete") {
|
|
865
|
-
await rm(source);
|
|
866
|
-
return;
|
|
867
|
-
}
|
|
868
|
-
const target = path.resolve(cwd, operation.target ?? "");
|
|
869
|
-
await mkdir(path.dirname(target), { recursive: true });
|
|
870
|
-
if (operation.kind === "copy")
|
|
871
|
-
await fs.copy(source, target, { overwrite: operation.overwrite === true, errorOnExist: true });
|
|
872
|
-
else await fs.move(source, target, { overwrite: operation.overwrite === true });
|
|
873
|
-
}
|
|
874
|
-
async function savePath(file: string): Promise<SavedPath> {
|
|
875
|
-
try {
|
|
876
|
-
return { path: file, existed: true, bytes: await readFile(file) };
|
|
877
|
-
} catch (error) {
|
|
878
|
-
if (isCode(error, "ENOENT")) return { path: file, existed: false };
|
|
879
|
-
throw error;
|
|
880
|
-
}
|
|
881
|
-
}
|
|
882
|
-
async function rollback(
|
|
883
|
-
saved: readonly SavedPath[],
|
|
884
|
-
restorePath: (state: SavedPath) => Promise<void> = restoreSavedPath,
|
|
885
|
-
): Promise<readonly string[]> {
|
|
886
|
-
const errors: string[] = [];
|
|
887
|
-
for (const item of [...saved].reverse())
|
|
888
|
-
try {
|
|
889
|
-
await restorePath(item);
|
|
890
|
-
} catch (error) {
|
|
891
|
-
if (!item.existed && (isCode(error, "ENOENT") || isCode(error, "ENOTDIR"))) continue;
|
|
892
|
-
errors.push(`${item.path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
893
|
-
}
|
|
894
|
-
return errors;
|
|
895
|
-
}
|
|
896
|
-
async function restoreSavedPath(item: SavedPath): Promise<void> {
|
|
897
|
-
if (item.existed) {
|
|
898
|
-
await mkdir(path.dirname(item.path), { recursive: true });
|
|
899
|
-
await writeFile(item.path, item.bytes ?? new Uint8Array());
|
|
900
|
-
} else await rm(item.path, { force: true });
|
|
901
|
-
}
|
|
902
|
-
async function optionalStat(file: string) {
|
|
903
|
-
try {
|
|
904
|
-
return await lstat(file);
|
|
905
|
-
} catch (error) {
|
|
906
|
-
if (isCode(error, "ENOENT")) return undefined;
|
|
907
|
-
throw error;
|
|
908
|
-
}
|
|
909
|
-
}
|
|
910
|
-
function isCode(error: unknown, code: string): boolean {
|
|
911
|
-
return error !== null && typeof error === "object" && "code" in error && error.code === code;
|
|
912
|
-
}
|
|
913
|
-
function invalid(message: string): Error {
|
|
914
|
-
return Object.assign(new Error(message), { code: "INVALID_TRANSACTION" });
|
|
915
|
-
}
|
|
916
|
-
function stale(source: string): Error {
|
|
917
|
-
return Object.assign(new Error(`Snapshot changed before flush(): ${source}`), {
|
|
918
|
-
code: "STALE_SNAPSHOT",
|
|
919
|
-
});
|
|
920
|
-
}
|