diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index dba3be5..f1c5639 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -124,6 +124,12 @@ importers: '@codemirror/lang-markdown': specifier: ^6.5.2 version: 6.5.2 + '@codemirror/language': + specifier: ^6.12.4 + version: 6.12.4 + '@codemirror/language-data': + specifier: ^6.5.2 + version: 6.5.2 '@codemirror/state': specifier: ^6.7.4 version: 6.7.4 @@ -540,21 +546,72 @@ packages: '@codemirror/commands@6.11.0': resolution: {integrity: sha512-/K4Rl5BN0OtTiPWmJCdqODu38XnDMsDxKY5rgrPnCkutPTJf2wVbkoixLfealF5Kwse/s8P8M5jAiURiwSwnFA==} + '@codemirror/lang-angular@0.1.4': + resolution: {integrity: sha512-oap+gsltb/fzdlTQWD6BFF4bSLKcDnlxDsLdePiJpCVNKWXSTAbiiQeYI3UmES+BLAdkmIC1WjyztC1pi/bX4g==} + + '@codemirror/lang-cpp@6.0.3': + resolution: {integrity: sha512-URM26M3vunFFn9/sm6rzqrBzDgfWuDixp85uTY49wKudToc2jTHUrKIGGKs+QWND+YLofNNZpxcNGRynFJfvgA==} + '@codemirror/lang-css@6.3.1': resolution: {integrity: sha512-kr5fwBGiGtmz6l0LSJIbno9QrifNMUusivHbnA1H6Dmqy4HZFte3UAICix1VuKo0lMPKQr2rqB+0BkKi/S3Ejg==} + '@codemirror/lang-go@6.0.1': + resolution: {integrity: sha512-7fNvbyNylvqCphW9HD6WFnRpcDjr+KXX/FgqXy5H5ZS0eC5edDljukm/yNgYkwTsgp2busdod50AOTIy6Jikfg==} + '@codemirror/lang-html@6.4.12': resolution: {integrity: sha512-pw2ReWKUqSkbvh76RAT4NYxiogRu+PWkR2ukAwO9uOgrm8uipkzjtKKtNpyeAQwHOqxEeSvAXZ6vr3AfyB9y/w==} + '@codemirror/lang-java@6.0.2': + resolution: {integrity: sha512-m5Nt1mQ/cznJY7tMfQTJchmrjdjQ71IDs+55d1GAa8DGaB8JXWsVCkVT284C3RTASaY43YknrK2X3hPO/J3MOQ==} + '@codemirror/lang-javascript@6.2.5': resolution: {integrity: sha512-zD4e5mS+50htS7F+TYjBPsiIFGanfVqg4HyUz6WNFikgOPf2BgKlx+TQedI1w6n/IqRBVBbBWmGFdLB/7uxO4A==} + '@codemirror/lang-jinja@6.0.1': + resolution: {integrity: sha512-P5kyHLObzjtbGj16h+hyvZTxJhSjBEeSx4wMjbnAf3b0uwTy2+F0zGjMZL4PQOm/mh2eGZ5xUDVZXgwP783Nsw==} + + '@codemirror/lang-json@6.0.2': + resolution: {integrity: sha512-x2OtO+AvwEHrEwR0FyyPtfDUiloG3rnVTSZV1W8UteaLL8/MajQd8DpvUb2YVzC+/T18aSDv0H9mu+xw0EStoQ==} + + '@codemirror/lang-less@6.0.2': + resolution: {integrity: sha512-EYdQTG22V+KUUk8Qq582g7FMnCZeEHsyuOJisHRft/mQ+ZSZ2w51NupvDUHiqtsOy7It5cHLPGfHQLpMh9bqpQ==} + + '@codemirror/lang-liquid@6.3.3': + resolution: {integrity: sha512-U9EnyzWuAx1TX3MZSKDXydKH734n1Aogk/ijKdW+jdIwmbf9zSPQYOIKggC/b2PTRjQb7ddvMktmxnfTLuv/SQ==} + '@codemirror/lang-markdown@6.5.2': resolution: {integrity: sha512-AwBOdkWYuA//WcM0xO5PfHPUcmz/O2i5o0Nsg1U69SII/loCJlFI1Romd9xp2HYb1kYJRGZotyqRghuHH5n8Kw==} + '@codemirror/lang-php@6.0.2': + resolution: {integrity: sha512-ZKy2v1n8Fc8oEXj0Th0PUMXzQJ0AIR6TaZU+PbDHExFwdu+guzOA4jmCHS1Nz4vbFezwD7LyBdDnddSJeScMCA==} + '@codemirror/lang-python@6.2.1': resolution: {integrity: sha512-IRjC8RUBhn9mGR9ywecNhB51yePWCGgvHfY1lWN/Mrp3cKuHr0isDKia+9HnvhiWNnMpbGhWrkhuWOc09exRyw==} + '@codemirror/lang-rust@6.0.2': + resolution: {integrity: sha512-EZaGjCUegtiU7kSMvOfEZpaCReowEf3yNidYu7+vfuGTm9ow4mthAparY5hisJqOHmJowVH3Upu+eJlUji6qqA==} + + '@codemirror/lang-sass@6.0.2': + resolution: {integrity: sha512-l/bdzIABvnTo1nzdY6U+kPAC51czYQcOErfzQ9zSm9D8GmNPD0WTW8st/CJwBTPLO8jlrbyvlSEcN20dc4iL0Q==} + + '@codemirror/lang-sql@6.10.0': + resolution: {integrity: sha512-6ayPkEd/yRw0XKBx5uAiToSgGECo/GY2NoJIHXIIQh1EVwLuKoU8BP/qK0qH5NLXAbtJRLuT73hx7P9X34iO4w==} + + '@codemirror/lang-vue@0.1.3': + resolution: {integrity: sha512-QSKdtYTDRhEHCfo5zOShzxCmqKJvgGrZwDQSdbvCRJ5pRLWBS7pD/8e/tH44aVQT6FKm0t6RVNoSUWHOI5vNug==} + + '@codemirror/lang-wast@6.0.2': + resolution: {integrity: sha512-Imi2KTpVGm7TKuUkqyJ5NRmeFWF7aMpNiwHnLQe0x9kmrxElndyH0K6H/gXtWwY6UshMRAhpENsgfpSwsgmC6Q==} + + '@codemirror/lang-xml@6.1.0': + resolution: {integrity: sha512-3z0blhicHLfwi2UgkZYRPioSgVTo9PV5GP5ducFH6FaHy0IAJRg+ixj5gTR1gnT/glAIC8xv4w2VL1LoZfs+Jg==} + + '@codemirror/lang-yaml@6.1.3': + resolution: {integrity: sha512-AZ8DJBuXGVHybpBQhmZtgew5//4hv3tdkXnr3vDmOUMJRuB6vn/uuwtmTOTlqEaQFg3hQSVeA90NmvIQyUV6FQ==} + + '@codemirror/language-data@6.5.2': + resolution: {integrity: sha512-CPkWBKrNS8stYbEU5kwBwTf3JB1kghlbh4FSAwzGW2TEscdeHHH4FGysREW86Mqnj3Qn09s0/6Ea/TutmoTobg==} + '@codemirror/language@6.12.4': resolution: {integrity: sha512-1q4PaT+o6PbgpkJt4Q8Fv5XJxTy4FUZ4MWETtyiDw3J0Pyr9E2vqcKL+k9wcvjNTIsauxvE7OfmWj3FRPHQ76A==} @@ -1008,27 +1065,54 @@ packages: '@lezer/common@1.5.2': resolution: {integrity: sha512-sxQE460fPZyU3sdc8lafxiPwJHBzZRy/udNFynGQky1SePYBdhkBl1kOagA9uT3pxR8K09bOrmTUqA9wb/PjSQ==} + '@lezer/cpp@1.1.6': + resolution: {integrity: sha512-vh9gWWJOXFVY8HBHK3Twzq8MgwG2iN4GSyzBP9sCGTe37P15x2R14VaBQk0VA0ezTRN1KHYBBsHhvpGZ2Xy/pA==} + '@lezer/css@1.3.6': resolution: {integrity: sha512-YJE78Wcg+zX8f10hiHWQ4Az48Qr/c13eId0VtRQYLBpxHDmDeSrXIlkbl+fJGW42rWC/uoUco9mhBZeVWP/A1g==} + '@lezer/go@1.0.1': + resolution: {integrity: sha512-xToRsYxwsgJNHTgNdStpcvmbVuKxTapV0dM0wey1geMMRc9aggoVyKgzYp41D2/vVOx+Ii4hmE206kvxIXBVXQ==} + '@lezer/highlight@1.2.3': resolution: {integrity: sha512-qXdH7UqTvGfdVBINrgKhDsVTJTxactNNxLk7+UMwZhU13lMHaOBlJe9Vqp907ya56Y3+ed2tlqzys7jDkTmW0g==} '@lezer/html@1.3.13': resolution: {integrity: sha512-oI7n6NJml729m7pjm9lvLvmXbdoMoi2f+1pwSDJkl9d68zGr7a9Btz8NdHTGQZtW2DA25ybeuv/SyDb9D5tseg==} + '@lezer/java@1.1.4': + resolution: {integrity: sha512-gQylJ2xHV2xXsVqtnAcJrcROvLZJaQiC0tR//+q0k961BxqvyYttusPkJ8d9T1fPDJKHJW2etUH8XbIn9jhVUg==} + '@lezer/javascript@1.5.4': resolution: {integrity: sha512-vvYx3MhWqeZtGPwDStM2dwgljd5smolYD2lR2UyFcHfxbBQebqx8yjmFmxtJ/E6nN6u1D9srOiVWm3Rb4tmcUA==} + '@lezer/json@1.0.3': + resolution: {integrity: sha512-BP9KzdF9Y35PDpv04r0VeSTKDeox5vVr3efE7eBbx3r4s3oNLfunchejZhjArmeieBH+nVOpgIiBJpEAv8ilqQ==} + '@lezer/lr@1.4.10': resolution: {integrity: sha512-rnCpTIBafOx4mRp43xOxDJbFipJm/c0cia/V5TiGlhmMa+wsSdoGmUN3w5Bqrks/09Q/D4tNAmWaT8p6NRi77A==} '@lezer/markdown@1.7.2': resolution: {integrity: sha512-iTkYvoVcKt3WkeL7qUDyXHONZEwLio4wj8KTNi2dnjQEXBZKMV63BpQrPqfsM+OkvuRbiSTAcycYAsQzLhRNoQ==} + '@lezer/php@1.0.6': + resolution: {integrity: sha512-QJ2xNXPmsm/Z3IpThq8fnJrEIVpxhsIoj6GZBW0JYEd/l9zxpJZW216X4fzqQY4vgpdGIumypvI4uQjd4tnYtw==} + '@lezer/python@1.1.19': resolution: {integrity: sha512-MhQIURHRytsNzP/YXnqpYKW6la6voAH3kyplTOOiCdjyFY6cWWGFVmYVdHIPrElqSDf4iCDktQCockB9FxuhzQ==} + '@lezer/rust@1.0.3': + resolution: {integrity: sha512-XxErOjZzQ7yJt1agUT4fu9qQvESZ3acgoxpPaPTPOiUx+duCjaVAtZGFIgphkHxlN05djdVAIOy/wItShMEjqQ==} + + '@lezer/sass@1.1.0': + resolution: {integrity: sha512-3mMGdCTUZ/84ArHOuXWQr37pnf7f+Nw9ycPUeKX+wu19b7pSMcZGLbaXwvD2APMBDOGxPmpK/O6S1v1EvLoqgQ==} + + '@lezer/xml@1.0.6': + resolution: {integrity: sha512-CdDwirL0OEaStFue/66ZmFSeppuL6Dwjlk8qk153mSQwiSH/Dlri4GNymrNWnUmPl2Um7QfV1FO9KFUyX3Twww==} + + '@lezer/yaml@1.0.4': + resolution: {integrity: sha512-2lrrHqxalACEbxIbsjhqGpSW8kWpUKuY6RHgnSAFZa6qK62wvnPxA8hGOwOoDbwHcOFs5M4o27mjGu+P7TvBmw==} + '@marijn/find-cluster-break@1.0.4': resolution: {integrity: sha512-Wy0V7+SGUjnF9/TkiM1hKVDPj7jKXduPNboMVtHTA8dySMURWqfg/JZ9E2Sq8JgSJmkl7k7Qe9FLeMSrSraWmQ==} @@ -4214,6 +4298,20 @@ snapshots: '@codemirror/view': 6.43.11 '@lezer/common': 1.5.2 + '@codemirror/lang-angular@0.1.4': + dependencies: + '@codemirror/lang-html': 6.4.12 + '@codemirror/lang-javascript': 6.2.5 + '@codemirror/language': 6.12.4 + '@lezer/common': 1.5.2 + '@lezer/highlight': 1.2.3 + '@lezer/lr': 1.4.10 + + '@codemirror/lang-cpp@6.0.3': + dependencies: + '@codemirror/language': 6.12.4 + '@lezer/cpp': 1.1.6 + '@codemirror/lang-css@6.3.1': dependencies: '@codemirror/autocomplete': 6.20.3 @@ -4222,6 +4320,14 @@ snapshots: '@lezer/common': 1.5.2 '@lezer/css': 1.3.6 + '@codemirror/lang-go@6.0.1': + dependencies: + '@codemirror/autocomplete': 6.20.3 + '@codemirror/language': 6.12.4 + '@codemirror/state': 6.7.4 + '@lezer/common': 1.5.2 + '@lezer/go': 1.0.1 + '@codemirror/lang-html@6.4.12': dependencies: '@codemirror/autocomplete': 6.20.3 @@ -4234,6 +4340,11 @@ snapshots: '@lezer/css': 1.3.6 '@lezer/html': 1.3.13 + '@codemirror/lang-java@6.0.2': + dependencies: + '@codemirror/language': 6.12.4 + '@lezer/java': 1.1.4 + '@codemirror/lang-javascript@6.2.5': dependencies: '@codemirror/autocomplete': 6.20.3 @@ -4244,6 +4355,41 @@ snapshots: '@lezer/common': 1.5.2 '@lezer/javascript': 1.5.4 + '@codemirror/lang-jinja@6.0.1': + dependencies: + '@codemirror/autocomplete': 6.20.3 + '@codemirror/lang-html': 6.4.12 + '@codemirror/language': 6.12.4 + '@codemirror/state': 6.7.4 + '@codemirror/view': 6.43.11 + '@lezer/common': 1.5.2 + '@lezer/highlight': 1.2.3 + '@lezer/lr': 1.4.10 + + '@codemirror/lang-json@6.0.2': + dependencies: + '@codemirror/language': 6.12.4 + '@lezer/json': 1.0.3 + + '@codemirror/lang-less@6.0.2': + dependencies: + '@codemirror/lang-css': 6.3.1 + '@codemirror/language': 6.12.4 + '@lezer/common': 1.5.2 + '@lezer/highlight': 1.2.3 + '@lezer/lr': 1.4.10 + + '@codemirror/lang-liquid@6.3.3': + dependencies: + '@codemirror/autocomplete': 6.20.3 + '@codemirror/lang-html': 6.4.12 + '@codemirror/language': 6.12.4 + '@codemirror/state': 6.7.4 + '@codemirror/view': 6.43.11 + '@lezer/common': 1.5.2 + '@lezer/highlight': 1.2.3 + '@lezer/lr': 1.4.10 + '@codemirror/lang-markdown@6.5.2': dependencies: '@codemirror/autocomplete': 6.20.3 @@ -4254,6 +4400,14 @@ snapshots: '@lezer/common': 1.5.2 '@lezer/markdown': 1.7.2 + '@codemirror/lang-php@6.0.2': + dependencies: + '@codemirror/lang-html': 6.4.12 + '@codemirror/language': 6.12.4 + '@codemirror/state': 6.7.4 + '@lezer/common': 1.5.2 + '@lezer/php': 1.0.6 + '@codemirror/lang-python@6.2.1': dependencies: '@codemirror/autocomplete': 6.20.3 @@ -4262,6 +4416,89 @@ snapshots: '@lezer/common': 1.5.2 '@lezer/python': 1.1.19 + '@codemirror/lang-rust@6.0.2': + dependencies: + '@codemirror/language': 6.12.4 + '@lezer/rust': 1.0.3 + + '@codemirror/lang-sass@6.0.2': + dependencies: + '@codemirror/lang-css': 6.3.1 + '@codemirror/language': 6.12.4 + '@codemirror/state': 6.7.4 + '@lezer/common': 1.5.2 + '@lezer/sass': 1.1.0 + + '@codemirror/lang-sql@6.10.0': + dependencies: + '@codemirror/autocomplete': 6.20.3 + '@codemirror/language': 6.12.4 + '@codemirror/state': 6.7.4 + '@lezer/common': 1.5.2 + '@lezer/highlight': 1.2.3 + '@lezer/lr': 1.4.10 + + '@codemirror/lang-vue@0.1.3': + dependencies: + '@codemirror/lang-html': 6.4.12 + '@codemirror/lang-javascript': 6.2.5 + '@codemirror/language': 6.12.4 + '@lezer/common': 1.5.2 + '@lezer/highlight': 1.2.3 + '@lezer/lr': 1.4.10 + + '@codemirror/lang-wast@6.0.2': + dependencies: + '@codemirror/language': 6.12.4 + '@lezer/common': 1.5.2 + '@lezer/highlight': 1.2.3 + '@lezer/lr': 1.4.10 + + '@codemirror/lang-xml@6.1.0': + dependencies: + '@codemirror/autocomplete': 6.20.3 + '@codemirror/language': 6.12.4 + '@codemirror/state': 6.7.4 + '@codemirror/view': 6.43.11 + '@lezer/common': 1.5.2 + '@lezer/xml': 1.0.6 + + '@codemirror/lang-yaml@6.1.3': + dependencies: + '@codemirror/autocomplete': 6.20.3 + '@codemirror/language': 6.12.4 + '@codemirror/state': 6.7.4 + '@lezer/common': 1.5.2 + '@lezer/highlight': 1.2.3 + '@lezer/lr': 1.4.10 + '@lezer/yaml': 1.0.4 + + '@codemirror/language-data@6.5.2': + dependencies: + '@codemirror/lang-angular': 0.1.4 + '@codemirror/lang-cpp': 6.0.3 + '@codemirror/lang-css': 6.3.1 + '@codemirror/lang-go': 6.0.1 + '@codemirror/lang-html': 6.4.12 + '@codemirror/lang-java': 6.0.2 + '@codemirror/lang-javascript': 6.2.5 + '@codemirror/lang-jinja': 6.0.1 + '@codemirror/lang-json': 6.0.2 + '@codemirror/lang-less': 6.0.2 + '@codemirror/lang-liquid': 6.3.3 + '@codemirror/lang-markdown': 6.5.2 + '@codemirror/lang-php': 6.0.2 + '@codemirror/lang-python': 6.2.1 + '@codemirror/lang-rust': 6.0.2 + '@codemirror/lang-sass': 6.0.2 + '@codemirror/lang-sql': 6.10.0 + '@codemirror/lang-vue': 0.1.3 + '@codemirror/lang-wast': 6.0.2 + '@codemirror/lang-xml': 6.1.0 + '@codemirror/lang-yaml': 6.1.3 + '@codemirror/language': 6.12.4 + '@codemirror/legacy-modes': 6.5.4 + '@codemirror/language@6.12.4': dependencies: '@codemirror/state': 6.7.4 @@ -4588,12 +4825,24 @@ snapshots: '@lezer/common@1.5.2': {} + '@lezer/cpp@1.1.6': + dependencies: + '@lezer/common': 1.5.2 + '@lezer/highlight': 1.2.3 + '@lezer/lr': 1.4.10 + '@lezer/css@1.3.6': dependencies: '@lezer/common': 1.5.2 '@lezer/highlight': 1.2.3 '@lezer/lr': 1.4.10 + '@lezer/go@1.0.1': + dependencies: + '@lezer/common': 1.5.2 + '@lezer/highlight': 1.2.3 + '@lezer/lr': 1.4.10 + '@lezer/highlight@1.2.3': dependencies: '@lezer/common': 1.5.2 @@ -4604,12 +4853,24 @@ snapshots: '@lezer/highlight': 1.2.3 '@lezer/lr': 1.4.10 + '@lezer/java@1.1.4': + dependencies: + '@lezer/common': 1.5.2 + '@lezer/highlight': 1.2.3 + '@lezer/lr': 1.4.10 + '@lezer/javascript@1.5.4': dependencies: '@lezer/common': 1.5.2 '@lezer/highlight': 1.2.3 '@lezer/lr': 1.4.10 + '@lezer/json@1.0.3': + dependencies: + '@lezer/common': 1.5.2 + '@lezer/highlight': 1.2.3 + '@lezer/lr': 1.4.10 + '@lezer/lr@1.4.10': dependencies: '@lezer/common': 1.5.2 @@ -4619,12 +4880,42 @@ snapshots: '@lezer/common': 1.5.2 '@lezer/highlight': 1.2.3 + '@lezer/php@1.0.6': + dependencies: + '@lezer/common': 1.5.2 + '@lezer/highlight': 1.2.3 + '@lezer/lr': 1.4.10 + '@lezer/python@1.1.19': dependencies: '@lezer/common': 1.5.2 '@lezer/highlight': 1.2.3 '@lezer/lr': 1.4.10 + '@lezer/rust@1.0.3': + dependencies: + '@lezer/common': 1.5.2 + '@lezer/highlight': 1.2.3 + '@lezer/lr': 1.4.10 + + '@lezer/sass@1.1.0': + dependencies: + '@lezer/common': 1.5.2 + '@lezer/highlight': 1.2.3 + '@lezer/lr': 1.4.10 + + '@lezer/xml@1.0.6': + dependencies: + '@lezer/common': 1.5.2 + '@lezer/highlight': 1.2.3 + '@lezer/lr': 1.4.10 + + '@lezer/yaml@1.0.4': + dependencies: + '@lezer/common': 1.5.2 + '@lezer/highlight': 1.2.3 + '@lezer/lr': 1.4.10 + '@marijn/find-cluster-break@1.0.4': {} '@mermaid-js/parser@2.0.0': diff --git a/ui-packages/web/package.json b/ui-packages/web/package.json index 5d29868..e3e85fc 100644 --- a/ui-packages/web/package.json +++ b/ui-packages/web/package.json @@ -13,6 +13,8 @@ "dependencies": { "@codemirror/lang-javascript": "^6.2.5", "@codemirror/lang-markdown": "^6.5.2", + "@codemirror/language": "^6.12.4", + "@codemirror/language-data": "^6.5.2", "@codemirror/state": "^6.7.4", "@earendil-works/pi-agent-core": "^0.85.1", "@earendil-works/pi-ai": "^0.85.1", diff --git a/ui-packages/web/src/core/files.ts b/ui-packages/web/src/core/files.ts index db4ece9..a2ca354 100644 --- a/ui-packages/web/src/core/files.ts +++ b/ui-packages/web/src/core/files.ts @@ -64,6 +64,19 @@ const textExtensions = new Set( 'erl r jl gradle properties env lock diff patch', ].flatMap(line => line.split(' ')), ) +// Text that is read as code: line by line, highlighted, laid out as written. +const codeExtensions = new Set( + [ + 'ts tsx js jsx mjs cjs py rb go rs java kt kts swift c h cc cpp hpp cs php sh bash zsh fish', + 'sql css scss less vue svelte lua dart scala clj cljs cljc edn el lisp scm rkt hs ml ex exs', + 'erl r jl json jsonl toml yaml yml xml ini conf gradle properties diff patch', + ].flatMap(line => line.split(' ')), +) +const codeFileNames = new Set(['dockerfile', 'makefile']) + +export const isCodePath = (path: string) => + codeExtensions.has(extensionOf(path)) || codeFileNames.has(baseName(path).toLowerCase()) + // Files a project keeps as text without an extension. const textFileNames = new Set([ 'changelog', diff --git a/ui-packages/web/src/features/reader/code-reader.tsx b/ui-packages/web/src/features/reader/code-reader.tsx new file mode 100644 index 0000000..1f9b9c2 --- /dev/null +++ b/ui-packages/web/src/features/reader/code-reader.tsx @@ -0,0 +1,140 @@ +import { LanguageDescription } from '@codemirror/language' +import { EditorState, type Extension } from '@codemirror/state' +import { basicSetup, EditorView } from 'codemirror' +import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react' +import styles from '../../components/source-editor/style.module.scss' +import { baseName } from '../../core/files' +import type { ReaderState } from '../../core/reader-state' +import type { ReadingPosition } from '../../core/reading-position' +import { useReaderBinding } from '../../shell/workspace-context' +import type { TextReaderProps } from './text-file-reader' + +// Each language arrives the first time a file in it is opened; an unknown one reads as plain text. +const languageFor = async (path: string): Promise => { + const { languages } = await import('@codemirror/language-data') + const description = LanguageDescription.matchFilename(languages, baseName(path)) + return description ? [await description.load()] : [] +} + +// The line at the top of the view and the share of it scrolled past: a flow position by line. +const positionOf = (view: EditorView): ReadingPosition => { + const top = view.scrollDOM.scrollTop - view.documentPadding.top + const block = view.lineBlockAtHeight(Math.max(top, 0)) + const offset = block.height > 0 ? (top - block.top) / block.height : 0 + return { + kind: 'flow', + block: view.state.doc.lineAt(block.from).number - 1, + offset: Math.min(Math.max(offset, 0), 0.999), + } +} + +const restore = (view: EditorView, position: ReadingPosition) => { + if (position.kind !== 'flow') return + const line = view.state.doc.line(Math.min(position.block + 1, view.state.doc.lines)) + view.dispatch({ effects: EditorView.scrollIntoView(line.from, { y: 'start' }) }) +} + +// The first and last lines on screen, so the assistant can find what the reader is looking at. +const visibleText = (view: EditorView | null): ReaderState['viewport'] => { + if (!view?.scrollDOM.clientHeight) return null + const top = Math.max(view.scrollDOM.scrollTop - view.documentPadding.top, 0) + const first = view.lineBlockAtHeight(top) + const last = view.lineBlockAtHeight(top + view.scrollDOM.clientHeight - 1) + return { + startText: view.state.doc.lineAt(first.from).text.trim().slice(0, 100), + endText: view.state.doc.lineAt(last.from).text.trim().slice(-100), + } +} + +// Source code reads in place: highlighted by its language, numbered, foldable and searchable, +// and selectable to copy. It is a reader, not an editor, and has no Source view beside it. +export const CodeReader = ({ + document, + content, + active, + defaultPosition, + onPositionChange, +}: TextReaderProps) => { + const hostRef = useRef(null) + const viewRef = useRef(null) + const activeRef = useRef(active) + const onPositionRef = useRef(onPositionChange) + // The starting place is taken once, when the reader is first shown with its code laid out. + const pendingPosition = useRef(defaultPosition) + const [error, setError] = useState(null) + useLayoutEffect(() => { + activeRef.current = active + onPositionRef.current = onPositionChange + }, [active, onPositionChange]) + + const showStart = useCallback(() => { + const view = viewRef.current + const position = pendingPosition.current + if (!view || !position || !activeRef.current || !view.scrollDOM.clientHeight) return + pendingPosition.current = undefined + restore(view, position) + }, []) + + useEffect(() => { + const host = hostRef.current + if (!host) return + let disposed = false + const report = () => { + const view = viewRef.current + if (view && activeRef.current && !pendingPosition.current) + onPositionRef.current(positionOf(view)) + } + void languageFor(document.path).then( + extensions => { + if (disposed) return + const view = new EditorView({ + parent: host, + state: EditorState.create({ + doc: content, + extensions: [ + basicSetup, + ...extensions, + EditorState.readOnly.of(true), + EditorView.contentAttributes.of({ 'aria-label': `${document.path} code` }), + ], + }), + }) + viewRef.current = view + view.scrollDOM.addEventListener('scroll', report) + showStart() + }, + cause => { + console.error('Unable to load the code reader', cause) + if (!disposed) setError('This code could not be shown. Reload to try again.') + }, + ) + return () => { + disposed = true + const view = viewRef.current + if (view) { + // Saved edits bring new text; the reader picks up where it was rather than at the top. + if (!pendingPosition.current) pendingPosition.current = positionOf(view) + view.scrollDOM.removeEventListener('scroll', report) + view.destroy() + } + viewRef.current = null + } + }, [content, document.path, showStart]) + + // A tab opened in the background lays its code out only once it is shown. + useEffect(() => { + if (active) showStart() + }, [active, showStart]) + + const binding = useMemo( + () => ({ fileId: document.id, getViewport: () => visibleText(viewRef.current) }), + [document.id], + ) + useReaderBinding(binding, active) + + return ( +
+ {error &&

{error}

} +
+ ) +} diff --git a/ui-packages/web/src/features/reader/document-tabs.tsx b/ui-packages/web/src/features/reader/document-tabs.tsx index 357e45b..b9e6ab1 100644 --- a/ui-packages/web/src/features/reader/document-tabs.tsx +++ b/ui-packages/web/src/features/reader/document-tabs.tsx @@ -1,7 +1,7 @@ import * as Tabs from '@radix-ui/react-tabs' import { BookOpen, Code2, MessageSquare, Save } from 'lucide-react' import { lazy, useEffect, useRef, useState } from 'react' -import { baseName, type StoredFileMetadata } from '../../core/files' +import { baseName, isCodePath, type StoredFileMetadata } from '../../core/files' import type { Workspace } from '../../shell/use-workspace' import { useSourceDrafts, useWorkspaceSourceActions } from '../../shell/workspace-context' import { sourceDirty } from '../../shell/workspace-store' @@ -36,8 +36,11 @@ const markdownReader: TextReaderDefinition = { sourceLanguage: 'markdown', } const plainReader: TextReaderDefinition = { Preview: MarkdownReader, sourceLanguage: 'plain' } -const jsReader: TextReaderDefinition = { Preview: MarkdownReader, sourceLanguage: 'javascript' } -const tsReader: TextReaderDefinition = { Preview: MarkdownReader, sourceLanguage: 'typescript' } +// Code reads in CodeMirror, which arrives only when a code file is first opened. +const CodeReader = lazy(() => + import('./code-reader').then(module => ({ default: module.CodeReader })), +) +const codeReader: TextReaderDefinition = { Preview: CodeReader } const p5Reader: TextReaderDefinition = { Preview: P5FileReader, sourceLanguage: 'javascript' } const htmlReader: TextReaderDefinition = { Preview: HtmlReader, sourceLanguage: 'plain' } const svgReader: TextReaderDefinition = { Preview: SvgReader, sourceLanguage: 'plain' } @@ -51,8 +54,7 @@ const textReaderFor = (file: StoredFileMetadata): TextReaderDefinition | undefin if (file.path.toLowerCase().endsWith('.svg') || file.mediaType === 'image/svg+xml') return svgReader if (file.path.toLowerCase().endsWith('.csv') || file.mediaType === 'text/csv') return csvReader - if (/\.(?:js|jsx|mjs|cjs)$/i.test(file.path)) return jsReader - if (/\.(?:ts|tsx)$/i.test(file.path)) return tsReader + if (isCodePath(file.path)) return codeReader if (file.previewKind === 'text') return plainReader return undefined } diff --git a/ui-packages/web/src/features/reader/text-file-reader.tsx b/ui-packages/web/src/features/reader/text-file-reader.tsx index 8dae100..3ee55c5 100644 --- a/ui-packages/web/src/features/reader/text-file-reader.tsx +++ b/ui-packages/web/src/features/reader/text-file-reader.tsx @@ -1,4 +1,4 @@ -import { type ComponentType, type ReactNode, useEffect, useRef, useState } from 'react' +import { type ComponentType, type ReactNode, Suspense, useEffect, useRef, useState } from 'react' import type { MarkdownImageResolver } from '../../components/markdown-image' import type { SourceLanguage } from '../../components/source-editor' import { maximumTextPreviewBytes, type StoredFileMetadata } from '../../core/files' @@ -31,7 +31,8 @@ export type DocumentScopeProps = { export type TextReaderDefinition = { Scope?: ComponentType Preview: TextReaderComponent - sourceLanguage: SourceLanguage + // Absent for formats that are only read, which then open without a Source view or drafts. + sourceLanguage?: SourceLanguage } export const TextFileReader = ({ @@ -101,6 +102,19 @@ export const TextFileReader = ({

{state.message}

) + if (!sourceLanguage) + return ( + Opening {document.path}…}> + + + ) return ( { + // Code reads in CodeMirror, which measures text geometry unavailable in jsdom. + Range.prototype.getClientRects = () => [new DOMRect(0, 0, 100, 20)] as unknown as DOMRectList +}) + const filesList = () => within(screen.getByRole('list', { name: 'Files' })) const waitForFiles = async () => { const panel = await screen.findByRole('complementary', { name: 'Files' }) @@ -224,6 +229,23 @@ describe('file library', () => { } }) + it('reads source code highlighted by its language, without a Source view', async () => { + await writeStoredTextFile('tools/run.py', 'def greet(name):\n return f"hello {name}"\n') + render( + + + , + ) + + const code = await screen.findByRole('textbox', { name: 'tools/run.py code' }) + expect(code).toHaveTextContent('def greet(name):') + expect(code).toHaveAttribute('aria-readonly', 'true') + // Python support loads on demand; once it has, the keyword stands apart from plain text. + await waitFor(() => expect(within(code).getByText('def').tagName).toBe('SPAN')) + // Code is read, not edited here, so there is no Source view to open beside it. + expect(screen.queryByRole('button', { name: 'Source' })).toBeNull() + }) + it('keeps the add-files action in the toolbar when the library is empty', async () => { await Promise.all(samples.map(file => removeStoredFile(file.id))) render(