diff --git a/.github/actions/build_docs/action.yml b/.github/actions/build_docs/action.yml index d2e62d5b22ee49c7dcb9b42085a648098fbdb6bb..1ff271f73ff6b800ec3a94615f31c35a7729bb47 100644 --- a/.github/actions/build_docs/action.yml +++ b/.github/actions/build_docs/action.yml @@ -19,6 +19,18 @@ runs: shell: bash -euxo pipefail {0} run: ./script/linux + - name: Install mold linker + shell: bash -euxo pipefail {0} + run: ./script/install-mold + + - name: Download WASI SDK + shell: bash -euxo pipefail {0} + run: ./script/download-wasi-sdk + + - name: Generate action metadata + shell: bash -euxo pipefail {0} + run: ./script/generate-action-metadata + - name: Check for broken links (in MD) uses: lycheeverse/lychee-action@82202e5e9c2f4ef1a55a3d02563e1cb6041e5332 # v2.4.1 with: diff --git a/.github/workflows/autofix_pr.yml b/.github/workflows/autofix_pr.yml index 762b86c446f4592e8fd76c8f5a00cf8cf8ab3f38..d3688a722aa107efb3dfb95351404f43c9aece65 100644 --- a/.github/workflows/autofix_pr.yml +++ b/.github/workflows/autofix_pr.yml @@ -90,7 +90,7 @@ jobs: runs-on: namespace-profile-2x4-ubuntu-2404 steps: - id: get-app-token - name: autofix_pr::commit_changes::authenticate_as_zippy + name: steps::authenticate_as_zippy uses: actions/create-github-app-token@bef1eaf1c0ac2b148ee2a0a74c65fbe6db0631f1 with: app-id: ${{ secrets.ZED_ZIPPY_APP_ID }} @@ -123,3 +123,6 @@ jobs: GIT_AUTHOR_NAME: Zed Zippy GIT_AUTHOR_EMAIL: 234243425+zed-zippy[bot]@users.noreply.github.com GITHUB_TOKEN: ${{ steps.get-app-token.outputs.token }} +concurrency: + group: ${{ github.workflow }}-${{ inputs.pr_number }} + cancel-in-progress: true diff --git a/.github/workflows/cherry_pick.yml b/.github/workflows/cherry_pick.yml index bc01aae17e7141a2359b162c3de94c1aec7b765c..d4dee5154f2209521f3e9d183c05c118e8861521 100644 --- a/.github/workflows/cherry_pick.yml +++ b/.github/workflows/cherry_pick.yml @@ -30,7 +30,7 @@ jobs: with: clean: false - id: get-app-token - name: cherry_pick::run_cherry_pick::authenticate_as_zippy + name: steps::authenticate_as_zippy uses: actions/create-github-app-token@bef1eaf1c0ac2b148ee2a0a74c65fbe6db0631f1 with: app-id: ${{ secrets.ZED_ZIPPY_APP_ID }} diff --git a/.github/workflows/community_champion_auto_labeler.yml b/.github/workflows/community_champion_auto_labeler.yml index 93f1d5602331bb76fe5d678098ab8c087b1f3d52..d73b38320731e0a2f9a52ff863de5095eddb7b6a 100644 --- a/.github/workflows/community_champion_auto_labeler.yml +++ b/.github/workflows/community_champion_auto_labeler.yml @@ -34,6 +34,7 @@ jobs: CharlesChen0823 chbk cppcoffee + davidbarsky davewa ddoemonn djsauble diff --git a/.github/workflows/community_close_stale_issues.yml b/.github/workflows/community_close_stale_issues.yml index 14c1a0a08338ee513a8269094b41ee404beef726..6347b713257f49c02f981774faa0d0359e05e4d3 100644 --- a/.github/workflows/community_close_stale_issues.yml +++ b/.github/workflows/community_close_stale_issues.yml @@ -1,29 +1,40 @@ name: "Close Stale Issues" on: schedule: - - cron: "0 8 31 DEC *" + - cron: "0 2 * * 5" workflow_dispatch: + inputs: + debug-only: + description: "Run in dry-run mode (no changes made)" + type: boolean + default: false + operations-per-run: + description: "Max number of issues to process (default: 1000)" + type: number + default: 1000 jobs: stale: if: github.repository_owner == 'zed-industries' runs-on: ubuntu-latest steps: - - uses: actions/stale@5bef64f19d7facfb25b37b414482c7164d639639 # v9 + - uses: actions/stale@997185467fa4f803885201cee163a9f38240193d # v10 with: repo-token: ${{ secrets.GITHUB_TOKEN }} stale-issue-message: > - Hi there! 👋 - - We're working to clean up our issue tracker by closing older bugs that might not be relevant anymore. If you are able to reproduce this issue in the latest version of Zed, please let us know by commenting on this issue, and it will be kept open. If you can't reproduce it, feel free to close the issue yourself. Otherwise, it will close automatically in 14 days. + Hi there! + Zed development moves fast and a significant number of bugs become outdated. + If you can reproduce this bug on the latest stable Zed, please let us know by leaving a comment with the Zed version. + If the bug doesn't appear for you anymore, feel free to close the issue yourself; otherwise, the bot will close it in a couple of weeks. Thanks for your help! - close-issue-message: "This issue was closed due to inactivity. If you're still experiencing this problem, please open a new issue with a link to this issue." + close-issue-message: "This issue was closed due to inactivity. If you're still experiencing this problem, please leave a comment with your Zed version so that we can reopen the issue." days-before-stale: 60 days-before-close: 14 only-issue-types: "Bug,Crash" - operations-per-run: 1000 + operations-per-run: ${{ inputs.operations-per-run || 1000 }} ascending: true enable-statistics: true + debug-only: ${{ inputs.debug-only }} stale-issue-label: "stale" exempt-issue-labels: "never stale" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7afac285b5a34df2aadd04952400809059e12222..ffc2554a55e00a5bdb7bd1ee0bfeebd5667755d5 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -472,11 +472,17 @@ jobs: if: startsWith(github.ref, 'refs/tags/v') && endsWith(github.ref, '-pre') && !endsWith(github.ref, '.0-pre') runs-on: namespace-profile-2x4-ubuntu-2404 steps: + - id: get-app-token + name: steps::authenticate_as_zippy + uses: actions/create-github-app-token@bef1eaf1c0ac2b148ee2a0a74c65fbe6db0631f1 + with: + app-id: ${{ secrets.ZED_ZIPPY_APP_ID }} + private-key: ${{ secrets.ZED_ZIPPY_APP_PRIVATE_KEY }} - name: gh release edit "$GITHUB_REF_NAME" --repo=zed-industries/zed --draft=false run: gh release edit "$GITHUB_REF_NAME" --repo=zed-industries/zed --draft=false shell: bash -euxo pipefail {0} env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_TOKEN: ${{ steps.get-app-token.outputs.token }} notify_on_failure: needs: - upload_release_assets diff --git a/.github/workflows/run_tests.yml b/.github/workflows/run_tests.yml index 9584d7a0cb70469820bf40d76beb6154f2a53b1e..47a84574e7c33fb8a40a90c67cd4f7dadb356978 100644 --- a/.github/workflows/run_tests.yml +++ b/.github/workflows/run_tests.yml @@ -74,9 +74,12 @@ jobs: uses: pnpm/action-setup@fe02b34f77f8bc703788d5817da081398fad5dd2 with: version: '9' - - name: ./script/prettier + - name: steps::prettier run: ./script/prettier shell: bash -euxo pipefail {0} + - name: steps::cargo_fmt + run: cargo fmt --all -- --check + shell: bash -euxo pipefail {0} - name: ./script/check-todos run: ./script/check-todos shell: bash -euxo pipefail {0} @@ -87,9 +90,6 @@ jobs: uses: crate-ci/typos@2d0ce569feab1f8752f1dde43cc2f2aa53236e06 with: config: ./typos.toml - - name: steps::cargo_fmt - run: cargo fmt --all -- --check - shell: bash -euxo pipefail {0} timeout-minutes: 60 run_tests_windows: needs: @@ -353,6 +353,9 @@ jobs: - name: steps::download_wasi_sdk run: ./script/download-wasi-sdk shell: bash -euxo pipefail {0} + - name: ./script/generate-action-metadata + run: ./script/generate-action-metadata + shell: bash -euxo pipefail {0} - name: run_tests::check_docs::install_mdbook uses: peaceiris/actions-mdbook@ee69d230fe19748b7abf22df32acaa93833fad08 with: diff --git a/.gitignore b/.gitignore index 54faaf1374299ee8f97925a95a93b375c349d707..c71417c32bff76af9d4c9c67661556e1625c9d15 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,7 @@ DerivedData/ Packages xcuserdata/ +crates/docs_preprocessor/actions.json # Don't commit any secrets to the repo. .env diff --git a/.mailmap b/.mailmap index db4632d6ca34346d3e8fa289222d7f310b7bdfe5..1e956c52cf76589fc016e1410122ccd94e4818ae 100644 --- a/.mailmap +++ b/.mailmap @@ -141,6 +141,9 @@ Uladzislau Kaminski Uladzislau Kaminski Vitaly Slobodin Vitaly Slobodin +Yara +Yara +Yara Will Bradley Will Bradley WindSoilder diff --git a/Cargo.lock b/Cargo.lock index 20f19de832c567c6866731f128f049bd77d7be57..f9acd6989be8734b6c5b528435fccea62d10f027 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -226,9 +226,9 @@ dependencies = [ [[package]] name = "agent-client-protocol" -version = "0.9.0" +version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2ffe7d502c1e451aafc5aff655000f84d09c9af681354ac0012527009b1af13" +checksum = "d3e527d7dfe0f334313d42d1d9318f0a79665f6f21c440d0798f230a77a7ed6c" dependencies = [ "agent-client-protocol-schema", "anyhow", @@ -243,9 +243,9 @@ dependencies = [ [[package]] name = "agent-client-protocol-schema" -version = "0.10.0" +version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8af81cc2d5c3f9c04f73db452efd058333735ba9d51c2cf7ef33c9fee038e7e6" +checksum = "6903a00e8ac822f9bacac59a1932754d7387c72ebb7c9c7439ad021505591da4" dependencies = [ "anyhow", "derive_more 2.0.1", @@ -793,7 +793,7 @@ dependencies = [ "url", "wayland-backend", "wayland-client", - "wayland-protocols 0.32.9", + "wayland-protocols", "zbus", ] @@ -1441,9 +1441,9 @@ dependencies = [ [[package]] name = "aws-config" -version = "1.8.8" +version = "1.8.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37cf2b6af2a95a20e266782b4f76f1a5e12bf412a9db2de9c1e9123b9d8c0ad8" +checksum = "1856b1b48b65f71a4dd940b1c0931f9a7b646d4a924b9828ffefc1454714668a" dependencies = [ "aws-credential-types", "aws-runtime", @@ -1507,9 +1507,9 @@ dependencies = [ [[package]] name = "aws-runtime" -version = "1.5.12" +version = "1.5.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa006bb32360ed90ac51203feafb9d02e3d21046e1fd3a450a404b90ea73e5d" +checksum = "9f2402da1a5e16868ba98725e5d73f26b8116eaa892e56f2cd0bf5eec7985f70" dependencies = [ "aws-credential-types", "aws-sigv4", @@ -1532,9 +1532,9 @@ dependencies = [ [[package]] name = "aws-sdk-bedrockruntime" -version = "1.109.0" +version = "1.112.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbfdfd941dcb253c17bf70baddbf1e5b22f19e29d313d2e049bad4b1dadb2011" +checksum = "c06c037e6823696d752702ec2bad758d3cf95d1b92b712c8ac7e93824b5e2391" dependencies = [ "aws-credential-types", "aws-runtime", @@ -1614,9 +1614,9 @@ dependencies = [ [[package]] name = "aws-sdk-sso" -version = "1.86.0" +version = "1.88.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a0abbfab841446cce6e87af853a3ba2cc1bc9afcd3f3550dd556c43d434c86d" +checksum = "d05b276777560aa9a196dbba2e3aada4d8006d3d7eeb3ba7fe0c317227d933c4" dependencies = [ "aws-credential-types", "aws-runtime", @@ -1636,9 +1636,9 @@ dependencies = [ [[package]] name = "aws-sdk-ssooidc" -version = "1.88.0" +version = "1.90.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a68d675582afea0e94d38b6ca9c5aaae4ca14f1d36faa6edb19b42e687e70d7" +checksum = "f9be14d6d9cd761fac3fd234a0f47f7ed6c0df62d83c0eeb7012750e4732879b" dependencies = [ "aws-credential-types", "aws-runtime", @@ -1658,9 +1658,9 @@ dependencies = [ [[package]] name = "aws-sdk-sts" -version = "1.88.0" +version = "1.90.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d30990923f4f675523c51eb1c0dec9b752fb267b36a61e83cbc219c9d86da715" +checksum = "98a862d704c817d865c8740b62d8bbeb5adcb30965e93b471df8a5bcefa20a80" dependencies = [ "aws-credential-types", "aws-runtime", @@ -1681,9 +1681,9 @@ dependencies = [ [[package]] name = "aws-sigv4" -version = "1.3.5" +version = "1.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bffc03068fbb9c8dd5ce1c6fb240678a5cffb86fb2b7b1985c999c4b83c8df68" +checksum = "c35452ec3f001e1f2f6db107b6373f1f48f05ec63ba2c5c9fa91f07dad32af11" dependencies = [ "aws-credential-types", "aws-smithy-eventstream", @@ -1740,9 +1740,9 @@ dependencies = [ [[package]] name = "aws-smithy-eventstream" -version = "0.60.12" +version = "0.60.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9656b85088f8d9dc7ad40f9a6c7228e1e8447cdf4b046c87e152e0805dea02fa" +checksum = "e29a304f8319781a39808847efb39561351b1bb76e933da7aa90232673638658" dependencies = [ "aws-smithy-types", "bytes 1.10.1", @@ -1751,9 +1751,9 @@ dependencies = [ [[package]] name = "aws-smithy-http" -version = "0.62.4" +version = "0.62.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3feafd437c763db26aa04e0cc7591185d0961e64c61885bece0fb9d50ceac671" +checksum = "445d5d720c99eed0b4aa674ed00d835d9b1427dd73e04adaf2f94c6b2d6f9fca" dependencies = [ "aws-smithy-eventstream", "aws-smithy-runtime-api", @@ -1761,6 +1761,7 @@ dependencies = [ "bytes 1.10.1", "bytes-utils", "futures-core", + "futures-util", "http 0.2.12", "http 1.3.1", "http-body 0.4.6", @@ -1772,9 +1773,9 @@ dependencies = [ [[package]] name = "aws-smithy-http-client" -version = "1.1.3" +version = "1.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1053b5e587e6fa40ce5a79ea27957b04ba660baa02b28b7436f64850152234f1" +checksum = "623254723e8dfd535f566ee7b2381645f8981da086b5c4aa26c0c41582bb1d2c" dependencies = [ "aws-smithy-async", "aws-smithy-runtime-api", @@ -1802,9 +1803,9 @@ dependencies = [ [[package]] name = "aws-smithy-json" -version = "0.61.6" +version = "0.61.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cff418fc8ec5cadf8173b10125f05c2e7e1d46771406187b2c878557d4503390" +checksum = "2db31f727935fc63c6eeae8b37b438847639ec330a9161ece694efba257e0c54" dependencies = [ "aws-smithy-types", ] @@ -1830,9 +1831,9 @@ dependencies = [ [[package]] name = "aws-smithy-runtime" -version = "1.9.3" +version = "1.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40ab99739082da5347660c556689256438defae3bcefd66c52b095905730e404" +checksum = "0bbe9d018d646b96c7be063dd07987849862b0e6d07c778aad7d93d1be6c1ef0" dependencies = [ "aws-smithy-async", "aws-smithy-http", @@ -1854,9 +1855,9 @@ dependencies = [ [[package]] name = "aws-smithy-runtime-api" -version = "1.9.1" +version = "1.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3683c5b152d2ad753607179ed71988e8cfd52964443b4f74fd8e552d0bbfeb46" +checksum = "ec7204f9fd94749a7c53b26da1b961b4ac36bf070ef1e0b94bb09f79d4f6c193" dependencies = [ "aws-smithy-async", "aws-smithy-types", @@ -1871,9 +1872,9 @@ dependencies = [ [[package]] name = "aws-smithy-types" -version = "1.3.3" +version = "1.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f5b3a7486f6690ba25952cabf1e7d75e34d69eaff5081904a47bc79074d6457" +checksum = "25f535879a207fce0db74b679cfc3e91a3159c8144d717d55f5832aea9eef46e" dependencies = [ "base64-simd", "bytes 1.10.1", @@ -1897,18 +1898,18 @@ dependencies = [ [[package]] name = "aws-smithy-xml" -version = "0.60.11" +version = "0.60.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9c34127e8c624bc2999f3b657e749c1393bedc9cd97b92a804db8ced4d2e163" +checksum = "eab77cdd036b11056d2a30a7af7b775789fb024bf216acc13884c6c97752ae56" dependencies = [ "xmlparser", ] [[package]] name = "aws-types" -version = "1.3.9" +version = "1.3.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2fd329bf0e901ff3f60425691410c69094dc2a1f34b331f37bfc4e9ac1565a1" +checksum = "d79fb68e3d7fe5d4833ea34dc87d2e97d26d3086cb3da660bb6b1f76d98680b6" dependencies = [ "aws-credential-types", "aws-smithy-async", @@ -2666,9 +2667,9 @@ dependencies = [ [[package]] name = "cap-fs-ext" -version = "3.4.5" +version = "3.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5528f85b1e134ae811704e41ef80930f56e795923f866813255bc342cc20654" +checksum = "e41cc18551193fe8fa6f15c1e3c799bc5ec9e2cfbfaa8ed46f37013e3e6c173c" dependencies = [ "cap-primitives", "cap-std", @@ -2678,9 +2679,9 @@ dependencies = [ [[package]] name = "cap-net-ext" -version = "3.4.5" +version = "3.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20a158160765c6a7d0d8c072a53d772e4cb243f38b04bfcf6b4939cfbe7482e7" +checksum = "9f83833816c66c986e913b22ac887cec216ea09301802054316fc5301809702c" dependencies = [ "cap-primitives", "cap-std", @@ -2690,9 +2691,9 @@ dependencies = [ [[package]] name = "cap-primitives" -version = "3.4.5" +version = "3.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6cf3aea8a5081171859ef57bc1606b1df6999df4f1110f8eef68b30098d1d3a" +checksum = "0a1e394ed14f39f8bc26f59d4c0c010dbe7f0a1b9bafff451b1f98b67c8af62a" dependencies = [ "ambient-authority", "fs-set-times", @@ -2708,9 +2709,9 @@ dependencies = [ [[package]] name = "cap-rand" -version = "3.4.5" +version = "3.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8144c22e24bbcf26ade86cb6501a0916c46b7e4787abdb0045a467eb1645a1d" +checksum = "0acb89ccf798a28683f00089d0630dfaceec087234eae0d308c05ddeaa941b40" dependencies = [ "ambient-authority", "rand 0.8.5", @@ -2718,9 +2719,9 @@ dependencies = [ [[package]] name = "cap-std" -version = "3.4.5" +version = "3.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6dc3090992a735d23219de5c204927163d922f42f575a0189b005c62d37549a" +checksum = "07c0355ca583dd58f176c3c12489d684163861ede3c9efa6fd8bba314c984189" dependencies = [ "cap-primitives", "io-extras", @@ -2730,9 +2731,9 @@ dependencies = [ [[package]] name = "cap-time-ext" -version = "3.4.5" +version = "3.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "def102506ce40c11710a9b16e614af0cde8e76ae51b1f48c04b8d79f4b671a80" +checksum = "491af520b8770085daa0466978c75db90368c71896523f2464214e38359b1a5b" dependencies = [ "ambient-authority", "cap-primitives", @@ -2895,6 +2896,17 @@ dependencies = [ "util", ] +[[package]] +name = "chardetng" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14b8f0b65b7b08ae3c8187e8d77174de20cb6777864c6b832d8ad365999cf1ea" +dependencies = [ + "cfg-if", + "encoding_rs", + "memchr", +] + [[package]] name = "chrono" version = "0.4.42" @@ -5009,8 +5021,6 @@ name = "docs_preprocessor" version = "0.1.0" dependencies = [ "anyhow", - "command_palette", - "gpui", "mdbook", "regex", "serde", @@ -5019,7 +5029,6 @@ dependencies = [ "task", "theme", "util", - "zed", "zlog", ] @@ -7358,7 +7367,7 @@ dependencies = [ "wayland-backend", "wayland-client", "wayland-cursor", - "wayland-protocols 0.31.2", + "wayland-protocols", "wayland-protocols-plasma", "wayland-protocols-wlr", "windows 0.61.3", @@ -8796,6 +8805,7 @@ dependencies = [ "ctor", "diffy", "ec4rs", + "encoding_rs", "fs", "futures 0.3.31", "fuzzy", @@ -8919,6 +8929,8 @@ dependencies = [ "credentials_provider", "deepseek", "editor", + "extension", + "extension_host", "fs", "futures 0.3.31", "google_ai", @@ -12464,6 +12476,7 @@ dependencies = [ "dap", "dap_adapters", "db", + "encoding_rs", "extension", "fancy-regex", "fs", @@ -12557,6 +12570,7 @@ dependencies = [ "gpui", "language", "menu", + "notifications", "pretty_assertions", "project", "rayon", @@ -12634,6 +12648,8 @@ dependencies = [ "paths", "rope", "serde", + "strum 0.27.2", + "tempfile", "text", "util", "uuid", @@ -18913,18 +18929,6 @@ dependencies = [ "xcursor", ] -[[package]] -name = "wayland-protocols" -version = "0.31.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f81f365b8b4a97f422ac0e8737c438024b5951734506b0e1d775c73030561f4" -dependencies = [ - "bitflags 2.9.4", - "wayland-backend", - "wayland-client", - "wayland-scanner", -] - [[package]] name = "wayland-protocols" version = "0.32.9" @@ -18939,14 +18943,14 @@ dependencies = [ [[package]] name = "wayland-protocols-plasma" -version = "0.2.0" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23803551115ff9ea9bce586860c5c5a971e360825a0309264102a9495a5ff479" +checksum = "a07a14257c077ab3279987c4f8bb987851bf57081b93710381daea94f2c2c032" dependencies = [ "bitflags 2.9.4", "wayland-backend", "wayland-client", - "wayland-protocols 0.31.2", + "wayland-protocols", "wayland-scanner", ] @@ -18959,7 +18963,7 @@ dependencies = [ "bitflags 2.9.4", "wayland-backend", "wayland-client", - "wayland-protocols 0.32.9", + "wayland-protocols", "wayland-scanner", ] @@ -19119,6 +19123,20 @@ dependencies = [ "winsafe", ] +[[package]] +name = "which_key" +version = "0.1.0" +dependencies = [ + "command_palette", + "gpui", + "serde", + "settings", + "theme", + "ui", + "util", + "workspace", +] + [[package]] name = "whoami" version = "1.6.1" @@ -20216,8 +20234,10 @@ version = "0.1.0" dependencies = [ "anyhow", "async-lock 2.8.0", + "chardetng", "clock", "collections", + "encoding_rs", "fs", "futures 0.3.31", "fuzzy", @@ -20245,6 +20265,16 @@ dependencies = [ "zlog", ] +[[package]] +name = "worktree_benchmarks" +version = "0.1.0" +dependencies = [ + "fs", + "gpui", + "settings", + "worktree", +] + [[package]] name = "writeable" version = "0.6.1" @@ -20729,6 +20759,7 @@ dependencies = [ "watch", "web_search", "web_search_providers", + "which_key", "windows 0.61.3", "winresource", "workspace", diff --git a/Cargo.toml b/Cargo.toml index f46ffa2583c022be8704e95684ddce65b19d3fab..b507e8824484ea670619b5225fef9cfd41c81d4c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -192,11 +192,13 @@ members = [ "crates/vercel", "crates/vim", "crates/vim_mode_setting", + "crates/which_key", "crates/watch", "crates/web_search", "crates/web_search_providers", "crates/workspace", "crates/worktree", + "crates/worktree_benchmarks", "crates/x_ai", "crates/zed", "crates/zed_actions", @@ -415,6 +417,7 @@ util_macros = { path = "crates/util_macros" } vercel = { path = "crates/vercel" } vim = { path = "crates/vim" } vim_mode_setting = { path = "crates/vim_mode_setting" } +which_key = { path = "crates/which_key" } watch = { path = "crates/watch" } web_search = { path = "crates/web_search" } @@ -436,7 +439,7 @@ ztracing_macro = { path = "crates/ztracing_macro" } # External crates # -agent-client-protocol = { version = "=0.9.0", features = ["unstable"] } +agent-client-protocol = { version = "=0.9.2", features = ["unstable"] } aho-corasick = "1.1" alacritty_terminal = "0.25.1-rc1" any_vec = "0.14" @@ -455,15 +458,15 @@ async-task = "4.7" async-trait = "0.1" async-tungstenite = "0.31.0" async_zip = { version = "0.0.18", features = ["deflate", "deflate64"] } -aws-config = { version = "1.6.1", features = ["behavior-version-latest"] } -aws-credential-types = { version = "1.2.2", features = [ +aws-config = { version = "1.8.10", features = ["behavior-version-latest"] } +aws-credential-types = { version = "1.2.8", features = [ "hardcoded-credentials", ] } -aws-sdk-bedrockruntime = { version = "1.80.0", features = [ +aws-sdk-bedrockruntime = { version = "1.112.0", features = [ "behavior-version-latest", ] } -aws-smithy-runtime-api = { version = "1.7.4", features = ["http-1x", "client"] } -aws-smithy-types = { version = "1.3.0", features = ["http-body-1-x"] } +aws-smithy-runtime-api = { version = "1.9.2", features = ["http-1x", "client"] } +aws-smithy-types = { version = "1.3.4", features = ["http-body-1-x"] } backtrace = "0.3" base64 = "0.22" bincode = "1.2.1" @@ -476,6 +479,7 @@ bytes = "1.0" cargo_metadata = "0.19" cargo_toml = "0.21" cfg-if = "1.0.3" +chardetng = "0.1" chrono = { version = "0.4", features = ["serde"] } ciborium = "0.2" circular-buffer = "1.0" @@ -499,6 +503,7 @@ dotenvy = "0.15.0" ec4rs = "1.1" emojis = "0.6.1" env_logger = "0.11" +encoding_rs = "0.8" exec = "0.3.1" fancy-regex = "0.16.0" fork = "0.4.0" diff --git a/README.md b/README.md index d3a5fd20526e5eae6826241dce2bb94e8533ecb3..866762c8c9139666993c2e29d9682966106c516b 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,6 @@ Other platforms are not yet available: - [Building Zed for macOS](./docs/src/development/macos.md) - [Building Zed for Linux](./docs/src/development/linux.md) - [Building Zed for Windows](./docs/src/development/windows.md) -- [Running Collaboration Locally](./docs/src/development/local-collaboration.md) ### Contributing diff --git a/REVIEWERS.conl b/REVIEWERS.conl index 45155ba3468f29062b58aa9094defc7f86110885..bca694d7a06fe1112f7f8bab158dad63a365ea74 100644 --- a/REVIEWERS.conl +++ b/REVIEWERS.conl @@ -28,7 +28,7 @@ ai = @rtfeldman audio - = @dvdsk + = @yara-blue crashes = @p1n3appl3 @@ -53,7 +53,7 @@ extension git = @cole-miller = @danilo-leal - = @dvdsk + = @yara-blue = @kubkon = @Anthony-Eid = @cameron1024 @@ -76,7 +76,7 @@ languages linux = @cole-miller - = @dvdsk + = @yara-blue = @p1n3appl3 = @probably-neb = @smitbarmase @@ -92,7 +92,7 @@ multi_buffer = @SomeoneToIgnore pickers - = @dvdsk + = @yara-blue = @p1n3appl3 = @SomeoneToIgnore diff --git a/assets/keymaps/default-linux.json b/assets/keymaps/default-linux.json index 1016a20bd6facdc8f5ef9163ebda3e03d451c5cf..465c7d86aeaff23bdebe65792304ac2963edaaa7 100644 --- a/assets/keymaps/default-linux.json +++ b/assets/keymaps/default-linux.json @@ -227,6 +227,7 @@ "ctrl-g": "search::SelectNextMatch", "ctrl-shift-g": "search::SelectPreviousMatch", "ctrl-k l": "agent::OpenRulesLibrary", + "ctrl-shift-v": "agent::PasteRaw", }, }, { @@ -264,9 +265,9 @@ { "context": "AgentPanel > Markdown", "bindings": { - "copy": "markdown::Copy", - "ctrl-insert": "markdown::Copy", - "ctrl-c": "markdown::Copy", + "copy": "markdown::CopyAsMarkdown", + "ctrl-insert": "markdown::CopyAsMarkdown", + "ctrl-c": "markdown::CopyAsMarkdown", }, }, { @@ -293,6 +294,7 @@ "shift-ctrl-r": "agent::OpenAgentDiff", "ctrl-shift-y": "agent::KeepAll", "ctrl-shift-n": "agent::RejectAll", + "ctrl-shift-v": "agent::PasteRaw", }, }, { @@ -304,6 +306,7 @@ "shift-ctrl-r": "agent::OpenAgentDiff", "ctrl-shift-y": "agent::KeepAll", "ctrl-shift-n": "agent::RejectAll", + "ctrl-shift-v": "agent::PasteRaw", }, }, { @@ -905,8 +908,8 @@ "bindings": { "left": "git_panel::CollapseSelectedEntry", "right": "git_panel::ExpandSelectedEntry", - "up": "menu::SelectPrevious", - "down": "menu::SelectNext", + "up": "git_panel::PreviousEntry", + "down": "git_panel::NextEntry", "enter": "menu::Confirm", "alt-y": "git::StageFile", "alt-shift-y": "git::UnstageFile", diff --git a/assets/keymaps/default-macos.json b/assets/keymaps/default-macos.json index c80edf01a02347cf678fe9cb24390f2fca41d70e..7ff00c41d5d6108b2a0b9fa0de85c511fab1f6e0 100644 --- a/assets/keymaps/default-macos.json +++ b/assets/keymaps/default-macos.json @@ -267,6 +267,7 @@ "cmd-shift-g": "search::SelectPreviousMatch", "cmd-k l": "agent::OpenRulesLibrary", "alt-tab": "agent::CycleFavoriteModels", + "cmd-shift-v": "agent::PasteRaw", }, }, { @@ -306,7 +307,7 @@ "context": "AgentPanel > Markdown", "use_key_equivalents": true, "bindings": { - "cmd-c": "markdown::Copy", + "cmd-c": "markdown::CopyAsMarkdown", }, }, { @@ -335,6 +336,7 @@ "shift-ctrl-r": "agent::OpenAgentDiff", "cmd-shift-y": "agent::KeepAll", "cmd-shift-n": "agent::RejectAll", + "cmd-shift-v": "agent::PasteRaw", }, }, { @@ -347,6 +349,7 @@ "shift-ctrl-r": "agent::OpenAgentDiff", "cmd-shift-y": "agent::KeepAll", "cmd-shift-n": "agent::RejectAll", + "cmd-shift-v": "agent::PasteRaw", }, }, { @@ -981,12 +984,12 @@ "context": "GitPanel && ChangesList", "use_key_equivalents": true, "bindings": { + "up": "git_panel::PreviousEntry", + "down": "git_panel::NextEntry", + "cmd-up": "git_panel::FirstEntry", + "cmd-down": "git_panel::LastEntry", "left": "git_panel::CollapseSelectedEntry", "right": "git_panel::ExpandSelectedEntry", - "up": "menu::SelectPrevious", - "down": "menu::SelectNext", - "cmd-up": "menu::SelectFirst", - "cmd-down": "menu::SelectLast", "enter": "menu::Confirm", "cmd-alt-y": "git::ToggleStaged", "space": "git::ToggleStaged", diff --git a/assets/keymaps/default-windows.json b/assets/keymaps/default-windows.json index dcc828ddf2ef63f3fef6e7e12d9349bead57572e..445933c950cbc9ef72eb2cca90ab8115471f1e6f 100644 --- a/assets/keymaps/default-windows.json +++ b/assets/keymaps/default-windows.json @@ -227,6 +227,7 @@ "ctrl-g": "search::SelectNextMatch", "ctrl-shift-g": "search::SelectPreviousMatch", "ctrl-k l": "agent::OpenRulesLibrary", + "ctrl-shift-v": "agent::PasteRaw", }, }, { @@ -267,7 +268,7 @@ "context": "AgentPanel > Markdown", "use_key_equivalents": true, "bindings": { - "ctrl-c": "markdown::Copy", + "ctrl-c": "markdown::CopyAsMarkdown", }, }, { @@ -296,6 +297,7 @@ "ctrl-shift-r": "agent::OpenAgentDiff", "ctrl-shift-y": "agent::KeepAll", "ctrl-shift-n": "agent::RejectAll", + "ctrl-shift-v": "agent::PasteRaw", }, }, { @@ -308,6 +310,7 @@ "ctrl-shift-r": "agent::OpenAgentDiff", "ctrl-shift-y": "agent::KeepAll", "ctrl-shift-n": "agent::RejectAll", + "ctrl-shift-v": "agent::PasteRaw", }, }, { @@ -908,10 +911,10 @@ "context": "GitPanel && ChangesList", "use_key_equivalents": true, "bindings": { + "up": "git_panel::PreviousEntry", + "down": "git_panel::NextEntry", "left": "git_panel::CollapseSelectedEntry", "right": "git_panel::ExpandSelectedEntry", - "up": "menu::SelectPrevious", - "down": "menu::SelectNext", "enter": "menu::Confirm", "alt-y": "git::StageFile", "shift-alt-y": "git::UnstageFile", diff --git a/assets/prompts/content_prompt_v2.hbs b/assets/prompts/content_prompt_v2.hbs index 87376f49f12f0e27cc61e9f9747d9de6bfde43cb..826aada8c04863c21d756cf99beb64e582ed4906 100644 --- a/assets/prompts/content_prompt_v2.hbs +++ b/assets/prompts/content_prompt_v2.hbs @@ -14,7 +14,6 @@ The section you'll need to rewrite is marked with The context around the relevant section has been truncated (possibly in the middle of a line) for brevity. {{/if}} -{{#if rewrite_section}} And here's the section to rewrite based on that prompt again for reference: @@ -33,8 +32,6 @@ Below are the diagnostic errors visible to the user. If the user requests probl {{/each}} {{/if}} -{{/if}} - Only make changes that are necessary to fulfill the prompt, leave everything else as-is. All surrounding {{content_type}} will be preserved. Start at the indentation level in the original file in the rewritten {{content_type}}. diff --git a/assets/settings/default.json b/assets/settings/default.json index a0e499934428b4bafcbe12b97b2e8fc4747a5f31..746ccb5986d0fd1d5ef11df525303e344a7393d2 100644 --- a/assets/settings/default.json +++ b/assets/settings/default.json @@ -1178,6 +1178,10 @@ "remove_trailing_whitespace_on_save": true, // Whether to start a new line with a comment when a previous line is a comment as well. "extend_comment_on_newline": true, + // Whether to continue markdown lists when pressing enter. + "extend_list_on_newline": true, + // Whether to indent list items when pressing tab after a list marker. + "indent_list_on_tab": true, // Removes any lines containing only whitespace at the end of the file and // ensures just one newline at the end. "ensure_final_newline_on_save": true, @@ -1321,6 +1325,14 @@ "hidden_files": ["**/.*"], // Git gutter behavior configuration. "git": { + // Global switch to enable or disable all git integration features. + // If set to true, disables all git integration features. + // If set to false, individual git integration features below will be independently enabled or disabled. + "disable_git": false, + // Whether to enable git status tracking. + "enable_status": true, + // Whether to enable git diff display. + "enable_diff": true, // Control whether the git gutter is shown. May take 2 values: // 1. Show the gutter // "git_gutter": "tracked_files" @@ -1705,7 +1717,12 @@ // } // "file_types": { - "JSONC": ["**/.zed/**/*.json", "**/zed/**/*.json", "**/Zed/**/*.json", "**/.vscode/**/*.json", "tsconfig*.json"], + "JSONC": [ + "**/.zed/*.json", + "**/.vscode/**/*.json", + "**/{zed,Zed}/{settings,keymap,tasks,debug}.json", + "tsconfig*.json", + ], "Markdown": [".rules", ".cursorrules", ".windsurfrules", ".clinerules"], "Shell Script": [".env.*"], }, @@ -2152,6 +2169,13 @@ // The shape can be one of the following: "block", "bar", "underline", "hollow". "cursor_shape": {}, }, + // Which-key popup settings + "which_key": { + // Whether to show the which-key popup when holding down key combinations. + "enabled": false, + // Delay in milliseconds before showing the which-key popup. + "delay_ms": 1000, + }, // The server to connect to. If the environment variable // ZED_SERVER_URL is set, it will override this setting. "server_url": "https://zed.dev", diff --git a/crates/acp_thread/src/acp_thread.rs b/crates/acp_thread/src/acp_thread.rs index 2ec6347fd4aa088d7ae2cc8f5a7b6cef37d3b202..a994cc8e57e4456ec57092b2257269b104af74c7 100644 --- a/crates/acp_thread/src/acp_thread.rs +++ b/crates/acp_thread/src/acp_thread.rs @@ -192,6 +192,7 @@ pub struct ToolCall { pub locations: Vec, pub resolved_locations: Vec>, pub raw_input: Option, + pub raw_input_markdown: Option>, pub raw_output: Option, } @@ -222,6 +223,11 @@ impl ToolCall { } } + let raw_input_markdown = tool_call + .raw_input + .as_ref() + .and_then(|input| markdown_for_raw_output(input, &language_registry, cx)); + let result = Self { id: tool_call.tool_call_id, label: cx @@ -232,6 +238,7 @@ impl ToolCall { resolved_locations: Vec::default(), status, raw_input: tool_call.raw_input, + raw_input_markdown, raw_output: tool_call.raw_output, }; Ok(result) @@ -307,6 +314,7 @@ impl ToolCall { } if let Some(raw_input) = raw_input { + self.raw_input_markdown = markdown_for_raw_output(&raw_input, &language_registry, cx); self.raw_input = Some(raw_input); } @@ -1355,6 +1363,7 @@ impl AcpThread { locations: Vec::new(), resolved_locations: Vec::new(), raw_input: None, + raw_input_markdown: None, raw_output: None, }; self.push_entry(AgentThreadEntry::ToolCall(failed_tool_call), cx); diff --git a/crates/acp_thread/src/connection.rs b/crates/acp_thread/src/connection.rs index a670ba601159ec323ad2c88695c30bf4aeae4118..598d0428174eb2fc124739a18ddeff1098521cb7 100644 --- a/crates/acp_thread/src/connection.rs +++ b/crates/acp_thread/src/connection.rs @@ -210,12 +210,21 @@ pub trait AgentModelSelector: 'static { } } +/// Icon for a model in the model selector. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum AgentModelIcon { + /// A built-in icon from Zed's icon set. + Named(IconName), + /// Path to a custom SVG icon file. + Path(SharedString), +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct AgentModelInfo { pub id: acp::ModelId, pub name: SharedString, pub description: Option, - pub icon: Option, + pub icon: Option, } impl From for AgentModelInfo { diff --git a/crates/agent/src/agent.rs b/crates/agent/src/agent.rs index 5e16f74682ef95a4e990ed5a124a0d6031acfb0e..4baa7f4ea4004d2137b5cddb255346fa91523091 100644 --- a/crates/agent/src/agent.rs +++ b/crates/agent/src/agent.rs @@ -30,7 +30,7 @@ use futures::{StreamExt, future}; use gpui::{ App, AppContext, AsyncApp, Context, Entity, SharedString, Subscription, Task, WeakEntity, }; -use language_model::{LanguageModel, LanguageModelProvider, LanguageModelRegistry}; +use language_model::{IconOrSvg, LanguageModel, LanguageModelProvider, LanguageModelRegistry}; use project::{Project, ProjectItem, ProjectPath, Worktree}; use prompt_store::{ ProjectContext, PromptStore, RULES_FILE_NAMES, RulesFileContext, UserRulesContext, @@ -93,7 +93,7 @@ impl LanguageModels { fn refresh_list(&mut self, cx: &App) { let providers = LanguageModelRegistry::global(cx) .read(cx) - .providers() + .visible_providers() .into_iter() .filter(|provider| provider.is_authenticated(cx)) .collect::>(); @@ -153,7 +153,10 @@ impl LanguageModels { id: Self::model_id(model), name: model.name().0, description: None, - icon: Some(provider.icon()), + icon: Some(match provider.icon() { + IconOrSvg::Svg(path) => acp_thread::AgentModelIcon::Path(path), + IconOrSvg::Icon(name) => acp_thread::AgentModelIcon::Named(name), + }), } } @@ -164,7 +167,7 @@ impl LanguageModels { fn authenticate_all_language_model_providers(cx: &mut App) -> Task<()> { let authenticate_all_providers = LanguageModelRegistry::global(cx) .read(cx) - .providers() + .visible_providers() .iter() .map(|provider| (provider.id(), provider.name(), provider.authenticate(cx))) .collect::>(); @@ -426,7 +429,7 @@ impl NativeAgent { .into_iter() .flat_map(|(contents, prompt_metadata)| match contents { Ok(contents) => Some(UserRulesContext { - uuid: prompt_metadata.id.user_id()?, + uuid: prompt_metadata.id.as_user()?, title: prompt_metadata.title.map(|title| title.to_string()), contents, }), @@ -1630,7 +1633,9 @@ mod internal_tests { id: acp::ModelId::new("fake/fake"), name: "Fake".into(), description: None, - icon: Some(ui::IconName::ZedAssistant), + icon: Some(acp_thread::AgentModelIcon::Named( + ui::IconName::ZedAssistant + )), }] )]) ); diff --git a/crates/agent/src/history_store.rs b/crates/agent/src/history_store.rs index 5a1b923d139060ed7df679a69d96928d03559c9d..c455f73316e3fc7a641fa8a31ac0ad766a2ae584 100644 --- a/crates/agent/src/history_store.rs +++ b/crates/agent/src/history_store.rs @@ -216,14 +216,10 @@ impl HistoryStore { } pub fn reload(&self, cx: &mut Context) { - let database_future = ThreadsDatabase::connect(cx); + let database_connection = ThreadsDatabase::connect(cx); cx.spawn(async move |this, cx| { - let threads = database_future - .await - .map_err(|err| anyhow!(err))? - .list_threads() - .await?; - + let database = database_connection.await; + let threads = database.map_err(|err| anyhow!(err))?.list_threads().await?; this.update(cx, |this, cx| { if this.recently_opened_entries.len() < MAX_RECENTLY_OPENED_ENTRIES { for thread in threads @@ -344,7 +340,8 @@ impl HistoryStore { fn load_recently_opened_entries(cx: &AsyncApp) -> Task>> { cx.background_spawn(async move { if cfg!(any(feature = "test-support", test)) { - anyhow::bail!("history store does not persist in tests"); + log::warn!("history store does not persist in tests"); + return Ok(VecDeque::new()); } let json = KEY_VALUE_STORE .read_kvp(RECENTLY_OPENED_THREADS_KEY)? diff --git a/crates/agent/src/thread.rs b/crates/agent/src/thread.rs index f8f46af5fe2bbea5888ded6e24495afee71680dd..ef3ca23c3caf816a28e91e9e75b21f2cc80451e7 100644 --- a/crates/agent/src/thread.rs +++ b/crates/agent/src/thread.rs @@ -1725,6 +1725,10 @@ impl Thread { self.pending_summary_generation.is_some() } + pub fn is_generating_title(&self) -> bool { + self.pending_title_generation.is_some() + } + pub fn summary(&mut self, cx: &mut Context) -> Shared>> { if let Some(summary) = self.summary.as_ref() { return Task::ready(Some(summary.clone())).shared(); @@ -1792,7 +1796,7 @@ impl Thread { task } - fn generate_title(&mut self, cx: &mut Context) { + pub fn generate_title(&mut self, cx: &mut Context) { let Some(model) = self.summarization_model.clone() else { return; }; diff --git a/crates/agent_ui/Cargo.toml b/crates/agent_ui/Cargo.toml index 38580b4d2c61597718d9fb718a20e52e84222481..8a9633e578a85323f2a289bd83c169a1f5d7f272 100644 --- a/crates/agent_ui/Cargo.toml +++ b/crates/agent_ui/Cargo.toml @@ -13,7 +13,7 @@ path = "src/agent_ui.rs" doctest = false [features] -test-support = ["assistant_text_thread/test-support", "eval_utils", "gpui/test-support", "language/test-support", "reqwest_client", "workspace/test-support"] +test-support = ["assistant_text_thread/test-support", "eval_utils", "gpui/test-support", "language/test-support", "reqwest_client", "workspace/test-support", "agent/test-support"] unit-eval = [] [dependencies] diff --git a/crates/agent_ui/src/acp/message_editor.rs b/crates/agent_ui/src/acp/message_editor.rs index 5e9c55cc56868ac2e7db65043d13eb46efcd89a6..6bed82accf876aaaba0668d366216c3a965ad8cb 100644 --- a/crates/agent_ui/src/acp/message_editor.rs +++ b/crates/agent_ui/src/acp/message_editor.rs @@ -34,7 +34,7 @@ use theme::ThemeSettings; use ui::prelude::*; use util::{ResultExt, debug_panic}; use workspace::{CollaboratorId, Workspace}; -use zed_actions::agent::Chat; +use zed_actions::agent::{Chat, PasteRaw}; pub struct MessageEditor { mention_set: Entity, @@ -543,6 +543,9 @@ impl MessageEditor { } fn paste(&mut self, _: &Paste, window: &mut Window, cx: &mut Context) { + let Some(workspace) = self.workspace.upgrade() else { + return; + }; let editor_clipboard_selections = cx .read_from_clipboard() .and_then(|item| item.entries().first().cloned()) @@ -553,133 +556,127 @@ impl MessageEditor { _ => None, }); - let has_file_context = editor_clipboard_selections - .as_ref() - .is_some_and(|selections| { - selections - .iter() - .any(|sel| sel.file_path.is_some() && sel.line_range.is_some()) - }); + // Insert creases for pasted clipboard selections that: + // 1. Contain exactly one selection + // 2. Have an associated file path + // 3. Span multiple lines (not single-line selections) + // 4. Belong to a file that exists in the current project + let should_insert_creases = util::maybe!({ + let selections = editor_clipboard_selections.as_ref()?; + if selections.len() > 1 { + return Some(false); + } + let selection = selections.first()?; + let file_path = selection.file_path.as_ref()?; + let line_range = selection.line_range.as_ref()?; - if has_file_context { - if let Some((workspace, selections)) = - self.workspace.upgrade().zip(editor_clipboard_selections) - { - let Some(first_selection) = selections.first() else { - return; - }; - if let Some(file_path) = &first_selection.file_path { - // In case someone pastes selections from another window - // with a different project, we don't want to insert the - // crease (containing the absolute path) since the agent - // cannot access files outside the project. - let is_in_project = workspace - .read(cx) - .project() - .read(cx) - .project_path_for_absolute_path(file_path, cx) - .is_some(); - if !is_in_project { - return; - } - } + if line_range.start() == line_range.end() { + return Some(false); + } - cx.stop_propagation(); - let insertion_target = self - .editor + Some( + workspace .read(cx) - .selections - .newest_anchor() - .start - .text_anchor; - - let project = workspace.read(cx).project().clone(); - for selection in selections { - if let (Some(file_path), Some(line_range)) = - (selection.file_path, selection.line_range) - { - let crease_text = - acp_thread::selection_name(Some(file_path.as_ref()), &line_range); + .project() + .read(cx) + .project_path_for_absolute_path(file_path, cx) + .is_some(), + ) + }) + .unwrap_or(false); - let mention_uri = MentionUri::Selection { - abs_path: Some(file_path.clone()), - line_range: line_range.clone(), - }; + if should_insert_creases && let Some(selections) = editor_clipboard_selections { + cx.stop_propagation(); + let insertion_target = self + .editor + .read(cx) + .selections + .newest_anchor() + .start + .text_anchor; + + let project = workspace.read(cx).project().clone(); + for selection in selections { + if let (Some(file_path), Some(line_range)) = + (selection.file_path, selection.line_range) + { + let crease_text = + acp_thread::selection_name(Some(file_path.as_ref()), &line_range); - let mention_text = mention_uri.as_link().to_string(); - let (excerpt_id, text_anchor, content_len) = - self.editor.update(cx, |editor, cx| { - let buffer = editor.buffer().read(cx); - let snapshot = buffer.snapshot(cx); - let (excerpt_id, _, buffer_snapshot) = - snapshot.as_singleton().unwrap(); - let text_anchor = insertion_target.bias_left(&buffer_snapshot); - - editor.insert(&mention_text, window, cx); - editor.insert(" ", window, cx); - - (*excerpt_id, text_anchor, mention_text.len()) - }); - - let Some((crease_id, tx)) = insert_crease_for_mention( - excerpt_id, - text_anchor, - content_len, - crease_text.into(), - mention_uri.icon_path(cx), - None, - self.editor.clone(), - window, - cx, - ) else { - continue; - }; - drop(tx); - - let mention_task = cx - .spawn({ - let project = project.clone(); - async move |_, cx| { - let project_path = project - .update(cx, |project, cx| { - project.project_path_for_absolute_path(&file_path, cx) - }) - .map_err(|e| e.to_string())? - .ok_or_else(|| "project path not found".to_string())?; - - let buffer = project - .update(cx, |project, cx| { - project.open_buffer(project_path, cx) - }) - .map_err(|e| e.to_string())? - .await - .map_err(|e| e.to_string())?; - - buffer - .update(cx, |buffer, cx| { - let start = Point::new(*line_range.start(), 0) - .min(buffer.max_point()); - let end = Point::new(*line_range.end() + 1, 0) - .min(buffer.max_point()); - let content = - buffer.text_for_range(start..end).collect(); - Mention::Text { - content, - tracked_buffers: vec![cx.entity()], - } - }) - .map_err(|e| e.to_string()) - } - }) - .shared(); + let mention_uri = MentionUri::Selection { + abs_path: Some(file_path.clone()), + line_range: line_range.clone(), + }; + + let mention_text = mention_uri.as_link().to_string(); + let (excerpt_id, text_anchor, content_len) = + self.editor.update(cx, |editor, cx| { + let buffer = editor.buffer().read(cx); + let snapshot = buffer.snapshot(cx); + let (excerpt_id, _, buffer_snapshot) = snapshot.as_singleton().unwrap(); + let text_anchor = insertion_target.bias_left(&buffer_snapshot); + + editor.insert(&mention_text, window, cx); + editor.insert(" ", window, cx); - self.mention_set.update(cx, |mention_set, _cx| { - mention_set.insert_mention(crease_id, mention_uri.clone(), mention_task) + (*excerpt_id, text_anchor, mention_text.len()) }); - } + + let Some((crease_id, tx)) = insert_crease_for_mention( + excerpt_id, + text_anchor, + content_len, + crease_text.into(), + mention_uri.icon_path(cx), + None, + self.editor.clone(), + window, + cx, + ) else { + continue; + }; + drop(tx); + + let mention_task = cx + .spawn({ + let project = project.clone(); + async move |_, cx| { + let project_path = project + .update(cx, |project, cx| { + project.project_path_for_absolute_path(&file_path, cx) + }) + .map_err(|e| e.to_string())? + .ok_or_else(|| "project path not found".to_string())?; + + let buffer = project + .update(cx, |project, cx| project.open_buffer(project_path, cx)) + .map_err(|e| e.to_string())? + .await + .map_err(|e| e.to_string())?; + + buffer + .update(cx, |buffer, cx| { + let start = Point::new(*line_range.start(), 0) + .min(buffer.max_point()); + let end = Point::new(*line_range.end() + 1, 0) + .min(buffer.max_point()); + let content = buffer.text_for_range(start..end).collect(); + Mention::Text { + content, + tracked_buffers: vec![cx.entity()], + } + }) + .map_err(|e| e.to_string()) + } + }) + .shared(); + + self.mention_set.update(cx, |mention_set, _cx| { + mention_set.insert_mention(crease_id, mention_uri.clone(), mention_task) + }); } - return; } + return; } if self.prompt_capabilities.borrow().image @@ -690,6 +687,13 @@ impl MessageEditor { } } + fn paste_raw(&mut self, _: &PasteRaw, window: &mut Window, cx: &mut Context) { + let editor = self.editor.clone(); + window.defer(cx, move |window, cx| { + editor.update(cx, |editor, cx| editor.paste(&Paste, window, cx)); + }); + } + pub fn insert_dragged_files( &mut self, paths: Vec, @@ -967,6 +971,7 @@ impl Render for MessageEditor { .on_action(cx.listener(Self::chat)) .on_action(cx.listener(Self::chat_with_follow)) .on_action(cx.listener(Self::cancel)) + .on_action(cx.listener(Self::paste_raw)) .capture_action(cx.listener(Self::paste)) .flex_1() .child({ @@ -1365,7 +1370,7 @@ mod tests { cx, ); }); - message_editor.read(cx).focus_handle(cx).focus(window); + message_editor.read(cx).focus_handle(cx).focus(window, cx); message_editor.read(cx).editor().clone() }); @@ -1587,7 +1592,7 @@ mod tests { cx, ); }); - message_editor.read(cx).focus_handle(cx).focus(window); + message_editor.read(cx).focus_handle(cx).focus(window, cx); let editor = message_editor.read(cx).editor().clone(); (message_editor, editor) }); @@ -2315,7 +2320,7 @@ mod tests { cx, ); }); - message_editor.read(cx).focus_handle(cx).focus(window); + message_editor.read(cx).focus_handle(cx).focus(window, cx); let editor = message_editor.read(cx).editor().clone(); (message_editor, editor) }); diff --git a/crates/agent_ui/src/acp/mode_selector.rs b/crates/agent_ui/src/acp/mode_selector.rs index 1f50ce74321d393ba6c7f5083bd889bc3dc2c0e1..22af75a6e96edc4f597819e04e2e84b80ba0417a 100644 --- a/crates/agent_ui/src/acp/mode_selector.rs +++ b/crates/agent_ui/src/acp/mode_selector.rs @@ -188,25 +188,25 @@ impl Render for ModeSelector { .gap_1() .child( h_flex() - .pb_1() .gap_2() .justify_between() - .border_b_1() - .border_color(cx.theme().colors().border_variant) - .child(Label::new("Cycle Through Modes")) + .child(Label::new("Toggle Mode Menu")) .child(KeyBinding::for_action_in( - &CycleModeSelector, + &ToggleProfileSelector, &focus_handle, cx, )), ) .child( h_flex() + .pb_1() .gap_2() .justify_between() - .child(Label::new("Toggle Mode Menu")) + .border_b_1() + .border_color(cx.theme().colors().border_variant) + .child(Label::new("Cycle Through Modes")) .child(KeyBinding::for_action_in( - &ToggleProfileSelector, + &CycleModeSelector, &focus_handle, cx, )), diff --git a/crates/agent_ui/src/acp/model_selector.rs b/crates/agent_ui/src/acp/model_selector.rs index cff5334a00472fd6f49abcb17897b4ed3c9f590e..903d5fe425d99389aae0e2a8028d9a31b986fbb3 100644 --- a/crates/agent_ui/src/acp/model_selector.rs +++ b/crates/agent_ui/src/acp/model_selector.rs @@ -1,6 +1,6 @@ use std::{cmp::Reverse, rc::Rc, sync::Arc}; -use acp_thread::{AgentModelInfo, AgentModelList, AgentModelSelector}; +use acp_thread::{AgentModelIcon, AgentModelInfo, AgentModelList, AgentModelSelector}; use agent_client_protocol::ModelId; use agent_servers::AgentServer; use agent_settings::AgentSettings; @@ -221,7 +221,7 @@ impl PickerDelegate for AcpModelPickerDelegate { cx: &mut Context>, ) -> Task<()> { let favorites = if self.selector.supports_favorites() { - Arc::new(AgentSettings::get_global(cx).favorite_model_ids()) + AgentSettings::get_global(cx).favorite_model_ids() } else { Default::default() }; @@ -242,7 +242,7 @@ impl PickerDelegate for AcpModelPickerDelegate { this.update_in(cx, |this, window, cx| { this.delegate.filtered_entries = - info_list_to_picker_entries(filtered_models, favorites); + info_list_to_picker_entries(filtered_models, &favorites); // Finds the currently selected model in the list let new_index = this .delegate @@ -350,7 +350,11 @@ impl PickerDelegate for AcpModelPickerDelegate { }) .child( ModelSelectorListItem::new(ix, model_info.name.clone()) - .when_some(model_info.icon, |this, icon| this.icon(icon)) + .map(|this| match &model_info.icon { + Some(AgentModelIcon::Path(path)) => this.icon_path(path.clone()), + Some(AgentModelIcon::Named(icon)) => this.icon(*icon), + None => this, + }) .is_selected(is_selected) .is_focused(selected) .when(supports_favorites, |this| { @@ -406,7 +410,7 @@ impl PickerDelegate for AcpModelPickerDelegate { fn info_list_to_picker_entries( model_list: AgentModelList, - favorites: Arc>, + favorites: &HashSet, ) -> Vec { let mut entries = Vec::new(); @@ -572,13 +576,11 @@ mod tests { } } - fn create_favorites(models: Vec<&str>) -> Arc> { - Arc::new( - models - .into_iter() - .map(|m| ModelId::new(m.to_string())) - .collect(), - ) + fn create_favorites(models: Vec<&str>) -> HashSet { + models + .into_iter() + .map(|m| ModelId::new(m.to_string())) + .collect() } fn get_entry_model_ids(entries: &[AcpModelPickerEntry]) -> Vec<&str> { @@ -609,7 +611,7 @@ mod tests { ]); let favorites = create_favorites(vec!["zed/gemini"]); - let entries = info_list_to_picker_entries(models, favorites); + let entries = info_list_to_picker_entries(models, &favorites); assert!(matches!( entries.first(), @@ -625,7 +627,7 @@ mod tests { let models = create_model_list(vec![("zed", vec!["zed/claude", "zed/gemini"])]); let favorites = create_favorites(vec![]); - let entries = info_list_to_picker_entries(models, favorites); + let entries = info_list_to_picker_entries(models, &favorites); assert!(matches!( entries.first(), @@ -641,7 +643,7 @@ mod tests { ]); let favorites = create_favorites(vec!["zed/claude"]); - let entries = info_list_to_picker_entries(models, favorites); + let entries = info_list_to_picker_entries(models, &favorites); for entry in &entries { if let AcpModelPickerEntry::Model(info, is_favorite) = entry { @@ -662,7 +664,7 @@ mod tests { ]); let favorites = create_favorites(vec!["zed/gemini", "openai/gpt-5"]); - let entries = info_list_to_picker_entries(models, favorites); + let entries = info_list_to_picker_entries(models, &favorites); let model_ids = get_entry_model_ids(&entries); assert_eq!(model_ids[0], "zed/gemini"); @@ -683,7 +685,7 @@ mod tests { let favorites = create_favorites(vec!["zed/claude"]); - let entries = info_list_to_picker_entries(models, favorites); + let entries = info_list_to_picker_entries(models, &favorites); let labels = get_entry_labels(&entries); assert_eq!( @@ -723,7 +725,7 @@ mod tests { ]); let favorites = create_favorites(vec!["zed/gemini"]); - let entries = info_list_to_picker_entries(models, favorites); + let entries = info_list_to_picker_entries(models, &favorites); assert!(matches!( entries.first(), diff --git a/crates/agent_ui/src/acp/model_selector_popover.rs b/crates/agent_ui/src/acp/model_selector_popover.rs index d6709081863c9545fba4c6e2304f195e77b013df..a15c01445dd8e9845f6744e795ed90a1ede6c7fc 100644 --- a/crates/agent_ui/src/acp/model_selector_popover.rs +++ b/crates/agent_ui/src/acp/model_selector_popover.rs @@ -1,7 +1,7 @@ use std::rc::Rc; use std::sync::Arc; -use acp_thread::{AgentModelInfo, AgentModelSelector}; +use acp_thread::{AgentModelIcon, AgentModelInfo, AgentModelSelector}; use agent_servers::AgentServer; use agent_settings::AgentSettings; use fs::Fs; @@ -70,7 +70,7 @@ impl Render for AcpModelSelectorPopover { .map(|model| model.name.clone()) .unwrap_or_else(|| SharedString::from("Select a Model")); - let model_icon = model.as_ref().and_then(|model| model.icon); + let model_icon = model.as_ref().and_then(|model| model.icon.clone()); let focus_handle = self.focus_handle.clone(); @@ -125,7 +125,14 @@ impl Render for AcpModelSelectorPopover { ButtonLike::new("active-model") .selected_style(ButtonStyle::Tinted(TintColor::Accent)) .when_some(model_icon, |this, icon| { - this.child(Icon::new(icon).color(color).size(IconSize::XSmall)) + this.child( + match icon { + AgentModelIcon::Path(path) => Icon::from_external_svg(path), + AgentModelIcon::Named(icon_name) => Icon::new(icon_name), + } + .color(color) + .size(IconSize::XSmall), + ) }) .child( Label::new(model_name) diff --git a/crates/agent_ui/src/acp/thread_history.rs b/crates/agent_ui/src/acp/thread_history.rs index 1aa89b35d34c8c0543a56014fee7766b6de66eb2..a885e52a05e342dbcd81d28a970560b3047ef9c0 100644 --- a/crates/agent_ui/src/acp/thread_history.rs +++ b/crates/agent_ui/src/acp/thread_history.rs @@ -1,7 +1,7 @@ use crate::acp::AcpThreadView; use crate::{AgentPanel, RemoveHistory, RemoveSelectedThread}; use agent::{HistoryEntry, HistoryStore}; -use chrono::{Datelike as _, Local, NaiveDate, TimeDelta}; +use chrono::{Datelike as _, Local, NaiveDate, TimeDelta, Utc}; use editor::{Editor, EditorEvent}; use fuzzy::StringMatchCandidate; use gpui::{ @@ -402,7 +402,22 @@ impl AcpThreadHistory { let selected = ix == self.selected_index; let hovered = Some(ix) == self.hovered_index; let timestamp = entry.updated_at().timestamp(); - let thread_timestamp = format.format_timestamp(timestamp, self.local_timezone); + + let display_text = match format { + EntryTimeFormat::DateAndTime => { + let entry_time = entry.updated_at(); + let now = Utc::now(); + let duration = now.signed_duration_since(entry_time); + let days = duration.num_days(); + + format!("{}d", days) + } + EntryTimeFormat::TimeOnly => format.format_timestamp(timestamp, self.local_timezone), + }; + + let title = entry.title().clone(); + let full_date = + EntryTimeFormat::DateAndTime.format_timestamp(timestamp, self.local_timezone); h_flex() .w_full() @@ -423,11 +438,14 @@ impl AcpThreadHistory { .truncate(), ) .child( - Label::new(thread_timestamp) + Label::new(display_text) .color(Color::Muted) .size(LabelSize::XSmall), ), ) + .tooltip(move |_, cx| { + Tooltip::with_meta(title.clone(), None, full_date.clone(), cx) + }) .on_hover(cx.listener(move |this, is_hovered, _window, cx| { if *is_hovered { this.hovered_index = Some(ix); diff --git a/crates/agent_ui/src/acp/thread_view.rs b/crates/agent_ui/src/acp/thread_view.rs index 05162348db060bff05aa7b1dd223815895f02e2d..32b2de2c0d850676bf7a6a80ee88950d62aa24e0 100644 --- a/crates/agent_ui/src/acp/thread_view.rs +++ b/crates/agent_ui/src/acp/thread_view.rs @@ -34,7 +34,7 @@ use language::Buffer; use language_model::LanguageModelRegistry; use markdown::{HeadingLevelStyles, Markdown, MarkdownElement, MarkdownStyle}; -use project::{Project, ProjectEntryId}; +use project::{AgentServerStore, ExternalAgentServerName, Project, ProjectEntryId}; use prompt_store::{PromptId, PromptStore}; use rope::Point; use settings::{NotifyWhenAgentWaiting, Settings as _, SettingsStore}; @@ -253,13 +253,14 @@ impl ThreadFeedbackState { editor }); - editor.read(cx).focus_handle(cx).focus(window); + editor.read(cx).focus_handle(cx).focus(window, cx); editor } } pub struct AcpThreadView { agent: Rc, + agent_server_store: Entity, workspace: WeakEntity, project: Entity, thread_state: ThreadState, @@ -337,7 +338,13 @@ impl AcpThreadView { let prompt_capabilities = Rc::new(RefCell::new(acp::PromptCapabilities::default())); let available_commands = Rc::new(RefCell::new(vec![])); - let placeholder = placeholder_text(agent.name().as_ref(), false); + let agent_server_store = project.read(cx).agent_server_store().clone(); + let agent_display_name = agent_server_store + .read(cx) + .agent_display_name(&ExternalAgentServerName(agent.name())) + .unwrap_or_else(|| agent.name()); + + let placeholder = placeholder_text(agent_display_name.as_ref(), false); let message_editor = cx.new(|cx| { let mut editor = MessageEditor::new( @@ -376,7 +383,6 @@ impl AcpThreadView { ) }); - let agent_server_store = project.read(cx).agent_server_store().clone(); let subscriptions = [ cx.observe_global_in::(window, Self::agent_ui_font_size_changed), cx.observe_global_in::(window, Self::agent_ui_font_size_changed), @@ -406,6 +412,7 @@ impl AcpThreadView { Self { agent: agent.clone(), + agent_server_store, workspace: workspace.clone(), project: project.clone(), entry_view_state, @@ -682,7 +689,7 @@ impl AcpThreadView { }) }); - this.message_editor.focus_handle(cx).focus(window); + this.message_editor.focus_handle(cx).focus(window, cx); cx.notify(); } @@ -737,7 +744,7 @@ impl AcpThreadView { cx: &mut App, ) { let agent_name = agent.name(); - let (configuration_view, subscription) = if let Some(provider_id) = err.provider_id { + let (configuration_view, subscription) = if let Some(provider_id) = &err.provider_id { let registry = LanguageModelRegistry::global(cx); let sub = window.subscribe(®istry, cx, { @@ -779,12 +786,11 @@ impl AcpThreadView { configuration_view, description: err .description - .clone() .map(|desc| cx.new(|cx| Markdown::new(desc.into(), None, None, cx))), _subscription: subscription, }; if this.message_editor.focus_handle(cx).is_focused(window) { - this.focus_handle.focus(window) + this.focus_handle.focus(window, cx) } cx.notify(); }) @@ -804,7 +810,7 @@ impl AcpThreadView { ThreadState::LoadError(LoadError::Other(format!("{:#}", err).into())) } if self.message_editor.focus_handle(cx).is_focused(window) { - self.focus_handle.focus(window) + self.focus_handle.focus(window, cx) } cx.notify(); } @@ -1088,10 +1094,7 @@ impl AcpThreadView { window.defer(cx, |window, cx| { Self::handle_auth_required( this, - AuthRequired { - description: None, - provider_id: None, - }, + AuthRequired::new(), agent, connection, window, @@ -1270,7 +1273,7 @@ impl AcpThreadView { } }) }; - self.focus_handle(cx).focus(window); + self.focus_handle(cx).focus(window, cx); cx.notify(); } @@ -1322,7 +1325,7 @@ impl AcpThreadView { .await?; this.update_in(cx, |this, window, cx| { this.send_impl(message_editor, window, cx); - this.focus_handle(cx).focus(window); + this.focus_handle(cx).focus(window, cx); })?; anyhow::Ok(()) }) @@ -1465,7 +1468,7 @@ impl AcpThreadView { self.thread_retry_status.take(); self.thread_state = ThreadState::LoadError(error.clone()); if self.message_editor.focus_handle(cx).is_focused(window) { - self.focus_handle.focus(window) + self.focus_handle.focus(window, cx) } } AcpThreadEvent::TitleUpdated => { @@ -1500,7 +1503,13 @@ impl AcpThreadView { let has_commands = !available_commands.is_empty(); self.available_commands.replace(available_commands); - let new_placeholder = placeholder_text(self.agent.name().as_ref(), has_commands); + let agent_display_name = self + .agent_server_store + .read(cx) + .agent_display_name(&ExternalAgentServerName(self.agent.name())) + .unwrap_or_else(|| self.agent.name()); + + let new_placeholder = placeholder_text(agent_display_name.as_ref(), has_commands); self.message_editor.update(cx, |editor, cx| { editor.set_placeholder_text(&new_placeholder, window, cx); @@ -1663,44 +1672,6 @@ impl AcpThreadView { }); return; } - } else if method.0.as_ref() == "anthropic-api-key" { - let registry = LanguageModelRegistry::global(cx); - let provider = registry - .read(cx) - .provider(&language_model::ANTHROPIC_PROVIDER_ID) - .unwrap(); - let this = cx.weak_entity(); - let agent = self.agent.clone(); - let connection = connection.clone(); - window.defer(cx, move |window, cx| { - if !provider.is_authenticated(cx) { - Self::handle_auth_required( - this, - AuthRequired { - description: Some("ANTHROPIC_API_KEY must be set".to_owned()), - provider_id: Some(language_model::ANTHROPIC_PROVIDER_ID), - }, - agent, - connection, - window, - cx, - ); - } else { - this.update(cx, |this, cx| { - this.thread_state = Self::initial_state( - agent, - None, - this.workspace.clone(), - this.project.clone(), - true, - window, - cx, - ) - }) - .ok(); - } - }); - return; } else if method.0.as_ref() == "vertex-ai" && std::env::var("GOOGLE_API_KEY").is_err() && (std::env::var("GOOGLE_CLOUD_PROJECT").is_err() @@ -1898,6 +1869,17 @@ impl AcpThreadView { }) } + pub fn has_user_submitted_prompt(&self, cx: &App) -> bool { + self.thread().is_some_and(|thread| { + thread.read(cx).entries().iter().any(|entry| { + matches!( + entry, + AgentThreadEntry::UserMessage(user_message) if user_message.id.is_some() + ) + }) + }) + } + fn authorize_tool_call( &mut self, tool_call_id: acp::ToolCallId, @@ -2142,6 +2124,7 @@ impl AcpThreadView { chunks, indented: _, }) => { + let mut is_blank = true; let is_last = entry_ix + 1 == total_entries; let style = default_markdown_style(false, false, window, cx); @@ -2151,36 +2134,55 @@ impl AcpThreadView { .children(chunks.iter().enumerate().filter_map( |(chunk_ix, chunk)| match chunk { AssistantMessageChunk::Message { block } => { - block.markdown().map(|md| { - self.render_markdown(md.clone(), style.clone()) - .into_any_element() + block.markdown().and_then(|md| { + let this_is_blank = md.read(cx).source().trim().is_empty(); + is_blank = is_blank && this_is_blank; + if this_is_blank { + return None; + } + + Some( + self.render_markdown(md.clone(), style.clone()) + .into_any_element(), + ) }) } AssistantMessageChunk::Thought { block } => { - block.markdown().map(|md| { - self.render_thinking_block( - entry_ix, - chunk_ix, - md.clone(), - window, - cx, + block.markdown().and_then(|md| { + let this_is_blank = md.read(cx).source().trim().is_empty(); + is_blank = is_blank && this_is_blank; + if this_is_blank { + return None; + } + + Some( + self.render_thinking_block( + entry_ix, + chunk_ix, + md.clone(), + window, + cx, + ) + .into_any_element(), ) - .into_any_element() }) } }, )) .into_any(); - v_flex() - .px_5() - .py_1p5() - .when(is_first_indented, |this| this.pt_0p5()) - .when(is_last, |this| this.pb_4()) - .w_full() - .text_ui(cx) - .child(message_body) - .into_any() + if is_blank { + Empty.into_any() + } else { + v_flex() + .px_5() + .py_1p5() + .when(is_last, |this| this.pb_4()) + .w_full() + .text_ui(cx) + .child(message_body) + .into_any() + } } AgentThreadEntry::ToolCall(tool_call) => { let has_terminals = tool_call.terminals().next().is_some(); @@ -2212,7 +2214,7 @@ impl AcpThreadView { div() .relative() .w_full() - .pl(rems_from_px(20.0)) + .pl_5() .bg(cx.theme().colors().panel_background.opacity(0.2)) .child( div() @@ -2429,6 +2431,12 @@ impl AcpThreadView { let is_collapsible = !tool_call.content.is_empty() && !needs_confirmation; let is_open = needs_confirmation || self.expanded_tool_calls.contains(&tool_call.id); + let input_output_header = |label: SharedString| { + Label::new(label) + .size(LabelSize::XSmall) + .color(Color::Muted) + .buffer_font(cx) + }; let tool_output_display = if is_open { @@ -2470,7 +2478,25 @@ impl AcpThreadView { | ToolCallStatus::Completed | ToolCallStatus::Failed | ToolCallStatus::Canceled => v_flex() - .w_full() + .when(!is_edit && !is_terminal_tool, |this| { + this.mt_1p5().w_full().child( + v_flex() + .ml(rems(0.4)) + .px_3p5() + .pb_1() + .gap_1() + .border_l_1() + .border_color(self.tool_card_border_color(cx)) + .child(input_output_header("Raw Input:".into())) + .children(tool_call.raw_input_markdown.clone().map(|input| { + self.render_markdown( + input, + default_markdown_style(false, false, window, cx), + ) + })) + .child(input_output_header("Output:".into())), + ) + }) .children(tool_call.content.iter().enumerate().map( |(content_ix, content)| { div().child(self.render_tool_call_content( @@ -2569,7 +2595,7 @@ impl AcpThreadView { .gap_px() .when(is_collapsible, |this| { this.child( - Disclosure::new(("expand", entry_ix), is_open) + Disclosure::new(("expand-output", entry_ix), is_open) .opened_icon(IconName::ChevronUp) .closed_icon(IconName::ChevronDown) .visible_on_hover(&card_header_id) @@ -2692,7 +2718,7 @@ impl AcpThreadView { ..default_markdown_style(false, true, window, cx) }, )) - .tooltip(Tooltip::text("Jump to File")) + .tooltip(Tooltip::text("Go to File")) .on_click(cx.listener(move |this, _, window, cx| { this.open_tool_call_location(entry_ix, 0, window, cx); })) @@ -2755,20 +2781,20 @@ impl AcpThreadView { let button_id = SharedString::from(format!("tool_output-{:?}", tool_call_id)); v_flex() - .mt_1p5() .gap_2() - .when(!card_layout, |this| { - this.ml(rems(0.4)) - .px_3p5() - .border_l_1() - .border_color(self.tool_card_border_color(cx)) - }) - .when(card_layout, |this| { - this.px_2().pb_2().when(context_ix > 0, |this| { - this.border_t_1() - .pt_2() + .map(|this| { + if card_layout { + this.when(context_ix > 0, |this| { + this.pt_2() + .border_t_1() + .border_color(self.tool_card_border_color(cx)) + }) + } else { + this.ml(rems(0.4)) + .px_3p5() + .border_l_1() .border_color(self.tool_card_border_color(cx)) - }) + } }) .text_xs() .text_color(cx.theme().colors().text_muted) @@ -3489,138 +3515,119 @@ impl AcpThreadView { pending_auth_method: Option<&acp::AuthMethodId>, window: &mut Window, cx: &Context, - ) -> Div { - let show_description = - configuration_view.is_none() && description.is_none() && pending_auth_method.is_none(); - + ) -> impl IntoElement { let auth_methods = connection.auth_methods(); - v_flex().flex_1().size_full().justify_end().child( - v_flex() - .p_2() - .pr_3() - .w_full() - .gap_1() - .border_t_1() - .border_color(cx.theme().colors().border) - .bg(cx.theme().status().warning.opacity(0.04)) - .child( - h_flex() - .gap_1p5() - .child( - Icon::new(IconName::Warning) - .color(Color::Warning) - .size(IconSize::Small), - ) - .child(Label::new("Authentication Required").size(LabelSize::Small)), - ) - .children(description.map(|desc| { - div().text_ui(cx).child(self.render_markdown( - desc.clone(), - default_markdown_style(false, false, window, cx), - )) - })) - .children( - configuration_view - .cloned() - .map(|view| div().w_full().child(view)), - ) - .when(show_description, |el| { - el.child( - Label::new(format!( - "You are not currently authenticated with {}.{}", - self.agent.name(), - if auth_methods.len() > 1 { - " Please choose one of the following options:" - } else { - "" - } - )) - .size(LabelSize::Small) - .color(Color::Muted) - .mb_1() - .ml_5(), - ) - }) - .when_some(pending_auth_method, |el, _| { - el.child( - h_flex() - .py_4() - .w_full() - .justify_center() - .gap_1() - .child( - Icon::new(IconName::ArrowCircle) - .size(IconSize::Small) - .color(Color::Muted) - .with_rotate_animation(2), - ) - .child(Label::new("Authenticating…").size(LabelSize::Small)), - ) - }) - .when(!auth_methods.is_empty(), |this| { - this.child( - h_flex() - .justify_end() - .flex_wrap() - .gap_1() - .when(!show_description, |this| { - this.border_t_1() - .mt_1() - .pt_2() - .border_color(cx.theme().colors().border.opacity(0.8)) + let agent_display_name = self + .agent_server_store + .read(cx) + .agent_display_name(&ExternalAgentServerName(self.agent.name())) + .unwrap_or_else(|| self.agent.name()); + + let show_fallback_description = auth_methods.len() > 1 + && configuration_view.is_none() + && description.is_none() + && pending_auth_method.is_none(); + + let auth_buttons = || { + h_flex().justify_end().flex_wrap().gap_1().children( + connection + .auth_methods() + .iter() + .enumerate() + .rev() + .map(|(ix, method)| { + let (method_id, name) = if self.project.read(cx).is_via_remote_server() + && method.id.0.as_ref() == "oauth-personal" + && method.name == "Log in with Google" + { + ("spawn-gemini-cli".into(), "Log in with Gemini CLI".into()) + } else { + (method.id.0.clone(), method.name.clone()) + }; + + let agent_telemetry_id = connection.telemetry_id(); + + Button::new(method_id.clone(), name) + .label_size(LabelSize::Small) + .map(|this| { + if ix == 0 { + this.style(ButtonStyle::Tinted(TintColor::Accent)) + } else { + this.style(ButtonStyle::Outlined) + } }) - .children(connection.auth_methods().iter().enumerate().rev().map( - |(ix, method)| { - let (method_id, name) = if self - .project - .read(cx) - .is_via_remote_server() - && method.id.0.as_ref() == "oauth-personal" - && method.name == "Log in with Google" - { - ("spawn-gemini-cli".into(), "Log in with Gemini CLI".into()) - } else { - (method.id.0.clone(), method.name.clone()) - }; + .when_some(method.description.clone(), |this, description| { + this.tooltip(Tooltip::text(description)) + }) + .on_click({ + cx.listener(move |this, _, window, cx| { + telemetry::event!( + "Authenticate Agent Started", + agent = agent_telemetry_id, + method = method_id + ); - let agent_telemetry_id = connection.telemetry_id(); + this.authenticate( + acp::AuthMethodId::new(method_id.clone()), + window, + cx, + ) + }) + }) + }), + ) + }; - Button::new(method_id.clone(), name) - .label_size(LabelSize::Small) - .map(|this| { - if ix == 0 { - this.style(ButtonStyle::Tinted(TintColor::Warning)) - } else { - this.style(ButtonStyle::Outlined) - } - }) - .when_some( - method.description.clone(), - |this, description| { - this.tooltip(Tooltip::text(description)) - }, - ) - .on_click({ - cx.listener(move |this, _, window, cx| { - telemetry::event!( - "Authenticate Agent Started", - agent = agent_telemetry_id, - method = method_id - ); - - this.authenticate( - acp::AuthMethodId::new(method_id.clone()), - window, - cx, - ) - }) - }) - }, - )), - ) - }), - ) + if pending_auth_method.is_some() { + return Callout::new() + .icon(IconName::Info) + .title(format!("Authenticating to {}…", agent_display_name)) + .actions_slot( + Icon::new(IconName::ArrowCircle) + .size(IconSize::Small) + .color(Color::Muted) + .with_rotate_animation(2) + .into_any_element(), + ) + .into_any_element(); + } + + Callout::new() + .icon(IconName::Info) + .title(format!("Authenticate to {}", agent_display_name)) + .when(auth_methods.len() == 1, |this| { + this.actions_slot(auth_buttons()) + }) + .description_slot( + v_flex() + .text_ui(cx) + .map(|this| { + if show_fallback_description { + this.child( + Label::new("Choose one of the following authentication options:") + .size(LabelSize::Small) + .color(Color::Muted), + ) + } else { + this.children( + configuration_view + .cloned() + .map(|view| div().w_full().child(view)), + ) + .children(description.map(|desc| { + self.render_markdown( + desc.clone(), + default_markdown_style(false, false, window, cx), + ) + })) + } + }) + .when(auth_methods.len() > 1, |this| { + this.gap_1().child(auth_buttons()) + }), + ) + .into_any_element() } fn render_load_error( @@ -4110,6 +4117,8 @@ impl AcpThreadView { .ml_1p5() }); + let full_path = path.display(path_style).to_string(); + let file_icon = FileIcons::get_icon(path.as_std_path(), cx) .map(Icon::from_path) .map(|icon| icon.color(Color::Muted).size(IconSize::Small)) @@ -4143,7 +4152,6 @@ impl AcpThreadView { .relative() .pr_8() .w_full() - .overflow_x_scroll() .child( h_flex() .id(("file-name-path", index)) @@ -4155,7 +4163,14 @@ impl AcpThreadView { .child(file_icon) .children(file_name) .children(file_path) - .tooltip(Tooltip::text("Go to File")) + .tooltip(move |_, cx| { + Tooltip::with_meta( + "Go to File", + None, + full_path.clone(), + cx, + ) + }) .on_click({ let buffer = buffer.clone(); cx.listener(move |this, _, window, cx| { @@ -5861,10 +5876,6 @@ impl AcpThreadView { }; let connection = thread.read(cx).connection().clone(); - let err = AuthRequired { - description: None, - provider_id: None, - }; this.clear_thread_error(cx); if let Some(message) = this.in_flight_prompt.take() { this.message_editor.update(cx, |editor, cx| { @@ -5873,7 +5884,14 @@ impl AcpThreadView { } let this = cx.weak_entity(); window.defer(cx, |window, cx| { - Self::handle_auth_required(this, err, agent, connection, window, cx); + Self::handle_auth_required( + this, + AuthRequired::new(), + agent, + connection, + window, + cx, + ); }) } })) @@ -5886,14 +5904,10 @@ impl AcpThreadView { }; let connection = thread.read(cx).connection().clone(); - let err = AuthRequired { - description: None, - provider_id: None, - }; self.clear_thread_error(cx); let this = cx.weak_entity(); window.defer(cx, |window, cx| { - Self::handle_auth_required(this, err, agent, connection, window, cx); + Self::handle_auth_required(this, AuthRequired::new(), agent, connection, window, cx); }) } @@ -5996,16 +6010,19 @@ impl Render for AcpThreadView { configuration_view, pending_auth_method, .. - } => self - .render_auth_required_state( + } => v_flex() + .flex_1() + .size_full() + .justify_end() + .child(self.render_auth_required_state( connection, description.as_ref(), configuration_view.as_ref(), pending_auth_method.as_ref(), window, cx, - ) - .into_any(), + )) + .into_any_element(), ThreadState::Loading { .. } => v_flex() .flex_1() .child(self.render_recent_history(cx)) diff --git a/crates/agent_ui/src/agent_configuration.rs b/crates/agent_ui/src/agent_configuration.rs index 24f019c605d1b167e62a6e68dfc1f3ed07c73f1c..562976453d963db65f9033536e528000de2b510f 100644 --- a/crates/agent_ui/src/agent_configuration.rs +++ b/crates/agent_ui/src/agent_configuration.rs @@ -22,7 +22,8 @@ use gpui::{ }; use language::LanguageRegistry; use language_model::{ - LanguageModelProvider, LanguageModelProviderId, LanguageModelRegistry, ZED_CLOUD_PROVIDER_ID, + IconOrSvg, LanguageModelProvider, LanguageModelProviderId, LanguageModelRegistry, + ZED_CLOUD_PROVIDER_ID, }; use language_models::AllLanguageModelSettings; use notifications::status_toast::{StatusToast, ToastIcon}; @@ -117,7 +118,7 @@ impl AgentConfiguration { } fn build_provider_configuration_views(&mut self, window: &mut Window, cx: &mut Context) { - let providers = LanguageModelRegistry::read_global(cx).providers(); + let providers = LanguageModelRegistry::read_global(cx).visible_providers(); for provider in providers { self.add_provider_configuration_view(&provider, window, cx); } @@ -261,9 +262,12 @@ impl AgentConfiguration { .w_full() .gap_1p5() .child( - Icon::new(provider.icon()) - .size(IconSize::Small) - .color(Color::Muted), + match provider.icon() { + IconOrSvg::Svg(path) => Icon::from_external_svg(path), + IconOrSvg::Icon(name) => Icon::new(name), + } + .size(IconSize::Small) + .color(Color::Muted), ) .child( h_flex() @@ -416,7 +420,7 @@ impl AgentConfiguration { &mut self, cx: &mut Context, ) -> impl IntoElement { - let providers = LanguageModelRegistry::read_global(cx).providers(); + let providers = LanguageModelRegistry::read_global(cx).visible_providers(); let popover_menu = PopoverMenu::new("add-provider-popover") .trigger( diff --git a/crates/agent_ui/src/agent_configuration/add_llm_provider_modal.rs b/crates/agent_ui/src/agent_configuration/add_llm_provider_modal.rs index 02269511bb9a4d9b95fe27b66e3ca0a9e5c498c5..e443df33b4ddcaeba32b9b2623c0fdca85fac51c 100644 --- a/crates/agent_ui/src/agent_configuration/add_llm_provider_modal.rs +++ b/crates/agent_ui/src/agent_configuration/add_llm_provider_modal.rs @@ -446,17 +446,17 @@ impl AddLlmProviderModal { }) } - fn on_tab(&mut self, _: &menu::SelectNext, window: &mut Window, _: &mut Context) { - window.focus_next(); + fn on_tab(&mut self, _: &menu::SelectNext, window: &mut Window, cx: &mut Context) { + window.focus_next(cx); } fn on_tab_prev( &mut self, _: &menu::SelectPrevious, window: &mut Window, - _: &mut Context, + cx: &mut Context, ) { - window.focus_prev(); + window.focus_prev(cx); } } @@ -493,7 +493,7 @@ impl Render for AddLlmProviderModal { .on_action(cx.listener(Self::on_tab)) .on_action(cx.listener(Self::on_tab_prev)) .capture_any_mouse_down(cx.listener(|this, _, window, cx| { - this.focus_handle(cx).focus(window); + this.focus_handle(cx).focus(window, cx); })) .child( Modal::new("configure-context-server", None) diff --git a/crates/agent_ui/src/agent_configuration/configure_context_server_modal.rs b/crates/agent_ui/src/agent_configuration/configure_context_server_modal.rs index a0f0be886a1bf5e1485a2d36440b9f91648ef0c6..b30f1494f0d4dcbf3ef63cc7f549d16374f4899b 100644 --- a/crates/agent_ui/src/agent_configuration/configure_context_server_modal.rs +++ b/crates/agent_ui/src/agent_configuration/configure_context_server_modal.rs @@ -831,7 +831,7 @@ impl Render for ConfigureContextServerModal { }), ) .capture_any_mouse_down(cx.listener(|this, _, window, cx| { - this.focus_handle(cx).focus(window); + this.focus_handle(cx).focus(window, cx); })) .child( Modal::new("configure-context-server", None) diff --git a/crates/agent_ui/src/agent_configuration/manage_profiles_modal.rs b/crates/agent_ui/src/agent_configuration/manage_profiles_modal.rs index 127852fd50e81cf56ae37a7af430f88ae2accf99..c7f395ebbd813cfd7c28f33a7e69ec32f6d90fca 100644 --- a/crates/agent_ui/src/agent_configuration/manage_profiles_modal.rs +++ b/crates/agent_ui/src/agent_configuration/manage_profiles_modal.rs @@ -156,7 +156,7 @@ impl ManageProfilesModal { cx.observe_global_in::(window, |this, window, cx| { if matches!(this.mode, Mode::ChooseProfile(_)) { this.mode = Mode::choose_profile(window, cx); - this.focus_handle(cx).focus(window); + this.focus_handle(cx).focus(window, cx); cx.notify(); } }); @@ -173,7 +173,7 @@ impl ManageProfilesModal { fn choose_profile(&mut self, window: &mut Window, cx: &mut Context) { self.mode = Mode::choose_profile(window, cx); - self.focus_handle(cx).focus(window); + self.focus_handle(cx).focus(window, cx); } fn new_profile( @@ -191,7 +191,7 @@ impl ManageProfilesModal { name_editor, base_profile_id, }); - self.focus_handle(cx).focus(window); + self.focus_handle(cx).focus(window, cx); } pub fn view_profile( @@ -209,7 +209,7 @@ impl ManageProfilesModal { delete_profile: NavigableEntry::focusable(cx), cancel_item: NavigableEntry::focusable(cx), }); - self.focus_handle(cx).focus(window); + self.focus_handle(cx).focus(window, cx); } fn configure_default_model( @@ -300,7 +300,7 @@ impl ManageProfilesModal { model_picker, _subscription: dismiss_subscription, }; - self.focus_handle(cx).focus(window); + self.focus_handle(cx).focus(window, cx); } fn configure_mcp_tools( @@ -336,7 +336,7 @@ impl ManageProfilesModal { tool_picker, _subscription: dismiss_subscription, }; - self.focus_handle(cx).focus(window); + self.focus_handle(cx).focus(window, cx); } fn configure_builtin_tools( @@ -377,7 +377,7 @@ impl ManageProfilesModal { tool_picker, _subscription: dismiss_subscription, }; - self.focus_handle(cx).focus(window); + self.focus_handle(cx).focus(window, cx); } fn confirm(&mut self, window: &mut Window, cx: &mut Context) { @@ -951,7 +951,7 @@ impl Render for ManageProfilesModal { .on_action(cx.listener(|this, _: &menu::Cancel, window, cx| this.cancel(window, cx))) .on_action(cx.listener(|this, _: &menu::Confirm, window, cx| this.confirm(window, cx))) .capture_any_mouse_down(cx.listener(|this, _, window, cx| { - this.focus_handle(cx).focus(window); + this.focus_handle(cx).focus(window, cx); })) .on_mouse_down_out(cx.listener(|_this, _, _, cx| cx.emit(DismissEvent))) .child(match &self.mode { diff --git a/crates/agent_ui/src/agent_diff.rs b/crates/agent_ui/src/agent_diff.rs index 06fce64819d3ce66b9e39f2b83cbebefb6ba9698..91d345b7ebb9dae5225626d7a054d0de1882dfe0 100644 --- a/crates/agent_ui/src/agent_diff.rs +++ b/crates/agent_ui/src/agent_diff.rs @@ -212,10 +212,10 @@ impl AgentDiffPane { .focus_handle(cx) .contains_focused(window, cx) { - self.focus_handle.focus(window); + self.focus_handle.focus(window, cx); } else if self.focus_handle.is_focused(window) && !self.multibuffer.read(cx).is_empty() { self.editor.update(cx, |editor, cx| { - editor.focus_handle(cx).focus(window); + editor.focus_handle(cx).focus(window, cx); }); } } @@ -874,12 +874,12 @@ impl AgentDiffToolbar { match active_item { AgentDiffToolbarItem::Pane(agent_diff) => { if let Some(agent_diff) = agent_diff.upgrade() { - agent_diff.focus_handle(cx).focus(window); + agent_diff.focus_handle(cx).focus(window, cx); } } AgentDiffToolbarItem::Editor { editor, .. } => { if let Some(editor) = editor.upgrade() { - editor.read(cx).focus_handle(cx).focus(window); + editor.read(cx).focus_handle(cx).focus(window, cx); } } } diff --git a/crates/agent_ui/src/agent_model_selector.rs b/crates/agent_ui/src/agent_model_selector.rs index ac57ed575d9d1b6de2c53d3e0e4a91b4bd16ab1a..45cefbf2b9f8d4b1639a9849f2ee2e4468e530b1 100644 --- a/crates/agent_ui/src/agent_model_selector.rs +++ b/crates/agent_ui/src/agent_model_selector.rs @@ -4,6 +4,7 @@ use crate::{ }; use fs::Fs; use gpui::{Entity, FocusHandle, SharedString}; +use language_model::IconOrSvg; use picker::popover_menu::PickerPopoverMenu; use settings::update_settings_file; use std::sync::Arc; @@ -103,7 +104,14 @@ impl Render for AgentModelSelector { self.selector.clone(), ButtonLike::new("active-model") .when_some(provider_icon, |this, icon| { - this.child(Icon::new(icon).color(color).size(IconSize::XSmall)) + this.child( + match icon { + IconOrSvg::Svg(path) => Icon::from_external_svg(path), + IconOrSvg::Icon(name) => Icon::new(name), + } + .color(color) + .size(IconSize::XSmall), + ) }) .selected_style(ButtonStyle::Tinted(TintColor::Accent)) .child( @@ -115,7 +123,7 @@ impl Render for AgentModelSelector { .child( Icon::new(IconName::ChevronDown) .color(color) - .size(IconSize::Small), + .size(IconSize::XSmall), ), move |_window, cx| { Tooltip::for_action_in("Change Model", &ToggleModelSelector, &focus_handle, cx) diff --git a/crates/agent_ui/src/agent_panel.rs b/crates/agent_ui/src/agent_panel.rs index ff8cf8db969e9ef2d1d86b306c0f38fb66a67fde..a050f75120cd73949251c09c8424314e3616c705 100644 --- a/crates/agent_ui/src/agent_panel.rs +++ b/crates/agent_ui/src/agent_panel.rs @@ -7,7 +7,6 @@ use db::kvp::{Dismissable, KEY_VALUE_STORE}; use project::{ ExternalAgentServerName, agent_server_store::{CLAUDE_CODE_NAME, CODEX_NAME, GEMINI_NAME}, - trusted_worktrees::{RemoteHostLocation, TrustedWorktrees, wait_for_workspace_trust}, }; use serde::{Deserialize, Serialize}; use settings::{ @@ -264,17 +263,6 @@ impl AgentType { Self::Custom { .. } => Some(IconName::Sparkle), } } - - fn is_mcp(&self) -> bool { - match self { - Self::NativeAgent => false, - Self::TextThread => false, - Self::Custom { .. } => false, - Self::Gemini => true, - Self::ClaudeCode => true, - Self::Codex => true, - } - } } impl From for AgentType { @@ -455,9 +443,7 @@ pub struct AgentPanel { pending_serialization: Option>>, onboarding: Entity, selected_agent: AgentType, - new_agent_thread_task: Task<()>, show_trust_workspace_message: bool, - _worktree_trust_subscription: Option, } impl AgentPanel { @@ -681,48 +667,6 @@ impl AgentPanel { None }; - let mut show_trust_workspace_message = false; - let worktree_trust_subscription = - TrustedWorktrees::try_get_global(cx).and_then(|trusted_worktrees| { - let has_global_trust = trusted_worktrees.update(cx, |trusted_worktrees, cx| { - trusted_worktrees.can_trust_workspace( - project - .read(cx) - .remote_connection_options(cx) - .map(RemoteHostLocation::from), - cx, - ) - }); - if has_global_trust { - None - } else { - show_trust_workspace_message = true; - let project = project.clone(); - Some(cx.subscribe( - &trusted_worktrees, - move |agent_panel, trusted_worktrees, _, cx| { - let new_show_trust_workspace_message = - !trusted_worktrees.update(cx, |trusted_worktrees, cx| { - trusted_worktrees.can_trust_workspace( - project - .read(cx) - .remote_connection_options(cx) - .map(RemoteHostLocation::from), - cx, - ) - }); - if new_show_trust_workspace_message - != agent_panel.show_trust_workspace_message - { - agent_panel.show_trust_workspace_message = - new_show_trust_workspace_message; - cx.notify(); - }; - }, - )) - } - }); - let mut panel = Self { active_view, workspace, @@ -745,14 +689,12 @@ impl AgentPanel { height: None, zoomed: false, pending_serialization: None, - new_agent_thread_task: Task::ready(()), onboarding, acp_history, history_store, selected_agent: AgentType::default(), loading: false, - show_trust_workspace_message, - _worktree_trust_subscription: worktree_trust_subscription, + show_trust_workspace_message: false, }; // Initial sync of agent servers from extensions @@ -880,7 +822,7 @@ impl AgentPanel { window, cx, ); - text_thread_editor.focus_handle(cx).focus(window); + text_thread_editor.focus_handle(cx).focus(window, cx); } fn external_thread( @@ -945,47 +887,6 @@ impl AgentPanel { } }; - if ext_agent.is_mcp() { - let wait_task = this.update(cx, |agent_panel, cx| { - agent_panel.project.update(cx, |project, cx| { - wait_for_workspace_trust( - project.remote_connection_options(cx), - "context servers", - cx, - ) - }) - })?; - if let Some(wait_task) = wait_task { - this.update_in(cx, |agent_panel, window, cx| { - agent_panel.show_trust_workspace_message = true; - cx.notify(); - agent_panel.new_agent_thread_task = - cx.spawn_in(window, async move |agent_panel, cx| { - wait_task.await; - let server = ext_agent.server(fs, history); - agent_panel - .update_in(cx, |agent_panel, window, cx| { - agent_panel.show_trust_workspace_message = false; - cx.notify(); - agent_panel._external_thread( - server, - resume_thread, - summarize_thread, - workspace, - project, - loading, - ext_agent, - window, - cx, - ); - }) - .ok(); - }); - })?; - return Ok(()); - } - } - let server = ext_agent.server(fs, history); this.update_in(cx, |agent_panel, window, cx| { agent_panel._external_thread( @@ -1034,7 +935,7 @@ impl AgentPanel { if let Some(thread_view) = self.active_thread_view() { thread_view.update(cx, |view, cx| { view.expand_message_editor(&ExpandMessageEditor, window, cx); - view.focus_handle(cx).focus(window); + view.focus_handle(cx).focus(window, cx); }); } } @@ -1115,12 +1016,12 @@ impl AgentPanel { match &self.active_view { ActiveView::ExternalAgentThread { thread_view } => { - thread_view.focus_handle(cx).focus(window); + thread_view.focus_handle(cx).focus(window, cx); } ActiveView::TextThread { text_thread_editor, .. } => { - text_thread_editor.focus_handle(cx).focus(window); + text_thread_editor.focus_handle(cx).focus(window, cx); } ActiveView::History | ActiveView::Configuration => {} } @@ -1268,7 +1169,7 @@ impl AgentPanel { Self::handle_agent_configuration_event, )); - configuration.focus_handle(cx).focus(window); + configuration.focus_handle(cx).focus(window, cx); } } @@ -1404,7 +1305,7 @@ impl AgentPanel { } if focus { - self.focus_handle(cx).focus(window); + self.focus_handle(cx).focus(window, cx); } } @@ -1510,36 +1411,6 @@ impl AgentPanel { window: &mut Window, cx: &mut Context, ) { - let wait_task = if agent.is_mcp() { - self.project.update(cx, |project, cx| { - wait_for_workspace_trust( - project.remote_connection_options(cx), - "context servers", - cx, - ) - }) - } else { - None - }; - if let Some(wait_task) = wait_task { - self.show_trust_workspace_message = true; - cx.notify(); - self.new_agent_thread_task = cx.spawn_in(window, async move |agent_panel, cx| { - wait_task.await; - agent_panel - .update_in(cx, |agent_panel, window, cx| { - agent_panel.show_trust_workspace_message = false; - cx.notify(); - agent_panel._new_agent_thread(agent, window, cx); - }) - .ok(); - }); - } else { - self._new_agent_thread(agent, window, cx); - } - } - - fn _new_agent_thread(&mut self, agent: AgentType, window: &mut Window, cx: &mut Context) { match agent { AgentType::TextThread => { window.dispatch_action(NewTextThread.boxed_clone(), cx); @@ -1749,14 +1620,19 @@ impl AgentPanel { let content = match &self.active_view { ActiveView::ExternalAgentThread { thread_view } => { + let is_generating_title = thread_view + .read(cx) + .as_native_thread(cx) + .map_or(false, |t| t.read(cx).is_generating_title()); + if let Some(title_editor) = thread_view.read(cx).title_editor() { - div() + let container = div() .w_full() .on_action({ let thread_view = thread_view.downgrade(); move |_: &menu::Confirm, window, cx| { if let Some(thread_view) = thread_view.upgrade() { - thread_view.focus_handle(cx).focus(window); + thread_view.focus_handle(cx).focus(window, cx); } } }) @@ -1764,12 +1640,25 @@ impl AgentPanel { let thread_view = thread_view.downgrade(); move |_: &editor::actions::Cancel, window, cx| { if let Some(thread_view) = thread_view.upgrade() { - thread_view.focus_handle(cx).focus(window); + thread_view.focus_handle(cx).focus(window, cx); } } }) - .child(title_editor) - .into_any_element() + .child(title_editor); + + if is_generating_title { + container + .with_animation( + "generating_title", + Animation::new(Duration::from_secs(2)) + .repeat() + .with_easing(pulsating_between(0.4, 0.8)), + |div, delta| div.opacity(delta), + ) + .into_any_element() + } else { + container.into_any_element() + } } else { Label::new(thread_view.read(cx).title(cx)) .color(Color::Muted) @@ -1799,6 +1688,13 @@ impl AgentPanel { Label::new(LOADING_SUMMARY_PLACEHOLDER) .truncate() .color(Color::Muted) + .with_animation( + "generating_title", + Animation::new(Duration::from_secs(2)) + .repeat() + .with_easing(pulsating_between(0.4, 0.8)), + |label, delta| label.alpha(delta), + ) .into_any_element() } } @@ -1842,6 +1738,25 @@ impl AgentPanel { .into_any() } + fn handle_regenerate_thread_title(thread_view: Entity, cx: &mut App) { + thread_view.update(cx, |thread_view, cx| { + if let Some(thread) = thread_view.as_native_thread(cx) { + thread.update(cx, |thread, cx| { + thread.generate_title(cx); + }); + } + }); + } + + fn handle_regenerate_text_thread_title( + text_thread_editor: Entity, + cx: &mut App, + ) { + text_thread_editor.update(cx, |text_thread_editor, cx| { + text_thread_editor.regenerate_summary(cx); + }); + } + fn render_panel_options_menu( &self, window: &mut Window, @@ -1861,6 +1776,35 @@ impl AgentPanel { let selected_agent = self.selected_agent.clone(); + let text_thread_view = match &self.active_view { + ActiveView::TextThread { + text_thread_editor, .. + } => Some(text_thread_editor.clone()), + _ => None, + }; + let text_thread_with_messages = match &self.active_view { + ActiveView::TextThread { + text_thread_editor, .. + } => text_thread_editor + .read(cx) + .text_thread() + .read(cx) + .messages(cx) + .any(|message| message.role == language_model::Role::Assistant), + _ => false, + }; + + let thread_view = match &self.active_view { + ActiveView::ExternalAgentThread { thread_view } => Some(thread_view.clone()), + _ => None, + }; + let thread_with_messages = match &self.active_view { + ActiveView::ExternalAgentThread { thread_view } => { + thread_view.read(cx).has_user_submitted_prompt(cx) + } + _ => false, + }; + PopoverMenu::new("agent-options-menu") .trigger_with_tooltip( IconButton::new("agent-options-menu", IconName::Ellipsis) @@ -1883,6 +1827,7 @@ impl AgentPanel { move |window, cx| { Some(ContextMenu::build(window, cx, |mut menu, _window, _| { menu = menu.context(focus_handle.clone()); + if let Some(usage) = usage { menu = menu .header_with_link("Prompt Usage", "Manage", account_url.clone()) @@ -1920,6 +1865,38 @@ impl AgentPanel { .separator() } + if thread_with_messages | text_thread_with_messages { + menu = menu.header("Current Thread"); + + if let Some(text_thread_view) = text_thread_view.as_ref() { + menu = menu + .entry("Regenerate Thread Title", None, { + let text_thread_view = text_thread_view.clone(); + move |_, cx| { + Self::handle_regenerate_text_thread_title( + text_thread_view.clone(), + cx, + ); + } + }) + .separator(); + } + + if let Some(thread_view) = thread_view.as_ref() { + menu = menu + .entry("Regenerate Thread Title", None, { + let thread_view = thread_view.clone(); + move |_, cx| { + Self::handle_regenerate_thread_title( + thread_view.clone(), + cx, + ); + } + }) + .separator(); + } + } + menu = menu .header("MCP Servers") .action( @@ -2451,7 +2428,7 @@ impl AgentPanel { let history_is_empty = self.history_store.read(cx).is_empty(cx); let has_configured_non_zed_providers = LanguageModelRegistry::read_global(cx) - .providers() + .visible_providers() .iter() .any(|provider| { provider.is_authenticated(cx) diff --git a/crates/agent_ui/src/agent_ui.rs b/crates/agent_ui/src/agent_ui.rs index c80c7b43644ab949e748609435e33dfe9f31d54e..401b506b302d9c2a86a36ddce0fc72df075f4c18 100644 --- a/crates/agent_ui/src/agent_ui.rs +++ b/crates/agent_ui/src/agent_ui.rs @@ -174,16 +174,6 @@ impl ExternalAgent { Self::Custom { name } => Rc::new(agent_servers::CustomAgentServer::new(name.clone())), } } - - pub fn is_mcp(&self) -> bool { - match self { - Self::Gemini => true, - Self::ClaudeCode => true, - Self::Codex => true, - Self::NativeAgent => false, - Self::Custom { .. } => false, - } - } } /// Opens the profile management interface for configuring agent tools and settings. @@ -358,7 +348,8 @@ fn init_language_model_settings(cx: &mut App) { |_, event: &language_model::Event, cx| match event { language_model::Event::ProviderStateChanged(_) | language_model::Event::AddedProvider(_) - | language_model::Event::RemovedProvider(_) => { + | language_model::Event::RemovedProvider(_) + | language_model::Event::ProvidersChanged => { update_active_language_model_from_settings(cx); } _ => {} diff --git a/crates/agent_ui/src/buffer_codegen.rs b/crates/agent_ui/src/buffer_codegen.rs index 87ce6d386b38f31a0d7b550aab00bb766ce75010..a296d4d20918fba6eb32bfcf7fcc657f9db2b3ac 100644 --- a/crates/agent_ui/src/buffer_codegen.rs +++ b/crates/agent_ui/src/buffer_codegen.rs @@ -75,6 +75,9 @@ pub struct BufferCodegen { session_id: Uuid, } +pub const REWRITE_SECTION_TOOL_NAME: &str = "rewrite_section"; +pub const FAILURE_MESSAGE_TOOL_NAME: &str = "failure_message"; + impl BufferCodegen { pub fn new( buffer: Entity, @@ -522,12 +525,12 @@ impl CodegenAlternative { let tools = vec![ LanguageModelRequestTool { - name: "rewrite_section".to_string(), + name: REWRITE_SECTION_TOOL_NAME.to_string(), description: "Replaces text in tags with your replacement_text.".to_string(), input_schema: language_model::tool_schema::root_schema_for::(tool_input_format).to_value(), }, LanguageModelRequestTool { - name: "failure_message".to_string(), + name: FAILURE_MESSAGE_TOOL_NAME.to_string(), description: "Use this tool to provide a message to the user when you're unable to complete a task.".to_string(), input_schema: language_model::tool_schema::root_schema_for::(tool_input_format).to_value(), }, @@ -1167,7 +1170,7 @@ impl CodegenAlternative { let process_tool_use = move |tool_use: LanguageModelToolUse| -> Option { let mut chars_read_so_far = chars_read_so_far.lock(); match tool_use.name.as_ref() { - "rewrite_section" => { + REWRITE_SECTION_TOOL_NAME => { let Ok(input) = serde_json::from_value::(tool_use.input) else { @@ -1180,7 +1183,7 @@ impl CodegenAlternative { description: None, }) } - "failure_message" => { + FAILURE_MESSAGE_TOOL_NAME => { let Ok(mut input) = serde_json::from_value::(tool_use.input) else { @@ -1493,7 +1496,10 @@ mod tests { use indoc::indoc; use language::{Buffer, Point}; use language_model::fake_provider::FakeLanguageModel; - use language_model::{LanguageModelRegistry, TokenUsage}; + use language_model::{ + LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelRegistry, + LanguageModelToolUse, StopReason, TokenUsage, + }; use languages::rust_lang; use rand::prelude::*; use settings::SettingsStore; @@ -1805,6 +1811,51 @@ mod tests { ); } + // When not streaming tool calls, we strip backticks as part of parsing the model's + // plain text response. This is a regression test for a bug where we stripped + // backticks incorrectly. + #[gpui::test] + async fn test_allows_model_to_output_backticks(cx: &mut TestAppContext) { + init_test(cx); + let text = "- Improved; `cmd+click` behavior. Now requires `cmd` to be pressed before the click starts or it doesn't run. ([#44579](https://github.com/zed-industries/zed/pull/44579); thanks [Zachiah](https://github.com/Zachiah))"; + let buffer = cx.new(|cx| Buffer::local("", cx)); + let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx)); + let range = buffer.read_with(cx, |buffer, cx| { + let snapshot = buffer.snapshot(cx); + snapshot.anchor_before(Point::new(0, 0))..snapshot.anchor_after(Point::new(0, 0)) + }); + let prompt_builder = Arc::new(PromptBuilder::new(None).unwrap()); + let codegen = cx.new(|cx| { + CodegenAlternative::new( + buffer.clone(), + range.clone(), + true, + prompt_builder, + Uuid::new_v4(), + cx, + ) + }); + + let events_tx = simulate_tool_based_completion(&codegen, cx); + let chunk_len = text.find('`').unwrap(); + events_tx + .unbounded_send(rewrite_tool_use("tool_1", &text[..chunk_len], false)) + .unwrap(); + events_tx + .unbounded_send(rewrite_tool_use("tool_2", &text, true)) + .unwrap(); + events_tx + .unbounded_send(LanguageModelCompletionEvent::Stop(StopReason::EndTurn)) + .unwrap(); + drop(events_tx); + cx.run_until_parked(); + + assert_eq!( + buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()), + text + ); + } + #[gpui::test] async fn test_strip_invalid_spans_from_codeblock() { assert_chunks("Lorem ipsum dolor", "Lorem ipsum dolor").await; @@ -1870,4 +1921,39 @@ mod tests { }); chunks_tx } + + fn simulate_tool_based_completion( + codegen: &Entity, + cx: &mut TestAppContext, + ) -> mpsc::UnboundedSender { + let (events_tx, events_rx) = mpsc::unbounded(); + let model = Arc::new(FakeLanguageModel::default()); + codegen.update(cx, |codegen, cx| { + let completion_stream = Task::ready(Ok(events_rx.map(Ok).boxed() + as BoxStream< + 'static, + Result, + >)); + codegen.generation = codegen.handle_completion(model, completion_stream, cx); + }); + events_tx + } + + fn rewrite_tool_use( + id: &str, + replacement_text: &str, + is_complete: bool, + ) -> LanguageModelCompletionEvent { + let input = RewriteSectionInput { + replacement_text: replacement_text.into(), + }; + LanguageModelCompletionEvent::ToolUse(LanguageModelToolUse { + id: id.into(), + name: REWRITE_SECTION_TOOL_NAME.into(), + raw_input: serde_json::to_string(&input).unwrap(), + input: serde_json::to_value(&input).unwrap(), + is_input_complete: is_complete, + thought_signature: None, + }) + } } diff --git a/crates/agent_ui/src/completion_provider.rs b/crates/agent_ui/src/completion_provider.rs index 206a2b3282b5471e8d5e8d18788519c3853dca55..a7b955b81ef3a7edccca98f15fa73bb40787a2c9 100644 --- a/crates/agent_ui/src/completion_provider.rs +++ b/crates/agent_ui/src/completion_provider.rs @@ -1586,7 +1586,7 @@ pub(crate) fn search_rules( None } else { Some(RulesContextEntry { - prompt_id: metadata.id.user_id()?, + prompt_id: metadata.id.as_user()?, title: metadata.title?, }) } diff --git a/crates/agent_ui/src/inline_assistant.rs b/crates/agent_ui/src/inline_assistant.rs index 6e3ab7a162bc69a5b0ec081b060b4a2ba08b09aa..671579f9ef018b495b7993279a852595c78d3e02 100644 --- a/crates/agent_ui/src/inline_assistant.rs +++ b/crates/agent_ui/src/inline_assistant.rs @@ -1197,7 +1197,7 @@ impl InlineAssistant { assist .editor - .update(cx, |editor, cx| window.focus(&editor.focus_handle(cx))) + .update(cx, |editor, cx| window.focus(&editor.focus_handle(cx), cx)) .ok(); } @@ -1209,7 +1209,7 @@ impl InlineAssistant { if let Some(decorations) = assist.decorations.as_ref() { decorations.prompt_editor.update(cx, |prompt_editor, cx| { prompt_editor.editor.update(cx, |editor, cx| { - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); editor.select_all(&SelectAll, window, cx); }) }); @@ -2271,6 +2271,36 @@ pub mod evals { ); } + #[test] + #[cfg_attr(not(feature = "unit-eval"), ignore)] + fn eval_empty_buffer() { + run_eval( + 20, + 1.0, + "Write a Python hello, world program".to_string(), + "ˇ".to_string(), + |output| match output { + InlineAssistantOutput::Success { + full_buffer_text, .. + } => { + if full_buffer_text.is_empty() { + EvalOutput::failed("expected some output".to_string()) + } else { + EvalOutput::passed(format!("Produced {full_buffer_text}")) + } + } + o @ InlineAssistantOutput::Failure { .. } => EvalOutput::failed(format!( + "Assistant output does not match expected output: {:?}", + o + )), + o @ InlineAssistantOutput::Malformed { .. } => EvalOutput::failed(format!( + "Assistant output does not match expected output: {:?}", + o + )), + }, + ); + } + fn run_eval( iterations: usize, expected_pass_ratio: f32, diff --git a/crates/agent_ui/src/inline_prompt_editor.rs b/crates/agent_ui/src/inline_prompt_editor.rs index 517f8f08a6e7e9e31b2f88d1f5ee9444202009d5..8d96d56ea67cc9366df420b23e2221636d3450fb 100644 --- a/crates/agent_ui/src/inline_prompt_editor.rs +++ b/crates/agent_ui/src/inline_prompt_editor.rs @@ -357,7 +357,7 @@ impl PromptEditor { creases = insert_message_creases(&mut editor, &existing_creases, window, cx); if focus { - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); } editor }); diff --git a/crates/agent_ui/src/language_model_selector.rs b/crates/agent_ui/src/language_model_selector.rs index 77c8c95255908dc54639ad7ac6c55f1e8b8151f0..704e340ace35f33f757ab7708f96ffc940a8eb91 100644 --- a/crates/agent_ui/src/language_model_selector.rs +++ b/crates/agent_ui/src/language_model_selector.rs @@ -7,8 +7,8 @@ use gpui::{ Action, AnyElement, App, BackgroundExecutor, DismissEvent, FocusHandle, Subscription, Task, }; use language_model::{ - AuthenticateError, ConfiguredModel, LanguageModel, LanguageModelId, LanguageModelProvider, - LanguageModelProviderId, LanguageModelRegistry, + AuthenticateError, ConfiguredModel, IconOrSvg, LanguageModel, LanguageModelId, + LanguageModelProvider, LanguageModelProviderId, LanguageModelRegistry, }; use ordered_float::OrderedFloat; use picker::{Picker, PickerDelegate}; @@ -55,7 +55,7 @@ pub fn language_model_selector( fn all_models(cx: &App) -> GroupedModels { let lm_registry = LanguageModelRegistry::global(cx).read(cx); - let providers = lm_registry.providers(); + let providers = lm_registry.visible_providers(); let mut favorites_index = FavoritesIndex::default(); @@ -94,7 +94,7 @@ type FavoritesIndex = HashMap> #[derive(Clone)] struct ModelInfo { model: Arc, - icon: IconName, + icon: IconOrSvg, is_favorite: bool, } @@ -203,7 +203,7 @@ impl LanguageModelPickerDelegate { fn authenticate_all_providers(cx: &mut App) -> Task<()> { let authenticate_all_providers = LanguageModelRegistry::global(cx) .read(cx) - .providers() + .visible_providers() .iter() .map(|provider| (provider.id(), provider.name(), provider.authenticate(cx))) .collect::>(); @@ -474,7 +474,7 @@ impl PickerDelegate for LanguageModelPickerDelegate { let configured_providers = language_model_registry .read(cx) - .providers() + .visible_providers() .into_iter() .filter(|provider| provider.is_authenticated(cx)) .collect::>(); @@ -566,7 +566,10 @@ impl PickerDelegate for LanguageModelPickerDelegate { Some( ModelSelectorListItem::new(ix, model_info.model.name().0) - .icon(model_info.icon) + .map(|this| match &model_info.icon { + IconOrSvg::Icon(icon_name) => this.icon(*icon_name), + IconOrSvg::Svg(icon_path) => this.icon_path(icon_path.clone()), + }) .is_selected(is_selected) .is_focused(selected) .is_favorite(is_favorite) @@ -702,7 +705,7 @@ mod tests { .any(|(fav_provider, fav_name)| *fav_provider == provider && *fav_name == name); ModelInfo { model: Arc::new(TestLanguageModel::new(name, provider)), - icon: IconName::Ai, + icon: IconOrSvg::Icon(IconName::Ai), is_favorite, } }) diff --git a/crates/agent_ui/src/profile_selector.rs b/crates/agent_ui/src/profile_selector.rs index ac08070fcefa92854b51bc8a66d4d388d08e087d..327d2c67e2d5e87e67935ecdfa7fb6cd41acbcb5 100644 --- a/crates/agent_ui/src/profile_selector.rs +++ b/crates/agent_ui/src/profile_selector.rs @@ -191,6 +191,9 @@ impl Render for ProfileSelector { let container = || h_flex().gap_1().justify_between(); v_flex() .gap_1() + .child(container().child(Label::new("Toggle Profile Menu")).child( + KeyBinding::for_action_in(&ToggleProfileSelector, &focus_handle, cx), + )) .child( container() .pb_1() @@ -203,9 +206,6 @@ impl Render for ProfileSelector { cx, )), ) - .child(container().child(Label::new("Toggle Profile Menu")).child( - KeyBinding::for_action_in(&ToggleProfileSelector, &focus_handle, cx), - )) .into_any() } }), diff --git a/crates/agent_ui/src/terminal_inline_assistant.rs b/crates/agent_ui/src/terminal_inline_assistant.rs index 84a74242b80d0b2f8479b3c6dbca1c7d0bb2cb6d..cacbc316bb84e74e5c369451791f777a9bf58e82 100644 --- a/crates/agent_ui/src/terminal_inline_assistant.rs +++ b/crates/agent_ui/src/terminal_inline_assistant.rs @@ -127,7 +127,7 @@ impl TerminalInlineAssistant { if let Some(prompt_editor) = assist.prompt_editor.as_ref() { prompt_editor.update(cx, |this, cx| { this.editor.update(cx, |editor, cx| { - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); editor.select_all(&SelectAll, window, cx); }); }); @@ -292,7 +292,7 @@ impl TerminalInlineAssistant { .terminal .update(cx, |this, cx| { this.clear_block_below_cursor(cx); - this.focus_handle(cx).focus(window); + this.focus_handle(cx).focus(window, cx); }) .log_err(); @@ -369,7 +369,7 @@ impl TerminalInlineAssistant { .terminal .update(cx, |this, cx| { this.clear_block_below_cursor(cx); - this.focus_handle(cx).focus(window); + this.focus_handle(cx).focus(window, cx); }) .is_ok() } diff --git a/crates/agent_ui/src/text_thread_editor.rs b/crates/agent_ui/src/text_thread_editor.rs index 947afe050639f89922873a12baa8b1eadfc44995..514f45528427af89eeccf85512abf850a7a1be05 100644 --- a/crates/agent_ui/src/text_thread_editor.rs +++ b/crates/agent_ui/src/text_thread_editor.rs @@ -33,7 +33,8 @@ use language::{ language_settings::{SoftWrap, all_language_settings}, }; use language_model::{ - ConfigurationError, LanguageModelExt, LanguageModelImage, LanguageModelRegistry, Role, + ConfigurationError, IconOrSvg, LanguageModelExt, LanguageModelImage, LanguageModelRegistry, + Role, }; use multi_buffer::MultiBufferRow; use picker::{Picker, popover_menu::PickerPopoverMenu}; @@ -71,7 +72,7 @@ use workspace::{ pane, searchable::{SearchEvent, SearchableItem}, }; -use zed_actions::agent::{AddSelectionToThread, ToggleModelSelector}; +use zed_actions::agent::{AddSelectionToThread, PasteRaw, ToggleModelSelector}; use crate::CycleFavoriteModels; @@ -1341,7 +1342,7 @@ impl TextThreadEditor { if let Some((text, _)) = Self::get_selection_or_code_block(&context_editor_view, cx) { active_editor_view.update(cx, |editor, cx| { editor.insert(&text, window, cx); - editor.focus_handle(cx).focus(window); + editor.focus_handle(cx).focus(window, cx); }) } } @@ -1698,6 +1699,9 @@ impl TextThreadEditor { window: &mut Window, cx: &mut Context, ) { + let Some(workspace) = self.workspace.upgrade() else { + return; + }; let editor_clipboard_selections = cx .read_from_clipboard() .and_then(|item| item.entries().first().cloned()) @@ -1708,84 +1712,101 @@ impl TextThreadEditor { _ => None, }); - let has_file_context = editor_clipboard_selections - .as_ref() - .is_some_and(|selections| { - selections - .iter() - .any(|sel| sel.file_path.is_some() && sel.line_range.is_some()) - }); - - if has_file_context { - if let Some(clipboard_item) = cx.read_from_clipboard() { - if let Some(ClipboardEntry::String(clipboard_text)) = - clipboard_item.entries().first() - { - if let Some(selections) = editor_clipboard_selections { - cx.stop_propagation(); - - let text = clipboard_text.text(); - self.editor.update(cx, |editor, cx| { - let mut current_offset = 0; - let weak_editor = cx.entity().downgrade(); - - for selection in selections { - if let (Some(file_path), Some(line_range)) = - (selection.file_path, selection.line_range) - { - let selected_text = - &text[current_offset..current_offset + selection.len]; - let fence = assistant_slash_commands::codeblock_fence_for_path( - file_path.to_str(), - Some(line_range.clone()), - ); - let formatted_text = format!("{fence}{selected_text}\n```"); - - let insert_point = editor - .selections - .newest::(&editor.display_snapshot(cx)) - .head(); - let start_row = MultiBufferRow(insert_point.row); - - editor.insert(&formatted_text, window, cx); + // Insert creases for pasted clipboard selections that: + // 1. Contain exactly one selection + // 2. Have an associated file path + // 3. Span multiple lines (not single-line selections) + // 4. Belong to a file that exists in the current project + let should_insert_creases = util::maybe!({ + let selections = editor_clipboard_selections.as_ref()?; + if selections.len() > 1 { + return Some(false); + } + let selection = selections.first()?; + let file_path = selection.file_path.as_ref()?; + let line_range = selection.line_range.as_ref()?; - let snapshot = editor.buffer().read(cx).snapshot(cx); - let anchor_before = snapshot.anchor_after(insert_point); - let anchor_after = editor - .selections - .newest_anchor() - .head() - .bias_left(&snapshot); + if line_range.start() == line_range.end() { + return Some(false); + } - editor.insert("\n", window, cx); + Some( + workspace + .read(cx) + .project() + .read(cx) + .project_path_for_absolute_path(file_path, cx) + .is_some(), + ) + }) + .unwrap_or(false); - let crease_text = acp_thread::selection_name( - Some(file_path.as_ref()), - &line_range, - ); + if should_insert_creases && let Some(clipboard_item) = cx.read_from_clipboard() { + if let Some(ClipboardEntry::String(clipboard_text)) = clipboard_item.entries().first() { + if let Some(selections) = editor_clipboard_selections { + cx.stop_propagation(); - let fold_placeholder = quote_selection_fold_placeholder( - crease_text, - weak_editor.clone(), - ); - let crease = Crease::inline( - anchor_before..anchor_after, - fold_placeholder, - render_quote_selection_output_toggle, - |_, _, _, _| Empty.into_any(), - ); - editor.insert_creases(vec![crease], cx); - editor.fold_at(start_row, window, cx); + let text = clipboard_text.text(); + self.editor.update(cx, |editor, cx| { + let mut current_offset = 0; + let weak_editor = cx.entity().downgrade(); - current_offset += selection.len; - if !selection.is_entire_line && current_offset < text.len() { - current_offset += 1; - } + for selection in selections { + if let (Some(file_path), Some(line_range)) = + (selection.file_path, selection.line_range) + { + let selected_text = + &text[current_offset..current_offset + selection.len]; + let fence = assistant_slash_commands::codeblock_fence_for_path( + file_path.to_str(), + Some(line_range.clone()), + ); + let formatted_text = format!("{fence}{selected_text}\n```"); + + let insert_point = editor + .selections + .newest::(&editor.display_snapshot(cx)) + .head(); + let start_row = MultiBufferRow(insert_point.row); + + editor.insert(&formatted_text, window, cx); + + let snapshot = editor.buffer().read(cx).snapshot(cx); + let anchor_before = snapshot.anchor_after(insert_point); + let anchor_after = editor + .selections + .newest_anchor() + .head() + .bias_left(&snapshot); + + editor.insert("\n", window, cx); + + let crease_text = acp_thread::selection_name( + Some(file_path.as_ref()), + &line_range, + ); + + let fold_placeholder = quote_selection_fold_placeholder( + crease_text, + weak_editor.clone(), + ); + let crease = Crease::inline( + anchor_before..anchor_after, + fold_placeholder, + render_quote_selection_output_toggle, + |_, _, _, _| Empty.into_any(), + ); + editor.insert_creases(vec![crease], cx); + editor.fold_at(start_row, window, cx); + + current_offset += selection.len; + if !selection.is_entire_line && current_offset < text.len() { + current_offset += 1; } } - }); - return; - } + } + }); + return; } } } @@ -1944,6 +1965,12 @@ impl TextThreadEditor { } } + fn paste_raw(&mut self, _: &PasteRaw, window: &mut Window, cx: &mut Context) { + self.editor.update(cx, |editor, cx| { + editor.paste(&editor::actions::Paste, window, cx); + }); + } + fn update_image_blocks(&mut self, cx: &mut Context) { self.editor.update(cx, |editor, cx| { let buffer = editor.buffer().read(cx).snapshot(cx); @@ -2205,10 +2232,10 @@ impl TextThreadEditor { .default_model() .map(|default| default.provider); - let provider_icon = match active_provider { - Some(provider) => provider.icon(), - None => IconName::Ai, - }; + let provider_icon = active_provider + .as_ref() + .map(|p| p.icon()) + .unwrap_or(IconOrSvg::Icon(IconName::Ai)); let focus_handle = self.editor().focus_handle(cx); @@ -2218,6 +2245,13 @@ impl TextThreadEditor { (Color::Muted, IconName::ChevronDown) }; + let provider_icon_element = match provider_icon { + IconOrSvg::Svg(path) => Icon::from_external_svg(path), + IconOrSvg::Icon(name) => Icon::new(name), + } + .color(color) + .size(IconSize::XSmall); + let tooltip = Tooltip::element({ move |_, cx| { let focus_handle = focus_handle.clone(); @@ -2265,7 +2299,7 @@ impl TextThreadEditor { .child( h_flex() .gap_0p5() - .child(Icon::new(provider_icon).color(color).size(IconSize::XSmall)) + .child(provider_icon_element) .child( Label::new(model_name) .color(color) @@ -2627,6 +2661,7 @@ impl Render for TextThreadEditor { .capture_action(cx.listener(TextThreadEditor::copy)) .capture_action(cx.listener(TextThreadEditor::cut)) .capture_action(cx.listener(TextThreadEditor::paste)) + .on_action(cx.listener(TextThreadEditor::paste_raw)) .capture_action(cx.listener(TextThreadEditor::cycle_message_role)) .capture_action(cx.listener(TextThreadEditor::confirm_command)) .on_action(cx.listener(TextThreadEditor::assist)) diff --git a/crates/agent_ui/src/ui/acp_onboarding_modal.rs b/crates/agent_ui/src/ui/acp_onboarding_modal.rs index 8433904fb3b540c2d78c8634b7a6755303d6e15c..e48a36bd5af3eff578e230195dc2247900977173 100644 --- a/crates/agent_ui/src/ui/acp_onboarding_modal.rs +++ b/crates/agent_ui/src/ui/acp_onboarding_modal.rs @@ -222,8 +222,8 @@ impl Render for AcpOnboardingModal { acp_onboarding_event!("Canceled", trigger = "Action"); cx.emit(DismissEvent); })) - .on_any_mouse_down(cx.listener(|this, _: &MouseDownEvent, window, _cx| { - this.focus_handle.focus(window); + .on_any_mouse_down(cx.listener(|this, _: &MouseDownEvent, window, cx| { + this.focus_handle.focus(window, cx); })) .child(illustration) .child( diff --git a/crates/agent_ui/src/ui/claude_code_onboarding_modal.rs b/crates/agent_ui/src/ui/claude_code_onboarding_modal.rs index 06980f18977aefe228bb7f09962e69fe2b3a5068..a8f007666d8957a7195fdf36b612b578b16f543c 100644 --- a/crates/agent_ui/src/ui/claude_code_onboarding_modal.rs +++ b/crates/agent_ui/src/ui/claude_code_onboarding_modal.rs @@ -230,8 +230,8 @@ impl Render for ClaudeCodeOnboardingModal { claude_code_onboarding_event!("Canceled", trigger = "Action"); cx.emit(DismissEvent); })) - .on_any_mouse_down(cx.listener(|this, _: &MouseDownEvent, window, _cx| { - this.focus_handle.focus(window); + .on_any_mouse_down(cx.listener(|this, _: &MouseDownEvent, window, cx| { + this.focus_handle.focus(window, cx); })) .child(illustration) .child( diff --git a/crates/agent_ui/src/ui/model_selector_components.rs b/crates/agent_ui/src/ui/model_selector_components.rs index 061b4f58288798696b068a091fb392c033906627..beb0c13d761aa9e7e41c2ac4e35a8cfcc7e8d869 100644 --- a/crates/agent_ui/src/ui/model_selector_components.rs +++ b/crates/agent_ui/src/ui/model_selector_components.rs @@ -1,6 +1,11 @@ use gpui::{Action, FocusHandle, prelude::*}; use ui::{ElevationIndex, KeyBinding, ListItem, ListItemSpacing, Tooltip, prelude::*}; +enum ModelIcon { + Name(IconName), + Path(SharedString), +} + #[derive(IntoElement)] pub struct ModelSelectorHeader { title: SharedString, @@ -39,7 +44,7 @@ impl RenderOnce for ModelSelectorHeader { pub struct ModelSelectorListItem { index: usize, title: SharedString, - icon: Option, + icon: Option, is_selected: bool, is_focused: bool, is_favorite: bool, @@ -60,7 +65,12 @@ impl ModelSelectorListItem { } pub fn icon(mut self, icon: IconName) -> Self { - self.icon = Some(icon); + self.icon = Some(ModelIcon::Name(icon)); + self + } + + pub fn icon_path(mut self, path: SharedString) -> Self { + self.icon = Some(ModelIcon::Path(path)); self } @@ -105,9 +115,12 @@ impl RenderOnce for ModelSelectorListItem { .gap_1p5() .when_some(self.icon, |this, icon| { this.child( - Icon::new(icon) - .color(model_icon_color) - .size(IconSize::Small), + match icon { + ModelIcon::Name(icon_name) => Icon::new(icon_name), + ModelIcon::Path(icon_path) => Icon::from_external_svg(icon_path), + } + .color(model_icon_color) + .size(IconSize::Small), ) }) .child(Label::new(self.title).truncate()), diff --git a/crates/agent_ui/src/ui/onboarding_modal.rs b/crates/agent_ui/src/ui/onboarding_modal.rs index ad404afa784974631f914e6fece2de6b6c7d6a46..b8ec2b00657efca29fede32a5cc23b669ede66e7 100644 --- a/crates/agent_ui/src/ui/onboarding_modal.rs +++ b/crates/agent_ui/src/ui/onboarding_modal.rs @@ -83,8 +83,8 @@ impl Render for AgentOnboardingModal { agent_onboarding_event!("Canceled", trigger = "Action"); cx.emit(DismissEvent); })) - .on_any_mouse_down(cx.listener(|this, _: &MouseDownEvent, window, _cx| { - this.focus_handle.focus(window); + .on_any_mouse_down(cx.listener(|this, _: &MouseDownEvent, window, cx| { + this.focus_handle.focus(window, cx); })) .child( div() diff --git a/crates/agent_ui_v2/Cargo.toml b/crates/agent_ui_v2/Cargo.toml index f24ef47471cdcfe0910cf36c5e220c5276d5f6ae..2b2cf337adf578432d594ce14f2f58e5911c45fb 100644 --- a/crates/agent_ui_v2/Cargo.toml +++ b/crates/agent_ui_v2/Cargo.toml @@ -12,6 +12,10 @@ workspace = true path = "src/agent_ui_v2.rs" doctest = false +[features] +test-support = ["agent/test-support"] + + [dependencies] agent.workspace = true agent_servers.workspace = true @@ -38,3 +42,6 @@ time_format.workspace = true ui.workspace = true util.workspace = true workspace.workspace = true + +[dev-dependencies] +agent = { workspace = true, features = ["test-support"] } diff --git a/crates/agent_ui_v2/LICENSE-GPL b/crates/agent_ui_v2/LICENSE-GPL index e0f9dbd5d63fef1630c297edc4ceba4790be6f02..89e542f750cd3860a0598eff0dc34b56d7336dc4 120000 --- a/crates/agent_ui_v2/LICENSE-GPL +++ b/crates/agent_ui_v2/LICENSE-GPL @@ -1 +1 @@ -LICENSE-GPL \ No newline at end of file +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/agent_ui_v2/src/thread_history.rs b/crates/agent_ui_v2/src/thread_history.rs index 8f6626814902a9489536439e90041437a527e151..0e379a24fc3047e6a686046ea16a94ef25efb52c 100644 --- a/crates/agent_ui_v2/src/thread_history.rs +++ b/crates/agent_ui_v2/src/thread_history.rs @@ -1,5 +1,5 @@ use agent::{HistoryEntry, HistoryStore}; -use chrono::{Datelike as _, Local, NaiveDate, TimeDelta}; +use chrono::{Datelike as _, Local, NaiveDate, TimeDelta, Utc}; use editor::{Editor, EditorEvent}; use fuzzy::StringMatchCandidate; use gpui::{ @@ -411,7 +411,22 @@ impl AcpThreadHistory { let selected = ix == self.selected_index; let hovered = Some(ix) == self.hovered_index; let timestamp = entry.updated_at().timestamp(); - let thread_timestamp = format.format_timestamp(timestamp, self.local_timezone); + + let display_text = match format { + EntryTimeFormat::DateAndTime => { + let entry_time = entry.updated_at(); + let now = Utc::now(); + let duration = now.signed_duration_since(entry_time); + let days = duration.num_days(); + + format!("{}d", days) + } + EntryTimeFormat::TimeOnly => format.format_timestamp(timestamp, self.local_timezone), + }; + + let title = entry.title().clone(); + let full_date = + EntryTimeFormat::DateAndTime.format_timestamp(timestamp, self.local_timezone); h_flex() .w_full() @@ -432,11 +447,14 @@ impl AcpThreadHistory { .truncate(), ) .child( - Label::new(thread_timestamp) + Label::new(display_text) .color(Color::Muted) .size(LabelSize::XSmall), ), ) + .tooltip(move |_, cx| { + Tooltip::with_meta(title.clone(), None, full_date.clone(), cx) + }) .on_hover(cx.listener(move |this, is_hovered, _window, cx| { if *is_hovered { this.hovered_index = Some(ix); diff --git a/crates/ai_onboarding/src/agent_api_keys_onboarding.rs b/crates/ai_onboarding/src/agent_api_keys_onboarding.rs index fadc4222ae44f3dbad862fd9479b89321dbd3016..47197ec2331b97dd4d7561d9f14c91c7f91c9fa0 100644 --- a/crates/ai_onboarding/src/agent_api_keys_onboarding.rs +++ b/crates/ai_onboarding/src/agent_api_keys_onboarding.rs @@ -1,9 +1,9 @@ use gpui::{Action, IntoElement, ParentElement, RenderOnce, point}; -use language_model::{LanguageModelRegistry, ZED_CLOUD_PROVIDER_ID}; +use language_model::{IconOrSvg, LanguageModelRegistry, ZED_CLOUD_PROVIDER_ID}; use ui::{Divider, List, ListBulletItem, prelude::*}; pub struct ApiKeysWithProviders { - configured_providers: Vec<(IconName, SharedString)>, + configured_providers: Vec<(IconOrSvg, SharedString)>, } impl ApiKeysWithProviders { @@ -13,7 +13,8 @@ impl ApiKeysWithProviders { |this: &mut Self, _registry, event: &language_model::Event, cx| match event { language_model::Event::ProviderStateChanged(_) | language_model::Event::AddedProvider(_) - | language_model::Event::RemovedProvider(_) => { + | language_model::Event::RemovedProvider(_) + | language_model::Event::ProvidersChanged => { this.configured_providers = Self::compute_configured_providers(cx) } _ => {} @@ -26,9 +27,9 @@ impl ApiKeysWithProviders { } } - fn compute_configured_providers(cx: &App) -> Vec<(IconName, SharedString)> { + fn compute_configured_providers(cx: &App) -> Vec<(IconOrSvg, SharedString)> { LanguageModelRegistry::read_global(cx) - .providers() + .visible_providers() .iter() .filter(|provider| { provider.is_authenticated(cx) && provider.id() != ZED_CLOUD_PROVIDER_ID @@ -47,7 +48,14 @@ impl Render for ApiKeysWithProviders { .map(|(icon, name)| { h_flex() .gap_1p5() - .child(Icon::new(icon).size(IconSize::XSmall).color(Color::Muted)) + .child( + match icon { + IconOrSvg::Icon(icon_name) => Icon::new(icon_name), + IconOrSvg::Svg(icon_path) => Icon::from_external_svg(icon_path), + } + .size(IconSize::XSmall) + .color(Color::Muted), + ) .child(Label::new(name)) }); div() diff --git a/crates/ai_onboarding/src/agent_panel_onboarding_content.rs b/crates/ai_onboarding/src/agent_panel_onboarding_content.rs index 3c8ffc1663e0660829698b5449a006de5b3c6009..c2756927136449d649996ec3b4b87471114aca38 100644 --- a/crates/ai_onboarding/src/agent_panel_onboarding_content.rs +++ b/crates/ai_onboarding/src/agent_panel_onboarding_content.rs @@ -11,7 +11,7 @@ use crate::{AgentPanelOnboardingCard, ApiKeysWithoutProviders, ZedAiOnboarding}; pub struct AgentPanelOnboarding { user_store: Entity, client: Arc, - configured_providers: Vec<(IconName, SharedString)>, + has_configured_providers: bool, continue_with_zed_ai: Arc, } @@ -27,8 +27,9 @@ impl AgentPanelOnboarding { |this: &mut Self, _registry, event: &language_model::Event, cx| match event { language_model::Event::ProviderStateChanged(_) | language_model::Event::AddedProvider(_) - | language_model::Event::RemovedProvider(_) => { - this.configured_providers = Self::compute_available_providers(cx) + | language_model::Event::RemovedProvider(_) + | language_model::Event::ProvidersChanged => { + this.has_configured_providers = Self::has_configured_providers(cx) } _ => {} }, @@ -38,20 +39,16 @@ impl AgentPanelOnboarding { Self { user_store, client, - configured_providers: Self::compute_available_providers(cx), + has_configured_providers: Self::has_configured_providers(cx), continue_with_zed_ai: Arc::new(continue_with_zed_ai), } } - fn compute_available_providers(cx: &App) -> Vec<(IconName, SharedString)> { + fn has_configured_providers(cx: &App) -> bool { LanguageModelRegistry::read_global(cx) - .providers() + .visible_providers() .iter() - .filter(|provider| { - provider.is_authenticated(cx) && provider.id() != ZED_CLOUD_PROVIDER_ID - }) - .map(|provider| (provider.icon(), provider.name().0)) - .collect() + .any(|provider| provider.is_authenticated(cx) && provider.id() != ZED_CLOUD_PROVIDER_ID) } } @@ -81,7 +78,7 @@ impl Render for AgentPanelOnboarding { }), ) .map(|this| { - if enrolled_in_trial || is_pro_user || !self.configured_providers.is_empty() { + if enrolled_in_trial || is_pro_user || self.has_configured_providers { this } else { this.child(ApiKeysWithoutProviders::new()) diff --git a/crates/bedrock/src/bedrock.rs b/crates/bedrock/src/bedrock.rs index ec0b4070906fdfd31195668312b3e7b425cd28ee..744dde38076a5a12c9bc957a75e2435b1b753d96 100644 --- a/crates/bedrock/src/bedrock.rs +++ b/crates/bedrock/src/bedrock.rs @@ -87,7 +87,7 @@ pub async fn stream_completion( Ok(None) => None, Err(err) => Some(( Err(BedrockError::ClientError(anyhow!( - "{:?}", + "{}", aws_sdk_bedrockruntime::error::DisplayErrorContext(err) ))), stream, diff --git a/crates/codestral/src/codestral.rs b/crates/codestral/src/codestral.rs index 9bf0296ac357937cd1ad1470dba9a98864911de9..9cf2fab80b78ba06c6a2523013e2f73934f50052 100644 --- a/crates/codestral/src/codestral.rs +++ b/crates/codestral/src/codestral.rs @@ -1,6 +1,6 @@ use anyhow::{Context as _, Result}; use edit_prediction_context::{EditPredictionExcerpt, EditPredictionExcerptOptions}; -use edit_prediction_types::{Direction, EditPrediction, EditPredictionDelegate}; +use edit_prediction_types::{EditPrediction, EditPredictionDelegate}; use futures::AsyncReadExt; use gpui::{App, Context, Entity, Task}; use http_client::HttpClient; @@ -300,16 +300,6 @@ impl EditPredictionDelegate for CodestralEditPredictionDelegate { })); } - fn cycle( - &mut self, - _buffer: Entity, - _cursor_position: Anchor, - _direction: Direction, - _cx: &mut Context, - ) { - // Codestral doesn't support multiple completions, so cycling does nothing - } - fn accept(&mut self, _cx: &mut Context) { log::debug!("Codestral: Completion accepted"); self.pending_request = None; diff --git a/crates/collab_ui/src/collab_panel.rs b/crates/collab_ui/src/collab_panel.rs index 2f1e2842cbd2f5024df0608578b7cb7f4bbc158d..0ae4ff270bd672ca028d638484b9a23f5981de1a 100644 --- a/crates/collab_ui/src/collab_panel.rs +++ b/crates/collab_ui/src/collab_panel.rs @@ -1252,7 +1252,7 @@ impl CollabPanel { context_menu }); - window.focus(&context_menu.focus_handle(cx)); + window.focus(&context_menu.focus_handle(cx), cx); let subscription = cx.subscribe_in( &context_menu, window, @@ -1424,7 +1424,7 @@ impl CollabPanel { context_menu }); - window.focus(&context_menu.focus_handle(cx)); + window.focus(&context_menu.focus_handle(cx), cx); let subscription = cx.subscribe_in( &context_menu, window, @@ -1487,7 +1487,7 @@ impl CollabPanel { }) }); - window.focus(&context_menu.focus_handle(cx)); + window.focus(&context_menu.focus_handle(cx), cx); let subscription = cx.subscribe_in( &context_menu, window, @@ -1521,9 +1521,9 @@ impl CollabPanel { if cx.stop_active_drag(window) { return; } else if self.take_editing_state(window, cx) { - window.focus(&self.filter_editor.focus_handle(cx)); + window.focus(&self.filter_editor.focus_handle(cx), cx); } else if !self.reset_filter_editor_text(window, cx) { - self.focus_handle.focus(window); + self.focus_handle.focus(window, cx); } if self.context_menu.is_some() { @@ -1826,7 +1826,7 @@ impl CollabPanel { }); self.update_entries(false, cx); self.select_channel_editor(); - window.focus(&self.channel_name_editor.focus_handle(cx)); + window.focus(&self.channel_name_editor.focus_handle(cx), cx); cx.notify(); } @@ -1851,7 +1851,7 @@ impl CollabPanel { }); self.update_entries(false, cx); self.select_channel_editor(); - window.focus(&self.channel_name_editor.focus_handle(cx)); + window.focus(&self.channel_name_editor.focus_handle(cx), cx); cx.notify(); } @@ -1900,7 +1900,7 @@ impl CollabPanel { editor.set_text(channel.name.clone(), window, cx); editor.select_all(&Default::default(), window, cx); }); - window.focus(&self.channel_name_editor.focus_handle(cx)); + window.focus(&self.channel_name_editor.focus_handle(cx), cx); self.update_entries(false, cx); self.select_channel_editor(); } diff --git a/crates/collab_ui/src/collab_panel/channel_modal.rs b/crates/collab_ui/src/collab_panel/channel_modal.rs index 9d882562cab710f562145087e5c38474fda4808b..ae5b537f2c66dc273d504a70f2b75cb8bec0be20 100644 --- a/crates/collab_ui/src/collab_panel/channel_modal.rs +++ b/crates/collab_ui/src/collab_panel/channel_modal.rs @@ -642,7 +642,7 @@ impl ChannelModalDelegate { }); menu }); - window.focus(&context_menu.focus_handle(cx)); + window.focus(&context_menu.focus_handle(cx), cx); let subscription = cx.subscribe_in( &context_menu, window, diff --git a/crates/command_palette/src/command_palette.rs b/crates/command_palette/src/command_palette.rs index daf97bf676e27b5dd81ce4882c102dbfdefc502a..038b58ac5f4e90544232ccc8da55d0ca71ec28df 100644 --- a/crates/command_palette/src/command_palette.rs +++ b/crates/command_palette/src/command_palette.rs @@ -588,7 +588,7 @@ impl PickerDelegate for CommandPaletteDelegate { }) .detach_and_log_err(cx); let action = command.action; - window.focus(&self.previous_focus_handle); + window.focus(&self.previous_focus_handle, cx); self.dismissed(window, cx); window.dispatch_action(action, cx); } @@ -784,7 +784,7 @@ mod tests { workspace.update_in(cx, |workspace, window, cx| { workspace.add_item_to_active_pane(Box::new(editor.clone()), None, true, window, cx); - editor.update(cx, |editor, cx| window.focus(&editor.focus_handle(cx))) + editor.update(cx, |editor, cx| window.focus(&editor.focus_handle(cx), cx)) }); cx.simulate_keystrokes("cmd-shift-p"); @@ -855,7 +855,7 @@ mod tests { workspace.update_in(cx, |workspace, window, cx| { workspace.add_item_to_active_pane(Box::new(editor.clone()), None, true, window, cx); - editor.update(cx, |editor, cx| window.focus(&editor.focus_handle(cx))) + editor.update(cx, |editor, cx| window.focus(&editor.focus_handle(cx), cx)) }); // Test normalize (trimming whitespace and double colons) diff --git a/crates/copilot/src/copilot.rs b/crates/copilot/src/copilot.rs index f248fbdb43ec37b19ca951992df6a7ddbc4f7313..a6963296f5c0ce0395698d2952618123c103ff55 100644 --- a/crates/copilot/src/copilot.rs +++ b/crates/copilot/src/copilot.rs @@ -4,6 +4,7 @@ pub mod copilot_responses; pub mod request; mod sign_in; +use crate::request::NextEditSuggestions; use crate::sign_in::initiate_sign_out; use ::fs::Fs; use anyhow::{Context as _, Result, anyhow}; @@ -18,7 +19,7 @@ use http_client::HttpClient; use language::language_settings::CopilotSettings; use language::{ Anchor, Bias, Buffer, BufferSnapshot, Language, PointUtf16, ToPointUtf16, - language_settings::{EditPredictionProvider, all_language_settings, language_settings}, + language_settings::{EditPredictionProvider, all_language_settings}, point_from_lsp, point_to_lsp, }; use lsp::{LanguageServer, LanguageServerBinary, LanguageServerId, LanguageServerName}; @@ -40,7 +41,7 @@ use std::{ sync::Arc, }; use sum_tree::Dimensions; -use util::{ResultExt, fs::remove_matching, rel_path::RelPath}; +use util::{ResultExt, fs::remove_matching}; use workspace::Workspace; pub use crate::copilot_edit_prediction_delegate::CopilotEditPredictionDelegate; @@ -315,6 +316,15 @@ struct GlobalCopilot(Entity); impl Global for GlobalCopilot {} +/// Copilot's NextEditSuggestion response, with coordinates converted to Anchors. +struct CopilotEditPrediction { + buffer: Entity, + range: Range, + text: String, + command: Option, + snapshot: BufferSnapshot, +} + impl Copilot { pub fn global(cx: &App) -> Option> { cx.try_global::() @@ -873,101 +883,19 @@ impl Copilot { } } - pub fn completions( - &mut self, - buffer: &Entity, - position: T, - cx: &mut Context, - ) -> Task>> - where - T: ToPointUtf16, - { - self.request_completions::(buffer, position, cx) - } - - pub fn completions_cycling( + pub(crate) fn completions( &mut self, buffer: &Entity, - position: T, + position: Anchor, cx: &mut Context, - ) -> Task>> - where - T: ToPointUtf16, - { - self.request_completions::(buffer, position, cx) - } - - pub fn accept_completion( - &mut self, - completion: &Completion, - cx: &mut Context, - ) -> Task> { - let server = match self.server.as_authenticated() { - Ok(server) => server, - Err(error) => return Task::ready(Err(error)), - }; - let request = - server - .lsp - .request::(request::NotifyAcceptedParams { - uuid: completion.uuid.clone(), - }); - cx.background_spawn(async move { - request - .await - .into_response() - .context("copilot: notify accepted")?; - Ok(()) - }) - } - - pub fn discard_completions( - &mut self, - completions: &[Completion], - cx: &mut Context, - ) -> Task> { - let server = match self.server.as_authenticated() { - Ok(server) => server, - Err(_) => return Task::ready(Ok(())), - }; - let request = - server - .lsp - .request::(request::NotifyRejectedParams { - uuids: completions - .iter() - .map(|completion| completion.uuid.clone()) - .collect(), - }); - cx.background_spawn(async move { - request - .await - .into_response() - .context("copilot: notify rejected")?; - Ok(()) - }) - } - - fn request_completions( - &mut self, - buffer: &Entity, - position: T, - cx: &mut Context, - ) -> Task>> - where - R: 'static - + lsp::request::Request< - Params = request::GetCompletionsParams, - Result = request::GetCompletionsResult, - >, - T: ToPointUtf16, - { + ) -> Task>> { self.register_buffer(buffer, cx); let server = match self.server.as_authenticated() { Ok(server) => server, Err(error) => return Task::ready(Err(error)), }; + let buffer_entity = buffer.clone(); let lsp = server.lsp.clone(); let registered_buffer = server .registered_buffers @@ -977,46 +905,31 @@ impl Copilot { let buffer = buffer.read(cx); let uri = registered_buffer.uri.clone(); let position = position.to_point_utf16(buffer); - let settings = language_settings( - buffer.language_at(position).map(|l| l.name()), - buffer.file(), - cx, - ); - let tab_size = settings.tab_size; - let hard_tabs = settings.hard_tabs; - let relative_path = buffer - .file() - .map_or(RelPath::empty().into(), |file| file.path().clone()); cx.background_spawn(async move { let (version, snapshot) = snapshot.await?; let result = lsp - .request::(request::GetCompletionsParams { - doc: request::GetCompletionsDocument { - uri, - tab_size: tab_size.into(), - indent_size: 1, - insert_spaces: !hard_tabs, - relative_path: relative_path.to_proto(), - position: point_to_lsp(position), - version: version.try_into().unwrap(), - }, + .request::(request::NextEditSuggestionsParams { + text_document: lsp::VersionedTextDocumentIdentifier { uri, version }, + position: point_to_lsp(position), }) .await .into_response() .context("copilot: get completions")?; let completions = result - .completions + .edits .into_iter() .map(|completion| { let start = snapshot .clip_point_utf16(point_from_lsp(completion.range.start), Bias::Left); let end = snapshot.clip_point_utf16(point_from_lsp(completion.range.end), Bias::Left); - Completion { - uuid: completion.uuid, + CopilotEditPrediction { + buffer: buffer_entity.clone(), range: snapshot.anchor_before(start)..snapshot.anchor_after(end), text: completion.text, + command: completion.command, + snapshot: snapshot.clone(), } }) .collect(); @@ -1024,6 +937,35 @@ impl Copilot { }) } + pub(crate) fn accept_completion( + &mut self, + completion: &CopilotEditPrediction, + cx: &mut Context, + ) -> Task> { + let server = match self.server.as_authenticated() { + Ok(server) => server, + Err(error) => return Task::ready(Err(error)), + }; + if let Some(command) = &completion.command { + let request = server + .lsp + .request::(lsp::ExecuteCommandParams { + command: command.command.clone(), + arguments: command.arguments.clone().unwrap_or_default(), + ..Default::default() + }); + cx.background_spawn(async move { + request + .await + .into_response() + .context("copilot: notify accepted")?; + Ok(()) + }) + } else { + Task::ready(Ok(())) + } + } + pub fn status(&self) -> Status { match &self.server { CopilotServer::Starting { task } => Status::Starting { task: task.clone() }, @@ -1260,7 +1202,11 @@ async fn get_copilot_lsp(fs: Arc, node_runtime: NodeRuntime) -> anyhow:: mod tests { use super::*; use gpui::TestAppContext; - use util::{path, paths::PathStyle, rel_path::rel_path}; + use util::{ + path, + paths::PathStyle, + rel_path::{RelPath, rel_path}, + }; #[gpui::test(iterations = 10)] async fn test_buffer_management(cx: &mut TestAppContext) { diff --git a/crates/copilot/src/copilot_edit_prediction_delegate.rs b/crates/copilot/src/copilot_edit_prediction_delegate.rs index 0e0cfe6cdca78d2a8b382269ce1ca9a340d1e69c..514e135cb4c34f6a1f49687fcd413113f78f9eae 100644 --- a/crates/copilot/src/copilot_edit_prediction_delegate.rs +++ b/crates/copilot/src/copilot_edit_prediction_delegate.rs @@ -1,49 +1,29 @@ -use crate::{Completion, Copilot}; +use crate::{Copilot, CopilotEditPrediction}; use anyhow::Result; -use edit_prediction_types::{Direction, EditPrediction, EditPredictionDelegate}; -use gpui::{App, Context, Entity, EntityId, Task}; -use language::{Buffer, OffsetRangeExt, ToOffset, language_settings::AllLanguageSettings}; -use settings::Settings; -use std::{path::Path, time::Duration}; +use edit_prediction_types::{EditPrediction, EditPredictionDelegate, interpolate_edits}; +use gpui::{App, Context, Entity, Task}; +use language::{Anchor, Buffer, EditPreview, OffsetRangeExt}; +use std::{ops::Range, sync::Arc, time::Duration}; pub const COPILOT_DEBOUNCE_TIMEOUT: Duration = Duration::from_millis(75); pub struct CopilotEditPredictionDelegate { - cycled: bool, - buffer_id: Option, - completions: Vec, - active_completion_index: usize, - file_extension: Option, + completion: Option<(CopilotEditPrediction, EditPreview)>, pending_refresh: Option>>, - pending_cycling_refresh: Option>>, copilot: Entity, } impl CopilotEditPredictionDelegate { pub fn new(copilot: Entity) -> Self { Self { - cycled: false, - buffer_id: None, - completions: Vec::new(), - active_completion_index: 0, - file_extension: None, + completion: None, pending_refresh: None, - pending_cycling_refresh: None, copilot, } } - fn active_completion(&self) -> Option<&Completion> { - self.completions.get(self.active_completion_index) - } - - fn push_completion(&mut self, new_completion: Completion) { - for completion in &self.completions { - if completion.text == new_completion.text && completion.range == new_completion.range { - return; - } - } - self.completions.push(new_completion); + fn active_completion(&self) -> Option<&(CopilotEditPrediction, EditPreview)> { + self.completion.as_ref() } } @@ -64,12 +44,8 @@ impl EditPredictionDelegate for CopilotEditPredictionDelegate { true } - fn supports_jump_to_edit() -> bool { - false - } - fn is_refreshing(&self, _cx: &App) -> bool { - self.pending_refresh.is_some() && self.completions.is_empty() + self.pending_refresh.is_some() && self.completion.is_none() } fn is_enabled( @@ -102,160 +78,96 @@ impl EditPredictionDelegate for CopilotEditPredictionDelegate { })? .await?; - this.update(cx, |this, cx| { - if !completions.is_empty() { - this.cycled = false; + if let Some(mut completion) = completions.into_iter().next() + && let Some(trimmed_completion) = cx + .update(|cx| trim_completion(&completion, cx)) + .ok() + .flatten() + { + let preview = buffer + .update(cx, |this, cx| { + this.preview_edits(Arc::from(std::slice::from_ref(&trimmed_completion)), cx) + })? + .await; + this.update(cx, |this, cx| { this.pending_refresh = None; - this.pending_cycling_refresh = None; - this.completions.clear(); - this.active_completion_index = 0; - this.buffer_id = Some(buffer.entity_id()); - this.file_extension = buffer.read(cx).file().and_then(|file| { - Some( - Path::new(file.file_name(cx)) - .extension()? - .to_str()? - .to_string(), - ) - }); - - for completion in completions { - this.push_completion(completion); - } + completion.range = trimmed_completion.0; + completion.text = trimmed_completion.1.to_string(); + this.completion = Some((completion, preview)); + cx.notify(); - } - })?; + })?; + } Ok(()) })); } - fn cycle( - &mut self, - buffer: Entity, - cursor_position: language::Anchor, - direction: Direction, - cx: &mut Context, - ) { - if self.cycled { - match direction { - Direction::Prev => { - self.active_completion_index = if self.active_completion_index == 0 { - self.completions.len().saturating_sub(1) - } else { - self.active_completion_index - 1 - }; - } - Direction::Next => { - if self.completions.is_empty() { - self.active_completion_index = 0 - } else { - self.active_completion_index = - (self.active_completion_index + 1) % self.completions.len(); - } - } - } - - cx.notify(); - } else { - let copilot = self.copilot.clone(); - self.pending_cycling_refresh = Some(cx.spawn(async move |this, cx| { - let completions = copilot - .update(cx, |copilot, cx| { - copilot.completions_cycling(&buffer, cursor_position, cx) - })? - .await?; - - this.update(cx, |this, cx| { - this.cycled = true; - this.file_extension = buffer.read(cx).file().and_then(|file| { - Some( - Path::new(file.file_name(cx)) - .extension()? - .to_str()? - .to_string(), - ) - }); - for completion in completions { - this.push_completion(completion); - } - this.cycle(buffer, cursor_position, direction, cx); - })?; - - Ok(()) - })); - } - } - fn accept(&mut self, cx: &mut Context) { - if let Some(completion) = self.active_completion() { + if let Some((completion, _)) = self.active_completion() { self.copilot .update(cx, |copilot, cx| copilot.accept_completion(completion, cx)) .detach_and_log_err(cx); } } - fn discard(&mut self, cx: &mut Context) { - let settings = AllLanguageSettings::get_global(cx); - - let copilot_enabled = settings.show_edit_predictions(None, cx); - - if !copilot_enabled { - return; - } - - self.copilot - .update(cx, |copilot, cx| { - copilot.discard_completions(&self.completions, cx) - }) - .detach_and_log_err(cx); - } + fn discard(&mut self, _: &mut Context) {} fn suggest( &mut self, buffer: &Entity, - cursor_position: language::Anchor, + _: language::Anchor, cx: &mut Context, ) -> Option { let buffer_id = buffer.entity_id(); let buffer = buffer.read(cx); - let completion = self.active_completion()?; - if Some(buffer_id) != self.buffer_id + let (completion, edit_preview) = self.active_completion()?; + + if Some(buffer_id) != Some(completion.buffer.entity_id()) || !completion.range.start.is_valid(buffer) || !completion.range.end.is_valid(buffer) { return None; } + let edits = vec![( + completion.range.clone(), + Arc::from(completion.text.as_ref()), + )]; + let edits = interpolate_edits(&completion.snapshot, &buffer.snapshot(), &edits) + .filter(|edits| !edits.is_empty())?; + + Some(EditPrediction::Local { + id: None, + edits, + edit_preview: Some(edit_preview.clone()), + }) + } +} - let mut completion_range = completion.range.to_offset(buffer); - let prefix_len = common_prefix( - buffer.chars_for_range(completion_range.clone()), - completion.text.chars(), - ); - completion_range.start += prefix_len; - let suffix_len = common_prefix( - buffer.reversed_chars_for_range(completion_range.clone()), - completion.text[prefix_len..].chars().rev(), - ); - completion_range.end = completion_range.end.saturating_sub(suffix_len); - - if completion_range.is_empty() - && completion_range.start == cursor_position.to_offset(buffer) - { - let completion_text = &completion.text[prefix_len..completion.text.len() - suffix_len]; - if completion_text.trim().is_empty() { - None - } else { - let position = cursor_position.bias_right(buffer); - Some(EditPrediction::Local { - id: None, - edits: vec![(position..position, completion_text.into())], - edit_preview: None, - }) - } - } else { - None - } +fn trim_completion( + completion: &CopilotEditPrediction, + cx: &mut App, +) -> Option<(Range, Arc)> { + let buffer = completion.buffer.read(cx); + let mut completion_range = completion.range.to_offset(buffer); + let prefix_len = common_prefix( + buffer.chars_for_range(completion_range.clone()), + completion.text.chars(), + ); + completion_range.start += prefix_len; + let suffix_len = common_prefix( + buffer.reversed_chars_for_range(completion_range.clone()), + completion.text[prefix_len..].chars().rev(), + ); + completion_range.end = completion_range.end.saturating_sub(suffix_len); + let completion_text = &completion.text[prefix_len..completion.text.len() - suffix_len]; + if completion_text.trim().is_empty() { + None + } else { + let completion_range = + buffer.anchor_after(completion_range.start)..buffer.anchor_after(completion_range.end); + + Some((completion_range, Arc::from(completion_text))) } } @@ -282,6 +194,7 @@ mod tests { Point, language_settings::{CompletionSettingsContent, LspInsertMode, WordsCompletionMode}, }; + use lsp::Uri; use project::Project; use serde_json::json; use settings::{AllLanguageSettingsContent, SettingsStore}; @@ -337,12 +250,15 @@ mod tests { )); handle_copilot_completion_request( &copilot_lsp, - vec![crate::request::Completion { + vec![crate::request::NextEditSuggestion { text: "one.copilot1".into(), range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 4)), - ..Default::default() + command: None, + text_document: lsp::VersionedTextDocumentIdentifier { + uri: Uri::from_file_path(path!("/root/dir/file.rs")).unwrap(), + version: 0, + }, }], - vec![], ); executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT); cx.update_editor(|editor, window, cx| { @@ -383,12 +299,15 @@ mod tests { )); handle_copilot_completion_request( &copilot_lsp, - vec![crate::request::Completion { + vec![crate::request::NextEditSuggestion { text: "one.copilot1".into(), range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 4)), - ..Default::default() + command: None, + text_document: lsp::VersionedTextDocumentIdentifier { + uri: Uri::from_file_path(path!("/root/dir/file.rs")).unwrap(), + version: 0, + }, }], - vec![], ); executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT); cx.update_editor(|editor, _, cx| { @@ -412,12 +331,15 @@ mod tests { // After debouncing, new Copilot completions should be requested. handle_copilot_completion_request( &copilot_lsp, - vec![crate::request::Completion { + vec![crate::request::NextEditSuggestion { text: "one.copilot2".into(), range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 5)), - ..Default::default() + command: None, + text_document: lsp::VersionedTextDocumentIdentifier { + uri: Uri::from_file_path(path!("/root/dir/file.rs")).unwrap(), + version: 0, + }, }], - vec![], ); executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT); cx.update_editor(|editor, window, cx| { @@ -479,45 +401,6 @@ mod tests { assert_eq!(editor.display_text(cx), "one.cop\ntwo\nthree\n"); assert_eq!(editor.text(cx), "one.cop\ntwo\nthree\n"); }); - - // Reset the editor to verify how suggestions behave when tabbing on leading indentation. - cx.update_editor(|editor, window, cx| { - editor.set_text("fn foo() {\n \n}", window, cx); - editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| { - s.select_ranges([Point::new(1, 2)..Point::new(1, 2)]) - }); - }); - handle_copilot_completion_request( - &copilot_lsp, - vec![crate::request::Completion { - text: " let x = 4;".into(), - range: lsp::Range::new(lsp::Position::new(1, 0), lsp::Position::new(1, 2)), - ..Default::default() - }], - vec![], - ); - - cx.update_editor(|editor, window, cx| { - editor.next_edit_prediction(&Default::default(), window, cx) - }); - executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT); - cx.update_editor(|editor, window, cx| { - assert!(editor.has_active_edit_prediction()); - assert_eq!(editor.display_text(cx), "fn foo() {\n let x = 4;\n}"); - assert_eq!(editor.text(cx), "fn foo() {\n \n}"); - - // Tabbing inside of leading whitespace inserts indentation without accepting the suggestion. - editor.tab(&Default::default(), window, cx); - assert!(editor.has_active_edit_prediction()); - assert_eq!(editor.text(cx), "fn foo() {\n \n}"); - assert_eq!(editor.display_text(cx), "fn foo() {\n let x = 4;\n}"); - - // Using AcceptEditPrediction again accepts the suggestion. - editor.accept_edit_prediction(&Default::default(), window, cx); - assert!(!editor.has_active_edit_prediction()); - assert_eq!(editor.text(cx), "fn foo() {\n let x = 4;\n}"); - assert_eq!(editor.display_text(cx), "fn foo() {\n let x = 4;\n}"); - }); } #[gpui::test(iterations = 10)] @@ -570,12 +453,15 @@ mod tests { )); handle_copilot_completion_request( &copilot_lsp, - vec![crate::request::Completion { + vec![crate::request::NextEditSuggestion { text: "one.copilot1".into(), range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 4)), - ..Default::default() + command: None, + text_document: lsp::VersionedTextDocumentIdentifier { + uri: Uri::from_file_path(path!("/root/dir/file.rs")).unwrap(), + version: 0, + }, }], - vec![], ); executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT); cx.update_editor(|editor, window, cx| { @@ -614,12 +500,15 @@ mod tests { )); handle_copilot_completion_request( &copilot_lsp, - vec![crate::request::Completion { + vec![crate::request::NextEditSuggestion { text: "one.123. copilot\n 456".into(), range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 4)), - ..Default::default() + command: None, + text_document: lsp::VersionedTextDocumentIdentifier { + uri: Uri::from_file_path(path!("/root/dir/file.rs")).unwrap(), + version: 0, + }, }], - vec![], ); executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT); cx.update_editor(|editor, window, cx| { @@ -686,15 +575,18 @@ mod tests { handle_copilot_completion_request( &copilot_lsp, - vec![crate::request::Completion { + vec![crate::request::NextEditSuggestion { text: "two.foo()".into(), range: lsp::Range::new(lsp::Position::new(1, 0), lsp::Position::new(1, 2)), - ..Default::default() + command: None, + text_document: lsp::VersionedTextDocumentIdentifier { + uri: Uri::from_file_path(path!("/root/dir/file.rs")).unwrap(), + version: 0, + }, }], - vec![], ); cx.update_editor(|editor, window, cx| { - editor.next_edit_prediction(&Default::default(), window, cx) + editor.show_edit_prediction(&Default::default(), window, cx) }); executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT); cx.update_editor(|editor, window, cx| { @@ -703,15 +595,22 @@ mod tests { assert_eq!(editor.text(cx), "one\ntw\nthree\n"); editor.backspace(&Default::default(), window, cx); + }); + executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT); + cx.run_until_parked(); + cx.update_editor(|editor, window, cx| { assert!(editor.has_active_edit_prediction()); assert_eq!(editor.display_text(cx), "one\ntwo.foo()\nthree\n"); assert_eq!(editor.text(cx), "one\nt\nthree\n"); editor.backspace(&Default::default(), window, cx); + }); + executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT); + cx.run_until_parked(); + cx.update_editor(|editor, window, cx| { assert!(editor.has_active_edit_prediction()); assert_eq!(editor.display_text(cx), "one\ntwo.foo()\nthree\n"); assert_eq!(editor.text(cx), "one\n\nthree\n"); - // Deleting across the original suggestion range invalidates it. editor.backspace(&Default::default(), window, cx); assert!(!editor.has_active_edit_prediction()); @@ -753,7 +652,7 @@ mod tests { editor .update(cx, |editor, window, cx| { use gpui::Focusable; - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); }) .unwrap(); let copilot_provider = cx.new(|_| CopilotEditPredictionDelegate::new(copilot)); @@ -765,19 +664,22 @@ mod tests { handle_copilot_completion_request( &copilot_lsp, - vec![crate::request::Completion { + vec![crate::request::NextEditSuggestion { text: "b = 2 + a".into(), range: lsp::Range::new(lsp::Position::new(1, 0), lsp::Position::new(1, 5)), - ..Default::default() + command: None, + text_document: lsp::VersionedTextDocumentIdentifier { + uri: Uri::from_file_path(path!("/root/dir/file.rs")).unwrap(), + version: 0, + }, }], - vec![], ); _ = editor.update(cx, |editor, window, cx| { // Ensure copilot suggestions are shown for the first excerpt. editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| { s.select_ranges([Point::new(1, 5)..Point::new(1, 5)]) }); - editor.next_edit_prediction(&Default::default(), window, cx); + editor.show_edit_prediction(&Default::default(), window, cx); }); executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT); _ = editor.update(cx, |editor, _, cx| { @@ -791,12 +693,15 @@ mod tests { handle_copilot_completion_request( &copilot_lsp, - vec![crate::request::Completion { + vec![crate::request::NextEditSuggestion { text: "d = 4 + c".into(), range: lsp::Range::new(lsp::Position::new(1, 0), lsp::Position::new(1, 6)), - ..Default::default() + command: None, + text_document: lsp::VersionedTextDocumentIdentifier { + uri: Uri::from_file_path(path!("/root/dir/file.rs")).unwrap(), + version: 0, + }, }], - vec![], ); _ = editor.update(cx, |editor, window, cx| { // Move to another excerpt, ensuring the suggestion gets cleared. @@ -873,15 +778,18 @@ mod tests { )); handle_copilot_completion_request( &copilot_lsp, - vec![crate::request::Completion { + vec![crate::request::NextEditSuggestion { text: "two.foo()".into(), range: lsp::Range::new(lsp::Position::new(1, 0), lsp::Position::new(1, 2)), - ..Default::default() + command: None, + text_document: lsp::VersionedTextDocumentIdentifier { + uri: Uri::from_file_path(path!("/root/dir/file.rs")).unwrap(), + version: 0, + }, }], - vec![], ); cx.update_editor(|editor, window, cx| { - editor.next_edit_prediction(&Default::default(), window, cx) + editor.show_edit_prediction(&Default::default(), window, cx) }); executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT); cx.update_editor(|editor, _, cx| { @@ -903,12 +811,15 @@ mod tests { )); handle_copilot_completion_request( &copilot_lsp, - vec![crate::request::Completion { + vec![crate::request::NextEditSuggestion { text: "two.foo()".into(), range: lsp::Range::new(lsp::Position::new(1, 0), lsp::Position::new(1, 3)), - ..Default::default() + command: None, + text_document: lsp::VersionedTextDocumentIdentifier { + uri: Uri::from_file_path(path!("/root/dir/file.rs")).unwrap(), + version: 0, + }, }], - vec![], ); executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT); cx.update_editor(|editor, _, cx| { @@ -930,12 +841,15 @@ mod tests { )); handle_copilot_completion_request( &copilot_lsp, - vec![crate::request::Completion { + vec![crate::request::NextEditSuggestion { text: "two.foo()".into(), range: lsp::Range::new(lsp::Position::new(1, 0), lsp::Position::new(1, 4)), - ..Default::default() + command: None, + text_document: lsp::VersionedTextDocumentIdentifier { + uri: Uri::from_file_path(path!("/root/dir/file.rs")).unwrap(), + version: 0, + }, }], - vec![], ); executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT); cx.update_editor(|editor, _, cx| { @@ -1000,7 +914,7 @@ mod tests { editor .update(cx, |editor, window, cx| { use gpui::Focusable; - window.focus(&editor.focus_handle(cx)) + window.focus(&editor.focus_handle(cx), cx) }) .unwrap(); let copilot_provider = cx.new(|_| CopilotEditPredictionDelegate::new(copilot)); @@ -1011,16 +925,20 @@ mod tests { .unwrap(); let mut copilot_requests = copilot_lsp - .set_request_handler::( + .set_request_handler::( move |_params, _cx| async move { - Ok(crate::request::GetCompletionsResult { - completions: vec![crate::request::Completion { + Ok(crate::request::NextEditSuggestionsResult { + edits: vec![crate::request::NextEditSuggestion { text: "next line".into(), range: lsp::Range::new( lsp::Position::new(1, 0), lsp::Position::new(1, 0), ), - ..Default::default() + command: None, + text_document: lsp::VersionedTextDocumentIdentifier { + uri: Uri::from_file_path(path!("/root/dir/file.rs")).unwrap(), + version: 0, + }, }], }) }, @@ -1049,23 +967,14 @@ mod tests { fn handle_copilot_completion_request( lsp: &lsp::FakeLanguageServer, - completions: Vec, - completions_cycling: Vec, + completions: Vec, ) { - lsp.set_request_handler::(move |_params, _cx| { - let completions = completions.clone(); - async move { - Ok(crate::request::GetCompletionsResult { - completions: completions.clone(), - }) - } - }); - lsp.set_request_handler::( + lsp.set_request_handler::( move |_params, _cx| { - let completions_cycling = completions_cycling.clone(); + let completions = completions.clone(); async move { - Ok(crate::request::GetCompletionsResult { - completions: completions_cycling.clone(), + Ok(crate::request::NextEditSuggestionsResult { + edits: completions.clone(), }) } }, diff --git a/crates/copilot/src/request.rs b/crates/copilot/src/request.rs index 85d6254dc060824a9b2686e8f53090fccb39980e..2f97fb72a42904b1fefdd3999f680fca12559ecd 100644 --- a/crates/copilot/src/request.rs +++ b/crates/copilot/src/request.rs @@ -1,3 +1,4 @@ +use lsp::VersionedTextDocumentIdentifier; use serde::{Deserialize, Serialize}; pub enum CheckStatus {} @@ -88,72 +89,6 @@ impl lsp::request::Request for SignOut { const METHOD: &'static str = "signOut"; } -pub enum GetCompletions {} - -#[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct GetCompletionsParams { - pub doc: GetCompletionsDocument, -} - -#[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct GetCompletionsDocument { - pub tab_size: u32, - pub indent_size: u32, - pub insert_spaces: bool, - pub uri: lsp::Uri, - pub relative_path: String, - pub position: lsp::Position, - pub version: usize, -} - -#[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct GetCompletionsResult { - pub completions: Vec, -} - -#[derive(Clone, Debug, Default, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct Completion { - pub text: String, - pub position: lsp::Position, - pub uuid: String, - pub range: lsp::Range, - pub display_text: String, -} - -impl lsp::request::Request for GetCompletions { - type Params = GetCompletionsParams; - type Result = GetCompletionsResult; - const METHOD: &'static str = "getCompletions"; -} - -pub enum GetCompletionsCycling {} - -impl lsp::request::Request for GetCompletionsCycling { - type Params = GetCompletionsParams; - type Result = GetCompletionsResult; - const METHOD: &'static str = "getCompletionsCycling"; -} - -pub enum LogMessage {} - -#[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct LogMessageParams { - pub level: u8, - pub message: String, - pub metadata_str: String, - pub extra: Vec, -} - -impl lsp::notification::Notification for LogMessage { - type Params = LogMessageParams; - const METHOD: &'static str = "LogMessage"; -} - pub enum StatusNotification {} #[derive(Debug, Serialize, Deserialize)] @@ -223,3 +158,36 @@ impl lsp::request::Request for NotifyRejected { type Result = String; const METHOD: &'static str = "notifyRejected"; } + +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct NextEditSuggestions; + +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct NextEditSuggestionsParams { + pub(crate) text_document: VersionedTextDocumentIdentifier, + pub(crate) position: lsp::Position, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct NextEditSuggestion { + pub text: String, + pub text_document: VersionedTextDocumentIdentifier, + pub range: lsp::Range, + pub command: Option, +} + +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct NextEditSuggestionsResult { + pub edits: Vec, +} + +impl lsp::request::Request for NextEditSuggestions { + type Params = NextEditSuggestionsParams; + type Result = NextEditSuggestionsResult; + + const METHOD: &'static str = "textDocument/copilotInlineEdit"; +} diff --git a/crates/copilot/src/sign_in.rs b/crates/copilot/src/sign_in.rs index 20e31525a8fdb09fce04934d3445d51ba4226a2e..4f71a34408e23f099d4d3c145d86af24e607e3c3 100644 --- a/crates/copilot/src/sign_in.rs +++ b/crates/copilot/src/sign_in.rs @@ -435,8 +435,8 @@ impl Render for CopilotCodeVerification { .on_action(cx.listener(|_, _: &menu::Cancel, _, cx| { cx.emit(DismissEvent); })) - .on_any_mouse_down(cx.listener(|this, _: &MouseDownEvent, window, _| { - window.focus(&this.focus_handle); + .on_any_mouse_down(cx.listener(|this, _: &MouseDownEvent, window, cx| { + window.focus(&this.focus_handle, cx); })) .child( Vector::new(VectorName::ZedXCopilot, rems(8.), rems(4.)) diff --git a/crates/debugger_ui/src/debugger_panel.rs b/crates/debugger_ui/src/debugger_panel.rs index 104a85dc097c575e7a4cd8f4a66a98a8bb6b0d69..35ce80d3f64e362735c1c020363dbbfc2703a101 100644 --- a/crates/debugger_ui/src/debugger_panel.rs +++ b/crates/debugger_ui/src/debugger_panel.rs @@ -577,7 +577,7 @@ impl DebugPanel { menu }); - window.focus(&context_menu.focus_handle(cx)); + window.focus(&context_menu.focus_handle(cx), cx); let subscription = cx.subscribe(&context_menu, |this, _, _: &DismissEvent, cx| { this.context_menu.take(); cx.notify(); @@ -1052,7 +1052,7 @@ impl DebugPanel { cx: &mut Context, ) { debug_assert!(self.sessions_with_children.contains_key(&session_item)); - session_item.focus_handle(cx).focus(window); + session_item.focus_handle(cx).focus(window, cx); session_item.update(cx, |this, cx| { this.running_state().update(cx, |this, cx| { this.go_to_selected_stack_frame(window, cx); diff --git a/crates/debugger_ui/src/new_process_modal.rs b/crates/debugger_ui/src/new_process_modal.rs index 8aaa61aad6380752a7bdd62ee35635ebb6d160e4..68e391562b57d530a21624b0626173eeb7a67c16 100644 --- a/crates/debugger_ui/src/new_process_modal.rs +++ b/crates/debugger_ui/src/new_process_modal.rs @@ -574,7 +574,7 @@ impl Render for NewProcessModal { NewProcessMode::Launch => NewProcessMode::Task, }; - this.mode_focus_handle(cx).focus(window); + this.mode_focus_handle(cx).focus(window, cx); })) .on_action( cx.listener(|this, _: &pane::ActivatePreviousItem, window, cx| { @@ -585,7 +585,7 @@ impl Render for NewProcessModal { NewProcessMode::Launch => NewProcessMode::Attach, }; - this.mode_focus_handle(cx).focus(window); + this.mode_focus_handle(cx).focus(window, cx); }), ) .child( @@ -602,7 +602,7 @@ impl Render for NewProcessModal { NewProcessMode::Task.to_string(), cx.listener(|this, _, window, cx| { this.mode = NewProcessMode::Task; - this.mode_focus_handle(cx).focus(window); + this.mode_focus_handle(cx).focus(window, cx); cx.notify(); }), ) @@ -611,7 +611,7 @@ impl Render for NewProcessModal { NewProcessMode::Debug.to_string(), cx.listener(|this, _, window, cx| { this.mode = NewProcessMode::Debug; - this.mode_focus_handle(cx).focus(window); + this.mode_focus_handle(cx).focus(window, cx); cx.notify(); }), ) @@ -629,7 +629,7 @@ impl Render for NewProcessModal { cx, ); } - this.mode_focus_handle(cx).focus(window); + this.mode_focus_handle(cx).focus(window, cx); cx.notify(); }), ) @@ -638,7 +638,7 @@ impl Render for NewProcessModal { NewProcessMode::Launch.to_string(), cx.listener(|this, _, window, cx| { this.mode = NewProcessMode::Launch; - this.mode_focus_handle(cx).focus(window); + this.mode_focus_handle(cx).focus(window, cx); cx.notify(); }), ) @@ -840,17 +840,17 @@ impl ConfigureMode { } } - fn on_tab(&mut self, _: &menu::SelectNext, window: &mut Window, _: &mut Context) { - window.focus_next(); + fn on_tab(&mut self, _: &menu::SelectNext, window: &mut Window, cx: &mut Context) { + window.focus_next(cx); } fn on_tab_prev( &mut self, _: &menu::SelectPrevious, window: &mut Window, - _: &mut Context, + cx: &mut Context, ) { - window.focus_prev(); + window.focus_prev(cx); } fn render( @@ -923,7 +923,7 @@ impl AttachMode { window, cx, ); - window.focus(&modal.focus_handle(cx)); + window.focus(&modal.focus_handle(cx), cx); modal }); diff --git a/crates/debugger_ui/src/onboarding_modal.rs b/crates/debugger_ui/src/onboarding_modal.rs index 18205209983421691046e8a9d93eb6de32cd4563..b6f1ab944183c4f44d2bc5f6855731abb65ce1f7 100644 --- a/crates/debugger_ui/src/onboarding_modal.rs +++ b/crates/debugger_ui/src/onboarding_modal.rs @@ -83,8 +83,8 @@ impl Render for DebuggerOnboardingModal { debugger_onboarding_event!("Canceled", trigger = "Action"); cx.emit(DismissEvent); })) - .on_any_mouse_down(cx.listener(|this, _: &MouseDownEvent, window, _cx| { - this.focus_handle.focus(window); + .on_any_mouse_down(cx.listener(|this, _: &MouseDownEvent, window, cx| { + this.focus_handle.focus(window, cx); })) .child( div() diff --git a/crates/debugger_ui/src/session/running.rs b/crates/debugger_ui/src/session/running.rs index 4898ec95ca3c5b55669896b3c1d898326851c0c3..422207d3cbf4880e0c8e3c02e01dbe373800ea62 100644 --- a/crates/debugger_ui/src/session/running.rs +++ b/crates/debugger_ui/src/session/running.rs @@ -604,7 +604,7 @@ impl DebugTerminal { let focus_handle = cx.focus_handle(); let focus_subscription = cx.on_focus(&focus_handle, window, |this, window, cx| { if let Some(terminal) = this.terminal.as_ref() { - terminal.focus_handle(cx).focus(window); + terminal.focus_handle(cx).focus(window, cx); } }); diff --git a/crates/debugger_ui/src/session/running/breakpoint_list.rs b/crates/debugger_ui/src/session/running/breakpoint_list.rs index 2c7e2074678290356b7669228dcf29008f1cc36b..f154757429a2bbfe153ee40c2c513dd06f05aa03 100644 --- a/crates/debugger_ui/src/session/running/breakpoint_list.rs +++ b/crates/debugger_ui/src/session/running/breakpoint_list.rs @@ -310,7 +310,7 @@ impl BreakpointList { fn dismiss(&mut self, _: &menu::Cancel, window: &mut Window, cx: &mut Context) { if self.input.focus_handle(cx).contains_focused(window, cx) { - self.focus_handle.focus(window); + self.focus_handle.focus(window, cx); } else if self.strip_mode.is_some() { self.strip_mode.take(); cx.notify(); @@ -364,9 +364,9 @@ impl BreakpointList { } } } - self.focus_handle.focus(window); + self.focus_handle.focus(window, cx); } else { - handle.focus(window); + handle.focus(window, cx); } return; @@ -627,7 +627,7 @@ impl BreakpointList { .on_click({ let focus_handle = focus_handle.clone(); move |_, window, cx| { - focus_handle.focus(window); + focus_handle.focus(window, cx); window.dispatch_action(ToggleEnableBreakpoint.boxed_clone(), cx) } }), @@ -654,7 +654,7 @@ impl BreakpointList { ) .on_click({ move |_, window, cx| { - focus_handle.focus(window); + focus_handle.focus(window, cx); window.dispatch_action(UnsetBreakpoint.boxed_clone(), cx) } }), diff --git a/crates/debugger_ui/src/session/running/console.rs b/crates/debugger_ui/src/session/running/console.rs index 927a57dc8bdf956eb7f7ff63d3ea058500abf6c3..040953bff6e8f0efa6045c1629c964ac98929547 100644 --- a/crates/debugger_ui/src/session/running/console.rs +++ b/crates/debugger_ui/src/session/running/console.rs @@ -105,7 +105,7 @@ impl Console { cx.subscribe(&stack_frame_list, Self::handle_stack_frame_list_events), cx.on_focus(&focus_handle, window, |console, window, cx| { if console.is_running(cx) { - console.query_bar.focus_handle(cx).focus(window); + console.query_bar.focus_handle(cx).focus(window, cx); } }), ]; diff --git a/crates/debugger_ui/src/session/running/memory_view.rs b/crates/debugger_ui/src/session/running/memory_view.rs index 55a8e8429eb23cd0bfcaa7d592d16797c061d2ae..f10e5179e37f87be0e27985b557fcb63cf089a42 100644 --- a/crates/debugger_ui/src/session/running/memory_view.rs +++ b/crates/debugger_ui/src/session/running/memory_view.rs @@ -403,7 +403,7 @@ impl MemoryView { this.set_placeholder_text("Write to Selected Memory Range", window, cx); }); self.is_writing_memory = true; - self.query_editor.focus_handle(cx).focus(window); + self.query_editor.focus_handle(cx).focus(window, cx); } else { self.query_editor.update(cx, |this, cx| { this.clear(window, cx); diff --git a/crates/debugger_ui/src/session/running/variable_list.rs b/crates/debugger_ui/src/session/running/variable_list.rs index 7b23cd685d93e6353d68dc57cd3998099ea56ad7..8329a6baf04061cc33e8130a4e6b3a33b35267b6 100644 --- a/crates/debugger_ui/src/session/running/variable_list.rs +++ b/crates/debugger_ui/src/session/running/variable_list.rs @@ -529,7 +529,7 @@ impl VariableList { fn cancel(&mut self, _: &menu::Cancel, window: &mut Window, cx: &mut Context) { self.edited_path.take(); - self.focus_handle.focus(window); + self.focus_handle.focus(window, cx); cx.notify(); } @@ -1067,7 +1067,7 @@ impl VariableList { editor.select_all(&editor::actions::SelectAll, window, cx); editor }); - editor.focus_handle(cx).focus(window); + editor.focus_handle(cx).focus(window, cx); editor } diff --git a/crates/deepseek/src/deepseek.rs b/crates/deepseek/src/deepseek.rs index e978aa08048bfa4c7b7b203ce6b405ba8a0a7d0c..636258a5a132ce79cb5d15b1aaa25d6e4d3af643 100644 --- a/crates/deepseek/src/deepseek.rs +++ b/crates/deepseek/src/deepseek.rs @@ -103,8 +103,9 @@ impl Model { pub fn max_output_tokens(&self) -> Option { match self { - Self::Chat => Some(8_192), - Self::Reasoner => Some(64_000), + // Their API treats this max against the context window, which means we hit the limit a lot + // Using the default value of None in the API instead + Self::Chat | Self::Reasoner => None, Self::Custom { max_output_tokens, .. } => *max_output_tokens, diff --git a/crates/diagnostics/src/buffer_diagnostics.rs b/crates/diagnostics/src/buffer_diagnostics.rs index ca28f2805adca78846a66e7b1f4d9f3fc57bb557..ba10f6fbdabf05a095a7fed7c6ae682d4dc177c7 100644 --- a/crates/diagnostics/src/buffer_diagnostics.rs +++ b/crates/diagnostics/src/buffer_diagnostics.rs @@ -175,7 +175,7 @@ impl BufferDiagnosticsEditor { // `BufferDiagnosticsEditor` instance. EditorEvent::Focused => { if buffer_diagnostics_editor.multibuffer.read(cx).is_empty() { - window.focus(&buffer_diagnostics_editor.focus_handle); + window.focus(&buffer_diagnostics_editor.focus_handle, cx); } } EditorEvent::Blurred => { @@ -517,7 +517,7 @@ impl BufferDiagnosticsEditor { .editor .read(cx) .focus_handle(cx) - .focus(window); + .focus(window, cx); } } } @@ -617,7 +617,7 @@ impl BufferDiagnosticsEditor { // not empty, focus on the editor instead, which will allow the user to // start interacting and editing the buffer's contents. if self.focus_handle.is_focused(window) && !self.multibuffer.read(cx).is_empty() { - self.editor.focus_handle(cx).focus(window) + self.editor.focus_handle(cx).focus(window, cx) } } diff --git a/crates/diagnostics/src/diagnostic_renderer.rs b/crates/diagnostics/src/diagnostic_renderer.rs index 72ad7b591413832183bb85d58d188e692d46ffad..521752ff1959fccc12b74857e342ff33a0444f3f 100644 --- a/crates/diagnostics/src/diagnostic_renderer.rs +++ b/crates/diagnostics/src/diagnostic_renderer.rs @@ -315,6 +315,6 @@ impl DiagnosticBlock { editor.change_selections(Default::default(), window, cx, |s| { s.select_ranges([range.start..range.start]); }); - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); } } diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index 0999bebdb6aa9ca744e3a5121670a1b7357411a9..d85eb07f68619e15bfe44d26282db3a3e49df4f3 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -243,7 +243,7 @@ impl ProjectDiagnosticsEditor { match event { EditorEvent::Focused => { if this.multibuffer.read(cx).is_empty() { - window.focus(&this.focus_handle); + window.focus(&this.focus_handle, cx); } } EditorEvent::Blurred => this.close_diagnosticless_buffers(cx, false), @@ -434,7 +434,7 @@ impl ProjectDiagnosticsEditor { fn focus_in(&mut self, window: &mut Window, cx: &mut Context) { if self.focus_handle.is_focused(window) && !self.multibuffer.read(cx).is_empty() { - self.editor.focus_handle(cx).focus(window) + self.editor.focus_handle(cx).focus(window, cx) } } @@ -650,7 +650,7 @@ impl ProjectDiagnosticsEditor { }) }); if this.focus_handle.is_focused(window) { - this.editor.read(cx).focus_handle(cx).focus(window); + this.editor.read(cx).focus_handle(cx).focus(window, cx); } } diff --git a/crates/docs_preprocessor/Cargo.toml b/crates/docs_preprocessor/Cargo.toml index e71f9ae3f3f6fcff790db27fb1e377f0d1c20e40..07da23899956822f7577118ae85b6338b4cefae7 100644 --- a/crates/docs_preprocessor/Cargo.toml +++ b/crates/docs_preprocessor/Cargo.toml @@ -7,8 +7,6 @@ license = "GPL-3.0-or-later" [dependencies] anyhow.workspace = true -command_palette.workspace = true -gpui.workspace = true # We are specifically pinning this version of mdbook, as later versions introduce issues with double-nested subdirectories. # Ask @maxdeviant about this before bumping. mdbook = "= 0.4.40" @@ -17,7 +15,6 @@ serde.workspace = true serde_json.workspace = true settings.workspace = true util.workspace = true -zed.workspace = true zlog.workspace = true task.workspace = true theme.workspace = true @@ -27,4 +24,4 @@ workspace = true [[bin]] name = "docs_preprocessor" -path = "src/main.rs" +path = "src/main.rs" \ No newline at end of file diff --git a/crates/docs_preprocessor/src/main.rs b/crates/docs_preprocessor/src/main.rs index b614a8251139413f4b316937db1d4e3c0d551df6..d90dcc10db9fbd8d27a968094ea8d733a79b7e80 100644 --- a/crates/docs_preprocessor/src/main.rs +++ b/crates/docs_preprocessor/src/main.rs @@ -22,16 +22,13 @@ static KEYMAP_WINDOWS: LazyLock = LazyLock::new(|| { load_keymap("keymaps/default-windows.json").expect("Failed to load Windows keymap") }); -static ALL_ACTIONS: LazyLock> = LazyLock::new(dump_all_gpui_actions); +static ALL_ACTIONS: LazyLock> = LazyLock::new(load_all_actions); const FRONT_MATTER_COMMENT: &str = ""; fn main() -> Result<()> { zlog::init(); zlog::init_output_stderr(); - // call a zed:: function so everything in `zed` crate is linked and - // all actions in the actual app are registered - zed::stdout_is_a_pty(); let args = std::env::args().skip(1).collect::>(); match args.get(0).map(String::as_str) { @@ -72,8 +69,8 @@ enum PreprocessorError { impl PreprocessorError { fn new_for_not_found_action(action_name: String) -> Self { for action in &*ALL_ACTIONS { - for alias in action.deprecated_aliases { - if alias == &action_name { + for alias in &action.deprecated_aliases { + if alias == action_name.as_str() { return PreprocessorError::DeprecatedActionUsed { used: action_name, should_be: action.name.to_string(), @@ -214,7 +211,7 @@ fn template_and_validate_keybindings(book: &mut Book, errors: &mut HashSet{}", name); }; format!("{}", &action.human_name) }) @@ -257,11 +256,19 @@ fn template_and_validate_actions(book: &mut Book, errors: &mut HashSet Option<&ActionDef> { ALL_ACTIONS - .binary_search_by(|action| action.name.cmp(name)) + .binary_search_by(|action| action.name.as_str().cmp(name)) .ok() .map(|index| &ALL_ACTIONS[index]) } +fn actions_available() -> bool { + !ALL_ACTIONS.is_empty() +} + +fn is_missing_action(name: &str) -> bool { + actions_available() && find_action_by_name(name).is_none() +} + fn find_binding(os: &str, action: &str) -> Option { let keymap = match os { "macos" => &KEYMAP_MACOS, @@ -384,18 +391,13 @@ fn template_and_validate_json_snippets(book: &mut Book, errors: &mut HashSet
, _>>()
-                            .context("Failed to parse keystroke")?;
+                    for (_keystrokes, action) in section.bindings() {
                         if let Some((action_name, _)) = settings::KeymapFile::parse_action(action)
                             .map_err(|err| anyhow::format_err!(err))
                             .context("Failed to parse action")?
                         {
                             anyhow::ensure!(
-                                find_action_by_name(action_name).is_some(),
+                                !is_missing_action(action_name),
                                 "Action not found: {}",
                                 action_name
                             );
@@ -491,27 +493,35 @@ where
     });
 }
 
-#[derive(Debug, serde::Serialize)]
+#[derive(Debug, serde::Serialize, serde::Deserialize)]
 struct ActionDef {
-    name: &'static str,
+    name: String,
     human_name: String,
-    deprecated_aliases: &'static [&'static str],
-    docs: Option<&'static str>,
+    deprecated_aliases: Vec,
+    #[serde(rename = "documentation")]
+    docs: Option,
 }
 
-fn dump_all_gpui_actions() -> Vec {
-    let mut actions = gpui::generate_list_of_all_registered_actions()
-        .map(|action| ActionDef {
-            name: action.name,
-            human_name: command_palette::humanize_action_name(action.name),
-            deprecated_aliases: action.deprecated_aliases,
-            docs: action.documentation,
-        })
-        .collect::>();
-
-    actions.sort_by_key(|a| a.name);
-
-    actions
+fn load_all_actions() -> Vec {
+    let asset_path = concat!(env!("CARGO_MANIFEST_DIR"), "/actions.json");
+    match std::fs::read_to_string(asset_path) {
+        Ok(content) => {
+            let mut actions: Vec =
+                serde_json::from_str(&content).expect("Failed to parse actions.json");
+            actions.sort_by(|a, b| a.name.cmp(&b.name));
+            actions
+        }
+        Err(err) => {
+            if std::env::var("CI").is_ok() {
+                panic!("actions.json not found at {}: {}", asset_path, err);
+            }
+            eprintln!(
+                "Warning: actions.json not found, action validation will be skipped: {}",
+                err
+            );
+            Vec::new()
+        }
+    }
 }
 
 fn handle_postprocessing() -> Result<()> {
@@ -647,7 +657,7 @@ fn generate_big_table_of_actions() -> String {
     let mut output = String::new();
 
     let mut actions_sorted = actions.iter().collect::>();
-    actions_sorted.sort_by_key(|a| a.name);
+    actions_sorted.sort_by_key(|a| a.name.as_str());
 
     // Start the definition list with custom styling for better spacing
     output.push_str("
\n"); @@ -664,7 +674,7 @@ fn generate_big_table_of_actions() -> String { output.push_str("
\n"); // Add the description, escaping HTML if needed - if let Some(description) = action.docs { + if let Some(description) = action.docs.as_ref() { output.push_str( &description .replace("&", "&") @@ -674,7 +684,7 @@ fn generate_big_table_of_actions() -> String { output.push_str("
\n"); } output.push_str("Keymap Name: "); - output.push_str(action.name); + output.push_str(&action.name); output.push_str("
\n"); if !action.deprecated_aliases.is_empty() { output.push_str("Deprecated Alias(es): "); diff --git a/crates/edit_prediction/src/mercury.rs b/crates/edit_prediction/src/mercury.rs index b47bd2ad0374eba33e7b8db726c2fa13c0519465..8186fc5d8c609468be04c117eabac11c6c015efd 100644 --- a/crates/edit_prediction/src/mercury.rs +++ b/crates/edit_prediction/src/mercury.rs @@ -6,7 +6,7 @@ use crate::{ use anyhow::{Context as _, Result}; use futures::AsyncReadExt as _; use gpui::{ - App, AppContext as _, Entity, SharedString, Task, + App, AppContext as _, Entity, Global, SharedString, Task, http_client::{self, AsyncBody, Method}, }; use language::{OffsetRangeExt as _, ToOffset, ToPoint as _}; @@ -300,14 +300,19 @@ pub const MERCURY_CREDENTIALS_URL: SharedString = SharedString::new_static("https://api.inceptionlabs.ai/v1/edit/completions"); pub const MERCURY_CREDENTIALS_USERNAME: &str = "mercury-api-token"; pub static MERCURY_TOKEN_ENV_VAR: std::sync::LazyLock = env_var!("MERCURY_AI_TOKEN"); -pub static MERCURY_API_KEY: std::sync::OnceLock> = std::sync::OnceLock::new(); + +struct GlobalMercuryApiKey(Entity); + +impl Global for GlobalMercuryApiKey {} pub fn mercury_api_token(cx: &mut App) -> Entity { - MERCURY_API_KEY - .get_or_init(|| { - cx.new(|_| ApiKeyState::new(MERCURY_CREDENTIALS_URL, MERCURY_TOKEN_ENV_VAR.clone())) - }) - .clone() + if let Some(global) = cx.try_global::() { + return global.0.clone(); + } + let entity = + cx.new(|_| ApiKeyState::new(MERCURY_CREDENTIALS_URL, MERCURY_TOKEN_ENV_VAR.clone())); + cx.set_global(GlobalMercuryApiKey(entity.clone())); + entity } pub fn load_mercury_api_token(cx: &mut App) -> Task> { diff --git a/crates/edit_prediction/src/onboarding_modal.rs b/crates/edit_prediction/src/onboarding_modal.rs index ed7adfc75476afb07f9c56b9c9c03abbbcef1134..97f529ae38df350ef21ffc04b79df6e8e6a7a501 100644 --- a/crates/edit_prediction/src/onboarding_modal.rs +++ b/crates/edit_prediction/src/onboarding_modal.rs @@ -131,8 +131,8 @@ impl Render for ZedPredictModal { onboarding_event!("Cancelled", trigger = "Action"); cx.emit(DismissEvent); })) - .on_any_mouse_down(cx.listener(|this, _: &MouseDownEvent, window, _cx| { - this.focus_handle.focus(window); + .on_any_mouse_down(cx.listener(|this, _: &MouseDownEvent, window, cx| { + this.focus_handle.focus(window, cx); })) .child( div() diff --git a/crates/edit_prediction/src/sweep_ai.rs b/crates/edit_prediction/src/sweep_ai.rs index 2ed24cd8ef728383ec800acbb2ab7c7b99f07c06..71f28c9213c3440a9267dab7d5a5416dc219f2f3 100644 --- a/crates/edit_prediction/src/sweep_ai.rs +++ b/crates/edit_prediction/src/sweep_ai.rs @@ -1,7 +1,7 @@ use anyhow::Result; use futures::AsyncReadExt as _; use gpui::{ - App, AppContext as _, Entity, SharedString, Task, + App, AppContext as _, Entity, Global, SharedString, Task, http_client::{self, AsyncBody, Method}, }; use language::{Point, ToOffset as _}; @@ -272,14 +272,19 @@ pub const SWEEP_CREDENTIALS_URL: SharedString = SharedString::new_static("https://autocomplete.sweep.dev"); pub const SWEEP_CREDENTIALS_USERNAME: &str = "sweep-api-token"; pub static SWEEP_AI_TOKEN_ENV_VAR: std::sync::LazyLock = env_var!("SWEEP_AI_TOKEN"); -pub static SWEEP_API_KEY: std::sync::OnceLock> = std::sync::OnceLock::new(); + +struct GlobalSweepApiKey(Entity); + +impl Global for GlobalSweepApiKey {} pub fn sweep_api_token(cx: &mut App) -> Entity { - SWEEP_API_KEY - .get_or_init(|| { - cx.new(|_| ApiKeyState::new(SWEEP_CREDENTIALS_URL, SWEEP_AI_TOKEN_ENV_VAR.clone())) - }) - .clone() + if let Some(global) = cx.try_global::() { + return global.0.clone(); + } + let entity = + cx.new(|_| ApiKeyState::new(SWEEP_CREDENTIALS_URL, SWEEP_AI_TOKEN_ENV_VAR.clone())); + cx.set_global(GlobalSweepApiKey(entity.clone())); + entity } pub fn load_sweep_api_token(cx: &mut App) -> Task> { diff --git a/crates/edit_prediction/src/zed_edit_prediction_delegate.rs b/crates/edit_prediction/src/zed_edit_prediction_delegate.rs index 0a87ca661435de4d22e6f258c30ff406f0deecc2..289bcd76daab2b9a4b82db88b86285e6c7aca00d 100644 --- a/crates/edit_prediction/src/zed_edit_prediction_delegate.rs +++ b/crates/edit_prediction/src/zed_edit_prediction_delegate.rs @@ -2,7 +2,7 @@ use std::{cmp, sync::Arc}; use client::{Client, UserStore}; use cloud_llm_client::EditPredictionRejectReason; -use edit_prediction_types::{DataCollectionState, Direction, EditPredictionDelegate}; +use edit_prediction_types::{DataCollectionState, EditPredictionDelegate}; use gpui::{App, Entity, prelude::*}; use language::{Buffer, ToPoint as _}; use project::Project; @@ -139,15 +139,6 @@ impl EditPredictionDelegate for ZedEditPredictionDelegate { }); } - fn cycle( - &mut self, - _buffer: Entity, - _cursor_position: language::Anchor, - _direction: Direction, - _cx: &mut Context, - ) { - } - fn accept(&mut self, cx: &mut Context) { self.store.update(cx, |store, cx| { store.accept_current_prediction(&self.project, cx); diff --git a/crates/edit_prediction_cli/src/headless.rs b/crates/edit_prediction_cli/src/headless.rs index 489e78d364d0fdbb08b93eab89fd5f91f345f68e..da96e7ef6520e952e2b7696eee6b82c243e90e4e 100644 --- a/crates/edit_prediction_cli/src/headless.rs +++ b/crates/edit_prediction_cli/src/headless.rs @@ -114,7 +114,7 @@ pub fn init(cx: &mut App) -> EpAppState { tx.send(Some(options)).log_err(); }) .detach(); - let node_runtime = NodeRuntime::new(client.http_client(), None, rx, None); + let node_runtime = NodeRuntime::new(client.http_client(), None, rx); let extension_host_proxy = ExtensionHostProxy::global(cx); diff --git a/crates/edit_prediction_types/src/edit_prediction_types.rs b/crates/edit_prediction_types/src/edit_prediction_types.rs index 945cfea4a168af4470d98ca844f311a79de9800a..5a37aba59923598b20becd91f07633e409b2bdb7 100644 --- a/crates/edit_prediction_types/src/edit_prediction_types.rs +++ b/crates/edit_prediction_types/src/edit_prediction_types.rs @@ -95,13 +95,6 @@ pub trait EditPredictionDelegate: 'static + Sized { debounce: bool, cx: &mut Context, ); - fn cycle( - &mut self, - buffer: Entity, - cursor_position: language::Anchor, - direction: Direction, - cx: &mut Context, - ); fn accept(&mut self, cx: &mut Context); fn discard(&mut self, cx: &mut Context); fn did_show(&mut self, _cx: &mut Context) {} @@ -136,13 +129,6 @@ pub trait EditPredictionDelegateHandle { debounce: bool, cx: &mut App, ); - fn cycle( - &self, - buffer: Entity, - cursor_position: language::Anchor, - direction: Direction, - cx: &mut App, - ); fn did_show(&self, cx: &mut App); fn accept(&self, cx: &mut App); fn discard(&self, cx: &mut App); @@ -215,18 +201,6 @@ where }) } - fn cycle( - &self, - buffer: Entity, - cursor_position: language::Anchor, - direction: Direction, - cx: &mut App, - ) { - self.update(cx, |this, cx| { - this.cycle(buffer, cursor_position, direction, cx) - }) - } - fn accept(&self, cx: &mut App) { self.update(cx, |this, cx| this.accept(cx)) } diff --git a/crates/edit_prediction_ui/src/rate_prediction_modal.rs b/crates/edit_prediction_ui/src/rate_prediction_modal.rs index 22e82bc445b394cc122e1cb1aa3604b45c25d1d1..1af65ad58083e3cccfa51ea7b674da01cad810a0 100644 --- a/crates/edit_prediction_ui/src/rate_prediction_modal.rs +++ b/crates/edit_prediction_ui/src/rate_prediction_modal.rs @@ -305,7 +305,7 @@ impl RatePredictionsModal { && prediction.id == prev_prediction.prediction.id { if focus { - window.focus(&prev_prediction.feedback_editor.focus_handle(cx)); + window.focus(&prev_prediction.feedback_editor.focus_handle(cx), cx); } return; } diff --git a/crates/editor/benches/editor_render.rs b/crates/editor/benches/editor_render.rs index 4323c6c973f3729623d8939ca89ecf3ac403bcbf..daaeede790cbd75a7238a81559513c5d3165a054 100644 --- a/crates/editor/benches/editor_render.rs +++ b/crates/editor/benches/editor_render.rs @@ -29,7 +29,7 @@ fn editor_input_with_1000_cursors(bencher: &mut Bencher<'_>, cx: &TestAppContext ); editor }); - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); editor }); @@ -72,7 +72,7 @@ fn open_editor_with_one_long_line(bencher: &mut Bencher<'_>, args: &(String, Tes editor.set_style(editor::EditorStyle::default(), window, cx); editor }); - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); editor }); }); @@ -100,7 +100,7 @@ fn editor_render(bencher: &mut Bencher<'_>, cx: &TestAppContext) { editor.set_style(editor::EditorStyle::default(), window, cx); editor }); - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); editor }); diff --git a/crates/editor/src/bracket_colorization.rs b/crates/editor/src/bracket_colorization.rs index 4879c5e9ce703227d3c03f4d3373512769b1515c..ee7e785ed30a14bce53bb777b67bdf69a9cecd07 100644 --- a/crates/editor/src/bracket_colorization.rs +++ b/crates/editor/src/bracket_colorization.rs @@ -348,6 +348,61 @@ where ); } + #[gpui::test] + async fn test_bracket_colorization_after_language_swap(cx: &mut gpui::TestAppContext) { + init_test(cx, |language_settings| { + language_settings.defaults.colorize_brackets = Some(true); + }); + + let language_registry = Arc::new(language::LanguageRegistry::test(cx.executor())); + language_registry.add(markdown_lang()); + language_registry.add(rust_lang()); + + let mut cx = EditorTestContext::new(cx).await; + cx.update_buffer(|buffer, cx| { + buffer.set_language_registry(language_registry.clone()); + buffer.set_language(Some(markdown_lang()), cx); + }); + + cx.set_state(indoc! {r#" + fn main() { + let v: Vec = vec![]; + } + "#}); + cx.executor().advance_clock(Duration::from_millis(100)); + cx.executor().run_until_parked(); + + assert_eq!( + r#"fn main«1()1» «1{ + let v: Vec = vec!«2[]2»; +}1» + +1 hsla(207.80, 16.20%, 69.19%, 1.00) +2 hsla(29.00, 54.00%, 65.88%, 1.00) +"#, + &bracket_colors_markup(&mut cx), + "Markdown does not colorize <> brackets" + ); + + cx.update_buffer(|buffer, cx| { + buffer.set_language(Some(rust_lang()), cx); + }); + cx.executor().advance_clock(Duration::from_millis(100)); + cx.executor().run_until_parked(); + + assert_eq!( + r#"fn main«1()1» «1{ + let v: Vec«22» = vec!«2[]2»; +}1» + +1 hsla(207.80, 16.20%, 69.19%, 1.00) +2 hsla(29.00, 54.00%, 65.88%, 1.00) +"#, + &bracket_colors_markup(&mut cx), + "After switching to Rust, <> brackets are now colorized" + ); + } + #[gpui::test] async fn test_bracket_colorization_when_editing(cx: &mut gpui::TestAppContext) { init_test(cx, |language_settings| { diff --git a/crates/editor/src/code_context_menus.rs b/crates/editor/src/code_context_menus.rs index d255effdb72a003014dff0805fa34a23d11c8c81..e5520be88e34307220126ebafdba6c6371a5db12 100644 --- a/crates/editor/src/code_context_menus.rs +++ b/crates/editor/src/code_context_menus.rs @@ -51,6 +51,8 @@ pub const MENU_ASIDE_MIN_WIDTH: Pixels = px(260.); pub const MENU_ASIDE_MAX_WIDTH: Pixels = px(500.); pub const COMPLETION_MENU_MIN_WIDTH: Pixels = px(280.); pub const COMPLETION_MENU_MAX_WIDTH: Pixels = px(540.); +pub const CODE_ACTION_MENU_MIN_WIDTH: Pixels = px(220.); +pub const CODE_ACTION_MENU_MAX_WIDTH: Pixels = px(540.); // Constants for the markdown cache. The purpose of this cache is to reduce flickering due to // documentation not yet being parsed. @@ -179,7 +181,7 @@ impl CodeContextMenu { ) -> Option { match self { CodeContextMenu::Completions(menu) => menu.render_aside(max_size, window, cx), - CodeContextMenu::CodeActions(_) => None, + CodeContextMenu::CodeActions(menu) => menu.render_aside(max_size, window, cx), } } @@ -891,7 +893,7 @@ impl CompletionsMenu { None } else { Some( - Label::new(text.clone()) + Label::new(text.trim().to_string()) .ml_4() .size(LabelSize::Small) .color(Color::Muted), @@ -1419,26 +1421,6 @@ pub enum CodeActionsItem { } impl CodeActionsItem { - fn as_task(&self) -> Option<&ResolvedTask> { - let Self::Task(_, task) = self else { - return None; - }; - Some(task) - } - - fn as_code_action(&self) -> Option<&CodeAction> { - let Self::CodeAction { action, .. } = self else { - return None; - }; - Some(action) - } - fn as_debug_scenario(&self) -> Option<&DebugScenario> { - let Self::DebugScenario(scenario) = self else { - return None; - }; - Some(scenario) - } - pub fn label(&self) -> String { match self { Self::CodeAction { action, .. } => action.lsp_action.title().to_owned(), @@ -1446,6 +1428,14 @@ impl CodeActionsItem { Self::DebugScenario(scenario) => scenario.label.to_string(), } } + + pub fn menu_label(&self) -> String { + match self { + Self::CodeAction { action, .. } => action.lsp_action.title().replace("\n", ""), + Self::Task(_, task) => task.resolved_label.replace("\n", ""), + Self::DebugScenario(scenario) => format!("debug: {}", scenario.label), + } + } } pub struct CodeActionsMenu { @@ -1555,60 +1545,33 @@ impl CodeActionsMenu { let item_ix = range.start + ix; let selected = item_ix == selected_item; let colors = cx.theme().colors(); - div().min_w(px(220.)).max_w(px(540.)).child( - ListItem::new(item_ix) - .inset(true) - .toggle_state(selected) - .when_some(action.as_code_action(), |this, action| { - this.child( - h_flex() - .overflow_hidden() - .when(is_quick_action_bar, |this| this.text_ui(cx)) - .child( - // TASK: It would be good to make lsp_action.title a SharedString to avoid allocating here. - action.lsp_action.title().replace("\n", ""), - ) - .when(selected, |this| { - this.text_color(colors.text_accent) - }), - ) - }) - .when_some(action.as_task(), |this, task| { - this.child( - h_flex() - .overflow_hidden() - .when(is_quick_action_bar, |this| this.text_ui(cx)) - .child(task.resolved_label.replace("\n", "")) - .when(selected, |this| { - this.text_color(colors.text_accent) - }), - ) - }) - .when_some(action.as_debug_scenario(), |this, scenario| { - this.child( - h_flex() - .overflow_hidden() - .when(is_quick_action_bar, |this| this.text_ui(cx)) - .child("debug: ") - .child(scenario.label.clone()) - .when(selected, |this| { - this.text_color(colors.text_accent) - }), - ) - }) - .on_click(cx.listener(move |editor, _, window, cx| { - cx.stop_propagation(); - if let Some(task) = editor.confirm_code_action( - &ConfirmCodeAction { - item_ix: Some(item_ix), - }, - window, - cx, - ) { - task.detach_and_log_err(cx) - } - })), - ) + + ListItem::new(item_ix) + .inset(true) + .toggle_state(selected) + .overflow_x() + .child( + div() + .min_w(CODE_ACTION_MENU_MIN_WIDTH) + .max_w(CODE_ACTION_MENU_MAX_WIDTH) + .overflow_hidden() + .text_ellipsis() + .when(is_quick_action_bar, |this| this.text_ui(cx)) + .when(selected, |this| this.text_color(colors.text_accent)) + .child(action.menu_label()), + ) + .on_click(cx.listener(move |editor, _, window, cx| { + cx.stop_propagation(); + if let Some(task) = editor.confirm_code_action( + &ConfirmCodeAction { + item_ix: Some(item_ix), + }, + window, + cx, + ) { + task.detach_and_log_err(cx) + } + })) }) .collect() }), @@ -1635,4 +1598,46 @@ impl CodeActionsMenu { Popover::new().child(list).into_any_element() } + + fn render_aside( + &mut self, + max_size: Size, + window: &mut Window, + _cx: &mut Context, + ) -> Option { + let Some(action) = self.actions.get(self.selected_item) else { + return None; + }; + + let label = action.menu_label(); + let text_system = window.text_system(); + let mut line_wrapper = text_system.line_wrapper( + window.text_style().font(), + window.text_style().font_size.to_pixels(window.rem_size()), + ); + let is_truncated = line_wrapper.should_truncate_line( + &label, + CODE_ACTION_MENU_MAX_WIDTH, + "…", + gpui::TruncateFrom::End, + ); + + if is_truncated.is_none() { + return None; + } + + Some( + Popover::new() + .child( + div() + .child(label) + .id("code_actions_menu_extended") + .px(MENU_ASIDE_X_PADDING / 2.) + .max_w(max_size.width) + .max_h(max_size.height) + .occlude(), + ) + .into_any_element(), + ) + } } diff --git a/crates/editor/src/display_map.rs b/crates/editor/src/display_map.rs index cab5b3686ee2f77dade059b434b1090cf9b2f7e5..413766cb283dfa2c5de0351b3ff10ff9b90a9c56 100644 --- a/crates/editor/src/display_map.rs +++ b/crates/editor/src/display_map.rs @@ -14,8 +14,57 @@ //! - [`DisplayMap`] that adds background highlights to the regions of text. //! Each one of those builds on top of preceding map. //! +//! ## Structure of the display map layers +//! +//! Each layer in the map (and the multibuffer itself to some extent) has a few +//! structures that are used to implement the public API available to the layer +//! above: +//! - a `Transform` type - this represents a region of text that the layer in +//! question is "managing", that it transforms into a more "processed" text +//! for the layer above. For example, the inlay map has an `enum Transform` +//! that has two variants: +//! - `Isomorphic`, representing a region of text that has no inlay hints (i.e. +//! is passed through the map transparently) +//! - `Inlay`, representing a location where an inlay hint is to be inserted. +//! - a `TransformSummary` type, which is usually a struct with two fields: +//! [`input: TextSummary`][`TextSummary`] and [`output: TextSummary`][`TextSummary`]. Here, +//! `input` corresponds to "text in the layer below", and `output` corresponds to the text +//! exposed to the layer above. So in the inlay map case, a `Transform::Isomorphic`'s summary is +//! just `input = output = summary`, where `summary` is the [`TextSummary`] stored in that +//! variant. Conversely, a `Transform::Inlay` always has an empty `input` summary, because it's +//! not "replacing" any text that exists on disk. The `output` is the summary of the inlay text +//! to be injected. - Various newtype wrappers for co-ordinate spaces (e.g. [`WrapRow`] +//! represents a row index, after soft-wrapping (and all lower layers)). +//! - A `Snapshot` type (e.g. [`InlaySnapshot`]) that captures the state of a layer at a specific +//! point in time. +//! - various APIs which drill through the layers below to work with the underlying text. Notably: +//! - `fn text_summary_for_offset()` returns a [`TextSummary`] for the range in the co-ordinate +//! space that the map in question is responsible for. +//! - `fn _point_to__point()` converts a point in co-ordinate space `A` into co-ordinate +//! space `B`. +//! - A [`RowInfo`] iterator (e.g. [`InlayBufferRows`]) and a [`Chunk`] iterator +//! (e.g. [`InlayChunks`]) +//! - A `sync` function (e.g. [`InlayMap::sync`]) that takes a snapshot and list of [`Edit`]s, +//! and returns a new snapshot and a list of transformed [`Edit`]s. Note that the generic +//! parameter on `Edit` changes, since these methods take in edits in the co-ordinate space of +//! the lower layer, and return edits in their own co-ordinate space. The term "edit" is +//! slightly misleading, since an [`Edit`] doesn't tell you what changed - rather it can be +//! thought of as a "region to invalidate". In theory, it would be correct to always use a +//! single edit that covers the entire range. However, this would lead to lots of unnecessary +//! recalculation. +//! +//! See the docs for the [`inlay_map`] module for a more in-depth explanation of how a single layer +//! works. +//! //! [Editor]: crate::Editor //! [EditorElement]: crate::element::EditorElement +//! [`TextSummary`]: multi_buffer::MBTextSummary +//! [`WrapRow`]: wrap_map::WrapRow +//! [`InlayBufferRows`]: inlay_map::InlayBufferRows +//! [`InlayChunks`]: inlay_map::InlayChunks +//! [`Edit`]: text::Edit +//! [`Edit`]: text::Edit +//! [`Chunk`]: language::Chunk #[macro_use] mod dimensions; diff --git a/crates/editor/src/display_map/block_map.rs b/crates/editor/src/display_map/block_map.rs index 9c7f9d8632224208248a6585fc6f94939ee076fe..15bf012cd907da2455c1a2205bcccd363162fd46 100644 --- a/crates/editor/src/display_map/block_map.rs +++ b/crates/editor/src/display_map/block_map.rs @@ -545,7 +545,7 @@ impl BlockMap { { let max_point = wrap_snapshot.max_point(); let edit_start = wrap_snapshot.prev_row_boundary(max_point); - let edit_end = max_point.row() + WrapRow(1); + let edit_end = max_point.row() + WrapRow(1); // this is end of file edits = edits.compose([WrapEdit { old: edit_start..edit_end, new: edit_start..edit_end, @@ -715,6 +715,7 @@ impl BlockMap { let placement = block.placement.to_wrap_row(wrap_snapshot)?; if let BlockPlacement::Above(row) = placement && row < new_start + // this will be true more often now { return None; } diff --git a/crates/editor/src/display_map/inlay_map.rs b/crates/editor/src/display_map/inlay_map.rs index d85f761a82e2f466b6868c4ce28bcb3a4e6b061d..cbdc4b18fee452163c5a11932c968cb7cc500f96 100644 --- a/crates/editor/src/display_map/inlay_map.rs +++ b/crates/editor/src/display_map/inlay_map.rs @@ -1,3 +1,10 @@ +//! The inlay map. See the [`display_map`][super] docs for an overview of how the inlay map fits +//! into the rest of the [`DisplayMap`][super::DisplayMap]. Much of the documentation for this +//! module generalizes to other layers. +//! +//! The core of this module is the [`InlayMap`] struct, which maintains a vec of [`Inlay`]s, and +//! [`InlaySnapshot`], which holds a sum tree of [`Transform`]s. + use crate::{ ChunkRenderer, HighlightStyles, inlays::{Inlay, InlayContent}, @@ -69,7 +76,9 @@ impl sum_tree::Item for Transform { #[derive(Clone, Debug, Default)] struct TransformSummary { + /// Summary of the text before inlays have been applied. input: MBTextSummary, + /// Summary of the text after inlays have been applied. output: MBTextSummary, } diff --git a/crates/editor/src/display_map/wrap_map.rs b/crates/editor/src/display_map/wrap_map.rs index 4d6b79d06170a22aaffafa05e0f144219e4d20a7..879ca11be1a84ffd44daa6e53677b06887172026 100644 --- a/crates/editor/src/display_map/wrap_map.rs +++ b/crates/editor/src/display_map/wrap_map.rs @@ -840,35 +840,62 @@ impl WrapSnapshot { self.tab_point_to_wrap_point(self.tab_snapshot.clip_point(self.to_tab_point(point), bias)) } - #[ztracing::instrument(skip_all, fields(point=?point, ret))] - pub fn prev_row_boundary(&self, mut point: WrapPoint) -> WrapRow { + /// Try to find a TabRow start that is also a WrapRow start + /// Every TabRow start is a WrapRow start + #[ztracing::instrument(skip_all, fields(point=?point))] + pub fn prev_row_boundary(&self, point: WrapPoint) -> WrapRow { if self.transforms.is_empty() { return WrapRow(0); } - *point.column_mut() = 0; + let point = WrapPoint::new(point.row(), 0); let mut cursor = self .transforms .cursor::>(()); - // start + cursor.seek(&point, Bias::Right); - // end if cursor.item().is_none() { cursor.prev(); } - // start + // real newline fake fake + // text: helloworldasldlfjasd\njdlasfalsk\naskdjfasdkfj\n + // dimensions v v v v v + // transforms |-------|-----NW----|-----W------|-----W------| + // cursor ^ ^^^^^^^^^^^^^ ^ + // (^) ^^^^^^^^^^^^^^ + // point: ^ + // point(col_zero): (^) + while let Some(transform) = cursor.item() { - if transform.is_isomorphic() && cursor.start().1.column() == 0 { - return cmp::min(cursor.end().0.row(), point.row()); - } else { - cursor.prev(); + if transform.is_isomorphic() { + // this transform only has real linefeeds + let tab_summary = &transform.summary.input; + // is the wrap just before the end of the transform a tab row? + // thats only if this transform has at least one newline + // + // "this wrap row is a tab row" <=> self.to_tab_point(WrapPoint::new(wrap_row, 0)).column() == 0 + + // Note on comparison: + // We have code that relies on this to be row > 1 + // It should work with row >= 1 but it does not :( + // + // That means that if every line is wrapped we walk back all the + // way to the start. Which invalidates the entire state triggering + // a full re-render. + if tab_summary.lines.row > 1 { + let wrap_point_at_end = cursor.end().0.row(); + return cmp::min(wrap_point_at_end - RowDelta(1), point.row()); + } else if cursor.start().1.column() == 0 { + return cmp::min(cursor.end().0.row(), point.row()); + } } + + cursor.prev(); } - // end - unreachable!() + WrapRow(0) } #[ztracing::instrument(skip_all)] @@ -891,13 +918,11 @@ impl WrapSnapshot { } #[cfg(test)] - #[ztracing::instrument(skip_all)] pub fn text(&self) -> String { self.text_chunks(WrapRow(0)).collect() } #[cfg(test)] - #[ztracing::instrument(skip_all)] pub fn text_chunks(&self, wrap_row: WrapRow) -> impl Iterator { self.chunks( wrap_row..self.max_point().row() + WrapRow(1), @@ -1298,6 +1323,71 @@ mod tests { use text::Rope; use theme::LoadThemes; + #[gpui::test] + async fn test_prev_row_boundary(cx: &mut gpui::TestAppContext) { + init_test(cx); + + fn test_wrap_snapshot( + text: &str, + soft_wrap_every: usize, // font size multiple + cx: &mut gpui::TestAppContext, + ) -> WrapSnapshot { + let text_system = cx.read(|cx| cx.text_system().clone()); + let tab_size = 4.try_into().unwrap(); + let font = test_font(); + let _font_id = text_system.resolve_font(&font); + let font_size = px(14.0); + // this is very much an estimate to try and get the wrapping to + // occur at `soft_wrap_every` we check that it pans out for every test case + let soft_wrapping = Some(font_size * soft_wrap_every * 0.6); + + let buffer = cx.new(|cx| language::Buffer::local(text, cx)); + let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx)); + let buffer_snapshot = buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx)); + let (_inlay_map, inlay_snapshot) = InlayMap::new(buffer_snapshot); + let (_fold_map, fold_snapshot) = FoldMap::new(inlay_snapshot); + let (mut tab_map, _) = TabMap::new(fold_snapshot, tab_size); + let tabs_snapshot = tab_map.set_max_expansion_column(32); + let (_wrap_map, wrap_snapshot) = + cx.update(|cx| WrapMap::new(tabs_snapshot, font, font_size, soft_wrapping, cx)); + + wrap_snapshot + } + + // These two should pass but dont, see the comparison note in + // prev_row_boundary about why. + // + // // 0123 4567 wrap_rows + // let wrap_snapshot = test_wrap_snapshot("1234\n5678", 1, cx); + // assert_eq!(wrap_snapshot.text(), "1\n2\n3\n4\n5\n6\n7\n8"); + // let row = wrap_snapshot.prev_row_boundary(wrap_snapshot.max_point()); + // assert_eq!(row.0, 3); + + // // 012 345 678 wrap_rows + // let wrap_snapshot = test_wrap_snapshot("123\n456\n789", 1, cx); + // assert_eq!(wrap_snapshot.text(), "1\n2\n3\n4\n5\n6\n7\n8\n9"); + // let row = wrap_snapshot.prev_row_boundary(wrap_snapshot.max_point()); + // assert_eq!(row.0, 5); + + // 012345678 wrap_rows + let wrap_snapshot = test_wrap_snapshot("123456789", 1, cx); + assert_eq!(wrap_snapshot.text(), "1\n2\n3\n4\n5\n6\n7\n8\n9"); + let row = wrap_snapshot.prev_row_boundary(wrap_snapshot.max_point()); + assert_eq!(row.0, 0); + + // 111 2222 44 wrap_rows + let wrap_snapshot = test_wrap_snapshot("123\n4567\n\n89", 4, cx); + assert_eq!(wrap_snapshot.text(), "123\n4567\n\n89"); + let row = wrap_snapshot.prev_row_boundary(wrap_snapshot.max_point()); + assert_eq!(row.0, 2); + + // 11 2223 wrap_rows + let wrap_snapshot = test_wrap_snapshot("12\n3456\n\n", 3, cx); + assert_eq!(wrap_snapshot.text(), "12\n345\n6\n\n"); + let row = wrap_snapshot.prev_row_boundary(wrap_snapshot.max_point()); + assert_eq!(row.0, 3); + } + #[gpui::test(iterations = 100)] async fn test_random_wraps(cx: &mut gpui::TestAppContext, mut rng: StdRng) { // todo this test is flaky diff --git a/crates/editor/src/edit_prediction_tests.rs b/crates/editor/src/edit_prediction_tests.rs index bfce1532ce78699e1fb524fd594df1ba83c864a5..b5931cde42a4e2c0e21b2d1f68558879de9750b4 100644 --- a/crates/editor/src/edit_prediction_tests.rs +++ b/crates/editor/src/edit_prediction_tests.rs @@ -485,15 +485,6 @@ impl EditPredictionDelegate for FakeEditPredictionDelegate { ) { } - fn cycle( - &mut self, - _buffer: gpui::Entity, - _cursor_position: language::Anchor, - _direction: edit_prediction_types::Direction, - _cx: &mut gpui::Context, - ) { - } - fn accept(&mut self, _cx: &mut gpui::Context) {} fn discard(&mut self, _cx: &mut gpui::Context) {} @@ -561,15 +552,6 @@ impl EditPredictionDelegate for FakeNonZedEditPredictionDelegate { ) { } - fn cycle( - &mut self, - _buffer: gpui::Entity, - _cursor_position: language::Anchor, - _direction: edit_prediction_types::Direction, - _cx: &mut gpui::Context, - ) { - } - fn accept(&mut self, _cx: &mut gpui::Context) {} fn discard(&mut self, _cx: &mut gpui::Context) {} diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 4072b1db7a1935e5dbb9c63d2a3aa19db270f131..6e4744335b8e9fba50a6c2c8b241607b0e05d276 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -73,11 +73,7 @@ pub use multi_buffer::{ pub use split::SplittableEditor; pub use text::Bias; -use ::git::{ - Restore, - blame::{BlameEntry, ParsedCommitMessage}, - status::FileStatus, -}; +use ::git::{Restore, blame::BlameEntry, commit::ParsedCommitMessage, status::FileStatus}; use aho_corasick::{AhoCorasick, AhoCorasickBuilder, BuildError}; use anyhow::{Context as _, Result, anyhow, bail}; use blink_manager::BlinkManager; @@ -124,8 +120,9 @@ use language::{ AutoindentMode, BlockCommentConfig, BracketMatch, BracketPair, Buffer, BufferRow, BufferSnapshot, Capability, CharClassifier, CharKind, CharScopeContext, CodeLabel, CursorShape, DiagnosticEntryRef, DiffOptions, EditPredictionsMode, EditPreview, HighlightedText, IndentKind, - IndentSize, Language, LanguageName, LanguageRegistry, OffsetRangeExt, OutlineItem, Point, - Runnable, Selection, SelectionGoal, TextObject, TransactionId, TreeSitterOptions, WordsQuery, + IndentSize, Language, LanguageName, LanguageRegistry, LanguageScope, OffsetRangeExt, + OutlineItem, Point, Runnable, Selection, SelectionGoal, TextObject, TransactionId, + TreeSitterOptions, WordsQuery, language_settings::{ self, LanguageSettings, LspInsertMode, RewrapBehavior, WordsCompletionMode, all_language_settings, language_settings, @@ -166,6 +163,7 @@ use project::{ project_settings::{DiagnosticSeverity, GoToDiagnosticSeverityFilter, ProjectSettings}, }; use rand::seq::SliceRandom; +use regex::Regex; use rpc::{ErrorCode, ErrorExt, proto::PeerId}; use scroll::{Autoscroll, OngoingScroll, ScrollAnchor, ScrollManager}; use selections_collection::{MutableSelectionsCollection, SelectionsCollection}; @@ -2063,46 +2061,34 @@ impl Editor { }) }); }); - let edited_buffers_already_open = { - let other_editors: Vec> = workspace - .read(cx) - .panes() - .iter() - .flat_map(|pane| pane.read(cx).items_of_type::()) - .filter(|editor| editor.entity_id() != cx.entity_id()) - .collect(); - - transaction.0.keys().all(|buffer| { - other_editors.iter().any(|editor| { - let multi_buffer = editor.read(cx).buffer(); - multi_buffer.read(cx).is_singleton() - && multi_buffer.read(cx).as_singleton().map_or( - false, - |singleton| { - singleton.entity_id() == buffer.entity_id() - }, - ) - }) - }) - }; - if !edited_buffers_already_open { - let workspace = workspace.downgrade(); - let transaction = transaction.clone(); - cx.defer_in(window, move |_, window, cx| { - cx.spawn_in(window, async move |editor, cx| { - Self::open_project_transaction( - &editor, - workspace, - transaction, - "Rename".to_string(), - cx, - ) - .await - .ok() - }) - .detach(); - }); - } + + Self::open_transaction_for_hidden_buffers( + workspace, + transaction.clone(), + "Rename".to_string(), + window, + cx, + ); + } + } + + project::Event::WorkspaceEditApplied(transaction) => { + let Some(workspace) = editor.workspace() else { + return; + }; + let Some(active_editor) = workspace.read(cx).active_item_as::(cx) + else { + return; + }; + + if active_editor.entity_id() == cx.entity_id() { + Self::open_transaction_for_hidden_buffers( + workspace, + transaction.clone(), + "LSP Edit".to_string(), + window, + cx, + ); } } @@ -3827,7 +3813,7 @@ impl Editor { ) { if !self.focus_handle.is_focused(window) { self.last_focused_descendant = None; - window.focus(&self.focus_handle); + window.focus(&self.focus_handle, cx); } let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); @@ -3932,7 +3918,7 @@ impl Editor { ) { if !self.focus_handle.is_focused(window) { self.last_focused_descendant = None; - window.focus(&self.focus_handle); + window.focus(&self.focus_handle, cx); } let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); @@ -4802,18 +4788,27 @@ impl Editor { let end = selection.end; let selection_is_empty = start == end; let language_scope = buffer.language_scope_at(start); - let ( - comment_delimiter, - doc_delimiter, - insert_extra_newline, - indent_on_newline, - indent_on_extra_newline, - ) = if let Some(language) = &language_scope { - let mut insert_extra_newline = - insert_extra_newline_brackets(&buffer, start..end, language) - || insert_extra_newline_tree_sitter(&buffer, start..end); - - // Comment extension on newline is allowed only for cursor selections + let (delimiter, newline_config) = if let Some(language) = &language_scope { + let needs_extra_newline = NewlineConfig::insert_extra_newline_brackets( + &buffer, + start..end, + language, + ) + || NewlineConfig::insert_extra_newline_tree_sitter( + &buffer, + start..end, + ); + + let mut newline_config = NewlineConfig::Newline { + additional_indent: IndentSize::spaces(0), + extra_line_additional_indent: if needs_extra_newline { + Some(IndentSize::spaces(0)) + } else { + None + }, + prevent_auto_indent: false, + }; + let comment_delimiter = maybe!({ if !selection_is_empty { return None; @@ -4823,63 +4818,13 @@ impl Editor { return None; } - let delimiters = language.line_comment_prefixes(); - let max_len_of_delimiter = - delimiters.iter().map(|delimiter| delimiter.len()).max()?; - let (snapshot, range) = - buffer.buffer_line_for_row(MultiBufferRow(start_point.row))?; - - let num_of_whitespaces = snapshot - .chars_for_range(range.clone()) - .take_while(|c| c.is_whitespace()) - .count(); - let comment_candidate = snapshot - .chars_for_range(range.clone()) - .skip(num_of_whitespaces) - .take(max_len_of_delimiter) - .collect::(); - let (delimiter, trimmed_len) = delimiters - .iter() - .filter_map(|delimiter| { - let prefix = delimiter.trim_end(); - if comment_candidate.starts_with(prefix) { - Some((delimiter, prefix.len())) - } else { - None - } - }) - .max_by_key(|(_, len)| *len)?; - - if let Some(BlockCommentConfig { - start: block_start, .. - }) = language.block_comment() - { - let block_start_trimmed = block_start.trim_end(); - if block_start_trimmed.starts_with(delimiter.trim_end()) { - let line_content = snapshot - .chars_for_range(range) - .skip(num_of_whitespaces) - .take(block_start_trimmed.len()) - .collect::(); - - if line_content.starts_with(block_start_trimmed) { - return None; - } - } - } - - let cursor_is_placed_after_comment_marker = - num_of_whitespaces + trimmed_len <= start_point.column as usize; - if cursor_is_placed_after_comment_marker { - Some(delimiter.clone()) - } else { - None - } + return comment_delimiter_for_newline( + &start_point, + &buffer, + language, + ); }); - let mut indent_on_newline = IndentSize::spaces(0); - let mut indent_on_extra_newline = IndentSize::spaces(0); - let doc_delimiter = maybe!({ if !selection_is_empty { return None; @@ -4889,149 +4834,100 @@ impl Editor { return None; } - let BlockCommentConfig { - start: start_tag, - end: end_tag, - prefix: delimiter, - tab_size: len, - } = language.documentation_comment()?; - let is_within_block_comment = buffer - .language_scope_at(start_point) - .is_some_and(|scope| scope.override_name() == Some("comment")); - if !is_within_block_comment { + return documentation_delimiter_for_newline( + &start_point, + &buffer, + language, + &mut newline_config, + ); + }); + + let list_delimiter = maybe!({ + if !selection_is_empty { return None; } - let (snapshot, range) = - buffer.buffer_line_for_row(MultiBufferRow(start_point.row))?; - - let num_of_whitespaces = snapshot - .chars_for_range(range.clone()) - .take_while(|c| c.is_whitespace()) - .count(); - - // It is safe to use a column from MultiBufferPoint in context of a single buffer ranges, because we're only ever looking at a single line at a time. - let column = start_point.column; - let cursor_is_after_start_tag = { - let start_tag_len = start_tag.len(); - let start_tag_line = snapshot - .chars_for_range(range.clone()) - .skip(num_of_whitespaces) - .take(start_tag_len) - .collect::(); - if start_tag_line.starts_with(start_tag.as_ref()) { - num_of_whitespaces + start_tag_len <= column as usize - } else { - false - } - }; - - let cursor_is_after_delimiter = { - let delimiter_trim = delimiter.trim_end(); - let delimiter_line = snapshot - .chars_for_range(range.clone()) - .skip(num_of_whitespaces) - .take(delimiter_trim.len()) - .collect::(); - if delimiter_line.starts_with(delimiter_trim) { - num_of_whitespaces + delimiter_trim.len() <= column as usize - } else { - false - } - }; - - let cursor_is_before_end_tag_if_exists = { - let mut char_position = 0u32; - let mut end_tag_offset = None; - - 'outer: for chunk in snapshot.text_for_range(range) { - if let Some(byte_pos) = chunk.find(&**end_tag) { - let chars_before_match = - chunk[..byte_pos].chars().count() as u32; - end_tag_offset = - Some(char_position + chars_before_match); - break 'outer; - } - char_position += chunk.chars().count() as u32; - } - - if let Some(end_tag_offset) = end_tag_offset { - let cursor_is_before_end_tag = column <= end_tag_offset; - if cursor_is_after_start_tag { - if cursor_is_before_end_tag { - insert_extra_newline = true; - } - let cursor_is_at_start_of_end_tag = - column == end_tag_offset; - if cursor_is_at_start_of_end_tag { - indent_on_extra_newline.len = *len; - } - } - cursor_is_before_end_tag - } else { - true - } - }; - - if (cursor_is_after_start_tag || cursor_is_after_delimiter) - && cursor_is_before_end_tag_if_exists - { - if cursor_is_after_start_tag { - indent_on_newline.len = *len; - } - Some(delimiter.clone()) - } else { - None + if !multi_buffer.language_settings(cx).extend_list_on_newline { + return None; } + + return list_delimiter_for_newline( + &start_point, + &buffer, + language, + &mut newline_config, + ); }); ( - comment_delimiter, - doc_delimiter, - insert_extra_newline, - indent_on_newline, - indent_on_extra_newline, + comment_delimiter.or(doc_delimiter).or(list_delimiter), + newline_config, ) } else { ( None, - None, - false, - IndentSize::default(), - IndentSize::default(), + NewlineConfig::Newline { + additional_indent: IndentSize::spaces(0), + extra_line_additional_indent: None, + prevent_auto_indent: false, + }, ) }; - let prevent_auto_indent = doc_delimiter.is_some(); - let delimiter = comment_delimiter.or(doc_delimiter); - - let capacity_for_delimiter = - delimiter.as_deref().map(str::len).unwrap_or_default(); - let mut new_text = String::with_capacity( - 1 + capacity_for_delimiter - + existing_indent.len as usize - + indent_on_newline.len as usize - + indent_on_extra_newline.len as usize, - ); - new_text.push('\n'); - new_text.extend(existing_indent.chars()); - new_text.extend(indent_on_newline.chars()); - - if let Some(delimiter) = &delimiter { - new_text.push_str(delimiter); - } - - if insert_extra_newline { - new_text.push('\n'); - new_text.extend(existing_indent.chars()); - new_text.extend(indent_on_extra_newline.chars()); - } + let (edit_start, new_text, prevent_auto_indent) = match &newline_config { + NewlineConfig::ClearCurrentLine => { + let row_start = + buffer.point_to_offset(Point::new(start_point.row, 0)); + (row_start, String::new(), false) + } + NewlineConfig::UnindentCurrentLine { continuation } => { + let row_start = + buffer.point_to_offset(Point::new(start_point.row, 0)); + let tab_size = buffer.language_settings_at(start, cx).tab_size; + let tab_size_indent = IndentSize::spaces(tab_size.get()); + let reduced_indent = + existing_indent.with_delta(Ordering::Less, tab_size_indent); + let mut new_text = String::new(); + new_text.extend(reduced_indent.chars()); + new_text.push_str(continuation); + (row_start, new_text, true) + } + NewlineConfig::Newline { + additional_indent, + extra_line_additional_indent, + prevent_auto_indent, + } => { + let capacity_for_delimiter = + delimiter.as_deref().map(str::len).unwrap_or_default(); + let extra_line_len = extra_line_additional_indent + .map(|i| 1 + existing_indent.len as usize + i.len as usize) + .unwrap_or(0); + let mut new_text = String::with_capacity( + 1 + capacity_for_delimiter + + existing_indent.len as usize + + additional_indent.len as usize + + extra_line_len, + ); + new_text.push('\n'); + new_text.extend(existing_indent.chars()); + new_text.extend(additional_indent.chars()); + if let Some(delimiter) = &delimiter { + new_text.push_str(delimiter); + } + if let Some(extra_indent) = extra_line_additional_indent { + new_text.push('\n'); + new_text.extend(existing_indent.chars()); + new_text.extend(extra_indent.chars()); + } + (start, new_text, *prevent_auto_indent) + } + }; let anchor = buffer.anchor_after(end); let new_selection = selection.map(|_| anchor); ( - ((start..end, new_text), prevent_auto_indent), - (insert_extra_newline, new_selection), + ((edit_start..end, new_text), prevent_auto_indent), + (newline_config.has_extra_line(), new_selection), ) }) .unzip() @@ -6672,6 +6568,52 @@ impl Editor { } } + fn open_transaction_for_hidden_buffers( + workspace: Entity, + transaction: ProjectTransaction, + title: String, + window: &mut Window, + cx: &mut Context, + ) { + if transaction.0.is_empty() { + return; + } + + let edited_buffers_already_open = { + let other_editors: Vec> = workspace + .read(cx) + .panes() + .iter() + .flat_map(|pane| pane.read(cx).items_of_type::()) + .filter(|editor| editor.entity_id() != cx.entity_id()) + .collect(); + + transaction.0.keys().all(|buffer| { + other_editors.iter().any(|editor| { + let multi_buffer = editor.read(cx).buffer(); + multi_buffer.read(cx).is_singleton() + && multi_buffer + .read(cx) + .as_singleton() + .map_or(false, |singleton| { + singleton.entity_id() == buffer.entity_id() + }) + }) + }) + }; + if !edited_buffers_already_open { + let workspace = workspace.downgrade(); + cx.defer_in(window, move |_, window, cx| { + cx.spawn_in(window, async move |editor, cx| { + Self::open_project_transaction(&editor, workspace, transaction, title, cx) + .await + .ok() + }) + .detach(); + }); + } + } + pub async fn open_project_transaction( editor: &WeakEntity, workspace: WeakEntity, @@ -6831,7 +6773,7 @@ impl Editor { }) }) .on_click(cx.listener(move |editor, _: &ClickEvent, window, cx| { - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); editor.toggle_code_actions( &crate::actions::ToggleCodeActions { deployed_from: Some(crate::actions::CodeActionSource::Indicator( @@ -7587,26 +7529,6 @@ impl Editor { .unwrap_or(false) } - fn cycle_edit_prediction( - &mut self, - direction: Direction, - window: &mut Window, - cx: &mut Context, - ) -> Option<()> { - let provider = self.edit_prediction_provider()?; - let cursor = self.selections.newest_anchor().head(); - let (buffer, cursor_buffer_position) = - self.buffer.read(cx).text_anchor_for_position(cursor, cx)?; - if self.edit_predictions_hidden_for_vim_mode || !self.should_show_edit_predictions() { - return None; - } - - provider.cycle(buffer, cursor_buffer_position, direction, cx); - self.update_visible_edit_prediction(window, cx); - - Some(()) - } - pub fn show_edit_prediction( &mut self, _: &ShowEditPrediction, @@ -7644,42 +7566,6 @@ impl Editor { .detach(); } - pub fn next_edit_prediction( - &mut self, - _: &NextEditPrediction, - window: &mut Window, - cx: &mut Context, - ) { - if self.has_active_edit_prediction() { - self.cycle_edit_prediction(Direction::Next, window, cx); - } else { - let is_copilot_disabled = self - .refresh_edit_prediction(false, true, window, cx) - .is_none(); - if is_copilot_disabled { - cx.propagate(); - } - } - } - - pub fn previous_edit_prediction( - &mut self, - _: &PreviousEditPrediction, - window: &mut Window, - cx: &mut Context, - ) { - if self.has_active_edit_prediction() { - self.cycle_edit_prediction(Direction::Prev, window, cx); - } else { - let is_copilot_disabled = self - .refresh_edit_prediction(false, true, window, cx) - .is_none(); - if is_copilot_disabled { - cx.propagate(); - } - } - } - pub fn accept_partial_edit_prediction( &mut self, granularity: EditPredictionGranularity, @@ -8724,7 +8610,7 @@ impl Editor { BreakpointEditAction::Toggle }; - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); editor.edit_breakpoint_at_anchor( position, breakpoint.as_ref().clone(), @@ -8916,7 +8802,7 @@ impl Editor { ClickEvent::Mouse(e) => e.down.button == MouseButton::Left, }; - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); editor.toggle_code_actions( &ToggleCodeActions { deployed_from: Some(CodeActionSource::RunMenu(row)), @@ -10566,6 +10452,22 @@ impl Editor { } prev_edited_row = selection.end.row; + // If cursor is after a list prefix, make selection non-empty to trigger line indent + if selection.is_empty() { + let cursor = selection.head(); + let settings = buffer.language_settings_at(cursor, cx); + if settings.indent_list_on_tab { + if let Some(language) = snapshot.language_scope_at(Point::new(cursor.row, 0)) { + if is_list_prefix_row(MultiBufferRow(cursor.row), &snapshot, &language) { + row_delta = Self::indent_selection( + buffer, &snapshot, selection, &mut edits, row_delta, cx, + ); + continue; + } + } + } + } + // If the selection is non-empty, then increase the indentation of the selected lines. if !selection.is_empty() { row_delta = @@ -11331,7 +11233,7 @@ impl Editor { }]; let focus_handle = bp_prompt.focus_handle(cx); - window.focus(&focus_handle); + window.focus(&focus_handle, cx); let block_ids = self.insert_blocks(blocks, None, cx); bp_prompt.update(cx, |prompt, _| { @@ -18158,7 +18060,7 @@ impl Editor { cx, ); let rename_focus_handle = rename_editor.focus_handle(cx); - window.focus(&rename_focus_handle); + window.focus(&rename_focus_handle, cx); let block_id = this.insert_blocks( [BlockProperties { style: BlockStyle::Flex, @@ -18272,7 +18174,7 @@ impl Editor { ) -> Option { let rename = self.pending_rename.take()?; if rename.editor.focus_handle(cx).is_focused(window) { - window.focus(&self.focus_handle); + window.focus(&self.focus_handle, cx); } self.remove_blocks( @@ -22842,7 +22744,7 @@ impl Editor { .take() .and_then(|descendant| descendant.upgrade()) { - window.focus(&descendant); + window.focus(&descendant, cx); } else { if let Some(blame) = self.blame.as_ref() { blame.update(cx, GitBlame::focus) @@ -23473,76 +23375,460 @@ struct CompletionEdit { snippet: Option, } -fn insert_extra_newline_brackets( +fn comment_delimiter_for_newline( + start_point: &Point, buffer: &MultiBufferSnapshot, - range: Range, - language: &language::LanguageScope, -) -> bool { - let leading_whitespace_len = buffer - .reversed_chars_at(range.start) - .take_while(|c| c.is_whitespace() && *c != '\n') - .map(|c| c.len_utf8()) - .sum::(); - let trailing_whitespace_len = buffer - .chars_at(range.end) - .take_while(|c| c.is_whitespace() && *c != '\n') - .map(|c| c.len_utf8()) - .sum::(); - let range = range.start - leading_whitespace_len..range.end + trailing_whitespace_len; - - language.brackets().any(|(pair, enabled)| { - let pair_start = pair.start.trim_end(); - let pair_end = pair.end.trim_start(); - - enabled - && pair.newline - && buffer.contains_str_at(range.end, pair_end) - && buffer.contains_str_at( - range.start.saturating_sub_usize(pair_start.len()), - pair_start, - ) - }) + language: &LanguageScope, +) -> Option> { + let delimiters = language.line_comment_prefixes(); + let max_len_of_delimiter = delimiters.iter().map(|delimiter| delimiter.len()).max()?; + let (snapshot, range) = buffer.buffer_line_for_row(MultiBufferRow(start_point.row))?; + + let num_of_whitespaces = snapshot + .chars_for_range(range.clone()) + .take_while(|c| c.is_whitespace()) + .count(); + let comment_candidate = snapshot + .chars_for_range(range.clone()) + .skip(num_of_whitespaces) + .take(max_len_of_delimiter) + .collect::(); + let (delimiter, trimmed_len) = delimiters + .iter() + .filter_map(|delimiter| { + let prefix = delimiter.trim_end(); + if comment_candidate.starts_with(prefix) { + Some((delimiter, prefix.len())) + } else { + None + } + }) + .max_by_key(|(_, len)| *len)?; + + if let Some(BlockCommentConfig { + start: block_start, .. + }) = language.block_comment() + { + let block_start_trimmed = block_start.trim_end(); + if block_start_trimmed.starts_with(delimiter.trim_end()) { + let line_content = snapshot + .chars_for_range(range) + .skip(num_of_whitespaces) + .take(block_start_trimmed.len()) + .collect::(); + + if line_content.starts_with(block_start_trimmed) { + return None; + } + } + } + + let cursor_is_placed_after_comment_marker = + num_of_whitespaces + trimmed_len <= start_point.column as usize; + if cursor_is_placed_after_comment_marker { + Some(delimiter.clone()) + } else { + None + } } -fn insert_extra_newline_tree_sitter( +fn documentation_delimiter_for_newline( + start_point: &Point, buffer: &MultiBufferSnapshot, - range: Range, -) -> bool { - let (buffer, range) = match buffer.range_to_buffer_ranges(range).as_slice() { - [(buffer, range, _)] => (*buffer, range.clone()), - _ => return false, + language: &LanguageScope, + newline_config: &mut NewlineConfig, +) -> Option> { + let BlockCommentConfig { + start: start_tag, + end: end_tag, + prefix: delimiter, + tab_size: len, + } = language.documentation_comment()?; + let is_within_block_comment = buffer + .language_scope_at(*start_point) + .is_some_and(|scope| scope.override_name() == Some("comment")); + if !is_within_block_comment { + return None; + } + + let (snapshot, range) = buffer.buffer_line_for_row(MultiBufferRow(start_point.row))?; + + let num_of_whitespaces = snapshot + .chars_for_range(range.clone()) + .take_while(|c| c.is_whitespace()) + .count(); + + // It is safe to use a column from MultiBufferPoint in context of a single buffer ranges, because we're only ever looking at a single line at a time. + let column = start_point.column; + let cursor_is_after_start_tag = { + let start_tag_len = start_tag.len(); + let start_tag_line = snapshot + .chars_for_range(range.clone()) + .skip(num_of_whitespaces) + .take(start_tag_len) + .collect::(); + if start_tag_line.starts_with(start_tag.as_ref()) { + num_of_whitespaces + start_tag_len <= column as usize + } else { + false + } }; - let pair = { - let mut result: Option> = None; - for pair in buffer - .all_bracket_ranges(range.start.0..range.end.0) - .filter(move |pair| { - pair.open_range.start <= range.start.0 && pair.close_range.end >= range.end.0 - }) + let cursor_is_after_delimiter = { + let delimiter_trim = delimiter.trim_end(); + let delimiter_line = snapshot + .chars_for_range(range.clone()) + .skip(num_of_whitespaces) + .take(delimiter_trim.len()) + .collect::(); + if delimiter_line.starts_with(delimiter_trim) { + num_of_whitespaces + delimiter_trim.len() <= column as usize + } else { + false + } + }; + + let mut needs_extra_line = false; + let mut extra_line_additional_indent = IndentSize::spaces(0); + + let cursor_is_before_end_tag_if_exists = { + let mut char_position = 0u32; + let mut end_tag_offset = None; + + 'outer: for chunk in snapshot.text_for_range(range) { + if let Some(byte_pos) = chunk.find(&**end_tag) { + let chars_before_match = chunk[..byte_pos].chars().count() as u32; + end_tag_offset = Some(char_position + chars_before_match); + break 'outer; + } + char_position += chunk.chars().count() as u32; + } + + if let Some(end_tag_offset) = end_tag_offset { + let cursor_is_before_end_tag = column <= end_tag_offset; + if cursor_is_after_start_tag { + if cursor_is_before_end_tag { + needs_extra_line = true; + } + let cursor_is_at_start_of_end_tag = column == end_tag_offset; + if cursor_is_at_start_of_end_tag { + extra_line_additional_indent.len = *len; + } + } + cursor_is_before_end_tag + } else { + true + } + }; + + if (cursor_is_after_start_tag || cursor_is_after_delimiter) + && cursor_is_before_end_tag_if_exists + { + let additional_indent = if cursor_is_after_start_tag { + IndentSize::spaces(*len) + } else { + IndentSize::spaces(0) + }; + + *newline_config = NewlineConfig::Newline { + additional_indent, + extra_line_additional_indent: if needs_extra_line { + Some(extra_line_additional_indent) + } else { + None + }, + prevent_auto_indent: true, + }; + Some(delimiter.clone()) + } else { + None + } +} + +const ORDERED_LIST_MAX_MARKER_LEN: usize = 16; + +fn list_delimiter_for_newline( + start_point: &Point, + buffer: &MultiBufferSnapshot, + language: &LanguageScope, + newline_config: &mut NewlineConfig, +) -> Option> { + let (snapshot, range) = buffer.buffer_line_for_row(MultiBufferRow(start_point.row))?; + + let num_of_whitespaces = snapshot + .chars_for_range(range.clone()) + .take_while(|c| c.is_whitespace()) + .count(); + + let task_list_entries: Vec<_> = language + .task_list() + .into_iter() + .flat_map(|config| { + config + .prefixes + .iter() + .map(|prefix| (prefix.as_ref(), config.continuation.as_ref())) + }) + .collect(); + let unordered_list_entries: Vec<_> = language + .unordered_list() + .iter() + .map(|marker| (marker.as_ref(), marker.as_ref())) + .collect(); + + let all_entries: Vec<_> = task_list_entries + .into_iter() + .chain(unordered_list_entries) + .collect(); + + if let Some(max_prefix_len) = all_entries.iter().map(|(p, _)| p.len()).max() { + let candidate: String = snapshot + .chars_for_range(range.clone()) + .skip(num_of_whitespaces) + .take(max_prefix_len) + .collect(); + + if let Some((prefix, continuation)) = all_entries + .iter() + .filter(|(prefix, _)| candidate.starts_with(*prefix)) + .max_by_key(|(prefix, _)| prefix.len()) { - let len = pair.close_range.end - pair.open_range.start; + let end_of_prefix = num_of_whitespaces + prefix.len(); + let cursor_is_after_prefix = end_of_prefix <= start_point.column as usize; + let has_content_after_marker = snapshot + .chars_for_range(range) + .skip(end_of_prefix) + .any(|c| !c.is_whitespace()); - if let Some(existing) = &result { - let existing_len = existing.close_range.end - existing.open_range.start; - if len > existing_len { - continue; + if has_content_after_marker && cursor_is_after_prefix { + return Some((*continuation).into()); + } + + if start_point.column as usize == end_of_prefix { + if num_of_whitespaces == 0 { + *newline_config = NewlineConfig::ClearCurrentLine; + } else { + *newline_config = NewlineConfig::UnindentCurrentLine { + continuation: (*continuation).into(), + }; } } - result = Some(pair); + return None; } + } - result - }; - let Some(pair) = pair else { + let candidate: String = snapshot + .chars_for_range(range.clone()) + .skip(num_of_whitespaces) + .take(ORDERED_LIST_MAX_MARKER_LEN) + .collect(); + + for ordered_config in language.ordered_list() { + let regex = match Regex::new(&ordered_config.pattern) { + Ok(r) => r, + Err(_) => continue, + }; + + if let Some(captures) = regex.captures(&candidate) { + let full_match = captures.get(0)?; + let marker_len = full_match.len(); + let end_of_prefix = num_of_whitespaces + marker_len; + let cursor_is_after_prefix = end_of_prefix <= start_point.column as usize; + + let has_content_after_marker = snapshot + .chars_for_range(range) + .skip(end_of_prefix) + .any(|c| !c.is_whitespace()); + + if has_content_after_marker && cursor_is_after_prefix { + let number: u32 = captures.get(1)?.as_str().parse().ok()?; + let continuation = ordered_config + .format + .replace("{1}", &(number + 1).to_string()); + return Some(continuation.into()); + } + + if start_point.column as usize == end_of_prefix { + let continuation = ordered_config.format.replace("{1}", "1"); + if num_of_whitespaces == 0 { + *newline_config = NewlineConfig::ClearCurrentLine; + } else { + *newline_config = NewlineConfig::UnindentCurrentLine { + continuation: continuation.into(), + }; + } + } + + return None; + } + } + + None +} + +fn is_list_prefix_row( + row: MultiBufferRow, + buffer: &MultiBufferSnapshot, + language: &LanguageScope, +) -> bool { + let Some((snapshot, range)) = buffer.buffer_line_for_row(row) else { return false; }; - pair.newline_only - && buffer - .chars_for_range(pair.open_range.end..range.start.0) - .chain(buffer.chars_for_range(range.end.0..pair.close_range.start)) - .all(|c| c.is_whitespace() && c != '\n') + + let num_of_whitespaces = snapshot + .chars_for_range(range.clone()) + .take_while(|c| c.is_whitespace()) + .count(); + + let task_list_prefixes: Vec<_> = language + .task_list() + .into_iter() + .flat_map(|config| { + config + .prefixes + .iter() + .map(|p| p.as_ref()) + .collect::>() + }) + .collect(); + let unordered_list_markers: Vec<_> = language + .unordered_list() + .iter() + .map(|marker| marker.as_ref()) + .collect(); + let all_prefixes: Vec<_> = task_list_prefixes + .into_iter() + .chain(unordered_list_markers) + .collect(); + if let Some(max_prefix_len) = all_prefixes.iter().map(|p| p.len()).max() { + let candidate: String = snapshot + .chars_for_range(range.clone()) + .skip(num_of_whitespaces) + .take(max_prefix_len) + .collect(); + if all_prefixes + .iter() + .any(|prefix| candidate.starts_with(*prefix)) + { + return true; + } + } + + let ordered_list_candidate: String = snapshot + .chars_for_range(range) + .skip(num_of_whitespaces) + .take(ORDERED_LIST_MAX_MARKER_LEN) + .collect(); + for ordered_config in language.ordered_list() { + let regex = match Regex::new(&ordered_config.pattern) { + Ok(r) => r, + Err(_) => continue, + }; + if let Some(captures) = regex.captures(&ordered_list_candidate) { + return captures.get(0).is_some(); + } + } + + false +} + +#[derive(Debug)] +enum NewlineConfig { + /// Insert newline with optional additional indent and optional extra blank line + Newline { + additional_indent: IndentSize, + extra_line_additional_indent: Option, + prevent_auto_indent: bool, + }, + /// Clear the current line + ClearCurrentLine, + /// Unindent the current line and add continuation + UnindentCurrentLine { continuation: Arc }, +} + +impl NewlineConfig { + fn has_extra_line(&self) -> bool { + matches!( + self, + Self::Newline { + extra_line_additional_indent: Some(_), + .. + } + ) + } + + fn insert_extra_newline_brackets( + buffer: &MultiBufferSnapshot, + range: Range, + language: &language::LanguageScope, + ) -> bool { + let leading_whitespace_len = buffer + .reversed_chars_at(range.start) + .take_while(|c| c.is_whitespace() && *c != '\n') + .map(|c| c.len_utf8()) + .sum::(); + let trailing_whitespace_len = buffer + .chars_at(range.end) + .take_while(|c| c.is_whitespace() && *c != '\n') + .map(|c| c.len_utf8()) + .sum::(); + let range = range.start - leading_whitespace_len..range.end + trailing_whitespace_len; + + language.brackets().any(|(pair, enabled)| { + let pair_start = pair.start.trim_end(); + let pair_end = pair.end.trim_start(); + + enabled + && pair.newline + && buffer.contains_str_at(range.end, pair_end) + && buffer.contains_str_at( + range.start.saturating_sub_usize(pair_start.len()), + pair_start, + ) + }) + } + + fn insert_extra_newline_tree_sitter( + buffer: &MultiBufferSnapshot, + range: Range, + ) -> bool { + let (buffer, range) = match buffer.range_to_buffer_ranges(range).as_slice() { + [(buffer, range, _)] => (*buffer, range.clone()), + _ => return false, + }; + let pair = { + let mut result: Option> = None; + + for pair in buffer + .all_bracket_ranges(range.start.0..range.end.0) + .filter(move |pair| { + pair.open_range.start <= range.start.0 && pair.close_range.end >= range.end.0 + }) + { + let len = pair.close_range.end - pair.open_range.start; + + if let Some(existing) = &result { + let existing_len = existing.close_range.end - existing.open_range.start; + if len > existing_len { + continue; + } + } + + result = Some(pair); + } + + result + }; + let Some(pair) = pair else { + return false; + }; + pair.newline_only + && buffer + .chars_for_range(pair.open_range.end..range.start.0) + .chain(buffer.chars_for_range(range.end.0..pair.close_range.start)) + .all(|c| c.is_whitespace() && c != '\n') + } } fn update_uncommitted_diff_for_buffer( @@ -25908,7 +26194,7 @@ impl BreakpointPromptEditor { self.editor .update(cx, |editor, cx| { editor.remove_blocks(self.block_ids.clone(), None, cx); - window.focus(&editor.focus_handle); + window.focus(&editor.focus_handle, cx); }) .log_err(); } diff --git a/crates/editor/src/editor_settings.rs b/crates/editor/src/editor_settings.rs index e1984311d4eb0ba9d989f77a707b22698b00c750..464157202f4821c8f05af479d2eff9f441a961ef 100644 --- a/crates/editor/src/editor_settings.rs +++ b/crates/editor/src/editor_settings.rs @@ -215,7 +215,8 @@ impl Settings for EditorSettings { }, scrollbar: Scrollbar { show: scrollbar.show.map(Into::into).unwrap(), - git_diff: scrollbar.git_diff.unwrap(), + git_diff: scrollbar.git_diff.unwrap() + && content.git.unwrap().enabled.unwrap().is_git_diff_enabled(), selected_text: scrollbar.selected_text.unwrap(), selected_symbol: scrollbar.selected_symbol.unwrap(), search_results: scrollbar.search_results.unwrap(), diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index bac3d12638a23bc54f4a981b874da35b77894fff..87674d8c507b1c294779b1f9ddba458320fc7671 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -69,7 +69,6 @@ use util::{ use workspace::{ CloseActiveItem, CloseAllItems, CloseOtherItems, MoveItemToPaneInDirection, NavigationEntry, OpenOptions, ViewId, - invalid_item_view::InvalidItemView, item::{FollowEvent, FollowableItem, Item, ItemHandle, SaveOptions}, register_project_item, }; @@ -18201,7 +18200,7 @@ async fn test_on_type_formatting_not_triggered(cx: &mut TestAppContext) { ); editor_handle.update_in(cx, |editor, window, cx| { - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| { s.select_ranges([Point::new(0, 21)..Point::new(0, 20)]) }); @@ -20881,6 +20880,36 @@ async fn test_toggling_adjacent_diff_hunks(cx: &mut TestAppContext) { .to_string(), ); + cx.update_editor(|editor, window, cx| { + editor.move_up(&MoveUp, window, cx); + editor.toggle_selected_diff_hunks(&Default::default(), window, cx); + }); + cx.assert_state_with_diff( + indoc! { " + ˇone + - two + three + five + "} + .to_string(), + ); + + cx.update_editor(|editor, window, cx| { + editor.move_down(&MoveDown, window, cx); + editor.move_down(&MoveDown, window, cx); + editor.toggle_selected_diff_hunks(&Default::default(), window, cx); + }); + cx.assert_state_with_diff( + indoc! { " + one + - two + ˇthree + - four + five + "} + .to_string(), + ); + cx.set_state(indoc! { " one ˇTWO @@ -20920,6 +20949,66 @@ async fn test_toggling_adjacent_diff_hunks(cx: &mut TestAppContext) { ); } +#[gpui::test] +async fn test_toggling_adjacent_diff_hunks_2( + executor: BackgroundExecutor, + cx: &mut TestAppContext, +) { + init_test(cx, |_| {}); + + let mut cx = EditorTestContext::new(cx).await; + + let diff_base = r#" + lineA + lineB + lineC + lineD + "# + .unindent(); + + cx.set_state( + &r#" + ˇlineA1 + lineB + lineD + "# + .unindent(), + ); + cx.set_head_text(&diff_base); + executor.run_until_parked(); + + cx.update_editor(|editor, window, cx| { + editor.toggle_selected_diff_hunks(&ToggleSelectedDiffHunks, window, cx); + }); + executor.run_until_parked(); + cx.assert_state_with_diff( + r#" + - lineA + + ˇlineA1 + lineB + lineD + "# + .unindent(), + ); + + cx.update_editor(|editor, window, cx| { + editor.move_down(&MoveDown, window, cx); + editor.move_right(&MoveRight, window, cx); + editor.toggle_selected_diff_hunks(&ToggleSelectedDiffHunks, window, cx); + }); + executor.run_until_parked(); + cx.assert_state_with_diff( + r#" + - lineA + + lineA1 + lˇineB + - lineC + lineD + "# + .unindent(), + ); +} + #[gpui::test] async fn test_edits_around_expanded_deletion_hunks( executor: BackgroundExecutor, @@ -27667,11 +27756,10 @@ async fn test_non_utf_8_opens(cx: &mut TestAppContext) { }) .await .unwrap(); - - assert_eq!( - handle.to_any_view().entity_type(), - TypeId::of::() - ); + // The test file content `vec![0xff, 0xfe, ...]` starts with a UTF-16 LE BOM. + // Previously, this fell back to `InvalidItemView` because it wasn't valid UTF-8. + // With auto-detection enabled, this is now recognized as UTF-16 and opens in the Editor. + assert_eq!(handle.to_any_view().entity_type(), TypeId::of::()); } #[gpui::test] @@ -27933,7 +28021,7 @@ async fn test_markdown_indents(cx: &mut gpui::TestAppContext) { " }); - // Case 2: Test adding new line after nested list preserves indent of previous line + // Case 2: Test adding new line after nested list continues the list with unchecked task cx.set_state(&indoc! {" - [ ] Item 1 - [ ] Item 1.a @@ -27950,20 +28038,12 @@ async fn test_markdown_indents(cx: &mut gpui::TestAppContext) { - [x] Item 2 - [x] Item 2.a - [x] Item 2.b - ˇ" + - [ ] ˇ" }); - // Case 3: Test adding a new nested list item preserves indent - cx.set_state(&indoc! {" - - [ ] Item 1 - - [ ] Item 1.a - - [x] Item 2 - - [x] Item 2.a - - [x] Item 2.b - ˇ" - }); + // Case 3: Test adding content to continued list item cx.update_editor(|editor, window, cx| { - editor.handle_input("-", window, cx); + editor.handle_input("Item 2.c", window, cx); }); cx.run_until_parked(); cx.assert_editor_state(indoc! {" @@ -27972,22 +28052,10 @@ async fn test_markdown_indents(cx: &mut gpui::TestAppContext) { - [x] Item 2 - [x] Item 2.a - [x] Item 2.b - -ˇ" - }); - cx.update_editor(|editor, window, cx| { - editor.handle_input(" [x] Item 2.c", window, cx); - }); - cx.run_until_parked(); - cx.assert_editor_state(indoc! {" - - [ ] Item 1 - - [ ] Item 1.a - - [x] Item 2 - - [x] Item 2.a - - [x] Item 2.b - - [x] Item 2.cˇ" + - [ ] Item 2.cˇ" }); - // Case 4: Test adding new line after nested ordered list preserves indent of previous line + // Case 4: Test adding new line after nested ordered list continues with next number cx.set_state(indoc! {" 1. Item 1 1. Item 1.a @@ -28004,44 +28072,12 @@ async fn test_markdown_indents(cx: &mut gpui::TestAppContext) { 2. Item 2 1. Item 2.a 2. Item 2.b - ˇ" + 3. ˇ" }); - // Case 5: Adding new ordered list item preserves indent - cx.set_state(indoc! {" - 1. Item 1 - 1. Item 1.a - 2. Item 2 - 1. Item 2.a - 2. Item 2.b - ˇ" - }); - cx.update_editor(|editor, window, cx| { - editor.handle_input("3", window, cx); - }); - cx.run_until_parked(); - cx.assert_editor_state(indoc! {" - 1. Item 1 - 1. Item 1.a - 2. Item 2 - 1. Item 2.a - 2. Item 2.b - 3ˇ" - }); - cx.update_editor(|editor, window, cx| { - editor.handle_input(".", window, cx); - }); - cx.run_until_parked(); - cx.assert_editor_state(indoc! {" - 1. Item 1 - 1. Item 1.a - 2. Item 2 - 1. Item 2.a - 2. Item 2.b - 3.ˇ" - }); + // Case 5: Adding content to continued ordered list item cx.update_editor(|editor, window, cx| { - editor.handle_input(" Item 2.c", window, cx); + editor.handle_input("Item 2.c", window, cx); }); cx.run_until_parked(); cx.assert_editor_state(indoc! {" @@ -29409,6 +29445,524 @@ async fn test_find_references_single_case(cx: &mut TestAppContext) { cx.assert_editor_state(after); } +#[gpui::test] +async fn test_newline_task_list_continuation(cx: &mut TestAppContext) { + init_test(cx, |settings| { + settings.defaults.tab_size = Some(2.try_into().unwrap()); + }); + + let markdown_language = languages::language("markdown", tree_sitter_md::LANGUAGE.into()); + let mut cx = EditorTestContext::new(cx).await; + cx.update_buffer(|buffer, cx| buffer.set_language(Some(markdown_language), cx)); + + // Case 1: Adding newline after (whitespace + prefix + any non-whitespace) adds marker + cx.set_state(indoc! {" + - [ ] taskˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + - [ ] task + - [ ] ˇ + "}); + + // Case 2: Works with checked task items too + cx.set_state(indoc! {" + - [x] completed taskˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + - [x] completed task + - [ ] ˇ + "}); + + // Case 3: Cursor position doesn't matter - content after marker is what counts + cx.set_state(indoc! {" + - [ ] taˇsk + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + - [ ] ta + - [ ] ˇsk + "}); + + // Case 4: Adding newline after (whitespace + prefix + some whitespace) does NOT add marker + cx.set_state(indoc! {" + - [ ] ˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state( + indoc! {" + - [ ]$$ + ˇ + "} + .replace("$", " ") + .as_str(), + ); + + // Case 5: Adding newline with content adds marker preserving indentation + cx.set_state(indoc! {" + - [ ] task + - [ ] indentedˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + - [ ] task + - [ ] indented + - [ ] ˇ + "}); + + // Case 6: Adding newline with cursor right after prefix, unindents + cx.set_state(indoc! {" + - [ ] task + - [ ] sub task + - [ ] ˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + - [ ] task + - [ ] sub task + - [ ] ˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + + // Case 7: Adding newline with cursor right after prefix, removes marker + cx.assert_editor_state(indoc! {" + - [ ] task + - [ ] sub task + - [ ] ˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + - [ ] task + - [ ] sub task + ˇ + "}); + + // Case 8: Cursor before or inside prefix does not add marker + cx.set_state(indoc! {" + ˇ- [ ] task + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + + ˇ- [ ] task + "}); + + cx.set_state(indoc! {" + - [ˇ ] task + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + - [ + ˇ + ] task + "}); +} + +#[gpui::test] +async fn test_newline_unordered_list_continuation(cx: &mut TestAppContext) { + init_test(cx, |settings| { + settings.defaults.tab_size = Some(2.try_into().unwrap()); + }); + + let markdown_language = languages::language("markdown", tree_sitter_md::LANGUAGE.into()); + let mut cx = EditorTestContext::new(cx).await; + cx.update_buffer(|buffer, cx| buffer.set_language(Some(markdown_language), cx)); + + // Case 1: Adding newline after (whitespace + marker + any non-whitespace) adds marker + cx.set_state(indoc! {" + - itemˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + - item + - ˇ + "}); + + // Case 2: Works with different markers + cx.set_state(indoc! {" + * starred itemˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + * starred item + * ˇ + "}); + + cx.set_state(indoc! {" + + plus itemˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + + plus item + + ˇ + "}); + + // Case 3: Cursor position doesn't matter - content after marker is what counts + cx.set_state(indoc! {" + - itˇem + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + - it + - ˇem + "}); + + // Case 4: Adding newline after (whitespace + marker + some whitespace) does NOT add marker + cx.set_state(indoc! {" + - ˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state( + indoc! {" + - $ + ˇ + "} + .replace("$", " ") + .as_str(), + ); + + // Case 5: Adding newline with content adds marker preserving indentation + cx.set_state(indoc! {" + - item + - indentedˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + - item + - indented + - ˇ + "}); + + // Case 6: Adding newline with cursor right after marker, unindents + cx.set_state(indoc! {" + - item + - sub item + - ˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + - item + - sub item + - ˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + + // Case 7: Adding newline with cursor right after marker, removes marker + cx.assert_editor_state(indoc! {" + - item + - sub item + - ˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + - item + - sub item + ˇ + "}); + + // Case 8: Cursor before or inside prefix does not add marker + cx.set_state(indoc! {" + ˇ- item + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + + ˇ- item + "}); + + cx.set_state(indoc! {" + -ˇ item + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + - + ˇitem + "}); +} + +#[gpui::test] +async fn test_newline_ordered_list_continuation(cx: &mut TestAppContext) { + init_test(cx, |settings| { + settings.defaults.tab_size = Some(2.try_into().unwrap()); + }); + + let markdown_language = languages::language("markdown", tree_sitter_md::LANGUAGE.into()); + let mut cx = EditorTestContext::new(cx).await; + cx.update_buffer(|buffer, cx| buffer.set_language(Some(markdown_language), cx)); + + // Case 1: Adding newline after (whitespace + marker + any non-whitespace) increments number + cx.set_state(indoc! {" + 1. first itemˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + 1. first item + 2. ˇ + "}); + + // Case 2: Works with larger numbers + cx.set_state(indoc! {" + 10. tenth itemˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + 10. tenth item + 11. ˇ + "}); + + // Case 3: Cursor position doesn't matter - content after marker is what counts + cx.set_state(indoc! {" + 1. itˇem + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + 1. it + 2. ˇem + "}); + + // Case 4: Adding newline after (whitespace + marker + some whitespace) does NOT add marker + cx.set_state(indoc! {" + 1. ˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state( + indoc! {" + 1. $ + ˇ + "} + .replace("$", " ") + .as_str(), + ); + + // Case 5: Adding newline with content adds marker preserving indentation + cx.set_state(indoc! {" + 1. item + 2. indentedˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + 1. item + 2. indented + 3. ˇ + "}); + + // Case 6: Adding newline with cursor right after marker, unindents + cx.set_state(indoc! {" + 1. item + 2. sub item + 3. ˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + 1. item + 2. sub item + 1. ˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + + // Case 7: Adding newline with cursor right after marker, removes marker + cx.assert_editor_state(indoc! {" + 1. item + 2. sub item + 1. ˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + 1. item + 2. sub item + ˇ + "}); + + // Case 8: Cursor before or inside prefix does not add marker + cx.set_state(indoc! {" + ˇ1. item + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + + ˇ1. item + "}); + + cx.set_state(indoc! {" + 1ˇ. item + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + 1 + ˇ. item + "}); +} + +#[gpui::test] +async fn test_newline_should_not_autoindent_ordered_list(cx: &mut TestAppContext) { + init_test(cx, |settings| { + settings.defaults.tab_size = Some(2.try_into().unwrap()); + }); + + let markdown_language = languages::language("markdown", tree_sitter_md::LANGUAGE.into()); + let mut cx = EditorTestContext::new(cx).await; + cx.update_buffer(|buffer, cx| buffer.set_language(Some(markdown_language), cx)); + + // Case 1: Adding newline after (whitespace + marker + any non-whitespace) increments number + cx.set_state(indoc! {" + 1. first item + 1. sub first item + 2. sub second item + 3. ˇ + "}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.wait_for_autoindent_applied().await; + cx.assert_editor_state(indoc! {" + 1. first item + 1. sub first item + 2. sub second item + 1. ˇ + "}); +} + +#[gpui::test] +async fn test_tab_list_indent(cx: &mut TestAppContext) { + init_test(cx, |settings| { + settings.defaults.tab_size = Some(2.try_into().unwrap()); + }); + + let markdown_language = languages::language("markdown", tree_sitter_md::LANGUAGE.into()); + let mut cx = EditorTestContext::new(cx).await; + cx.update_buffer(|buffer, cx| buffer.set_language(Some(markdown_language), cx)); + + // Case 1: Unordered list - cursor after prefix, adds indent before prefix + cx.set_state(indoc! {" + - ˇitem + "}); + cx.update_editor(|e, window, cx| e.tab(&Tab, window, cx)); + cx.wait_for_autoindent_applied().await; + let expected = indoc! {" + $$- ˇitem + "}; + cx.assert_editor_state(expected.replace("$", " ").as_str()); + + // Case 2: Task list - cursor after prefix + cx.set_state(indoc! {" + - [ ] ˇtask + "}); + cx.update_editor(|e, window, cx| e.tab(&Tab, window, cx)); + cx.wait_for_autoindent_applied().await; + let expected = indoc! {" + $$- [ ] ˇtask + "}; + cx.assert_editor_state(expected.replace("$", " ").as_str()); + + // Case 3: Ordered list - cursor after prefix + cx.set_state(indoc! {" + 1. ˇfirst + "}); + cx.update_editor(|e, window, cx| e.tab(&Tab, window, cx)); + cx.wait_for_autoindent_applied().await; + let expected = indoc! {" + $$1. ˇfirst + "}; + cx.assert_editor_state(expected.replace("$", " ").as_str()); + + // Case 4: With existing indentation - adds more indent + let initial = indoc! {" + $$- ˇitem + "}; + cx.set_state(initial.replace("$", " ").as_str()); + cx.update_editor(|e, window, cx| e.tab(&Tab, window, cx)); + cx.wait_for_autoindent_applied().await; + let expected = indoc! {" + $$$$- ˇitem + "}; + cx.assert_editor_state(expected.replace("$", " ").as_str()); + + // Case 5: Empty list item + cx.set_state(indoc! {" + - ˇ + "}); + cx.update_editor(|e, window, cx| e.tab(&Tab, window, cx)); + cx.wait_for_autoindent_applied().await; + let expected = indoc! {" + $$- ˇ + "}; + cx.assert_editor_state(expected.replace("$", " ").as_str()); + + // Case 6: Cursor at end of line with content + cx.set_state(indoc! {" + - itemˇ + "}); + cx.update_editor(|e, window, cx| e.tab(&Tab, window, cx)); + cx.wait_for_autoindent_applied().await; + let expected = indoc! {" + $$- itemˇ + "}; + cx.assert_editor_state(expected.replace("$", " ").as_str()); + + // Case 7: Cursor at start of list item, indents it + cx.set_state(indoc! {" + - item + ˇ - sub item + "}); + cx.update_editor(|e, window, cx| e.tab(&Tab, window, cx)); + cx.wait_for_autoindent_applied().await; + let expected = indoc! {" + - item + ˇ - sub item + "}; + cx.assert_editor_state(expected); + + // Case 8: Cursor at start of list item, moves the cursor when "indent_list_on_tab" is false + cx.update_editor(|_, _, cx| { + SettingsStore::update_global(cx, |store, cx| { + store.update_user_settings(cx, |settings| { + settings.project.all_languages.defaults.indent_list_on_tab = Some(false); + }); + }); + }); + cx.set_state(indoc! {" + - item + ˇ - sub item + "}); + cx.update_editor(|e, window, cx| e.tab(&Tab, window, cx)); + cx.wait_for_autoindent_applied().await; + let expected = indoc! {" + - item + ˇ- sub item + "}; + cx.assert_editor_state(expected); +} + #[gpui::test] async fn test_local_worktree_trust(cx: &mut TestAppContext) { init_test(cx, |_| {}); diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 85b32324a1c1cc7fb84162fb120e8ef0e4e8b599..4c3b44335bcad10be4303d545a8d2ad505938098 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -37,11 +37,7 @@ use crate::{ use buffer_diff::{DiffHunkStatus, DiffHunkStatusKind}; use collections::{BTreeMap, HashMap}; use file_icons::FileIcons; -use git::{ - Oid, - blame::{BlameEntry, ParsedCommitMessage}, - status::FileStatus, -}; +use git::{Oid, blame::BlameEntry, commit::ParsedCommitMessage, status::FileStatus}; use gpui::{ Action, Along, AnyElement, App, AppContext, AvailableSpace, Axis as ScrollbarAxis, BorderStyle, Bounds, ClickEvent, ClipboardItem, ContentMask, Context, Corner, Corners, CursorStyle, @@ -594,8 +590,6 @@ impl EditorElement { register_action(editor, window, Editor::show_signature_help); register_action(editor, window, Editor::signature_help_prev); register_action(editor, window, Editor::signature_help_next); - register_action(editor, window, Editor::next_edit_prediction); - register_action(editor, window, Editor::previous_edit_prediction); register_action(editor, window, Editor::show_edit_prediction); register_action(editor, window, Editor::context_menu_first); register_action(editor, window, Editor::context_menu_prev); @@ -5423,6 +5417,12 @@ impl EditorElement { .max(MIN_POPOVER_LINE_HEIGHT * line_height), // Apply minimum height of 4 lines ); + // Don't show hover popovers when context menu is open to avoid overlap + let has_context_menu = self.editor.read(cx).mouse_context_menu.is_some(); + if has_context_menu { + return; + } + let hover_popovers = self.editor.update(cx, |editor, cx| { editor.hover_state.render( snapshot, diff --git a/crates/editor/src/git/blame.rs b/crates/editor/src/git/blame.rs index 031795ff2dbfceb96f950db18101b37fd3cdcf84..d1338c3cbd3540914b23a53410fd5c823e1285c8 100644 --- a/crates/editor/src/git/blame.rs +++ b/crates/editor/src/git/blame.rs @@ -3,9 +3,9 @@ use anyhow::{Context as _, Result}; use collections::HashMap; use git::{ - GitHostingProviderRegistry, GitRemote, Oid, - blame::{Blame, BlameEntry, ParsedCommitMessage}, - parse_git_remote_url, + GitHostingProviderRegistry, Oid, + blame::{Blame, BlameEntry}, + commit::ParsedCommitMessage, }; use gpui::{ AnyElement, App, AppContext as _, Context, Entity, Hsla, ScrollHandle, Subscription, Task, @@ -525,12 +525,7 @@ impl GitBlame { .git_store() .read(cx) .repository_and_path_for_buffer_id(buffer.read(cx).remote_id(), cx) - .and_then(|(repo, _)| { - repo.read(cx) - .remote_upstream_url - .clone() - .or(repo.read(cx).remote_origin_url.clone()) - }); + .and_then(|(repo, _)| repo.read(cx).default_remote_url()); let blame_buffer = project .update(cx, |project, cx| project.blame_buffer(&buffer, None, cx)); Ok(async move { @@ -554,13 +549,19 @@ impl GitBlame { entries, snapshot.max_point().row, ); - let commit_details = parse_commit_messages( - messages, - remote_url, - provider_registry.clone(), - ) - .await; - + let commit_details = messages + .into_iter() + .map(|(oid, message)| { + let parsed_commit_message = + ParsedCommitMessage::parse( + oid.to_string(), + message, + remote_url.as_deref(), + Some(provider_registry.clone()), + ); + (oid, parsed_commit_message) + }) + .collect(); res.push(( id, snapshot, @@ -680,55 +681,6 @@ fn build_blame_entry_sum_tree(entries: Vec, max_row: u32) -> SumTree entries } -async fn parse_commit_messages( - messages: impl IntoIterator, - remote_url: Option, - provider_registry: Arc, -) -> HashMap { - let mut commit_details = HashMap::default(); - - let parsed_remote_url = remote_url - .as_deref() - .and_then(|remote_url| parse_git_remote_url(provider_registry, remote_url)); - - for (oid, message) in messages { - let permalink = if let Some((provider, git_remote)) = parsed_remote_url.as_ref() { - Some(provider.build_commit_permalink( - git_remote, - git::BuildCommitPermalinkParams { - sha: oid.to_string().as_str(), - }, - )) - } else { - None - }; - - let remote = parsed_remote_url - .as_ref() - .map(|(provider, remote)| GitRemote { - host: provider.clone(), - owner: remote.owner.clone().into(), - repo: remote.repo.clone().into(), - }); - - let pull_request = parsed_remote_url - .as_ref() - .and_then(|(provider, remote)| provider.extract_pull_request(remote, &message)); - - commit_details.insert( - oid, - ParsedCommitMessage { - message: message.into(), - permalink, - remote, - pull_request, - }, - ); - } - - commit_details -} - #[cfg(test)] mod tests { use super::*; diff --git a/crates/editor/src/hover_links.rs b/crates/editor/src/hover_links.rs index d7e4169a721765e0f93805bf0c157033bf0cafab..1c00acbfa9f1a69cbe01c45758db5a0cd4fee757 100644 --- a/crates/editor/src/hover_links.rs +++ b/crates/editor/src/hover_links.rs @@ -218,7 +218,7 @@ impl Editor { self.hide_hovered_link(cx); if !hovered_link_state.links.is_empty() { if !self.focus_handle.is_focused(window) { - window.focus(&self.focus_handle); + window.focus(&self.focus_handle, cx); } // exclude links pointing back to the current anchor diff --git a/crates/editor/src/mouse_context_menu.rs b/crates/editor/src/mouse_context_menu.rs index 36521d46a6c20223e973346b9d1e9391db3306ca..7314991bd5e4842f395383888a87b4e2db7e0a0c 100644 --- a/crates/editor/src/mouse_context_menu.rs +++ b/crates/editor/src/mouse_context_menu.rs @@ -90,8 +90,8 @@ impl MouseContextMenu { // `true` when the `ContextMenu` is focused. let focus_handle = context_menu_focus.clone(); cx.on_next_frame(window, move |_, window, cx| { - cx.on_next_frame(window, move |_, window, _cx| { - window.focus(&focus_handle); + cx.on_next_frame(window, move |_, window, cx| { + window.focus(&focus_handle, cx); }); }); @@ -100,7 +100,7 @@ impl MouseContextMenu { move |editor, _, _event: &DismissEvent, window, cx| { editor.mouse_context_menu.take(); if context_menu_focus.contains_focused(window, cx) { - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); } } }); @@ -127,7 +127,7 @@ impl MouseContextMenu { } editor.mouse_context_menu.take(); if context_menu_focus.contains_focused(window, cx) { - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); } }, ); @@ -161,7 +161,7 @@ pub fn deploy_context_menu( cx: &mut Context, ) { if !editor.is_focused(window) { - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); } // Don't show context menu for inline editors diff --git a/crates/editor/src/scroll/autoscroll.rs b/crates/editor/src/scroll/autoscroll.rs index 28fd9442193bbec663d3f72eaa805214375dd8ca..fc2ecb9205109532da2b43c97821b5352f27aff2 100644 --- a/crates/editor/src/scroll/autoscroll.rs +++ b/crates/editor/src/scroll/autoscroll.rs @@ -5,7 +5,7 @@ use crate::{ }; use gpui::{Bounds, Context, Pixels, Window}; use language::Point; -use multi_buffer::Anchor; +use multi_buffer::{Anchor, ToPoint}; use std::cmp; #[derive(Debug, PartialEq, Eq, Clone, Copy)] @@ -186,6 +186,19 @@ impl Editor { } } + let style = self.style(cx).clone(); + let sticky_headers = self.sticky_headers(&style, cx).unwrap_or_default(); + let visible_sticky_headers = sticky_headers + .iter() + .filter(|h| { + let buffer_snapshot = display_map.buffer_snapshot(); + let buffer_range = + h.range.start.to_point(buffer_snapshot)..h.range.end.to_point(buffer_snapshot); + + buffer_range.contains(&Point::new(target_top as u32, 0)) + }) + .count(); + let margin = if matches!(self.mode, EditorMode::AutoHeight { .. }) { 0. } else { @@ -218,7 +231,7 @@ impl Editor { let was_autoscrolled = match strategy { AutoscrollStrategy::Fit | AutoscrollStrategy::Newest => { let margin = margin.min(self.scroll_manager.vertical_scroll_margin); - let target_top = (target_top - margin).max(0.0); + let target_top = (target_top - margin - visible_sticky_headers as f64).max(0.0); let target_bottom = target_bottom + margin; let start_row = scroll_position.y; let end_row = start_row + visible_lines; diff --git a/crates/editor/src/test/editor_lsp_test_context.rs b/crates/editor/src/test/editor_lsp_test_context.rs index 3afe0e6134221fc69837abd30618f2b74ae069f5..3e7c47c2ac5efeedde51f180bcfcb424aec31c86 100644 --- a/crates/editor/src/test/editor_lsp_test_context.rs +++ b/crates/editor/src/test/editor_lsp_test_context.rs @@ -126,7 +126,7 @@ impl EditorLspTestContext { .read(cx) .nav_history_for_item(&cx.entity()); editor.set_nav_history(Some(nav_history)); - window.focus(&editor.focus_handle(cx)) + window.focus(&editor.focus_handle(cx), cx) }); let lsp = fake_servers.next().await.unwrap(); @@ -205,6 +205,49 @@ impl EditorLspTestContext { (_ "{" "}" @end) @indent (_ "(" ")" @end) @indent "#})), + text_objects: Some(Cow::from(indoc! {r#" + (function_declaration + body: (_ + "{" + (_)* @function.inside + "}")) @function.around + + (method_definition + body: (_ + "{" + (_)* @function.inside + "}")) @function.around + + ; Arrow function in variable declaration - capture the full declaration + ([ + (lexical_declaration + (variable_declarator + value: (arrow_function + body: (statement_block + "{" + (_)* @function.inside + "}")))) + (variable_declaration + (variable_declarator + value: (arrow_function + body: (statement_block + "{" + (_)* @function.inside + "}")))) + ]) @function.around + + ([ + (lexical_declaration + (variable_declarator + value: (arrow_function))) + (variable_declaration + (variable_declarator + value: (arrow_function))) + ]) @function.around + + ; Catch-all for arrow functions in other contexts (callbacks, etc.) + ((arrow_function) @function.around (#not-has-parent? @function.around variable_declarator)) + "#})), ..Default::default() }) .expect("Could not parse queries"); @@ -276,6 +319,49 @@ impl EditorLspTestContext { (jsx_opening_element) @start (jsx_closing_element)? @end) @indent "#})), + text_objects: Some(Cow::from(indoc! {r#" + (function_declaration + body: (_ + "{" + (_)* @function.inside + "}")) @function.around + + (method_definition + body: (_ + "{" + (_)* @function.inside + "}")) @function.around + + ; Arrow function in variable declaration - capture the full declaration + ([ + (lexical_declaration + (variable_declarator + value: (arrow_function + body: (statement_block + "{" + (_)* @function.inside + "}")))) + (variable_declaration + (variable_declarator + value: (arrow_function + body: (statement_block + "{" + (_)* @function.inside + "}")))) + ]) @function.around + + ([ + (lexical_declaration + (variable_declarator + value: (arrow_function))) + (variable_declaration + (variable_declarator + value: (arrow_function))) + ]) @function.around + + ; Catch-all for arrow functions in other contexts (callbacks, etc.) + ((arrow_function) @function.around (#not-has-parent? @function.around variable_declarator)) + "#})), ..Default::default() }) .expect("Could not parse queries"); diff --git a/crates/editor/src/test/editor_test_context.rs b/crates/editor/src/test/editor_test_context.rs index bcfaeea3a7330539b2f2790e7dbe9a4969c76981..267058691d0070678830ba9d7c40f54a9363737b 100644 --- a/crates/editor/src/test/editor_test_context.rs +++ b/crates/editor/src/test/editor_test_context.rs @@ -78,7 +78,7 @@ impl EditorTestContext { cx, ); - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); editor }); let editor_view = editor.root(cx).unwrap(); @@ -139,7 +139,7 @@ impl EditorTestContext { let editor = cx.add_window(|window, cx| { let editor = build_editor(buffer, window, cx); - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); editor }); diff --git a/crates/eval/src/eval.rs b/crates/eval/src/eval.rs index 3a2891922c80b95c85f0daed25603bea14b41842..80633696b7d5e655bb7db3627568b881642cf62c 100644 --- a/crates/eval/src/eval.rs +++ b/crates/eval/src/eval.rs @@ -422,7 +422,7 @@ pub fn init(cx: &mut App) -> Arc { tx.send(Some(options)).log_err(); }) .detach(); - let node_runtime = NodeRuntime::new(client.http_client(), None, rx, None); + let node_runtime = NodeRuntime::new(client.http_client(), None, rx); let extension_host_proxy = ExtensionHostProxy::global(cx); debug_adapter_extension::init(extension_host_proxy.clone(), cx); diff --git a/crates/eval_utils/LICENSE-GPL b/crates/eval_utils/LICENSE-GPL index e0f9dbd5d63fef1630c297edc4ceba4790be6f02..89e542f750cd3860a0598eff0dc34b56d7336dc4 120000 --- a/crates/eval_utils/LICENSE-GPL +++ b/crates/eval_utils/LICENSE-GPL @@ -1 +1 @@ -LICENSE-GPL \ No newline at end of file +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/extension/src/extension_host_proxy.rs b/crates/extension/src/extension_host_proxy.rs index 6a24e3ba3f496bd0f0b89d61e9125b29ecae0204..b445878389015d4b3b8c3e25a0d103586462fd86 100644 --- a/crates/extension/src/extension_host_proxy.rs +++ b/crates/extension/src/extension_host_proxy.rs @@ -19,6 +19,9 @@ impl Global for GlobalExtensionHostProxy {} /// /// This object implements each of the individual proxy types so that their /// methods can be called directly on it. +/// Registration function for language model providers. +pub type LanguageModelProviderRegistration = Box; + #[derive(Default)] pub struct ExtensionHostProxy { theme_proxy: RwLock>>, @@ -29,6 +32,7 @@ pub struct ExtensionHostProxy { slash_command_proxy: RwLock>>, context_server_proxy: RwLock>>, debug_adapter_provider_proxy: RwLock>>, + language_model_provider_proxy: RwLock>>, } impl ExtensionHostProxy { @@ -54,6 +58,7 @@ impl ExtensionHostProxy { slash_command_proxy: RwLock::default(), context_server_proxy: RwLock::default(), debug_adapter_provider_proxy: RwLock::default(), + language_model_provider_proxy: RwLock::default(), } } @@ -90,6 +95,15 @@ impl ExtensionHostProxy { .write() .replace(Arc::new(proxy)); } + + pub fn register_language_model_provider_proxy( + &self, + proxy: impl ExtensionLanguageModelProviderProxy, + ) { + self.language_model_provider_proxy + .write() + .replace(Arc::new(proxy)); + } } pub trait ExtensionThemeProxy: Send + Sync + 'static { @@ -446,3 +460,37 @@ impl ExtensionDebugAdapterProviderProxy for ExtensionHostProxy { proxy.unregister_debug_locator(locator_name) } } + +pub trait ExtensionLanguageModelProviderProxy: Send + Sync + 'static { + fn register_language_model_provider( + &self, + provider_id: Arc, + register_fn: LanguageModelProviderRegistration, + cx: &mut App, + ); + + fn unregister_language_model_provider(&self, provider_id: Arc, cx: &mut App); +} + +impl ExtensionLanguageModelProviderProxy for ExtensionHostProxy { + fn register_language_model_provider( + &self, + provider_id: Arc, + register_fn: LanguageModelProviderRegistration, + cx: &mut App, + ) { + let Some(proxy) = self.language_model_provider_proxy.read().clone() else { + return; + }; + + proxy.register_language_model_provider(provider_id, register_fn, cx) + } + + fn unregister_language_model_provider(&self, provider_id: Arc, cx: &mut App) { + let Some(proxy) = self.language_model_provider_proxy.read().clone() else { + return; + }; + + proxy.unregister_language_model_provider(provider_id, cx) + } +} diff --git a/crates/extension/src/extension_manifest.rs b/crates/extension/src/extension_manifest.rs index 4ecdd378ca86dbee263e439e13fa4776dab9e316..39b629db30d0d1cee3374dafc317bdeb0f368146 100644 --- a/crates/extension/src/extension_manifest.rs +++ b/crates/extension/src/extension_manifest.rs @@ -93,6 +93,8 @@ pub struct ExtensionManifest { pub debug_adapters: BTreeMap, DebugAdapterManifestEntry>, #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] pub debug_locators: BTreeMap, DebugLocatorManifestEntry>, + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] + pub language_model_providers: BTreeMap, LanguageModelProviderManifestEntry>, } impl ExtensionManifest { @@ -288,6 +290,16 @@ pub struct DebugAdapterManifestEntry { #[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)] pub struct DebugLocatorManifestEntry {} +/// Manifest entry for a language model provider. +#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)] +pub struct LanguageModelProviderManifestEntry { + /// Display name for the provider. + pub name: String, + /// Path to an SVG icon file relative to the extension root (e.g., "icons/provider.svg"). + #[serde(default)] + pub icon: Option, +} + impl ExtensionManifest { pub async fn load(fs: Arc, extension_dir: &Path) -> Result { let extension_name = extension_dir @@ -358,6 +370,7 @@ fn manifest_from_old_manifest( capabilities: Vec::new(), debug_adapters: Default::default(), debug_locators: Default::default(), + language_model_providers: Default::default(), } } @@ -391,6 +404,7 @@ mod tests { capabilities: vec![], debug_adapters: Default::default(), debug_locators: Default::default(), + language_model_providers: BTreeMap::default(), } } diff --git a/crates/extension_api/src/extension_api.rs b/crates/extension_api/src/extension_api.rs index 9418623224289f795fed061acbfc6035a4cc5cdf..acd1cba47b0150b85ddec8baafa8b5f341460a39 100644 --- a/crates/extension_api/src/extension_api.rs +++ b/crates/extension_api/src/extension_api.rs @@ -331,7 +331,6 @@ static mut EXTENSION: Option> = None; pub static ZED_API_VERSION: [u8; 6] = *include_bytes!(concat!(env!("OUT_DIR"), "/version_bytes")); mod wit { - wit_bindgen::generate!({ skip: ["init-extension"], path: "./wit/since_v0.8.0", @@ -524,6 +523,12 @@ impl wit::Guest for Component { #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)] pub struct LanguageServerId(String); +impl LanguageServerId { + pub fn new(value: String) -> Self { + Self(value) + } +} + impl AsRef for LanguageServerId { fn as_ref(&self) -> &str { &self.0 @@ -540,6 +545,12 @@ impl fmt::Display for LanguageServerId { #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)] pub struct ContextServerId(String); +impl ContextServerId { + pub fn new(value: String) -> Self { + Self(value) + } +} + impl AsRef for ContextServerId { fn as_ref(&self) -> &str { &self.0 diff --git a/crates/extension_host/benches/extension_compilation_benchmark.rs b/crates/extension_host/benches/extension_compilation_benchmark.rs index a28f617dc36e5cba3ad36d7ab6477e7a665dd5c4..605b98c67071155d8444639ef7043b9c8901161d 100644 --- a/crates/extension_host/benches/extension_compilation_benchmark.rs +++ b/crates/extension_host/benches/extension_compilation_benchmark.rs @@ -148,6 +148,7 @@ fn manifest() -> ExtensionManifest { )], debug_adapters: Default::default(), debug_locators: Default::default(), + language_model_providers: BTreeMap::default(), } } diff --git a/crates/extension_host/src/capability_granter.rs b/crates/extension_host/src/capability_granter.rs index 9f27b5e480bc3c22faefe67cd49a06af21614096..6278deef0a7d41e40d4444ddbe992f007cd5e53e 100644 --- a/crates/extension_host/src/capability_granter.rs +++ b/crates/extension_host/src/capability_granter.rs @@ -113,6 +113,7 @@ mod tests { capabilities: vec![], debug_adapters: Default::default(), debug_locators: Default::default(), + language_model_providers: BTreeMap::default(), } } diff --git a/crates/extension_host/src/extension_store_test.rs b/crates/extension_host/src/extension_store_test.rs index 54b090347ffad3ffed444827f5cb60c120d25ad7..c17484f26a06b3392cdbcd8f3c1578eb43c7b213 100644 --- a/crates/extension_host/src/extension_store_test.rs +++ b/crates/extension_host/src/extension_store_test.rs @@ -165,6 +165,7 @@ async fn test_extension_store(cx: &mut TestAppContext) { capabilities: Vec::new(), debug_adapters: Default::default(), debug_locators: Default::default(), + language_model_providers: BTreeMap::default(), }), dev: false, }, @@ -196,6 +197,7 @@ async fn test_extension_store(cx: &mut TestAppContext) { capabilities: Vec::new(), debug_adapters: Default::default(), debug_locators: Default::default(), + language_model_providers: BTreeMap::default(), }), dev: false, }, @@ -376,6 +378,7 @@ async fn test_extension_store(cx: &mut TestAppContext) { capabilities: Vec::new(), debug_adapters: Default::default(), debug_locators: Default::default(), + language_model_providers: BTreeMap::default(), }), dev: false, }, diff --git a/crates/file_finder/src/file_finder.rs b/crates/file_finder/src/file_finder.rs index 050d7a45a1b46e94a195f88e49fd6795ce37f09f..73b21bb828a598d5bbc53c0ecf4511988c30bc65 100644 --- a/crates/file_finder/src/file_finder.rs +++ b/crates/file_finder/src/file_finder.rs @@ -1713,7 +1713,7 @@ impl PickerDelegate for FileFinderDelegate { ui::IconPosition::End, Some(ToggleIncludeIgnored.boxed_clone()), move |window, cx| { - window.focus(&focus_handle); + window.focus(&focus_handle, cx); window.dispatch_action( ToggleIncludeIgnored.boxed_clone(), cx, diff --git a/crates/fs/src/fs.rs b/crates/fs/src/fs.rs index e6f69a14593a0246ae8ccb4aa4673f4e1f5a1e8e..2cbbf61a21e145464e9dbec01ace3b5510709d0d 100644 --- a/crates/fs/src/fs.rs +++ b/crates/fs/src/fs.rs @@ -434,7 +434,18 @@ impl RealFs { for component in path.components() { match component { std::path::Component::Prefix(_) => { - let canonicalized = std::fs::canonicalize(component)?; + let component = component.as_os_str(); + let canonicalized = if component + .to_str() + .map(|e| e.ends_with("\\")) + .unwrap_or(false) + { + std::fs::canonicalize(component) + } else { + let mut component = component.to_os_string(); + component.push("\\"); + std::fs::canonicalize(component) + }?; let mut strip = PathBuf::new(); for component in canonicalized.components() { @@ -3394,6 +3405,26 @@ mod tests { assert_eq!(content, "Hello"); } + #[gpui::test] + #[cfg(target_os = "windows")] + async fn test_realfs_canonicalize(executor: BackgroundExecutor) { + use util::paths::SanitizedPath; + + let fs = RealFs { + bundled_git_binary_path: None, + executor, + next_job_id: Arc::new(AtomicUsize::new(0)), + job_event_subscribers: Arc::new(Mutex::new(Vec::new())), + }; + let temp_dir = TempDir::new().unwrap(); + let file = temp_dir.path().join("test (1).txt"); + let file = SanitizedPath::new(&file); + std::fs::write(&file, "test").unwrap(); + + let canonicalized = fs.canonicalize(file.as_path()).await; + assert!(canonicalized.is_ok()); + } + #[gpui::test] async fn test_rename(executor: BackgroundExecutor) { let fs = FakeFs::new(executor.clone()); diff --git a/crates/git/src/blame.rs b/crates/git/src/blame.rs index c3bbeff3f7d15d84b779f2ab92cb89799f63c4e8..d6011de98b8c69837d16bf2a2211fc7632726230 100644 --- a/crates/git/src/blame.rs +++ b/crates/git/src/blame.rs @@ -1,10 +1,9 @@ +use crate::Oid; use crate::commit::get_messages; use crate::repository::RepoPath; -use crate::{GitRemote, Oid}; use anyhow::{Context as _, Result}; use collections::{HashMap, HashSet}; use futures::AsyncWriteExt; -use gpui::SharedString; use serde::{Deserialize, Serialize}; use std::process::Stdio; use std::{ops::Range, path::Path}; @@ -21,14 +20,6 @@ pub struct Blame { pub messages: HashMap, } -#[derive(Clone, Debug, Default)] -pub struct ParsedCommitMessage { - pub message: SharedString, - pub permalink: Option, - pub pull_request: Option, - pub remote: Option, -} - impl Blame { pub async fn for_path( git_binary: &Path, diff --git a/crates/git/src/commit.rs b/crates/git/src/commit.rs index ece1d76b8ae9c9f40f27178da1ef13fe1a78e659..1b450a3dffb9e9956e5b43aa2797ae02f90e731c 100644 --- a/crates/git/src/commit.rs +++ b/crates/git/src/commit.rs @@ -1,7 +1,52 @@ -use crate::{Oid, status::StatusCode}; +use crate::{ + BuildCommitPermalinkParams, GitHostingProviderRegistry, GitRemote, Oid, parse_git_remote_url, + status::StatusCode, +}; use anyhow::{Context as _, Result}; use collections::HashMap; -use std::path::Path; +use gpui::SharedString; +use std::{path::Path, sync::Arc}; + +#[derive(Clone, Debug, Default)] +pub struct ParsedCommitMessage { + pub message: SharedString, + pub permalink: Option, + pub pull_request: Option, + pub remote: Option, +} + +impl ParsedCommitMessage { + pub fn parse( + sha: String, + message: String, + remote_url: Option<&str>, + provider_registry: Option>, + ) -> Self { + if let Some((hosting_provider, remote)) = provider_registry + .and_then(|reg| remote_url.and_then(|url| parse_git_remote_url(reg, url))) + { + let pull_request = hosting_provider.extract_pull_request(&remote, &message); + Self { + message: message.into(), + permalink: Some( + hosting_provider + .build_commit_permalink(&remote, BuildCommitPermalinkParams { sha: &sha }), + ), + pull_request, + remote: Some(GitRemote { + host: hosting_provider, + owner: remote.owner.into(), + repo: remote.repo.into(), + }), + } + } else { + Self { + message: message.into(), + ..Default::default() + } + } + } +} pub async fn get_messages(working_directory: &Path, shas: &[Oid]) -> Result> { if shas.is_empty() { diff --git a/crates/git_ui/src/blame_ui.rs b/crates/git_ui/src/blame_ui.rs index 09ab3229bc5b2b7814b89bbb914472407793a52d..d4d8750a18ee6efbd90a38722043450c6ec61358 100644 --- a/crates/git_ui/src/blame_ui.rs +++ b/crates/git_ui/src/blame_ui.rs @@ -3,10 +3,7 @@ use crate::{ commit_view::CommitView, }; use editor::{BlameRenderer, Editor, hover_markdown_style}; -use git::{ - blame::{BlameEntry, ParsedCommitMessage}, - repository::CommitSummary, -}; +use git::{blame::BlameEntry, commit::ParsedCommitMessage, repository::CommitSummary}; use gpui::{ ClipboardItem, Entity, Hsla, MouseButton, ScrollHandle, Subscription, TextStyle, TextStyleRefinement, UnderlineStyle, WeakEntity, prelude::*, diff --git a/crates/git_ui/src/branch_picker.rs b/crates/git_ui/src/branch_picker.rs index 7395f1588fececcf4f374ec0e66cdac6024656d7..4db37e91b8720e51ff0416cc471842483ab1d0ca 100644 --- a/crates/git_ui/src/branch_picker.rs +++ b/crates/git_ui/src/branch_picker.rs @@ -91,7 +91,7 @@ pub fn popover( window, cx, ); - list.focus_handle(cx).focus(window); + list.focus_handle(cx).focus(window, cx); list }) } @@ -1880,7 +1880,7 @@ mod tests { branch_list .update_in(cx, |branch_list, window, cx| { - window.focus(&branch_list.picker_focus_handle); + window.focus(&branch_list.picker_focus_handle, cx); assert!( branch_list.picker_focus_handle.is_focused(window), "Branch picker should be focused when selecting an entry" @@ -1898,7 +1898,7 @@ mod tests { branch_list.update_in(cx, |branch_list, window, cx| { // Re-focus the picker since workspace initialization during run_until_parked - window.focus(&branch_list.picker_focus_handle); + window.focus(&branch_list.picker_focus_handle, cx); branch_list.picker.update(cx, |picker, cx| { let last_match = picker.delegate.matches.last().unwrap(); diff --git a/crates/git_ui/src/clone.rs b/crates/git_ui/src/clone.rs new file mode 100644 index 0000000000000000000000000000000000000000..a6767d33304d3f20b7a5e78340f62c89ebe3ae58 --- /dev/null +++ b/crates/git_ui/src/clone.rs @@ -0,0 +1,155 @@ +use gpui::{App, Context, WeakEntity, Window}; +use notifications::status_toast::{StatusToast, ToastIcon}; +use std::sync::Arc; +use ui::{Color, IconName, SharedString}; +use util::ResultExt; +use workspace::{self, Workspace}; + +pub fn clone_and_open( + repo_url: SharedString, + workspace: WeakEntity, + window: &mut Window, + cx: &mut App, + on_success: Arc< + dyn Fn(&mut Workspace, &mut Window, &mut Context) + Send + Sync + 'static, + >, +) { + let destination_prompt = cx.prompt_for_paths(gpui::PathPromptOptions { + files: false, + directories: true, + multiple: false, + prompt: Some("Select as Repository Destination".into()), + }); + + window + .spawn(cx, async move |cx| { + let mut paths = destination_prompt.await.ok()?.ok()??; + let mut destination_dir = paths.pop()?; + + let repo_name = repo_url + .split('/') + .next_back() + .map(|name| name.strip_suffix(".git").unwrap_or(name)) + .unwrap_or("repository") + .to_owned(); + + let clone_task = workspace + .update(cx, |workspace, cx| { + let fs = workspace.app_state().fs.clone(); + let destination_dir = destination_dir.clone(); + let repo_url = repo_url.clone(); + cx.spawn(async move |_workspace, _cx| { + fs.git_clone(&repo_url, destination_dir.as_path()).await + }) + }) + .ok()?; + + if let Err(error) = clone_task.await { + workspace + .update(cx, |workspace, cx| { + let toast = StatusToast::new(error.to_string(), cx, |this, _| { + this.icon(ToastIcon::new(IconName::XCircle).color(Color::Error)) + .dismiss_button(true) + }); + workspace.toggle_status_toast(toast, cx); + }) + .log_err(); + return None; + } + + let has_worktrees = workspace + .read_with(cx, |workspace, cx| { + workspace.project().read(cx).worktrees(cx).next().is_some() + }) + .ok()?; + + let prompt_answer = if has_worktrees { + cx.update(|window, cx| { + window.prompt( + gpui::PromptLevel::Info, + &format!("Git Clone: {}", repo_name), + None, + &["Add repo to project", "Open repo in new project"], + cx, + ) + }) + .ok()? + .await + .ok()? + } else { + // Don't ask if project is empty + 0 + }; + + destination_dir.push(&repo_name); + + match prompt_answer { + 0 => { + workspace + .update_in(cx, |workspace, window, cx| { + let create_task = workspace.project().update(cx, |project, cx| { + project.create_worktree(destination_dir.as_path(), true, cx) + }); + + let workspace_weak = cx.weak_entity(); + let on_success = on_success.clone(); + cx.spawn_in(window, async move |_window, cx| { + if create_task.await.log_err().is_some() { + workspace_weak + .update_in(cx, |workspace, window, cx| { + (on_success)(workspace, window, cx); + }) + .ok(); + } + }) + .detach(); + }) + .ok()?; + } + 1 => { + workspace + .update(cx, move |workspace, cx| { + let app_state = workspace.app_state().clone(); + let destination_path = destination_dir.clone(); + let on_success = on_success.clone(); + + workspace::open_new( + Default::default(), + app_state, + cx, + move |workspace, window, cx| { + cx.activate(true); + + let create_task = + workspace.project().update(cx, |project, cx| { + project.create_worktree( + destination_path.as_path(), + true, + cx, + ) + }); + + let workspace_weak = cx.weak_entity(); + cx.spawn_in(window, async move |_window, cx| { + if create_task.await.log_err().is_some() { + workspace_weak + .update_in(cx, |workspace, window, cx| { + (on_success)(workspace, window, cx); + }) + .ok(); + } + }) + .detach(); + }, + ) + .detach(); + }) + .ok(); + } + _ => {} + } + + Some(()) + }) + .detach(); +} diff --git a/crates/git_ui/src/commit_modal.rs b/crates/git_ui/src/commit_modal.rs index 291a96f47590b145b5c190150af54bd3d43c2fff..e154933adc794221159c7f1b28b3d1e33cf1854d 100644 --- a/crates/git_ui/src/commit_modal.rs +++ b/crates/git_ui/src/commit_modal.rs @@ -521,7 +521,7 @@ impl CommitModal { fn toggle_branch_selector(&mut self, window: &mut Window, cx: &mut Context) { if self.branch_list_handle.is_focused(window, cx) { - self.focus_handle(cx).focus(window) + self.focus_handle(cx).focus(window, cx) } else { self.branch_list_handle.toggle(window, cx); } @@ -587,8 +587,8 @@ impl Render for CommitModal { .bg(cx.theme().colors().editor_background) .border_1() .border_color(cx.theme().colors().border_variant) - .on_click(cx.listener(move |_, _: &ClickEvent, window, _cx| { - window.focus(&editor_focus_handle); + .on_click(cx.listener(move |_, _: &ClickEvent, window, cx| { + window.focus(&editor_focus_handle, cx); })) .child( div() diff --git a/crates/git_ui/src/commit_tooltip.rs b/crates/git_ui/src/commit_tooltip.rs index cf6512b0763e128633cfa65f934d8ed18cd6d022..d18770a704ff31d6dffd705baf44defaaf6d8d4a 100644 --- a/crates/git_ui/src/commit_tooltip.rs +++ b/crates/git_ui/src/commit_tooltip.rs @@ -3,7 +3,7 @@ use editor::hover_markdown_style; use futures::Future; use git::blame::BlameEntry; use git::repository::CommitSummary; -use git::{GitRemote, blame::ParsedCommitMessage}; +use git::{GitRemote, commit::ParsedCommitMessage}; use gpui::{ App, Asset, ClipboardItem, Element, Entity, MouseButton, ParentElement, Render, ScrollHandle, StatefulInteractiveElement, WeakEntity, prelude::*, diff --git a/crates/git_ui/src/file_history_view.rs b/crates/git_ui/src/file_history_view.rs index 4e91fe7e06a5823caac5bf00be8f48cc98dc8da4..f48160719ba5d9b00b8961b75e9ea402c80dd06a 100644 --- a/crates/git_ui/src/file_history_view.rs +++ b/crates/git_ui/src/file_history_view.rs @@ -633,9 +633,9 @@ impl Item for FileHistoryView { &mut self, _workspace: &mut Workspace, window: &mut Window, - _cx: &mut Context, + cx: &mut Context, ) { - window.focus(&self.focus_handle); + window.focus(&self.focus_handle, cx); } fn show_toolbar(&self) -> bool { diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rs index 1426ed1e65412da5cb8be22e7592e5a42917b367..1323ee014f76ebde42b8dff436b2abed851d13f0 100644 --- a/crates/git_ui/src/git_panel.rs +++ b/crates/git_ui/src/git_panel.rs @@ -15,12 +15,13 @@ use askpass::AskPassDelegate; use cloud_llm_client::CompletionIntent; use collections::{BTreeMap, HashMap, HashSet}; use db::kvp::KEY_VALUE_STORE; +use editor::RewrapOptions; use editor::{ Direction, Editor, EditorElement, EditorMode, MultiBuffer, MultiBufferOffset, actions::ExpandAllDiffHunks, }; use futures::StreamExt as _; -use git::blame::ParsedCommitMessage; +use git::commit::ParsedCommitMessage; use git::repository::{ Branch, CommitDetails, CommitOptions, CommitSummary, DiffType, FetchOptions, GitCommitter, PushOptions, Remote, RemoteCommandOutput, ResetMode, Upstream, UpstreamTracking, @@ -30,15 +31,14 @@ use git::stash::GitStash; use git::status::StageStatus; use git::{Amend, Signoff, ToggleStaged, repository::RepoPath, status::FileStatus}; use git::{ - ExpandCommitEditor, RestoreTrackedFiles, StageAll, StashAll, StashApply, StashPop, - TrashUntrackedFiles, UnstageAll, + ExpandCommitEditor, GitHostingProviderRegistry, RestoreTrackedFiles, StageAll, StashAll, + StashApply, StashPop, TrashUntrackedFiles, UnstageAll, }; use gpui::{ Action, AsyncApp, AsyncWindowContext, Bounds, ClickEvent, Corner, DismissEvent, Entity, - EventEmitter, FocusHandle, Focusable, KeyContext, ListHorizontalSizingBehavior, - ListSizingBehavior, MouseButton, MouseDownEvent, Point, PromptLevel, ScrollStrategy, - Subscription, Task, UniformListScrollHandle, WeakEntity, actions, anchored, deferred, point, - size, uniform_list, + EventEmitter, FocusHandle, Focusable, KeyContext, MouseButton, MouseDownEvent, Point, + PromptLevel, ScrollStrategy, Subscription, Task, UniformListScrollHandle, WeakEntity, actions, + anchored, deferred, point, size, uniform_list, }; use itertools::Itertools; use language::{Buffer, File}; @@ -46,7 +46,7 @@ use language_model::{ ConfiguredModel, LanguageModelRegistry, LanguageModelRequest, LanguageModelRequestMessage, Role, ZED_CLOUD_PROVIDER_ID, }; -use menu::{Confirm, SecondaryConfirm, SelectFirst, SelectLast, SelectNext, SelectPrevious}; +use menu; use multi_buffer::ExcerptInfo; use notifications::status_toast::{StatusToast, ToastIcon}; use panel::{ @@ -58,7 +58,7 @@ use project::{ git_store::{GitStoreEvent, Repository, RepositoryEvent, RepositoryId, pending_op}, project_settings::{GitPathStyle, ProjectSettings}, }; -use prompt_store::{PromptId, PromptStore, RULES_FILE_NAMES}; +use prompt_store::{BuiltInPrompt, PromptId, PromptStore, RULES_FILE_NAMES}; use serde::{Deserialize, Serialize}; use settings::{Settings, SettingsStore, StatusStyle}; use std::future::Future; @@ -93,6 +93,14 @@ actions!( FocusEditor, /// Focuses on the changes list. FocusChanges, + /// Select next git panel menu item, and show it in the diff view + NextEntry, + /// Select previous git panel menu item, and show it in the diff view + PreviousEntry, + /// Select first git panel menu item, and show it in the diff view + FirstEntry, + /// Select last git panel menu item, and show it in the diff view + LastEntry, /// Toggles automatic co-author suggestions. ToggleFillCoAuthors, /// Toggles sorting entries by path vs status. @@ -204,8 +212,7 @@ const GIT_PANEL_KEY: &str = "GitPanel"; const UPDATE_DEBOUNCE: Duration = Duration::from_millis(50); // TODO: We should revise this part. It seems the indentation width is not aligned with the one in project panel -const TREE_INDENT: f32 = 12.0; -const TREE_INDENT_GUIDE_OFFSET: f32 = 16.0; +const TREE_INDENT: f32 = 16.0; pub fn register(workspace: &mut Workspace) { workspace.register_action(|workspace, _: &ToggleFocus, window, cx| { @@ -279,6 +286,13 @@ impl GitListEntry { _ => None, } } + + fn directory_entry(&self) -> Option<&GitTreeDirEntry> { + match self { + GitListEntry::Directory(entry) => Some(entry), + _ => None, + } + } } enum GitPanelViewMode { @@ -786,20 +800,63 @@ impl GitPanel { pub fn select_entry_by_path( &mut self, path: ProjectPath, - _: &mut Window, + window: &mut Window, cx: &mut Context, ) { let Some(git_repo) = self.active_repository.as_ref() else { return; }; - let Some(repo_path) = git_repo.read(cx).project_path_to_repo_path(&path, cx) else { - return; + + let (repo_path, section) = { + let repo = git_repo.read(cx); + let Some(repo_path) = repo.project_path_to_repo_path(&path, cx) else { + return; + }; + + let section = repo + .status_for_path(&repo_path) + .map(|status| status.status) + .map(|status| { + if repo.had_conflict_on_last_merge_head_change(&repo_path) { + Section::Conflict + } else if status.is_created() { + Section::New + } else { + Section::Tracked + } + }); + + (repo_path, section) }; + + let mut needs_rebuild = false; + if let (Some(section), Some(tree_state)) = (section, self.view_mode.tree_state_mut()) { + let mut current_dir = repo_path.parent(); + while let Some(dir) = current_dir { + let key = TreeKey { + section, + path: RepoPath::from_rel_path(dir), + }; + + if tree_state.expanded_dirs.get(&key) == Some(&false) { + tree_state.expanded_dirs.insert(key, true); + needs_rebuild = true; + } + + current_dir = dir.parent(); + } + } + + if needs_rebuild { + self.update_visible_entries(window, cx); + } + let Some(ix) = self.entry_by_path(&repo_path) else { return; }; + self.selected_entry = Some(ix); - cx.notify(); + self.scroll_to_selected_entry(cx); } fn serialization_key(workspace: &Workspace) -> Option { @@ -887,20 +944,27 @@ impl GitPanel { } fn scroll_to_selected_entry(&mut self, cx: &mut Context) { - if let Some(selected_entry) = self.selected_entry { + let Some(selected_entry) = self.selected_entry else { + cx.notify(); + return; + }; + + let visible_index = match &self.view_mode { + GitPanelViewMode::Flat => Some(selected_entry), + GitPanelViewMode::Tree(state) => state + .logical_indices + .iter() + .position(|&ix| ix == selected_entry), + }; + + if let Some(visible_index) = visible_index { self.scroll_handle - .scroll_to_item(selected_entry, ScrollStrategy::Center); + .scroll_to_item(visible_index, ScrollStrategy::Center); } cx.notify(); } - fn first_status_entry_index(&self) -> Option { - self.entries - .iter() - .position(|entry| entry.status_entry().is_some()) - } - fn expand_selected_entry( &mut self, _: &ExpandSelectedEntry, @@ -913,12 +977,12 @@ impl GitPanel { if let GitListEntry::Directory(dir_entry) = entry { if dir_entry.expanded { - self.select_next(&SelectNext, window, cx); + self.select_next(&menu::SelectNext, window, cx); } else { self.toggle_directory(&dir_entry.key, window, cx); } } else { - self.select_next(&SelectNext, window, cx); + self.select_next(&menu::SelectNext, window, cx); } } @@ -936,15 +1000,34 @@ impl GitPanel { if dir_entry.expanded { self.toggle_directory(&dir_entry.key, window, cx); } else { - self.select_previous(&SelectPrevious, window, cx); + self.select_previous(&menu::SelectPrevious, window, cx); } } else { - self.select_previous(&SelectPrevious, window, cx); + self.select_previous(&menu::SelectPrevious, window, cx); } } - fn select_first(&mut self, _: &SelectFirst, _window: &mut Window, cx: &mut Context) { - if let Some(first_entry) = self.first_status_entry_index() { + fn select_first( + &mut self, + _: &menu::SelectFirst, + _window: &mut Window, + cx: &mut Context, + ) { + let first_entry = match &self.view_mode { + GitPanelViewMode::Flat => self + .entries + .iter() + .position(|entry| entry.status_entry().is_some()), + GitPanelViewMode::Tree(state) => { + let index = self.entries.iter().position(|entry| { + entry.status_entry().is_some() || entry.directory_entry().is_some() + }); + + index.map(|index| state.logical_indices[index]) + } + }; + + if let Some(first_entry) = first_entry { self.selected_entry = Some(first_entry); self.scroll_to_selected_entry(cx); } @@ -952,7 +1035,7 @@ impl GitPanel { fn select_previous( &mut self, - _: &SelectPrevious, + _: &menu::SelectPrevious, _window: &mut Window, cx: &mut Context, ) { @@ -1001,7 +1084,7 @@ impl GitPanel { self.scroll_to_selected_entry(cx); } - fn select_next(&mut self, _: &SelectNext, _window: &mut Window, cx: &mut Context) { + fn select_next(&mut self, _: &menu::SelectNext, _window: &mut Window, cx: &mut Context) { let item_count = self.entries.len(); if item_count == 0 { return; @@ -1039,29 +1122,64 @@ impl GitPanel { self.scroll_to_selected_entry(cx); } - fn select_last(&mut self, _: &SelectLast, _window: &mut Window, cx: &mut Context) { + fn select_last(&mut self, _: &menu::SelectLast, _window: &mut Window, cx: &mut Context) { if self.entries.last().is_some() { self.selected_entry = Some(self.entries.len() - 1); self.scroll_to_selected_entry(cx); } } + /// Show diff view at selected entry, only if the diff view is open + fn move_diff_to_entry(&mut self, window: &mut Window, cx: &mut Context) { + maybe!({ + let workspace = self.workspace.upgrade()?; + + if let Some(project_diff) = workspace.read(cx).item_of_type::(cx) { + let entry = self.entries.get(self.selected_entry?)?.status_entry()?; + + project_diff.update(cx, |project_diff, cx| { + project_diff.move_to_entry(entry.clone(), window, cx); + }); + } + + Some(()) + }); + } + + fn first_entry(&mut self, _: &FirstEntry, window: &mut Window, cx: &mut Context) { + self.select_first(&menu::SelectFirst, window, cx); + self.move_diff_to_entry(window, cx); + } + + fn last_entry(&mut self, _: &LastEntry, window: &mut Window, cx: &mut Context) { + self.select_last(&menu::SelectLast, window, cx); + self.move_diff_to_entry(window, cx); + } + + fn next_entry(&mut self, _: &NextEntry, window: &mut Window, cx: &mut Context) { + self.select_next(&menu::SelectNext, window, cx); + self.move_diff_to_entry(window, cx); + } + + fn previous_entry(&mut self, _: &PreviousEntry, window: &mut Window, cx: &mut Context) { + self.select_previous(&menu::SelectPrevious, window, cx); + self.move_diff_to_entry(window, cx); + } + fn focus_editor(&mut self, _: &FocusEditor, window: &mut Window, cx: &mut Context) { self.commit_editor.update(cx, |editor, cx| { - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); }); cx.notify(); } - fn select_first_entry_if_none(&mut self, cx: &mut Context) { + fn select_first_entry_if_none(&mut self, window: &mut Window, cx: &mut Context) { let have_entries = self .active_repository .as_ref() .is_some_and(|active_repository| active_repository.read(cx).status_summary().count > 0); if have_entries && self.selected_entry.is_none() { - self.selected_entry = self.first_status_entry_index(); - self.scroll_to_selected_entry(cx); - cx.notify(); + self.select_first(&menu::SelectFirst, window, cx); } } @@ -1071,10 +1189,8 @@ impl GitPanel { window: &mut Window, cx: &mut Context, ) { - self.select_first_entry_if_none(cx); - - self.focus_handle.focus(window); - cx.notify(); + self.focus_handle.focus(window, cx); + self.select_first_entry_if_none(window, cx); } fn get_selected_entry(&self) -> Option<&GitListEntry> { @@ -1095,7 +1211,7 @@ impl GitPanel { .project_path_to_repo_path(&project_path, cx) .as_ref() { - project_diff.focus_handle(cx).focus(window); + project_diff.focus_handle(cx).focus(window, cx); project_diff.update(cx, |project_diff, cx| project_diff.autoscroll(cx)); return None; }; @@ -1105,7 +1221,7 @@ impl GitPanel { ProjectDiff::deploy_at(workspace, Some(entry.clone()), window, cx); }) .ok(); - self.focus_handle.focus(window); + self.focus_handle.focus(window, cx); Some(()) }); @@ -1208,14 +1324,14 @@ impl GitPanel { let prompt = window.prompt( PromptLevel::Warning, &format!( - "Are you sure you want to restore {}?", + "Are you sure you want to discard changes to {}?", entry .repo_path .file_name() .unwrap_or(entry.repo_path.display(path_style).as_ref()), ), None, - &["Restore", "Cancel"], + &["Discard Changes", "Cancel"], cx, ); cx.background_spawn(prompt) @@ -2065,7 +2181,13 @@ impl GitPanel { let editor = cx.new(|cx| Editor::for_buffer(buffer, None, window, cx)); let wrapped_message = editor.update(cx, |editor, cx| { editor.select_all(&Default::default(), window, cx); - editor.rewrap(&Default::default(), window, cx); + editor.rewrap_impl( + RewrapOptions { + override_language_settings: false, + preserve_existing_whitespace: true, + }, + cx, + ); editor.text(cx) }); if wrapped_message.trim().is_empty() { @@ -2116,7 +2238,10 @@ impl GitPanel { let commit_message = self.custom_or_suggested_commit_message(window, cx); let Some(mut message) = commit_message else { - self.commit_editor.read(cx).focus_handle(cx).focus(window); + self.commit_editor + .read(cx) + .focus_handle(cx) + .focus(window, cx); return; }; @@ -2454,25 +2579,26 @@ impl GitPanel { is_using_legacy_zed_pro: bool, cx: &mut AsyncApp, ) -> String { - const DEFAULT_PROMPT: &str = include_str!("commit_message_prompt.txt"); - // Remove this once we stop supporting legacy Zed Pro // In legacy Zed Pro, Git commit summary generation did not count as a // prompt. If the user changes the prompt, our classification will fail, // meaning that users will be charged for generating commit messages. if is_using_legacy_zed_pro { - return DEFAULT_PROMPT.to_string(); + return BuiltInPrompt::CommitMessage.default_content().to_string(); } let load = async { let store = cx.update(|cx| PromptStore::global(cx)).ok()?.await.ok()?; store - .update(cx, |s, cx| s.load(PromptId::CommitMessage, cx)) + .update(cx, |s, cx| { + s.load(PromptId::BuiltIn(BuiltInPrompt::CommitMessage), cx) + }) .ok()? .await .ok() }; - load.await.unwrap_or_else(|| DEFAULT_PROMPT.to_string()) + load.await + .unwrap_or_else(|| BuiltInPrompt::CommitMessage.default_content().to_string()) } /// Generates a commit message using an LLM. @@ -2723,93 +2849,15 @@ impl GitPanel { } pub(crate) fn git_clone(&mut self, repo: String, window: &mut Window, cx: &mut Context) { - let path = cx.prompt_for_paths(gpui::PathPromptOptions { - files: false, - directories: true, - multiple: false, - prompt: Some("Select as Repository Destination".into()), - }); - let workspace = self.workspace.clone(); - cx.spawn_in(window, async move |this, cx| { - let mut paths = path.await.ok()?.ok()??; - let mut path = paths.pop()?; - let repo_name = repo.split("/").last()?.strip_suffix(".git")?.to_owned(); - - let fs = this.read_with(cx, |this, _| this.fs.clone()).ok()?; - - let prompt_answer = match fs.git_clone(&repo, path.as_path()).await { - Ok(_) => cx.update(|window, cx| { - window.prompt( - PromptLevel::Info, - &format!("Git Clone: {}", repo_name), - None, - &["Add repo to project", "Open repo in new project"], - cx, - ) - }), - Err(e) => { - this.update(cx, |this: &mut GitPanel, cx| { - let toast = StatusToast::new(e.to_string(), cx, |this, _| { - this.icon(ToastIcon::new(IconName::XCircle).color(Color::Error)) - .dismiss_button(true) - }); - - this.workspace - .update(cx, |workspace, cx| { - workspace.toggle_status_toast(toast, cx); - }) - .ok(); - }) - .ok()?; - - return None; - } - } - .ok()?; - - path.push(repo_name); - match prompt_answer.await.ok()? { - 0 => { - workspace - .update(cx, |workspace, cx| { - workspace - .project() - .update(cx, |project, cx| { - project.create_worktree(path.as_path(), true, cx) - }) - .detach(); - }) - .ok(); - } - 1 => { - workspace - .update(cx, move |workspace, cx| { - workspace::open_new( - Default::default(), - workspace.app_state().clone(), - cx, - move |workspace, _, cx| { - cx.activate(true); - workspace - .project() - .update(cx, |project, cx| { - project.create_worktree(&path, true, cx) - }) - .detach(); - }, - ) - .detach(); - }) - .ok(); - } - _ => {} - } - - Some(()) - }) - .detach(); + crate::clone::clone_and_open( + repo.into(), + workspace, + window, + cx, + Arc::new(|_workspace: &mut workspace::Workspace, _window, _cx| {}), + ); } pub(crate) fn git_init(&mut self, window: &mut Window, cx: &mut Context) { @@ -3549,7 +3597,7 @@ impl GitPanel { self.bulk_staging = bulk_staging; } - self.select_first_entry_if_none(cx); + self.select_first_entry_if_none(window, cx); let suggested_commit_message = self.suggest_commit_message(cx); let placeholder_text = suggested_commit_message.unwrap_or("Enter commit message".into()); @@ -4134,7 +4182,7 @@ impl GitPanel { .border_color(cx.theme().colors().border) .cursor_text() .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { - window.focus(&this.commit_editor.focus_handle(cx)); + window.focus(&this.commit_editor.focus_handle(cx), cx); })) .child( h_flex() @@ -4577,7 +4625,10 @@ impl GitPanel { }, ) .with_render_fn(cx.entity(), |_, params, _, _| { - let left_offset = px(TREE_INDENT_GUIDE_OFFSET); + // Magic number to align the tree item is 3 here + // because we're using 12px as the left-side padding + // and 3 makes the alignment work with the bounding box of the icon + let left_offset = px(TREE_INDENT + 3_f32); let indent_size = params.indent_size; let item_height = params.item_height; @@ -4605,10 +4656,6 @@ impl GitPanel { }) .size_full() .flex_grow() - .with_sizing_behavior(ListSizingBehavior::Auto) - .with_horizontal_sizing_behavior( - ListHorizontalSizingBehavior::Unconstrained, - ) .with_width_from_item(self.max_width_item_index) .track_scroll(&self.scroll_handle), ) @@ -4632,7 +4679,7 @@ impl GitPanel { } fn entry_label(&self, label: impl Into, color: Color) -> Label { - Label::new(label.into()).color(color).single_line() + Label::new(label.into()).color(color) } fn list_item_height(&self) -> Rems { @@ -4654,8 +4701,8 @@ impl GitPanel { .h(self.list_item_height()) .w_full() .items_end() - .px(rems(0.75)) // ~12px - .pb(rems(0.3125)) // ~ 5px + .px_3() + .pb_1() .child( Label::new(header.title()) .color(Color::Muted) @@ -4698,7 +4745,7 @@ impl GitPanel { let restore_title = if entry.status.is_created() { "Trash File" } else { - "Restore File" + "Discard Changes" }; let context_menu = ContextMenu::build(window, cx, |context_menu, _, _| { let is_created = entry.status.is_created(); @@ -4712,8 +4759,8 @@ impl GitPanel { git::AddToGitignore.boxed_clone(), ) .separator() - .action("Open Diff", Confirm.boxed_clone()) - .action("Open File", SecondaryConfirm.boxed_clone()) + .action("Open Diff", menu::Confirm.boxed_clone()) + .action("Open File", menu::SecondaryConfirm.boxed_clone()) .separator() .action_disabled_when(is_created, "View File History", Box::new(git::FileHistory)) }); @@ -4843,113 +4890,68 @@ impl GitPanel { let marked_bg_alpha = 0.12; let state_opacity_step = 0.04; + let info_color = cx.theme().status().info; + let base_bg = match (selected, marked) { - (true, true) => cx - .theme() - .status() - .info - .alpha(selected_bg_alpha + marked_bg_alpha), - (true, false) => cx.theme().status().info.alpha(selected_bg_alpha), - (false, true) => cx.theme().status().info.alpha(marked_bg_alpha), + (true, true) => info_color.alpha(selected_bg_alpha + marked_bg_alpha), + (true, false) => info_color.alpha(selected_bg_alpha), + (false, true) => info_color.alpha(marked_bg_alpha), _ => cx.theme().colors().ghost_element_background, }; - let hover_bg = if selected { - cx.theme() - .status() - .info - .alpha(selected_bg_alpha + state_opacity_step) - } else { - cx.theme().colors().ghost_element_hover - }; - - let active_bg = if selected { - cx.theme() - .status() - .info - .alpha(selected_bg_alpha + state_opacity_step * 2.0) + let (hover_bg, active_bg) = if selected { + ( + info_color.alpha(selected_bg_alpha + state_opacity_step), + info_color.alpha(selected_bg_alpha + state_opacity_step * 2.0), + ) } else { - cx.theme().colors().ghost_element_active + ( + cx.theme().colors().ghost_element_hover, + cx.theme().colors().ghost_element_active, + ) }; - let mut name_row = h_flex() - .items_center() - .gap_1() + let name_row = h_flex() + .min_w_0() .flex_1() - .pl(if tree_view { - px(depth as f32 * TREE_INDENT) - } else { - px(0.) - }) - .child(git_status_icon(status)); - - name_row = if tree_view { - name_row.child( - self.entry_label(display_name, label_color) - .when(status.is_deleted(), Label::strikethrough) - .truncate(), - ) - } else { - name_row.child(h_flex().items_center().flex_1().map(|this| { - self.path_formatted( - this, - entry.parent_dir(path_style), - path_color, - display_name, - label_color, - path_style, - git_path_style, - status.is_deleted(), - ) - })) - }; + .gap_1() + .child(git_status_icon(status)) + .map(|this| { + if tree_view { + this.pl(px(depth as f32 * TREE_INDENT)).child( + self.entry_label(display_name, label_color) + .when(status.is_deleted(), Label::strikethrough) + .truncate(), + ) + } else { + this.child(self.path_formatted( + entry.parent_dir(path_style), + path_color, + display_name, + label_color, + path_style, + git_path_style, + status.is_deleted(), + )) + } + }); h_flex() .id(id) .h(self.list_item_height()) .w_full() + .pl_3() + .pr_1() + .gap_1p5() .border_1() .border_r_2() .when(selected && self.focus_handle.is_focused(window), |el| { el.border_color(cx.theme().colors().panel_focused_border) }) - .px(rems(0.75)) // ~12px - .overflow_hidden() - .flex_none() - .gap_1p5() .bg(base_bg) - .hover(|this| this.bg(hover_bg)) - .active(|this| this.bg(active_bg)) - .on_click({ - cx.listener(move |this, event: &ClickEvent, window, cx| { - this.selected_entry = Some(ix); - cx.notify(); - if event.modifiers().secondary() { - this.open_file(&Default::default(), window, cx) - } else { - this.open_diff(&Default::default(), window, cx); - this.focus_handle.focus(window); - } - }) - }) - .on_mouse_down( - MouseButton::Right, - move |event: &MouseDownEvent, window, cx| { - // why isn't this happening automatically? we are passing MouseButton::Right to `on_mouse_down`? - if event.button != MouseButton::Right { - return; - } - - let Some(this) = handle.upgrade() else { - return; - }; - this.update(cx, |this, cx| { - this.deploy_entry_context_menu(event.position, ix, window, cx); - }); - cx.stop_propagation(); - }, - ) - .child(name_row.overflow_x_hidden()) + .hover(|s| s.bg(hover_bg)) + .active(|s| s.bg(active_bg)) + .child(name_row) .child( div() .id(checkbox_wrapper_id) @@ -4999,6 +5001,35 @@ impl GitPanel { }), ), ) + .on_click({ + cx.listener(move |this, event: &ClickEvent, window, cx| { + this.selected_entry = Some(ix); + cx.notify(); + if event.modifiers().secondary() { + this.open_file(&Default::default(), window, cx) + } else { + this.open_diff(&Default::default(), window, cx); + this.focus_handle.focus(window, cx); + } + }) + }) + .on_mouse_down( + MouseButton::Right, + move |event: &MouseDownEvent, window, cx| { + // why isn't this happening automatically? we are passing MouseButton::Right to `on_mouse_down`? + if event.button != MouseButton::Right { + return; + } + + let Some(this) = handle.upgrade() else { + return; + }; + this.update(cx, |this, cx| { + this.deploy_entry_context_menu(event.position, ix, window, cx); + }); + cx.stop_propagation(); + }, + ) .into_any_element() } @@ -5023,29 +5054,23 @@ impl GitPanel { let selected_bg_alpha = 0.08; let state_opacity_step = 0.04; - let base_bg = if selected { - cx.theme().status().info.alpha(selected_bg_alpha) - } else { - cx.theme().colors().ghost_element_background - }; + let info_color = cx.theme().status().info; + let colors = cx.theme().colors(); - let hover_bg = if selected { - cx.theme() - .status() - .info - .alpha(selected_bg_alpha + state_opacity_step) + let (base_bg, hover_bg, active_bg) = if selected { + ( + info_color.alpha(selected_bg_alpha), + info_color.alpha(selected_bg_alpha + state_opacity_step), + info_color.alpha(selected_bg_alpha + state_opacity_step * 2.0), + ) } else { - cx.theme().colors().ghost_element_hover + ( + colors.ghost_element_background, + colors.ghost_element_hover, + colors.ghost_element_active, + ) }; - let active_bg = if selected { - cx.theme() - .status() - .info - .alpha(selected_bg_alpha + state_opacity_step * 2.0) - } else { - cx.theme().colors().ghost_element_active - }; let folder_icon = if entry.expanded { IconName::FolderOpen } else { @@ -5068,9 +5093,8 @@ impl GitPanel { }; let name_row = h_flex() - .items_center() + .min_w_0() .gap_1() - .flex_1() .pl(px(entry.depth as f32 * TREE_INDENT)) .child( Icon::new(folder_icon) @@ -5082,28 +5106,21 @@ impl GitPanel { h_flex() .id(id) .h(self.list_item_height()) + .min_w_0() .w_full() - .items_center() + .pl_3() + .pr_1() + .gap_1p5() + .justify_between() .border_1() .border_r_2() .when(selected && self.focus_handle.is_focused(window), |el| { el.border_color(cx.theme().colors().panel_focused_border) }) - .px(rems(0.75)) - .overflow_hidden() - .flex_none() - .gap_1p5() .bg(base_bg) - .hover(|this| this.bg(hover_bg)) - .active(|this| this.bg(active_bg)) - .on_click({ - let key = entry.key.clone(); - cx.listener(move |this, _event: &ClickEvent, window, cx| { - this.selected_entry = Some(ix); - this.toggle_directory(&key, window, cx); - }) - }) - .child(name_row.overflow_x_hidden()) + .hover(|s| s.bg(hover_bg)) + .active(|s| s.bg(active_bg)) + .child(name_row) .child( div() .id(checkbox_wrapper_id) @@ -5142,12 +5159,18 @@ impl GitPanel { }), ), ) + .on_click({ + let key = entry.key.clone(); + cx.listener(move |this, _event: &ClickEvent, window, cx| { + this.selected_entry = Some(ix); + this.toggle_directory(&key, window, cx); + }) + }) .into_any_element() } fn path_formatted( &self, - parent: Div, directory: Option, path_color: Color, file_name: String, @@ -5156,41 +5179,31 @@ impl GitPanel { git_path_style: GitPathStyle, strikethrough: bool, ) -> Div { - parent - .when(git_path_style == GitPathStyle::FileNameFirst, |this| { - this.child( - self.entry_label( - match directory.as_ref().is_none_or(|d| d.is_empty()) { - true => file_name.clone(), - false => format!("{file_name} "), - }, - label_color, - ) - .when(strikethrough, Label::strikethrough), - ) - }) - .when_some(directory, |this, dir| { - match ( - !dir.is_empty(), - git_path_style == GitPathStyle::FileNameFirst, - ) { - (true, true) => this.child( - self.entry_label(dir, path_color) - .when(strikethrough, Label::strikethrough), - ), - (true, false) => this.child( - self.entry_label( - format!("{dir}{}", path_style.primary_separator()), - path_color, - ) + let file_name_first = git_path_style == GitPathStyle::FileNameFirst; + let file_path_first = git_path_style == GitPathStyle::FilePathFirst; + + let file_name = format!("{} ", file_name); + + h_flex() + .min_w_0() + .overflow_hidden() + .when(file_path_first, |this| this.flex_row_reverse()) + .child( + div().flex_none().child( + self.entry_label(file_name, label_color) .when(strikethrough, Label::strikethrough), - ), - _ => this, - } - }) - .when(git_path_style == GitPathStyle::FilePathFirst, |this| { + ), + ) + .when_some(directory, |this, dir| { + let path_name = if file_name_first { + dir + } else { + format!("{dir}{}", path_style.primary_separator()) + }; + this.child( - self.entry_label(file_name, label_color) + self.entry_label(path_name, path_color) + .truncate_start() .when(strikethrough, Label::strikethrough), ) }) @@ -5376,6 +5389,10 @@ impl Render for GitPanel { .on_action(cx.listener(Self::select_next)) .on_action(cx.listener(Self::select_previous)) .on_action(cx.listener(Self::select_last)) + .on_action(cx.listener(Self::first_entry)) + .on_action(cx.listener(Self::next_entry)) + .on_action(cx.listener(Self::previous_entry)) + .on_action(cx.listener(Self::last_entry)) .on_action(cx.listener(Self::close_panel)) .on_action(cx.listener(Self::open_diff)) .on_action(cx.listener(Self::open_file)) @@ -5526,6 +5543,7 @@ impl GitPanelMessageTooltip { window: &mut Window, cx: &mut App, ) -> Entity { + let remote_url = repository.read(cx).default_remote_url(); cx.new(|cx| { cx.spawn_in(window, async move |this, cx| { let (details, workspace) = git_panel.update(cx, |git_panel, cx| { @@ -5535,16 +5553,21 @@ impl GitPanelMessageTooltip { ) })?; let details = details.await?; + let provider_registry = cx + .update(|_, app| GitHostingProviderRegistry::default_global(app)) + .ok(); let commit_details = crate::commit_tooltip::CommitDetails { sha: details.sha.clone(), author_name: details.author_name.clone(), author_email: details.author_email.clone(), commit_time: OffsetDateTime::from_unix_timestamp(details.commit_timestamp)?, - message: Some(ParsedCommitMessage { - message: details.message, - ..Default::default() - }), + message: Some(ParsedCommitMessage::parse( + details.sha.to_string(), + details.message.to_string(), + remote_url.as_deref(), + provider_registry, + )), }; this.update(cx, |this: &mut GitPanelMessageTooltip, cx| { @@ -6841,7 +6864,7 @@ mod tests { // the Project Diff's active path. panel.update_in(cx, |panel, window, cx| { panel.selected_entry = Some(1); - panel.open_diff(&Confirm, window, cx); + panel.open_diff(&menu::Confirm, window, cx); }); cx.run_until_parked(); @@ -6857,6 +6880,128 @@ mod tests { }); } + #[gpui::test] + async fn test_tree_view_reveals_collapsed_parent_on_select_entry_by_path( + cx: &mut TestAppContext, + ) { + init_test(cx); + + let fs = FakeFs::new(cx.background_executor.clone()); + fs.insert_tree( + path!("/project"), + json!({ + ".git": {}, + "src": { + "a": { + "foo.rs": "fn foo() {}", + }, + "b": { + "bar.rs": "fn bar() {}", + }, + }, + }), + ) + .await; + + fs.set_status_for_repo( + path!("/project/.git").as_ref(), + &[ + ("src/a/foo.rs", StatusCode::Modified.worktree()), + ("src/b/bar.rs", StatusCode::Modified.worktree()), + ], + ); + + let project = Project::test(fs.clone(), [Path::new(path!("/project"))], cx).await; + let workspace = + cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx)); + let cx = &mut VisualTestContext::from_window(*workspace, cx); + + cx.read(|cx| { + project + .read(cx) + .worktrees(cx) + .next() + .unwrap() + .read(cx) + .as_local() + .unwrap() + .scan_complete() + }) + .await; + + cx.executor().run_until_parked(); + + cx.update(|_window, cx| { + SettingsStore::update_global(cx, |store, cx| { + store.update_user_settings(cx, |settings| { + settings.git_panel.get_or_insert_default().tree_view = Some(true); + }) + }); + }); + + let panel = workspace.update(cx, GitPanel::new).unwrap(); + + let handle = cx.update_window_entity(&panel, |panel, _, _| { + std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(())) + }); + cx.executor().advance_clock(2 * UPDATE_DEBOUNCE); + handle.await; + + let src_key = panel.read_with(cx, |panel, _| { + panel + .entries + .iter() + .find_map(|entry| match entry { + GitListEntry::Directory(dir) if dir.key.path == repo_path("src") => { + Some(dir.key.clone()) + } + _ => None, + }) + .expect("src directory should exist in tree view") + }); + + panel.update_in(cx, |panel, window, cx| { + panel.toggle_directory(&src_key, window, cx); + }); + + panel.read_with(cx, |panel, _| { + let state = panel + .view_mode + .tree_state() + .expect("tree view state should exist"); + assert_eq!(state.expanded_dirs.get(&src_key).copied(), Some(false)); + }); + + let worktree_id = + cx.read(|cx| project.read(cx).worktrees(cx).next().unwrap().read(cx).id()); + let project_path = ProjectPath { + worktree_id, + path: RelPath::unix("src/a/foo.rs").unwrap().into_arc(), + }; + + panel.update_in(cx, |panel, window, cx| { + panel.select_entry_by_path(project_path, window, cx); + }); + + panel.read_with(cx, |panel, _| { + let state = panel + .view_mode + .tree_state() + .expect("tree view state should exist"); + assert_eq!(state.expanded_dirs.get(&src_key).copied(), Some(true)); + + let selected_ix = panel.selected_entry.expect("selection should be set"); + assert!(state.logical_indices.contains(&selected_ix)); + + let selected_entry = panel + .entries + .get(selected_ix) + .and_then(|entry| entry.status_entry()) + .expect("selected entry should be a status entry"); + assert_eq!(selected_entry.repo_path, repo_path("src/a/foo.rs")); + }); + } + fn assert_entry_paths(entries: &[GitListEntry], expected_paths: &[Option<&str>]) { assert_eq!(entries.len(), expected_paths.len()); for (entry, expected_path) in entries.iter().zip(expected_paths) { diff --git a/crates/git_ui/src/git_ui.rs b/crates/git_ui/src/git_ui.rs index 54adc8130d78e80af5c561541efb8128f1b2a017..053c41bf10c5d97f9f5326fd17d6b5bf91297a03 100644 --- a/crates/git_ui/src/git_ui.rs +++ b/crates/git_ui/src/git_ui.rs @@ -10,6 +10,7 @@ use ui::{ }; mod blame_ui; +pub mod clone; use git::{ repository::{Branch, Upstream, UpstreamTracking, UpstreamTrackingStatus}, @@ -817,7 +818,7 @@ impl GitCloneModal { }); let focus_handle = repo_input.focus_handle(cx); - window.focus(&focus_handle); + window.focus(&focus_handle, cx); Self { panel, diff --git a/crates/git_ui/src/onboarding.rs b/crates/git_ui/src/onboarding.rs index d1709e043b92216e974c1a4f451db5c28b98f773..eccb18a5400647ff86e44f4426d271d6c9361164 100644 --- a/crates/git_ui/src/onboarding.rs +++ b/crates/git_ui/src/onboarding.rs @@ -85,8 +85,8 @@ impl Render for GitOnboardingModal { git_onboarding_event!("Cancelled", trigger = "Action"); cx.emit(DismissEvent); })) - .on_any_mouse_down(cx.listener(|this, _: &MouseDownEvent, window, _cx| { - this.focus_handle.focus(window); + .on_any_mouse_down(cx.listener(|this, _: &MouseDownEvent, window, cx| { + this.focus_handle.focus(window, cx); })) .child( div().p_1p5().absolute().inset_0().h(px(160.)).child( diff --git a/crates/git_ui/src/project_diff.rs b/crates/git_ui/src/project_diff.rs index 4d7a27354b1b4b6e972579e73c48bcd4c2448a5c..0e0632d9d049f54a648f65c55a96d639c9103e4d 100644 --- a/crates/git_ui/src/project_diff.rs +++ b/crates/git_ui/src/project_diff.rs @@ -492,7 +492,7 @@ impl ProjectDiff { if editor.focus_handle(cx).contains_focused(window, cx) && self.multibuffer.read(cx).is_empty() { - self.focus_handle.focus(window) + self.focus_handle.focus(window, cx) } } @@ -597,10 +597,10 @@ impl ProjectDiff { .focus_handle(cx) .contains_focused(window, cx) { - self.focus_handle.focus(window); + self.focus_handle.focus(window, cx); } else if self.focus_handle.is_focused(window) && !self.multibuffer.read(cx).is_empty() { self.editor.update(cx, |editor, cx| { - editor.focus_handle(cx).focus(window); + editor.focus_handle(cx).focus(window, cx); }); } if self.pending_scroll.as_ref() == Some(&path_key) { @@ -983,7 +983,7 @@ impl Render for ProjectDiff { cx, )) .on_click(move |_, window, cx| { - window.focus(&keybinding_focus_handle); + window.focus(&keybinding_focus_handle, cx); window.dispatch_action( Box::new(CloseActiveItem::default()), cx, @@ -1153,7 +1153,7 @@ impl ProjectDiffToolbar { fn dispatch_action(&self, action: &dyn Action, window: &mut Window, cx: &mut Context) { if let Some(project_diff) = self.project_diff(cx) { - project_diff.focus_handle(cx).focus(window); + project_diff.focus_handle(cx).focus(window, cx); } let action = action.boxed_clone(); cx.defer(move |cx| { diff --git a/crates/git_ui/src/worktree_picker.rs b/crates/git_ui/src/worktree_picker.rs index 875ae55eefae19e24aa26fe75f80d70f8316c82b..fef5e16c80ddd26ae6dd0b2a5c0ad1d8e5b21b2c 100644 --- a/crates/git_ui/src/worktree_picker.rs +++ b/crates/git_ui/src/worktree_picker.rs @@ -1,4 +1,5 @@ use anyhow::Context as _; +use collections::HashSet; use fuzzy::StringMatchCandidate; use git::repository::Worktree as GitWorktree; @@ -9,7 +10,11 @@ use gpui::{ actions, rems, }; use picker::{Picker, PickerDelegate, PickerEditorPosition}; -use project::{DirectoryLister, git_store::Repository}; +use project::{ + DirectoryLister, + git_store::Repository, + trusted_worktrees::{PathTrust, RemoteHostLocation, TrustedWorktrees}, +}; use recent_projects::{RemoteConnectionModal, connect}; use remote::{RemoteConnectionOptions, remote_client::ConnectionIdentifier}; use std::{path::PathBuf, sync::Arc}; @@ -219,7 +224,6 @@ impl WorktreeListDelegate { window: &mut Window, cx: &mut Context>, ) { - let workspace = self.workspace.clone(); let Some(repo) = self.repo.clone() else { return; }; @@ -247,6 +251,7 @@ impl WorktreeListDelegate { let branch = worktree_branch.to_string(); let window_handle = window.window_handle(); + let workspace = self.workspace.clone(); cx.spawn_in(window, async move |_, cx| { let Some(paths) = worktree_path.await? else { return anyhow::Ok(()); @@ -257,8 +262,32 @@ impl WorktreeListDelegate { repo.create_worktree(branch.clone(), path.clone(), commit) })? .await??; - - let final_path = path.join(branch); + let new_worktree_path = path.join(branch); + + workspace.update(cx, |workspace, cx| { + if let Some(trusted_worktrees) = TrustedWorktrees::try_get_global(cx) { + let repo_path = &repo.read(cx).snapshot().work_directory_abs_path; + let project = workspace.project(); + if let Some((parent_worktree, _)) = + project.read(cx).find_worktree(repo_path, cx) + { + trusted_worktrees.update(cx, |trusted_worktrees, cx| { + if trusted_worktrees.can_trust(parent_worktree.read(cx).id(), cx) { + trusted_worktrees.trust( + HashSet::from_iter([PathTrust::AbsPath( + new_worktree_path.clone(), + )]), + project + .read(cx) + .remote_connection_options(cx) + .map(RemoteHostLocation::from), + cx, + ); + } + }); + } + } + })?; let (connection_options, app_state, is_local) = workspace.update(cx, |workspace, cx| { @@ -274,7 +303,7 @@ impl WorktreeListDelegate { .update_in(cx, |workspace, window, cx| { workspace.open_workspace_for_paths( replace_current_window, - vec![final_path], + vec![new_worktree_path], window, cx, ) @@ -283,7 +312,7 @@ impl WorktreeListDelegate { } else if let Some(connection_options) = connection_options { open_remote_worktree( connection_options, - vec![final_path], + vec![new_worktree_path], app_state, window_handle, replace_current_window, diff --git a/crates/go_to_line/src/go_to_line.rs b/crates/go_to_line/src/go_to_line.rs index 461b0be659fc3ffb7b7bc984485dc68ece988500..7c42972a75420ae87bf3c5b9caaf041852efc009 100644 --- a/crates/go_to_line/src/go_to_line.rs +++ b/crates/go_to_line/src/go_to_line.rs @@ -268,7 +268,7 @@ impl GoToLine { cx, |s| s.select_anchor_ranges([start..start]), ); - editor.focus_handle(cx).focus(window); + editor.focus_handle(cx).focus(window, cx); cx.notify() }); self.prev_scroll_position.take(); diff --git a/crates/google_ai/src/google_ai.rs b/crates/google_ai/src/google_ai.rs index 3eff860e16f15fae76d8f9cb2523d2b91b611125..a7d82c584b208cec33075d65a53a74c963ec05b5 100644 --- a/crates/google_ai/src/google_ai.rs +++ b/crates/google_ai/src/google_ai.rs @@ -512,6 +512,8 @@ pub enum Model { Gemini25Pro, #[serde(rename = "gemini-3-pro-preview")] Gemini3Pro, + #[serde(rename = "gemini-3-flash-preview")] + Gemini3Flash, #[serde(rename = "custom")] Custom { name: String, @@ -534,6 +536,7 @@ impl Model { Self::Gemini25Flash => "gemini-2.5-flash", Self::Gemini25Pro => "gemini-2.5-pro", Self::Gemini3Pro => "gemini-3-pro-preview", + Self::Gemini3Flash => "gemini-3-flash-preview", Self::Custom { name, .. } => name, } } @@ -543,6 +546,7 @@ impl Model { Self::Gemini25Flash => "gemini-2.5-flash", Self::Gemini25Pro => "gemini-2.5-pro", Self::Gemini3Pro => "gemini-3-pro-preview", + Self::Gemini3Flash => "gemini-3-flash-preview", Self::Custom { name, .. } => name, } } @@ -553,6 +557,7 @@ impl Model { Self::Gemini25Flash => "Gemini 2.5 Flash", Self::Gemini25Pro => "Gemini 2.5 Pro", Self::Gemini3Pro => "Gemini 3 Pro", + Self::Gemini3Flash => "Gemini 3 Flash", Self::Custom { name, display_name, .. } => display_name.as_ref().unwrap_or(name), @@ -561,20 +566,22 @@ impl Model { pub fn max_token_count(&self) -> u64 { match self { - Self::Gemini25FlashLite => 1_048_576, - Self::Gemini25Flash => 1_048_576, - Self::Gemini25Pro => 1_048_576, - Self::Gemini3Pro => 1_048_576, + Self::Gemini25FlashLite + | Self::Gemini25Flash + | Self::Gemini25Pro + | Self::Gemini3Pro + | Self::Gemini3Flash => 1_048_576, Self::Custom { max_tokens, .. } => *max_tokens, } } pub fn max_output_tokens(&self) -> Option { match self { - Model::Gemini25FlashLite => Some(65_536), - Model::Gemini25Flash => Some(65_536), - Model::Gemini25Pro => Some(65_536), - Model::Gemini3Pro => Some(65_536), + Model::Gemini25FlashLite + | Model::Gemini25Flash + | Model::Gemini25Pro + | Model::Gemini3Pro + | Model::Gemini3Flash => Some(65_536), Model::Custom { .. } => None, } } @@ -599,6 +606,7 @@ impl Model { budget_tokens: None, } } + Self::Gemini3Flash => GoogleModelMode::Default, Self::Custom { mode, .. } => *mode, } } diff --git a/crates/gpui/Cargo.toml b/crates/gpui/Cargo.toml index da7e660a0171f38b8dd61de1c9323773ded2589b..40376f476b6d80f6b5170840f295a71acdfebb7d 100644 --- a/crates/gpui/Cargo.toml +++ b/crates/gpui/Cargo.toml @@ -198,14 +198,14 @@ wayland-backend = { version = "0.3.3", features = [ "client_system", "dlopen", ], optional = true } -wayland-client = { version = "0.31.2", optional = true } -wayland-cursor = { version = "0.31.1", optional = true } -wayland-protocols = { version = "0.31.2", features = [ +wayland-client = { version = "0.31.11", optional = true } +wayland-cursor = { version = "0.31.11", optional = true } +wayland-protocols = { version = "0.32.9", features = [ "client", "staging", "unstable", ], optional = true } -wayland-protocols-plasma = { version = "0.2.0", features = [ +wayland-protocols-plasma = { version = "0.3.9", features = [ "client", ], optional = true } wayland-protocols-wlr = { version = "0.3.9", features = [ diff --git a/crates/gpui/examples/focus_visible.rs b/crates/gpui/examples/focus_visible.rs index 737317cabadb7d3358c9c0497b52d4c2ff2e1028..d7c15396f0381ef29b3d6600347fd90a602256f5 100644 --- a/crates/gpui/examples/focus_visible.rs +++ b/crates/gpui/examples/focus_visible.rs @@ -29,7 +29,7 @@ impl Example { ]; let focus_handle = cx.focus_handle(); - window.focus(&focus_handle); + window.focus(&focus_handle, cx); Self { focus_handle, @@ -40,13 +40,13 @@ impl Example { } } - fn on_tab(&mut self, _: &Tab, window: &mut Window, _: &mut Context) { - window.focus_next(); + fn on_tab(&mut self, _: &Tab, window: &mut Window, cx: &mut Context) { + window.focus_next(cx); self.message = SharedString::from("Pressed Tab - focus-visible border should appear!"); } - fn on_tab_prev(&mut self, _: &TabPrev, window: &mut Window, _: &mut Context) { - window.focus_prev(); + fn on_tab_prev(&mut self, _: &TabPrev, window: &mut Window, cx: &mut Context) { + window.focus_prev(cx); self.message = SharedString::from("Pressed Shift-Tab - focus-visible border should appear!"); } diff --git a/crates/gpui/examples/input.rs b/crates/gpui/examples/input.rs index 37115feaa551a787562e7299c9d44bcc97b5fca3..44fae4ffe6bb9e120a8f96c10e0af8f4f8026cdd 100644 --- a/crates/gpui/examples/input.rs +++ b/crates/gpui/examples/input.rs @@ -736,7 +736,7 @@ fn main() { window .update(cx, |view, window, cx| { - window.focus(&view.text_input.focus_handle(cx)); + window.focus(&view.text_input.focus_handle(cx), cx); cx.activate(true); }) .unwrap(); diff --git a/crates/gpui/examples/on_window_close_quit.rs b/crates/gpui/examples/on_window_close_quit.rs index 8fe24001445d94b1629bf766294d850d0918a5e8..9a2b2f2fee43f753aece55d076be647ad8060965 100644 --- a/crates/gpui/examples/on_window_close_quit.rs +++ b/crates/gpui/examples/on_window_close_quit.rs @@ -55,7 +55,7 @@ fn main() { cx.activate(false); cx.new(|cx| { let focus_handle = cx.focus_handle(); - focus_handle.focus(window); + focus_handle.focus(window, cx); ExampleWindow { focus_handle } }) }, @@ -72,7 +72,7 @@ fn main() { |window, cx| { cx.new(|cx| { let focus_handle = cx.focus_handle(); - focus_handle.focus(window); + focus_handle.focus(window, cx); ExampleWindow { focus_handle } }) }, diff --git a/crates/gpui/examples/tab_stop.rs b/crates/gpui/examples/tab_stop.rs index 8dbcbeccb7351fda18e8d36fe38d8f26c4a70cc9..4d99da1a07a123e9a18b3c64a90834c31bd76909 100644 --- a/crates/gpui/examples/tab_stop.rs +++ b/crates/gpui/examples/tab_stop.rs @@ -22,7 +22,7 @@ impl Example { ]; let focus_handle = cx.focus_handle(); - window.focus(&focus_handle); + window.focus(&focus_handle, cx); Self { focus_handle, @@ -31,13 +31,13 @@ impl Example { } } - fn on_tab(&mut self, _: &Tab, window: &mut Window, _: &mut Context) { - window.focus_next(); + fn on_tab(&mut self, _: &Tab, window: &mut Window, cx: &mut Context) { + window.focus_next(cx); self.message = SharedString::from("You have pressed `Tab`."); } - fn on_tab_prev(&mut self, _: &TabPrev, window: &mut Window, _: &mut Context) { - window.focus_prev(); + fn on_tab_prev(&mut self, _: &TabPrev, window: &mut Window, cx: &mut Context) { + window.focus_prev(cx); self.message = SharedString::from("You have pressed `Shift-Tab`."); } } diff --git a/crates/gpui/examples/window.rs b/crates/gpui/examples/window.rs index 06003c4663ee5711283a85684c25b9f5d8c5b743..3f41f3d55f240e688965ac8248ac3d5b4ef40401 100644 --- a/crates/gpui/examples/window.rs +++ b/crates/gpui/examples/window.rs @@ -5,6 +5,7 @@ use gpui::{ struct SubWindow { custom_titlebar: bool, + is_dialog: bool, } fn button(text: &str, on_click: impl Fn(&mut Window, &mut App) + 'static) -> impl IntoElement { @@ -23,7 +24,10 @@ fn button(text: &str, on_click: impl Fn(&mut Window, &mut App) + 'static) -> imp } impl Render for SubWindow { - fn render(&mut self, _window: &mut Window, _: &mut Context) -> impl IntoElement { + fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { + let window_bounds = + WindowBounds::Windowed(Bounds::centered(None, size(px(250.0), px(200.0)), cx)); + div() .flex() .flex_col() @@ -52,8 +56,28 @@ impl Render for SubWindow { .child( div() .p_8() + .flex() + .flex_col() .gap_2() .child("SubWindow") + .when(self.is_dialog, |div| { + div.child(button("Open Nested Dialog", move |_, cx| { + cx.open_window( + WindowOptions { + window_bounds: Some(window_bounds), + kind: WindowKind::Dialog, + ..Default::default() + }, + |_, cx| { + cx.new(|_| SubWindow { + custom_titlebar: false, + is_dialog: true, + }) + }, + ) + .unwrap(); + })) + }) .child(button("Close", |window, _| { window.remove_window(); })), @@ -86,6 +110,7 @@ impl Render for WindowDemo { |_, cx| { cx.new(|_| SubWindow { custom_titlebar: false, + is_dialog: false, }) }, ) @@ -101,6 +126,39 @@ impl Render for WindowDemo { |_, cx| { cx.new(|_| SubWindow { custom_titlebar: false, + is_dialog: false, + }) + }, + ) + .unwrap(); + })) + .child(button("Floating", move |_, cx| { + cx.open_window( + WindowOptions { + window_bounds: Some(window_bounds), + kind: WindowKind::Floating, + ..Default::default() + }, + |_, cx| { + cx.new(|_| SubWindow { + custom_titlebar: false, + is_dialog: false, + }) + }, + ) + .unwrap(); + })) + .child(button("Dialog", move |_, cx| { + cx.open_window( + WindowOptions { + window_bounds: Some(window_bounds), + kind: WindowKind::Dialog, + ..Default::default() + }, + |_, cx| { + cx.new(|_| SubWindow { + custom_titlebar: false, + is_dialog: true, }) }, ) @@ -116,6 +174,7 @@ impl Render for WindowDemo { |_, cx| { cx.new(|_| SubWindow { custom_titlebar: true, + is_dialog: false, }) }, ) @@ -131,6 +190,7 @@ impl Render for WindowDemo { |_, cx| { cx.new(|_| SubWindow { custom_titlebar: false, + is_dialog: false, }) }, ) @@ -147,6 +207,7 @@ impl Render for WindowDemo { |_, cx| { cx.new(|_| SubWindow { custom_titlebar: false, + is_dialog: false, }) }, ) @@ -162,6 +223,7 @@ impl Render for WindowDemo { |_, cx| { cx.new(|_| SubWindow { custom_titlebar: false, + is_dialog: false, }) }, ) @@ -177,6 +239,7 @@ impl Render for WindowDemo { |_, cx| { cx.new(|_| SubWindow { custom_titlebar: false, + is_dialog: false, }) }, ) diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index aa1acae33b8fb55fc5e2f8fa8c0f5b8bb91758f3..96f815ac0b592600f22b3c9b9686571487ff77a2 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -316,6 +316,7 @@ impl SystemWindowTabController { .find_map(|(group, tabs)| tabs.iter().find(|tab| tab.id == id).map(|_| group)); let current_group = current_group?; + // TODO: `.keys()` returns arbitrary order, what does "next" mean? let mut group_ids: Vec<_> = controller.tab_groups.keys().collect(); let idx = group_ids.iter().position(|g| *g == current_group)?; let next_idx = (idx + 1) % group_ids.len(); @@ -340,6 +341,7 @@ impl SystemWindowTabController { .find_map(|(group, tabs)| tabs.iter().find(|tab| tab.id == id).map(|_| group)); let current_group = current_group?; + // TODO: `.keys()` returns arbitrary order, what does "previous" mean? let mut group_ids: Vec<_> = controller.tab_groups.keys().collect(); let idx = group_ids.iter().position(|g| *g == current_group)?; let prev_idx = if idx == 0 { @@ -361,12 +363,9 @@ impl SystemWindowTabController { /// Get all tabs in the same window. pub fn tabs(&self, id: WindowId) -> Option<&Vec> { - let tab_group = self - .tab_groups - .iter() - .find_map(|(group, tabs)| tabs.iter().find(|tab| tab.id == id).map(|_| *group))?; - - self.tab_groups.get(&tab_group) + self.tab_groups + .values() + .find(|tabs| tabs.iter().any(|tab| tab.id == id)) } /// Initialize the visibility of the system window tab controller. @@ -441,7 +440,7 @@ impl SystemWindowTabController { /// Insert a tab into a tab group. pub fn add_tab(cx: &mut App, id: WindowId, tabs: Vec) { let mut controller = cx.global_mut::(); - let Some(tab) = tabs.clone().into_iter().find(|tab| tab.id == id) else { + let Some(tab) = tabs.iter().find(|tab| tab.id == id).cloned() else { return; }; @@ -504,16 +503,14 @@ impl SystemWindowTabController { return; }; + let initial_tabs_len = initial_tabs.len(); let mut all_tabs = initial_tabs.clone(); - for tabs in controller.tab_groups.values() { - all_tabs.extend( - tabs.iter() - .filter(|tab| !initial_tabs.contains(tab)) - .cloned(), - ); + + for (_, mut tabs) in controller.tab_groups.drain() { + tabs.retain(|tab| !all_tabs[..initial_tabs_len].contains(tab)); + all_tabs.extend(tabs); } - controller.tab_groups.clear(); controller.tab_groups.insert(0, all_tabs); } @@ -1080,11 +1077,9 @@ impl App { self.platform.window_appearance() } - /// Writes data to the primary selection buffer. - /// Only available on Linux. - #[cfg(any(target_os = "linux", target_os = "freebsd"))] - pub fn write_to_primary(&self, item: ClipboardItem) { - self.platform.write_to_primary(item) + /// Reads data from the platform clipboard. + pub fn read_from_clipboard(&self) -> Option { + self.platform.read_from_clipboard() } /// Writes data to the platform clipboard. @@ -1099,9 +1094,31 @@ impl App { self.platform.read_from_primary() } - /// Reads data from the platform clipboard. - pub fn read_from_clipboard(&self) -> Option { - self.platform.read_from_clipboard() + /// Writes data to the primary selection buffer. + /// Only available on Linux. + #[cfg(any(target_os = "linux", target_os = "freebsd"))] + pub fn write_to_primary(&self, item: ClipboardItem) { + self.platform.write_to_primary(item) + } + + /// Reads data from macOS's "Find" pasteboard. + /// + /// Used to share the current search string between apps. + /// + /// https://developer.apple.com/documentation/appkit/nspasteboard/name-swift.struct/find + #[cfg(target_os = "macos")] + pub fn read_from_find_pasteboard(&self) -> Option { + self.platform.read_from_find_pasteboard() + } + + /// Writes data to macOS's "Find" pasteboard. + /// + /// Used to share the current search string between apps. + /// + /// https://developer.apple.com/documentation/appkit/nspasteboard/name-swift.struct/find + #[cfg(target_os = "macos")] + pub fn write_to_find_pasteboard(&self, item: ClipboardItem) { + self.platform.write_to_find_pasteboard(item) } /// Writes credentials to the platform keychain. @@ -1900,8 +1917,11 @@ impl App { pub(crate) fn clear_pending_keystrokes(&mut self) { for window in self.windows() { window - .update(self, |_, window, _| { - window.clear_pending_keystrokes(); + .update(self, |_, window, cx| { + if window.pending_input_keystrokes().is_some() { + window.clear_pending_keystrokes(); + window.pending_input_changed(cx); + } }) .ok(); } diff --git a/crates/gpui/src/app/async_context.rs b/crates/gpui/src/app/async_context.rs index f5dcd30ae943954cbc042e1ce02edad39370a04a..805dfced162cd27f0cc785a8282ae3b802c2873a 100644 --- a/crates/gpui/src/app/async_context.rs +++ b/crates/gpui/src/app/async_context.rs @@ -487,7 +487,7 @@ impl VisualContext for AsyncWindowContext { V: Focusable, { self.app.update_window(self.window, |_, window, cx| { - view.read(cx).focus_handle(cx).focus(window); + view.read(cx).focus_handle(cx).focus(window, cx); }) } } diff --git a/crates/gpui/src/app/context.rs b/crates/gpui/src/app/context.rs index 27ccbecaf83cafe7bf7562c32a164268a74a396b..b780ca426c15c99030f24ee48bde978ad38526e7 100644 --- a/crates/gpui/src/app/context.rs +++ b/crates/gpui/src/app/context.rs @@ -285,7 +285,7 @@ impl<'a, T: 'static> Context<'a, T> { /// Focus the given view in the given window. View type is required to implement Focusable. pub fn focus_view(&mut self, view: &Entity, window: &mut Window) { - window.focus(&view.focus_handle(self)); + window.focus(&view.focus_handle(self), self); } /// Sets a given callback to be run on the next frame. @@ -732,7 +732,7 @@ impl<'a, T: 'static> Context<'a, T> { { let view = self.entity(); window.defer(self, move |window, cx| { - view.read(cx).focus_handle(cx).focus(window) + view.read(cx).focus_handle(cx).focus(window, cx) }) } } diff --git a/crates/gpui/src/app/test_context.rs b/crates/gpui/src/app/test_context.rs index 5be2e394e8edfd26a25c70c79c321a7fb8fdc8ba..9b982f9a1ca3c14b99dfc93e938aafe4e2f75cff 100644 --- a/crates/gpui/src/app/test_context.rs +++ b/crates/gpui/src/app/test_context.rs @@ -1045,7 +1045,7 @@ impl VisualContext for VisualTestContext { fn focus(&mut self, view: &Entity) -> Self::Result<()> { self.window .update(&mut self.cx, |_, window, cx| { - view.read(cx).focus_handle(cx).focus(window) + view.read(cx).focus_handle(cx).focus(window, cx) }) .unwrap() } diff --git a/crates/gpui/src/elements/div.rs b/crates/gpui/src/elements/div.rs index 374fd2c55a8e1cd5280d6ea9378a64c265a5c508..cf55edefaf70c080e171a8e21b350fd3c6d82f75 100644 --- a/crates/gpui/src/elements/div.rs +++ b/crates/gpui/src/elements/div.rs @@ -654,7 +654,7 @@ pub trait InteractiveElement: Sized { /// Set whether this element is a tab stop. /// /// When false, the element remains in tab-index order but cannot be reached via keyboard navigation. - /// Useful for container elements: focus the container, then call `window.focus_next()` to focus + /// Useful for container elements: focus the container, then call `window.focus_next(cx)` to focus /// the first tab stop inside it while having the container element itself be unreachable via the keyboard. /// Should only be used with `tab_index`. fn tab_stop(mut self, tab_stop: bool) -> Self { @@ -2096,12 +2096,12 @@ impl Interactivity { // This behavior can be suppressed by using `cx.prevent_default()`. if let Some(focus_handle) = self.tracked_focus_handle.clone() { let hitbox = hitbox.clone(); - window.on_mouse_event(move |_: &MouseDownEvent, phase, window, _| { + window.on_mouse_event(move |_: &MouseDownEvent, phase, window, cx| { if phase == DispatchPhase::Bubble && hitbox.is_hovered(window) && !window.default_prevented() { - window.focus(&focus_handle); + window.focus(&focus_handle, cx); // If there is a parent that is also focusable, prevent it // from transferring focus because we already did so. window.prevent_default(); diff --git a/crates/gpui/src/elements/text.rs b/crates/gpui/src/elements/text.rs index 1b1bfd778c7bc746c67551eb31cf70f60b1485ea..770c1f871432afbecc9ffd4e903dfeddcfcba6ee 100644 --- a/crates/gpui/src/elements/text.rs +++ b/crates/gpui/src/elements/text.rs @@ -2,8 +2,8 @@ use crate::{ ActiveTooltip, AnyView, App, Bounds, DispatchPhase, Element, ElementId, GlobalElementId, HighlightStyle, Hitbox, HitboxBehavior, InspectorElementId, IntoElement, LayoutId, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, Point, SharedString, Size, TextOverflow, - TextRun, TextStyle, TooltipId, WhiteSpace, Window, WrappedLine, WrappedLineLayout, - register_tooltip_mouse_handlers, set_tooltip_on_window, + TextRun, TextStyle, TooltipId, TruncateFrom, WhiteSpace, Window, WrappedLine, + WrappedLineLayout, register_tooltip_mouse_handlers, set_tooltip_on_window, }; use anyhow::Context as _; use itertools::Itertools; @@ -354,7 +354,7 @@ impl TextLayout { None }; - let (truncate_width, truncation_suffix) = + let (truncate_width, truncation_affix, truncate_from) = if let Some(text_overflow) = text_style.text_overflow.clone() { let width = known_dimensions.width.or(match available_space.width { crate::AvailableSpace::Definite(x) => match text_style.line_clamp { @@ -365,17 +365,24 @@ impl TextLayout { }); match text_overflow { - TextOverflow::Truncate(s) => (width, s), + TextOverflow::Truncate(s) => (width, s, TruncateFrom::End), + TextOverflow::TruncateStart(s) => (width, s, TruncateFrom::Start), } } else { - (None, "".into()) + (None, "".into(), TruncateFrom::End) }; + // Only use cached layout if: + // 1. We have a cached size + // 2. wrap_width matches (or both are None) + // 3. truncate_width is None (if truncate_width is Some, we need to re-layout + // because the previous layout may have been computed without truncation) if let Some(text_layout) = element_state.0.borrow().as_ref() - && text_layout.size.is_some() + && let Some(size) = text_layout.size && (wrap_width.is_none() || wrap_width == text_layout.wrap_width) + && truncate_width.is_none() { - return text_layout.size.unwrap(); + return size; } let mut line_wrapper = cx.text_system().line_wrapper(text_style.font(), font_size); @@ -383,8 +390,9 @@ impl TextLayout { line_wrapper.truncate_line( text.clone(), truncate_width, - &truncation_suffix, + &truncation_affix, &runs, + truncate_from, ) } else { (text.clone(), Cow::Borrowed(&*runs)) diff --git a/crates/gpui/src/elements/uniform_list.rs b/crates/gpui/src/elements/uniform_list.rs index 1ad71b97673e6f54015dbc67fa829725dd4fccb2..a7486f0c00ac4e11ef807af90f6fb75b74b5d142 100644 --- a/crates/gpui/src/elements/uniform_list.rs +++ b/crates/gpui/src/elements/uniform_list.rs @@ -788,7 +788,7 @@ mod test { let (view, cx) = cx.add_window_view(|window, cx| { let focus_handle = cx.focus_handle(); - window.focus(&focus_handle); + window.focus(&focus_handle, cx); TestView { scroll_handle: UniformListScrollHandle::new(), index: 0, diff --git a/crates/gpui/src/interactive.rs b/crates/gpui/src/interactive.rs index 6852b9596a3f74e1d533fc2a7e9a7b7eeab71cda..a500ac46f0bbf96fc2b9d326a3a61da42c40b7ec 100644 --- a/crates/gpui/src/interactive.rs +++ b/crates/gpui/src/interactive.rs @@ -705,8 +705,8 @@ mod test { }); window - .update(cx, |test_view, window, _cx| { - window.focus(&test_view.focus_handle) + .update(cx, |test_view, window, cx| { + window.focus(&test_view.focus_handle, cx) }) .unwrap(); diff --git a/crates/gpui/src/key_dispatch.rs b/crates/gpui/src/key_dispatch.rs index ae4553408fa8d0dc7ed640319ae0b0a178465b74..1b92b9fe3ffabdbeec4bc7450adc1439e8e223eb 100644 --- a/crates/gpui/src/key_dispatch.rs +++ b/crates/gpui/src/key_dispatch.rs @@ -462,6 +462,17 @@ impl DispatchTree { (bindings, partial, context_stack) } + /// Find the bindings that can follow the current input sequence. + pub fn possible_next_bindings_for_input( + &self, + input: &[Keystroke], + context_stack: &[KeyContext], + ) -> Vec { + self.keymap + .borrow() + .possible_next_bindings_for_input(input, context_stack) + } + /// dispatch_key processes the keystroke /// input should be set to the value of `pending` from the previous call to dispatch_key. /// This returns three instructions to the input handler: @@ -610,8 +621,8 @@ impl DispatchTree { #[cfg(test)] mod tests { use crate::{ - self as gpui, DispatchResult, Element, ElementId, GlobalElementId, InspectorElementId, - Keystroke, LayoutId, Style, + self as gpui, AppContext, DispatchResult, Element, ElementId, GlobalElementId, + InspectorElementId, Keystroke, LayoutId, Style, }; use core::panic; use smallvec::SmallVec; @@ -619,8 +630,8 @@ mod tests { use crate::{ Action, ActionRegistry, App, Bounds, Context, DispatchTree, FocusHandle, InputHandler, - IntoElement, KeyBinding, KeyContext, Keymap, Pixels, Point, Render, TestAppContext, - UTF16Selection, Window, + IntoElement, KeyBinding, KeyContext, Keymap, Pixels, Point, Render, Subscription, + TestAppContext, UTF16Selection, Window, }; #[derive(PartialEq, Eq)] @@ -723,6 +734,213 @@ mod tests { assert!(!result.pending_has_binding); } + #[crate::test] + fn test_pending_input_observers_notified_on_focus_change(cx: &mut TestAppContext) { + #[derive(Clone)] + struct CustomElement { + focus_handle: FocusHandle, + text: Rc>, + } + + impl CustomElement { + fn new(cx: &mut Context) -> Self { + Self { + focus_handle: cx.focus_handle(), + text: Rc::default(), + } + } + } + + impl Element for CustomElement { + type RequestLayoutState = (); + + type PrepaintState = (); + + fn id(&self) -> Option { + Some("custom".into()) + } + + fn source_location(&self) -> Option<&'static panic::Location<'static>> { + None + } + + fn request_layout( + &mut self, + _: Option<&GlobalElementId>, + _: Option<&InspectorElementId>, + window: &mut Window, + cx: &mut App, + ) -> (LayoutId, Self::RequestLayoutState) { + (window.request_layout(Style::default(), [], cx), ()) + } + + fn prepaint( + &mut self, + _: Option<&GlobalElementId>, + _: Option<&InspectorElementId>, + _: Bounds, + _: &mut Self::RequestLayoutState, + window: &mut Window, + cx: &mut App, + ) -> Self::PrepaintState { + window.set_focus_handle(&self.focus_handle, cx); + } + + fn paint( + &mut self, + _: Option<&GlobalElementId>, + _: Option<&InspectorElementId>, + _: Bounds, + _: &mut Self::RequestLayoutState, + _: &mut Self::PrepaintState, + window: &mut Window, + cx: &mut App, + ) { + let mut key_context = KeyContext::default(); + key_context.add("Terminal"); + window.set_key_context(key_context); + window.handle_input(&self.focus_handle, self.clone(), cx); + window.on_action(std::any::TypeId::of::(), |_, _, _, _| {}); + } + } + + impl IntoElement for CustomElement { + type Element = Self; + + fn into_element(self) -> Self::Element { + self + } + } + + impl InputHandler for CustomElement { + fn selected_text_range( + &mut self, + _: bool, + _: &mut Window, + _: &mut App, + ) -> Option { + None + } + + fn marked_text_range(&mut self, _: &mut Window, _: &mut App) -> Option> { + None + } + + fn text_for_range( + &mut self, + _: Range, + _: &mut Option>, + _: &mut Window, + _: &mut App, + ) -> Option { + None + } + + fn replace_text_in_range( + &mut self, + replacement_range: Option>, + text: &str, + _: &mut Window, + _: &mut App, + ) { + if replacement_range.is_some() { + unimplemented!() + } + self.text.borrow_mut().push_str(text) + } + + fn replace_and_mark_text_in_range( + &mut self, + replacement_range: Option>, + new_text: &str, + _: Option>, + _: &mut Window, + _: &mut App, + ) { + if replacement_range.is_some() { + unimplemented!() + } + self.text.borrow_mut().push_str(new_text) + } + + fn unmark_text(&mut self, _: &mut Window, _: &mut App) {} + + fn bounds_for_range( + &mut self, + _: Range, + _: &mut Window, + _: &mut App, + ) -> Option> { + None + } + + fn character_index_for_point( + &mut self, + _: Point, + _: &mut Window, + _: &mut App, + ) -> Option { + None + } + } + + impl Render for CustomElement { + fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { + self.clone() + } + } + + cx.update(|cx| { + cx.bind_keys([KeyBinding::new("ctrl-b", TestAction, Some("Terminal"))]); + cx.bind_keys([KeyBinding::new("ctrl-b h", TestAction, Some("Terminal"))]); + }); + + let (test, cx) = cx.add_window_view(|_, cx| CustomElement::new(cx)); + let focus_handle = test.update(cx, |test, _| test.focus_handle.clone()); + + let pending_input_changed_count = Rc::new(RefCell::new(0usize)); + let pending_input_changed_count_for_observer = pending_input_changed_count.clone(); + + struct PendingInputObserver { + _subscription: Subscription, + } + + let _observer = cx.update(|window, cx| { + cx.new(|cx| PendingInputObserver { + _subscription: cx.observe_pending_input(window, move |_, _, _| { + *pending_input_changed_count_for_observer.borrow_mut() += 1; + }), + }) + }); + + cx.update(|window, cx| { + window.focus(&focus_handle, cx); + window.activate_window(); + }); + + cx.simulate_keystrokes("ctrl-b"); + + let count_after_pending = Rc::new(RefCell::new(0usize)); + let count_after_pending_for_assertion = count_after_pending.clone(); + + cx.update(|window, cx| { + assert!(window.has_pending_keystrokes()); + *count_after_pending.borrow_mut() = *pending_input_changed_count.borrow(); + assert!(*count_after_pending.borrow() > 0); + + window.focus(&cx.focus_handle(), cx); + + assert!(!window.has_pending_keystrokes()); + }); + + // Focus-triggered pending-input notifications are deferred to the end of the current + // effect cycle, so the observer callback should run after the focus update completes. + cx.update(|_, _| { + let count_after_focus_change = *pending_input_changed_count.borrow(); + assert!(count_after_focus_change > *count_after_pending_for_assertion.borrow()); + }); + } + #[crate::test] fn test_input_handler_pending(cx: &mut TestAppContext) { #[derive(Clone)] @@ -876,8 +1094,9 @@ mod tests { cx.bind_keys([KeyBinding::new("ctrl-b h", TestAction, Some("Terminal"))]); }); let (test, cx) = cx.add_window_view(|_, cx| CustomElement::new(cx)); + let focus_handle = test.update(cx, |test, _| test.focus_handle.clone()); cx.update(|window, cx| { - window.focus(&test.read(cx).focus_handle); + window.focus(&focus_handle, cx); window.activate_window(); }); cx.simulate_keystrokes("ctrl-b ["); diff --git a/crates/gpui/src/keymap.rs b/crates/gpui/src/keymap.rs index 33d956917055942cce365e9069cbb007e202eaf2..d5398ff0447849ca5bfcdbbb5a838af0cbc22836 100644 --- a/crates/gpui/src/keymap.rs +++ b/crates/gpui/src/keymap.rs @@ -215,6 +215,41 @@ impl Keymap { Some(contexts.len()) } } + + /// Find the bindings that can follow the current input sequence. + pub fn possible_next_bindings_for_input( + &self, + input: &[Keystroke], + context_stack: &[KeyContext], + ) -> Vec { + let mut bindings = self + .bindings() + .enumerate() + .rev() + .filter_map(|(ix, binding)| { + let depth = self.binding_enabled(binding, context_stack)?; + let pending = binding.match_keystrokes(input); + match pending { + None => None, + Some(is_pending) => { + if !is_pending || is_no_action(&*binding.action) { + return None; + } + Some((depth, BindingIndex(ix), binding)) + } + } + }) + .collect::>(); + + bindings.sort_by(|(depth_a, ix_a, _), (depth_b, ix_b, _)| { + depth_b.cmp(depth_a).then(ix_b.cmp(ix_a)) + }); + + bindings + .into_iter() + .map(|(_, _, binding)| binding.clone()) + .collect::>() + } } #[cfg(test)] diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs index f120e075fea7f9336e2f6e10c51611d8ba03564d..112775890ef6e478f0b2d347bc9c9ae56dac3c73 100644 --- a/crates/gpui/src/platform.rs +++ b/crates/gpui/src/platform.rs @@ -262,12 +262,18 @@ pub(crate) trait Platform: 'static { fn set_cursor_style(&self, style: CursorStyle); fn should_auto_hide_scrollbars(&self) -> bool; - #[cfg(any(target_os = "linux", target_os = "freebsd"))] - fn write_to_primary(&self, item: ClipboardItem); + fn read_from_clipboard(&self) -> Option; fn write_to_clipboard(&self, item: ClipboardItem); + #[cfg(any(target_os = "linux", target_os = "freebsd"))] fn read_from_primary(&self) -> Option; - fn read_from_clipboard(&self) -> Option; + #[cfg(any(target_os = "linux", target_os = "freebsd"))] + fn write_to_primary(&self, item: ClipboardItem); + + #[cfg(target_os = "macos")] + fn read_from_find_pasteboard(&self) -> Option; + #[cfg(target_os = "macos")] + fn write_to_find_pasteboard(&self, item: ClipboardItem); fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Task>; fn read_credentials(&self, url: &str) -> Task)>>>; @@ -1348,6 +1354,10 @@ pub enum WindowKind { /// docks, notifications or wallpapers. #[cfg(all(target_os = "linux", feature = "wayland"))] LayerShell(layer_shell::LayerShellOptions), + + /// A window that appears on top of its parent window and blocks interaction with it + /// until the modal window is closed + Dialog, } /// The appearance of the window, as defined by the operating system. diff --git a/crates/gpui/src/platform/linux/wayland/client.rs b/crates/gpui/src/platform/linux/wayland/client.rs index 0e7bf8fbf8880baf5876027e6e764d7411932577..b6bfbec0679f9413fceef2bb37e7bd304371707e 100644 --- a/crates/gpui/src/platform/linux/wayland/client.rs +++ b/crates/gpui/src/platform/linux/wayland/client.rs @@ -36,12 +36,6 @@ use wayland_client::{ wl_shm_pool, wl_surface, }, }; -use wayland_protocols::wp::cursor_shape::v1::client::{ - wp_cursor_shape_device_v1, wp_cursor_shape_manager_v1, -}; -use wayland_protocols::wp::fractional_scale::v1::client::{ - wp_fractional_scale_manager_v1, wp_fractional_scale_v1, -}; use wayland_protocols::wp::primary_selection::zv1::client::zwp_primary_selection_offer_v1::{ self, ZwpPrimarySelectionOfferV1, }; @@ -61,6 +55,14 @@ use wayland_protocols::xdg::decoration::zv1::client::{ zxdg_decoration_manager_v1, zxdg_toplevel_decoration_v1, }; use wayland_protocols::xdg::shell::client::{xdg_surface, xdg_toplevel, xdg_wm_base}; +use wayland_protocols::{ + wp::cursor_shape::v1::client::{wp_cursor_shape_device_v1, wp_cursor_shape_manager_v1}, + xdg::dialog::v1::client::xdg_wm_dialog_v1::{self, XdgWmDialogV1}, +}; +use wayland_protocols::{ + wp::fractional_scale::v1::client::{wp_fractional_scale_manager_v1, wp_fractional_scale_v1}, + xdg::dialog::v1::client::xdg_dialog_v1::XdgDialogV1, +}; use wayland_protocols_plasma::blur::client::{org_kde_kwin_blur, org_kde_kwin_blur_manager}; use wayland_protocols_wlr::layer_shell::v1::client::{zwlr_layer_shell_v1, zwlr_layer_surface_v1}; use xkbcommon::xkb::ffi::XKB_KEYMAP_FORMAT_TEXT_V1; @@ -122,6 +124,7 @@ pub struct Globals { pub layer_shell: Option, pub blur_manager: Option, pub text_input_manager: Option, + pub dialog: Option, pub executor: ForegroundExecutor, } @@ -132,6 +135,7 @@ impl Globals { qh: QueueHandle, seat: wl_seat::WlSeat, ) -> Self { + let dialog_v = XdgWmDialogV1::interface().version; Globals { activation: globals.bind(&qh, 1..=1, ()).ok(), compositor: globals @@ -160,6 +164,7 @@ impl Globals { layer_shell: globals.bind(&qh, 1..=5, ()).ok(), blur_manager: globals.bind(&qh, 1..=1, ()).ok(), text_input_manager: globals.bind(&qh, 1..=1, ()).ok(), + dialog: globals.bind(&qh, dialog_v..=dialog_v, ()).ok(), executor, qh, } @@ -729,10 +734,7 @@ impl LinuxClient for WaylandClient { ) -> anyhow::Result> { let mut state = self.0.borrow_mut(); - let parent = state - .keyboard_focused_window - .as_ref() - .and_then(|w| w.toplevel()); + let parent = state.keyboard_focused_window.clone(); let (window, surface_id) = WaylandWindow::new( handle, @@ -751,7 +753,12 @@ impl LinuxClient for WaylandClient { fn set_cursor_style(&self, style: CursorStyle) { let mut state = self.0.borrow_mut(); - let need_update = state.cursor_style != Some(style); + let need_update = state.cursor_style != Some(style) + && (state.mouse_focused_window.is_none() + || state + .mouse_focused_window + .as_ref() + .is_some_and(|w| !w.is_blocked())); if need_update { let serial = state.serial_tracker.get(SerialKind::MouseEnter); @@ -1011,7 +1018,7 @@ impl Dispatch for WaylandClientStatePtr { } } -fn get_window( +pub(crate) fn get_window( mut state: &mut RefMut, surface_id: &ObjectId, ) -> Option { @@ -1654,6 +1661,30 @@ impl Dispatch for WaylandClientStatePtr { state.mouse_location = Some(point(px(surface_x as f32), px(surface_y as f32))); if let Some(window) = state.mouse_focused_window.clone() { + if window.is_blocked() { + let default_style = CursorStyle::Arrow; + if state.cursor_style != Some(default_style) { + let serial = state.serial_tracker.get(SerialKind::MouseEnter); + state.cursor_style = Some(default_style); + + if let Some(cursor_shape_device) = &state.cursor_shape_device { + cursor_shape_device.set_shape(serial, default_style.to_shape()); + } else { + // cursor-shape-v1 isn't supported, set the cursor using a surface. + let wl_pointer = state + .wl_pointer + .clone() + .expect("window is focused by pointer"); + let scale = window.primary_output_scale(); + state.cursor.set_icon( + &wl_pointer, + serial, + default_style.to_icon_names(), + scale, + ); + } + } + } if state .keyboard_focused_window .as_ref() @@ -2225,3 +2256,27 @@ impl Dispatch } } } + +impl Dispatch for WaylandClientStatePtr { + fn event( + _: &mut Self, + _: &XdgWmDialogV1, + _: ::Event, + _: &(), + _: &Connection, + _: &QueueHandle, + ) { + } +} + +impl Dispatch for WaylandClientStatePtr { + fn event( + _state: &mut Self, + _proxy: &XdgDialogV1, + _event: ::Event, + _data: &(), + _conn: &Connection, + _qhandle: &QueueHandle, + ) { + } +} diff --git a/crates/gpui/src/platform/linux/wayland/window.rs b/crates/gpui/src/platform/linux/wayland/window.rs index 3334ae28a31927b2150e79fc513855fa699c55ba..6b4dad3b3917d025a80594b5ece63c26bbadde69 100644 --- a/crates/gpui/src/platform/linux/wayland/window.rs +++ b/crates/gpui/src/platform/linux/wayland/window.rs @@ -7,7 +7,7 @@ use std::{ }; use blade_graphics as gpu; -use collections::HashMap; +use collections::{FxHashSet, HashMap}; use futures::channel::oneshot::Receiver; use raw_window_handle as rwh; @@ -20,7 +20,7 @@ use wayland_protocols::xdg::shell::client::xdg_surface; use wayland_protocols::xdg::shell::client::xdg_toplevel::{self}; use wayland_protocols::{ wp::fractional_scale::v1::client::wp_fractional_scale_v1, - xdg::shell::client::xdg_toplevel::XdgToplevel, + xdg::dialog::v1::client::xdg_dialog_v1::XdgDialogV1, }; use wayland_protocols_plasma::blur::client::org_kde_kwin_blur; use wayland_protocols_wlr::layer_shell::v1::client::zwlr_layer_surface_v1; @@ -29,7 +29,7 @@ use crate::{ AnyWindowHandle, Bounds, Decorations, Globals, GpuSpecs, Modifiers, Output, Pixels, PlatformDisplay, PlatformInput, Point, PromptButton, PromptLevel, RequestFrameOptions, ResizeEdge, Size, Tiling, WaylandClientStatePtr, WindowAppearance, WindowBackgroundAppearance, - WindowBounds, WindowControlArea, WindowControls, WindowDecorations, WindowParams, + WindowBounds, WindowControlArea, WindowControls, WindowDecorations, WindowParams, get_window, layer_shell::LayerShellNotSupportedError, px, size, }; use crate::{ @@ -87,6 +87,8 @@ struct InProgressConfigure { pub struct WaylandWindowState { surface_state: WaylandSurfaceState, acknowledged_first_configure: bool, + parent: Option, + children: FxHashSet, pub surface: wl_surface::WlSurface, app_id: Option, appearance: WindowAppearance, @@ -126,7 +128,7 @@ impl WaylandSurfaceState { surface: &wl_surface::WlSurface, globals: &Globals, params: &WindowParams, - parent: Option, + parent: Option, ) -> anyhow::Result { // For layer_shell windows, create a layer surface instead of an xdg surface if let WindowKind::LayerShell(options) = ¶ms.kind { @@ -178,10 +180,28 @@ impl WaylandSurfaceState { .get_xdg_surface(&surface, &globals.qh, surface.id()); let toplevel = xdg_surface.get_toplevel(&globals.qh, surface.id()); - if params.kind == WindowKind::Floating { - toplevel.set_parent(parent.as_ref()); + let xdg_parent = parent.as_ref().and_then(|w| w.toplevel()); + + if params.kind == WindowKind::Floating || params.kind == WindowKind::Dialog { + toplevel.set_parent(xdg_parent.as_ref()); } + let dialog = if params.kind == WindowKind::Dialog { + let dialog = globals.dialog.as_ref().map(|dialog| { + let xdg_dialog = dialog.get_xdg_dialog(&toplevel, &globals.qh, ()); + xdg_dialog.set_modal(); + xdg_dialog + }); + + if let Some(parent) = parent.as_ref() { + parent.add_child(surface.id()); + } + + dialog + } else { + None + }; + if let Some(size) = params.window_min_size { toplevel.set_min_size(size.width.0 as i32, size.height.0 as i32); } @@ -198,6 +218,7 @@ impl WaylandSurfaceState { xdg_surface, toplevel, decoration, + dialog, })) } } @@ -206,6 +227,7 @@ pub struct WaylandXdgSurfaceState { xdg_surface: xdg_surface::XdgSurface, toplevel: xdg_toplevel::XdgToplevel, decoration: Option, + dialog: Option, } pub struct WaylandLayerSurfaceState { @@ -258,7 +280,13 @@ impl WaylandSurfaceState { xdg_surface, toplevel, decoration: _decoration, + dialog, }) => { + // drop the dialog before toplevel so compositor can explicitly unapply it's effects + if let Some(dialog) = dialog { + dialog.destroy(); + } + // The role object (toplevel) must always be destroyed before the xdg_surface. // See https://wayland.app/protocols/xdg-shell#xdg_surface:request:destroy toplevel.destroy(); @@ -288,6 +316,7 @@ impl WaylandWindowState { globals: Globals, gpu_context: &BladeContext, options: WindowParams, + parent: Option, ) -> anyhow::Result { let renderer = { let raw_window = RawWindow { @@ -319,6 +348,8 @@ impl WaylandWindowState { Ok(Self { surface_state, acknowledged_first_configure: false, + parent, + children: FxHashSet::default(), surface, app_id: None, blur: None, @@ -391,6 +422,10 @@ impl Drop for WaylandWindow { fn drop(&mut self) { let mut state = self.0.state.borrow_mut(); let surface_id = state.surface.id(); + if let Some(parent) = state.parent.as_ref() { + parent.state.borrow_mut().children.remove(&surface_id); + } + let client = state.client.clone(); state.renderer.destroy(); @@ -448,10 +483,10 @@ impl WaylandWindow { client: WaylandClientStatePtr, params: WindowParams, appearance: WindowAppearance, - parent: Option, + parent: Option, ) -> anyhow::Result<(Self, ObjectId)> { let surface = globals.compositor.create_surface(&globals.qh, ()); - let surface_state = WaylandSurfaceState::new(&surface, &globals, ¶ms, parent)?; + let surface_state = WaylandSurfaceState::new(&surface, &globals, ¶ms, parent.clone())?; if let Some(fractional_scale_manager) = globals.fractional_scale_manager.as_ref() { fractional_scale_manager.get_fractional_scale(&surface, &globals.qh, surface.id()); @@ -473,6 +508,7 @@ impl WaylandWindow { globals, gpu_context, params, + parent, )?)), callbacks: Rc::new(RefCell::new(Callbacks::default())), }); @@ -501,6 +537,16 @@ impl WaylandWindowStatePtr { Rc::ptr_eq(&self.state, &other.state) } + pub fn add_child(&self, child: ObjectId) { + let mut state = self.state.borrow_mut(); + state.children.insert(child); + } + + pub fn is_blocked(&self) -> bool { + let state = self.state.borrow(); + !state.children.is_empty() + } + pub fn frame(&self) { let mut state = self.state.borrow_mut(); state.surface.frame(&state.globals.qh, state.surface.id()); @@ -818,6 +864,9 @@ impl WaylandWindowStatePtr { } pub fn handle_ime(&self, ime: ImeInput) { + if self.is_blocked() { + return; + } let mut state = self.state.borrow_mut(); if let Some(mut input_handler) = state.input_handler.take() { drop(state); @@ -894,6 +943,21 @@ impl WaylandWindowStatePtr { } pub fn close(&self) { + let state = self.state.borrow(); + let client = state.client.get_client(); + #[allow(clippy::mutable_key_type)] + let children = state.children.clone(); + drop(state); + + for child in children { + let mut client_state = client.borrow_mut(); + let window = get_window(&mut client_state, &child); + drop(client_state); + + if let Some(child) = window { + child.close(); + } + } let mut callbacks = self.callbacks.borrow_mut(); if let Some(fun) = callbacks.close.take() { fun() @@ -901,6 +965,9 @@ impl WaylandWindowStatePtr { } pub fn handle_input(&self, input: PlatformInput) { + if self.is_blocked() { + return; + } if let Some(ref mut fun) = self.callbacks.borrow_mut().input && !fun(input.clone()).propagate { @@ -1025,13 +1092,26 @@ impl PlatformWindow for WaylandWindow { fn resize(&mut self, size: Size) { let state = self.borrow(); let state_ptr = self.0.clone(); - let dp_size = size.to_device_pixels(self.scale_factor()); + + // Keep window geometry consistent with configure handling. On Wayland, window geometry is + // surface-local: resizing should not attempt to translate the window; the compositor + // controls placement. We also account for client-side decoration insets and tiling. + let window_geometry = inset_by_tiling( + Bounds { + origin: Point::default(), + size, + }, + state.inset(), + state.tiling, + ) + .map(|v| v.0 as i32) + .map_size(|v| if v <= 0 { 1 } else { v }); state.surface_state.set_geometry( - state.bounds.origin.x.0 as i32, - state.bounds.origin.y.0 as i32, - dp_size.width.0, - dp_size.height.0, + window_geometry.origin.x, + window_geometry.origin.y, + window_geometry.size.width, + window_geometry.size.height, ); state diff --git a/crates/gpui/src/platform/linux/x11/client.rs b/crates/gpui/src/platform/linux/x11/client.rs index 5e9089b09809a7ec1b8b257427b0a670adc0f123..7feec41d433158325592d566f83a6063f7a7196e 100644 --- a/crates/gpui/src/platform/linux/x11/client.rs +++ b/crates/gpui/src/platform/linux/x11/client.rs @@ -222,7 +222,7 @@ pub struct X11ClientState { pub struct X11ClientStatePtr(pub Weak>); impl X11ClientStatePtr { - fn get_client(&self) -> Option { + pub fn get_client(&self) -> Option { self.0.upgrade().map(X11Client) } @@ -752,7 +752,7 @@ impl X11Client { } } - fn get_window(&self, win: xproto::Window) -> Option { + pub(crate) fn get_window(&self, win: xproto::Window) -> Option { let state = self.0.borrow(); state .windows @@ -789,12 +789,12 @@ impl X11Client { let [atom, arg1, arg2, arg3, arg4] = event.data.as_data32(); let mut state = self.0.borrow_mut(); - if atom == state.atoms.WM_DELETE_WINDOW { + if atom == state.atoms.WM_DELETE_WINDOW && window.should_close() { // window "x" button clicked by user - if window.should_close() { - // Rest of the close logic is handled in drop_window() - window.close(); - } + // Rest of the close logic is handled in drop_window() + drop(state); + window.close(); + state = self.0.borrow_mut(); } else if atom == state.atoms._NET_WM_SYNC_REQUEST { window.state.borrow_mut().last_sync_counter = Some(x11rb::protocol::sync::Int64 { @@ -1216,6 +1216,33 @@ impl X11Client { Event::XinputMotion(event) => { let window = self.get_window(event.event)?; let mut state = self.0.borrow_mut(); + if window.is_blocked() { + // We want to set the cursor to the default arrow + // when the window is blocked + let style = CursorStyle::Arrow; + + let current_style = state + .cursor_styles + .get(&window.x_window) + .unwrap_or(&CursorStyle::Arrow); + if *current_style != style + && let Some(cursor) = state.get_cursor_icon(style) + { + state.cursor_styles.insert(window.x_window, style); + check_reply( + || "Failed to set cursor style", + state.xcb_connection.change_window_attributes( + window.x_window, + &ChangeWindowAttributesAux { + cursor: Some(cursor), + ..Default::default() + }, + ), + ) + .log_err(); + state.xcb_connection.flush().log_err(); + }; + } let pressed_button = pressed_button_from_mask(event.button_mask[0]); let position = point( px(event.event_x as f32 / u16::MAX as f32 / state.scale_factor), @@ -1489,7 +1516,7 @@ impl LinuxClient for X11Client { let parent_window = state .keyboard_focused_window .and_then(|focused_window| state.windows.get(&focused_window)) - .map(|window| window.window.x_window); + .map(|w| w.window.clone()); let x_window = state .xcb_connection .generate_id() @@ -1544,7 +1571,15 @@ impl LinuxClient for X11Client { .cursor_styles .get(&focused_window) .unwrap_or(&CursorStyle::Arrow); - if *current_style == style { + + let window = state + .mouse_focused_window + .and_then(|w| state.windows.get(&w)); + + let should_change = *current_style != style + && (window.is_none() || window.is_some_and(|w| !w.is_blocked())); + + if !should_change { return; } diff --git a/crates/gpui/src/platform/linux/x11/window.rs b/crates/gpui/src/platform/linux/x11/window.rs index fe197a670177689ce776b6b55d439483c43921e0..1986ff6cce6b1930bdc3527eced5f2d5b8f45117 100644 --- a/crates/gpui/src/platform/linux/x11/window.rs +++ b/crates/gpui/src/platform/linux/x11/window.rs @@ -11,6 +11,7 @@ use crate::{ }; use blade_graphics as gpu; +use collections::FxHashSet; use raw_window_handle as rwh; use util::{ResultExt, maybe}; use x11rb::{ @@ -74,6 +75,7 @@ x11rb::atom_manager! { _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_NOTIFICATION, _NET_WM_WINDOW_TYPE_DIALOG, + _NET_WM_STATE_MODAL, _NET_WM_SYNC, _NET_SUPPORTED, _MOTIF_WM_HINTS, @@ -249,6 +251,8 @@ pub struct Callbacks { pub struct X11WindowState { pub destroyed: bool, + parent: Option, + children: FxHashSet, client: X11ClientStatePtr, executor: ForegroundExecutor, atoms: XcbAtoms, @@ -394,7 +398,7 @@ impl X11WindowState { atoms: &XcbAtoms, scale_factor: f32, appearance: WindowAppearance, - parent_window: Option, + parent_window: Option, ) -> anyhow::Result { let x_screen_index = params .display_id @@ -546,8 +550,8 @@ impl X11WindowState { )?; } - if params.kind == WindowKind::Floating { - if let Some(parent_window) = parent_window { + if params.kind == WindowKind::Floating || params.kind == WindowKind::Dialog { + if let Some(parent_window) = parent_window.as_ref().map(|w| w.x_window) { // WM_TRANSIENT_FOR hint indicating the main application window. For floating windows, we set // a parent window (WM_TRANSIENT_FOR) such that the window manager knows where to // place the floating window in relation to the main window. @@ -563,11 +567,23 @@ impl X11WindowState { ), )?; } + } + + let parent = if params.kind == WindowKind::Dialog + && let Some(parent) = parent_window + { + parent.add_child(x_window); + + Some(parent) + } else { + None + }; + if params.kind == WindowKind::Dialog { // _NET_WM_WINDOW_TYPE_DIALOG indicates that this is a dialog (floating) window // https://specifications.freedesktop.org/wm-spec/1.4/ar01s05.html check_reply( - || "X11 ChangeProperty32 setting window type for floating window failed.", + || "X11 ChangeProperty32 setting window type for dialog window failed.", xcb.change_property32( xproto::PropMode::REPLACE, x_window, @@ -576,6 +592,20 @@ impl X11WindowState { &[atoms._NET_WM_WINDOW_TYPE_DIALOG], ), )?; + + // We set the modal state for dialog windows, so that the window manager + // can handle it appropriately (e.g., prevent interaction with the parent window + // while the dialog is open). + check_reply( + || "X11 ChangeProperty32 setting modal state for dialog window failed.", + xcb.change_property32( + xproto::PropMode::REPLACE, + x_window, + atoms._NET_WM_STATE, + xproto::AtomEnum::ATOM, + &[atoms._NET_WM_STATE_MODAL], + ), + )?; } check_reply( @@ -667,6 +697,8 @@ impl X11WindowState { let display = Rc::new(X11Display::new(xcb, scale_factor, x_screen_index)?); Ok(Self { + parent, + children: FxHashSet::default(), client, executor, display, @@ -720,6 +752,11 @@ pub(crate) struct X11Window(pub X11WindowStatePtr); impl Drop for X11Window { fn drop(&mut self) { let mut state = self.0.state.borrow_mut(); + + if let Some(parent) = state.parent.as_ref() { + parent.state.borrow_mut().children.remove(&self.0.x_window); + } + state.renderer.destroy(); let destroy_x_window = maybe!({ @@ -734,8 +771,6 @@ impl Drop for X11Window { .log_err(); if destroy_x_window.is_some() { - // Mark window as destroyed so that we can filter out when X11 events - // for it still come in. state.destroyed = true; let this_ptr = self.0.clone(); @@ -773,7 +808,7 @@ impl X11Window { atoms: &XcbAtoms, scale_factor: f32, appearance: WindowAppearance, - parent_window: Option, + parent_window: Option, ) -> anyhow::Result { let ptr = X11WindowStatePtr { state: Rc::new(RefCell::new(X11WindowState::new( @@ -979,7 +1014,31 @@ impl X11WindowStatePtr { Ok(()) } + pub fn add_child(&self, child: xproto::Window) { + let mut state = self.state.borrow_mut(); + state.children.insert(child); + } + + pub fn is_blocked(&self) -> bool { + let state = self.state.borrow(); + !state.children.is_empty() + } + pub fn close(&self) { + let state = self.state.borrow(); + let client = state.client.clone(); + #[allow(clippy::mutable_key_type)] + let children = state.children.clone(); + drop(state); + + if let Some(client) = client.get_client() { + for child in children { + if let Some(child_window) = client.get_window(child) { + child_window.close(); + } + } + } + let mut callbacks = self.callbacks.borrow_mut(); if let Some(fun) = callbacks.close.take() { fun() @@ -994,6 +1053,9 @@ impl X11WindowStatePtr { } pub fn handle_input(&self, input: PlatformInput) { + if self.is_blocked() { + return; + } if let Some(ref mut fun) = self.callbacks.borrow_mut().input && !fun(input.clone()).propagate { @@ -1016,6 +1078,9 @@ impl X11WindowStatePtr { } pub fn handle_ime_commit(&self, text: String) { + if self.is_blocked() { + return; + } let mut state = self.state.borrow_mut(); if let Some(mut input_handler) = state.input_handler.take() { drop(state); @@ -1026,6 +1091,9 @@ impl X11WindowStatePtr { } pub fn handle_ime_preedit(&self, text: String) { + if self.is_blocked() { + return; + } let mut state = self.state.borrow_mut(); if let Some(mut input_handler) = state.input_handler.take() { drop(state); @@ -1036,6 +1104,9 @@ impl X11WindowStatePtr { } pub fn handle_ime_unmark(&self) { + if self.is_blocked() { + return; + } let mut state = self.state.borrow_mut(); if let Some(mut input_handler) = state.input_handler.take() { drop(state); @@ -1046,6 +1117,9 @@ impl X11WindowStatePtr { } pub fn handle_ime_delete(&self) { + if self.is_blocked() { + return; + } let mut state = self.state.borrow_mut(); if let Some(mut input_handler) = state.input_handler.take() { drop(state); diff --git a/crates/gpui/src/platform/mac.rs b/crates/gpui/src/platform/mac.rs index aa056846e6bc56e53d95c41a44444dbb89a16237..a229ec7dce928597ec73b1f4be50edd1ea3e5114 100644 --- a/crates/gpui/src/platform/mac.rs +++ b/crates/gpui/src/platform/mac.rs @@ -5,6 +5,7 @@ mod display; mod display_link; mod events; mod keyboard; +mod pasteboard; #[cfg(feature = "screen-capture")] mod screen_capture; @@ -21,8 +22,6 @@ use metal_renderer as renderer; #[cfg(feature = "macos-blade")] use crate::platform::blade as renderer; -mod attributed_string; - #[cfg(feature = "font-kit")] mod open_type; diff --git a/crates/gpui/src/platform/mac/attributed_string.rs b/crates/gpui/src/platform/mac/attributed_string.rs deleted file mode 100644 index 42fe1e5bf7a396a4eaa8ade26977a207d43b49b5..0000000000000000000000000000000000000000 --- a/crates/gpui/src/platform/mac/attributed_string.rs +++ /dev/null @@ -1,129 +0,0 @@ -use cocoa::base::id; -use cocoa::foundation::NSRange; -use objc::{class, msg_send, sel, sel_impl}; - -/// The `cocoa` crate does not define NSAttributedString (and related Cocoa classes), -/// which are needed for copying rich text (that is, text intermingled with images) -/// to the clipboard. This adds access to those APIs. -#[allow(non_snake_case)] -pub trait NSAttributedString: Sized { - unsafe fn alloc(_: Self) -> id { - msg_send![class!(NSAttributedString), alloc] - } - - unsafe fn init_attributed_string(self, string: id) -> id; - unsafe fn appendAttributedString_(self, attr_string: id); - unsafe fn RTFDFromRange_documentAttributes_(self, range: NSRange, attrs: id) -> id; - unsafe fn RTFFromRange_documentAttributes_(self, range: NSRange, attrs: id) -> id; - unsafe fn string(self) -> id; -} - -impl NSAttributedString for id { - unsafe fn init_attributed_string(self, string: id) -> id { - msg_send![self, initWithString: string] - } - - unsafe fn appendAttributedString_(self, attr_string: id) { - let _: () = msg_send![self, appendAttributedString: attr_string]; - } - - unsafe fn RTFDFromRange_documentAttributes_(self, range: NSRange, attrs: id) -> id { - msg_send![self, RTFDFromRange: range documentAttributes: attrs] - } - - unsafe fn RTFFromRange_documentAttributes_(self, range: NSRange, attrs: id) -> id { - msg_send![self, RTFFromRange: range documentAttributes: attrs] - } - - unsafe fn string(self) -> id { - msg_send![self, string] - } -} - -pub trait NSMutableAttributedString: NSAttributedString { - unsafe fn alloc(_: Self) -> id { - msg_send![class!(NSMutableAttributedString), alloc] - } -} - -impl NSMutableAttributedString for id {} - -#[cfg(test)] -mod tests { - use crate::platform::mac::ns_string; - - use super::*; - use cocoa::appkit::NSImage; - use cocoa::base::nil; - use cocoa::foundation::NSAutoreleasePool; - #[test] - #[ignore] // This was SIGSEGV-ing on CI but not locally; need to investigate https://github.com/zed-industries/zed/actions/runs/10362363230/job/28684225486?pr=15782#step:4:1348 - fn test_nsattributed_string() { - // TODO move these to parent module once it's actually ready to be used - #[allow(non_snake_case)] - pub trait NSTextAttachment: Sized { - unsafe fn alloc(_: Self) -> id { - msg_send![class!(NSTextAttachment), alloc] - } - } - - impl NSTextAttachment for id {} - - unsafe { - let image: id = { - let img: id = msg_send![class!(NSImage), alloc]; - let img: id = msg_send![img, initWithContentsOfFile: ns_string("test.jpeg")]; - let img: id = msg_send![img, autorelease]; - img - }; - let _size = image.size(); - - let string = ns_string("Test String"); - let attr_string = NSMutableAttributedString::alloc(nil) - .init_attributed_string(string) - .autorelease(); - let hello_string = ns_string("Hello World"); - let hello_attr_string = NSAttributedString::alloc(nil) - .init_attributed_string(hello_string) - .autorelease(); - attr_string.appendAttributedString_(hello_attr_string); - - let attachment: id = msg_send![NSTextAttachment::alloc(nil), autorelease]; - let _: () = msg_send![attachment, setImage: image]; - let image_attr_string = - msg_send![class!(NSAttributedString), attributedStringWithAttachment: attachment]; - attr_string.appendAttributedString_(image_attr_string); - - let another_string = ns_string("Another String"); - let another_attr_string = NSAttributedString::alloc(nil) - .init_attributed_string(another_string) - .autorelease(); - attr_string.appendAttributedString_(another_attr_string); - - let _len: cocoa::foundation::NSUInteger = msg_send![attr_string, length]; - - /////////////////////////////////////////////////// - // pasteboard.clearContents(); - - let rtfd_data = attr_string.RTFDFromRange_documentAttributes_( - NSRange::new(0, msg_send![attr_string, length]), - nil, - ); - assert_ne!(rtfd_data, nil); - // if rtfd_data != nil { - // pasteboard.setData_forType(rtfd_data, NSPasteboardTypeRTFD); - // } - - // let rtf_data = attributed_string.RTFFromRange_documentAttributes_( - // NSRange::new(0, attributed_string.length()), - // nil, - // ); - // if rtf_data != nil { - // pasteboard.setData_forType(rtf_data, NSPasteboardTypeRTF); - // } - - // let plain_text = attributed_string.string(); - // pasteboard.setString_forType(plain_text, NSPasteboardTypeString); - } - } -} diff --git a/crates/gpui/src/platform/mac/metal_renderer.rs b/crates/gpui/src/platform/mac/metal_renderer.rs index 550041a0ccb4cd39bc7a86317d9540e806af2a28..66f54e5ba0c66a508f9db73d5ad8f84cb52d0d69 100644 --- a/crates/gpui/src/platform/mac/metal_renderer.rs +++ b/crates/gpui/src/platform/mac/metal_renderer.rs @@ -46,9 +46,9 @@ pub unsafe fn new_renderer( _native_window: *mut c_void, _native_view: *mut c_void, _bounds: crate::Size, - _transparent: bool, + transparent: bool, ) -> Renderer { - MetalRenderer::new(context) + MetalRenderer::new(context, transparent) } pub(crate) struct InstanceBufferPool { @@ -128,7 +128,7 @@ pub struct PathRasterizationVertex { } impl MetalRenderer { - pub fn new(instance_buffer_pool: Arc>) -> Self { + pub fn new(instance_buffer_pool: Arc>, transparent: bool) -> Self { // Prefer low‐power integrated GPUs on Intel Mac. On Apple // Silicon, there is only ever one GPU, so this is equivalent to // `metal::Device::system_default()`. @@ -152,8 +152,13 @@ impl MetalRenderer { let layer = metal::MetalLayer::new(); layer.set_device(&device); layer.set_pixel_format(MTLPixelFormat::BGRA8Unorm); - layer.set_opaque(false); + // Support direct-to-display rendering if the window is not transparent + // https://developer.apple.com/documentation/metal/managing-your-game-window-for-metal-in-macos + layer.set_opaque(!transparent); layer.set_maximum_drawable_count(3); + // We already present at display sync with the display link + // This allows to use direct-to-display even in window mode + layer.set_display_sync_enabled(false); unsafe { let _: () = msg_send![&*layer, setAllowsNextDrawableTimeout: NO]; let _: () = msg_send![&*layer, setNeedsDisplayOnBoundsChange: YES]; @@ -352,8 +357,8 @@ impl MetalRenderer { } } - pub fn update_transparency(&self, _transparent: bool) { - // todo(mac)? + pub fn update_transparency(&self, transparent: bool) { + self.layer.set_opaque(!transparent); } pub fn destroy(&self) { diff --git a/crates/gpui/src/platform/mac/open_type.rs b/crates/gpui/src/platform/mac/open_type.rs index 37a29559fdfbc284ffd1021cc6c2c6ed717ca228..ff501df15f671318548a3959bd6b966f97e051b1 100644 --- a/crates/gpui/src/platform/mac/open_type.rs +++ b/crates/gpui/src/platform/mac/open_type.rs @@ -52,6 +52,11 @@ pub fn apply_features_and_fallbacks( &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks, ); + + for value in &values { + CFRelease(*value as _); + } + let new_descriptor = CTFontDescriptorCreateWithAttributes(attrs); CFRelease(attrs as _); let new_descriptor = CTFontDescriptor::wrap_under_create_rule(new_descriptor); diff --git a/crates/gpui/src/platform/mac/pasteboard.rs b/crates/gpui/src/platform/mac/pasteboard.rs new file mode 100644 index 0000000000000000000000000000000000000000..38710951f15b25515d906afc738c5b971b1bb135 --- /dev/null +++ b/crates/gpui/src/platform/mac/pasteboard.rs @@ -0,0 +1,344 @@ +use core::slice; +use std::ffi::c_void; + +use cocoa::{ + appkit::{NSPasteboard, NSPasteboardTypePNG, NSPasteboardTypeString, NSPasteboardTypeTIFF}, + base::{id, nil}, + foundation::NSData, +}; +use objc::{msg_send, runtime::Object, sel, sel_impl}; +use strum::IntoEnumIterator as _; + +use crate::{ + ClipboardEntry, ClipboardItem, ClipboardString, Image, ImageFormat, asset_cache::hash, + platform::mac::ns_string, +}; + +pub struct Pasteboard { + inner: id, + text_hash_type: id, + metadata_type: id, +} + +impl Pasteboard { + pub fn general() -> Self { + unsafe { Self::new(NSPasteboard::generalPasteboard(nil)) } + } + + pub fn find() -> Self { + unsafe { Self::new(NSPasteboard::pasteboardWithName(nil, NSPasteboardNameFind)) } + } + + #[cfg(test)] + pub fn unique() -> Self { + unsafe { Self::new(NSPasteboard::pasteboardWithUniqueName(nil)) } + } + + unsafe fn new(inner: id) -> Self { + Self { + inner, + text_hash_type: unsafe { ns_string("zed-text-hash") }, + metadata_type: unsafe { ns_string("zed-metadata") }, + } + } + + pub fn read(&self) -> Option { + // First, see if it's a string. + unsafe { + let pasteboard_types: id = self.inner.types(); + let string_type: id = ns_string("public.utf8-plain-text"); + + if msg_send![pasteboard_types, containsObject: string_type] { + let data = self.inner.dataForType(string_type); + if data == nil { + return None; + } else if data.bytes().is_null() { + // https://developer.apple.com/documentation/foundation/nsdata/1410616-bytes?language=objc + // "If the length of the NSData object is 0, this property returns nil." + return Some(self.read_string(&[])); + } else { + let bytes = + slice::from_raw_parts(data.bytes() as *mut u8, data.length() as usize); + + return Some(self.read_string(bytes)); + } + } + + // If it wasn't a string, try the various supported image types. + for format in ImageFormat::iter() { + if let Some(item) = self.read_image(format) { + return Some(item); + } + } + } + + // If it wasn't a string or a supported image type, give up. + None + } + + fn read_image(&self, format: ImageFormat) -> Option { + let mut ut_type: UTType = format.into(); + + unsafe { + let types: id = self.inner.types(); + if msg_send![types, containsObject: ut_type.inner()] { + self.data_for_type(ut_type.inner_mut()).map(|bytes| { + let bytes = bytes.to_vec(); + let id = hash(&bytes); + + ClipboardItem { + entries: vec![ClipboardEntry::Image(Image { format, bytes, id })], + } + }) + } else { + None + } + } + } + + fn read_string(&self, text_bytes: &[u8]) -> ClipboardItem { + unsafe { + let text = String::from_utf8_lossy(text_bytes).to_string(); + let metadata = self + .data_for_type(self.text_hash_type) + .and_then(|hash_bytes| { + let hash_bytes = hash_bytes.try_into().ok()?; + let hash = u64::from_be_bytes(hash_bytes); + let metadata = self.data_for_type(self.metadata_type)?; + + if hash == ClipboardString::text_hash(&text) { + String::from_utf8(metadata.to_vec()).ok() + } else { + None + } + }); + + ClipboardItem { + entries: vec![ClipboardEntry::String(ClipboardString { text, metadata })], + } + } + } + + unsafe fn data_for_type(&self, kind: id) -> Option<&[u8]> { + unsafe { + let data = self.inner.dataForType(kind); + if data == nil { + None + } else { + Some(slice::from_raw_parts( + data.bytes() as *mut u8, + data.length() as usize, + )) + } + } + } + + pub fn write(&self, item: ClipboardItem) { + unsafe { + match item.entries.as_slice() { + [] => { + // Writing an empty list of entries just clears the clipboard. + self.inner.clearContents(); + } + [ClipboardEntry::String(string)] => { + self.write_plaintext(string); + } + [ClipboardEntry::Image(image)] => { + self.write_image(image); + } + [ClipboardEntry::ExternalPaths(_)] => {} + _ => { + // Agus NB: We're currently only writing string entries to the clipboard when we have more than one. + // + // This was the existing behavior before I refactored the outer clipboard code: + // https://github.com/zed-industries/zed/blob/65f7412a0265552b06ce122655369d6cc7381dd6/crates/gpui/src/platform/mac/platform.rs#L1060-L1110 + // + // Note how `any_images` is always `false`. We should fix that, but that's orthogonal to the refactor. + + let mut combined = ClipboardString { + text: String::new(), + metadata: None, + }; + + for entry in item.entries { + match entry { + ClipboardEntry::String(text) => { + combined.text.push_str(&text.text()); + if combined.metadata.is_none() { + combined.metadata = text.metadata; + } + } + _ => {} + } + } + + self.write_plaintext(&combined); + } + } + } + } + + fn write_plaintext(&self, string: &ClipboardString) { + unsafe { + self.inner.clearContents(); + + let text_bytes = NSData::dataWithBytes_length_( + nil, + string.text.as_ptr() as *const c_void, + string.text.len() as u64, + ); + self.inner + .setData_forType(text_bytes, NSPasteboardTypeString); + + if let Some(metadata) = string.metadata.as_ref() { + let hash_bytes = ClipboardString::text_hash(&string.text).to_be_bytes(); + let hash_bytes = NSData::dataWithBytes_length_( + nil, + hash_bytes.as_ptr() as *const c_void, + hash_bytes.len() as u64, + ); + self.inner.setData_forType(hash_bytes, self.text_hash_type); + + let metadata_bytes = NSData::dataWithBytes_length_( + nil, + metadata.as_ptr() as *const c_void, + metadata.len() as u64, + ); + self.inner + .setData_forType(metadata_bytes, self.metadata_type); + } + } + } + + unsafe fn write_image(&self, image: &Image) { + unsafe { + self.inner.clearContents(); + + let bytes = NSData::dataWithBytes_length_( + nil, + image.bytes.as_ptr() as *const c_void, + image.bytes.len() as u64, + ); + + self.inner + .setData_forType(bytes, Into::::into(image.format).inner_mut()); + } + } +} + +#[link(name = "AppKit", kind = "framework")] +unsafe extern "C" { + /// [Apple's documentation](https://developer.apple.com/documentation/appkit/nspasteboardnamefind?language=objc) + pub static NSPasteboardNameFind: id; +} + +impl From for UTType { + fn from(value: ImageFormat) -> Self { + match value { + ImageFormat::Png => Self::png(), + ImageFormat::Jpeg => Self::jpeg(), + ImageFormat::Tiff => Self::tiff(), + ImageFormat::Webp => Self::webp(), + ImageFormat::Gif => Self::gif(), + ImageFormat::Bmp => Self::bmp(), + ImageFormat::Svg => Self::svg(), + ImageFormat::Ico => Self::ico(), + } + } +} + +// See https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/ +pub struct UTType(id); + +impl UTType { + pub fn png() -> Self { + // https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/png + Self(unsafe { NSPasteboardTypePNG }) // This is a rare case where there's a built-in NSPasteboardType + } + + pub fn jpeg() -> Self { + // https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/jpeg + Self(unsafe { ns_string("public.jpeg") }) + } + + pub fn gif() -> Self { + // https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/gif + Self(unsafe { ns_string("com.compuserve.gif") }) + } + + pub fn webp() -> Self { + // https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/webp + Self(unsafe { ns_string("org.webmproject.webp") }) + } + + pub fn bmp() -> Self { + // https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/bmp + Self(unsafe { ns_string("com.microsoft.bmp") }) + } + + pub fn svg() -> Self { + // https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/svg + Self(unsafe { ns_string("public.svg-image") }) + } + + pub fn ico() -> Self { + // https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/ico + Self(unsafe { ns_string("com.microsoft.ico") }) + } + + pub fn tiff() -> Self { + // https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/tiff + Self(unsafe { NSPasteboardTypeTIFF }) // This is a rare case where there's a built-in NSPasteboardType + } + + fn inner(&self) -> *const Object { + self.0 + } + + pub fn inner_mut(&self) -> *mut Object { + self.0 as *mut _ + } +} + +#[cfg(test)] +mod tests { + use cocoa::{appkit::NSPasteboardTypeString, foundation::NSData}; + + use crate::{ClipboardEntry, ClipboardItem, ClipboardString}; + + use super::*; + + #[test] + fn test_string() { + let pasteboard = Pasteboard::unique(); + assert_eq!(pasteboard.read(), None); + + let item = ClipboardItem::new_string("1".to_string()); + pasteboard.write(item.clone()); + assert_eq!(pasteboard.read(), Some(item)); + + let item = ClipboardItem { + entries: vec![ClipboardEntry::String( + ClipboardString::new("2".to_string()).with_json_metadata(vec![3, 4]), + )], + }; + pasteboard.write(item.clone()); + assert_eq!(pasteboard.read(), Some(item)); + + let text_from_other_app = "text from other app"; + unsafe { + let bytes = NSData::dataWithBytes_length_( + nil, + text_from_other_app.as_ptr() as *const c_void, + text_from_other_app.len() as u64, + ); + pasteboard + .inner + .setData_forType(bytes, NSPasteboardTypeString); + } + assert_eq!( + pasteboard.read(), + Some(ClipboardItem::new_string(text_from_other_app.to_string())) + ); + } +} diff --git a/crates/gpui/src/platform/mac/platform.rs b/crates/gpui/src/platform/mac/platform.rs index ee67f465e34bd8109246f68b311e225aa8f9fd0a..9b32c6735bf6215fecc0455defc4237fd25e8cb0 100644 --- a/crates/gpui/src/platform/mac/platform.rs +++ b/crates/gpui/src/platform/mac/platform.rs @@ -1,29 +1,24 @@ use super::{ - BoolExt, MacKeyboardLayout, MacKeyboardMapper, - attributed_string::{NSAttributedString, NSMutableAttributedString}, - events::key_to_native, - ns_string, renderer, + BoolExt, MacKeyboardLayout, MacKeyboardMapper, events::key_to_native, ns_string, renderer, }; use crate::{ - Action, AnyWindowHandle, BackgroundExecutor, ClipboardEntry, ClipboardItem, ClipboardString, - CursorStyle, ForegroundExecutor, Image, ImageFormat, KeyContext, Keymap, MacDispatcher, - MacDisplay, MacWindow, Menu, MenuItem, OsMenu, OwnedMenu, PathPromptOptions, Platform, - PlatformDisplay, PlatformKeyboardLayout, PlatformKeyboardMapper, PlatformTextSystem, - PlatformWindow, Result, SystemMenuType, Task, WindowAppearance, WindowParams, hash, + Action, AnyWindowHandle, BackgroundExecutor, ClipboardItem, CursorStyle, ForegroundExecutor, + KeyContext, Keymap, MacDispatcher, MacDisplay, MacWindow, Menu, MenuItem, OsMenu, OwnedMenu, + PathPromptOptions, Platform, PlatformDisplay, PlatformKeyboardLayout, PlatformKeyboardMapper, + PlatformTextSystem, PlatformWindow, Result, SystemMenuType, Task, WindowAppearance, + WindowParams, platform::mac::pasteboard::Pasteboard, }; use anyhow::{Context as _, anyhow}; use block::ConcreteBlock; use cocoa::{ appkit::{ NSApplication, NSApplicationActivationPolicy::NSApplicationActivationPolicyRegular, - NSEventModifierFlags, NSMenu, NSMenuItem, NSModalResponse, NSOpenPanel, NSPasteboard, - NSPasteboardTypePNG, NSPasteboardTypeRTF, NSPasteboardTypeRTFD, NSPasteboardTypeString, - NSPasteboardTypeTIFF, NSSavePanel, NSVisualEffectState, NSVisualEffectView, NSWindow, + NSEventModifierFlags, NSMenu, NSMenuItem, NSModalResponse, NSOpenPanel, NSSavePanel, + NSVisualEffectState, NSVisualEffectView, NSWindow, }, base::{BOOL, NO, YES, id, nil, selector}, foundation::{ - NSArray, NSAutoreleasePool, NSBundle, NSData, NSInteger, NSProcessInfo, NSRange, NSString, - NSUInteger, NSURL, + NSArray, NSAutoreleasePool, NSBundle, NSInteger, NSProcessInfo, NSString, NSUInteger, NSURL, }, }; use core_foundation::{ @@ -49,7 +44,6 @@ use ptr::null_mut; use semver::Version; use std::{ cell::Cell, - convert::TryInto, ffi::{CStr, OsStr, c_void}, os::{raw::c_char, unix::ffi::OsStrExt}, path::{Path, PathBuf}, @@ -58,7 +52,6 @@ use std::{ slice, str, sync::{Arc, OnceLock}, }; -use strum::IntoEnumIterator; use util::{ ResultExt, command::{new_smol_command, new_std_command}, @@ -164,9 +157,8 @@ pub(crate) struct MacPlatformState { text_system: Arc, renderer_context: renderer::Context, headless: bool, - pasteboard: id, - text_hash_pasteboard_type: id, - metadata_pasteboard_type: id, + general_pasteboard: Pasteboard, + find_pasteboard: Pasteboard, reopen: Option>, on_keyboard_layout_change: Option>, quit: Option>, @@ -206,9 +198,8 @@ impl MacPlatform { background_executor: BackgroundExecutor::new(dispatcher.clone()), foreground_executor: ForegroundExecutor::new(dispatcher), renderer_context: renderer::Context::default(), - pasteboard: unsafe { NSPasteboard::generalPasteboard(nil) }, - text_hash_pasteboard_type: unsafe { ns_string("zed-text-hash") }, - metadata_pasteboard_type: unsafe { ns_string("zed-metadata") }, + general_pasteboard: Pasteboard::general(), + find_pasteboard: Pasteboard::find(), reopen: None, quit: None, menu_command: None, @@ -224,20 +215,6 @@ impl MacPlatform { })) } - unsafe fn read_from_pasteboard(&self, pasteboard: *mut Object, kind: id) -> Option<&[u8]> { - unsafe { - let data = pasteboard.dataForType(kind); - if data == nil { - None - } else { - Some(slice::from_raw_parts( - data.bytes() as *mut u8, - data.length() as usize, - )) - } - } - } - unsafe fn create_menu_bar( &self, menus: &Vec, @@ -1034,119 +1011,24 @@ impl Platform for MacPlatform { } } - fn write_to_clipboard(&self, item: ClipboardItem) { - use crate::ClipboardEntry; - - unsafe { - // We only want to use NSAttributedString if there are multiple entries to write. - if item.entries.len() <= 1 { - match item.entries.first() { - Some(entry) => match entry { - ClipboardEntry::String(string) => { - self.write_plaintext_to_clipboard(string); - } - ClipboardEntry::Image(image) => { - self.write_image_to_clipboard(image); - } - ClipboardEntry::ExternalPaths(_) => {} - }, - None => { - // Writing an empty list of entries just clears the clipboard. - let state = self.0.lock(); - state.pasteboard.clearContents(); - } - } - } else { - let mut any_images = false; - let attributed_string = { - let mut buf = NSMutableAttributedString::alloc(nil) - // TODO can we skip this? Or at least part of it? - .init_attributed_string(ns_string("")) - .autorelease(); - - for entry in item.entries { - if let ClipboardEntry::String(ClipboardString { text, metadata: _ }) = entry - { - let to_append = NSAttributedString::alloc(nil) - .init_attributed_string(ns_string(&text)) - .autorelease(); - - buf.appendAttributedString_(to_append); - } - } - - buf - }; - - let state = self.0.lock(); - state.pasteboard.clearContents(); - - // Only set rich text clipboard types if we actually have 1+ images to include. - if any_images { - let rtfd_data = attributed_string.RTFDFromRange_documentAttributes_( - NSRange::new(0, msg_send![attributed_string, length]), - nil, - ); - if rtfd_data != nil { - state - .pasteboard - .setData_forType(rtfd_data, NSPasteboardTypeRTFD); - } - - let rtf_data = attributed_string.RTFFromRange_documentAttributes_( - NSRange::new(0, attributed_string.length()), - nil, - ); - if rtf_data != nil { - state - .pasteboard - .setData_forType(rtf_data, NSPasteboardTypeRTF); - } - } - - let plain_text = attributed_string.string(); - state - .pasteboard - .setString_forType(plain_text, NSPasteboardTypeString); - } - } - } - fn read_from_clipboard(&self) -> Option { let state = self.0.lock(); - let pasteboard = state.pasteboard; - - // First, see if it's a string. - unsafe { - let types: id = pasteboard.types(); - let string_type: id = ns_string("public.utf8-plain-text"); - - if msg_send![types, containsObject: string_type] { - let data = pasteboard.dataForType(string_type); - if data == nil { - return None; - } else if data.bytes().is_null() { - // https://developer.apple.com/documentation/foundation/nsdata/1410616-bytes?language=objc - // "If the length of the NSData object is 0, this property returns nil." - return Some(self.read_string_from_clipboard(&state, &[])); - } else { - let bytes = - slice::from_raw_parts(data.bytes() as *mut u8, data.length() as usize); + state.general_pasteboard.read() + } - return Some(self.read_string_from_clipboard(&state, bytes)); - } - } + fn write_to_clipboard(&self, item: ClipboardItem) { + let state = self.0.lock(); + state.general_pasteboard.write(item); + } - // If it wasn't a string, try the various supported image types. - for format in ImageFormat::iter() { - if let Some(item) = try_clipboard_image(pasteboard, format) { - return Some(item); - } - } - } + fn read_from_find_pasteboard(&self) -> Option { + let state = self.0.lock(); + state.find_pasteboard.read() + } - // If it wasn't a string or a supported image type, give up. - None + fn write_to_find_pasteboard(&self, item: ClipboardItem) { + let state = self.0.lock(); + state.find_pasteboard.write(item); } fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Task> { @@ -1255,116 +1137,6 @@ impl Platform for MacPlatform { } } -impl MacPlatform { - unsafe fn read_string_from_clipboard( - &self, - state: &MacPlatformState, - text_bytes: &[u8], - ) -> ClipboardItem { - unsafe { - let text = String::from_utf8_lossy(text_bytes).to_string(); - let metadata = self - .read_from_pasteboard(state.pasteboard, state.text_hash_pasteboard_type) - .and_then(|hash_bytes| { - let hash_bytes = hash_bytes.try_into().ok()?; - let hash = u64::from_be_bytes(hash_bytes); - let metadata = self - .read_from_pasteboard(state.pasteboard, state.metadata_pasteboard_type)?; - - if hash == ClipboardString::text_hash(&text) { - String::from_utf8(metadata.to_vec()).ok() - } else { - None - } - }); - - ClipboardItem { - entries: vec![ClipboardEntry::String(ClipboardString { text, metadata })], - } - } - } - - unsafe fn write_plaintext_to_clipboard(&self, string: &ClipboardString) { - unsafe { - let state = self.0.lock(); - state.pasteboard.clearContents(); - - let text_bytes = NSData::dataWithBytes_length_( - nil, - string.text.as_ptr() as *const c_void, - string.text.len() as u64, - ); - state - .pasteboard - .setData_forType(text_bytes, NSPasteboardTypeString); - - if let Some(metadata) = string.metadata.as_ref() { - let hash_bytes = ClipboardString::text_hash(&string.text).to_be_bytes(); - let hash_bytes = NSData::dataWithBytes_length_( - nil, - hash_bytes.as_ptr() as *const c_void, - hash_bytes.len() as u64, - ); - state - .pasteboard - .setData_forType(hash_bytes, state.text_hash_pasteboard_type); - - let metadata_bytes = NSData::dataWithBytes_length_( - nil, - metadata.as_ptr() as *const c_void, - metadata.len() as u64, - ); - state - .pasteboard - .setData_forType(metadata_bytes, state.metadata_pasteboard_type); - } - } - } - - unsafe fn write_image_to_clipboard(&self, image: &Image) { - unsafe { - let state = self.0.lock(); - state.pasteboard.clearContents(); - - let bytes = NSData::dataWithBytes_length_( - nil, - image.bytes.as_ptr() as *const c_void, - image.bytes.len() as u64, - ); - - state - .pasteboard - .setData_forType(bytes, Into::::into(image.format).inner_mut()); - } - } -} - -fn try_clipboard_image(pasteboard: id, format: ImageFormat) -> Option { - let mut ut_type: UTType = format.into(); - - unsafe { - let types: id = pasteboard.types(); - if msg_send![types, containsObject: ut_type.inner()] { - let data = pasteboard.dataForType(ut_type.inner_mut()); - if data == nil { - None - } else { - let bytes = Vec::from(slice::from_raw_parts( - data.bytes() as *mut u8, - data.length() as usize, - )); - let id = hash(&bytes); - - Some(ClipboardItem { - entries: vec![ClipboardEntry::Image(Image { format, bytes, id })], - }) - } - } else { - None - } - } -} - unsafe fn path_from_objc(path: id) -> PathBuf { let len = msg_send![path, lengthOfBytesUsingEncoding: NSUTF8StringEncoding]; let bytes = unsafe { path.UTF8String() as *const u8 }; @@ -1605,120 +1377,3 @@ mod security { pub const errSecUserCanceled: OSStatus = -128; pub const errSecItemNotFound: OSStatus = -25300; } - -impl From for UTType { - fn from(value: ImageFormat) -> Self { - match value { - ImageFormat::Png => Self::png(), - ImageFormat::Jpeg => Self::jpeg(), - ImageFormat::Tiff => Self::tiff(), - ImageFormat::Webp => Self::webp(), - ImageFormat::Gif => Self::gif(), - ImageFormat::Bmp => Self::bmp(), - ImageFormat::Svg => Self::svg(), - ImageFormat::Ico => Self::ico(), - } - } -} - -// See https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/ -struct UTType(id); - -impl UTType { - pub fn png() -> Self { - // https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/png - Self(unsafe { NSPasteboardTypePNG }) // This is a rare case where there's a built-in NSPasteboardType - } - - pub fn jpeg() -> Self { - // https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/jpeg - Self(unsafe { ns_string("public.jpeg") }) - } - - pub fn gif() -> Self { - // https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/gif - Self(unsafe { ns_string("com.compuserve.gif") }) - } - - pub fn webp() -> Self { - // https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/webp - Self(unsafe { ns_string("org.webmproject.webp") }) - } - - pub fn bmp() -> Self { - // https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/bmp - Self(unsafe { ns_string("com.microsoft.bmp") }) - } - - pub fn svg() -> Self { - // https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/svg - Self(unsafe { ns_string("public.svg-image") }) - } - - pub fn ico() -> Self { - // https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/ico - Self(unsafe { ns_string("com.microsoft.ico") }) - } - - pub fn tiff() -> Self { - // https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/tiff - Self(unsafe { NSPasteboardTypeTIFF }) // This is a rare case where there's a built-in NSPasteboardType - } - - fn inner(&self) -> *const Object { - self.0 - } - - fn inner_mut(&self) -> *mut Object { - self.0 as *mut _ - } -} - -#[cfg(test)] -mod tests { - use crate::ClipboardItem; - - use super::*; - - #[test] - fn test_clipboard() { - let platform = build_platform(); - assert_eq!(platform.read_from_clipboard(), None); - - let item = ClipboardItem::new_string("1".to_string()); - platform.write_to_clipboard(item.clone()); - assert_eq!(platform.read_from_clipboard(), Some(item)); - - let item = ClipboardItem { - entries: vec![ClipboardEntry::String( - ClipboardString::new("2".to_string()).with_json_metadata(vec![3, 4]), - )], - }; - platform.write_to_clipboard(item.clone()); - assert_eq!(platform.read_from_clipboard(), Some(item)); - - let text_from_other_app = "text from other app"; - unsafe { - let bytes = NSData::dataWithBytes_length_( - nil, - text_from_other_app.as_ptr() as *const c_void, - text_from_other_app.len() as u64, - ); - platform - .0 - .lock() - .pasteboard - .setData_forType(bytes, NSPasteboardTypeString); - } - assert_eq!( - platform.read_from_clipboard(), - Some(ClipboardItem::new_string(text_from_other_app.to_string())) - ); - } - - fn build_platform() -> MacPlatform { - let platform = MacPlatform::new(false); - platform.0.lock().pasteboard = unsafe { NSPasteboard::pasteboardWithUniqueName(nil) }; - platform - } -} diff --git a/crates/gpui/src/platform/mac/screen_capture.rs b/crates/gpui/src/platform/mac/screen_capture.rs index 2f2c1eae335c8bcb366879661534c46dacfd47b4..4b80a87d32f45540c76790065514f29cc7f93b3f 100644 --- a/crates/gpui/src/platform/mac/screen_capture.rs +++ b/crates/gpui/src/platform/mac/screen_capture.rs @@ -110,13 +110,21 @@ impl ScreenCaptureSource for MacScreenCaptureSource { let _: id = msg_send![configuration, setHeight: meta.resolution.height.0 as i64]; let stream: id = msg_send![stream, initWithFilter:filter configuration:configuration delegate:delegate]; + // Stream contains filter, configuration, and delegate internally so we release them here + // to prevent a memory leak when steam is dropped + let _: () = msg_send![filter, release]; + let _: () = msg_send![configuration, release]; + let _: () = msg_send![delegate, release]; + let (mut tx, rx) = oneshot::channel(); let mut error: id = nil; let _: () = msg_send![stream, addStreamOutput:output type:SCStreamOutputTypeScreen sampleHandlerQueue:0 error:&mut error as *mut id]; if error != nil { let message: id = msg_send![error, localizedDescription]; - tx.send(Err(anyhow!("failed to add stream output {message:?}"))) + let _: () = msg_send![stream, release]; + let _: () = msg_send![output, release]; + tx.send(Err(anyhow!("failed to add stream output {message:?}"))) .ok(); return rx; } @@ -132,8 +140,10 @@ impl ScreenCaptureSource for MacScreenCaptureSource { }; Ok(Box::new(stream) as Box) } else { + let _: () = msg_send![stream, release]; + let _: () = msg_send![output, release]; let message: id = msg_send![error, localizedDescription]; - Err(anyhow!("failed to stop screen capture stream {message:?}")) + Err(anyhow!("failed to start screen capture stream {message:?}")) }; if let Some(tx) = tx.borrow_mut().take() { tx.send(result).ok(); diff --git a/crates/gpui/src/platform/mac/text_system.rs b/crates/gpui/src/platform/mac/text_system.rs index 3faf4e6491e6588bdb1341d5a8845171562fa8a0..8595582f4ad7e078f7cfb0140e249feb0a9740dc 100644 --- a/crates/gpui/src/platform/mac/text_system.rs +++ b/crates/gpui/src/platform/mac/text_system.rs @@ -8,6 +8,7 @@ use anyhow::anyhow; use cocoa::appkit::CGFloat; use collections::HashMap; use core_foundation::{ + array::{CFArray, CFArrayRef}, attributed_string::CFMutableAttributedString, base::{CFRange, TCFType}, number::CFNumber, @@ -21,8 +22,10 @@ use core_graphics::{ }; use core_text::{ font::CTFont, + font_collection::CTFontCollectionRef, font_descriptor::{ - kCTFontSlantTrait, kCTFontSymbolicTrait, kCTFontWeightTrait, kCTFontWidthTrait, + CTFontDescriptor, kCTFontSlantTrait, kCTFontSymbolicTrait, kCTFontWeightTrait, + kCTFontWidthTrait, }, line::CTLine, string_attributes::kCTFontAttributeName, @@ -97,7 +100,26 @@ impl PlatformTextSystem for MacTextSystem { fn all_font_names(&self) -> Vec { let mut names = Vec::new(); let collection = core_text::font_collection::create_for_all_families(); - let Some(descriptors) = collection.get_descriptors() else { + // NOTE: We intentionally avoid using `collection.get_descriptors()` here because + // it has a memory leak bug in core-text v21.0.0. The upstream code uses + // `wrap_under_get_rule` but `CTFontCollectionCreateMatchingFontDescriptors` + // follows the Create Rule (caller owns the result), so it should use + // `wrap_under_create_rule`. We call the function directly with correct memory management. + unsafe extern "C" { + fn CTFontCollectionCreateMatchingFontDescriptors( + collection: CTFontCollectionRef, + ) -> CFArrayRef; + } + let descriptors: Option> = unsafe { + let array_ref = + CTFontCollectionCreateMatchingFontDescriptors(collection.as_concrete_TypeRef()); + if array_ref.is_null() { + None + } else { + Some(CFArray::wrap_under_create_rule(array_ref)) + } + }; + let Some(descriptors) = descriptors else { return names; }; for descriptor in descriptors.into_iter() { diff --git a/crates/gpui/src/platform/mac/window.rs b/crates/gpui/src/platform/mac/window.rs index 19ad1777570da9494148e01161e156748cd9bcfc..f843fcd943523dc9a1c228cea1c4dcdf63c76097 100644 --- a/crates/gpui/src/platform/mac/window.rs +++ b/crates/gpui/src/platform/mac/window.rs @@ -62,9 +62,12 @@ static mut BLURRED_VIEW_CLASS: *const Class = ptr::null(); #[allow(non_upper_case_globals)] const NSWindowStyleMaskNonactivatingPanel: NSWindowStyleMask = NSWindowStyleMask::from_bits_retain(1 << 7); +// WindowLevel const value ref: https://docs.rs/core-graphics2/0.4.1/src/core_graphics2/window_level.rs.html #[allow(non_upper_case_globals)] const NSNormalWindowLevel: NSInteger = 0; #[allow(non_upper_case_globals)] +const NSFloatingWindowLevel: NSInteger = 3; +#[allow(non_upper_case_globals)] const NSPopUpWindowLevel: NSInteger = 101; #[allow(non_upper_case_globals)] const NSTrackingMouseEnteredAndExited: NSUInteger = 0x01; @@ -423,6 +426,8 @@ struct MacWindowState { select_previous_tab_callback: Option>, toggle_tab_bar_callback: Option>, activated_least_once: bool, + // The parent window if this window is a sheet (Dialog kind) + sheet_parent: Option, } impl MacWindowState { @@ -622,11 +627,16 @@ impl MacWindow { } let native_window: id = match kind { - WindowKind::Normal | WindowKind::Floating => msg_send![WINDOW_CLASS, alloc], + WindowKind::Normal => { + msg_send![WINDOW_CLASS, alloc] + } WindowKind::PopUp => { style_mask |= NSWindowStyleMaskNonactivatingPanel; msg_send![PANEL_CLASS, alloc] } + WindowKind::Floating | WindowKind::Dialog => { + msg_send![PANEL_CLASS, alloc] + } }; let display = display_id @@ -729,6 +739,7 @@ impl MacWindow { select_previous_tab_callback: None, toggle_tab_bar_callback: None, activated_least_once: false, + sheet_parent: None, }))); (*native_window).set_ivar( @@ -779,9 +790,18 @@ impl MacWindow { content_view.addSubview_(native_view.autorelease()); native_window.makeFirstResponder_(native_view); + let app: id = NSApplication::sharedApplication(nil); + let main_window: id = msg_send![app, mainWindow]; + let mut sheet_parent = None; + match kind { WindowKind::Normal | WindowKind::Floating => { - native_window.setLevel_(NSNormalWindowLevel); + if kind == WindowKind::Floating { + // Let the window float keep above normal windows. + native_window.setLevel_(NSFloatingWindowLevel); + } else { + native_window.setLevel_(NSNormalWindowLevel); + } native_window.setAcceptsMouseMovedEvents_(YES); if let Some(tabbing_identifier) = tabbing_identifier { @@ -816,10 +836,23 @@ impl MacWindow { NSWindowCollectionBehavior::NSWindowCollectionBehaviorFullScreenAuxiliary ); } + WindowKind::Dialog => { + if !main_window.is_null() { + let parent = { + let active_sheet: id = msg_send![main_window, attachedSheet]; + if active_sheet.is_null() { + main_window + } else { + active_sheet + } + }; + let _: () = + msg_send![parent, beginSheet: native_window completionHandler: nil]; + sheet_parent = Some(parent); + } + } } - let app = NSApplication::sharedApplication(nil); - let main_window: id = msg_send![app, mainWindow]; if allows_automatic_window_tabbing && !main_window.is_null() && main_window != native_window @@ -861,7 +894,11 @@ impl MacWindow { // the window position might be incorrect if the main screen (the screen that contains the window that has focus) // is different from the primary screen. NSWindow::setFrameTopLeftPoint_(native_window, window_rect.origin); - window.0.lock().move_traffic_light(); + { + let mut window_state = window.0.lock(); + window_state.move_traffic_light(); + window_state.sheet_parent = sheet_parent; + } pool.drain(); @@ -938,6 +975,7 @@ impl Drop for MacWindow { let mut this = self.0.lock(); this.renderer.destroy(); let window = this.native_window; + let sheet_parent = this.sheet_parent.take(); this.display_link.take(); unsafe { this.native_window.setDelegate_(nil); @@ -946,6 +984,9 @@ impl Drop for MacWindow { this.executor .spawn(async move { unsafe { + if let Some(parent) = sheet_parent { + let _: () = msg_send![parent, endSheet: window]; + } window.close(); window.autorelease(); } @@ -1190,6 +1231,7 @@ impl PlatformWindow for MacWindow { let (done_tx, done_rx) = oneshot::channel(); let done_tx = Cell::new(Some(done_tx)); let block = ConcreteBlock::new(move |answer: NSInteger| { + let _: () = msg_send![alert, release]; if let Some(done_tx) = done_tx.take() { let _ = done_tx.send(answer.try_into().unwrap()); } diff --git a/crates/gpui/src/platform/test/platform.rs b/crates/gpui/src/platform/test/platform.rs index dfada364667989792325e02f8530e6c91bdf4716..ca9d5e2c3b7d405e40f208f5406f879467eafc5c 100644 --- a/crates/gpui/src/platform/test/platform.rs +++ b/crates/gpui/src/platform/test/platform.rs @@ -32,6 +32,8 @@ pub(crate) struct TestPlatform { current_clipboard_item: Mutex>, #[cfg(any(target_os = "linux", target_os = "freebsd"))] current_primary_item: Mutex>, + #[cfg(target_os = "macos")] + current_find_pasteboard_item: Mutex>, pub(crate) prompts: RefCell, screen_capture_sources: RefCell>, pub opened_url: RefCell>, @@ -117,6 +119,8 @@ impl TestPlatform { current_clipboard_item: Mutex::new(None), #[cfg(any(target_os = "linux", target_os = "freebsd"))] current_primary_item: Mutex::new(None), + #[cfg(target_os = "macos")] + current_find_pasteboard_item: Mutex::new(None), weak: weak.clone(), opened_url: Default::default(), #[cfg(target_os = "windows")] @@ -398,9 +402,8 @@ impl Platform for TestPlatform { false } - #[cfg(any(target_os = "linux", target_os = "freebsd"))] - fn write_to_primary(&self, item: ClipboardItem) { - *self.current_primary_item.lock() = Some(item); + fn read_from_clipboard(&self) -> Option { + self.current_clipboard_item.lock().clone() } fn write_to_clipboard(&self, item: ClipboardItem) { @@ -412,8 +415,19 @@ impl Platform for TestPlatform { self.current_primary_item.lock().clone() } - fn read_from_clipboard(&self) -> Option { - self.current_clipboard_item.lock().clone() + #[cfg(any(target_os = "linux", target_os = "freebsd"))] + fn write_to_primary(&self, item: ClipboardItem) { + *self.current_primary_item.lock() = Some(item); + } + + #[cfg(target_os = "macos")] + fn read_from_find_pasteboard(&self) -> Option { + self.current_find_pasteboard_item.lock().clone() + } + + #[cfg(target_os = "macos")] + fn write_to_find_pasteboard(&self, item: ClipboardItem) { + *self.current_find_pasteboard_item.lock() = Some(item); } fn write_credentials(&self, _url: &str, _username: &str, _password: &[u8]) -> Task> { diff --git a/crates/gpui/src/platform/windows/events.rs b/crates/gpui/src/platform/windows/events.rs index e6fa6006eb95ec45f1634cb72ef63e2f622455a7..1f0a4a0d28c2b266fb8588e4ce54251be010a78d 100644 --- a/crates/gpui/src/platform/windows/events.rs +++ b/crates/gpui/src/platform/windows/events.rs @@ -40,6 +40,11 @@ impl WindowsWindowInner { lparam: LPARAM, ) -> LRESULT { let handled = match msg { + // eagerly activate the window, so calls to `active_window` will work correctly + WM_MOUSEACTIVATE => { + unsafe { SetActiveWindow(handle).log_err() }; + None + } WM_ACTIVATE => self.handle_activate_msg(wparam), WM_CREATE => self.handle_create_msg(handle), WM_MOVE => self.handle_move_msg(handle, lparam), @@ -265,6 +270,14 @@ impl WindowsWindowInner { fn handle_destroy_msg(&self, handle: HWND) -> Option { let callback = { self.state.callbacks.close.take() }; + // Re-enable parent window if this was a modal dialog + if let Some(parent_hwnd) = self.parent_hwnd { + unsafe { + let _ = EnableWindow(parent_hwnd, true); + let _ = SetForegroundWindow(parent_hwnd); + } + } + if let Some(callback) = callback { callback(); } diff --git a/crates/gpui/src/platform/windows/platform.rs b/crates/gpui/src/platform/windows/platform.rs index af0cb89ecc94da70cc42c8d4c397aeb2a811d6fb..0e0fdd56c54d56587c09bca14f16dd8e5aef389d 100644 --- a/crates/gpui/src/platform/windows/platform.rs +++ b/crates/gpui/src/platform/windows/platform.rs @@ -659,7 +659,7 @@ impl Platform for WindowsPlatform { if let Err(err) = result { // ERROR_NOT_FOUND means the credential doesn't exist. // Return Ok(None) to match macOS and Linux behavior. - if err.code().0 == ERROR_NOT_FOUND.0 as i32 { + if err.code() == ERROR_NOT_FOUND.to_hresult() { return Ok(None); } return Err(err.into()); diff --git a/crates/gpui/src/platform/windows/window.rs b/crates/gpui/src/platform/windows/window.rs index 7ef92b4150e69424b68e9417dda377aa7f2e9cc0..3fcc29ad7864f8e45d27638bef489ffbf03788b2 100644 --- a/crates/gpui/src/platform/windows/window.rs +++ b/crates/gpui/src/platform/windows/window.rs @@ -83,6 +83,7 @@ pub(crate) struct WindowsWindowInner { pub(crate) validation_number: usize, pub(crate) main_receiver: flume::Receiver, pub(crate) platform_window_handle: HWND, + pub(crate) parent_hwnd: Option, } impl WindowsWindowState { @@ -241,6 +242,7 @@ impl WindowsWindowInner { main_receiver: context.main_receiver.clone(), platform_window_handle: context.platform_window_handle, system_settings: WindowsSystemSettings::new(context.display), + parent_hwnd: context.parent_hwnd, })) } @@ -368,6 +370,7 @@ struct WindowCreateContext { disable_direct_composition: bool, directx_devices: DirectXDevices, invalidate_devices: Arc, + parent_hwnd: Option, } impl WindowsWindow { @@ -390,6 +393,20 @@ impl WindowsWindow { invalidate_devices, } = creation_info; register_window_class(icon); + let parent_hwnd = if params.kind == WindowKind::Dialog { + let parent_window = unsafe { GetActiveWindow() }; + if parent_window.is_invalid() { + None + } else { + // Disable the parent window to make this dialog modal + unsafe { + EnableWindow(parent_window, false).as_bool(); + }; + Some(parent_window) + } + } else { + None + }; let hide_title_bar = params .titlebar .as_ref() @@ -416,8 +433,14 @@ impl WindowsWindow { if params.is_minimizable { dwstyle |= WS_MINIMIZEBOX; } + let dwexstyle = if params.kind == WindowKind::Dialog { + dwstyle |= WS_POPUP | WS_CAPTION; + WS_EX_DLGMODALFRAME + } else { + WS_EX_APPWINDOW + }; - (WS_EX_APPWINDOW, dwstyle) + (dwexstyle, dwstyle) }; if !disable_direct_composition { dwexstyle |= WS_EX_NOREDIRECTIONBITMAP; @@ -449,6 +472,7 @@ impl WindowsWindow { disable_direct_composition, directx_devices, invalidate_devices, + parent_hwnd, }; let creation_result = unsafe { CreateWindowExW( @@ -460,7 +484,7 @@ impl WindowsWindow { CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, - None, + parent_hwnd, None, Some(hinstance.into()), Some(&context as *const _ as *const _), diff --git a/crates/gpui/src/queue.rs b/crates/gpui/src/queue.rs index 9e9da710977ee80df1853791918eebe5e7f01096..1ecec183c6c58a86e305343f7bcd1056cda7a581 100644 --- a/crates/gpui/src/queue.rs +++ b/crates/gpui/src/queue.rs @@ -1,4 +1,5 @@ use std::{ + collections::VecDeque, fmt, iter::FusedIterator, sync::{Arc, atomic::AtomicUsize}, @@ -9,9 +10,9 @@ use rand::{Rng, SeedableRng, rngs::SmallRng}; use crate::Priority; struct PriorityQueues { - high_priority: Vec, - medium_priority: Vec, - low_priority: Vec, + high_priority: VecDeque, + medium_priority: VecDeque, + low_priority: VecDeque, } impl PriorityQueues { @@ -42,9 +43,9 @@ impl PriorityQueueState { let mut queues = self.queues.lock(); match priority { Priority::Realtime(_) => unreachable!(), - Priority::High => queues.high_priority.push(item), - Priority::Medium => queues.medium_priority.push(item), - Priority::Low => queues.low_priority.push(item), + Priority::High => queues.high_priority.push_back(item), + Priority::Medium => queues.medium_priority.push_back(item), + Priority::Low => queues.low_priority.push_back(item), }; self.condvar.notify_one(); Ok(()) @@ -141,9 +142,9 @@ impl PriorityQueueReceiver { pub(crate) fn new() -> (PriorityQueueSender, Self) { let state = PriorityQueueState { queues: parking_lot::Mutex::new(PriorityQueues { - high_priority: Vec::new(), - medium_priority: Vec::new(), - low_priority: Vec::new(), + high_priority: VecDeque::new(), + medium_priority: VecDeque::new(), + low_priority: VecDeque::new(), }), condvar: parking_lot::Condvar::new(), receiver_count: AtomicUsize::new(1), @@ -226,7 +227,7 @@ impl PriorityQueueReceiver { if !queues.high_priority.is_empty() { let flip = self.rand.random_ratio(P::High.probability(), mass); if flip { - return Ok(queues.high_priority.pop()); + return Ok(queues.high_priority.pop_front()); } mass -= P::High.probability(); } @@ -234,7 +235,7 @@ impl PriorityQueueReceiver { if !queues.medium_priority.is_empty() { let flip = self.rand.random_ratio(P::Medium.probability(), mass); if flip { - return Ok(queues.medium_priority.pop()); + return Ok(queues.medium_priority.pop_front()); } mass -= P::Medium.probability(); } @@ -242,7 +243,7 @@ impl PriorityQueueReceiver { if !queues.low_priority.is_empty() { let flip = self.rand.random_ratio(P::Low.probability(), mass); if flip { - return Ok(queues.low_priority.pop()); + return Ok(queues.low_priority.pop_front()); } } diff --git a/crates/gpui/src/style.rs b/crates/gpui/src/style.rs index 4d6e6f490d81d967692a3e9d8316af75a7a4d306..7481b8001e5752599b90625450d7adb0c66ea2ca 100644 --- a/crates/gpui/src/style.rs +++ b/crates/gpui/src/style.rs @@ -334,9 +334,13 @@ pub enum WhiteSpace { /// How to truncate text that overflows the width of the element #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] pub enum TextOverflow { - /// Truncate the text when it doesn't fit, and represent this truncation by displaying the - /// provided string. + /// Truncate the text at the end when it doesn't fit, and represent this truncation by + /// displaying the provided string (e.g., "very long te…"). Truncate(SharedString), + /// Truncate the text at the start when it doesn't fit, and represent this truncation by + /// displaying the provided string at the beginning (e.g., "…ong text here"). + /// Typically more adequate for file paths where the end is more important than the beginning. + TruncateStart(SharedString), } /// How to align text within the element diff --git a/crates/gpui/src/styled.rs b/crates/gpui/src/styled.rs index e8088a84d7fc141d0a320988c6399afe2b93ce07..c5eef0d4496edea4d30c665c82dc0a9f00bb83be 100644 --- a/crates/gpui/src/styled.rs +++ b/crates/gpui/src/styled.rs @@ -75,13 +75,21 @@ pub trait Styled: Sized { self } - /// Sets the truncate overflowing text with an ellipsis (…) if needed. + /// Sets the truncate overflowing text with an ellipsis (…) at the end if needed. /// [Docs](https://tailwindcss.com/docs/text-overflow#ellipsis) fn text_ellipsis(mut self) -> Self { self.text_style().text_overflow = Some(TextOverflow::Truncate(ELLIPSIS)); self } + /// Sets the truncate overflowing text with an ellipsis (…) at the start if needed. + /// Typically more adequate for file paths where the end is more important than the beginning. + /// Note: This doesn't exist in Tailwind CSS. + fn text_ellipsis_start(mut self) -> Self { + self.text_style().text_overflow = Some(TextOverflow::TruncateStart(ELLIPSIS)); + self + } + /// Sets the text overflow behavior of the element. fn text_overflow(mut self, overflow: TextOverflow) -> Self { self.text_style().text_overflow = Some(overflow); diff --git a/crates/gpui/src/test.rs b/crates/gpui/src/test.rs index 5ae72d2be1688893374e16a55445558b5bc33040..2a5711a01a9c8f2874cea4803fc517089cafd0fe 100644 --- a/crates/gpui/src/test.rs +++ b/crates/gpui/src/test.rs @@ -69,7 +69,10 @@ pub fn run_test( std::mem::forget(error); } else { if is_multiple_runs { - eprintln!("failing seed: {}", seed); + eprintln!("failing seed: {seed}"); + eprintln!( + "You can rerun from this seed by setting the environmental variable SEED to {seed}" + ); } if let Some(on_fail_fn) = on_fail_fn { on_fail_fn() diff --git a/crates/gpui/src/text_system/line_wrapper.rs b/crates/gpui/src/text_system/line_wrapper.rs index 45159313b43c508029f2525234c80c6575d0f695..457316f353a48fa112de1736b2b7eaa2d4c72313 100644 --- a/crates/gpui/src/text_system/line_wrapper.rs +++ b/crates/gpui/src/text_system/line_wrapper.rs @@ -2,6 +2,15 @@ use crate::{FontId, FontRun, Pixels, PlatformTextSystem, SharedString, TextRun, use collections::HashMap; use std::{borrow::Cow, iter, sync::Arc}; +/// Determines whether to truncate text from the start or end. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum TruncateFrom { + /// Truncate text from the start. + Start, + /// Truncate text from the end. + End, +} + /// The GPUI line wrapper, used to wrap lines of text to a given width. pub struct LineWrapper { platform_text_system: Arc, @@ -128,40 +137,83 @@ impl LineWrapper { }) } - /// Truncate a line of text to the given width with this wrapper's font and font size. - pub fn truncate_line<'a>( + /// Determines if a line should be truncated based on its width. + /// + /// Returns the truncation index in `line`. + pub fn should_truncate_line( &mut self, - line: SharedString, + line: &str, truncate_width: Pixels, - truncation_suffix: &str, - runs: &'a [TextRun], - ) -> (SharedString, Cow<'a, [TextRun]>) { + truncation_affix: &str, + truncate_from: TruncateFrom, + ) -> Option { let mut width = px(0.); - let mut suffix_width = truncation_suffix + let suffix_width = truncation_affix .chars() .map(|c| self.width_for_char(c)) .fold(px(0.0), |a, x| a + x); - let mut char_indices = line.char_indices(); let mut truncate_ix = 0; - for (ix, c) in char_indices { - if width + suffix_width < truncate_width { - truncate_ix = ix; - } - let char_width = self.width_for_char(c); - width += char_width; + match truncate_from { + TruncateFrom::Start => { + for (ix, c) in line.char_indices().rev() { + if width + suffix_width < truncate_width { + truncate_ix = ix; + } - if width.floor() > truncate_width { - let result = - SharedString::from(format!("{}{}", &line[..truncate_ix], truncation_suffix)); - let mut runs = runs.to_vec(); - update_runs_after_truncation(&result, truncation_suffix, &mut runs); + let char_width = self.width_for_char(c); + width += char_width; - return (result, Cow::Owned(runs)); + if width.floor() > truncate_width { + return Some(truncate_ix); + } + } + } + TruncateFrom::End => { + for (ix, c) in line.char_indices() { + if width + suffix_width < truncate_width { + truncate_ix = ix; + } + + let char_width = self.width_for_char(c); + width += char_width; + + if width.floor() > truncate_width { + return Some(truncate_ix); + } + } } } - (line, Cow::Borrowed(runs)) + None + } + + /// Truncate a line of text to the given width with this wrapper's font and font size. + pub fn truncate_line<'a>( + &mut self, + line: SharedString, + truncate_width: Pixels, + truncation_affix: &str, + runs: &'a [TextRun], + truncate_from: TruncateFrom, + ) -> (SharedString, Cow<'a, [TextRun]>) { + if let Some(truncate_ix) = + self.should_truncate_line(&line, truncate_width, truncation_affix, truncate_from) + { + let result = match truncate_from { + TruncateFrom::Start => { + SharedString::from(format!("{truncation_affix}{}", &line[truncate_ix + 1..])) + } + TruncateFrom::End => { + SharedString::from(format!("{}{truncation_affix}", &line[..truncate_ix])) + } + }; + let mut runs = runs.to_vec(); + update_runs_after_truncation(&result, truncation_affix, &mut runs, truncate_from); + (result, Cow::Owned(runs)) + } else { + (line, Cow::Borrowed(runs)) + } } /// Any character in this list should be treated as a word character, @@ -182,6 +234,11 @@ impl LineWrapper { // Cyrillic for Russian, Ukrainian, etc. // https://en.wikipedia.org/wiki/Cyrillic_script_in_Unicode matches!(c, '\u{0400}'..='\u{04FF}') || + + // Vietnamese (https://vietunicode.sourceforge.net/charset/) + matches!(c, '\u{1E00}'..='\u{1EFF}') || // Latin Extended Additional + matches!(c, '\u{0300}'..='\u{036F}') || // Combining Diacritical Marks + // Some other known special characters that should be treated as word characters, // e.g. `a-b`, `var_name`, `I'm`, '@mention`, `#hashtag`, `100%`, `3.1415`, // `2^3`, `a~b`, `a=1`, `Self::new`, etc. @@ -225,15 +282,35 @@ impl LineWrapper { } } -fn update_runs_after_truncation(result: &str, ellipsis: &str, runs: &mut Vec) { +fn update_runs_after_truncation( + result: &str, + ellipsis: &str, + runs: &mut Vec, + truncate_from: TruncateFrom, +) { let mut truncate_at = result.len() - ellipsis.len(); - for (run_index, run) in runs.iter_mut().enumerate() { - if run.len <= truncate_at { - truncate_at -= run.len; - } else { - run.len = truncate_at + ellipsis.len(); - runs.truncate(run_index + 1); - break; + match truncate_from { + TruncateFrom::Start => { + for (run_index, run) in runs.iter_mut().enumerate().rev() { + if run.len <= truncate_at { + truncate_at -= run.len; + } else { + run.len = truncate_at + ellipsis.len(); + runs.splice(..run_index, std::iter::empty()); + break; + } + } + } + TruncateFrom::End => { + for (run_index, run) in runs.iter_mut().enumerate() { + if run.len <= truncate_at { + truncate_at -= run.len; + } else { + run.len = truncate_at + ellipsis.len(); + runs.truncate(run_index + 1); + break; + } + } } } } @@ -483,7 +560,7 @@ mod tests { } #[test] - fn test_truncate_line() { + fn test_truncate_line_end() { let mut wrapper = build_wrapper(); fn perform_test( @@ -494,8 +571,13 @@ mod tests { ) { let dummy_run_lens = vec![text.len()]; let dummy_runs = generate_test_runs(&dummy_run_lens); - let (result, dummy_runs) = - wrapper.truncate_line(text.into(), px(220.), ellipsis, &dummy_runs); + let (result, dummy_runs) = wrapper.truncate_line( + text.into(), + px(220.), + ellipsis, + &dummy_runs, + TruncateFrom::End, + ); assert_eq!(result, expected); assert_eq!(dummy_runs.first().unwrap().len, result.len()); } @@ -521,7 +603,50 @@ mod tests { } #[test] - fn test_truncate_multiple_runs() { + fn test_truncate_line_start() { + let mut wrapper = build_wrapper(); + + fn perform_test( + wrapper: &mut LineWrapper, + text: &'static str, + expected: &'static str, + ellipsis: &str, + ) { + let dummy_run_lens = vec![text.len()]; + let dummy_runs = generate_test_runs(&dummy_run_lens); + let (result, dummy_runs) = wrapper.truncate_line( + text.into(), + px(220.), + ellipsis, + &dummy_runs, + TruncateFrom::Start, + ); + assert_eq!(result, expected); + assert_eq!(dummy_runs.first().unwrap().len, result.len()); + } + + perform_test( + &mut wrapper, + "aaaa bbbb cccc ddddd eeee fff gg", + "cccc ddddd eeee fff gg", + "", + ); + perform_test( + &mut wrapper, + "aaaa bbbb cccc ddddd eeee fff gg", + "…ccc ddddd eeee fff gg", + "…", + ); + perform_test( + &mut wrapper, + "aaaa bbbb cccc ddddd eeee fff gg", + "......dddd eeee fff gg", + "......", + ); + } + + #[test] + fn test_truncate_multiple_runs_end() { let mut wrapper = build_wrapper(); fn perform_test( @@ -534,7 +659,7 @@ mod tests { ) { let dummy_runs = generate_test_runs(run_lens); let (result, dummy_runs) = - wrapper.truncate_line(text.into(), line_width, "…", &dummy_runs); + wrapper.truncate_line(text.into(), line_width, "…", &dummy_runs, TruncateFrom::End); assert_eq!(result, expected); for (run, result_len) in dummy_runs.iter().zip(result_run_len) { assert_eq!(run.len, *result_len); @@ -580,10 +705,75 @@ mod tests { } #[test] - fn test_update_run_after_truncation() { + fn test_truncate_multiple_runs_start() { + let mut wrapper = build_wrapper(); + + #[track_caller] + fn perform_test( + wrapper: &mut LineWrapper, + text: &'static str, + expected: &str, + run_lens: &[usize], + result_run_len: &[usize], + line_width: Pixels, + ) { + let dummy_runs = generate_test_runs(run_lens); + let (result, dummy_runs) = wrapper.truncate_line( + text.into(), + line_width, + "…", + &dummy_runs, + TruncateFrom::Start, + ); + assert_eq!(result, expected); + for (run, result_len) in dummy_runs.iter().zip(result_run_len) { + assert_eq!(run.len, *result_len); + } + } + // Case 0: Normal + // Text: abcdefghijkl + // Runs: Run0 { len: 12, ... } + // + // Truncate res: …ijkl (truncate_at = 9) + // Run res: Run0 { string: …ijkl, len: 7, ... } + perform_test(&mut wrapper, "abcdefghijkl", "…ijkl", &[12], &[7], px(50.)); + // Case 1: Drop some runs + // Text: abcdefghijkl + // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... } + // + // Truncate res: …ghijkl (truncate_at = 7) + // Runs res: Run0 { string: …gh, len: 5, ... }, Run1 { string: ijkl, len: + // 4, ... } + perform_test( + &mut wrapper, + "abcdefghijkl", + "…ghijkl", + &[4, 4, 4], + &[5, 4], + px(70.), + ); + // Case 2: Truncate at start of some run + // Text: abcdefghijkl + // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... } + // + // Truncate res: abcdefgh… (truncate_at = 3) + // Runs res: Run0 { string: …, len: 3, ... }, Run1 { string: efgh, len: + // 4, ... }, Run2 { string: ijkl, len: 4, ... } + perform_test( + &mut wrapper, + "abcdefghijkl", + "…efghijkl", + &[4, 4, 4], + &[3, 4, 4], + px(90.), + ); + } + + #[test] + fn test_update_run_after_truncation_end() { fn perform_test(result: &str, run_lens: &[usize], result_run_lens: &[usize]) { let mut dummy_runs = generate_test_runs(run_lens); - update_runs_after_truncation(result, "…", &mut dummy_runs); + update_runs_after_truncation(result, "…", &mut dummy_runs, TruncateFrom::End); for (run, result_len) in dummy_runs.iter().zip(result_run_lens) { assert_eq!(run.len, *result_len); } @@ -618,7 +808,12 @@ mod tests { #[track_caller] fn assert_word(word: &str) { for c in word.chars() { - assert!(LineWrapper::is_word_char(c), "assertion failed for '{}'", c); + assert!( + LineWrapper::is_word_char(c), + "assertion failed for '{}' (unicode 0x{:x})", + c, + c as u32 + ); } } @@ -661,6 +856,8 @@ mod tests { assert_word("ƀƁƂƃƄƅƆƇƈƉƊƋƌƍƎƏ"); // Cyrillic assert_word("АБВГДЕЖЗИЙКЛМНОП"); + // Vietnamese (https://github.com/zed-industries/zed/issues/23245) + assert_word("ThậmchíđếnkhithuachạychúngcònnhẫntâmgiếtnốtsốđôngtùchínhtrịởYênBáivàCaoBằng"); // non-word characters assert_not_word("你好"); diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs index c606409661eb022b8627fe9bc9f6c53565f5569f..8df421feb968677be0abbb642a7127871881bcf3 100644 --- a/crates/gpui/src/window.rs +++ b/crates/gpui/src/window.rs @@ -345,8 +345,8 @@ impl FocusHandle { } /// Moves the focus to the element associated with this handle. - pub fn focus(&self, window: &mut Window) { - window.focus(self) + pub fn focus(&self, window: &mut Window, cx: &mut App) { + window.focus(self, cx) } /// Obtains whether the element associated with this handle is currently focused. @@ -876,7 +876,9 @@ pub struct Window { active: Rc>, hovered: Rc>, pub(crate) needs_present: Rc>, - pub(crate) last_input_timestamp: Rc>, + /// Tracks recent input event timestamps to determine if input is arriving at a high rate. + /// Used to selectively enable VRR optimization only when input rate exceeds 60fps. + pub(crate) input_rate_tracker: Rc>, last_input_modality: InputModality, pub(crate) refreshing: bool, pub(crate) activation_observers: SubscriberSet<(), AnyObserver>, @@ -897,6 +899,51 @@ struct ModifierState { saw_keystroke: bool, } +/// Tracks input event timestamps to determine if input is arriving at a high rate. +/// Used for selective VRR (Variable Refresh Rate) optimization. +#[derive(Clone, Debug)] +pub(crate) struct InputRateTracker { + timestamps: Vec, + window: Duration, + inputs_per_second: u32, + sustain_until: Instant, + sustain_duration: Duration, +} + +impl Default for InputRateTracker { + fn default() -> Self { + Self { + timestamps: Vec::new(), + window: Duration::from_millis(100), + inputs_per_second: 60, + sustain_until: Instant::now(), + sustain_duration: Duration::from_secs(1), + } + } +} + +impl InputRateTracker { + pub fn record_input(&mut self) { + let now = Instant::now(); + self.timestamps.push(now); + self.prune_old_timestamps(now); + + let min_events = self.inputs_per_second as u128 * self.window.as_millis() / 1000; + if self.timestamps.len() as u128 >= min_events { + self.sustain_until = now + self.sustain_duration; + } + } + + pub fn is_high_rate(&self) -> bool { + Instant::now() < self.sustain_until + } + + fn prune_old_timestamps(&mut self, now: Instant) { + self.timestamps + .retain(|&t| now.duration_since(t) <= self.window); + } +} + #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub(crate) enum DrawPhase { None, @@ -1047,7 +1094,7 @@ impl Window { let hovered = Rc::new(Cell::new(platform_window.is_hovered())); let needs_present = Rc::new(Cell::new(false)); let next_frame_callbacks: Rc>> = Default::default(); - let last_input_timestamp = Rc::new(Cell::new(Instant::now())); + let input_rate_tracker = Rc::new(RefCell::new(InputRateTracker::default())); platform_window .request_decorations(window_decorations.unwrap_or(WindowDecorations::Server)); @@ -1075,7 +1122,7 @@ impl Window { let active = active.clone(); let needs_present = needs_present.clone(); let next_frame_callbacks = next_frame_callbacks.clone(); - let last_input_timestamp = last_input_timestamp.clone(); + let input_rate_tracker = input_rate_tracker.clone(); move |request_frame_options| { let next_frame_callbacks = next_frame_callbacks.take(); if !next_frame_callbacks.is_empty() { @@ -1088,12 +1135,12 @@ impl Window { .log_err(); } - // Keep presenting the current scene for 1 extra second since the - // last input to prevent the display from underclocking the refresh rate. + // Keep presenting if input was recently arriving at a high rate (>= 60fps). + // Once high-rate input is detected, we sustain presentation for 1 second + // to prevent display underclocking during active input. let needs_present = request_frame_options.require_presentation || needs_present.get() - || (active.get() - && last_input_timestamp.get().elapsed() < Duration::from_secs(1)); + || (active.get() && input_rate_tracker.borrow_mut().is_high_rate()); if invalidator.is_dirty() || request_frame_options.force_render { measure("frame duration", || { @@ -1101,7 +1148,6 @@ impl Window { .update(&mut cx, |_, window, cx| { let arena_clear_needed = window.draw(cx); window.present(); - // drop the arena elements after present to reduce latency arena_clear_needed.clear(); }) .log_err(); @@ -1299,7 +1345,7 @@ impl Window { active, hovered, needs_present, - last_input_timestamp, + input_rate_tracker, last_input_modality: InputModality::Mouse, refreshing: false, activation_observers: SubscriberSet::new(), @@ -1436,13 +1482,25 @@ impl Window { } /// Move focus to the element associated with the given [`FocusHandle`]. - pub fn focus(&mut self, handle: &FocusHandle) { + pub fn focus(&mut self, handle: &FocusHandle, cx: &mut App) { if !self.focus_enabled || self.focus == Some(handle.id) { return; } self.focus = Some(handle.id); self.clear_pending_keystrokes(); + + // Avoid re-entrant entity updates by deferring observer notifications to the end of the + // current effect cycle, and only for this window. + let window_handle = self.handle; + cx.defer(move |cx| { + window_handle + .update(cx, |_, window, cx| { + window.pending_input_changed(cx); + }) + .ok(); + }); + self.refresh(); } @@ -1463,24 +1521,24 @@ impl Window { } /// Move focus to next tab stop. - pub fn focus_next(&mut self) { + pub fn focus_next(&mut self, cx: &mut App) { if !self.focus_enabled { return; } if let Some(handle) = self.rendered_frame.tab_stops.next(self.focus.as_ref()) { - self.focus(&handle) + self.focus(&handle, cx) } } /// Move focus to previous tab stop. - pub fn focus_prev(&mut self) { + pub fn focus_prev(&mut self, cx: &mut App) { if !self.focus_enabled { return; } if let Some(handle) = self.rendered_frame.tab_stops.prev(self.focus.as_ref()) { - self.focus(&handle) + self.focus(&handle, cx) } } @@ -3679,8 +3737,6 @@ impl Window { /// Dispatch a mouse or keyboard event on the window. #[profiling::function] pub fn dispatch_event(&mut self, event: PlatformInput, cx: &mut App) -> DispatchEventResult { - self.last_input_timestamp.set(Instant::now()); - // Track whether this input was keyboard-based for focus-visible styling self.last_input_modality = match &event { PlatformInput::KeyDown(_) | PlatformInput::ModifiersChanged(_) => { @@ -3781,6 +3837,10 @@ impl Window { self.dispatch_key_event(any_key_event, cx); } + if self.invalidator.is_dirty() { + self.input_rate_tracker.borrow_mut().record_input(); + } + DispatchEventResult { propagate: cx.propagate_event, default_prevented: self.default_prevented, @@ -4020,7 +4080,7 @@ impl Window { self.dispatch_keystroke_observers(event, None, context_stack, cx); } - fn pending_input_changed(&mut self, cx: &mut App) { + pub(crate) fn pending_input_changed(&mut self, cx: &mut App) { self.pending_input_observers .clone() .retain(&(), |callback| callback(self, cx)); @@ -4438,6 +4498,13 @@ impl Window { dispatch_tree.highest_precedence_binding_for_action(action, &context_stack) } + /// Find the bindings that can follow the current input sequence for the current context stack. + pub fn possible_bindings_for_input(&self, input: &[Keystroke]) -> Vec { + self.rendered_frame + .dispatch_tree + .possible_next_bindings_for_input(input, &self.context_stack()) + } + fn context_stack_for_focus_handle( &self, focus_handle: &FocusHandle, @@ -4947,7 +5014,7 @@ impl From> for AnyWindowHandle { } /// A handle to a window with any root view type, which can be downcast to a window with a specific root view type. -#[derive(Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] pub struct AnyWindowHandle { pub(crate) id: WindowId, state_type: TypeId, diff --git a/crates/gpui/src/window/prompts.rs b/crates/gpui/src/window/prompts.rs index 63ad1668bec298a6b59d218bf7d4ca7cdce11e8c..980c6f6812405a8fbf4f8c6e24388ab4f967a94c 100644 --- a/crates/gpui/src/window/prompts.rs +++ b/crates/gpui/src/window/prompts.rs @@ -44,10 +44,10 @@ impl PromptHandle { if let Some(sender) = sender.take() { sender.send(e.0).ok(); window_handle - .update(cx, |_, window, _cx| { + .update(cx, |_, window, cx| { window.prompt.take(); if let Some(previous_focus) = &previous_focus { - window.focus(previous_focus); + window.focus(previous_focus, cx); } }) .ok(); @@ -55,7 +55,7 @@ impl PromptHandle { }) .detach(); - window.focus(&view.focus_handle(cx)); + window.focus(&view.focus_handle(cx), cx); RenderablePromptHandle { view: Box::new(view), diff --git a/crates/gpui_macros/src/derive_visual_context.rs b/crates/gpui_macros/src/derive_visual_context.rs index f2681bb29b92f31d31599ebb7201a42a482283d8..b827e753d9678efba01d3fdd77f8e66ea62b6bbd 100644 --- a/crates/gpui_macros/src/derive_visual_context.rs +++ b/crates/gpui_macros/src/derive_visual_context.rs @@ -62,7 +62,7 @@ pub fn derive_visual_context(input: TokenStream) -> TokenStream { V: gpui::Focusable, { let focus_handle = gpui::Focusable::focus_handle(entity, self.#app_variable); - self.#window_variable.focus(&focus_handle) + self.#window_variable.focus(&focus_handle, self.#app_variable) } } }; diff --git a/crates/keymap_editor/src/keymap_editor.rs b/crates/keymap_editor/src/keymap_editor.rs index 9e243d32151e3caeec2b8c51c7889d2ebe93f29b..be20feaf5f8c1feea5b08fa3a6a3b542b26ef5ce 100644 --- a/crates/keymap_editor/src/keymap_editor.rs +++ b/crates/keymap_editor/src/keymap_editor.rs @@ -911,7 +911,7 @@ impl KeymapEditor { .focus_handle(cx) .contains_focused(window, cx) { - window.focus(&self.filter_editor.focus_handle(cx)); + window.focus(&self.filter_editor.focus_handle(cx), cx); } else { self.filter_editor.update(cx, |editor, cx| { editor.select_all(&Default::default(), window, cx); @@ -948,7 +948,7 @@ impl KeymapEditor { if let Some(scroll_strategy) = scroll { self.scroll_to_item(index, scroll_strategy, cx); } - window.focus(&self.focus_handle); + window.focus(&self.focus_handle, cx); cx.notify(); } } @@ -998,7 +998,7 @@ impl KeymapEditor { }); let context_menu_handle = context_menu.focus_handle(cx); - window.defer(cx, move |window, _cx| window.focus(&context_menu_handle)); + window.defer(cx, move |window, cx| window.focus(&context_menu_handle, cx)); let subscription = cx.subscribe_in( &context_menu, window, @@ -1014,7 +1014,7 @@ impl KeymapEditor { fn dismiss_context_menu(&mut self, window: &mut Window, cx: &mut Context) { self.context_menu.take(); - window.focus(&self.focus_handle); + window.focus(&self.focus_handle, cx); cx.notify(); } @@ -1230,7 +1230,7 @@ impl KeymapEditor { window, cx, ); - window.focus(&modal.focus_handle(cx)); + window.focus(&modal.focus_handle(cx), cx); modal }); }) @@ -1338,7 +1338,7 @@ impl KeymapEditor { editor.stop_recording(&StopRecording, window, cx); editor.clear_keystrokes(&ClearKeystrokes, window, cx); }); - window.focus(&self.filter_editor.focus_handle(cx)); + window.focus(&self.filter_editor.focus_handle(cx), cx); } } } @@ -2698,32 +2698,32 @@ impl KeybindingEditorModalFocusState { .map(|i| i as i32) } - fn focus_index(&self, mut index: i32, window: &mut Window) { + fn focus_index(&self, mut index: i32, window: &mut Window, cx: &mut App) { if index < 0 { index = self.handles.len() as i32 - 1; } if index >= self.handles.len() as i32 { index = 0; } - window.focus(&self.handles[index as usize]); + window.focus(&self.handles[index as usize], cx); } - fn focus_next(&self, window: &mut Window, cx: &App) { + fn focus_next(&self, window: &mut Window, cx: &mut App) { let index_to_focus = if let Some(index) = self.focused_index(window, cx) { index + 1 } else { 0 }; - self.focus_index(index_to_focus, window); + self.focus_index(index_to_focus, window, cx); } - fn focus_previous(&self, window: &mut Window, cx: &App) { + fn focus_previous(&self, window: &mut Window, cx: &mut App) { let index_to_focus = if let Some(index) = self.focused_index(window, cx) { index - 1 } else { self.handles.len() as i32 - 1 }; - self.focus_index(index_to_focus, window); + self.focus_index(index_to_focus, window, cx); } } @@ -2757,7 +2757,7 @@ impl ActionArgumentsEditor { ) -> Self { let focus_handle = cx.focus_handle(); cx.on_focus_in(&focus_handle, window, |this, window, cx| { - this.editor.focus_handle(cx).focus(window); + this.editor.focus_handle(cx).focus(window, cx); }) .detach(); let editor = cx.new(|cx| { @@ -2810,7 +2810,7 @@ impl ActionArgumentsEditor { this.update_in(cx, |this, window, cx| { if this.editor.focus_handle(cx).is_focused(window) { - editor.focus_handle(cx).focus(window); + editor.focus_handle(cx).focus(window, cx); } this.editor = editor; this.backup_temp_dir = backup_temp_dir; diff --git a/crates/keymap_editor/src/ui_components/keystroke_input.rs b/crates/keymap_editor/src/ui_components/keystroke_input.rs index 6936de784f9d5c16b218d0952c41d6336299a0f9..496a8ae7e6359bc169845542a0f05800008a4786 100644 --- a/crates/keymap_editor/src/ui_components/keystroke_input.rs +++ b/crates/keymap_editor/src/ui_components/keystroke_input.rs @@ -388,7 +388,7 @@ impl KeystrokeInput { window: &mut Window, cx: &mut Context, ) { - window.focus(&self.inner_focus_handle); + window.focus(&self.inner_focus_handle, cx); self.clear_keystrokes(&ClearKeystrokes, window, cx); self.previous_modifiers = window.modifiers(); #[cfg(test)] @@ -407,7 +407,7 @@ impl KeystrokeInput { if !self.is_recording(window) { return; } - window.focus(&self.outer_focus_handle); + window.focus(&self.outer_focus_handle, cx); if let Some(close_keystrokes_start) = self.close_keystrokes_start.take() && close_keystrokes_start < self.keystrokes.len() { diff --git a/crates/language/Cargo.toml b/crates/language/Cargo.toml index 3ba93476d2a9fa5371b9d146cfc0c5833a748842..06d41e729bfabbf4f7e050409d2675dd909941d6 100644 --- a/crates/language/Cargo.toml +++ b/crates/language/Cargo.toml @@ -32,6 +32,7 @@ async-trait.workspace = true clock.workspace = true collections.workspace = true ec4rs.workspace = true +encoding_rs.workspace = true fs.workspace = true futures.workspace = true fuzzy.workspace = true diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index 39003773f83718c6c61d4cfda55b9528f7c6eb2a..99e0c8d4ebdad709eea0e9ab6dbdf9d889d54ec5 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -25,6 +25,7 @@ use anyhow::{Context as _, Result}; use clock::Lamport; pub use clock::ReplicaId; use collections::{HashMap, HashSet}; +use encoding_rs::Encoding; use fs::MTime; use futures::channel::oneshot; use gpui::{ @@ -131,6 +132,8 @@ pub struct Buffer { change_bits: Vec>>, _subscriptions: Vec, tree_sitter_data: Arc, + encoding: &'static Encoding, + has_bom: bool, } #[derive(Debug)] @@ -1100,6 +1103,8 @@ impl Buffer { has_conflict: false, change_bits: Default::default(), _subscriptions: Vec::new(), + encoding: encoding_rs::UTF_8, + has_bom: false, } } @@ -1383,6 +1388,26 @@ impl Buffer { self.saved_mtime } + /// Returns the character encoding of the buffer's file. + pub fn encoding(&self) -> &'static Encoding { + self.encoding + } + + /// Sets the character encoding of the buffer. + pub fn set_encoding(&mut self, encoding: &'static Encoding) { + self.encoding = encoding; + } + + /// Returns whether the buffer has a Byte Order Mark. + pub fn has_bom(&self) -> bool { + self.has_bom + } + + /// Sets whether the buffer has a Byte Order Mark. + pub fn set_has_bom(&mut self, has_bom: bool) { + self.has_bom = has_bom; + } + /// Assign a language to the buffer. pub fn set_language_async(&mut self, language: Option>, cx: &mut Context) { self.set_language_(language, cfg!(any(test, feature = "test-support")), cx); @@ -1776,9 +1801,7 @@ impl Buffer { self.syntax_map.lock().did_parse(syntax_snapshot); self.request_autoindent(cx); self.parse_status.0.send(ParseStatus::Idle).unwrap(); - if self.text.version() != *self.tree_sitter_data.version() { - self.invalidate_tree_sitter_data(self.text.snapshot()); - } + self.invalidate_tree_sitter_data(self.text.snapshot()); cx.emit(BufferEvent::Reparsed); cx.notify(); } diff --git a/crates/language/src/buffer_tests.rs b/crates/language/src/buffer_tests.rs index 54e2ef4065460547f4a3f86db7d3a3986dff65eb..2c2d93c8239f0f3fcb1de0956de2d3400f13e96b 100644 --- a/crates/language/src/buffer_tests.rs +++ b/crates/language/src/buffer_tests.rs @@ -1141,6 +1141,104 @@ fn test_text_objects(cx: &mut App) { ) } +#[gpui::test] +fn test_text_objects_with_has_parent_predicate(cx: &mut App) { + use std::borrow::Cow; + + // Create a language with a custom text_objects query that uses #has-parent? + // This query only matches closure_expression when it's inside a call_expression + let language = Language::new( + LanguageConfig { + name: "Rust".into(), + matcher: LanguageMatcher { + path_suffixes: vec!["rs".to_string()], + ..Default::default() + }, + ..Default::default() + }, + Some(tree_sitter_rust::LANGUAGE.into()), + ) + .with_queries(LanguageQueries { + text_objects: Some(Cow::from(indoc! {r#" + ; Only match closures that are arguments to function calls + (closure_expression) @function.around + (#has-parent? @function.around arguments) + "#})), + ..Default::default() + }) + .expect("Could not parse queries"); + + let (text, ranges) = marked_text_ranges( + indoc! {r#" + fn main() { + let standalone = |x| x + 1; + let result = foo(|y| y * ˇ2); + }"# + }, + false, + ); + + let buffer = cx.new(|cx| Buffer::local(text.clone(), cx).with_language(Arc::new(language), cx)); + let snapshot = buffer.update(cx, |buffer, _| buffer.snapshot()); + + let matches = snapshot + .text_object_ranges(ranges[0].clone(), TreeSitterOptions::default()) + .map(|(range, text_object)| (&text[range], text_object)) + .collect::>(); + + // Should only match the closure inside foo(), not the standalone closure + assert_eq!(matches, &[("|y| y * 2", TextObject::AroundFunction),]); +} + +#[gpui::test] +fn test_text_objects_with_not_has_parent_predicate(cx: &mut App) { + use std::borrow::Cow; + + // Create a language with a custom text_objects query that uses #not-has-parent? + // This query only matches closure_expression when it's NOT inside a call_expression + let language = Language::new( + LanguageConfig { + name: "Rust".into(), + matcher: LanguageMatcher { + path_suffixes: vec!["rs".to_string()], + ..Default::default() + }, + ..Default::default() + }, + Some(tree_sitter_rust::LANGUAGE.into()), + ) + .with_queries(LanguageQueries { + text_objects: Some(Cow::from(indoc! {r#" + ; Only match closures that are NOT arguments to function calls + (closure_expression) @function.around + (#not-has-parent? @function.around arguments) + "#})), + ..Default::default() + }) + .expect("Could not parse queries"); + + let (text, ranges) = marked_text_ranges( + indoc! {r#" + fn main() { + let standalone = |x| x +ˇ 1; + let result = foo(|y| y * 2); + }"# + }, + false, + ); + + let buffer = cx.new(|cx| Buffer::local(text.clone(), cx).with_language(Arc::new(language), cx)); + let snapshot = buffer.update(cx, |buffer, _| buffer.snapshot()); + + let matches = snapshot + .text_object_ranges(ranges[0].clone(), TreeSitterOptions::default()) + .map(|(range, text_object)| (&text[range], text_object)) + .collect::>(); + + // Should only match the standalone closure, not the one inside foo() + assert_eq!(matches, &[("|x| x + 1", TextObject::AroundFunction),]); +} + #[gpui::test] fn test_enclosing_bracket_ranges(cx: &mut App) { #[track_caller] diff --git a/crates/language/src/language.rs b/crates/language/src/language.rs index eceb68f3e578fda97af292fee395a8ac4f0829c9..290cad4e4497015ef63f79e58a0dacf231168c9f 100644 --- a/crates/language/src/language.rs +++ b/crates/language/src/language.rs @@ -330,6 +330,10 @@ impl CachedLspAdapter { .cloned() .unwrap_or_else(|| language_name.lsp_id()) } + + pub fn process_prompt_response(&self, context: &PromptResponseContext, cx: &mut AsyncApp) { + self.adapter.process_prompt_response(context, cx) + } } /// [`LspAdapterDelegate`] allows [`LspAdapter]` implementations to interface with the application @@ -355,6 +359,17 @@ pub trait LspAdapterDelegate: Send + Sync { async fn try_exec(&self, binary: LanguageServerBinary) -> Result<()>; } +/// Context provided to LSP adapters when a user responds to a ShowMessageRequest prompt. +/// This allows adapters to intercept preference selections (like "Always" or "Never") +/// and potentially persist them to Zed's settings. +#[derive(Debug, Clone)] +pub struct PromptResponseContext { + /// The original message shown to the user + pub message: String, + /// The action (button) the user selected + pub selected_action: lsp::MessageActionItem, +} + #[async_trait(?Send)] pub trait LspAdapter: 'static + Send + Sync + DynLspInstaller { fn name(&self) -> LanguageServerName; @@ -511,6 +526,11 @@ pub trait LspAdapter: 'static + Send + Sync + DynLspInstaller { fn is_extension(&self) -> bool { false } + + /// Called when a user responds to a ShowMessageRequest from this language server. + /// This allows adapters to intercept preference selections (like "Always" or "Never") + /// for settings that should be persisted to Zed's settings file. + fn process_prompt_response(&self, _context: &PromptResponseContext, _cx: &mut AsyncApp) {} } pub trait LspInstaller { @@ -807,6 +827,15 @@ pub struct LanguageConfig { /// Delimiters and configuration for recognizing and formatting documentation comments. #[serde(default, alias = "documentation")] pub documentation_comment: Option, + /// List markers that are inserted unchanged on newline (e.g., `- `, `* `, `+ `). + #[serde(default)] + pub unordered_list: Vec>, + /// Configuration for ordered lists with auto-incrementing numbers on newline (e.g., `1. ` becomes `2. `). + #[serde(default)] + pub ordered_list: Vec, + /// Configuration for task lists where multiple markers map to a single continuation prefix (e.g., `- [x] ` continues as `- [ ] `). + #[serde(default)] + pub task_list: Option, /// A list of additional regex patterns that should be treated as prefixes /// for creating boundaries during rewrapping, ensuring content from one /// prefixed section doesn't merge with another (e.g., markdown list items). @@ -878,6 +907,24 @@ pub struct DecreaseIndentConfig { pub valid_after: Vec, } +/// Configuration for continuing ordered lists with auto-incrementing numbers. +#[derive(Clone, Debug, Deserialize, JsonSchema)] +pub struct OrderedListConfig { + /// A regex pattern with a capture group for the number portion (e.g., `(\\d+)\\. `). + pub pattern: String, + /// A format string where `{1}` is replaced with the incremented number (e.g., `{1}. `). + pub format: String, +} + +/// Configuration for continuing task lists on newline. +#[derive(Clone, Debug, Deserialize, JsonSchema)] +pub struct TaskListConfig { + /// The list markers to match (e.g., `- [ ] `, `- [x] `). + pub prefixes: Vec>, + /// The marker to insert when continuing the list on a new line (e.g., `- [ ] `). + pub continuation: Arc, +} + #[derive(Clone, Debug, Serialize, Deserialize, Default, JsonSchema)] pub struct LanguageMatcher { /// Given a list of `LanguageConfig`'s, the language of a file can be determined based on the path extension matching any of the `path_suffixes`. @@ -1048,6 +1095,9 @@ impl Default for LanguageConfig { line_comments: Default::default(), block_comment: Default::default(), documentation_comment: Default::default(), + unordered_list: Default::default(), + ordered_list: Default::default(), + task_list: Default::default(), rewrap_prefixes: Default::default(), scope_opt_in_language_servers: Default::default(), overrides: Default::default(), @@ -2133,6 +2183,21 @@ impl LanguageScope { self.language.config.documentation_comment.as_ref() } + /// Returns list markers that are inserted unchanged on newline (e.g., `- `, `* `, `+ `). + pub fn unordered_list(&self) -> &[Arc] { + &self.language.config.unordered_list + } + + /// Returns configuration for ordered lists with auto-incrementing numbers (e.g., `1. ` becomes `2. `). + pub fn ordered_list(&self) -> &[OrderedListConfig] { + &self.language.config.ordered_list + } + + /// Returns configuration for task list continuation, if any (e.g., `- [x] ` continues as `- [ ] `). + pub fn task_list(&self) -> Option<&TaskListConfig> { + self.language.config.task_list.as_ref() + } + /// Returns additional regex patterns that act as prefix markers for creating /// boundaries during rewrapping. /// diff --git a/crates/language/src/language_settings.rs b/crates/language/src/language_settings.rs index fccaa545b79c1f24589889df8fcd163fbc5b6c7d..205f2431c6d9deeaa7661b583caa516bdc77ae79 100644 --- a/crates/language/src/language_settings.rs +++ b/crates/language/src/language_settings.rs @@ -122,6 +122,10 @@ pub struct LanguageSettings { pub whitespace_map: WhitespaceMap, /// Whether to start a new line with a comment when a previous line is a comment as well. pub extend_comment_on_newline: bool, + /// Whether to continue markdown lists when pressing enter. + pub extend_list_on_newline: bool, + /// Whether to indent list items when pressing tab after a list marker. + pub indent_list_on_tab: bool, /// Inlay hint related settings. pub inlay_hints: InlayHintSettings, /// Whether to automatically close brackets. @@ -567,6 +571,8 @@ impl settings::Settings for AllLanguageSettings { tab: SharedString::new(whitespace_map.tab.unwrap().to_string()), }, extend_comment_on_newline: settings.extend_comment_on_newline.unwrap(), + extend_list_on_newline: settings.extend_list_on_newline.unwrap(), + indent_list_on_tab: settings.indent_list_on_tab.unwrap(), inlay_hints: InlayHintSettings { enabled: inlay_hints.enabled.unwrap(), show_value_hints: inlay_hints.show_value_hints.unwrap(), diff --git a/crates/language/src/syntax_map.rs b/crates/language/src/syntax_map.rs index 77e90c4ca89d0b6e5b8cb0a604175ec9a97e719e..db4ab4f459c35a98752bef1eb5be558084b5c906 100644 --- a/crates/language/src/syntax_map.rs +++ b/crates/language/src/syntax_map.rs @@ -19,7 +19,10 @@ use std::{ use streaming_iterator::StreamingIterator; use sum_tree::{Bias, Dimensions, SeekTarget, SumTree}; use text::{Anchor, BufferSnapshot, OffsetRangeExt, Point, Rope, ToOffset, ToPoint}; -use tree_sitter::{Node, Query, QueryCapture, QueryCaptures, QueryCursor, QueryMatches, Tree}; +use tree_sitter::{ + Node, Query, QueryCapture, QueryCaptures, QueryCursor, QueryMatch, QueryMatches, + QueryPredicateArg, Tree, +}; pub const MAX_BYTES_TO_QUERY: usize = 16 * 1024; @@ -82,6 +85,7 @@ struct SyntaxMapMatchesLayer<'a> { next_captures: Vec>, has_next: bool, matches: QueryMatches<'a, 'a, TextProvider<'a>, &'a [u8]>, + query: &'a Query, grammar_index: usize, _query_cursor: QueryCursorHandle, } @@ -1163,6 +1167,7 @@ impl<'a> SyntaxMapMatches<'a> { depth: layer.depth, grammar_index, matches, + query, next_pattern_index: 0, next_captures: Vec::new(), has_next: false, @@ -1260,13 +1265,20 @@ impl SyntaxMapCapturesLayer<'_> { impl SyntaxMapMatchesLayer<'_> { fn advance(&mut self) { - if let Some(mat) = self.matches.next() { - self.next_captures.clear(); - self.next_captures.extend_from_slice(mat.captures); - self.next_pattern_index = mat.pattern_index; - self.has_next = true; - } else { - self.has_next = false; + loop { + if let Some(mat) = self.matches.next() { + if !satisfies_custom_predicates(self.query, mat) { + continue; + } + self.next_captures.clear(); + self.next_captures.extend_from_slice(mat.captures); + self.next_pattern_index = mat.pattern_index; + self.has_next = true; + return; + } else { + self.has_next = false; + return; + } } } @@ -1295,6 +1307,39 @@ impl<'a> Iterator for SyntaxMapCaptures<'a> { } } +fn satisfies_custom_predicates(query: &Query, mat: &QueryMatch) -> bool { + for predicate in query.general_predicates(mat.pattern_index) { + let satisfied = match predicate.operator.as_ref() { + "has-parent?" => has_parent(&predicate.args, mat), + "not-has-parent?" => !has_parent(&predicate.args, mat), + _ => true, + }; + if !satisfied { + return false; + } + } + true +} + +fn has_parent(args: &[QueryPredicateArg], mat: &QueryMatch) -> bool { + let ( + Some(QueryPredicateArg::Capture(capture_ix)), + Some(QueryPredicateArg::String(parent_kind)), + ) = (args.first(), args.get(1)) + else { + return false; + }; + + let Some(capture) = mat.captures.iter().find(|c| c.index == *capture_ix) else { + return false; + }; + + capture + .node + .parent() + .is_some_and(|p| p.kind() == parent_kind.as_ref()) +} + fn join_ranges( a: impl Iterator>, b: impl Iterator>, diff --git a/crates/language/src/toolchain.rs b/crates/language/src/toolchain.rs index 5717ffb5143e38bce736c354b43febc86e321f32..815ece30a1ed46ae65ec4af2ba64501ff3489718 100644 --- a/crates/language/src/toolchain.rs +++ b/crates/language/src/toolchain.rs @@ -4,7 +4,10 @@ //! which is a set of tools used to interact with the projects written in said language. //! For example, a Python project can have an associated virtual environment; a Rust project can have a toolchain override. -use std::{path::PathBuf, sync::Arc}; +use std::{ + path::{Path, PathBuf}, + sync::Arc, +}; use async_trait::async_trait; use collections::HashMap; @@ -36,7 +39,7 @@ pub struct Toolchain { /// - Only in the subproject they're currently in. #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] pub enum ToolchainScope { - Subproject(WorktreeId, Arc), + Subproject(Arc, Arc), Project, /// Available in all projects on this box. It wouldn't make sense to show suggestions across machines. Global, diff --git a/crates/language_model/src/language_model.rs b/crates/language_model/src/language_model.rs index 09d44b5b408324936af00a2a5e4f1deb4f351434..56a970404419ec6042c463d26c2844eb0904f829 100644 --- a/crates/language_model/src/language_model.rs +++ b/crates/language_model/src/language_model.rs @@ -797,11 +797,26 @@ pub enum AuthenticateError { Other(#[from] anyhow::Error), } +/// Either a built-in icon name or a path to an external SVG. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum IconOrSvg { + /// A built-in icon from Zed's icon set. + Icon(IconName), + /// Path to a custom SVG icon file. + Svg(SharedString), +} + +impl Default for IconOrSvg { + fn default() -> Self { + Self::Icon(IconName::ZedAssistant) + } +} + pub trait LanguageModelProvider: 'static { fn id(&self) -> LanguageModelProviderId; fn name(&self) -> LanguageModelProviderName; - fn icon(&self) -> IconName { - IconName::ZedAssistant + fn icon(&self) -> IconOrSvg { + IconOrSvg::default() } fn default_model(&self, cx: &App) -> Option>; fn default_fast_model(&self, cx: &App) -> Option>; @@ -820,7 +835,7 @@ pub trait LanguageModelProvider: 'static { fn reset_credentials(&self, cx: &mut App) -> Task>; } -#[derive(Default, Clone)] +#[derive(Default, Clone, PartialEq, Eq)] pub enum ConfigurationViewTargetAgent { #[default] ZedAgent, diff --git a/crates/language_model/src/registry.rs b/crates/language_model/src/registry.rs index 27b8309810962981d3c0ec78e6e67dfdfba122bf..cf7718f7b102010cc0c8a981a0425583436176b7 100644 --- a/crates/language_model/src/registry.rs +++ b/crates/language_model/src/registry.rs @@ -2,12 +2,16 @@ use crate::{ LanguageModel, LanguageModelId, LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderState, }; -use collections::BTreeMap; +use collections::{BTreeMap, HashSet}; use gpui::{App, Context, Entity, EventEmitter, Global, prelude::*}; use std::{str::FromStr, sync::Arc}; use thiserror::Error; use util::maybe; +/// Function type for checking if a built-in provider should be hidden. +/// Returns Some(extension_id) if the provider should be hidden when that extension is installed. +pub type BuiltinProviderHidingFn = Box Option<&'static str> + Send + Sync>; + pub fn init(cx: &mut App) { let registry = cx.new(|_cx| LanguageModelRegistry::default()); cx.set_global(GlobalLanguageModelRegistry(registry)); @@ -48,6 +52,11 @@ pub struct LanguageModelRegistry { thread_summary_model: Option, providers: BTreeMap>, inline_alternatives: Vec>, + /// Set of installed extension IDs that provide language models. + /// Used to determine which built-in providers should be hidden. + installed_llm_extension_ids: HashSet>, + /// Function to check if a built-in provider should be hidden by an extension. + builtin_provider_hiding_fn: Option, } #[derive(Debug)] @@ -104,6 +113,8 @@ pub enum Event { ProviderStateChanged(LanguageModelProviderId), AddedProvider(LanguageModelProviderId), RemovedProvider(LanguageModelProviderId), + /// Emitted when provider visibility changes due to extension install/uninstall. + ProvidersChanged, } impl EventEmitter for LanguageModelRegistry {} @@ -183,6 +194,60 @@ impl LanguageModelRegistry { providers } + /// Returns providers, filtering out hidden built-in providers. + pub fn visible_providers(&self) -> Vec> { + self.providers() + .into_iter() + .filter(|p| !self.should_hide_provider(&p.id())) + .collect() + } + + /// Sets the function used to check if a built-in provider should be hidden. + pub fn set_builtin_provider_hiding_fn(&mut self, hiding_fn: BuiltinProviderHidingFn) { + self.builtin_provider_hiding_fn = Some(hiding_fn); + } + + /// Called when an extension is installed/loaded. + /// If the extension provides language models, track it so we can hide the corresponding built-in. + pub fn extension_installed(&mut self, extension_id: Arc, cx: &mut Context) { + if self.installed_llm_extension_ids.insert(extension_id) { + cx.emit(Event::ProvidersChanged); + cx.notify(); + } + } + + /// Called when an extension is uninstalled/unloaded. + pub fn extension_uninstalled(&mut self, extension_id: &str, cx: &mut Context) { + if self.installed_llm_extension_ids.remove(extension_id) { + cx.emit(Event::ProvidersChanged); + cx.notify(); + } + } + + /// Sync the set of installed LLM extension IDs. + pub fn sync_installed_llm_extensions( + &mut self, + extension_ids: HashSet>, + cx: &mut Context, + ) { + if extension_ids != self.installed_llm_extension_ids { + self.installed_llm_extension_ids = extension_ids; + cx.emit(Event::ProvidersChanged); + cx.notify(); + } + } + + /// Returns true if a provider should be hidden from the UI. + /// Built-in providers are hidden when their corresponding extension is installed. + pub fn should_hide_provider(&self, provider_id: &LanguageModelProviderId) -> bool { + if let Some(ref hiding_fn) = self.builtin_provider_hiding_fn { + if let Some(extension_id) = hiding_fn(&provider_id.0) { + return self.installed_llm_extension_ids.contains(extension_id); + } + } + false + } + pub fn configuration_error( &self, model: Option, @@ -416,4 +481,132 @@ mod tests { let providers = registry.read(cx).providers(); assert!(providers.is_empty()); } + + #[gpui::test] + fn test_provider_hiding_on_extension_install(cx: &mut App) { + let registry = cx.new(|_| LanguageModelRegistry::default()); + + let provider = Arc::new(FakeLanguageModelProvider::default()); + let provider_id = provider.id(); + + registry.update(cx, |registry, cx| { + registry.register_provider(provider.clone(), cx); + + registry.set_builtin_provider_hiding_fn(Box::new(|id| { + if id == "fake" { + Some("fake-extension") + } else { + None + } + })); + }); + + let visible = registry.read(cx).visible_providers(); + assert_eq!(visible.len(), 1); + assert_eq!(visible[0].id(), provider_id); + + registry.update(cx, |registry, cx| { + registry.extension_installed("fake-extension".into(), cx); + }); + + let visible = registry.read(cx).visible_providers(); + assert!(visible.is_empty()); + + let all = registry.read(cx).providers(); + assert_eq!(all.len(), 1); + } + + #[gpui::test] + fn test_provider_unhiding_on_extension_uninstall(cx: &mut App) { + let registry = cx.new(|_| LanguageModelRegistry::default()); + + let provider = Arc::new(FakeLanguageModelProvider::default()); + let provider_id = provider.id(); + + registry.update(cx, |registry, cx| { + registry.register_provider(provider.clone(), cx); + + registry.set_builtin_provider_hiding_fn(Box::new(|id| { + if id == "fake" { + Some("fake-extension") + } else { + None + } + })); + + registry.extension_installed("fake-extension".into(), cx); + }); + + let visible = registry.read(cx).visible_providers(); + assert!(visible.is_empty()); + + registry.update(cx, |registry, cx| { + registry.extension_uninstalled("fake-extension", cx); + }); + + let visible = registry.read(cx).visible_providers(); + assert_eq!(visible.len(), 1); + assert_eq!(visible[0].id(), provider_id); + } + + #[gpui::test] + fn test_should_hide_provider(cx: &mut App) { + let registry = cx.new(|_| LanguageModelRegistry::default()); + + registry.update(cx, |registry, cx| { + registry.set_builtin_provider_hiding_fn(Box::new(|id| { + if id == "anthropic" { + Some("anthropic") + } else if id == "openai" { + Some("openai") + } else { + None + } + })); + + registry.extension_installed("anthropic".into(), cx); + }); + + let registry_read = registry.read(cx); + + assert!(registry_read.should_hide_provider(&LanguageModelProviderId("anthropic".into()))); + + assert!(!registry_read.should_hide_provider(&LanguageModelProviderId("openai".into()))); + + assert!(!registry_read.should_hide_provider(&LanguageModelProviderId("unknown".into()))); + } + + #[gpui::test] + fn test_sync_installed_llm_extensions(cx: &mut App) { + let registry = cx.new(|_| LanguageModelRegistry::default()); + + let provider = Arc::new(FakeLanguageModelProvider::default()); + + registry.update(cx, |registry, cx| { + registry.register_provider(provider.clone(), cx); + + registry.set_builtin_provider_hiding_fn(Box::new(|id| { + if id == "fake" { + Some("fake-extension") + } else { + None + } + })); + }); + + let mut extension_ids = HashSet::default(); + extension_ids.insert(Arc::from("fake-extension")); + + registry.update(cx, |registry, cx| { + registry.sync_installed_llm_extensions(extension_ids, cx); + }); + + assert!(registry.read(cx).visible_providers().is_empty()); + + registry.update(cx, |registry, cx| { + registry.sync_installed_llm_extensions(HashSet::default(), cx); + }); + + assert_eq!(registry.read(cx).visible_providers().len(), 1); + } } diff --git a/crates/language_models/Cargo.toml b/crates/language_models/Cargo.toml index 5531e698ab7fccae736e800f38b16e35bcd35ac4..1bec5d94d2bb35f91305c6c77a9e85ed8579e1af 100644 --- a/crates/language_models/Cargo.toml +++ b/crates/language_models/Cargo.toml @@ -28,6 +28,8 @@ convert_case.workspace = true copilot.workspace = true credentials_provider.workspace = true deepseek = { workspace = true, features = ["schemars"] } +extension.workspace = true +extension_host.workspace = true fs.workspace = true futures.workspace = true google_ai = { workspace = true, features = ["schemars"] } diff --git a/crates/language_models/src/extension.rs b/crates/language_models/src/extension.rs new file mode 100644 index 0000000000000000000000000000000000000000..e0b46ab5e1d667fb61449a654769ecf7c221e720 --- /dev/null +++ b/crates/language_models/src/extension.rs @@ -0,0 +1,67 @@ +use collections::HashMap; +use extension::{ + ExtensionHostProxy, ExtensionLanguageModelProviderProxy, LanguageModelProviderRegistration, +}; +use gpui::{App, Entity}; +use language_model::{LanguageModelProviderId, LanguageModelRegistry}; +use std::sync::{Arc, LazyLock}; + +/// Maps built-in provider IDs to their corresponding extension IDs. +/// When an extension with this ID is installed, the built-in provider should be hidden. +static BUILTIN_TO_EXTENSION_MAP: LazyLock> = + LazyLock::new(|| { + let mut map = HashMap::default(); + map.insert("anthropic", "anthropic"); + map.insert("openai", "openai"); + map.insert("google", "google-ai"); + map.insert("openrouter", "openrouter"); + map.insert("copilot_chat", "copilot-chat"); + map + }); + +/// Returns the extension ID that should hide the given built-in provider. +pub fn extension_for_builtin_provider(provider_id: &str) -> Option<&'static str> { + BUILTIN_TO_EXTENSION_MAP.get(provider_id).copied() +} + +/// Proxy that registers extension language model providers with the LanguageModelRegistry. +pub struct LanguageModelProviderRegistryProxy { + registry: Entity, +} + +impl LanguageModelProviderRegistryProxy { + pub fn new(registry: Entity) -> Self { + Self { registry } + } +} + +impl ExtensionLanguageModelProviderProxy for LanguageModelProviderRegistryProxy { + fn register_language_model_provider( + &self, + _provider_id: Arc, + register_fn: LanguageModelProviderRegistration, + cx: &mut App, + ) { + register_fn(cx); + } + + fn unregister_language_model_provider(&self, provider_id: Arc, cx: &mut App) { + self.registry.update(cx, |registry, cx| { + registry.unregister_provider(LanguageModelProviderId::from(provider_id), cx); + }); + } +} + +/// Initialize the extension language model provider proxy. +/// This must be called BEFORE extension_host::init to ensure the proxy is available +/// when extensions try to register their language model providers. +pub fn init_proxy(cx: &mut App) { + let proxy = ExtensionHostProxy::default_global(cx); + let registry = LanguageModelRegistry::global(cx); + + registry.update(cx, |registry, _cx| { + registry.set_builtin_provider_hiding_fn(Box::new(extension_for_builtin_provider)); + }); + + proxy.register_language_model_provider_proxy(LanguageModelProviderRegistryProxy::new(registry)); +} diff --git a/crates/language_models/src/language_models.rs b/crates/language_models/src/language_models.rs index 1038f5e233e0a5970b0e8bd969a65f6f0e2a7550..37d4ca5ddd4e5c1e7a0202c88c012d18b018cd4f 100644 --- a/crates/language_models/src/language_models.rs +++ b/crates/language_models/src/language_models.rs @@ -7,9 +7,12 @@ use gpui::{App, Context, Entity}; use language_model::{LanguageModelProviderId, LanguageModelRegistry}; use provider::deepseek::DeepSeekLanguageModelProvider; +pub mod extension; pub mod provider; mod settings; +pub use crate::extension::init_proxy as init_extension_proxy; + use crate::provider::anthropic::AnthropicLanguageModelProvider; use crate::provider::bedrock::BedrockLanguageModelProvider; use crate::provider::cloud::CloudLanguageModelProvider; @@ -31,6 +34,56 @@ pub fn init(user_store: Entity, client: Arc, cx: &mut App) { register_language_model_providers(registry, user_store, client.clone(), cx); }); + // Subscribe to extension store events to track LLM extension installations + if let Some(extension_store) = extension_host::ExtensionStore::try_global(cx) { + cx.subscribe(&extension_store, { + let registry = registry.clone(); + move |extension_store, event, cx| match event { + extension_host::Event::ExtensionInstalled(extension_id) => { + if let Some(manifest) = extension_store + .read(cx) + .extension_manifest_for_id(extension_id) + { + if !manifest.language_model_providers.is_empty() { + registry.update(cx, |registry, cx| { + registry.extension_installed(extension_id.clone(), cx); + }); + } + } + } + extension_host::Event::ExtensionUninstalled(extension_id) => { + registry.update(cx, |registry, cx| { + registry.extension_uninstalled(extension_id, cx); + }); + } + extension_host::Event::ExtensionsUpdated => { + let mut new_ids = HashSet::default(); + for (extension_id, entry) in extension_store.read(cx).installed_extensions() { + if !entry.manifest.language_model_providers.is_empty() { + new_ids.insert(extension_id.clone()); + } + } + registry.update(cx, |registry, cx| { + registry.sync_installed_llm_extensions(new_ids, cx); + }); + } + _ => {} + } + }) + .detach(); + + // Initialize with currently installed extensions + registry.update(cx, |registry, cx| { + let mut initial_ids = HashSet::default(); + for (extension_id, entry) in extension_store.read(cx).installed_extensions() { + if !entry.manifest.language_model_providers.is_empty() { + initial_ids.insert(extension_id.clone()); + } + } + registry.sync_installed_llm_extensions(initial_ids, cx); + }); + } + let mut openai_compatible_providers = AllLanguageModelSettings::get_global(cx) .openai_compatible .keys() diff --git a/crates/language_models/src/provider/anthropic.rs b/crates/language_models/src/provider/anthropic.rs index d8c972399c33922386bfba4236e1369d03d338dc..598834f85c496cd54ddd956089715cac64420202 100644 --- a/crates/language_models/src/provider/anthropic.rs +++ b/crates/language_models/src/provider/anthropic.rs @@ -8,7 +8,7 @@ use futures::{FutureExt, Stream, StreamExt, future, future::BoxFuture, stream::B use gpui::{AnyView, App, AsyncApp, Context, Entity, Task}; use http_client::HttpClient; use language_model::{ - ApiKeyState, AuthenticateError, ConfigurationViewTargetAgent, EnvVar, LanguageModel, + ApiKeyState, AuthenticateError, ConfigurationViewTargetAgent, EnvVar, IconOrSvg, LanguageModel, LanguageModelCacheConfiguration, LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest, @@ -125,8 +125,8 @@ impl LanguageModelProvider for AnthropicLanguageModelProvider { PROVIDER_NAME } - fn icon(&self) -> IconName { - IconName::AiAnthropic + fn icon(&self) -> IconOrSvg { + IconOrSvg::Icon(IconName::AiAnthropic) } fn default_model(&self, _cx: &App) -> Option> { diff --git a/crates/language_models/src/provider/bedrock.rs b/crates/language_models/src/provider/bedrock.rs index b85a038bb235d97bd9de8614f19764ecabf7bbfe..62237fbf376a0739fd2518bda44f51149b3457df 100644 --- a/crates/language_models/src/provider/bedrock.rs +++ b/crates/language_models/src/provider/bedrock.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use anyhow::{Context as _, Result, anyhow}; use aws_config::stalled_stream_protection::StalledStreamProtectionConfig; use aws_config::{BehaviorVersion, Region}; -use aws_credential_types::Credentials; +use aws_credential_types::{Credentials, Token}; use aws_http_client::AwsHttpClient; use bedrock::bedrock_client::Client as BedrockClient; use bedrock::bedrock_client::config::timeout::TimeoutConfig; @@ -30,18 +30,19 @@ use gpui::{ use gpui_tokio::Tokio; use http_client::HttpClient; use language_model::{ - AuthenticateError, LanguageModel, LanguageModelCacheConfiguration, + AuthenticateError, EnvVar, IconOrSvg, LanguageModel, LanguageModelCacheConfiguration, LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest, LanguageModelToolChoice, LanguageModelToolResultContent, LanguageModelToolUse, MessageContent, RateLimiter, Role, - TokenUsage, + TokenUsage, env_var, }; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use serde_json::Value; use settings::{BedrockAvailableModel as AvailableModel, Settings, SettingsStore}; use smol::lock::OnceCell; +use std::sync::LazyLock; use strum::{EnumIter, IntoEnumIterator, IntoStaticStr}; use ui::{ButtonLink, ConfiguredApiCard, List, ListBulletItem, prelude::*}; use ui_input::InputField; @@ -54,12 +55,52 @@ actions!(bedrock, [Tab, TabPrev]); const PROVIDER_ID: LanguageModelProviderId = LanguageModelProviderId::new("amazon-bedrock"); const PROVIDER_NAME: LanguageModelProviderName = LanguageModelProviderName::new("Amazon Bedrock"); +/// Credentials stored in the keychain for static authentication. +/// Region is handled separately since it's orthogonal to auth method. #[derive(Default, Clone, Deserialize, Serialize, PartialEq, Debug)] pub struct BedrockCredentials { pub access_key_id: String, pub secret_access_key: String, pub session_token: Option, - pub region: String, + pub bearer_token: Option, +} + +/// Resolved authentication configuration for Bedrock. +/// Settings take priority over UX-provided credentials. +#[derive(Clone, Debug, PartialEq)] +pub enum BedrockAuth { + /// Use default AWS credential provider chain (IMDSv2, PodIdentity, env vars, etc.) + Automatic, + /// Use AWS named profile from ~/.aws/credentials or ~/.aws/config + NamedProfile { profile_name: String }, + /// Use AWS SSO profile + SingleSignOn { profile_name: String }, + /// Use IAM credentials (access key + secret + optional session token) + IamCredentials { + access_key_id: String, + secret_access_key: String, + session_token: Option, + }, + /// Use Bedrock API Key (bearer token authentication) + ApiKey { api_key: String }, +} + +impl BedrockCredentials { + /// Convert stored credentials to the appropriate auth variant. + /// Prefers API key if present, otherwise uses IAM credentials. + fn into_auth(self) -> Option { + if let Some(api_key) = self.bearer_token.filter(|t| !t.is_empty()) { + Some(BedrockAuth::ApiKey { api_key }) + } else if !self.access_key_id.is_empty() && !self.secret_access_key.is_empty() { + Some(BedrockAuth::IamCredentials { + access_key_id: self.access_key_id, + secret_access_key: self.secret_access_key, + session_token: self.session_token.filter(|t| !t.is_empty()), + }) + } else { + None + } + } } #[derive(Default, Clone, Debug, PartialEq)] @@ -79,6 +120,8 @@ pub enum BedrockAuthMethod { NamedProfile, #[serde(rename = "sso")] SingleSignOn, + #[serde(rename = "api_key")] + ApiKey, /// IMDSv2, PodIdentity, env vars, etc. #[serde(rename = "default")] Automatic, @@ -90,6 +133,7 @@ impl From for BedrockAuthMethod { settings::BedrockAuthMethodContent::SingleSignOn => BedrockAuthMethod::SingleSignOn, settings::BedrockAuthMethodContent::Automatic => BedrockAuthMethod::Automatic, settings::BedrockAuthMethodContent::NamedProfile => BedrockAuthMethod::NamedProfile, + settings::BedrockAuthMethodContent::ApiKey => BedrockAuthMethod::ApiKey, } } } @@ -130,23 +174,26 @@ impl From for ModelMode { const AMAZON_AWS_URL: &str = "https://amazonaws.com"; // These environment variables all use a `ZED_` prefix because we don't want to overwrite the user's AWS credentials. -const ZED_BEDROCK_ACCESS_KEY_ID_VAR: &str = "ZED_ACCESS_KEY_ID"; -const ZED_BEDROCK_SECRET_ACCESS_KEY_VAR: &str = "ZED_SECRET_ACCESS_KEY"; -const ZED_BEDROCK_SESSION_TOKEN_VAR: &str = "ZED_SESSION_TOKEN"; -const ZED_AWS_PROFILE_VAR: &str = "ZED_AWS_PROFILE"; -const ZED_BEDROCK_REGION_VAR: &str = "ZED_AWS_REGION"; -const ZED_AWS_CREDENTIALS_VAR: &str = "ZED_AWS_CREDENTIALS"; -const ZED_AWS_ENDPOINT_VAR: &str = "ZED_AWS_ENDPOINT"; +static ZED_BEDROCK_ACCESS_KEY_ID_VAR: LazyLock = env_var!("ZED_ACCESS_KEY_ID"); +static ZED_BEDROCK_SECRET_ACCESS_KEY_VAR: LazyLock = env_var!("ZED_SECRET_ACCESS_KEY"); +static ZED_BEDROCK_SESSION_TOKEN_VAR: LazyLock = env_var!("ZED_SESSION_TOKEN"); +static ZED_AWS_PROFILE_VAR: LazyLock = env_var!("ZED_AWS_PROFILE"); +static ZED_BEDROCK_REGION_VAR: LazyLock = env_var!("ZED_AWS_REGION"); +static ZED_AWS_ENDPOINT_VAR: LazyLock = env_var!("ZED_AWS_ENDPOINT"); +static ZED_BEDROCK_BEARER_TOKEN_VAR: LazyLock = env_var!("ZED_BEDROCK_BEARER_TOKEN"); pub struct State { - credentials: Option, + /// The resolved authentication method. Settings take priority over UX credentials. + auth: Option, + /// Raw settings from settings.json settings: Option, + /// Whether credentials came from environment variables (only relevant for static credentials) credentials_from_env: bool, _subscription: Subscription, } impl State { - fn reset_credentials(&self, cx: &mut Context) -> Task> { + fn reset_auth(&self, cx: &mut Context) -> Task> { let credentials_provider = ::global(cx); cx.spawn(async move |this, cx| { credentials_provider @@ -154,19 +201,19 @@ impl State { .await .log_err(); this.update(cx, |this, cx| { - this.credentials = None; + this.auth = None; this.credentials_from_env = false; - this.settings = None; cx.notify(); }) }) } - fn set_credentials( + fn set_static_credentials( &mut self, credentials: BedrockCredentials, cx: &mut Context, ) -> Task> { + let auth = credentials.clone().into_auth(); let credentials_provider = ::global(cx); cx.spawn(async move |this, cx| { credentials_provider @@ -178,50 +225,131 @@ impl State { ) .await?; this.update(cx, |this, cx| { - this.credentials = Some(credentials); + this.auth = auth; + this.credentials_from_env = false; cx.notify(); }) }) } fn is_authenticated(&self) -> bool { - let derived = self - .settings - .as_ref() - .and_then(|s| s.authentication_method.as_ref()); - let creds = self.credentials.as_ref(); - - derived.is_some() || creds.is_some() + self.auth.is_some() } + /// Resolve authentication. Settings take priority over UX-provided credentials. fn authenticate(&self, cx: &mut Context) -> Task> { if self.is_authenticated() { return Task::ready(Ok(())); } + // Step 1: Check if settings specify an auth method (enterprise control) + if let Some(settings) = &self.settings { + if let Some(method) = &settings.authentication_method { + let profile_name = settings + .profile_name + .clone() + .unwrap_or_else(|| "default".to_string()); + + let auth = match method { + BedrockAuthMethod::Automatic => BedrockAuth::Automatic, + BedrockAuthMethod::NamedProfile => BedrockAuth::NamedProfile { profile_name }, + BedrockAuthMethod::SingleSignOn => BedrockAuth::SingleSignOn { profile_name }, + BedrockAuthMethod::ApiKey => { + // ApiKey method means "use static credentials from keychain/env" + // Fall through to load them below + return self.load_static_credentials(cx); + } + }; + + return cx.spawn(async move |this, cx| { + this.update(cx, |this, cx| { + this.auth = Some(auth); + this.credentials_from_env = false; + cx.notify(); + })?; + Ok(()) + }); + } + } + + // Step 2: No settings auth method - try to load static credentials + self.load_static_credentials(cx) + } + + /// Load static credentials from environment variables or keychain. + fn load_static_credentials( + &self, + cx: &mut Context, + ) -> Task> { let credentials_provider = ::global(cx); cx.spawn(async move |this, cx| { - let (credentials, from_env) = - if let Ok(credentials) = std::env::var(ZED_AWS_CREDENTIALS_VAR) { - (credentials, true) - } else { - let (_, credentials) = credentials_provider - .read_credentials(AMAZON_AWS_URL, cx) - .await? - .ok_or_else(|| AuthenticateError::CredentialsNotFound)?; + // Try environment variables first + let (auth, from_env) = if let Some(bearer_token) = &ZED_BEDROCK_BEARER_TOKEN_VAR.value { + if !bearer_token.is_empty() { ( - String::from_utf8(credentials) - .context("invalid {PROVIDER_NAME} credentials")?, - false, + Some(BedrockAuth::ApiKey { + api_key: bearer_token.to_string(), + }), + true, ) - }; + } else { + (None, false) + } + } else if let Some(access_key_id) = &ZED_BEDROCK_ACCESS_KEY_ID_VAR.value { + if let Some(secret_access_key) = &ZED_BEDROCK_SECRET_ACCESS_KEY_VAR.value { + if !access_key_id.is_empty() && !secret_access_key.is_empty() { + let session_token = ZED_BEDROCK_SESSION_TOKEN_VAR + .value + .as_deref() + .filter(|s| !s.is_empty()) + .map(|s| s.to_string()); + ( + Some(BedrockAuth::IamCredentials { + access_key_id: access_key_id.to_string(), + secret_access_key: secret_access_key.to_string(), + session_token, + }), + true, + ) + } else { + (None, false) + } + } else { + (None, false) + } + } else { + (None, false) + }; + + // If we got auth from env vars, use it + if let Some(auth) = auth { + this.update(cx, |this, cx| { + this.auth = Some(auth); + this.credentials_from_env = from_env; + cx.notify(); + })?; + return Ok(()); + } + + // Try keychain + let (_, credentials_bytes) = credentials_provider + .read_credentials(AMAZON_AWS_URL, cx) + .await? + .ok_or(AuthenticateError::CredentialsNotFound)?; + + let credentials_str = String::from_utf8(credentials_bytes) + .context("invalid {PROVIDER_NAME} credentials")?; let credentials: BedrockCredentials = - serde_json::from_str(&credentials).context("failed to parse credentials")?; + serde_json::from_str(&credentials_str).context("failed to parse credentials")?; + + let auth = credentials + .into_auth() + .ok_or(AuthenticateError::CredentialsNotFound)?; this.update(cx, |this, cx| { - this.credentials = Some(credentials); - this.credentials_from_env = from_env; + this.auth = Some(auth); + this.credentials_from_env = false; cx.notify(); })?; @@ -229,15 +357,19 @@ impl State { }) } + /// Get the resolved region. Checks env var, then settings, then defaults to us-east-1. fn get_region(&self) -> String { - // Get region - from credentials or directly from settings - let credentials_region = self.credentials.as_ref().map(|s| s.region.clone()); - let settings_region = self.settings.as_ref().and_then(|s| s.region.clone()); - - // Use credentials region if available, otherwise use settings region, finally fall back to default - credentials_region - .or(settings_region) - .unwrap_or(String::from("us-east-1")) + // Priority: env var > settings > default + if let Some(region) = ZED_BEDROCK_REGION_VAR.value.as_deref() { + if !region.is_empty() { + return region.to_string(); + } + } + + self.settings + .as_ref() + .and_then(|s| s.region.clone()) + .unwrap_or_else(|| "us-east-1".to_string()) } fn get_allow_global(&self) -> bool { @@ -257,7 +389,7 @@ pub struct BedrockLanguageModelProvider { impl BedrockLanguageModelProvider { pub fn new(http_client: Arc, cx: &mut App) -> Self { let state = cx.new(|cx| State { - credentials: None, + auth: None, settings: Some(AllLanguageModelSettings::get_global(cx).bedrock.clone()), credentials_from_env: false, _subscription: cx.observe_global::(|_, cx| { @@ -266,7 +398,7 @@ impl BedrockLanguageModelProvider { }); Self { - http_client: AwsHttpClient::new(http_client.clone()), + http_client: AwsHttpClient::new(http_client), handle: Tokio::handle(cx), state, } @@ -294,8 +426,8 @@ impl LanguageModelProvider for BedrockLanguageModelProvider { PROVIDER_NAME } - fn icon(&self) -> IconName { - IconName::AiBedrock + fn icon(&self) -> IconOrSvg { + IconOrSvg::Icon(IconName::AiBedrock) } fn default_model(&self, _cx: &App) -> Option> { @@ -312,7 +444,6 @@ impl LanguageModelProvider for BedrockLanguageModelProvider { for model in bedrock::Model::iter() { if !matches!(model, bedrock::Model::Custom { .. }) { - // TODO: Sonnet 3.7 vs. 3.7 Thinking bug is here. models.insert(model.id().to_string(), model); } } @@ -366,8 +497,7 @@ impl LanguageModelProvider for BedrockLanguageModelProvider { } fn reset_credentials(&self, cx: &mut App) -> Task> { - self.state - .update(cx, |state, cx| state.reset_credentials(cx)) + self.state.update(cx, |state, cx| state.reset_auth(cx)) } } @@ -393,25 +523,11 @@ impl BedrockModel { fn get_or_init_client(&self, cx: &AsyncApp) -> anyhow::Result<&BedrockClient> { self.client .get_or_try_init_blocking(|| { - let (auth_method, credentials, endpoint, region, settings) = - cx.read_entity(&self.state, |state, _cx| { - let auth_method = state - .settings - .as_ref() - .and_then(|s| s.authentication_method.clone()); - - let endpoint = state.settings.as_ref().and_then(|s| s.endpoint.clone()); - - let region = state.get_region(); - - ( - auth_method, - state.credentials.clone(), - endpoint, - region, - state.settings.clone(), - ) - })?; + let (auth, endpoint, region) = cx.read_entity(&self.state, |state, _cx| { + let endpoint = state.settings.as_ref().and_then(|s| s.endpoint.clone()); + let region = state.get_region(); + (state.auth.clone(), endpoint, region) + })?; let mut config_builder = aws_config::defaults(BehaviorVersion::latest()) .stalled_stream_protection(StalledStreamProtectionConfig::disabled()) @@ -425,37 +541,39 @@ impl BedrockModel { config_builder = config_builder.endpoint_url(endpoint_url); } - match auth_method { - None => { - if let Some(creds) = credentials { - let aws_creds = Credentials::new( - creds.access_key_id, - creds.secret_access_key, - creds.session_token, - None, - "zed-bedrock-provider", - ); - config_builder = config_builder.credentials_provider(aws_creds); - } + match auth { + Some(BedrockAuth::Automatic) | None => { + // Use default AWS credential provider chain } - Some(BedrockAuthMethod::NamedProfile) - | Some(BedrockAuthMethod::SingleSignOn) => { - // Currently NamedProfile and SSO behave the same way but only the instructions change - // Until we support BearerAuth through SSO, this will not change. - let profile_name = settings - .and_then(|s| s.profile_name) - .unwrap_or_else(|| "default".to_string()); - + Some(BedrockAuth::NamedProfile { profile_name }) + | Some(BedrockAuth::SingleSignOn { profile_name }) => { if !profile_name.is_empty() { config_builder = config_builder.profile_name(profile_name); } } - Some(BedrockAuthMethod::Automatic) => { - // Use default credential provider chain + Some(BedrockAuth::IamCredentials { + access_key_id, + secret_access_key, + session_token, + }) => { + let aws_creds = Credentials::new( + access_key_id, + secret_access_key, + session_token, + None, + "zed-bedrock-provider", + ); + config_builder = config_builder.credentials_provider(aws_creds); + } + Some(BedrockAuth::ApiKey { api_key }) => { + config_builder = config_builder + .auth_scheme_preference(["httpBearerAuth".into()]) // https://github.com/smithy-lang/smithy-rs/pull/4241 + .token_provider(Token::new(api_key, None)); } } let config = self.handle.block_on(config_builder.load()); + anyhow::Ok(BedrockClient::new(&config)) }) .context("initializing Bedrock client")?; @@ -1024,7 +1142,7 @@ struct ConfigurationView { access_key_id_editor: Entity, secret_access_key_editor: Entity, session_token_editor: Entity, - region_editor: Entity, + bearer_token_editor: Entity, state: Entity, load_credentials_task: Option>, focus_handle: FocusHandle, @@ -1035,7 +1153,7 @@ impl ConfigurationView { const PLACEHOLDER_SECRET_ACCESS_KEY_TEXT: &'static str = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; const PLACEHOLDER_SESSION_TOKEN_TEXT: &'static str = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; - const PLACEHOLDER_REGION: &'static str = "us-east-1"; + const PLACEHOLDER_BEARER_TOKEN_TEXT: &'static str = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; fn new(state: Entity, window: &mut Window, cx: &mut Context) -> Self { let focus_handle = cx.focus_handle(); @@ -1066,9 +1184,9 @@ impl ConfigurationView { .tab_stop(true) }); - let region_editor = cx.new(|cx| { - InputField::new(window, cx, Self::PLACEHOLDER_REGION) - .label("Region") + let bearer_token_editor = cx.new(|cx| { + InputField::new(window, cx, Self::PLACEHOLDER_BEARER_TOKEN_TEXT) + .label("Bedrock API Key") .tab_index(3) .tab_stop(true) }); @@ -1095,7 +1213,7 @@ impl ConfigurationView { access_key_id_editor, secret_access_key_editor, session_token_editor, - region_editor, + bearer_token_editor, state, load_credentials_task, focus_handle, @@ -1131,25 +1249,30 @@ impl ConfigurationView { } else { Some(session_token) }; - let region = self.region_editor.read(cx).text(cx).trim().to_string(); - let region = if region.is_empty() { - "us-east-1".to_string() + let bearer_token = self + .bearer_token_editor + .read(cx) + .text(cx) + .trim() + .to_string(); + let bearer_token = if bearer_token.is_empty() { + None } else { - region + Some(bearer_token) }; let state = self.state.clone(); cx.spawn(async move |_, cx| { state .update(cx, |state, cx| { - let credentials: BedrockCredentials = BedrockCredentials { - region: region.clone(), - access_key_id: access_key_id.clone(), - secret_access_key: secret_access_key.clone(), - session_token: session_token.clone(), + let credentials = BedrockCredentials { + access_key_id, + secret_access_key, + session_token, + bearer_token, }; - state.set_credentials(credentials, cx) + state.set_static_credentials(credentials, cx) })? .await }) @@ -1163,41 +1286,39 @@ impl ConfigurationView { .update(cx, |editor, cx| editor.set_text("", window, cx)); self.session_token_editor .update(cx, |editor, cx| editor.set_text("", window, cx)); - self.region_editor + self.bearer_token_editor .update(cx, |editor, cx| editor.set_text("", window, cx)); let state = self.state.clone(); - cx.spawn(async move |_, cx| { - state - .update(cx, |state, cx| state.reset_credentials(cx))? - .await - }) - .detach_and_log_err(cx); + cx.spawn(async move |_, cx| state.update(cx, |state, cx| state.reset_auth(cx))?.await) + .detach_and_log_err(cx); } fn should_render_editor(&self, cx: &Context) -> bool { self.state.read(cx).is_authenticated() } - fn on_tab(&mut self, _: &menu::SelectNext, window: &mut Window, _: &mut Context) { - window.focus_next(); + fn on_tab(&mut self, _: &menu::SelectNext, window: &mut Window, cx: &mut Context) { + window.focus_next(cx); } fn on_tab_prev( &mut self, _: &menu::SelectPrevious, window: &mut Window, - _: &mut Context, + cx: &mut Context, ) { - window.focus_prev(); + window.focus_prev(cx); } } impl Render for ConfigurationView { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { - let env_var_set = self.state.read(cx).credentials_from_env; - let bedrock_settings = self.state.read(cx).settings.as_ref(); - let bedrock_method = bedrock_settings + let state = self.state.read(cx); + let env_var_set = state.credentials_from_env; + let auth = state.auth.clone(); + let settings_auth_method = state + .settings .as_ref() .and_then(|s| s.authentication_method.clone()); @@ -1205,34 +1326,62 @@ impl Render for ConfigurationView { return div().child(Label::new("Loading credentials...")).into_any(); } - let configured_label = if env_var_set { - format!( - "Access Key ID is set in {ZED_BEDROCK_ACCESS_KEY_ID_VAR}, Secret Key is set in {ZED_BEDROCK_SECRET_ACCESS_KEY_VAR}, Region is set in {ZED_BEDROCK_REGION_VAR} environment variables." - ) - } else { - match bedrock_method { - Some(BedrockAuthMethod::Automatic) => "You are using automatic credentials.".into(), - Some(BedrockAuthMethod::NamedProfile) => "You are using named profile.".into(), - Some(BedrockAuthMethod::SingleSignOn) => { - "You are using a single sign on profile.".into() - } - None => "You are using static credentials.".into(), + let configured_label = match &auth { + Some(BedrockAuth::Automatic) => { + "Using automatic credentials (AWS default chain)".into() + } + Some(BedrockAuth::NamedProfile { profile_name }) => { + format!("Using AWS profile: {profile_name}") + } + Some(BedrockAuth::SingleSignOn { profile_name }) => { + format!("Using AWS SSO profile: {profile_name}") + } + Some(BedrockAuth::IamCredentials { .. }) if env_var_set => { + format!( + "Using IAM credentials from {} and {} environment variables", + ZED_BEDROCK_ACCESS_KEY_ID_VAR.name, ZED_BEDROCK_SECRET_ACCESS_KEY_VAR.name + ) + } + Some(BedrockAuth::IamCredentials { .. }) => "Using IAM credentials".into(), + Some(BedrockAuth::ApiKey { .. }) if env_var_set => { + format!( + "Using Bedrock API Key from {} environment variable", + ZED_BEDROCK_BEARER_TOKEN_VAR.name + ) } + Some(BedrockAuth::ApiKey { .. }) => "Using Bedrock API Key".into(), + None => "Not authenticated".into(), }; + // Determine if credentials can be reset + // Settings-derived auth (non-ApiKey) cannot be reset from UI + let is_settings_derived = matches!( + settings_auth_method, + Some(BedrockAuthMethod::Automatic) + | Some(BedrockAuthMethod::NamedProfile) + | Some(BedrockAuthMethod::SingleSignOn) + ); + let tooltip_label = if env_var_set { Some(format!( - "To reset your credentials, unset the {ZED_BEDROCK_ACCESS_KEY_ID_VAR}, {ZED_BEDROCK_SECRET_ACCESS_KEY_VAR}, and {ZED_BEDROCK_REGION_VAR} environment variables." + "To reset your credentials, unset the {}, {}, and {} or {} environment variables.", + ZED_BEDROCK_ACCESS_KEY_ID_VAR.name, + ZED_BEDROCK_SECRET_ACCESS_KEY_VAR.name, + ZED_BEDROCK_SESSION_TOKEN_VAR.name, + ZED_BEDROCK_BEARER_TOKEN_VAR.name )) - } else if bedrock_method.is_some() { - Some("You cannot reset credentials as they're being derived, check Zed settings to understand how.".to_string()) + } else if is_settings_derived { + Some( + "Authentication method is configured in settings. Edit settings.json to change." + .to_string(), + ) } else { None }; if self.should_render_editor(cx) { return ConfiguredApiCard::new(configured_label) - .disabled(env_var_set || bedrock_method.is_some()) + .disabled(env_var_set || is_settings_derived) .on_click(cx.listener(|this, _, window, cx| this.reset_credentials(window, cx))) .when_some(tooltip_label, |this, label| this.tooltip_label(label)) .into_any_element(); @@ -1262,7 +1411,7 @@ impl Render for ConfigurationView { .child(self.render_static_credentials_ui()) .child( Label::new( - format!("You can also assign the {ZED_BEDROCK_ACCESS_KEY_ID_VAR}, {ZED_BEDROCK_SECRET_ACCESS_KEY_VAR} AND {ZED_BEDROCK_REGION_VAR} environment variables and restart Zed."), + format!("You can also assign the {}, {} AND {} environment variables (or {} for Bedrock API Key authentication) and restart Zed.", ZED_BEDROCK_ACCESS_KEY_ID_VAR.name, ZED_BEDROCK_SECRET_ACCESS_KEY_VAR.name, ZED_BEDROCK_REGION_VAR.name, ZED_BEDROCK_BEARER_TOKEN_VAR.name), ) .size(LabelSize::Small) .color(Color::Muted) @@ -1270,7 +1419,7 @@ impl Render for ConfigurationView { ) .child( Label::new( - format!("Optionally, if your environment uses AWS CLI profiles, you can set {ZED_AWS_PROFILE_VAR}; if it requires a custom endpoint, you can set {ZED_AWS_ENDPOINT_VAR}; and if it requires a Session Token, you can set {ZED_BEDROCK_SESSION_TOKEN_VAR}."), + format!("Optionally, if your environment uses AWS CLI profiles, you can set {}; if it requires a custom endpoint, you can set {}; and if it requires a Session Token, you can set {}.", ZED_AWS_PROFILE_VAR.name, ZED_AWS_ENDPOINT_VAR.name, ZED_BEDROCK_SESSION_TOKEN_VAR.name), ) .size(LabelSize::Small) .color(Color::Muted), @@ -1292,31 +1441,47 @@ impl ConfigurationView { ) .child( Label::new( - "This method uses your AWS access key ID and secret access key directly.", + "This method uses your AWS access key ID and secret access key, or a Bedrock API Key.", ) ) .child( List::new() .child( ListBulletItem::new("") - .child(Label::new("Create an IAM user in the AWS console with programmatic access")) + .child(Label::new("For access keys: Create an IAM user in the AWS console with programmatic access")) .child(ButtonLink::new("IAM Console", "https://us-east-1.console.aws.amazon.com/iam/home?region=us-east-1#/users")) ) .child( ListBulletItem::new("") - .child(Label::new("Attach the necessary Bedrock permissions to this")) - .child(ButtonLink::new("user", "https://docs.aws.amazon.com/bedrock/latest/userguide/inference-prereq.html")) + .child(Label::new("For Bedrock API Keys: Generate an API key from the")) + .child(ButtonLink::new("Bedrock Console", "https://docs.aws.amazon.com/bedrock/latest/userguide/api-keys-use.html")) ) .child( - ListBulletItem::new("Copy the access key ID and secret access key when provided") + ListBulletItem::new("") + .child(Label::new("Attach the necessary Bedrock permissions to this")) + .child(ButtonLink::new("user", "https://docs.aws.amazon.com/bedrock/latest/userguide/inference-prereq.html")) ) .child( - ListBulletItem::new("Enter these credentials below") - ) + ListBulletItem::new("Enter either access keys OR a Bedrock API Key below (not both)") + ), ) .child(self.access_key_id_editor.clone()) .child(self.secret_access_key_editor.clone()) .child(self.session_token_editor.clone()) - .child(self.region_editor.clone()) + .child( + Label::new("OR") + .size(LabelSize::Default) + .weight(FontWeight::BOLD) + .my_1(), + ) + .child(self.bearer_token_editor.clone()) + .child( + Label::new( + format!("Region is configured via {} environment variable or settings.json (defaults to us-east-1).", ZED_BEDROCK_REGION_VAR.name), + ) + .size(LabelSize::Small) + .color(Color::Muted) + .mt_2(), + ) } } diff --git a/crates/language_models/src/provider/cloud.rs b/crates/language_models/src/provider/cloud.rs index def1cef84d3166d08dcc7638ca5a29cabbd149c5..65a42740eb9a8aff830d7544ed5aa972c6697d88 100644 --- a/crates/language_models/src/provider/cloud.rs +++ b/crates/language_models/src/provider/cloud.rs @@ -19,7 +19,7 @@ use gpui::{AnyElement, AnyView, App, AsyncApp, Context, Entity, Subscription, Ta use http_client::http::{HeaderMap, HeaderValue}; use http_client::{AsyncBody, HttpClient, HttpRequestExt, Method, Response, StatusCode}; use language_model::{ - AuthenticateError, LanguageModel, LanguageModelCacheConfiguration, + AuthenticateError, IconOrSvg, LanguageModel, LanguageModelCacheConfiguration, LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest, LanguageModelToolChoice, @@ -304,8 +304,8 @@ impl LanguageModelProvider for CloudLanguageModelProvider { PROVIDER_NAME } - fn icon(&self) -> IconName { - IconName::AiZed + fn icon(&self) -> IconOrSvg { + IconOrSvg::Icon(IconName::AiZed) } fn default_model(&self, cx: &App) -> Option> { diff --git a/crates/language_models/src/provider/copilot_chat.rs b/crates/language_models/src/provider/copilot_chat.rs index 70198b337e467e1618192e781d3e3be305fea9c5..68eaab1dbed33a8d983de6a919b75dc809410a70 100644 --- a/crates/language_models/src/provider/copilot_chat.rs +++ b/crates/language_models/src/provider/copilot_chat.rs @@ -18,12 +18,12 @@ use gpui::{AnyView, App, AsyncApp, Entity, Subscription, Task}; use http_client::StatusCode; use language::language_settings::all_language_settings; use language_model::{ - AuthenticateError, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent, - LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId, - LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest, - LanguageModelRequestMessage, LanguageModelToolChoice, LanguageModelToolResultContent, - LanguageModelToolSchemaFormat, LanguageModelToolUse, MessageContent, RateLimiter, Role, - StopReason, TokenUsage, + AuthenticateError, IconOrSvg, LanguageModel, LanguageModelCompletionError, + LanguageModelCompletionEvent, LanguageModelId, LanguageModelName, LanguageModelProvider, + LanguageModelProviderId, LanguageModelProviderName, LanguageModelProviderState, + LanguageModelRequest, LanguageModelRequestMessage, LanguageModelToolChoice, + LanguageModelToolResultContent, LanguageModelToolSchemaFormat, LanguageModelToolUse, + MessageContent, RateLimiter, Role, StopReason, TokenUsage, }; use settings::SettingsStore; use ui::prelude::*; @@ -104,8 +104,8 @@ impl LanguageModelProvider for CopilotChatLanguageModelProvider { PROVIDER_NAME } - fn icon(&self) -> IconName { - IconName::Copilot + fn icon(&self) -> IconOrSvg { + IconOrSvg::Icon(IconName::Copilot) } fn default_model(&self, cx: &App) -> Option> { diff --git a/crates/language_models/src/provider/deepseek.rs b/crates/language_models/src/provider/deepseek.rs index b00a5d82f5665a5c87c662d1af84fbeb9ac07ebb..b3264b869195aa34d7083cd31992d8c220d20349 100644 --- a/crates/language_models/src/provider/deepseek.rs +++ b/crates/language_models/src/provider/deepseek.rs @@ -7,7 +7,7 @@ use futures::{FutureExt, StreamExt, future, future::BoxFuture, stream::BoxStream use gpui::{AnyView, App, AsyncApp, Context, Entity, SharedString, Task, Window}; use http_client::HttpClient; use language_model::{ - ApiKeyState, AuthenticateError, EnvVar, LanguageModel, LanguageModelCompletionError, + ApiKeyState, AuthenticateError, EnvVar, IconOrSvg, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest, LanguageModelToolChoice, LanguageModelToolResultContent, @@ -127,8 +127,8 @@ impl LanguageModelProvider for DeepSeekLanguageModelProvider { PROVIDER_NAME } - fn icon(&self) -> IconName { - IconName::AiDeepSeek + fn icon(&self) -> IconOrSvg { + IconOrSvg::Icon(IconName::AiDeepSeek) } fn default_model(&self, _cx: &App) -> Option> { diff --git a/crates/language_models/src/provider/google.rs b/crates/language_models/src/provider/google.rs index 989b99061b6d0f4c6680f08616c55946138ae0fe..7d567d60f405c7880cb6494f6d2ff604d7f53ac2 100644 --- a/crates/language_models/src/provider/google.rs +++ b/crates/language_models/src/provider/google.rs @@ -14,7 +14,7 @@ use language_model::{ LanguageModelToolUse, LanguageModelToolUseId, MessageContent, StopReason, }; use language_model::{ - LanguageModel, LanguageModelId, LanguageModelName, LanguageModelProvider, + IconOrSvg, LanguageModel, LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest, RateLimiter, Role, }; @@ -164,8 +164,8 @@ impl LanguageModelProvider for GoogleLanguageModelProvider { PROVIDER_NAME } - fn icon(&self) -> IconName { - IconName::AiGoogle + fn icon(&self) -> IconOrSvg { + IconOrSvg::Icon(IconName::AiGoogle) } fn default_model(&self, _cx: &App) -> Option> { diff --git a/crates/language_models/src/provider/lmstudio.rs b/crates/language_models/src/provider/lmstudio.rs index 8e42d12db4c24ef6a66ddef470a34c620ed7ee00..237b64ac7d0ed728b057f6b553ad2a2a1ebae1db 100644 --- a/crates/language_models/src/provider/lmstudio.rs +++ b/crates/language_models/src/provider/lmstudio.rs @@ -10,7 +10,7 @@ use language_model::{ StopReason, TokenUsage, }; use language_model::{ - LanguageModel, LanguageModelId, LanguageModelName, LanguageModelProvider, + IconOrSvg, LanguageModel, LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest, RateLimiter, Role, }; @@ -20,7 +20,7 @@ use settings::{Settings, SettingsStore}; use std::pin::Pin; use std::str::FromStr; use std::{collections::BTreeMap, sync::Arc}; -use ui::{ButtonLike, Indicator, InlineCode, List, ListBulletItem, prelude::*}; +use ui::{ButtonLike, Indicator, List, ListBulletItem, prelude::*}; use util::ResultExt; use crate::AllLanguageModelSettings; @@ -175,8 +175,8 @@ impl LanguageModelProvider for LmStudioLanguageModelProvider { PROVIDER_NAME } - fn icon(&self) -> IconName { - IconName::AiLmStudio + fn icon(&self) -> IconOrSvg { + IconOrSvg::Icon(IconName::AiLmStudio) } fn default_model(&self, _: &App) -> Option> { @@ -691,7 +691,7 @@ impl Render for ConfigurationView { .child( ListBulletItem::new("") .child(Label::new("To get your first model, try running")) - .child(InlineCode::new("lms get qwen2.5-coder-7b")), + .child(Label::new("lms get qwen2.5-coder-7b").inline_code(cx)), ), ), ) diff --git a/crates/language_models/src/provider/mistral.rs b/crates/language_models/src/provider/mistral.rs index 64f3999e3aa96b2611e265a6eaf5df8063332c2a..0b8af405ade8fc00c0d1e2e57ba115560d94a71d 100644 --- a/crates/language_models/src/provider/mistral.rs +++ b/crates/language_models/src/provider/mistral.rs @@ -5,7 +5,7 @@ use futures::{FutureExt, Stream, StreamExt, future, future::BoxFuture, stream::B use gpui::{AnyView, App, AsyncApp, Context, Entity, Global, SharedString, Task, Window}; use http_client::HttpClient; use language_model::{ - ApiKeyState, AuthenticateError, EnvVar, LanguageModel, LanguageModelCompletionError, + ApiKeyState, AuthenticateError, EnvVar, IconOrSvg, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest, LanguageModelToolChoice, LanguageModelToolResultContent, @@ -176,8 +176,8 @@ impl LanguageModelProvider for MistralLanguageModelProvider { PROVIDER_NAME } - fn icon(&self) -> IconName { - IconName::AiMistral + fn icon(&self) -> IconOrSvg { + IconOrSvg::Icon(IconName::AiMistral) } fn default_model(&self, _cx: &App) -> Option> { diff --git a/crates/language_models/src/provider/ollama.rs b/crates/language_models/src/provider/ollama.rs index 6f3c49f8669885bfd02e5b11b81a091b1248227c..f5d8820e710ea6c9f89de6da5a7aae2f204c6470 100644 --- a/crates/language_models/src/provider/ollama.rs +++ b/crates/language_models/src/provider/ollama.rs @@ -5,7 +5,7 @@ use futures::{Stream, TryFutureExt, stream}; use gpui::{AnyView, App, AsyncApp, Context, CursorStyle, Entity, Task}; use http_client::HttpClient; use language_model::{ - ApiKeyState, AuthenticateError, EnvVar, LanguageModel, LanguageModelCompletionError, + ApiKeyState, AuthenticateError, EnvVar, IconOrSvg, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest, LanguageModelRequestTool, LanguageModelToolChoice, LanguageModelToolUse, @@ -23,8 +23,8 @@ use std::sync::LazyLock; use std::sync::atomic::{AtomicU64, Ordering}; use std::{collections::HashMap, sync::Arc}; use ui::{ - ButtonLike, ButtonLink, ConfiguredApiCard, ElevationIndex, InlineCode, List, ListBulletItem, - Tooltip, prelude::*, + ButtonLike, ButtonLink, ConfiguredApiCard, ElevationIndex, List, ListBulletItem, Tooltip, + prelude::*, }; use ui_input::InputField; @@ -221,8 +221,8 @@ impl LanguageModelProvider for OllamaLanguageModelProvider { PROVIDER_NAME } - fn icon(&self) -> IconName { - IconName::AiOllama + fn icon(&self) -> IconOrSvg { + IconOrSvg::Icon(IconName::AiOllama) } fn default_model(&self, _: &App) -> Option> { @@ -249,33 +249,7 @@ impl LanguageModelProvider for OllamaLanguageModelProvider { } // Override with available models from settings - for setting_model in &OllamaLanguageModelProvider::settings(cx).available_models { - let setting_base = setting_model.name.split(':').next().unwrap(); - if let Some(model) = models - .values_mut() - .find(|m| m.name.split(':').next().unwrap() == setting_base) - { - model.max_tokens = setting_model.max_tokens; - model.display_name = setting_model.display_name.clone(); - model.keep_alive = setting_model.keep_alive.clone(); - model.supports_tools = setting_model.supports_tools; - model.supports_vision = setting_model.supports_images; - model.supports_thinking = setting_model.supports_thinking; - } else { - models.insert( - setting_model.name.clone(), - ollama::Model { - name: setting_model.name.clone(), - display_name: setting_model.display_name.clone(), - max_tokens: setting_model.max_tokens, - keep_alive: setting_model.keep_alive.clone(), - supports_tools: setting_model.supports_tools, - supports_vision: setting_model.supports_images, - supports_thinking: setting_model.supports_thinking, - }, - ); - } - } + merge_settings_into_models(&mut models, &settings.available_models); let mut models = models .into_values() @@ -724,7 +698,7 @@ impl ConfigurationView { cx.notify(); } - fn render_instructions() -> Div { + fn render_instructions(cx: &mut Context) -> Div { v_flex() .gap_2() .child(Label::new( @@ -742,7 +716,7 @@ impl ConfigurationView { .child( ListBulletItem::new("") .child(Label::new("Start Ollama and download a model:")) - .child(InlineCode::new("ollama run gpt-oss:20b")), + .child(Label::new("ollama run gpt-oss:20b").inline_code(cx)), ) .child(ListBulletItem::new( "Click 'Connect' below to start using Ollama in Zed", @@ -833,7 +807,7 @@ impl Render for ConfigurationView { v_flex() .gap_2() - .child(Self::render_instructions()) + .child(Self::render_instructions(cx)) .child(self.render_api_url_editor(cx)) .child(self.render_api_key_editor(cx)) .child( @@ -921,6 +895,35 @@ impl Render for ConfigurationView { } } +fn merge_settings_into_models( + models: &mut HashMap, + available_models: &[AvailableModel], +) { + for setting_model in available_models { + if let Some(model) = models.get_mut(&setting_model.name) { + model.max_tokens = setting_model.max_tokens; + model.display_name = setting_model.display_name.clone(); + model.keep_alive = setting_model.keep_alive.clone(); + model.supports_tools = setting_model.supports_tools; + model.supports_vision = setting_model.supports_images; + model.supports_thinking = setting_model.supports_thinking; + } else { + models.insert( + setting_model.name.clone(), + ollama::Model { + name: setting_model.name.clone(), + display_name: setting_model.display_name.clone(), + max_tokens: setting_model.max_tokens, + keep_alive: setting_model.keep_alive.clone(), + supports_tools: setting_model.supports_tools, + supports_vision: setting_model.supports_images, + supports_thinking: setting_model.supports_thinking, + }, + ); + } + } +} + fn tool_into_ollama(tool: LanguageModelRequestTool) -> ollama::OllamaTool { ollama::OllamaTool::Function { function: OllamaFunctionTool { @@ -930,3 +933,83 @@ fn tool_into_ollama(tool: LanguageModelRequestTool) -> ollama::OllamaTool { }, } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_merge_settings_preserves_display_names_for_similar_models() { + // Regression test for https://github.com/zed-industries/zed/issues/43646 + // When multiple models share the same base name (e.g., qwen2.5-coder:1.5b and qwen2.5-coder:3b), + // each model should get its own display_name from settings, not a random one. + + let mut models: HashMap = HashMap::new(); + models.insert( + "qwen2.5-coder:1.5b".to_string(), + ollama::Model { + name: "qwen2.5-coder:1.5b".to_string(), + display_name: None, + max_tokens: 4096, + keep_alive: None, + supports_tools: None, + supports_vision: None, + supports_thinking: None, + }, + ); + models.insert( + "qwen2.5-coder:3b".to_string(), + ollama::Model { + name: "qwen2.5-coder:3b".to_string(), + display_name: None, + max_tokens: 4096, + keep_alive: None, + supports_tools: None, + supports_vision: None, + supports_thinking: None, + }, + ); + + let available_models = vec![ + AvailableModel { + name: "qwen2.5-coder:1.5b".to_string(), + display_name: Some("QWEN2.5 Coder 1.5B".to_string()), + max_tokens: 5000, + keep_alive: None, + supports_tools: Some(true), + supports_images: None, + supports_thinking: None, + }, + AvailableModel { + name: "qwen2.5-coder:3b".to_string(), + display_name: Some("QWEN2.5 Coder 3B".to_string()), + max_tokens: 6000, + keep_alive: None, + supports_tools: Some(true), + supports_images: None, + supports_thinking: None, + }, + ]; + + merge_settings_into_models(&mut models, &available_models); + + let model_1_5b = models + .get("qwen2.5-coder:1.5b") + .expect("1.5b model missing"); + let model_3b = models.get("qwen2.5-coder:3b").expect("3b model missing"); + + assert_eq!( + model_1_5b.display_name, + Some("QWEN2.5 Coder 1.5B".to_string()), + "1.5b model should have its own display_name" + ); + assert_eq!(model_1_5b.max_tokens, 5000); + + assert_eq!( + model_3b.display_name, + Some("QWEN2.5 Coder 3B".to_string()), + "3b model should have its own display_name" + ); + assert_eq!(model_3b.max_tokens, 6000); + } +} diff --git a/crates/language_models/src/provider/open_ai.rs b/crates/language_models/src/provider/open_ai.rs index afaffba3e53eb2496f9fae795d69b9e9c9f57249..905d2b37862eebf57c7fb56a540b388338cfd065 100644 --- a/crates/language_models/src/provider/open_ai.rs +++ b/crates/language_models/src/provider/open_ai.rs @@ -5,7 +5,7 @@ use futures::{FutureExt, StreamExt, future, future::BoxFuture}; use gpui::{AnyView, App, AsyncApp, Context, Entity, SharedString, Task, Window}; use http_client::HttpClient; use language_model::{ - ApiKeyState, AuthenticateError, EnvVar, LanguageModel, LanguageModelCompletionError, + ApiKeyState, AuthenticateError, EnvVar, IconOrSvg, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest, LanguageModelToolChoice, LanguageModelToolResultContent, @@ -122,8 +122,8 @@ impl LanguageModelProvider for OpenAiLanguageModelProvider { PROVIDER_NAME } - fn icon(&self) -> IconName { - IconName::AiOpenAi + fn icon(&self) -> IconOrSvg { + IconOrSvg::Icon(IconName::AiOpenAi) } fn default_model(&self, _cx: &App) -> Option> { diff --git a/crates/language_models/src/provider/open_ai_compatible.rs b/crates/language_models/src/provider/open_ai_compatible.rs index e6e7a9984da3d48b9e3c0f9571b8e916359fba03..f95f567739d76670d3cfa7b835bbfaf34ddef92f 100644 --- a/crates/language_models/src/provider/open_ai_compatible.rs +++ b/crates/language_models/src/provider/open_ai_compatible.rs @@ -4,7 +4,7 @@ use futures::{FutureExt, StreamExt, future, future::BoxFuture}; use gpui::{AnyView, App, AsyncApp, Context, Entity, SharedString, Task, Window}; use http_client::HttpClient; use language_model::{ - ApiKeyState, AuthenticateError, EnvVar, LanguageModel, LanguageModelCompletionError, + ApiKeyState, AuthenticateError, EnvVar, IconOrSvg, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest, LanguageModelToolChoice, LanguageModelToolSchemaFormat, RateLimiter, @@ -133,8 +133,8 @@ impl LanguageModelProvider for OpenAiCompatibleLanguageModelProvider { self.name.clone() } - fn icon(&self) -> IconName { - IconName::AiOpenAiCompat + fn icon(&self) -> IconOrSvg { + IconOrSvg::Icon(IconName::AiOpenAiCompat) } fn default_model(&self, cx: &App) -> Option> { diff --git a/crates/language_models/src/provider/open_router.rs b/crates/language_models/src/provider/open_router.rs index ad2e90d9dd5f4ece7e2582a867da50f6962c981c..48d68ddebff7e0c9bbe39dbca696dd2ffcf62605 100644 --- a/crates/language_models/src/provider/open_router.rs +++ b/crates/language_models/src/provider/open_router.rs @@ -4,7 +4,7 @@ use futures::{FutureExt, Stream, StreamExt, future, future::BoxFuture}; use gpui::{AnyView, App, AsyncApp, Context, Entity, SharedString, Task}; use http_client::HttpClient; use language_model::{ - ApiKeyState, AuthenticateError, EnvVar, LanguageModel, LanguageModelCompletionError, + ApiKeyState, AuthenticateError, EnvVar, IconOrSvg, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest, LanguageModelToolChoice, LanguageModelToolResultContent, @@ -180,8 +180,8 @@ impl LanguageModelProvider for OpenRouterLanguageModelProvider { PROVIDER_NAME } - fn icon(&self) -> IconName { - IconName::AiOpenRouter + fn icon(&self) -> IconOrSvg { + IconOrSvg::Icon(IconName::AiOpenRouter) } fn default_model(&self, _cx: &App) -> Option> { diff --git a/crates/language_models/src/provider/vercel.rs b/crates/language_models/src/provider/vercel.rs index 4dfe848df80123dc4c37d27b81f76db359e076f9..e2e692eafff94c56d481dfc2bd96dbfa7adda262 100644 --- a/crates/language_models/src/provider/vercel.rs +++ b/crates/language_models/src/provider/vercel.rs @@ -4,7 +4,7 @@ use futures::{FutureExt, StreamExt, future, future::BoxFuture}; use gpui::{AnyView, App, AsyncApp, Context, Entity, SharedString, Task, Window}; use http_client::HttpClient; use language_model::{ - ApiKeyState, AuthenticateError, EnvVar, LanguageModel, LanguageModelCompletionError, + ApiKeyState, AuthenticateError, EnvVar, IconOrSvg, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest, LanguageModelToolChoice, RateLimiter, Role, env_var, @@ -117,8 +117,8 @@ impl LanguageModelProvider for VercelLanguageModelProvider { PROVIDER_NAME } - fn icon(&self) -> IconName { - IconName::AiVZero + fn icon(&self) -> IconOrSvg { + IconOrSvg::Icon(IconName::AiVZero) } fn default_model(&self, _cx: &App) -> Option> { diff --git a/crates/language_models/src/provider/x_ai.rs b/crates/language_models/src/provider/x_ai.rs index 19c50d71cf4e483b68d48c8b982a975f3091ff46..f0aa0e71a83ae1a201d76a33f63ca0aadc6936a9 100644 --- a/crates/language_models/src/provider/x_ai.rs +++ b/crates/language_models/src/provider/x_ai.rs @@ -4,7 +4,7 @@ use futures::{FutureExt, StreamExt, future, future::BoxFuture}; use gpui::{AnyView, App, AsyncApp, Context, Entity, Task, Window}; use http_client::HttpClient; use language_model::{ - ApiKeyState, AuthenticateError, EnvVar, LanguageModel, LanguageModelCompletionError, + ApiKeyState, AuthenticateError, EnvVar, IconOrSvg, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest, LanguageModelToolChoice, LanguageModelToolSchemaFormat, RateLimiter, @@ -118,8 +118,8 @@ impl LanguageModelProvider for XAiLanguageModelProvider { PROVIDER_NAME } - fn icon(&self) -> IconName { - IconName::AiXAi + fn icon(&self) -> IconOrSvg { + IconOrSvg::Icon(IconName::AiXAi) } fn default_model(&self, _cx: &App) -> Option> { diff --git a/crates/language_tools/src/lsp_button.rs b/crates/language_tools/src/lsp_button.rs index 335381c6f79d950498a0f0c1d330cb21c681f32e..7775586bf19539e13adc6b9df6d92914be6b7f21 100644 --- a/crates/language_tools/src/lsp_button.rs +++ b/crates/language_tools/src/lsp_button.rs @@ -127,6 +127,16 @@ impl LanguageServerState { return menu; }; + let server_versions = self + .lsp_store + .update(cx, |lsp_store, _| { + lsp_store + .language_server_statuses() + .map(|(server_id, status)| (server_id, status.server_version.clone())) + .collect::>() + }) + .unwrap_or_default(); + let mut first_button_encountered = false; for item in &self.items { if let LspMenuItem::ToggleServersButton { restart } = item { @@ -254,6 +264,22 @@ impl LanguageServerState { }; let server_name = server_info.name.clone(); + let server_version = server_versions + .get(&server_info.id) + .and_then(|version| version.clone()); + + let tooltip_text = match (&server_version, &message) { + (None, None) => None, + (Some(version), None) => { + Some(SharedString::from(format!("Version: {}", version.as_ref()))) + } + (None, Some(message)) => Some(message.clone()), + (Some(version), Some(message)) => Some(SharedString::from(format!( + "Version: {}\n\n{}", + version.as_ref(), + message.as_ref() + ))), + }; menu = menu.item(ContextMenuItem::custom_entry( move |_, _| { h_flex() @@ -355,11 +381,11 @@ impl LanguageServerState { } } }, - message.map(|server_message| { + tooltip_text.map(|tooltip_text| { DocumentationAside::new( DocumentationSide::Right, - DocumentationEdge::Bottom, - Rc::new(move |_| Label::new(server_message.clone()).into_any_element()), + DocumentationEdge::Top, + Rc::new(move |_| Label::new(tooltip_text.clone()).into_any_element()), ) }), )); diff --git a/crates/language_tools/src/lsp_log_view.rs b/crates/language_tools/src/lsp_log_view.rs index 314dcc0b9bde998a0fec65b2847ae13641f0d011..2b2575912ae4543d2bf3cbd0c6b667ace7c82e91 100644 --- a/crates/language_tools/src/lsp_log_view.rs +++ b/crates/language_tools/src/lsp_log_view.rs @@ -125,7 +125,7 @@ pub fn init(on_headless_host: bool, cx: &mut App) { let server_id = server.server_id(); let weak_lsp_store = cx.weak_entity(); log_store.copilot_log_subscription = - Some(server.on_notification::( + Some(server.on_notification::( move |params, cx| { weak_lsp_store .update(cx, |lsp_store, cx| { @@ -269,7 +269,7 @@ impl LspLogView { let focus_handle = cx.focus_handle(); let focus_subscription = cx.on_focus(&focus_handle, window, |log_view, window, cx| { - window.focus(&log_view.editor.focus_handle(cx)); + window.focus(&log_view.editor.focus_handle(cx), cx); }); cx.on_release(|log_view, cx| { @@ -330,6 +330,8 @@ impl LspLogView { let server_info = format!( "* Server: {NAME} (id {ID}) +* Version: {VERSION} + * Binary: {BINARY} * Registered workspace folders: @@ -340,6 +342,12 @@ impl LspLogView { * Configuration: {CONFIGURATION}", NAME = info.status.name, ID = info.id, + VERSION = info + .status + .server_version + .as_ref() + .map(|version| version.as_ref()) + .unwrap_or("Unknown"), BINARY = info .status .binary @@ -462,7 +470,7 @@ impl LspLogView { self.editor_subscriptions = editor_subscriptions; cx.notify(); } - self.editor.read(cx).focus_handle(cx).focus(window); + self.editor.read(cx).focus_handle(cx).focus(window, cx); self.log_store.update(cx, |log_store, cx| { let state = log_store.get_language_server_state(server_id)?; state.toggled_log_kind = Some(LogKind::Logs); @@ -494,7 +502,7 @@ impl LspLogView { cx.notify(); } - self.editor.read(cx).focus_handle(cx).focus(window); + self.editor.read(cx).focus_handle(cx).focus(window, cx); } fn show_trace_for_server( @@ -528,7 +536,7 @@ impl LspLogView { }); cx.notify(); } - self.editor.read(cx).focus_handle(cx).focus(window); + self.editor.read(cx).focus_handle(cx).focus(window, cx); } fn show_rpc_trace_for_server( @@ -572,7 +580,7 @@ impl LspLogView { cx.notify(); } - self.editor.read(cx).focus_handle(cx).focus(window); + self.editor.read(cx).focus_handle(cx).focus(window, cx); } fn toggle_rpc_trace_for_server( @@ -660,7 +668,7 @@ impl LspLogView { self.editor = editor; self.editor_subscriptions = editor_subscriptions; cx.notify(); - self.editor.read(cx).focus_handle(cx).focus(window); + self.editor.read(cx).focus_handle(cx).focus(window, cx); self.log_store.update(cx, |log_store, cx| { let state = log_store.get_language_server_state(server_id)?; if let Some(log_kind) = state.toggled_log_kind.take() { @@ -1314,7 +1322,7 @@ impl LspLogToolbarItemView { log_view.show_rpc_trace_for_server(id, window, cx); cx.notify(); } - window.focus(&log_view.focus_handle); + window.focus(&log_view.focus_handle, cx); }); } cx.notify(); @@ -1334,6 +1342,7 @@ impl ServerInfo { capabilities: server.capabilities(), status: LanguageServerStatus { name: server.name(), + server_version: server.version(), pending_work: Default::default(), has_pending_diagnostic_updates: false, progress_tokens: Default::default(), diff --git a/crates/language_tools/src/syntax_tree_view.rs b/crates/language_tools/src/syntax_tree_view.rs index 0fbcdcca5eca80a01738888266389db5a678f3e8..15776e07d6d18835885ac5bafb2b29191d9e6bed 100644 --- a/crates/language_tools/src/syntax_tree_view.rs +++ b/crates/language_tools/src/syntax_tree_view.rs @@ -659,7 +659,7 @@ impl SyntaxTreeToolbarItemView { buffer_state.active_layer = Some(layer.to_owned()); view.selected_descendant_ix = None; cx.notify(); - view.focus_handle.focus(window); + view.focus_handle.focus(window, cx); Some(()) }) } diff --git a/crates/languages/src/javascript/textobjects.scm b/crates/languages/src/javascript/textobjects.scm index 1a273ddb5000ba920868272bb4ac31d270095442..eace658e6b9847bcc651deedad2bc27cbfbf6975 100644 --- a/crates/languages/src/javascript/textobjects.scm +++ b/crates/languages/src/javascript/textobjects.scm @@ -18,13 +18,47 @@ (_)* @function.inside "}")) @function.around -(arrow_function +((arrow_function body: (statement_block "{" (_)* @function.inside "}")) @function.around + (#not-has-parent? @function.around variable_declarator)) -(arrow_function) @function.around +; Arrow function in variable declaration - capture the full declaration +([ + (lexical_declaration + (variable_declarator + value: (arrow_function + body: (statement_block + "{" + (_)* @function.inside + "}")))) + (variable_declaration + (variable_declarator + value: (arrow_function + body: (statement_block + "{" + (_)* @function.inside + "}")))) +]) @function.around + +; Arrow function in variable declaration (captures body for expression-bodied arrows) +([ + (lexical_declaration + (variable_declarator + value: (arrow_function + body: (_) @function.inside))) + (variable_declaration + (variable_declarator + value: (arrow_function + body: (_) @function.inside))) +]) @function.around + +; Catch-all for arrow functions in other contexts (callbacks, etc.) +((arrow_function + body: (_) @function.inside) @function.around + (#not-has-parent? @function.around variable_declarator)) (generator_function body: (_ diff --git a/crates/languages/src/markdown/config.toml b/crates/languages/src/markdown/config.toml index 84c79d2538a0af470ec16d55fe9cf2d1ae05805b..423a4c008f6e8a64f3c4e883b0d6e2bde65c88ae 100644 --- a/crates/languages/src/markdown/config.toml +++ b/crates/languages/src/markdown/config.toml @@ -20,6 +20,9 @@ rewrap_prefixes = [ ">\\s*", "[-*+]\\s+\\[[\\sx]\\]\\s+" ] +unordered_list = ["- ", "* ", "+ "] +ordered_list = [{ pattern = "(\\d+)\\. ", format = "{1}. " }] +task_list = { prefixes = ["- [ ] ", "- [x] "], continuation = "- [ ] " } auto_indent_on_paste = false auto_indent_using_last_non_empty_line = false diff --git a/crates/languages/src/python.rs b/crates/languages/src/python.rs index 77d4be6f49a4928731d39d2154cbe4f0e38024ef..a06b1efe649b93ef56a35c40bd0d35cd1bc7ca9c 100644 --- a/crates/languages/src/python.rs +++ b/crates/languages/src/python.rs @@ -295,6 +295,23 @@ impl LspInstaller for TyLspAdapter { }) } + async fn check_if_user_installed( + &self, + delegate: &dyn LspAdapterDelegate, + _: Option, + _: &AsyncApp, + ) -> Option { + let Some(ty_bin) = delegate.which(Self::SERVER_NAME.as_ref()).await else { + return None; + }; + let env = delegate.shell_env().await; + Some(LanguageServerBinary { + path: ty_bin, + env: Some(env), + arguments: vec!["server".into()], + }) + } + async fn fetch_server_binary( &self, latest_version: Self::BinaryVersion, diff --git a/crates/languages/src/rust.rs b/crates/languages/src/rust.rs index c10f76b079bf093e71b5444934196940e7b26d6c..80bc48908b0894f251d6631b67cb4a19658454bd 100644 --- a/crates/languages/src/rust.rs +++ b/crates/languages/src/rust.rs @@ -355,7 +355,7 @@ impl LspAdapter for RustLspAdapter { | lsp::CompletionTextEdit::Edit(lsp::TextEdit { new_text, .. }), ) = completion.text_edit.as_ref() && let Ok(mut snippet) = snippet::Snippet::parse(new_text) - && !snippet.tabstops.is_empty() + && snippet.tabstops.len() > 1 { label = String::new(); @@ -421,7 +421,9 @@ impl LspAdapter for RustLspAdapter { 0..label.rfind('(').unwrap_or(completion.label.len()), highlight_id, )); - } else if detail_left.is_none() { + } else if detail_left.is_none() + && kind != Some(lsp::CompletionItemKind::SNIPPET) + { return None; } } @@ -1597,6 +1599,40 @@ mod tests { )) ); + // Postfix completion without actual tabstops (only implicit final $0) + // The label should use completion.label so it can be filtered by "ref" + let ref_completion = adapter + .label_for_completion( + &lsp::CompletionItem { + kind: Some(lsp::CompletionItemKind::SNIPPET), + label: "ref".to_string(), + filter_text: Some("ref".to_string()), + label_details: Some(CompletionItemLabelDetails { + detail: None, + description: Some("&expr".to_string()), + }), + detail: Some("&expr".to_string()), + insert_text_format: Some(lsp::InsertTextFormat::SNIPPET), + text_edit: Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit { + range: lsp::Range::default(), + new_text: "&String::new()".to_string(), + })), + ..Default::default() + }, + &language, + ) + .await; + assert!( + ref_completion.is_some(), + "ref postfix completion should have a label" + ); + let ref_label = ref_completion.unwrap(); + let filter_text = &ref_label.text[ref_label.filter_range.clone()]; + assert!( + filter_text.contains("ref"), + "filter range text '{filter_text}' should contain 'ref' for filtering to work", + ); + // Test for correct range calculation with mixed empty and non-empty tabstops.(See https://github.com/zed-industries/zed/issues/44825) let res = adapter .label_for_completion( diff --git a/crates/languages/src/tsx/textobjects.scm b/crates/languages/src/tsx/textobjects.scm index 836fed35ba1c1093b84e48a8da19d89177a69944..628a921f3ac9ea04ff59654d72caf73cebbc9071 100644 --- a/crates/languages/src/tsx/textobjects.scm +++ b/crates/languages/src/tsx/textobjects.scm @@ -18,13 +18,47 @@ (_)* @function.inside "}")) @function.around -(arrow_function +((arrow_function body: (statement_block "{" (_)* @function.inside "}")) @function.around + (#not-has-parent? @function.around variable_declarator)) -(arrow_function) @function.around +; Arrow function in variable declaration - capture the full declaration +([ + (lexical_declaration + (variable_declarator + value: (arrow_function + body: (statement_block + "{" + (_)* @function.inside + "}")))) + (variable_declaration + (variable_declarator + value: (arrow_function + body: (statement_block + "{" + (_)* @function.inside + "}")))) +]) @function.around + +; Arrow function in variable declaration (expression body fallback) +([ + (lexical_declaration + (variable_declarator + value: (arrow_function + body: (_) @function.inside))) + (variable_declaration + (variable_declarator + value: (arrow_function + body: (_) @function.inside))) +]) @function.around + +; Catch-all for arrow functions in other contexts (callbacks, etc.) +((arrow_function + body: (_) @function.inside) @function.around + (#not-has-parent? @function.around variable_declarator)) (function_signature) @function.around (generator_function diff --git a/crates/languages/src/typescript/textobjects.scm b/crates/languages/src/typescript/textobjects.scm index 836fed35ba1c1093b84e48a8da19d89177a69944..96289f058cd7b605a8f5b4c8966e3c372022d065 100644 --- a/crates/languages/src/typescript/textobjects.scm +++ b/crates/languages/src/typescript/textobjects.scm @@ -18,13 +18,48 @@ (_)* @function.inside "}")) @function.around -(arrow_function +((arrow_function body: (statement_block "{" (_)* @function.inside "}")) @function.around + (#not-has-parent? @function.around variable_declarator)) -(arrow_function) @function.around +; Arrow function in variable declaration - capture the full declaration +([ + (lexical_declaration + (variable_declarator + value: (arrow_function + body: (statement_block + "{" + (_)* @function.inside + "}")))) + (variable_declaration + (variable_declarator + value: (arrow_function + body: (statement_block + "{" + (_)* @function.inside + "}")))) +]) @function.around + +; Arrow function in variable declaration - capture body as @function.inside +; (for statement blocks, the more specific pattern above captures just the contents) +([ + (lexical_declaration + (variable_declarator + value: (arrow_function + body: (_) @function.inside))) + (variable_declaration + (variable_declarator + value: (arrow_function + body: (_) @function.inside))) +]) @function.around + +; Catch-all for arrow functions in other contexts (callbacks, etc.) +((arrow_function + body: (_) @function.inside) @function.around + (#not-has-parent? @function.around variable_declarator)) (function_signature) @function.around (generator_function diff --git a/crates/languages/src/vtsls.rs b/crates/languages/src/vtsls.rs index 3d38e022afe06f79d7e555263183a948c941f337..29b21a7cd80f1f0457e7720d68a6fb37954a02c5 100644 --- a/crates/languages/src/vtsls.rs +++ b/crates/languages/src/vtsls.rs @@ -2,13 +2,17 @@ use anyhow::Result; use async_trait::async_trait; use collections::HashMap; use gpui::AsyncApp; -use language::{LanguageName, LspAdapter, LspAdapterDelegate, LspInstaller, Toolchain}; +use language::{ + LanguageName, LspAdapter, LspAdapterDelegate, LspInstaller, PromptResponseContext, Toolchain, +}; use lsp::{CodeActionKind, LanguageServerBinary, LanguageServerName, Uri}; use node_runtime::{NodeRuntime, VersionStrategy}; use project::{Fs, lsp_store::language_server_settings}; use regex::Regex; use semver::Version; use serde_json::Value; +use serde_json::json; +use settings::update_settings_file; use std::{ ffi::OsString, path::{Path, PathBuf}, @@ -16,6 +20,11 @@ use std::{ }; use util::{ResultExt, maybe, merge_json_value_into}; +const ACTION_ALWAYS: &str = "Always"; +const ACTION_NEVER: &str = "Never"; +const UPDATE_IMPORTS_MESSAGE_PATTERN: &str = "Update imports for"; +const VTSLS_SERVER_NAME: &str = "vtsls"; + fn typescript_server_binary_arguments(server_path: &Path) -> Vec { vec![server_path.into(), "--stdio".into()] } @@ -302,6 +311,52 @@ impl LspAdapter for VtslsLspAdapter { (LanguageName::new_static("TSX"), "typescriptreact".into()), ]) } + + fn process_prompt_response(&self, context: &PromptResponseContext, cx: &mut AsyncApp) { + let selected_title = context.selected_action.title.as_str(); + let is_preference_response = + selected_title == ACTION_ALWAYS || selected_title == ACTION_NEVER; + if !is_preference_response { + return; + } + + if context.message.contains(UPDATE_IMPORTS_MESSAGE_PATTERN) { + let setting_value = match selected_title { + ACTION_ALWAYS => "always", + ACTION_NEVER => "never", + _ => return, + }; + + let settings = json!({ + "typescript": { + "updateImportsOnFileMove": { + "enabled": setting_value + } + }, + "javascript": { + "updateImportsOnFileMove": { + "enabled": setting_value + } + } + }); + + let _ = cx.update(|cx| { + update_settings_file(self.fs.clone(), cx, move |content, _| { + let lsp_settings = content + .project + .lsp + .entry(VTSLS_SERVER_NAME.into()) + .or_default(); + + if let Some(existing) = &mut lsp_settings.settings { + merge_json_value_into(settings, existing); + } else { + lsp_settings.settings = Some(settings); + } + }); + }); + } + } } async fn get_cached_ts_server_binary( diff --git a/crates/languages/src/yaml/config.toml b/crates/languages/src/yaml/config.toml index 51e8e1224a40904e0dfbb0204eb531e6b2664825..9a07a560b06766ac00dd73b6210023c4cddd491d 100644 --- a/crates/languages/src/yaml/config.toml +++ b/crates/languages/src/yaml/config.toml @@ -1,6 +1,6 @@ name = "YAML" grammar = "yaml" -path_suffixes = ["yml", "yaml", "pixi.lock", "clang-format", "clangd"] +path_suffixes = ["yml", "yaml", "pixi.lock", "clang-format", "clangd", "bst"] line_comments = ["# "] autoclose_before = ",]}" brackets = [ diff --git a/crates/lsp/src/lsp.rs b/crates/lsp/src/lsp.rs index 9ff6e245c49d771c162ca55fa98bbd7ca37d7bd0..36938f62a3048b87dd890ca6e7ca8fc2499689e4 100644 --- a/crates/lsp/src/lsp.rs +++ b/crates/lsp/src/lsp.rs @@ -89,6 +89,7 @@ pub struct LanguageServer { outbound_tx: channel::Sender, notification_tx: channel::Sender, name: LanguageServerName, + version: Option, process_name: Arc, binary: LanguageServerBinary, capabilities: RwLock, @@ -501,6 +502,7 @@ impl LanguageServer { response_handlers, io_handlers, name: server_name, + version: None, process_name: binary .path .file_name() @@ -882,7 +884,9 @@ impl LanguageServer { window: Some(WindowClientCapabilities { work_done_progress: Some(true), show_message: Some(ShowMessageRequestClientCapabilities { - message_action_item: None, + message_action_item: Some(MessageActionItemCapabilities { + additional_properties_support: Some(true), + }), }), ..WindowClientCapabilities::default() }), @@ -923,6 +927,7 @@ impl LanguageServer { ) })?; if let Some(info) = response.server_info { + self.version = info.version.map(SharedString::from); self.process_name = info.name.into(); } self.capabilities = RwLock::new(response.capabilities); @@ -1153,6 +1158,11 @@ impl LanguageServer { self.name.clone() } + /// Get the version of the running language server. + pub fn version(&self) -> Option { + self.version.clone() + } + pub fn process_name(&self) -> &str { &self.process_name } diff --git a/crates/markdown/src/markdown.rs b/crates/markdown/src/markdown.rs index 3654418e419bb58f5c9c29ac1baf7172a423156f..0bc3b9eb726e1782bafb2a31229ea21f308adc6e 100644 --- a/crates/markdown/src/markdown.rs +++ b/crates/markdown/src/markdown.rs @@ -151,6 +151,8 @@ actions!( [ /// Copies the selected text to the clipboard. Copy, + /// Copies the selected text as markdown to the clipboard. + CopyAsMarkdown ] ); @@ -295,6 +297,14 @@ impl Markdown { cx.write_to_clipboard(ClipboardItem::new_string(text)); } + fn copy_as_markdown(&self, _: &mut Window, cx: &mut Context) { + if self.selection.end <= self.selection.start { + return; + } + let text = self.source[self.selection.start..self.selection.end].to_string(); + cx.write_to_clipboard(ClipboardItem::new_string(text)); + } + fn parse(&mut self, cx: &mut Context) { if self.source.is_empty() { return; @@ -697,7 +707,7 @@ impl MarkdownElement { pending: true, mode, }; - window.focus(&markdown.focus_handle); + window.focus(&markdown.focus_handle, cx); } window.prevent_default(); @@ -1356,6 +1366,14 @@ impl Element for MarkdownElement { } } }); + window.on_action(std::any::TypeId::of::(), { + let entity = self.markdown.clone(); + move |_, phase, window, cx| { + if phase == DispatchPhase::Bubble { + entity.update(cx, move |this, cx| this.copy_as_markdown(window, cx)) + } + } + }); self.paint_mouse_listeners(hitbox, &rendered_markdown.text, window, cx); rendered_markdown.element.paint(window, cx); diff --git a/crates/markdown_preview/src/markdown_preview_view.rs b/crates/markdown_preview/src/markdown_preview_view.rs index 20613b112eeccf76ec8be12bddc49c12b600ff9b..650f369309561d76669289737277b45fb99af5ec 100644 --- a/crates/markdown_preview/src/markdown_preview_view.rs +++ b/crates/markdown_preview/src/markdown_preview_view.rs @@ -96,7 +96,7 @@ impl MarkdownPreviewView { pane.add_item(Box::new(view.clone()), false, false, None, window, cx) } }); - editor.focus_handle(cx).focus(window); + editor.focus_handle(cx).focus(window, cx); cx.notify(); } }); @@ -370,7 +370,7 @@ impl MarkdownPreviewView { cx, |selections| selections.select_ranges(vec![selection]), ); - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); }); } } diff --git a/crates/mistral/src/mistral.rs b/crates/mistral/src/mistral.rs index eca4743d0442b9ca169ac966f78af0112565fcbc..2fa8a2cedaee01daa1452ade35b20c440055b7fc 100644 --- a/crates/mistral/src/mistral.rs +++ b/crates/mistral/src/mistral.rs @@ -155,15 +155,15 @@ impl Model { pub fn max_token_count(&self) -> u64 { match self { Self::CodestralLatest => 256000, - Self::MistralLargeLatest => 131000, + Self::MistralLargeLatest => 256000, Self::MistralMediumLatest => 128000, Self::MistralSmallLatest => 32000, - Self::MagistralMediumLatest => 40000, - Self::MagistralSmallLatest => 40000, + Self::MagistralMediumLatest => 128000, + Self::MagistralSmallLatest => 128000, Self::OpenMistralNemo => 131000, Self::OpenCodestralMamba => 256000, - Self::DevstralMediumLatest => 128000, - Self::DevstralSmallLatest => 262144, + Self::DevstralMediumLatest => 256000, + Self::DevstralSmallLatest => 256000, Self::Pixtral12BLatest => 128000, Self::PixtralLargeLatest => 128000, Self::Custom { max_tokens, .. } => *max_tokens, diff --git a/crates/multi_buffer/src/multi_buffer.rs b/crates/multi_buffer/src/multi_buffer.rs index 5b343ecc5791c0f6f5f8a6d734cb79fc8226a8fa..0c0e87b60a7b8950f7461228c929503d516791e0 100644 --- a/crates/multi_buffer/src/multi_buffer.rs +++ b/crates/multi_buffer/src/multi_buffer.rs @@ -2610,9 +2610,8 @@ impl MultiBuffer { for range in ranges { let range = range.to_point(&snapshot); let start = snapshot.point_to_offset(Point::new(range.start.row, 0)); - let end = snapshot.point_to_offset(Point::new(range.end.row + 1, 0)); - let start = start.saturating_sub_usize(1); - let end = snapshot.len().min(end + 1usize); + let end = (snapshot.point_to_offset(Point::new(range.end.row + 1, 0)) + 1usize) + .min(snapshot.len()); cursor.seek(&start, Bias::Right); while let Some(item) = cursor.item() { if *cursor.start() >= end { diff --git a/crates/node_runtime/src/node_runtime.rs b/crates/node_runtime/src/node_runtime.rs index 5e4297988b665c3b89771838ef629ee87e88fb5b..eb8a5b45797baf7329554cb0b8d4a4f67a1f6579 100644 --- a/crates/node_runtime/src/node_runtime.rs +++ b/crates/node_runtime/src/node_runtime.rs @@ -9,8 +9,6 @@ use serde::Deserialize; use smol::io::BufReader; use smol::{fs, lock::Mutex}; use std::fmt::Display; -use std::future::Future; -use std::pin::Pin; use std::{ env::{self, consts}, ffi::OsString, @@ -48,7 +46,6 @@ struct NodeRuntimeState { last_options: Option, options: watch::Receiver>, shell_env_loaded: Shared>, - trust_task: Option + Send>>>, } impl NodeRuntime { @@ -56,11 +53,9 @@ impl NodeRuntime { http: Arc, shell_env_loaded: Option>, options: watch::Receiver>, - trust_task: Option + Send>>>, ) -> Self { NodeRuntime(Arc::new(Mutex::new(NodeRuntimeState { http, - trust_task, instance: None, last_options: None, options, @@ -75,15 +70,11 @@ impl NodeRuntime { last_options: None, options: watch::channel(Some(NodeBinaryOptions::default())).1, shell_env_loaded: oneshot::channel().1.shared(), - trust_task: None, }))) } async fn instance(&self) -> Box { let mut state = self.0.lock().await; - if let Some(trust_task) = state.trust_task.take() { - trust_task.await; - } let options = loop { if let Some(options) = state.options.borrow().as_ref() { diff --git a/crates/onboarding/src/onboarding.rs b/crates/onboarding/src/onboarding.rs index 66402f33d31c6e9ce5894c56872c8d92d2c4c36c..495a55411fc936d476dfa0d443e155d1fa7faecd 100644 --- a/crates/onboarding/src/onboarding.rs +++ b/crates/onboarding/src/onboarding.rs @@ -190,7 +190,7 @@ pub fn show_onboarding_view(app_state: Arc, cx: &mut App) -> Task(); - window.focus(&active_editor.focus_handle(cx)); + window.focus(&active_editor.focus_handle(cx), cx); } }); diff --git a/crates/outline_panel/src/outline_panel.rs b/crates/outline_panel/src/outline_panel.rs index 8dbf7b681d9be45bda0fd9803cbb8e2cd434e921..5a32bd73b74a9e8caade1042a381983af0da71d3 100644 --- a/crates/outline_panel/src/outline_panel.rs +++ b/crates/outline_panel/src/outline_panel.rs @@ -998,9 +998,9 @@ impl OutlinePanel { fn cancel(&mut self, _: &Cancel, window: &mut Window, cx: &mut Context) { if self.filter_editor.focus_handle(cx).is_focused(window) { - self.focus_handle.focus(window); + self.focus_handle.focus(window, cx); } else { - self.filter_editor.focus_handle(cx).focus(window); + self.filter_editor.focus_handle(cx).focus(window, cx); } if self.context_menu.is_some() { @@ -1153,9 +1153,9 @@ impl OutlinePanel { } if change_focus { - active_editor.focus_handle(cx).focus(window); + active_editor.focus_handle(cx).focus(window, cx); } else { - self.focus_handle.focus(window); + self.focus_handle.focus(window, cx); } } } @@ -1458,7 +1458,7 @@ impl OutlinePanel { Box::new(zed_actions::workspace::CopyRelativePath), ) }); - window.focus(&context_menu.focus_handle(cx)); + window.focus(&context_menu.focus_handle(cx), cx); let subscription = cx.subscribe(&context_menu, |outline_panel, _, _: &DismissEvent, cx| { outline_panel.context_menu.take(); cx.notify(); @@ -4539,7 +4539,7 @@ impl OutlinePanel { cx: &mut Context, ) { if focus { - self.focus_handle.focus(window); + self.focus_handle.focus(window, cx); } let ix = self .cached_entries diff --git a/crates/outline_panel/src/outline_panel_settings.rs b/crates/outline_panel/src/outline_panel_settings.rs index b2b1a6fe685c18853087d3eb04edeef2ceebd89f..bf73aebecc194baca0156c9cdb850ed89627e001 100644 --- a/crates/outline_panel/src/outline_panel_settings.rs +++ b/crates/outline_panel/src/outline_panel_settings.rs @@ -50,7 +50,13 @@ impl Settings for OutlinePanelSettings { dock: panel.dock.unwrap(), file_icons: panel.file_icons.unwrap(), folder_icons: panel.folder_icons.unwrap(), - git_status: panel.git_status.unwrap(), + git_status: panel.git_status.unwrap() + && content + .git + .unwrap() + .enabled + .unwrap() + .is_git_status_enabled(), indent_size: panel.indent_size.unwrap(), indent_guides: IndentGuidesSettings { show: panel.indent_guides.unwrap().show.unwrap(), diff --git a/crates/picker/src/picker.rs b/crates/picker/src/picker.rs index 3d6ae27dfa0c6b60088995de6ccc1d85b08c9428..2da40b5bf4b47651df7236b0decb25fac67a3b1b 100644 --- a/crates/picker/src/picker.rs +++ b/crates/picker/src/picker.rs @@ -384,7 +384,7 @@ impl Picker { } pub fn focus(&self, window: &mut Window, cx: &mut App) { - self.focus_handle(cx).focus(window); + self.focus_handle(cx).focus(window, cx); } /// Handles the selecting an index, and passing the change to the delegate. diff --git a/crates/project/Cargo.toml b/crates/project/Cargo.toml index f39c368218511b6ddf560dda1198ef5c06bd0a2e..0d264f9e58363f5e8d8e23dff565d512f118a8d1 100644 --- a/crates/project/Cargo.toml +++ b/crates/project/Cargo.toml @@ -40,6 +40,7 @@ clock.workspace = true collections.workspace = true context_server.workspace = true dap.workspace = true +encoding_rs.workspace = true extension.workspace = true fancy-regex.workspace = true fs.workspace = true diff --git a/crates/project/src/agent_server_store.rs b/crates/project/src/agent_server_store.rs index 287b25935676e2d5a09e92285a6cc94b81e52e13..1443e4d877d4e288fb379a02fee8a351075d8db8 100644 --- a/crates/project/src/agent_server_store.rs +++ b/crates/project/src/agent_server_store.rs @@ -460,7 +460,7 @@ impl AgentServerStore { .gemini .as_ref() .and_then(|settings| settings.ignore_system_version) - .unwrap_or(false), + .unwrap_or(true), }), ); self.external_agents.insert( diff --git a/crates/project/src/buffer_store.rs b/crates/project/src/buffer_store.rs index aea2482c83edb952f3b0dba03a510085c7c4d3f6..22106fa368904d91a5c3da4338e1a79cef7f0fd0 100644 --- a/crates/project/src/buffer_store.rs +++ b/crates/project/src/buffer_store.rs @@ -376,6 +376,8 @@ impl LocalBufferStore { let text = buffer.as_rope().clone(); let line_ending = buffer.line_ending(); + let encoding = buffer.encoding(); + let has_bom = buffer.has_bom(); let version = buffer.version(); let buffer_id = buffer.remote_id(); let file = buffer.file().cloned(); @@ -387,7 +389,7 @@ impl LocalBufferStore { } let save = worktree.update(cx, |worktree, cx| { - worktree.write_file(path, text, line_ending, cx) + worktree.write_file(path, text, line_ending, encoding, has_bom, cx) }); cx.spawn(async move |this, cx| { @@ -630,7 +632,11 @@ impl LocalBufferStore { }) .await; cx.insert_entity(reservation, |_| { - Buffer::build(text_buffer, Some(loaded.file), Capability::ReadWrite) + let mut buffer = + Buffer::build(text_buffer, Some(loaded.file), Capability::ReadWrite); + buffer.set_encoding(loaded.encoding); + buffer.set_has_bom(loaded.has_bom); + buffer })? } Err(error) if is_not_found_error(&error) => cx.new(|cx| { diff --git a/crates/project/src/context_server_store.rs b/crates/project/src/context_server_store.rs index 7d060db887b1b5d07dd4d6de9ca85297adfd0c6f..7ba46a46872ba57c758baccf9f67b0039818ee75 100644 --- a/crates/project/src/context_server_store.rs +++ b/crates/project/src/context_server_store.rs @@ -15,7 +15,6 @@ use util::{ResultExt as _, rel_path::RelPath}; use crate::{ Project, project_settings::{ContextServerSettings, ProjectSettings}, - trusted_worktrees::wait_for_workspace_trust, worktree_store::WorktreeStore, }; @@ -333,15 +332,6 @@ impl ContextServerStore { pub fn start_server(&mut self, server: Arc, cx: &mut Context) { cx.spawn(async move |this, cx| { - let wait_task = this.update(cx, |context_server_store, cx| { - context_server_store.project.update(cx, |project, cx| { - let remote_host = project.remote_connection_options(cx); - wait_for_workspace_trust(remote_host, "context servers", cx) - }) - })??; - if let Some(wait_task) = wait_task { - wait_task.await; - } let this = this.upgrade().context("Context server store dropped")?; let settings = this .update(cx, |this, _| { @@ -582,15 +572,6 @@ impl ContextServerStore { } async fn maintain_servers(this: WeakEntity, cx: &mut AsyncApp) -> Result<()> { - let wait_task = this.update(cx, |context_server_store, cx| { - context_server_store.project.update(cx, |project, cx| { - let remote_host = project.remote_connection_options(cx); - wait_for_workspace_trust(remote_host, "context servers", cx) - }) - })??; - if let Some(wait_task) = wait_task { - wait_task.await; - } let (mut configured_servers, registry, worktree_store) = this.update(cx, |this, _| { ( this.context_server_settings.clone(), diff --git a/crates/project/src/git_store.rs b/crates/project/src/git_store.rs index c73ab914b788fb92e69ea3a47db5446223098c2d..85ff38ab67f873d8197729de9577075951676597 100644 --- a/crates/project/src/git_store.rs +++ b/crates/project/src/git_store.rs @@ -5867,6 +5867,11 @@ impl Repository { self.pending_ops.edit(edits, ()); ids } + pub fn default_remote_url(&self) -> Option { + self.remote_upstream_url + .clone() + .or(self.remote_origin_url.clone()) + } } fn get_permalink_in_rust_registry_src( diff --git a/crates/project/src/lsp_store.rs b/crates/project/src/lsp_store.rs index 9b3eeebed79724196290738b51376e412ca11b22..5841be02b2db80b2fa15667833b8a3d3eec4ec11 100644 --- a/crates/project/src/lsp_store.rs +++ b/crates/project/src/lsp_store.rs @@ -128,6 +128,7 @@ use util::{ ConnectionResult, ResultExt as _, debug_panic, defer, maybe, merge_json_value_into, paths::{PathStyle, SanitizedPath}, post_inc, + redact::redact_command, rel_path::RelPath, }; @@ -577,9 +578,12 @@ impl LocalLspStore { }, }, ); - log::error!("Failed to start language server {server_name:?}: {err:?}"); + log::error!( + "Failed to start language server {server_name:?}: {}", + redact_command(&format!("{err:?}")) + ); if !log.is_empty() { - log::error!("server stderr: {log}"); + log::error!("server stderr: {}", redact_command(&log)); } None } @@ -1056,12 +1060,15 @@ impl LocalLspStore { .on_request::({ let this = lsp_store.clone(); let name = name.to_string(); + let adapter = adapter.clone(); move |params, cx| { let this = this.clone(); let name = name.to_string(); + let adapter = adapter.clone(); let mut cx = cx.clone(); async move { let actions = params.actions.unwrap_or_default(); + let message = params.message.clone(); let (tx, rx) = smol::channel::bounded(1); let request = LanguageServerPromptRequest { level: match params.typ { @@ -1082,6 +1089,14 @@ impl LocalLspStore { .is_ok(); if did_update { let response = rx.recv().await.ok(); + if let Some(ref selected_action) = response { + let context = language::PromptResponseContext { + message, + selected_action: selected_action.clone(), + }; + adapter.process_prompt_response(&context, &mut cx) + } + Ok(response) } else { Ok(None) @@ -3300,8 +3315,10 @@ impl LocalLspStore { ) .await .log_err(); - this.update(cx, |this, _| { + this.update(cx, |this, cx| { if let Some(transaction) = transaction { + cx.emit(LspStoreEvent::WorkspaceEditApplied(transaction.clone())); + this.as_local_mut() .unwrap() .last_workspace_edits_by_language_server @@ -3841,11 +3858,13 @@ pub enum LspStoreEvent { edits: Vec<(lsp::Range, Snippet)>, most_recent_edit: clock::Lamport, }, + WorkspaceEditApplied(ProjectTransaction), } #[derive(Clone, Debug, Serialize)] pub struct LanguageServerStatus { pub name: LanguageServerName, + pub server_version: Option, pub pending_work: BTreeMap, pub has_pending_diagnostic_updates: bool, pub progress_tokens: HashSet, @@ -8336,6 +8355,7 @@ impl LspStore { server_id, LanguageServerStatus { name, + server_version: None, pending_work: Default::default(), has_pending_diagnostic_updates: false, progress_tokens: Default::default(), @@ -9371,6 +9391,7 @@ impl LspStore { server_id, LanguageServerStatus { name: server_name.clone(), + server_version: None, pending_work: Default::default(), has_pending_diagnostic_updates: false, progress_tokens: Default::default(), @@ -11401,6 +11422,7 @@ impl LspStore { server_id, LanguageServerStatus { name: language_server.name(), + server_version: language_server.version(), pending_work: Default::default(), has_pending_diagnostic_updates: false, progress_tokens: Default::default(), @@ -13754,7 +13776,7 @@ impl From for CompletionDocumentation { match docs { lsp::Documentation::String(text) => { if text.lines().count() <= 1 { - CompletionDocumentation::SingleLine(text.into()) + CompletionDocumentation::SingleLine(text.trim().to_string().into()) } else { CompletionDocumentation::MultiLinePlainText(text.into()) } @@ -14346,4 +14368,22 @@ mod tests { ) ); } + + #[test] + fn test_trailing_newline_in_completion_documentation() { + let doc = lsp::Documentation::String( + "Inappropriate argument value (of correct type).\n".to_string(), + ); + let completion_doc: CompletionDocumentation = doc.into(); + assert!( + matches!(completion_doc, CompletionDocumentation::SingleLine(s) if s == "Inappropriate argument value (of correct type).") + ); + + let doc = lsp::Documentation::String(" some value \n".to_string()); + let completion_doc: CompletionDocumentation = doc.into(); + assert!(matches!( + completion_doc, + CompletionDocumentation::SingleLine(s) if s == "some value" + )); + } } diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index 9152096508b76d34fe3b2209cba94b4755b6ac67..25a19788fdb464f5f289ef3bc3513f21743e3a9a 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -65,6 +65,7 @@ use debugger::{ dap_store::{DapStore, DapStoreEvent}, session::Session, }; +use encoding_rs; pub use environment::ProjectEnvironment; #[cfg(test)] use futures::future::join_all; @@ -350,6 +351,7 @@ pub enum Event { SnippetEdit(BufferId, Vec<(lsp::Range, Snippet)>), ExpandedAllForEntry(WorktreeId, ProjectEntryId), EntryRenamed(ProjectTransaction, ProjectPath, PathBuf), + WorkspaceEditApplied(ProjectTransaction), AgentLocationChanged, } @@ -1328,7 +1330,12 @@ impl Project { cx.subscribe(&buffer_store, Self::on_buffer_store_event) .detach(); let toolchain_store = cx.new(|cx| { - ToolchainStore::remote(REMOTE_SERVER_PROJECT_ID, remote.read(cx).proto_client(), cx) + ToolchainStore::remote( + REMOTE_SERVER_PROJECT_ID, + worktree_store.clone(), + remote.read(cx).proto_client(), + cx, + ) }); let task_store = cx.new(|cx| { TaskStore::remote( @@ -3249,6 +3256,9 @@ impl Project { cx.emit(Event::SnippetEdit(*buffer_id, edits.clone())) } } + LspStoreEvent::WorkspaceEditApplied(transaction) => { + cx.emit(Event::WorkspaceEditApplied(transaction.clone())) + } } } @@ -4891,16 +4901,13 @@ impl Project { .update(|cx| TrustedWorktrees::try_get_global(cx))? .context("missing trusted worktrees")?; trusted_worktrees.update(&mut cx, |trusted_worktrees, cx| { - let mut restricted_paths = envelope + let restricted_paths = envelope .payload .worktree_ids .into_iter() .map(WorktreeId::from_proto) .map(PathTrust::Worktree) .collect::>(); - if envelope.payload.restrict_workspace { - restricted_paths.insert(PathTrust::Workspace); - } let remote_host = this .read(cx) .remote_connection_options(cx) @@ -5460,13 +5467,22 @@ impl Project { .await .context("Failed to load settings file")?; + let has_bom = file.has_bom; + let new_text = cx.read_global::(|store, cx| { store.new_text_for_update(file.text, move |settings| update(settings, cx)) })?; worktree .update(cx, |worktree, cx| { let line_ending = text::LineEnding::detect(&new_text); - worktree.write_file(rel_path.clone(), new_text.into(), line_ending, cx) + worktree.write_file( + rel_path.clone(), + new_text.into(), + line_ending, + encoding_rs::UTF_8, + has_bom, + cx, + ) })? .await .context("Failed to write settings file")?; diff --git a/crates/project/src/project_settings.rs b/crates/project/src/project_settings.rs index 6d95411681d5d350271e7071b752f27d0807f60d..633f2bbd3b40139f6355e109211d665cfd0c1e5f 100644 --- a/crates/project/src/project_settings.rs +++ b/crates/project/src/project_settings.rs @@ -332,6 +332,10 @@ impl GoToDiagnosticSeverityFilter { #[derive(Copy, Clone, Debug)] pub struct GitSettings { + /// Whether or not git integration is enabled. + /// + /// Default: true + pub enabled: GitEnabledSettings, /// Whether or not to show the git gutter. /// /// Default: tracked_files @@ -361,6 +365,18 @@ pub struct GitSettings { pub path_style: GitPathStyle, } +#[derive(Clone, Copy, Debug)] +pub struct GitEnabledSettings { + /// Whether git integration is enabled for showing git status. + /// + /// Default: true + pub status: bool, + /// Whether git integration is enabled for showing diffs. + /// + /// Default: true + pub diff: bool, +} + #[derive(Clone, Copy, Debug, PartialEq, Default)] pub enum GitPathStyle { #[default] @@ -502,7 +518,14 @@ impl Settings for ProjectSettings { let inline_diagnostics = diagnostics.inline.as_ref().unwrap(); let git = content.git.as_ref().unwrap(); + let git_enabled = { + GitEnabledSettings { + status: git.enabled.as_ref().unwrap().is_git_status_enabled(), + diff: git.enabled.as_ref().unwrap().is_git_diff_enabled(), + } + }; let git_settings = GitSettings { + enabled: git_enabled, git_gutter: git.git_gutter.unwrap(), gutter_debounce: git.gutter_debounce.unwrap_or_default(), inline_blame: { diff --git a/crates/project/src/toolchain_store.rs b/crates/project/src/toolchain_store.rs index 21b74bd784d1d9af12fe43e3fe82051afc103b0d..7afc70827f85e1a1bafcad436409936876fd3b45 100644 --- a/crates/project/src/toolchain_store.rs +++ b/crates/project/src/toolchain_store.rs @@ -32,6 +32,7 @@ use crate::{ pub struct ToolchainStore { mode: ToolchainStoreInner, user_toolchains: BTreeMap>, + worktree_store: Entity, _sub: Subscription, } @@ -66,7 +67,7 @@ impl ToolchainStore { ) -> Self { let entity = cx.new(|_| LocalToolchainStore { languages, - worktree_store, + worktree_store: worktree_store.clone(), project_environment, active_toolchains: Default::default(), manifest_tree, @@ -77,12 +78,18 @@ impl ToolchainStore { }); Self { mode: ToolchainStoreInner::Local(entity), + worktree_store, user_toolchains: Default::default(), _sub, } } - pub(super) fn remote(project_id: u64, client: AnyProtoClient, cx: &mut Context) -> Self { + pub(super) fn remote( + project_id: u64, + worktree_store: Entity, + client: AnyProtoClient, + cx: &mut Context, + ) -> Self { let entity = cx.new(|_| RemoteToolchainStore { client, project_id }); let _sub = cx.subscribe(&entity, |_, _, e: &ToolchainStoreEvent, cx| { cx.emit(e.clone()) @@ -90,6 +97,7 @@ impl ToolchainStore { Self { mode: ToolchainStoreInner::Remote(entity), user_toolchains: Default::default(), + worktree_store, _sub, } } @@ -165,12 +173,22 @@ impl ToolchainStore { language_name: LanguageName, cx: &mut Context, ) -> Task> { + let Some(worktree) = self + .worktree_store + .read(cx) + .worktree_for_id(path.worktree_id, cx) + else { + return Task::ready(None); + }; + let target_root_path = worktree.read_with(cx, |this, _| this.abs_path()); + let user_toolchains = self .user_toolchains .iter() .filter(|(scope, _)| { - if let ToolchainScope::Subproject(worktree_id, relative_path) = scope { - path.worktree_id == *worktree_id && relative_path.starts_with(&path.path) + if let ToolchainScope::Subproject(subproject_root_path, relative_path) = scope { + target_root_path == *subproject_root_path + && relative_path.starts_with(&path.path) } else { true } diff --git a/crates/project/src/trusted_worktrees.rs b/crates/project/src/trusted_worktrees.rs index 9f849ceaf1db62c1a88e269565e95bc97bc56011..0e1a8b4011bf56b150fe99a502eece905dcc9d78 100644 --- a/crates/project/src/trusted_worktrees.rs +++ b/crates/project/src/trusted_worktrees.rs @@ -27,36 +27,20 @@ //! Spawning a language server is potentially dangerous, and Zed needs to restrict that by default. //! Each single file worktree requires a separate trust permission, unless a more global level is trusted. //! -//! * "workspace" -//! -//! Even an empty Zed instance with no files or directories open is potentially dangerous: opening an Assistant Panel and creating new external agent thread might require installing and running MCP servers. -//! -//! Disabling the entire panel is possible with ai-related settings. -//! Yet when it's enabled, it's still reasonably safe to use remote AI agents and control their permissions in the Assistant Panel. -//! -//! Unlike that, MCP servers are similar to language servers and may require fetching, installing and running packages or binaries. -//! Given that those servers are not tied to any particular worktree, this level of trust is required to operate any MCP server. -//! -//! Workspace level of trust assumes all single file worktrees are trusted too, for the same host: if we allow global MCP server-related functionality, we can already allow spawning language servers for single file worktrees as well. -//! //! * "directory worktree" //! //! If a directory is open in Zed, it's a full worktree which may spawn multiple language servers associated with it. //! Each such worktree requires a separate trust permission, so each separate directory worktree has to be trusted separately, unless a more global level is trusted. //! -//! When a directory worktree is trusted and language servers are allowed to be downloaded and started, hence we also allow workspace level of trust (hence, "single file worktree" level of trust also). +//! When a directory worktree is trusted and language servers are allowed to be downloaded and started, hence, "single file worktree" level of trust also. //! //! * "path override" //! //! To ease trusting multiple directory worktrees at once, it's possible to trust a parent directory of a certain directory worktree opened in Zed. //! Trusting a directory means trusting all its subdirectories as well, including all current and potential directory worktrees. -//! -//! If we trust multiple projects to install and spawn various language server processes, we can also allow workspace trust requests for MCP servers installation and spawning. use collections::{HashMap, HashSet}; -use gpui::{ - App, AppContext as _, Context, Entity, EventEmitter, Global, SharedString, Task, WeakEntity, -}; +use gpui::{App, AppContext as _, Context, Entity, EventEmitter, Global, SharedString, WeakEntity}; use remote::RemoteConnectionOptions; use rpc::{AnyProtoClient, proto}; use settings::{Settings as _, WorktreeId}; @@ -132,57 +116,6 @@ pub fn track_worktree_trust( } } -/// Waits until at least [`PathTrust::Workspace`] level of trust is granted for the host the [`TrustedWorktrees`] was initialized with. -pub fn wait_for_default_workspace_trust( - what_waits: &'static str, - cx: &mut App, -) -> Option> { - let trusted_worktrees = TrustedWorktrees::try_get_global(cx)?; - wait_for_workspace_trust( - trusted_worktrees.read(cx).remote_host.clone(), - what_waits, - cx, - ) -} - -/// Waits until at least [`PathTrust::Workspace`] level of trust is granted for a particular host. -pub fn wait_for_workspace_trust( - remote_host: Option>, - what_waits: &'static str, - cx: &mut App, -) -> Option> { - let trusted_worktrees = TrustedWorktrees::try_get_global(cx)?; - let remote_host = remote_host.map(|host| host.into()); - - let remote_host = if trusted_worktrees.update(cx, |trusted_worktrees, cx| { - trusted_worktrees.can_trust_workspace(remote_host.clone(), cx) - }) { - None - } else { - Some(remote_host) - }?; - - Some(cx.spawn(async move |cx| { - log::info!("Waiting for workspace to be trusted before starting {what_waits}"); - let (tx, restricted_worktrees_task) = smol::channel::bounded::<()>(1); - let Ok(_subscription) = cx.update(|cx| { - cx.subscribe(&trusted_worktrees, move |_, e, _| { - if let TrustedWorktreesEvent::Trusted(trusted_host, trusted_paths) = e { - if trusted_host == &remote_host && trusted_paths.contains(&PathTrust::Workspace) - { - log::info!("Workspace is trusted for {what_waits}"); - tx.send_blocking(()).ok(); - } - } - }) - }) else { - return; - }; - - restricted_worktrees_task.recv().await.ok(); - })) -} - /// A collection of worktree trust metadata, can be accessed globally (if initialized) and subscribed to. pub struct TrustedWorktrees(Entity); @@ -205,8 +138,6 @@ pub struct TrustedWorktreesStore { worktree_stores: HashMap, Option>, trusted_paths: TrustedPaths, restricted: HashSet, - remote_host: Option, - restricted_workspaces: HashSet>, } /// An identifier of a host to split the trust questions by. @@ -246,9 +177,6 @@ impl From for RemoteHostLocation { /// See module-level documentation on the trust model. #[derive(Debug, PartialEq, Eq, Clone, Hash)] pub enum PathTrust { - /// General, no worktrees or files open case. - /// E.g. MCP servers can be spawned from a blank Zed instance, but will do `npm i` and other potentially malicious actions. - Workspace, /// A worktree that is familiar to this workspace. /// Either a single file or a directory worktree. Worktree(WorktreeId), @@ -260,9 +188,6 @@ pub enum PathTrust { impl PathTrust { fn to_proto(&self) -> proto::PathTrust { match self { - Self::Workspace => proto::PathTrust { - content: Some(proto::path_trust::Content::Workspace(0)), - }, Self::Worktree(worktree_id) => proto::PathTrust { content: Some(proto::path_trust::Content::WorktreeId( worktree_id.to_proto(), @@ -282,7 +207,6 @@ impl PathTrust { Self::Worktree(WorktreeId::from_proto(id)) } proto::path_trust::Content::AbsPath(path) => Self::AbsPath(PathBuf::from(path)), - proto::path_trust::Content::Workspace(_) => Self::Workspace, }) } } @@ -322,9 +246,7 @@ impl TrustedWorktreesStore { } let worktree_stores = match worktree_store { - Some(worktree_store) => { - HashMap::from_iter([(worktree_store.downgrade(), remote_host.clone())]) - } + Some(worktree_store) => HashMap::from_iter([(worktree_store.downgrade(), remote_host)]), None => HashMap::default(), }; @@ -332,8 +254,6 @@ impl TrustedWorktreesStore { trusted_paths, downstream_client, upstream_client, - remote_host, - restricted_workspaces: HashSet::default(), restricted: HashSet::default(), worktree_stores, } @@ -345,11 +265,9 @@ impl TrustedWorktreesStore { worktree_store: &Entity, cx: &App, ) -> bool { - let Some(remote_host) = self.worktree_stores.get(&worktree_store.downgrade()) else { - return false; - }; - self.restricted_workspaces.contains(remote_host) - || self.restricted.iter().any(|restricted_worktree| { + self.worktree_stores + .contains_key(&worktree_store.downgrade()) + && self.restricted.iter().any(|restricted_worktree| { worktree_store .read(cx) .worktree_for_id(*restricted_worktree, cx) @@ -366,7 +284,6 @@ impl TrustedWorktreesStore { remote_host: Option, cx: &mut Context, ) { - let mut new_workspace_trusted = false; let mut new_trusted_single_file_worktrees = HashSet::default(); let mut new_trusted_other_worktrees = HashSet::default(); let mut new_trusted_abs_paths = HashSet::default(); @@ -377,7 +294,6 @@ impl TrustedWorktreesStore { .flat_map(|current_trusted| current_trusted.iter()), ) { match trusted_path { - PathTrust::Workspace => new_workspace_trusted = true, PathTrust::Worktree(worktree_id) => { self.restricted.remove(worktree_id); if let Some((abs_path, is_file, host)) = @@ -388,13 +304,11 @@ impl TrustedWorktreesStore { new_trusted_single_file_worktrees.insert(*worktree_id); } else { new_trusted_other_worktrees.insert((abs_path, *worktree_id)); - new_workspace_trusted = true; } } } } PathTrust::AbsPath(path) => { - new_workspace_trusted = true; debug_assert!( path.is_absolute(), "Cannot trust non-absolute path {path:?}" @@ -404,11 +318,6 @@ impl TrustedWorktreesStore { } } - if new_workspace_trusted { - new_trusted_single_file_worktrees.clear(); - self.restricted_workspaces.remove(&remote_host); - trusted_paths.insert(PathTrust::Workspace); - } new_trusted_other_worktrees.retain(|(worktree_abs_path, _)| { new_trusted_abs_paths .iter() @@ -428,8 +337,7 @@ impl TrustedWorktreesStore { if restricted_host != remote_host { return true; } - let retain = (!is_file - || (!new_workspace_trusted && new_trusted_other_worktrees.is_empty())) + let retain = (!is_file || new_trusted_other_worktrees.is_empty()) && new_trusted_abs_paths.iter().all(|new_trusted_path| { !restricted_worktree_path.starts_with(new_trusted_path) }); @@ -453,9 +361,6 @@ impl TrustedWorktreesStore { .into_iter() .map(PathTrust::Worktree), ); - if trusted_paths.is_empty() && new_workspace_trusted { - trusted_paths.insert(PathTrust::Workspace); - } } cx.emit(TrustedWorktreesEvent::Trusted( @@ -489,13 +394,6 @@ impl TrustedWorktreesStore { ) { for restricted_path in restricted_paths { match restricted_path { - PathTrust::Workspace => { - self.restricted_workspaces.insert(remote_host.clone()); - cx.emit(TrustedWorktreesEvent::Restricted( - remote_host.clone(), - HashSet::from_iter([PathTrust::Workspace]), - )); - } PathTrust::Worktree(worktree_id) => { self.restricted.insert(worktree_id); cx.emit(TrustedWorktreesEvent::Restricted( @@ -568,7 +466,6 @@ impl TrustedWorktreesStore { downstream_client .send(proto::RestrictWorktrees { project_id: *downstream_project_id, - restrict_workspace: false, worktree_ids: vec![worktree_id.to_proto()], }) .ok(); @@ -577,7 +474,6 @@ impl TrustedWorktreesStore { upstream_client .send(proto::RestrictWorktrees { project_id: *upstream_project_id, - restrict_workspace: false, worktree_ids: vec![worktree_id.to_proto()], }) .ok(); @@ -585,61 +481,12 @@ impl TrustedWorktreesStore { false } - /// Checks whether a certain worktree is trusted globally (or on a larger trust level). - /// If not, emits [`TrustedWorktreesEvent::Restricted`] event if checked for the first time and not trusted. - /// - /// No events or data adjustment happens when `trust_all_worktrees` auto trust is enabled. - pub fn can_trust_workspace( - &mut self, - remote_host: Option, - cx: &mut Context, - ) -> bool { - if ProjectSettings::get_global(cx).session.trust_all_worktrees { - return true; - } - if self.restricted_workspaces.contains(&remote_host) { - return false; - } - if self.trusted_paths.contains_key(&remote_host) { - return true; - } - - self.restricted_workspaces.insert(remote_host.clone()); - cx.emit(TrustedWorktreesEvent::Restricted( - remote_host.clone(), - HashSet::from_iter([PathTrust::Workspace]), - )); - - if remote_host == self.remote_host { - if let Some((downstream_client, downstream_project_id)) = &self.downstream_client { - downstream_client - .send(proto::RestrictWorktrees { - project_id: *downstream_project_id, - restrict_workspace: true, - worktree_ids: Vec::new(), - }) - .ok(); - } - if let Some((upstream_client, upstream_project_id)) = &self.upstream_client { - upstream_client - .send(proto::RestrictWorktrees { - project_id: *upstream_project_id, - restrict_workspace: true, - worktree_ids: Vec::new(), - }) - .ok(); - } - } - false - } - - /// Lists all explicitly restricted worktrees (via [`TrustedWorktreesStore::can_trust`] and [`TrustedWorktreesStore::can_trust_workspace`] method calls) for a particular worktree store on a particular host. + /// Lists all explicitly restricted worktrees (via [`TrustedWorktreesStore::can_trust`] method calls) for a particular worktree store on a particular host. pub fn restricted_worktrees( &self, worktree_store: &WorktreeStore, - remote_host: Option, cx: &App, - ) -> HashSet)>> { + ) -> HashSet<(WorktreeId, Arc)> { let mut single_file_paths = HashSet::default(); let other_paths = self .restricted @@ -649,19 +496,16 @@ impl TrustedWorktreesStore { let worktree = worktree.read(cx); let abs_path = worktree.abs_path(); if worktree.is_single_file() { - single_file_paths.insert(Some((restricted_worktree_id, abs_path))); + single_file_paths.insert((restricted_worktree_id, abs_path)); None } else { Some((restricted_worktree_id, abs_path)) } }) - .map(Some) .collect::>(); if !other_paths.is_empty() { return other_paths; - } else if self.restricted_workspaces.contains(&remote_host) { - return HashSet::from_iter([None]); } else { single_file_paths } @@ -670,7 +514,7 @@ impl TrustedWorktreesStore { /// Switches the "trust nothing" mode to "automatically trust everything". /// This does not influence already persisted data, but stops adding new worktrees there. pub fn auto_trust_all(&mut self, cx: &mut Context) { - for (remote_host, mut worktrees) in std::mem::take(&mut self.restricted) + for (remote_host, worktrees) in std::mem::take(&mut self.restricted) .into_iter() .flat_map(|restricted_worktree| { let (_, _, host) = self.find_worktree_data(restricted_worktree, cx)?; @@ -683,26 +527,15 @@ impl TrustedWorktreesStore { acc }) { - if self.restricted_workspaces.remove(&remote_host) { - worktrees.insert(PathTrust::Workspace); - } self.trust(worktrees, remote_host, cx); } - - for remote_host in std::mem::take(&mut self.restricted_workspaces) { - self.trust(HashSet::from_iter([PathTrust::Workspace]), remote_host, cx); - } } /// Returns a normalized representation of the trusted paths to store in the DB. pub fn trusted_paths_for_serialization( &mut self, cx: &mut Context, - ) -> ( - HashSet>, - HashMap, HashSet>, - ) { - let mut new_trusted_workspaces = HashSet::default(); + ) -> HashMap, HashSet> { let new_trusted_worktrees = self .trusted_paths .clone() @@ -715,16 +548,12 @@ impl TrustedWorktreesStore { .find_worktree_data(worktree_id, cx) .map(|(abs_path, ..)| abs_path.to_path_buf()), PathTrust::AbsPath(abs_path) => Some(abs_path), - PathTrust::Workspace => { - new_trusted_workspaces.insert(host.clone()); - None - } }) .collect(); (host, abs_paths) }) .collect(); - (new_trusted_workspaces, new_trusted_worktrees) + new_trusted_worktrees } fn find_worktree_data( @@ -888,15 +717,9 @@ mod tests { assert!(has_restricted, "should have restricted worktrees"); let restricted = worktree_store.read_with(cx, |ws, cx| { - trusted_worktrees - .read(cx) - .restricted_worktrees(ws, None, cx) + trusted_worktrees.read(cx).restricted_worktrees(ws, cx) }); - assert!( - restricted - .iter() - .any(|r| r.as_ref().map(|(id, _)| *id) == Some(worktree_id)) - ); + assert!(restricted.iter().any(|(id, _)| *id == worktree_id)); events.borrow_mut().clear(); @@ -941,9 +764,7 @@ mod tests { ); let restricted_after = worktree_store.read_with(cx, |ws, cx| { - trusted_worktrees - .read(cx) - .restricted_worktrees(ws, None, cx) + trusted_worktrees.read(cx).restricted_worktrees(ws, cx) }); assert!( restricted_after.is_empty(), @@ -951,92 +772,6 @@ mod tests { ); } - #[gpui::test] - async fn test_workspace_trust_no_worktrees(cx: &mut TestAppContext) { - init_test(cx); - - let fs = FakeFs::new(cx.executor()); - fs.insert_tree(path!("/root"), json!({})).await; - - let project = Project::test(fs, Vec::<&Path>::new(), cx).await; - let worktree_store = project.read_with(cx, |project, _| project.worktree_store()); - - let trusted_worktrees = init_trust_global(worktree_store, cx); - - let events: Rc>> = Rc::default(); - cx.update({ - let events = events.clone(); - |cx| { - cx.subscribe(&trusted_worktrees, move |_, event, _| { - events.borrow_mut().push(match event { - TrustedWorktreesEvent::Trusted(host, paths) => { - TrustedWorktreesEvent::Trusted(host.clone(), paths.clone()) - } - TrustedWorktreesEvent::Restricted(host, paths) => { - TrustedWorktreesEvent::Restricted(host.clone(), paths.clone()) - } - }); - }) - } - }) - .detach(); - - let can_trust_workspace = - trusted_worktrees.update(cx, |store, cx| store.can_trust_workspace(None, cx)); - assert!( - !can_trust_workspace, - "workspace should be restricted by default" - ); - - { - let events = events.borrow(); - assert_eq!(events.len(), 1); - match &events[0] { - TrustedWorktreesEvent::Restricted(host, paths) => { - assert!(host.is_none()); - assert!(paths.contains(&PathTrust::Workspace)); - } - _ => panic!("expected Restricted event"), - } - } - - events.borrow_mut().clear(); - - let can_trust_workspace_again = - trusted_worktrees.update(cx, |store, cx| store.can_trust_workspace(None, cx)); - assert!( - !can_trust_workspace_again, - "workspace should still be restricted" - ); - assert!( - events.borrow().is_empty(), - "no duplicate Restricted event on repeated can_trust_workspace" - ); - - trusted_worktrees.update(cx, |store, cx| { - store.trust(HashSet::from_iter([PathTrust::Workspace]), None, cx); - }); - - { - let events = events.borrow(); - assert_eq!(events.len(), 1); - match &events[0] { - TrustedWorktreesEvent::Trusted(host, paths) => { - assert!(host.is_none()); - assert!(paths.contains(&PathTrust::Workspace)); - } - _ => panic!("expected Trusted event"), - } - } - - let can_trust_workspace_after = - trusted_worktrees.update(cx, |store, cx| store.can_trust_workspace(None, cx)); - assert!( - can_trust_workspace_after, - "workspace should be trusted after trust()" - ); - } - #[gpui::test] async fn test_single_file_worktree_trust(cx: &mut TestAppContext) { init_test(cx); @@ -1122,58 +857,6 @@ mod tests { ); } - #[gpui::test] - async fn test_workspace_trust_unlocks_single_file_worktree(cx: &mut TestAppContext) { - init_test(cx); - - let fs = FakeFs::new(cx.executor()); - fs.insert_tree(path!("/root"), json!({ "foo.rs": "fn foo() {}" })) - .await; - - let project = Project::test(fs, [path!("/root/foo.rs").as_ref()], cx).await; - let worktree_store = project.read_with(cx, |project, _| project.worktree_store()); - let worktree_id = worktree_store.read_with(cx, |store, cx| { - let worktree = store.worktrees().next().unwrap(); - let worktree = worktree.read(cx); - assert!(worktree.is_single_file(), "expected single-file worktree"); - worktree.id() - }); - - let trusted_worktrees = init_trust_global(worktree_store, cx); - - let can_trust_workspace = - trusted_worktrees.update(cx, |store, cx| store.can_trust_workspace(None, cx)); - assert!( - !can_trust_workspace, - "workspace should be restricted by default" - ); - - let can_trust_file = - trusted_worktrees.update(cx, |store, cx| store.can_trust(worktree_id, cx)); - assert!( - !can_trust_file, - "single-file worktree should be restricted by default" - ); - - trusted_worktrees.update(cx, |store, cx| { - store.trust(HashSet::from_iter([PathTrust::Workspace]), None, cx); - }); - - let can_trust_workspace_after = - trusted_worktrees.update(cx, |store, cx| store.can_trust_workspace(None, cx)); - assert!( - can_trust_workspace_after, - "workspace should be trusted after trust(Workspace)" - ); - - let can_trust_file_after = - trusted_worktrees.update(cx, |store, cx| store.can_trust(worktree_id, cx)); - assert!( - can_trust_file_after, - "single-file worktree should be trusted after workspace trust" - ); - } - #[gpui::test] async fn test_multiple_single_file_worktrees_trust_one(cx: &mut TestAppContext) { init_test(cx); @@ -1319,47 +1002,6 @@ mod tests { assert!(can_trust_b, "project_b should now be trusted"); } - #[gpui::test] - async fn test_directory_worktree_trust_enables_workspace(cx: &mut TestAppContext) { - init_test(cx); - - let fs = FakeFs::new(cx.executor()); - fs.insert_tree(path!("/root"), json!({ "main.rs": "fn main() {}" })) - .await; - - let project = Project::test(fs, [path!("/root").as_ref()], cx).await; - let worktree_store = project.read_with(cx, |project, _| project.worktree_store()); - let worktree_id = worktree_store.read_with(cx, |store, cx| { - let worktree = store.worktrees().next().unwrap(); - assert!(!worktree.read(cx).is_single_file()); - worktree.read(cx).id() - }); - - let trusted_worktrees = init_trust_global(worktree_store, cx); - - let can_trust_workspace = - trusted_worktrees.update(cx, |store, cx| store.can_trust_workspace(None, cx)); - assert!( - !can_trust_workspace, - "workspace should be restricted initially" - ); - - trusted_worktrees.update(cx, |store, cx| { - store.trust( - HashSet::from_iter([PathTrust::Worktree(worktree_id)]), - None, - cx, - ); - }); - - let can_trust_workspace_after = - trusted_worktrees.update(cx, |store, cx| store.can_trust_workspace(None, cx)); - assert!( - can_trust_workspace_after, - "workspace should be trusted after trusting directory worktree" - ); - } - #[gpui::test] async fn test_directory_worktree_trust_enables_single_file(cx: &mut TestAppContext) { init_test(cx); @@ -1428,7 +1070,7 @@ mod tests { let fs = FakeFs::new(cx.executor()); fs.insert_tree( - path!("/workspace"), + path!("/root"), json!({ "project_a": { "main.rs": "fn main() {}" }, "project_b": { "lib.rs": "pub fn lib() {}" } @@ -1439,8 +1081,8 @@ mod tests { let project = Project::test( fs, [ - path!("/workspace/project_a").as_ref(), - path!("/workspace/project_b").as_ref(), + path!("/root/project_a").as_ref(), + path!("/root/project_b").as_ref(), ], cx, ) @@ -1464,7 +1106,7 @@ mod tests { trusted_worktrees.update(cx, |store, cx| { store.trust( - HashSet::from_iter([PathTrust::AbsPath(PathBuf::from(path!("/workspace")))]), + HashSet::from_iter([PathTrust::AbsPath(PathBuf::from(path!("/root")))]), None, cx, ); @@ -1539,12 +1181,6 @@ mod tests { trusted_worktrees.update(cx, |store, cx| store.can_trust(worktree_id, cx)); assert!(!can_trust, "worktree should be restricted initially"); } - let can_trust_workspace = - trusted_worktrees.update(cx, |store, cx| store.can_trust_workspace(None, cx)); - assert!( - !can_trust_workspace, - "workspace should be restricted initially" - ); let has_restricted = trusted_worktrees.read_with(cx, |store, cx| { store.has_restricted_worktrees(&worktree_store, cx) @@ -1566,13 +1202,6 @@ mod tests { ); } - let can_trust_workspace = - trusted_worktrees.update(cx, |store, cx| store.can_trust_workspace(None, cx)); - assert!( - can_trust_workspace, - "workspace should be trusted after auto_trust_all" - ); - let has_restricted_after = trusted_worktrees.read_with(cx, |store, cx| { store.has_restricted_worktrees(&worktree_store, cx) }); @@ -1592,100 +1221,6 @@ mod tests { ); } - #[gpui::test] - async fn test_wait_for_global_trust_already_trusted(cx: &mut TestAppContext) { - init_test(cx); - - let fs = FakeFs::new(cx.executor()); - fs.insert_tree(path!("/root"), json!({ "main.rs": "fn main() {}" })) - .await; - - let project = Project::test(fs, [path!("/root").as_ref()], cx).await; - let worktree_store = project.read_with(cx, |project, _| project.worktree_store()); - - let trusted_worktrees = init_trust_global(worktree_store, cx); - - trusted_worktrees.update(cx, |store, cx| { - store.trust(HashSet::from_iter([PathTrust::Workspace]), None, cx); - }); - - let task = cx.update(|cx| wait_for_workspace_trust(None::, "test", cx)); - assert!(task.is_none(), "should return None when already trusted"); - } - - #[gpui::test] - async fn test_wait_for_workspace_trust_resolves_on_trust(cx: &mut TestAppContext) { - init_test(cx); - - let fs = FakeFs::new(cx.executor()); - fs.insert_tree(path!("/root"), json!({ "main.rs": "fn main() {}" })) - .await; - - let project = Project::test(fs, [path!("/root").as_ref()], cx).await; - let worktree_store = project.read_with(cx, |project, _| project.worktree_store()); - - let trusted_worktrees = init_trust_global(worktree_store, cx); - - let task = cx.update(|cx| wait_for_workspace_trust(None::, "test", cx)); - assert!( - task.is_some(), - "should return Some(Task) when not yet trusted" - ); - - let task = task.unwrap(); - - cx.executor().run_until_parked(); - - trusted_worktrees.update(cx, |store, cx| { - store.trust(HashSet::from_iter([PathTrust::Workspace]), None, cx); - }); - - cx.executor().run_until_parked(); - task.await; - } - - #[gpui::test] - async fn test_wait_for_default_workspace_trust_resolves_on_directory_worktree_trust( - cx: &mut TestAppContext, - ) { - init_test(cx); - - let fs = FakeFs::new(cx.executor()); - fs.insert_tree(path!("/root"), json!({ "main.rs": "fn main() {}" })) - .await; - - let project = Project::test(fs, [path!("/root").as_ref()], cx).await; - let worktree_store = project.read_with(cx, |project, _| project.worktree_store()); - let worktree_id = worktree_store.read_with(cx, |store, cx| { - let worktree = store.worktrees().next().unwrap(); - assert!(!worktree.read(cx).is_single_file()); - worktree.read(cx).id() - }); - - let trusted_worktrees = init_trust_global(worktree_store, cx); - - let task = cx.update(|cx| wait_for_default_workspace_trust("test", cx)); - assert!( - task.is_some(), - "should return Some(Task) when not yet trusted" - ); - - let task = task.unwrap(); - - cx.executor().run_until_parked(); - - trusted_worktrees.update(cx, |store, cx| { - store.trust( - HashSet::from_iter([PathTrust::Worktree(worktree_id)]), - None, - cx, - ); - }); - - cx.executor().run_until_parked(); - task.await; - } - #[gpui::test] async fn test_trust_restrict_trust_cycle(cx: &mut TestAppContext) { init_test(cx); @@ -1820,36 +1355,11 @@ mod tests { let trusted_worktrees = init_trust_global(worktree_store, cx); let host_a: Option = None; - let host_b = Some(RemoteHostLocation { - user_name: Some("user".into()), - host_identifier: "remote-host".into(), - }); let can_trust_local = trusted_worktrees.update(cx, |store, cx| store.can_trust(local_worktree, cx)); assert!(!can_trust_local, "local worktree restricted on host_a"); - trusted_worktrees.update(cx, |store, cx| { - store.trust( - HashSet::from_iter([PathTrust::Workspace]), - host_b.clone(), - cx, - ); - }); - - let can_trust_workspace_a = trusted_worktrees.update(cx, |store, cx| { - store.can_trust_workspace(host_a.clone(), cx) - }); - assert!( - !can_trust_workspace_a, - "host_a workspace should still be restricted" - ); - - let can_trust_workspace_b = trusted_worktrees.update(cx, |store, cx| { - store.can_trust_workspace(host_b.clone(), cx) - }); - assert!(can_trust_workspace_b, "host_b workspace should be trusted"); - trusted_worktrees.update(cx, |store, cx| { store.trust( HashSet::from_iter([PathTrust::Worktree(local_worktree)]), @@ -1864,13 +1374,5 @@ mod tests { can_trust_local_after, "local worktree should be trusted on host_a" ); - - let can_trust_workspace_a_after = trusted_worktrees.update(cx, |store, cx| { - store.can_trust_workspace(host_a.clone(), cx) - }); - assert!( - can_trust_workspace_a_after, - "host_a workspace should be trusted after directory trust" - ); } } diff --git a/crates/project/src/x.py b/crates/project/src/x.py new file mode 100644 index 0000000000000000000000000000000000000000..58947a58a41bcc2e2f8c2046b5e1c8c38c0fbbb8 --- /dev/null +++ b/crates/project/src/x.py @@ -0,0 +1 @@ +Gliwice makerspace \ No newline at end of file diff --git a/crates/project_benchmarks/src/main.rs b/crates/project_benchmarks/src/main.rs index 03c25bc464af06793e351f27588b023ec8eb3eb9..e4ddbb6cf2c7b6984df2533963bdf6bf88eacba0 100644 --- a/crates/project_benchmarks/src/main.rs +++ b/crates/project_benchmarks/src/main.rs @@ -62,7 +62,7 @@ fn main() -> Result<(), anyhow::Error> { let client = Client::production(cx); let http_client = FakeHttpClient::with_200_response(); let (_, rx) = watch::channel(None); - let node = NodeRuntime::new(http_client, None, rx, None); + let node = NodeRuntime::new(http_client, None, rx); let user_store = cx.new(|cx| UserStore::new(client.clone(), cx)); let registry = Arc::new(LanguageRegistry::new(cx.background_executor().clone())); let fs = Arc::new(RealFs::new(None, cx.background_executor().clone())); diff --git a/crates/project_panel/Cargo.toml b/crates/project_panel/Cargo.toml index 2c47efd0b0e2490bbfd6125069fa5ca1438ffb51..0385c3789e923da95a1eca7a5a469bad00020639 100644 --- a/crates/project_panel/Cargo.toml +++ b/crates/project_panel/Cargo.toml @@ -45,6 +45,7 @@ workspace.workspace = true language.workspace = true zed_actions.workspace = true telemetry.workspace = true +notifications.workspace = true [dev-dependencies] client = { workspace = true, features = ["test-support"] } diff --git a/crates/project_panel/src/project_panel.rs b/crates/project_panel/src/project_panel.rs index ea667ecbb479ca347914ee11ec789a14f29cf474..43f63d90789a65bce54814f3adbc6f1d53235568 100644 --- a/crates/project_panel/src/project_panel.rs +++ b/crates/project_panel/src/project_panel.rs @@ -29,6 +29,7 @@ use gpui::{ }; use language::DiagnosticSeverity; use menu::{Confirm, SelectFirst, SelectLast, SelectNext, SelectPrevious}; +use notifications::status_toast::{StatusToast, ToastIcon}; use project::{ Entry, EntryKind, Fs, GitEntry, GitEntryRef, GitTraversal, Project, ProjectEntryId, ProjectPath, Worktree, WorktreeId, @@ -880,7 +881,7 @@ impl ProjectPanel { }); if !focus_opened_item { let focus_handle = project_panel.read(cx).focus_handle.clone(); - window.focus(&focus_handle); + window.focus(&focus_handle, cx); } } } @@ -1140,6 +1141,12 @@ impl ProjectPanel { "Copy Relative Path", Box::new(zed_actions::workspace::CopyRelativePath), ) + .when(!is_dir && self.has_git_changes(entry_id), |menu| { + menu.separator().action( + "Restore File", + Box::new(git::RestoreFile { skip_prompt: false }), + ) + }) .when(has_git_repo, |menu| { menu.separator() .action("View File History", Box::new(git::FileHistory)) @@ -1169,7 +1176,7 @@ impl ProjectPanel { }) }); - window.focus(&context_menu.focus_handle(cx)); + window.focus(&context_menu.focus_handle(cx), cx); let subscription = cx.subscribe(&context_menu, |this, _, _: &DismissEvent, cx| { this.context_menu.take(); cx.notify(); @@ -1180,6 +1187,19 @@ impl ProjectPanel { cx.notify(); } + fn has_git_changes(&self, entry_id: ProjectEntryId) -> bool { + for visible in &self.state.visible_entries { + if let Some(git_entry) = visible.entries.iter().find(|e| e.id == entry_id) { + let total_modified = + git_entry.git_summary.index.modified + git_entry.git_summary.worktree.modified; + let total_deleted = + git_entry.git_summary.index.deleted + git_entry.git_summary.worktree.deleted; + return total_modified > 0 || total_deleted > 0; + } + } + false + } + fn is_unfoldable(&self, entry: &Entry, worktree: &Worktree) -> bool { if !entry.is_dir() || self.state.unfolded_dir_ids.contains(&entry.id) { return false; @@ -1376,7 +1396,7 @@ impl ProjectPanel { } }); self.update_visible_entries(Some((worktree_id, entry_id)), false, false, window, cx); - window.focus(&self.focus_handle); + window.focus(&self.focus_handle, cx); cx.notify(); } } @@ -1399,7 +1419,7 @@ impl ProjectPanel { } } self.update_visible_entries(Some((worktree_id, entry_id)), false, false, window, cx); - window.focus(&self.focus_handle); + window.focus(&self.focus_handle, cx); cx.notify(); } } @@ -1719,7 +1739,7 @@ impl ProjectPanel { }; if let Some(existing) = worktree.read(cx).entry_for_path(&new_path) { if existing.id == entry.id && refocus { - window.focus(&self.focus_handle); + window.focus(&self.focus_handle, cx); } return None; } @@ -1730,7 +1750,7 @@ impl ProjectPanel { }; if refocus { - window.focus(&self.focus_handle); + window.focus(&self.focus_handle, cx); } edit_state.processing_filename = Some(filename); cx.notify(); @@ -1839,7 +1859,7 @@ impl ProjectPanel { self.autoscroll(cx); } - window.focus(&self.focus_handle); + window.focus(&self.focus_handle, cx); cx.notify(); } @@ -2041,6 +2061,100 @@ impl ProjectPanel { self.remove(false, action.skip_prompt, window, cx); } + fn restore_file( + &mut self, + action: &git::RestoreFile, + window: &mut Window, + cx: &mut Context, + ) { + maybe!({ + let selection = self.state.selection?; + let project = self.project.read(cx); + + let (_worktree, entry) = self.selected_sub_entry(cx)?; + if entry.is_dir() { + return None; + } + + let project_path = project.path_for_entry(selection.entry_id, cx)?; + + let git_store = project.git_store(); + let (repository, repo_path) = git_store + .read(cx) + .repository_and_path_for_project_path(&project_path, cx)?; + + let snapshot = repository.read(cx).snapshot(); + let status = snapshot.status_for_path(&repo_path)?; + if !status.status.is_modified() && !status.status.is_deleted() { + return None; + } + + let file_name = entry.path.file_name()?.to_string(); + + let answer = if !action.skip_prompt { + let prompt = format!("Discard changes to {}?", file_name); + Some(window.prompt(PromptLevel::Info, &prompt, None, &["Restore", "Cancel"], cx)) + } else { + None + }; + + cx.spawn_in(window, async move |panel, cx| { + if let Some(answer) = answer + && answer.await != Ok(0) + { + return anyhow::Ok(()); + } + + let task = panel.update(cx, |_panel, cx| { + repository.update(cx, |repo, cx| { + repo.checkout_files("HEAD", vec![repo_path], cx) + }) + })?; + + if let Err(e) = task.await { + panel + .update(cx, |panel, cx| { + let message = format!("Failed to restore {}: {}", file_name, e); + let toast = StatusToast::new(message, cx, |this, _| { + this.icon(ToastIcon::new(IconName::XCircle).color(Color::Error)) + .dismiss_button(true) + }); + panel + .workspace + .update(cx, |workspace, cx| { + workspace.toggle_status_toast(toast, cx); + }) + .ok(); + }) + .ok(); + } + + panel + .update(cx, |panel, cx| { + panel.project.update(cx, |project, cx| { + if let Some(buffer_id) = project + .buffer_store() + .read(cx) + .buffer_id_for_project_path(&project_path) + { + if let Some(buffer) = project.buffer_for_id(*buffer_id, cx) { + buffer.update(cx, |buffer, cx| { + let _ = buffer.reload(cx); + }); + } + } + }) + }) + .ok(); + + anyhow::Ok(()) + }) + .detach_and_log_err(cx); + + Some(()) + }); + } + fn remove( &mut self, trash: bool, @@ -3616,7 +3730,7 @@ impl ProjectPanel { if this.update_visible_entries_task.focus_filename_editor { this.update_visible_entries_task.focus_filename_editor = false; this.filename_editor.update(cx, |editor, cx| { - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); }); } if this.update_visible_entries_task.autoscroll { @@ -5631,6 +5745,7 @@ impl Render for ProjectPanel { .on_action(cx.listener(Self::copy)) .on_action(cx.listener(Self::paste)) .on_action(cx.listener(Self::duplicate)) + .on_action(cx.listener(Self::restore_file)) .when(!project.is_remote(), |el| { el.on_action(cx.listener(Self::trash)) }) @@ -5952,7 +6067,7 @@ impl Render for ProjectPanel { cx.stop_propagation(); this.state.selection = None; this.marked_entries.clear(); - this.focus_handle(cx).focus(window); + this.focus_handle(cx).focus(window, cx); })) .on_mouse_down( MouseButton::Right, diff --git a/crates/project_panel/src/project_panel_settings.rs b/crates/project_panel/src/project_panel_settings.rs index b0316270340203177278edebaececd0d86e39869..5d498da0f9d519bc25d738bcf9368c394bbdabfd 100644 --- a/crates/project_panel/src/project_panel_settings.rs +++ b/crates/project_panel/src/project_panel_settings.rs @@ -92,7 +92,13 @@ impl Settings for ProjectPanelSettings { entry_spacing: project_panel.entry_spacing.unwrap(), file_icons: project_panel.file_icons.unwrap(), folder_icons: project_panel.folder_icons.unwrap(), - git_status: project_panel.git_status.unwrap(), + git_status: project_panel.git_status.unwrap() + && content + .git + .unwrap() + .enabled + .unwrap() + .is_git_status_enabled(), indent_size: project_panel.indent_size.unwrap(), indent_guides: IndentGuidesSettings { show: project_panel.indent_guides.unwrap().show.unwrap(), diff --git a/crates/prompt_store/Cargo.toml b/crates/prompt_store/Cargo.toml index 13bacbfad3bf2b5deb4a20af866f37dad47288ff..a7df9d13ee82da62838175029b9bdfd7c9375508 100644 --- a/crates/prompt_store/Cargo.toml +++ b/crates/prompt_store/Cargo.toml @@ -28,6 +28,11 @@ parking_lot.workspace = true paths.workspace = true rope.workspace = true serde.workspace = true +strum.workspace = true text.workspace = true util.workspace = true uuid.workspace = true + +[dev-dependencies] +gpui = { workspace = true, features = ["test-support"] } +tempfile.workspace = true diff --git a/crates/prompt_store/src/prompt_store.rs b/crates/prompt_store/src/prompt_store.rs index 6417a7ad214c84258d4cc18eddc0b1c1d785ca18..2c45410c2aa172c8a4f7118a914cacca69ea7ca8 100644 --- a/crates/prompt_store/src/prompt_store.rs +++ b/crates/prompt_store/src/prompt_store.rs @@ -1,6 +1,6 @@ mod prompts; -use anyhow::{Context as _, Result, anyhow}; +use anyhow::{Result, anyhow}; use chrono::{DateTime, Utc}; use collections::HashMap; use futures::FutureExt as _; @@ -23,6 +23,7 @@ use std::{ path::PathBuf, sync::{Arc, atomic::AtomicBool}, }; +use strum::{EnumIter, IntoEnumIterator as _}; use text::LineEnding; use util::ResultExt; use uuid::Uuid; @@ -51,11 +52,51 @@ pub struct PromptMetadata { pub saved_at: DateTime, } +impl PromptMetadata { + fn builtin(builtin: BuiltInPrompt) -> Self { + Self { + id: PromptId::BuiltIn(builtin), + title: Some(builtin.title().into()), + default: false, + saved_at: DateTime::default(), + } + } +} + +/// Built-in prompts that have default content and can be customized by users. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, EnumIter)] +pub enum BuiltInPrompt { + CommitMessage, +} + +impl BuiltInPrompt { + pub fn title(&self) -> &'static str { + match self { + Self::CommitMessage => "Commit message", + } + } + + /// Returns the default content for this built-in prompt. + pub fn default_content(&self) -> &'static str { + match self { + Self::CommitMessage => include_str!("../../git_ui/src/commit_message_prompt.txt"), + } + } +} + +impl std::fmt::Display for BuiltInPrompt { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::CommitMessage => write!(f, "Commit message"), + } + } +} + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(tag = "kind")] pub enum PromptId { User { uuid: UserPromptId }, - CommitMessage, + BuiltIn(BuiltInPrompt), } impl PromptId { @@ -63,31 +104,37 @@ impl PromptId { UserPromptId::new().into() } - pub fn user_id(&self) -> Option { + pub fn as_user(&self) -> Option { match self { Self::User { uuid } => Some(*uuid), - _ => None, + Self::BuiltIn { .. } => None, } } - pub fn is_built_in(&self) -> bool { + pub fn as_built_in(&self) -> Option { match self { - Self::User { .. } => false, - Self::CommitMessage => true, + Self::User { .. } => None, + Self::BuiltIn(builtin) => Some(*builtin), } } + pub fn is_built_in(&self) -> bool { + matches!(self, Self::BuiltIn { .. }) + } + pub fn can_edit(&self) -> bool { match self { - Self::User { .. } | Self::CommitMessage => true, + Self::User { .. } => true, + Self::BuiltIn(builtin) => match builtin { + BuiltInPrompt::CommitMessage => true, + }, } } +} - pub fn default_content(&self) -> Option<&'static str> { - match self { - Self::User { .. } => None, - Self::CommitMessage => Some(include_str!("../../git_ui/src/commit_message_prompt.txt")), - } +impl From for PromptId { + fn from(builtin: BuiltInPrompt) -> Self { + PromptId::BuiltIn(builtin) } } @@ -117,7 +164,7 @@ impl std::fmt::Display for PromptId { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { PromptId::User { uuid } => write!(f, "{}", uuid.0), - PromptId::CommitMessage => write!(f, "Commit message"), + PromptId::BuiltIn(builtin) => write!(f, "{}", builtin), } } } @@ -150,6 +197,16 @@ impl MetadataCache { cache.metadata.push(metadata.clone()); cache.metadata_by_id.insert(prompt_id, metadata); } + + // Insert all the built-in prompts that were not customized by the user + for builtin in BuiltInPrompt::iter() { + let builtin_id = PromptId::BuiltIn(builtin); + if !cache.metadata_by_id.contains_key(&builtin_id) { + let metadata = PromptMetadata::builtin(builtin); + cache.metadata.push(metadata.clone()); + cache.metadata_by_id.insert(builtin_id, metadata); + } + } cache.sort(); Ok(cache) } @@ -198,26 +255,6 @@ impl PromptStore { let mut txn = db_env.write_txn()?; let metadata = db_env.create_database(&mut txn, Some("metadata.v2"))?; let bodies = db_env.create_database(&mut txn, Some("bodies.v2"))?; - - // Insert default commit message prompt if not present - if metadata.get(&txn, &PromptId::CommitMessage)?.is_none() { - metadata.put( - &mut txn, - &PromptId::CommitMessage, - &PromptMetadata { - id: PromptId::CommitMessage, - title: Some("Git Commit Message".into()), - default: false, - saved_at: Utc::now(), - }, - )?; - } - if bodies.get(&txn, &PromptId::CommitMessage)?.is_none() { - let commit_message_prompt = - include_str!("../../git_ui/src/commit_message_prompt.txt"); - bodies.put(&mut txn, &PromptId::CommitMessage, commit_message_prompt)?; - } - txn.commit()?; Self::upgrade_dbs(&db_env, metadata, bodies).log_err(); @@ -310,7 +347,16 @@ impl PromptStore { let bodies = self.bodies; cx.background_spawn(async move { let txn = env.read_txn()?; - let mut prompt = bodies.get(&txn, &id)?.context("prompt not found")?.into(); + let mut prompt: String = match bodies.get(&txn, &id)? { + Some(body) => body.into(), + None => { + if let Some(built_in) = id.as_built_in() { + built_in.default_content().into() + } else { + anyhow::bail!("prompt not found") + } + } + }; LineEnding::normalize(&mut prompt); Ok(prompt) }) @@ -355,11 +401,6 @@ impl PromptStore { }) } - /// Returns the number of prompts in the store. - pub fn prompt_count(&self) -> usize { - self.metadata_cache.read().metadata.len() - } - pub fn metadata(&self, id: PromptId) -> Option { self.metadata_cache.read().metadata_by_id.get(&id).cloned() } @@ -428,23 +469,38 @@ impl PromptStore { return Task::ready(Err(anyhow!("this prompt cannot be edited"))); } - let prompt_metadata = PromptMetadata { - id, - title, - default, - saved_at: Utc::now(), + let body = body.to_string(); + let is_default_content = id + .as_built_in() + .is_some_and(|builtin| body.trim() == builtin.default_content().trim()); + + let metadata = if let Some(builtin) = id.as_built_in() { + PromptMetadata::builtin(builtin) + } else { + PromptMetadata { + id, + title, + default, + saved_at: Utc::now(), + } }; - self.metadata_cache.write().insert(prompt_metadata.clone()); + + self.metadata_cache.write().insert(metadata.clone()); let db_connection = self.env.clone(); let bodies = self.bodies; - let metadata = self.metadata; + let metadata_db = self.metadata; let task = cx.background_spawn(async move { let mut txn = db_connection.write_txn()?; - metadata.put(&mut txn, &id, &prompt_metadata)?; - bodies.put(&mut txn, &id, &body.to_string())?; + if is_default_content { + metadata_db.delete(&mut txn, &id)?; + bodies.delete(&mut txn, &id)?; + } else { + metadata_db.put(&mut txn, &id, &metadata)?; + bodies.put(&mut txn, &id, &body)?; + } txn.commit()?; @@ -506,3 +562,122 @@ impl PromptStore { pub struct GlobalPromptStore(Shared, Arc>>>); impl Global for GlobalPromptStore {} + +#[cfg(test)] +mod tests { + use super::*; + use gpui::TestAppContext; + + #[gpui::test] + async fn test_built_in_prompt_load_save(cx: &mut TestAppContext) { + cx.executor().allow_parking(); + + let temp_dir = tempfile::tempdir().unwrap(); + let db_path = temp_dir.path().join("prompts-db"); + + let store = cx.update(|cx| PromptStore::new(db_path, cx)).await.unwrap(); + let store = cx.new(|_cx| store); + + let commit_message_id = PromptId::BuiltIn(BuiltInPrompt::CommitMessage); + + let loaded_content = store + .update(cx, |store, cx| store.load(commit_message_id, cx)) + .await + .unwrap(); + + let mut expected_content = BuiltInPrompt::CommitMessage.default_content().to_string(); + LineEnding::normalize(&mut expected_content); + assert_eq!( + loaded_content.trim(), + expected_content.trim(), + "Loading a built-in prompt not in DB should return default content" + ); + + let metadata = store.read_with(cx, |store, _| store.metadata(commit_message_id)); + assert!( + metadata.is_some(), + "Built-in prompt should always have metadata" + ); + assert!( + store.read_with(cx, |store, _| { + store + .metadata_cache + .read() + .metadata_by_id + .contains_key(&commit_message_id) + }), + "Built-in prompt should always be in cache" + ); + + let custom_content = "Custom commit message prompt"; + store + .update(cx, |store, cx| { + store.save( + commit_message_id, + Some("Commit message".into()), + false, + Rope::from(custom_content), + cx, + ) + }) + .await + .unwrap(); + + let loaded_custom = store + .update(cx, |store, cx| store.load(commit_message_id, cx)) + .await + .unwrap(); + assert_eq!( + loaded_custom.trim(), + custom_content.trim(), + "Custom content should be loaded after saving" + ); + + assert!( + store + .read_with(cx, |store, _| store.metadata(commit_message_id)) + .is_some(), + "Built-in prompt should have metadata after customization" + ); + + store + .update(cx, |store, cx| { + store.save( + commit_message_id, + Some("Commit message".into()), + false, + Rope::from(BuiltInPrompt::CommitMessage.default_content()), + cx, + ) + }) + .await + .unwrap(); + + let metadata_after_reset = + store.read_with(cx, |store, _| store.metadata(commit_message_id)); + assert!( + metadata_after_reset.is_some(), + "Built-in prompt should still have metadata after reset" + ); + assert_eq!( + metadata_after_reset + .as_ref() + .and_then(|m| m.title.as_ref().map(|t| t.as_ref())), + Some("Commit message"), + "Built-in prompt should have default title after reset" + ); + + let loaded_after_reset = store + .update(cx, |store, cx| store.load(commit_message_id, cx)) + .await + .unwrap(); + let mut expected_content_after_reset = + BuiltInPrompt::CommitMessage.default_content().to_string(); + LineEnding::normalize(&mut expected_content_after_reset); + assert_eq!( + loaded_after_reset.trim(), + expected_content_after_reset.trim(), + "After saving default content, load should return default" + ); + } +} diff --git a/crates/prompt_store/src/prompts.rs b/crates/prompt_store/src/prompts.rs index 674d4869e9825fd700dde3db510fbf68c6b4d5cc..6a845bb8dd394f8a1ff26a8a0e130156a2a158bd 100644 --- a/crates/prompt_store/src/prompts.rs +++ b/crates/prompt_store/src/prompts.rs @@ -112,7 +112,7 @@ pub struct ContentPromptContextV2 { pub language_name: Option, pub is_truncated: bool, pub document_content: String, - pub rewrite_section: Option, + pub rewrite_section: String, pub diagnostic_errors: Vec, } @@ -310,7 +310,6 @@ impl PromptBuilder { }; const MAX_CTX: usize = 50000; - let is_insert = range.is_empty(); let mut is_truncated = false; let before_range = 0..range.start; @@ -335,28 +334,19 @@ impl PromptBuilder { for chunk in buffer.text_for_range(truncated_before) { document_content.push_str(chunk); } - if is_insert { - document_content.push_str(""); - } else { - document_content.push_str("\n"); - for chunk in buffer.text_for_range(range.clone()) { - document_content.push_str(chunk); - } - document_content.push_str("\n"); + + document_content.push_str("\n"); + for chunk in buffer.text_for_range(range.clone()) { + document_content.push_str(chunk); } + document_content.push_str("\n"); + for chunk in buffer.text_for_range(truncated_after) { document_content.push_str(chunk); } - let rewrite_section = if !is_insert { - let mut section = String::new(); - for chunk in buffer.text_for_range(range.clone()) { - section.push_str(chunk); - } - Some(section) - } else { - None - }; + let rewrite_section: String = buffer.text_for_range(range.clone()).collect(); + let diagnostics = buffer.diagnostics_in_range::<_, Point>(range, false); let diagnostic_errors: Vec = diagnostics .map(|entry| { diff --git a/crates/proto/proto/worktree.proto b/crates/proto/proto/worktree.proto index 315aeb311e1e4284970dffa17bee4b0142373e92..5873cfc10c1c6af24520705c27781b916dfda3d0 100644 --- a/crates/proto/proto/worktree.proto +++ b/crates/proto/proto/worktree.proto @@ -166,14 +166,16 @@ message TrustWorktrees { message PathTrust { oneof content { - uint64 workspace = 1; uint64 worktree_id = 2; string abs_path = 3; } + + reserved 1; } message RestrictWorktrees { uint64 project_id = 1; - bool restrict_workspace = 2; repeated uint64 worktree_ids = 3; + + reserved 2; } diff --git a/crates/recent_projects/src/remote_connections.rs b/crates/recent_projects/src/remote_connections.rs index e8349601b5303331c0a6a38aca306fe57ab07ed3..1bab31b4d0ebb80444c40c99feb984ebd23feb60 100644 --- a/crates/recent_projects/src/remote_connections.rs +++ b/crates/recent_projects/src/remote_connections.rs @@ -209,7 +209,7 @@ impl RemoteConnectionPrompt { let markdown = cx.new(|cx| Markdown::new_text(prompt.into(), cx)); self.prompt = Some((markdown, tx)); self.status_message.take(); - window.focus(&self.editor.focus_handle(cx)); + window.focus(&self.editor.focus_handle(cx), cx); cx.notify(); } diff --git a/crates/recent_projects/src/remote_servers.rs b/crates/recent_projects/src/remote_servers.rs index a4388c6026ab7aa6bbdfc75d025e095b5a2a6187..15735b6664e4b72749b0149013d02428eb2735de 100644 --- a/crates/recent_projects/src/remote_servers.rs +++ b/crates/recent_projects/src/remote_servers.rs @@ -76,7 +76,7 @@ impl CreateRemoteServer { fn new(window: &mut Window, cx: &mut App) -> Self { let address_editor = cx.new(|cx| Editor::single_line(window, cx)); address_editor.update(cx, |this, cx| { - this.focus_handle(cx).focus(window); + this.focus_handle(cx).focus(window, cx); }); Self { address_editor, @@ -107,7 +107,7 @@ struct CreateRemoteDevContainer { impl CreateRemoteDevContainer { fn new(window: &mut Window, cx: &mut Context) -> Self { let entries = std::array::from_fn(|_| NavigableEntry::focusable(cx)); - entries[0].focus_handle.focus(window); + entries[0].focus_handle.focus(window, cx); Self { entries, progress: DevContainerCreationProgress::Initial, @@ -199,7 +199,7 @@ impl EditNicknameState { this.set_text(starting_text, window, cx); } }); - this.editor.focus_handle(cx).focus(window); + this.editor.focus_handle(cx).focus(window, cx); this } } @@ -792,7 +792,7 @@ impl RemoteServerProjects { this.retained_connections.push(client); this.add_ssh_server(connection_options, cx); this.mode = Mode::default_mode(&this.ssh_config_servers, cx); - this.focus_handle(cx).focus(window); + this.focus_handle(cx).focus(window, cx); cx.notify() }) .log_err(), @@ -875,7 +875,7 @@ impl RemoteServerProjects { crate::add_wsl_distro(fs, &connection_options, cx); this.mode = Mode::default_mode(&BTreeSet::new(), cx); - this.focus_handle(cx).focus(window); + this.focus_handle(cx).focus(window, cx); cx.notify(); }), _ => this.update(cx, |this, cx| { @@ -924,7 +924,7 @@ impl RemoteServerProjects { return; } }); - self.focus_handle(cx).focus(window); + self.focus_handle(cx).focus(window, cx); cx.notify(); } @@ -933,7 +933,7 @@ impl RemoteServerProjects { CreateRemoteDevContainer::new(window, cx) .progress(DevContainerCreationProgress::Creating), ); - self.focus_handle(cx).focus(window); + self.focus_handle(cx).focus(window, cx); cx.notify(); } @@ -1068,7 +1068,7 @@ impl RemoteServerProjects { } }); self.mode = Mode::default_mode(&self.ssh_config_servers, cx); - self.focus_handle.focus(window); + self.focus_handle.focus(window, cx); } #[cfg(target_os = "windows")] Mode::AddWslDistro(state) => { @@ -1094,7 +1094,7 @@ impl RemoteServerProjects { } _ => { self.mode = Mode::default_mode(&self.ssh_config_servers, cx); - self.focus_handle(cx).focus(window); + self.focus_handle(cx).focus(window, cx); cx.notify(); } } @@ -1640,7 +1640,7 @@ impl RemoteServerProjects { ) -> impl IntoElement { match &state.progress { DevContainerCreationProgress::Error(message) => { - self.focus_handle(cx).focus(window); + self.focus_handle(cx).focus(window, cx); return div() .track_focus(&self.focus_handle(cx)) .size_full() @@ -1952,7 +1952,7 @@ impl RemoteServerProjects { let connection_prompt = state.connection_prompt.clone(); state.picker.update(cx, |picker, cx| { - picker.focus_handle(cx).focus(window); + picker.focus_handle(cx).focus(window, cx); }); v_flex() @@ -2752,7 +2752,7 @@ impl Render for RemoteServerProjects { .on_action(cx.listener(Self::cancel)) .on_action(cx.listener(Self::confirm)) .capture_any_mouse_down(cx.listener(|this, _, window, cx| { - this.focus_handle(cx).focus(window); + this.focus_handle(cx).focus(window, cx); })) .on_mouse_down_out(cx.listener(|this, _, _, cx| { if matches!(this.mode, Mode::Default(_)) { diff --git a/crates/remote/src/transport/ssh.rs b/crates/remote/src/transport/ssh.rs index 6c8eb49c1c2158322a275e064162b53e2f5f3d5e..d13e1c4934947e39b08e05eb32e2787548e621e1 100644 --- a/crates/remote/src/transport/ssh.rs +++ b/crates/remote/src/transport/ssh.rs @@ -32,8 +32,7 @@ use tempfile::TempDir; use util::{ paths::{PathStyle, RemotePathBuf}, rel_path::RelPath, - shell::{Shell, ShellKind}, - shell_builder::ShellBuilder, + shell::ShellKind, }; pub(crate) struct SshRemoteConnection { @@ -1544,8 +1543,6 @@ fn build_command( } else { write!(exec, "{ssh_shell} -l")?; }; - let (command, command_args) = ShellBuilder::new(&Shell::Program(ssh_shell.to_owned()), false) - .build(Some(exec.clone()), &[]); let mut args = Vec::new(); args.extend(ssh_args); @@ -1556,8 +1553,7 @@ fn build_command( } args.push("-t".into()); - args.push(command); - args.extend(command_args); + args.push(exec); Ok(CommandTemplate { program: "ssh".into(), @@ -1597,9 +1593,6 @@ mod tests { "-p", "2222", "-t", - "/bin/fish", - "-i", - "-c", "cd \"$HOME/work\" && exec env INPUT_VA=val remote_program arg1 arg2" ] ); @@ -1632,9 +1625,6 @@ mod tests { "-L", "1:foo:2", "-t", - "/bin/fish", - "-i", - "-c", "cd && exec env INPUT_VA=val /bin/fish -l" ] ); diff --git a/crates/remote_server/src/headless_project.rs b/crates/remote_server/src/headless_project.rs index 89d26d35c77e076e1e618669acb5e54dc8afdcca..c83cc6aa34402a082fe104d64a8cb47f460704b8 100644 --- a/crates/remote_server/src/headless_project.rs +++ b/crates/remote_server/src/headless_project.rs @@ -642,16 +642,13 @@ impl HeadlessProject { .update(|cx| TrustedWorktrees::try_get_global(cx))? .context("missing trusted worktrees")?; trusted_worktrees.update(&mut cx, |trusted_worktrees, cx| { - let mut restricted_paths = envelope + let restricted_paths = envelope .payload .worktree_ids .into_iter() .map(WorktreeId::from_proto) .map(PathTrust::Worktree) .collect::>(); - if envelope.payload.restrict_workspace { - restricted_paths.insert(PathTrust::Workspace); - } trusted_worktrees.restrict(restricted_paths, None, cx); })?; Ok(proto::Ack {}) diff --git a/crates/remote_server/src/unix.rs b/crates/remote_server/src/unix.rs index af603998171e19d4776d47479ff81aa08d26d258..449b8491ece2494dacf8bfb1fa89aeeb8f6a81ac 100644 --- a/crates/remote_server/src/unix.rs +++ b/crates/remote_server/src/unix.rs @@ -36,7 +36,6 @@ use smol::Async; use smol::channel::{Receiver, Sender}; use smol::io::AsyncReadExt; use smol::{net::unix::UnixListener, stream::StreamExt as _}; -use std::pin::Pin; use std::{ env, ffi::OsStr, @@ -453,13 +452,10 @@ pub fn execute_run( ) }; - let trust_task = trusted_worktrees::wait_for_default_workspace_trust("Node runtime", cx) - .map(|trust_task| Box::pin(trust_task) as Pin>); let node_runtime = NodeRuntime::new( http_client.clone(), Some(shell_env_loaded_rx), node_settings_rx, - trust_task, ); let mut languages = LanguageRegistry::new(cx.background_executor().clone()); diff --git a/crates/rules_library/src/rules_library.rs b/crates/rules_library/src/rules_library.rs index 00cf939f7af45f7701cd9d3599a103ece4a6f393..fc6af46782f26615aa0f5faeb7062ca03181ab9b 100644 --- a/crates/rules_library/src/rules_library.rs +++ b/crates/rules_library/src/rules_library.rs @@ -3,9 +3,9 @@ use collections::{HashMap, HashSet}; use editor::{CompletionProvider, SelectionEffects}; use editor::{CurrentLineHighlight, Editor, EditorElement, EditorEvent, EditorStyle, actions::Tab}; use gpui::{ - Action, App, Bounds, DEFAULT_ADDITIONAL_WINDOW_SIZE, Entity, EventEmitter, Focusable, - PromptLevel, Subscription, Task, TextStyle, TitlebarOptions, WindowBounds, WindowHandle, - WindowOptions, actions, point, size, transparent_black, + App, Bounds, DEFAULT_ADDITIONAL_WINDOW_SIZE, Entity, EventEmitter, Focusable, PromptLevel, + Subscription, Task, TextStyle, TitlebarOptions, WindowBounds, WindowHandle, WindowOptions, + actions, point, size, transparent_black, }; use language::{Buffer, LanguageRegistry, language_settings::SoftWrap}; use language_model::{ @@ -21,7 +21,7 @@ use std::sync::atomic::AtomicBool; use std::time::Duration; use theme::ThemeSettings; use title_bar::platform_title_bar::PlatformTitleBar; -use ui::{Divider, KeyBinding, ListItem, ListItemSpacing, ListSubHeader, Tooltip, prelude::*}; +use ui::{Divider, ListItem, ListItemSpacing, ListSubHeader, Tooltip, prelude::*}; use util::{ResultExt, TryFutureExt}; use workspace::{Workspace, WorkspaceSettings, client_side_decorations}; use zed_actions::assistant::InlineAssist; @@ -206,13 +206,8 @@ impl PickerDelegate for RulePickerDelegate { self.filtered_entries.len() } - fn no_matches_text(&self, _window: &mut Window, cx: &mut App) -> Option { - let text = if self.store.read(cx).prompt_count() == 0 { - "No rules.".into() - } else { - "No rules found matching your search.".into() - }; - Some(text) + fn no_matches_text(&self, _window: &mut Window, _cx: &mut App) -> Option { + Some("No rules found matching your search.".into()) } fn selected_index(&self) -> usize { @@ -680,13 +675,13 @@ impl RulesLibrary { window: &mut Window, cx: &mut Context, ) { - let Some(default_content) = prompt_id.default_content() else { + let Some(built_in) = prompt_id.as_built_in() else { return; }; if let Some(rule_editor) = self.rule_editors.get(&prompt_id) { rule_editor.body_editor.update(cx, |editor, cx| { - editor.set_text(default_content, window, cx); + editor.set_text(built_in.default_content(), window, cx); }); } } @@ -720,7 +715,7 @@ impl RulesLibrary { if focus { rule_editor .body_editor - .update(cx, |editor, cx| window.focus(&editor.focus_handle(cx))); + .update(cx, |editor, cx| window.focus(&editor.focus_handle(cx), cx)); } self.set_active_rule(Some(prompt_id), window, cx); } else if let Some(rule_metadata) = self.store.read(cx).metadata(prompt_id) { @@ -763,7 +758,7 @@ impl RulesLibrary { editor.set_current_line_highlight(Some(CurrentLineHighlight::None)); editor.set_completion_provider(Some(make_completion_provider())); if focus { - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); } editor }); @@ -939,7 +934,7 @@ impl RulesLibrary { if let Some(active_rule) = self.active_rule_id { self.rule_editors[&active_rule] .body_editor - .update(cx, |editor, cx| window.focus(&editor.focus_handle(cx))); + .update(cx, |editor, cx| window.focus(&editor.focus_handle(cx), cx)); cx.stop_propagation(); } } @@ -998,7 +993,7 @@ impl RulesLibrary { if let Some(rule_id) = self.active_rule_id && let Some(rule_editor) = self.rule_editors.get(&rule_id) { - window.focus(&rule_editor.body_editor.focus_handle(cx)); + window.focus(&rule_editor.body_editor.focus_handle(cx), cx); } } @@ -1011,7 +1006,7 @@ impl RulesLibrary { if let Some(rule_id) = self.active_rule_id && let Some(rule_editor) = self.rule_editors.get(&rule_id) { - window.focus(&rule_editor.title_editor.focus_handle(cx)); + window.focus(&rule_editor.title_editor.focus_handle(cx), cx); } } @@ -1308,8 +1303,8 @@ impl RulesLibrary { .size_full() .relative() .overflow_hidden() - .on_click(cx.listener(move |_, _, window, _| { - window.focus(&focus_handle); + .on_click(cx.listener(move |_, _, window, cx| { + window.focus(&focus_handle, cx); })) .child( h_flex() @@ -1428,31 +1423,7 @@ impl Render for RulesLibrary { this.border_t_1().border_color(cx.theme().colors().border) }) .child(self.render_rule_list(cx)) - .map(|el| { - if self.store.read(cx).prompt_count() == 0 { - el.child( - v_flex() - .h_full() - .flex_1() - .items_center() - .justify_center() - .border_l_1() - .border_color(cx.theme().colors().border) - .bg(cx.theme().colors().editor_background) - .child( - Button::new("create-rule", "New Rule") - .style(ButtonStyle::Outlined) - .key_binding(KeyBinding::for_action(&NewRule, cx)) - .on_click(|_, window, cx| { - window - .dispatch_action(NewRule.boxed_clone(), cx) - }), - ), - ) - } else { - el.child(self.render_active_rule(cx)) - } - }), + .child(self.render_active_rule(cx)), ), window, cx, diff --git a/crates/search/src/buffer_search.rs b/crates/search/src/buffer_search.rs index 686d385aa07accac168062fa598790b36e80199f..be3331048bc78a91a8d3c5a3637d6bf6ea007e4d 100644 --- a/crates/search/src/buffer_search.rs +++ b/crates/search/src/buffer_search.rs @@ -7,7 +7,6 @@ use crate::{ search_bar::{ActionButtonState, input_base_styles, render_action_button, render_text_input}, }; use any_vec::AnyVec; -use anyhow::Context as _; use collections::HashMap; use editor::{ DisplayPoint, Editor, EditorSettings, MultiBufferOffset, @@ -107,7 +106,10 @@ pub struct BufferSearchBar { replacement_editor_focused: bool, active_searchable_item: Option>, active_match_index: Option, - active_searchable_item_subscription: Option, + #[cfg(target_os = "macos")] + active_searchable_item_subscriptions: Option<[Subscription; 2]>, + #[cfg(not(target_os = "macos"))] + active_searchable_item_subscriptions: Option, active_search: Option>, searchable_items_with_matches: HashMap, AnyVec>, pending_search: Option>, @@ -473,7 +475,7 @@ impl ToolbarItemView for BufferSearchBar { cx: &mut Context, ) -> ToolbarItemLocation { cx.notify(); - self.active_searchable_item_subscription.take(); + self.active_searchable_item_subscriptions.take(); self.active_searchable_item.take(); self.pending_search.take(); @@ -483,18 +485,58 @@ impl ToolbarItemView for BufferSearchBar { { let this = cx.entity().downgrade(); - self.active_searchable_item_subscription = - Some(searchable_item_handle.subscribe_to_search_events( - window, - cx, - Box::new(move |search_event, window, cx| { - if let Some(this) = this.upgrade() { - this.update(cx, |this, cx| { - this.on_active_searchable_item_event(search_event, window, cx) - }); + let search_event_subscription = searchable_item_handle.subscribe_to_search_events( + window, + cx, + Box::new(move |search_event, window, cx| { + if let Some(this) = this.upgrade() { + this.update(cx, |this, cx| { + this.on_active_searchable_item_event(search_event, window, cx) + }); + } + }), + ); + + #[cfg(target_os = "macos")] + { + let item_focus_handle = searchable_item_handle.item_focus_handle(cx); + + self.active_searchable_item_subscriptions = Some([ + search_event_subscription, + cx.on_focus(&item_focus_handle, window, |this, window, cx| { + if this.query_editor_focused || this.replacement_editor_focused { + // no need to read pasteboard since focus came from toolbar + return; } + + cx.defer_in(window, |this, window, cx| { + if let Some(item) = cx.read_from_find_pasteboard() + && let Some(text) = item.text() + { + if this.query(cx) != text { + let search_options = item + .metadata() + .and_then(|m| m.parse().ok()) + .and_then(SearchOptions::from_bits) + .unwrap_or(this.search_options); + + drop(this.search( + &text, + Some(search_options), + true, + window, + cx, + )); + } + } + }); }), - )); + ]); + } + #[cfg(not(target_os = "macos"))] + { + self.active_searchable_item_subscriptions = Some(search_event_subscription); + } let is_project_search = searchable_item_handle.supported_options(cx).find_in_results; self.active_searchable_item = Some(searchable_item_handle); @@ -518,7 +560,7 @@ impl BufferSearchBar { pub fn register(registrar: &mut impl SearchActionsRegistrar) { registrar.register_handler(ForDeployed(|this, _: &FocusSearch, window, cx| { - this.query_editor.focus_handle(cx).focus(window); + this.query_editor.focus_handle(cx).focus(window, cx); this.select_query(window, cx); })); registrar.register_handler(ForDeployed( @@ -634,15 +676,19 @@ impl BufferSearchBar { .read(cx) .as_singleton() .expect("query editor should be backed by a singleton buffer"); + query_buffer .read(cx) .set_language_registry(languages.clone()); cx.spawn(async move |buffer_search_bar, cx| { + use anyhow::Context as _; + let regex_language = languages .language_for_name("regex") .await .context("loading regex language")?; + buffer_search_bar .update(cx, |buffer_search_bar, cx| { buffer_search_bar.regex_language = Some(regex_language); @@ -660,7 +706,7 @@ impl BufferSearchBar { replacement_editor, replacement_editor_focused: false, active_searchable_item: None, - active_searchable_item_subscription: None, + active_searchable_item_subscriptions: None, active_match_index: None, searchable_items_with_matches: Default::default(), default_options: search_options, @@ -706,7 +752,7 @@ impl BufferSearchBar { active_editor.search_bar_visibility_changed(false, window, cx); active_editor.toggle_filtered_search_ranges(None, window, cx); let handle = active_editor.item_focus_handle(cx); - self.focus(&handle, window); + self.focus(&handle, window, cx); } cx.emit(Event::UpdateLocation); @@ -749,7 +795,7 @@ impl BufferSearchBar { self.select_query(window, cx); } - window.focus(&handle); + window.focus(&handle, cx); } return true; } @@ -878,7 +924,7 @@ impl BufferSearchBar { } pub fn focus_replace(&mut self, window: &mut Window, cx: &mut Context) { - self.focus(&self.replacement_editor.focus_handle(cx), window); + self.focus(&self.replacement_editor.focus_handle(cx), window, cx); cx.notify(); } @@ -901,15 +947,25 @@ impl BufferSearchBar { }); self.set_search_options(options, cx); self.clear_matches(window, cx); + #[cfg(target_os = "macos")] + self.update_find_pasteboard(cx); cx.notify(); } self.update_matches(!updated, add_to_history, window, cx) } + #[cfg(target_os = "macos")] + pub fn update_find_pasteboard(&mut self, cx: &mut App) { + cx.write_to_find_pasteboard(gpui::ClipboardItem::new_string_with_metadata( + self.query(cx), + self.search_options.bits().to_string(), + )); + } + pub fn focus_editor(&mut self, _: &FocusEditor, window: &mut Window, cx: &mut Context) { if let Some(active_editor) = self.active_searchable_item.as_ref() { let handle = active_editor.item_focus_handle(cx); - window.focus(&handle); + window.focus(&handle, cx); } } @@ -1095,11 +1151,12 @@ impl BufferSearchBar { cx.spawn_in(window, async move |this, cx| { if search.await.is_ok() { this.update_in(cx, |this, window, cx| { - this.activate_current_match(window, cx) - }) - } else { - Ok(()) + this.activate_current_match(window, cx); + #[cfg(target_os = "macos")] + this.update_find_pasteboard(cx); + })?; } + anyhow::Ok(()) }) .detach_and_log_err(cx); } @@ -1290,6 +1347,7 @@ impl BufferSearchBar { .insert(active_searchable_item.downgrade(), matches); this.update_match_index(window, cx); + if add_to_history { this.search_history .add(&mut this.search_history_cursor, query_text); @@ -1384,7 +1442,7 @@ impl BufferSearchBar { Direction::Prev => (current_index - 1) % handles.len(), }; let next_focus_handle = &handles[new_index]; - self.focus(next_focus_handle, window); + self.focus(next_focus_handle, window, cx); cx.stop_propagation(); } @@ -1431,9 +1489,9 @@ impl BufferSearchBar { } } - fn focus(&self, handle: &gpui::FocusHandle, window: &mut Window) { + fn focus(&self, handle: &gpui::FocusHandle, window: &mut Window, cx: &mut App) { window.invalidate_character_coordinates(); - window.focus(handle); + window.focus(handle, cx); } fn toggle_replace(&mut self, _: &ToggleReplace, window: &mut Window, cx: &mut Context) { @@ -1444,7 +1502,7 @@ impl BufferSearchBar { } else { self.query_editor.focus_handle(cx) }; - self.focus(&handle, window); + self.focus(&handle, window, cx); cx.notify(); } } @@ -2038,7 +2096,7 @@ mod tests { .update(cx, |_, window, cx| { search_bar.update(cx, |search_bar, cx| { let handle = search_bar.query_editor.focus_handle(cx); - window.focus(&handle); + window.focus(&handle, cx); search_bar.activate_current_match(window, cx); }); assert!( @@ -2056,7 +2114,7 @@ mod tests { search_bar.update(cx, |search_bar, cx| { assert_eq!(search_bar.active_match_index, Some(0)); let handle = search_bar.query_editor.focus_handle(cx); - window.focus(&handle); + window.focus(&handle, cx); search_bar.select_all_matches(&SelectAllMatches, window, cx); }); assert!( @@ -2109,7 +2167,7 @@ mod tests { "Match index should be updated to the next one" ); let handle = search_bar.query_editor.focus_handle(cx); - window.focus(&handle); + window.focus(&handle, cx); search_bar.select_all_matches(&SelectAllMatches, window, cx); }); }) @@ -2175,7 +2233,7 @@ mod tests { .update(cx, |_, window, cx| { search_bar.update(cx, |search_bar, cx| { let handle = search_bar.query_editor.focus_handle(cx); - window.focus(&handle); + window.focus(&handle, cx); search_bar.search("abas_nonexistent_match", None, true, window, cx) }) }) diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs index 278f2e86b7b13fd5a82777054c12ff2e1b6239bb..e0bbf58ce6f1d0c752914bbbfa6fcdf70ea30175 100644 --- a/crates/search/src/project_search.rs +++ b/crates/search/src/project_search.rs @@ -954,9 +954,9 @@ impl ProjectSearchView { cx.on_next_frame(window, |this, window, cx| { if this.focus_handle.is_focused(window) { if this.has_matches() { - this.results_editor.focus_handle(cx).focus(window); + this.results_editor.focus_handle(cx).focus(window, cx); } else { - this.query_editor.focus_handle(cx).focus(window); + this.query_editor.focus_handle(cx).focus(window, cx); } } }); @@ -1453,7 +1453,7 @@ impl ProjectSearchView { query_editor.select_all(&SelectAll, window, cx); }); let editor_handle = self.query_editor.focus_handle(cx); - window.focus(&editor_handle); + window.focus(&editor_handle, cx); } fn set_query(&mut self, query: &str, window: &mut Window, cx: &mut Context) { @@ -1493,7 +1493,7 @@ impl ProjectSearchView { }); }); let results_handle = self.results_editor.focus_handle(cx); - window.focus(&results_handle); + window.focus(&results_handle, cx); } fn entity_changed(&mut self, window: &mut Window, cx: &mut Context) { @@ -1750,7 +1750,7 @@ impl ProjectSearchBar { fn focus_search(&mut self, window: &mut Window, cx: &mut Context) { if let Some(search_view) = self.active_project_search.as_ref() { search_view.update(cx, |search_view, cx| { - search_view.query_editor.focus_handle(cx).focus(window); + search_view.query_editor.focus_handle(cx).focus(window, cx); }); } } @@ -1783,7 +1783,7 @@ impl ProjectSearchBar { Direction::Prev => (current_index - 1) % views.len(), }; let next_focus_handle = &views[new_index]; - window.focus(next_focus_handle); + window.focus(next_focus_handle, cx); cx.stop_propagation(); }); } @@ -1832,7 +1832,7 @@ impl ProjectSearchBar { } else { this.query_editor.focus_handle(cx) }; - window.focus(&editor_to_focus); + window.focus(&editor_to_focus, cx); cx.notify(); }); } @@ -4352,7 +4352,7 @@ pub mod tests { let buffer_search_query = "search bar query"; buffer_search_bar .update_in(&mut cx, |buffer_search_bar, window, cx| { - buffer_search_bar.focus_handle(cx).focus(window); + buffer_search_bar.focus_handle(cx).focus(window, cx); buffer_search_bar.search(buffer_search_query, None, true, window, cx) }) .await diff --git a/crates/search/src/search.rs b/crates/search/src/search.rs index 6663f8c3184aba9fedbcd5faa3d80d5889181074..3aa40894ea91ed7af3441fad210f6ce0f9e1dd53 100644 --- a/crates/search/src/search.rs +++ b/crates/search/src/search.rs @@ -143,7 +143,7 @@ impl SearchOption { let focus_handle = focus_handle.clone(); button.on_click(move |_: &ClickEvent, window, cx| { if !focus_handle.is_focused(window) { - window.focus(&focus_handle); + window.focus(&focus_handle, cx); } window.dispatch_action(action.boxed_clone(), cx); }) diff --git a/crates/search/src/search_bar.rs b/crates/search/src/search_bar.rs index 13b4df9574aa6b2568dd6db25c6b63551d9b6d03..a1f6c070724c4d57b438c452ef4b4ae3cf20e66d 100644 --- a/crates/search/src/search_bar.rs +++ b/crates/search/src/search_bar.rs @@ -27,7 +27,7 @@ pub(super) fn render_action_button( let focus_handle = focus_handle.clone(); move |_, window, cx| { if !focus_handle.is_focused(window) { - window.focus(&focus_handle); + window.focus(&focus_handle, cx); } window.dispatch_action(action.boxed_clone(), cx); } diff --git a/crates/settings/src/settings_content.rs b/crates/settings/src/settings_content.rs index 3d7e6b5948b1db4d375814d6969ddabe95fc3e58..a00daaab1b9a93e1ec20b173dd6864849880d55e 100644 --- a/crates/settings/src/settings_content.rs +++ b/crates/settings/src/settings_content.rs @@ -158,6 +158,9 @@ pub struct SettingsContent { /// Default: false pub disable_ai: Option, + /// Settings for the which-key popup. + pub which_key: Option, + /// Settings related to Vim mode in Zed. pub vim: Option, } @@ -976,6 +979,19 @@ pub struct ReplSettingsContent { pub max_columns: Option, } +/// Settings for configuring the which-key popup behaviour. +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)] +pub struct WhichKeySettingsContent { + /// Whether to show the which-key popup when holding down key combinations + /// + /// Default: false + pub enabled: Option, + /// Delay in milliseconds before showing the which-key popup. + /// + /// Default: 700 + pub delay_ms: Option, +} + #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)] /// An ExtendingVec in the settings can only accumulate new values. /// diff --git a/crates/settings/src/settings_content/language.rs b/crates/settings/src/settings_content/language.rs index f9c85f18f380a7ad82b0d8bc202fe3763ba3a832..cf8cf7b63589e84a96e6b9d92f23a4488479d1f3 100644 --- a/crates/settings/src/settings_content/language.rs +++ b/crates/settings/src/settings_content/language.rs @@ -363,6 +363,14 @@ pub struct LanguageSettingsContent { /// /// Default: true pub extend_comment_on_newline: Option, + /// Whether to continue markdown lists when pressing enter. + /// + /// Default: true + pub extend_list_on_newline: Option, + /// Whether to indent list items when pressing tab after a list marker. + /// + /// Default: true + pub indent_list_on_tab: Option, /// Inlay hint related settings. pub inlay_hints: Option, /// Whether to automatically type closing characters for you. For example, diff --git a/crates/settings/src/settings_content/language_model.rs b/crates/settings/src/settings_content/language_model.rs index b106f3d9925cb4afe058cff44649f998c8b73d8a..e523286e5f56af88110c2d4a7d874c22195ea2b1 100644 --- a/crates/settings/src/settings_content/language_model.rs +++ b/crates/settings/src/settings_content/language_model.rs @@ -83,6 +83,8 @@ pub enum BedrockAuthMethodContent { NamedProfile, #[serde(rename = "sso")] SingleSignOn, + #[serde(rename = "api_key")] + ApiKey, /// IMDSv2, PodIdentity, env vars, etc. #[serde(rename = "default")] Automatic, diff --git a/crates/settings/src/settings_content/project.rs b/crates/settings/src/settings_content/project.rs index a5e15153832c425134e129cba1984b3b5886aa56..8e2d864149c9ecb6ca38ca73ef58205f588dc07b 100644 --- a/crates/settings/src/settings_content/project.rs +++ b/crates/settings/src/settings_content/project.rs @@ -288,6 +288,11 @@ impl std::fmt::Debug for ContextServerCommand { #[with_fallible_options] #[derive(Copy, Clone, Debug, PartialEq, Default, Serialize, Deserialize, JsonSchema, MergeFrom)] pub struct GitSettings { + /// Whether or not to enable git integration. + /// + /// Default: true + #[serde(flatten)] + pub enabled: Option, /// Whether or not to show the git gutter. /// /// Default: tracked_files @@ -317,6 +322,25 @@ pub struct GitSettings { pub path_style: Option, } +#[with_fallible_options] +#[derive(Clone, Copy, Debug, PartialEq, Default, Serialize, Deserialize, JsonSchema, MergeFrom)] +#[serde(rename_all = "snake_case")] +pub struct GitEnabledSettings { + pub disable_git: Option, + pub enable_status: Option, + pub enable_diff: Option, +} + +impl GitEnabledSettings { + pub fn is_git_status_enabled(&self) -> bool { + !self.disable_git.unwrap_or(false) && self.enable_status.unwrap_or(true) + } + + pub fn is_git_diff_enabled(&self) -> bool { + !self.disable_git.unwrap_or(false) && self.enable_diff.unwrap_or(true) + } +} + #[derive( Clone, Copy, diff --git a/crates/settings/src/vscode_import.rs b/crates/settings/src/vscode_import.rs index 587850303f13649fcc4adf8cf4ddbb8dc7181dcb..64343b05fd57c33eb9cfb0d8cb8674971266b464 100644 --- a/crates/settings/src/vscode_import.rs +++ b/crates/settings/src/vscode_import.rs @@ -215,6 +215,7 @@ impl VsCodeSettings { vim: None, vim_mode: None, workspace: self.workspace_settings_content(), + which_key: None, } } @@ -429,6 +430,8 @@ impl VsCodeSettings { enable_language_server: None, ensure_final_newline_on_save: self.read_bool("files.insertFinalNewline"), extend_comment_on_newline: None, + extend_list_on_newline: None, + indent_list_on_tab: None, format_on_save: self.read_bool("editor.guides.formatOnSave").map(|b| { if b { FormatOnSave::On diff --git a/crates/settings_ui/src/page_data.rs b/crates/settings_ui/src/page_data.rs index 1d0603de3184ad9da874b428a94af37d8966e6a2..ca2e23252a4483b365c7c42cfd086105d757a097 100644 --- a/crates/settings_ui/src/page_data.rs +++ b/crates/settings_ui/src/page_data.rs @@ -1233,6 +1233,49 @@ pub(crate) fn settings_data(cx: &App) -> Vec { } }).collect(), }), + SettingsPageItem::SectionHeader("Which-key Menu"), + SettingsPageItem::SettingItem(SettingItem { + title: "Show Which-key Menu", + description: "Display the which-key menu with matching bindings while a multi-stroke binding is pending.", + field: Box::new(SettingField { + json_path: Some("which_key.enabled"), + pick: |settings_content| { + settings_content + .which_key + .as_ref() + .and_then(|settings| settings.enabled.as_ref()) + }, + write: |settings_content, value| { + settings_content + .which_key + .get_or_insert_default() + .enabled = value; + }, + }), + metadata: None, + files: USER, + }), + SettingsPageItem::SettingItem(SettingItem { + title: "Menu Delay", + description: "Delay in milliseconds before the which-key menu appears.", + field: Box::new(SettingField { + json_path: Some("which_key.delay_ms"), + pick: |settings_content| { + settings_content + .which_key + .as_ref() + .and_then(|settings| settings.delay_ms.as_ref()) + }, + write: |settings_content, value| { + settings_content + .which_key + .get_or_insert_default() + .delay_ms = value; + }, + }), + metadata: None, + files: USER, + }), SettingsPageItem::SectionHeader("Multibuffer"), SettingsPageItem::SettingItem(SettingItem { title: "Double Click In Multibuffer", @@ -5476,6 +5519,102 @@ pub(crate) fn settings_data(cx: &App) -> Vec { SettingsPage { title: "Version Control", items: vec![ + SettingsPageItem::SectionHeader("Git Integration"), + SettingsPageItem::DynamicItem(DynamicItem { + discriminant: SettingItem { + files: USER, + title: "Disable Git Integration", + description: "Disable all Git integration features in Zed.", + field: Box::new(SettingField:: { + json_path: Some("git.disable_git"), + pick: |settings_content| { + settings_content + .git + .as_ref()? + .enabled + .as_ref()? + .disable_git + .as_ref() + }, + write: |settings_content, value| { + settings_content + .git + .get_or_insert_default() + .enabled + .get_or_insert_default() + .disable_git = value; + }, + }), + metadata: None, + }, + pick_discriminant: |settings_content| { + let disabled = settings_content + .git + .as_ref()? + .enabled + .as_ref()? + .disable_git + .unwrap_or(false); + Some(if disabled { 0 } else { 1 }) + }, + fields: vec![ + vec![], + vec![ + SettingItem { + files: USER, + title: "Enable Git Status", + description: "Show Git status information in the editor.", + field: Box::new(SettingField:: { + json_path: Some("git.enable_status"), + pick: |settings_content| { + settings_content + .git + .as_ref()? + .enabled + .as_ref()? + .enable_status + .as_ref() + }, + write: |settings_content, value| { + settings_content + .git + .get_or_insert_default() + .enabled + .get_or_insert_default() + .enable_status = value; + }, + }), + metadata: None, + }, + SettingItem { + files: USER, + title: "Enable Git Diff", + description: "Show Git diff information in the editor.", + field: Box::new(SettingField:: { + json_path: Some("git.enable_diff"), + pick: |settings_content| { + settings_content + .git + .as_ref()? + .enabled + .as_ref()? + .enable_diff + .as_ref() + }, + write: |settings_content, value| { + settings_content + .git + .get_or_insert_default() + .enabled + .get_or_insert_default() + .enable_diff = value; + }, + }), + metadata: None, + }, + ], + ], + }), SettingsPageItem::SectionHeader("Git Gutter"), SettingsPageItem::SettingItem(SettingItem { title: "Visibility", diff --git a/crates/settings_ui/src/settings_ui.rs b/crates/settings_ui/src/settings_ui.rs index 3a1ef5ee390eca0b15d8a34ca5245a714d2215d6..bed3f057ee9954acb8e9ef8e68d8abb20bc30058 100644 --- a/crates/settings_ui/src/settings_ui.rs +++ b/crates/settings_ui/src/settings_ui.rs @@ -346,8 +346,8 @@ impl NonFocusableHandle { fn from_handle(handle: FocusHandle, window: &mut Window, cx: &mut App) -> Entity { cx.new(|cx| { let _subscription = cx.on_focus(&handle, window, { - move |_, window, _| { - window.focus_next(); + move |_, window, cx| { + window.focus_next(cx); } }); Self { @@ -891,7 +891,7 @@ impl SettingsPageItem { .size(ButtonSize::Medium) .on_click({ let sub_page_link = sub_page_link.clone(); - cx.listener(move |this, _, _, cx| { + cx.listener(move |this, _, window, cx| { let mut section_index = item_index; let current_page = this.current_page(); @@ -910,7 +910,7 @@ impl SettingsPageItem { ) }; - this.push_sub_page(sub_page_link.clone(), header, cx) + this.push_sub_page(sub_page_link.clone(), header, window, cx) }) }), ) @@ -1551,7 +1551,7 @@ impl SettingsWindow { this.build_search_index(); this.search_bar.update(cx, |editor, cx| { - editor.focus_handle(cx).focus(window); + editor.focus_handle(cx).focus(window, cx); }); this @@ -2187,7 +2187,7 @@ impl SettingsWindow { let focus_handle = focus_handle.clone(); move |this, _: &gpui::ClickEvent, window, cx| { this.change_file(ix, window, cx); - focus_handle.focus(window); + focus_handle.focus(window, cx); } })) }; @@ -2264,7 +2264,7 @@ impl SettingsWindow { this.update(cx, |this, cx| { this.change_file(ix, window, cx); }); - focus_handle.focus(window); + focus_handle.focus(window, cx); } }, ); @@ -2398,7 +2398,7 @@ impl SettingsWindow { let focused_entry_parent = this.root_entry_containing(focused_entry); if this.navbar_entries[focused_entry_parent].expanded { this.toggle_navbar_entry(focused_entry_parent); - window.focus(&this.navbar_entries[focused_entry_parent].focus_handle); + window.focus(&this.navbar_entries[focused_entry_parent].focus_handle, cx); } cx.notify(); })) @@ -2547,6 +2547,7 @@ impl SettingsWindow { window.focus( &this.navbar_entries[entry_index] .focus_handle, + cx, ); cx.notify(); }, @@ -2671,7 +2672,7 @@ impl SettingsWindow { // back to back. cx.on_next_frame(window, move |_, window, cx| { if let Some(handle) = handle_to_focus.as_ref() { - window.focus(handle); + window.focus(handle, cx); } cx.on_next_frame(window, |_, _, cx| { @@ -2738,7 +2739,7 @@ impl SettingsWindow { }; self.navbar_scroll_handle .scroll_to_item(position, gpui::ScrollStrategy::Top); - window.focus(&self.navbar_entries[nav_entry_index].focus_handle); + window.focus(&self.navbar_entries[nav_entry_index].focus_handle, cx); cx.notify(); } @@ -3008,8 +3009,8 @@ impl SettingsWindow { IconButton::new("back-btn", IconName::ArrowLeft) .icon_size(IconSize::Small) .shape(IconButtonShape::Square) - .on_click(cx.listener(|this, _, _, cx| { - this.pop_sub_page(cx); + .on_click(cx.listener(|this, _, window, cx| { + this.pop_sub_page(window, cx); })), ) .child(self.render_sub_page_breadcrumbs()), @@ -3113,7 +3114,7 @@ impl SettingsWindow { .id("settings-ui-page") .on_action(cx.listener(|this, _: &menu::SelectNext, window, cx| { if !sub_page_stack().is_empty() { - window.focus_next(); + window.focus_next(cx); return; } for (logical_index, (actual_index, _)) in this.visible_page_items().enumerate() { @@ -3133,7 +3134,7 @@ impl SettingsWindow { cx.on_next_frame(window, |_, window, cx| { cx.notify(); cx.on_next_frame(window, |_, window, cx| { - window.focus_next(); + window.focus_next(cx); cx.notify(); }); }); @@ -3141,11 +3142,11 @@ impl SettingsWindow { return; } } - window.focus_next(); + window.focus_next(cx); })) .on_action(cx.listener(|this, _: &menu::SelectPrevious, window, cx| { if !sub_page_stack().is_empty() { - window.focus_prev(); + window.focus_prev(cx); return; } let mut prev_was_header = false; @@ -3165,7 +3166,7 @@ impl SettingsWindow { cx.on_next_frame(window, |_, window, cx| { cx.notify(); cx.on_next_frame(window, |_, window, cx| { - window.focus_prev(); + window.focus_prev(cx); cx.notify(); }); }); @@ -3174,7 +3175,7 @@ impl SettingsWindow { } prev_was_header = is_header; } - window.focus_prev(); + window.focus_prev(cx); })) .when(sub_page_stack().is_empty(), |this| { this.vertical_scrollbar_for(&self.list_state, window, cx) @@ -3368,23 +3369,28 @@ impl SettingsWindow { &mut self, sub_page_link: SubPageLink, section_header: &'static str, + window: &mut Window, cx: &mut Context, ) { sub_page_stack_mut().push(SubPage { link: sub_page_link, section_header, }); + self.sub_page_scroll_handle + .set_offset(point(px(0.), px(0.))); + self.content_focus_handle.focus_handle(cx).focus(window, cx); cx.notify(); } - fn pop_sub_page(&mut self, cx: &mut Context) { + fn pop_sub_page(&mut self, window: &mut Window, cx: &mut Context) { sub_page_stack_mut().pop(); + self.content_focus_handle.focus_handle(cx).focus(window, cx); cx.notify(); } - fn focus_file_at_index(&mut self, index: usize, window: &mut Window) { + fn focus_file_at_index(&mut self, index: usize, window: &mut Window, cx: &mut App) { if let Some((_, handle)) = self.files.get(index) { - handle.focus(window); + handle.focus(window, cx); } } @@ -3464,7 +3470,7 @@ impl Render for SettingsWindow { window.minimize_window(); }) .on_action(cx.listener(|this, _: &search::FocusSearch, window, cx| { - this.search_bar.focus_handle(cx).focus(window); + this.search_bar.focus_handle(cx).focus(window, cx); })) .on_action(cx.listener(|this, _: &ToggleFocusNav, window, cx| { if this @@ -3484,8 +3490,8 @@ impl Render for SettingsWindow { } })) .on_action(cx.listener( - |this, FocusFile(file_index): &FocusFile, window, _| { - this.focus_file_at_index(*file_index as usize, window); + |this, FocusFile(file_index): &FocusFile, window, cx| { + this.focus_file_at_index(*file_index as usize, window, cx); }, )) .on_action(cx.listener(|this, _: &FocusNextFile, window, cx| { @@ -3493,11 +3499,11 @@ impl Render for SettingsWindow { this.focused_file_index(window, cx) + 1, this.files.len().saturating_sub(1), ); - this.focus_file_at_index(next_index, window); + this.focus_file_at_index(next_index, window, cx); })) .on_action(cx.listener(|this, _: &FocusPreviousFile, window, cx| { let prev_index = this.focused_file_index(window, cx).saturating_sub(1); - this.focus_file_at_index(prev_index, window); + this.focus_file_at_index(prev_index, window, cx); })) .on_action(cx.listener(|this, _: &menu::SelectNext, window, cx| { if this @@ -3507,11 +3513,11 @@ impl Render for SettingsWindow { { this.focus_and_scroll_to_first_visible_nav_entry(window, cx); } else { - window.focus_next(); + window.focus_next(cx); } })) - .on_action(|_: &menu::SelectPrevious, window, _| { - window.focus_prev(); + .on_action(|_: &menu::SelectPrevious, window, cx| { + window.focus_prev(cx); }) .flex() .flex_row() diff --git a/crates/supermaven/src/supermaven_edit_prediction_delegate.rs b/crates/supermaven/src/supermaven_edit_prediction_delegate.rs index 578bc894f223fd458f510694194aebe633d7a6db..9563a0aa99f1760b5af214be28f25dbf1734c371 100644 --- a/crates/supermaven/src/supermaven_edit_prediction_delegate.rs +++ b/crates/supermaven/src/supermaven_edit_prediction_delegate.rs @@ -1,6 +1,6 @@ use crate::{Supermaven, SupermavenCompletionStateId}; use anyhow::Result; -use edit_prediction_types::{Direction, EditPrediction, EditPredictionDelegate}; +use edit_prediction_types::{EditPrediction, EditPredictionDelegate}; use futures::StreamExt as _; use gpui::{App, Context, Entity, EntityId, Task}; use language::{Anchor, Buffer, BufferSnapshot}; @@ -189,15 +189,6 @@ impl EditPredictionDelegate for SupermavenEditPredictionDelegate { })); } - fn cycle( - &mut self, - _buffer: Entity, - _cursor_position: Anchor, - _direction: Direction, - _cx: &mut Context, - ) { - } - fn accept(&mut self, _cx: &mut Context) { reset_completion_cache(self, _cx); } diff --git a/crates/terminal/src/terminal_hyperlinks.rs b/crates/terminal/src/terminal_hyperlinks.rs index 8ff33895251f707c8bc9a7894bd74b0bb323ae6c..4fe2baa2dc27f3a589efd9b7739262a6fec3fcb4 100644 --- a/crates/terminal/src/terminal_hyperlinks.rs +++ b/crates/terminal/src/terminal_hyperlinks.rs @@ -11,6 +11,7 @@ use alacritty_terminal::{ use log::{info, warn}; use regex::Regex; use std::{ + iter::{once, once_with}, ops::{Index, Range}, time::{Duration, Instant}, }; @@ -232,14 +233,17 @@ fn path_match( (line_end.line.0 - line_start.line.0 + 1) as usize * term.grid().columns(), ); let first_cell = &term.grid()[line_start]; + let mut prev_len = 0; line.push(first_cell.c); - let mut start_offset = 0; + let mut prev_char_is_space = first_cell.c == ' '; let mut hovered_point_byte_offset = None; + let mut hovered_word_start_offset = None; + let mut hovered_word_end_offset = None; - if !first_cell.flags.intersects(WIDE_CHAR_SPACERS) { - start_offset += first_cell.c.len_utf8(); - if line_start == hovered { - hovered_point_byte_offset = Some(0); + if line_start == hovered { + hovered_point_byte_offset = Some(0); + if first_cell.c != ' ' { + hovered_word_start_offset = Some(0); } } @@ -247,27 +251,44 @@ fn path_match( if cell.point > line_end { break; } - let is_spacer = cell.flags.intersects(WIDE_CHAR_SPACERS); - if cell.point == hovered { - debug_assert!(hovered_point_byte_offset.is_none()); - if start_offset > 0 && cell.flags.contains(Flags::WIDE_CHAR_SPACER) { - // If we hovered on a trailing spacer, back up to the end of the previous char's bytes. - start_offset -= 1; + + if !cell.flags.intersects(WIDE_CHAR_SPACERS) { + prev_len = line.len(); + match cell.c { + ' ' | '\t' => { + if hovered_point_byte_offset.is_some() && !prev_char_is_space { + if hovered_word_end_offset.is_none() { + hovered_word_end_offset = Some(line.len()); + } + } + line.push(' '); + prev_char_is_space = true; + } + c @ _ => { + if hovered_point_byte_offset.is_none() && prev_char_is_space { + hovered_word_start_offset = Some(line.len()); + } + line.push(c); + prev_char_is_space = false; + } } - hovered_point_byte_offset = Some(start_offset); - } else if cell.point < hovered && !is_spacer { - start_offset += cell.c.len_utf8(); } - if !is_spacer { - line.push(match cell.c { - '\t' => ' ', - c @ _ => c, - }); + if cell.point == hovered { + debug_assert!(hovered_point_byte_offset.is_none()); + hovered_point_byte_offset = Some(prev_len); } } let line = line.trim_ascii_end(); let hovered_point_byte_offset = hovered_point_byte_offset?; + let hovered_word_range = { + let word_start_offset = hovered_word_start_offset.unwrap_or(0); + (word_start_offset != 0) + .then_some(word_start_offset..hovered_word_end_offset.unwrap_or(line.len())) + }; + if line.len() <= hovered_point_byte_offset { + return None; + } let found_from_range = |path_range: Range, link_range: Range, position: Option<(u32, Option)>| { @@ -313,10 +334,27 @@ fn path_match( for regex in path_hyperlink_regexes { let mut path_found = false; - for captures in regex.captures_iter(&line) { + for (line_start_offset, captures) in once( + regex + .captures_iter(&line) + .next() + .map(|captures| (0, captures)), + ) + .chain(once_with(|| { + if let Some(hovered_word_range) = &hovered_word_range { + regex + .captures_iter(&line[hovered_word_range.clone()]) + .next() + .map(|captures| (hovered_word_range.start, captures)) + } else { + None + } + })) + .flatten() + { path_found = true; let match_range = captures.get(0).unwrap().range(); - let (path_range, line_column) = if let Some(path) = captures.name("path") { + let (mut path_range, line_column) = if let Some(path) = captures.name("path") { let parse = |name: &str| { captures .name(name) @@ -330,10 +368,15 @@ fn path_match( } else { (match_range.clone(), None) }; - let link_range = captures + let mut link_range = captures .name("link") .map_or_else(|| match_range.clone(), |link| link.range()); + path_range.start += line_start_offset; + path_range.end += line_start_offset; + link_range.start += line_start_offset; + link_range.end += line_start_offset; + if !link_range.contains(&hovered_point_byte_offset) { // No match, just skip. continue; @@ -638,9 +681,6 @@ mod tests { test_path!( "‹«🦀 multiple_👉same_line 🦀» 🚣«4» 🏛️«2»›: 🦀 multiple_same_line 🦀 🚣4 🏛️2:" ); - test_path!( - "🦀 multiple_same_line 🦀 🚣4 🏛️2 ‹«🦀 multiple_👉same_line 🦀» 🚣«4» 🏛️«2»›:" - ); // ls output (tab separated) test_path!( @@ -977,7 +1017,7 @@ mod tests { use crate::TerminalSettings; use alacritty_terminal::{ event::VoidListener, - grid::Dimensions, + grid::Scroll, index::{Column, Point as AlacPoint}, term::test::mock_term, term::{Term, search::Match}, @@ -986,14 +1026,20 @@ mod tests { use std::{cell::RefCell, rc::Rc}; use util_macros::perf; - fn build_test_term(line: &str) -> (Term, AlacPoint) { - let content = line.repeat(500); - let term = mock_term(&content); - let point = AlacPoint::new( - term.grid().bottommost_line() - 1, - Column(term.grid().last_column().0 / 2), - ); - + fn build_test_term( + line: &str, + repeat: usize, + hover_offset_column: usize, + ) -> (Term, AlacPoint) { + let content = line.repeat(repeat); + let mut term = mock_term(&content); + term.resize(TermSize { + columns: 1024, + screen_lines: 10, + }); + term.scroll_display(Scroll::Top); + let point = + AlacPoint::new(Line(term.topmost_line().0 + 3), Column(hover_offset_column)); (term, point) } @@ -1002,11 +1048,14 @@ mod tests { const LINE: &str = " Compiling terminal v0.1.0 (/Hyperlinks/Bench/Source/zed-hyperlinks/crates/terminal)\r\n"; thread_local! { static TEST_TERM_AND_POINT: (Term, AlacPoint) = - build_test_term(LINE); + build_test_term(LINE, 500, 50); } TEST_TERM_AND_POINT.with(|(term, point)| { - assert!( - find_from_grid_point_bench(term, *point).is_some(), + assert_eq!( + find_from_grid_point_bench(term, *point) + .map(|(path, ..)| path) + .unwrap_or_default(), + "/Hyperlinks/Bench/Source/zed-hyperlinks/crates/terminal", "Hyperlink should have been found" ); }); @@ -1017,11 +1066,14 @@ mod tests { const LINE: &str = " --> /Hyperlinks/Bench/Source/zed-hyperlinks/crates/terminal/terminal.rs:1000:42\r\n"; thread_local! { static TEST_TERM_AND_POINT: (Term, AlacPoint) = - build_test_term(LINE); + build_test_term(LINE, 500, 50); } TEST_TERM_AND_POINT.with(|(term, point)| { - assert!( - find_from_grid_point_bench(term, *point).is_some(), + assert_eq!( + find_from_grid_point_bench(term, *point) + .map(|(path, ..)| path) + .unwrap_or_default(), + "/Hyperlinks/Bench/Source/zed-hyperlinks/crates/terminal/terminal.rs:1000:42", "Hyperlink should have been found" ); }); @@ -1032,11 +1084,111 @@ mod tests { const LINE: &str = "Cargo.toml experiments notebooks rust-toolchain.toml tooling\r\n"; thread_local! { static TEST_TERM_AND_POINT: (Term, AlacPoint) = - build_test_term(LINE); + build_test_term(LINE, 500, 60); } TEST_TERM_AND_POINT.with(|(term, point)| { - assert!( - find_from_grid_point_bench(term, *point).is_some(), + assert_eq!( + find_from_grid_point_bench(term, *point) + .map(|(path, ..)| path) + .unwrap_or_default(), + "rust-toolchain.toml", + "Hyperlink should have been found" + ); + }); + } + + #[perf] + // https://github.com/zed-industries/zed/pull/44407 + pub fn pr_44407_hyperlink_benchmark() { + const LINE: &str = "-748, 706, 163, 222, -980, 949, 381, -568, 199, 501, 760, -821, 90, -451, 183, 867, -351, -810, -762, -109, 423, 84, 14, -77, -820, -345, 74, -791, 930, -618, -900, 862, -959, 289, -19, 471, -757, 793, 155, -554, 249, 830, 402, 732, -731, -866, -720, -703, -257, -439, 731, 872, -489, 676, -167, 613, -698, 415, -80, -453, -896, 333, -511, 621, -450, 624, -309, -575, 177, 141, 891, -104, -97, -367, -599, -675, 607, -225, -760, 552, -465, 804, 55, 282, 104, -929, -252,\ +-311, 900, 550, 599, -80, 774, 553, 837, -395, 541, 953, 154, -396, -596, -111, -802, -221, -337, -633, -73, -527, -82, -658, -264, 222, 375, 434, 204, -756, -703, 303, 239, -257, -365, -351, 904, 364, -743, -484, 655, -542, 446, 888, 632, -167, -260, 716, 150, 806, 723, 513, -118, -323, -683, 983, -564, 358, -16, -287, 277, -607, 87, 365, -1, 164, 401, 257, 369, -893, 145, -969, 375, -53, 541, -408, -865, 753, 258, 337, -886, 593, -378, -528, 191, 204, 566, -61, -621, 769, 524, -628, 6,\ +249, 896, -785, -776, 321, -681, 604, -740, 886, 426, -480, -983, 23, -247, 125, -666, 913, 842, -460, -797, -483, -58, -565, -587, -206, 197, 715, 764, -97, 457, -149, -226, 261, 194, -390, 431, 180, -778, 829, -657, -668, 397, 859, 152, -178, 677, -18, 687, -247, 96, 466, -572, 478, 622, -143, -25, -471, 265, 335, 957, 152, -951, -647, 670, 57, 152, -115, 206, 87, 629, -798, -125, -725, -31, 844, 398, -876, 44, 963, -211, 518, -8, -103, -999, 948, 823, 149, -803, 769, -236, -683, 527,\ +-108, -36, 18, -437, 687, -305, -526, 972, -965, 276, 420, -259, -379, -142, -747, 600, -578, 197, 673, 890, 324, -931, 755, -765, -422, 785, -369, -110, -505, 532, -208, -438, 713, 110, 853, 996, -360, 823, 289, -699, 629, -661, 560, -329, -323, 439, 571, -537, 644, -84, 25, -536, -161, 112, 169, -922, -537, -734, -423, 37, 451, -149, 408, 18, -672, 206, -784, 444, 593, -241, 502, -259, -798, -352, -658, 712, -675, -734, 627, -620, 64, -554, 999, -537, -160, -641, 464, 894, 29, 322, 566,\ +-510, -749, 982, 204, 967, -261, -986, -136, 251, -598, 995, -831, 891, 22, 761, -783, -415, 125, 470, -919, -97, -668, 85, 205, -175, -550, 502, 652, -468, 798, 775, -216, 89, -433, -24, -621, 877, -126, 951, 809, 782, 156, -618, -841, -463, 19, -723, -904, 550, 263, 991, -758, -114, 446, -731, -623, -634, 462, 48, 851, 333, -846, 480, 892, -966, -910, -436, 317, -711, -341, -294, 124, 238, -214, -281, 467, -950, -342, 913, -90, -388, -573, 740, -883, -451, 493, -500, 863, 930, 127, 530,\ +-810, 540, 541, -664, -951, -227, -420, -476, -581, -534, 549, 253, 984, -985, -84, -521, 538, 484, -440, 371, 784, -306, -850, 530, -133, 251, -799, 446, -170, -243, -674, 769, 646, 778, -680, -714, -442, 804, 901, -774, 69, 307, -293, 755, 443, 224, -918, -771, 723, 40, 132, 568, -847, -47, 844, 69, 986, -293, -459, 313, 155, 331, 69, 280, -637, 569, 104, -119, -988, 252, 857, -590, 810, -891, 484, 566, -934, -587, -290, 566, 587, 489, 870, 280, 454, -252, 613, -701, -278, 195, -198,\ +683, 533, -372, 707, -152, 371, 866, 609, -5, -372, -30, -694, 552, 192, 452, -663, 350, -985, 10, 884, 813, -592, -331, -470, 711, -941, 928, 379, -339, 220, 999, 376, 507, 179, 916, 84, 104, 392, 192, 299, -860, 218, -698, -919, -452, 37, 850, 5, -874, 287, 123, -746, -575, 776, -909, 118, 903, -275, 450, -996, -591, -920, -850, 453, -896, 73, 83, -535, -20, 287, -765, 442, 808, 45, 445, 202, 917, -208, 783, 790, -534, 373, -129, 556, -757, -69, 459, -163, -59, 265, -563, -889, 635,\ +-583, -261, -790, 799, 826, 953, 85, 619, 334, 842, 672, -869, -4, -833, 315, 942, -524, 579, 926, 628, -404, 128, -629, 161, 568, -117, -526, 223, -876, 906, 176, -549, -317, 381, 375, -801, -416, 647, 335, 253, -386, -375, -254, 635, 352, 317, 398, -422, 111, 201, 220, 554, -972, 853, 378, 956, 942, -857, -289, -333, -180, 488, -814, -42, -595, 721, 39, 644, 721, -242, -44, 643, -457, -419, 560, -863, 974, 458, 222, -882, 526, -243, -318, -343, -707, -401, 117, 677, -489, 546, -903,\ +-960, -881, -684, 125, -928, -995, -692, -773, 647, -718, -862, -814, 671, 664, -130, -856, -674, 653, 711, 194, -685, -160, 138, -27, -128, -671, -242, 526, 494, -674, 424, -921, -778, 313, -237, 332, 913, 252, 808, -936, 289, 755, 52, -139, 57, -19, -827, -775, -561, -14, 107, -84, 622, -303, -747, 258, -942, 290, 211, -919, -207, 797, 95, 794, -830, -181, -788, 757, 75, -946, -949, -988, 152, 340, 732, 886, -891, -642, -666, 321, -910, 841, 632, 298, 55, -349, 498, 287, -711, 97, 305,\ +-974, -987, 790, -64, 605, -583, -821, 345, 887, -861, 548, 894, 288, 452, 556, -448, 813, 420, 545, 967, 127, -947, 19, -314, -607, -513, -851, 254, -290, -938, -783, -93, 474, 368, -485, -935, -539, 81, 404, -283, 779, 345, -164, 53, 563, -771, 911, -323, 522, -998, 315, 415, 460, 58, -541, -878, -152, -886, 201, -446, -810, 549, -142, -575, -632, 521, 549, 209, -681, 998, 798, -611, -919, -708, -4, 677, -172, 588, 750, -435, 508, 609, 498, -535, -691, -738, 85, 615, 705, 169, 425,\ +-669, -491, -783, 73, -847, 228, -981, -812, -229, 950, -904, 175, -438, 632, -556, 910, 173, 576, -751, -53, -169, 635, 607, -944, -13, -84, 105, -644, 984, 935, 259, -445, 620, -405, 832, 167, 114, 209, -181, -944, -496, 693, -473, 137, 38, -873, -334, -353, -57, 397, 944, 698, 811, -401, 712, -667, 905, 276, -653, 368, -543, -349, 414, 287, 894, 935, 461, 55, 741, -623, -660, -773, 617, 834, 278, -121, 52, 495, -855, -440, -210, -99, 279, -661, 540, 934, 540, 784, 895, 268, -503, 513,\ +-484, -352, 528, 341, -451, 885, -71, 799, -195, -885, -585, -233, 92, 453, 994, 464, 694, 190, -561, -116, 675, -775, -236, 556, -110, -465, 77, -781, 507, -960, -410, 229, -632, 717, 597, 429, 358, -430, -692, -825, 576, 571, 758, -891, 528, -267, 190, -869, 132, -811, 796, 750, -596, -681, 870, 360, 969, 860, -412, -567, 694, -86, -498, 38, -178, -583, -778, 412, 842, -586, 722, -192, 350, 363, 81, -677, -163, 564, 543, 671, 110, 314, 739, -552, -224, -644, 922, 685, 134, 613, 793,\ +-363, -244, -284, -257, -561, 418, 988, 333, 110, -966, 790, 927, 536, -620, -309, -358, 895, -867, -796, -357, 308, -740, 287, -732, -363, -969, 658, 711, 511, 256, 590, -574, 815, -845, -84, 546, -581, -71, -334, -890, 652, -959, 320, -236, 445, -851, 825, -756, -4, 877, 308, 573, -117, 293, 686, -483, 391, 342, -550, -982, 713, 886, 552, 474, -673, 283, -591, -383, 988, 435, -131, 708, -326, -884, 87, 680, -818, -408, -486, 813, -307, -799, 23, -497, 802, -146, -100, 541, 7, -493, 577,\ +50, -270, 672, 834, 111, -788, 247, 337, 628, -33, -964, -519, 683, 54, -703, 633, -127, -448, 759, -975, 696, 2, -870, -760, 67, 696, 306, 750, 615, 155, -933, -568, 399, 795, 164, -460, 205, 439, -526, -691, 35, -136, -481, -63, 73, -598, 748, 133, 874, -29, 4, -73, 472, 389, 962, 231, -328, 240, 149, 959, 46, -207, 72, -514, -608, 0, -14, 32, 374, -478, -806, 919, -729, -286, 652, 109, 509, -879, -979, -865, 584, -92, -346, -992, 781, 401, 575, 993, -746, -33, 684, -683, 750, -105,\ +-425, -508, -627, 27, 770, -45, 338, 921, -139, -392, -933, 634, 563, 224, -780, 921, 991, 737, 22, 64, 414, -249, -687, 869, 50, 759, -97, 515, 20, -775, -332, 957, 138, -542, -835, 591, -819, 363, -715, -146, -950, -641, -35, -435, -407, -548, -984, 383, -216, -559, 853, 4, -410, -319, -831, -459, -628, -819, -324, 755, 696, -192, 238, -234, -724, -445, 915, 302, -708, 484, 224, -641, 25, -771, 528, -106, -744, -588, 913, -554, -515, -239, -843, -812, -171, 721, 543, -269, 440, 151,\ +996, -723, -557, -522, -280, -514, -593, 208, 715, 404, 353, 270, -483, -785, 318, -313, 798, 638, 764, 748, -929, -827, -318, -56, 389, -546, -958, -398, 463, -700, 461, 311, -787, -488, 877, 456, 166, 535, -995, -189, -715, 244, 40, 484, 212, -329, -351, 638, -69, -446, -292, 801, -822, 490, -486, -185, 790, 370, -340, 401, -656, 584, 561, -749, 269, -19, -294, -111, 975, 874, -73, 851, 231, -331, -684, 460, 765, -654, -76, 10, 733, 520, 521, 416, -958, -202, -186, -167, 175, 343, -50,\ +673, -763, -854, -977, -17, -853, -122, -25, 180, 149, 268, 874, -816, -745, 747, -303, -959, 390, 509, 18, -66, 275, -277, 9, 837, -124, 989, -542, -649, -845, 894, 926, 997, -847, -809, -579, -96, -372, 766, 238, -251, 503, 559, 276, -281, -102, -735, 815, 109, 175, -10, 128, 543, -558, -707, 949, 996, -422, -506, 252, 702, -930, 552, -961, 584, -79, -177, 341, -275, 503, -21, 677, -545, 8, -956, -795, -870, -254, 170, -502, -880, 106, 174, 459, 603, -600, -963, 164, -136, -641, -309,\ +-380, -707, -727, -10, 727, 952, 997, -731, -133, 269, 287, 855, 716, -650, 479, 299, -839, -308, -782, 769, 545, 663, -536, -115, 904, -986, -258, -562, 582, 664, 408, -525, -889, 471, -370, -534, -220, 310, 766, 931, -193, -897, -192, -74, -365, -256, -359, -328, 658, -691, -431, 406, 699, 425, 713, -584, -45, -588, 289, 658, -290, -880, -987, -444, 371, 904, -155, 81, -278, -708, -189, -78, 655, 342, -998, -647, -734, -218, 726, 619, 663, 744, 518, 60, -409, 561, -727, -961, -306,\ +-147, -550, 240, -218, -393, 267, 724, 791, -548, 480, 180, -631, 825, -170, 107, 227, -691, 905, -909, 359, 227, 287, 909, 632, -89, -522, 80, -429, 37, 561, -732, -474, 565, -798, -460, 188, 507, -511, -654, 212, -314, -376, -997, -114, -708, 512, -848, 781, 126, -956, -298, 354, -400, -121, 510, 445, 926, 27, -708, 676, 248, 834, 542, 236, -105, -153, 102, 128, 96, -348, -626, 598, 8, 978, -589, -461, -38, 381, -232, -817, 467, 356, -151, -460, 429, -408, 425, 618, -611, -247, 819,\ +963, -160, 1000, 141, -647, -875, 108, 790, -127, 463, -37, -195, -542, 12, 845, -384, 770, -129, 315, 826, -942, 430, 146, -170, -583, -903, -489, 497, -559, -401, -29, -129, -411, 166, 942, -646, -862, -404, 785, 777, -111, -481, -738, 490, 741, -398, 846, -178, -509, -661, 748, 297, -658, -567, 531, 427, -201, -41, -808, -668, 782, -860, -324, 249, 835, -234, 116, 542, -201, 328, 675, 480, -906, 188, 445, 63, -525, 811, 277, 133, 779, -680, 950, -477, -306, -64, 552, -890, -956, 169,\ +442, 44, -169, -243, -242, 423, -884, -757, -403, 739, -350, 383, 429, 153, -702, -725, 51, 310, 857, -56, 538, 46, -311, 132, -620, -297, -124, 534, 884, -629, -117, 506, -837, -100, -27, -381, -735, 262, 843, 703, 260, -457, 834, 469, 9, 950, 59, 127, -820, 518, 64, -783, 659, -608, -676, 802, 30, 589, 246, -369, 361, 347, 534, -376, 68, 941, 709, 264, 384, 481, 628, 199, -568, -342, -337, 853, -804, -858, -169, -270, 641, -344, 112, 530, -773, -349, -135, -367, -350, -756, -911, 180,\ +-660, 116, -478, -265, -581, 510, 520, -986, 935, 219, 522, 744, 47, -145, 917, 638, 301, 296, 858, -721, 511, -816, 328, 473, 441, 697, -260, -673, -379, 893, 458, 154, 86, 905, 590, 231, -717, -179, 79, 272, -439, -192, 178, -200, 51, 717, -256, -358, -626, -518, -314, -825, -325, 588, 675, -892, -798, 448, -518, 603, -23, 668, -655, 845, -314, 783, -347, -496, 921, 893, -163, -748, -906, 11, -143, -64, 300, 336, 882, 646, 533, 676, -98, -148, -607, -952, -481, -959, -874, 764, 537,\ +736, -347, 646, -843, 966, -916, -718, -391, -648, 740, 755, 919, -608, 388, -655, 68, 201, 675, -855, 7, -503, 881, 760, 669, 831, 721, -564, -445, 217, 331, 970, 521, 486, -254, 25, -259, 336, -831, 252, -995, 908, -412, -240, 123, -478, 366, 264, -504, -843, 632, -288, 896, 301, 423, 185, 318, 380, 457, -450, -162, -313, 673, -963, 570, 433, -548, 107, -39, -142, -98, -884, -3, 599, -486, -926, 923, -82, 686, 290, 99, -382, -789, 16, 495, 570, 284, 474, -504, -201, -178, -1, 592, 52,\ +827, -540, -151, -991, 130, 353, -420, -467, -661, 417, -690, 942, 936, 814, -566, -251, -298, 341, -139, 786, 129, 525, -861, 680, 955, -245, -50, 331, 412, -38, -66, 611, -558, 392, -629, -471, -68, -535, 744, 495, 87, 558, 695, 260, -308, 215, -464, 239, -50, 193, -540, 184, -8, -194, 148, 898, -557, -21, 884, 644, -785, -689, -281, -737, 267, 50, 206, 292, 265, 380, -511, 310, 53, 375, -497, -40, 312, -606, -395, 142, 422, 662, -584, 72, 144, 40, -679, -593, 581, 689, -829, 442, 822,\ +977, -832, -134, -248, -207, 248, 29, 259, 189, 592, -834, -866, 102, 0, 340, 25, -354, -239, 420, -730, -992, -925, -314, 420, 914, 607, -296, -415, -30, 813, 866, 153, -90, 150, -81, 636, -392, -222, -835, 482, -631, -962, -413, -727, 280, 686, -382, 157, -404, -511, -432, 455, 58, 108, -408, 290, -829, -252, 113, 550, -935, 925, 422, 38, 789, 361, 487, -460, -769, -963, -285, 206, -799, -488, -233, 416, 143, -456, 753, 520, 599, 621, -168, 178, -841, 51, 952, 374, 166, -300, -576, 844,\ +-656, 90, 780, 371, 730, -896, -895, -386, -662, 467, -61, 130, -362, -675, -113, 135, -761, -55, 408, 822, 675, -347, 725, 114, 952, -510, -972, 390, -413, -277, -52, 315, -80, 401, -712, 147, -202, 84, 214, -178, 970, -571, -210, 525, -887, -863, 504, 192, 837, -594, 203, -876, -209, 305, -826, 377, 103, -928, -803, -956, 949, -868, -547, 824, -994, 516, 93, -524, -866, -890, -988, -501, 15, -6, 413, -825, 304, -818, -223, 525, 176, 610, 828, 391, 940, 540, -831, 650, 438, 589, 941, 57,\ +523, 126, 221, 860, -282, -262, -226, 764, 743, -640, 390, 384, -434, 608, -983, 566, -446, 618, 456, -176, -278, 215, 871, -180, 444, -931, -200, -781, 404, 881, 780, -782, 517, -739, -548, -811, 201, -95, -249, -228, 491, -299, 700, 964, -550, 108, 334, -653, 245, -293, -552, 350, -685, -415, -818, 216, -194, -255, 295, 249, 408, 351, 287, 379, 682, 231, -693, 902, -902, 574, 937, -708, -402, -460, 827, -268, 791, 343, -780, -150, -738, 920, -430, -88, -361, -588, -727, -47, -297, 662,\ +-840, -637, -635, 916, -857, 938, 132, -553, 391, -522, 640, 626, 690, 833, 867, -555, 577, 226, 686, -44, 0, -965, 651, -1, 909, 595, -646, 740, -821, -648, -962, 927, -193, 159, 490, 594, -189, 707, -884, 759, -278, -160, -566, -340, 19, 862, -440, 445, -598, 341, 664, -311, 309, -159, 19, -672, 705, -646, 976, 247, 686, -830, -27, -667, 81, 399, -423, -567, 945, 38, 51, 740, 621, 204, -199, -908, -593, 424, 250, -561, 695, 9, 520, 878, 120, -109, 42, -375, -635, -711, -687, 383, -278,\ +36, 970, 925, 864, 836, 309, 117, 89, 654, -387, 346, -53, 617, -164, -624, 184, -45, 852, 498, -513, 794, -682, -576, 13, -147, 285, -776, -886, -96, 483, 994, -188, 346, -629, -848, 738, 51, 128, -898, -753, -906, 270, -203, -577, 48, -243, -210, 666, 353, 636, -954, 862, 560, -944, -877, -137, 440, -945, -316, 274, -211, -435, 615, -635, -468, 744, 948, -589, 525, 757, -191, -431, 42, 451, -160, -827, -991, 324, 697, 342, -610, 894, -787, -384, 872, 734, 878, 70, -260, 57, 397, -518,\ +629, -510, -94, 207, 214, -625, 106, -882, -575, 908, -650, 723, -154, 45, 108, -69, -565, 927, -68, -351, 707, -282, 429, -889, -596, 848, 578, -492, 41, -822, -992, 168, -286, -780, 970, 597, -293, -12, 367, 708, -415, 194, -86, -390, 224, 69, -368, -674, 1000, -672, 356, -202, -169, 826, 476, -285, 29, -448, 545, 186, 319, 67, 705, 412, 225, -212, -351, -391, -783, -9, 875, -59, -159, -123, -151, -296, 871, -638, 359, 909, -945, 345, -16, -562, -363, -183, -625, -115, -571, -329, 514,\ +99, 263, 463, -39, 597, -652, -349, 246, 77, -127, -563, -879, -30, 756, 777, -865, 675, -813, -501, 871, -406, -627, 834, -609, -205, -812, 643, -204, 291, -251, -184, -584, -541, 410, -573, -600, 908, -871, -687, 296, -713, -139, -778, -790, 347, -52, -400, 407, -653, 670, 39, -856, 904, 433, 392, 590, -271, -144, -863, 443, 353, 468, -544, 486, -930, 458, -596, -890, 163, 822, 768, 980, -783, -792, 126, 386, 367, -264, 603, -61, 728, 160, -4, -837, 832, 591, 436, 518, 796, -622, -867,\ +-669, -947, 253, 100, -792, 841, 413, 833, -249, -550, 282, -825, 936, -348, 898, -451, -283, 818, -237, 630, 216, -499, -637, -511, 767, -396, 221, 958, -586, -920, 401, -313, -580, -145, -270, 118, 497, 426, -975, 480, -445, -150, -721, -929, 439, -893, 902, 960, -525, -793, 924, 563, 683, -727, -86, 309, 432, -762, -345, 371, -617, 149, -215, -228, 505, 593, -20, -292, 704, -999, 149, -104, 819, -414, -443, 517, -599, -5, 145, -24, -993, -283, 904, 174, -112, -276, -860, 44, -257,\ +-931, -821, -667, 540, 421, 485, 531, 407, 833, 431, -415, 878, 503, -901, 639, -608, 896, 860, 927, 424, 113, -808, -323, 729, 382, -922, 548, -791, -379, 207, 203, 559, 537, 137, 999, -913, -240, 942, 249, 616, 775, -4, 915, 855, -987, -234, -384, 948, -310, -542, 125, -289, -599, 967, -492, -349, -552, 562, -926, 632, -164, 217, -165, -496, 847, 684, -884, 457, -748, -745, -38, 93, 961, 934, 588, 366, -130, 851, -803, -811, -211, 428, 183, -469, 888, 596, -475, -899, -681, 508, 184,\ +921, 863, -610, -416, -119, -966, -686, 210, 733, 715, -889, -925, -434, -566, -455, 596, -514, 983, 755, -194, -802, -313, 91, -541, 808, -834, 243, -377, 256, 966, -402, -773, -308, -605, 266, 866, 118, -425, -531, 498, 666, 813, -267, 830, 69, -869, -496, 735, 28, 488, -645, -493, -689, 170, -940, 532, 844, -658, -617, 408, -200, 764, -665, 568, 342, 621, 908, 471, 280, 859, 709, 898, 81, -547, 406, 514, -595, 43, -824, -696, -746, -429, -59, -263, -813, 233, 279, -125, 687, -418,\ +-530, 409, 614, 803, -407, 78, -676, -39, -887, -141, -292, 270, -343, 400, 907, 588, 668, 899, 973, 103, -101, -11, 397, -16, 165, 705, -410, -585, 316, 391, -346, -336, 957, -118, -538, -441, -845, 121, 591, -359, -188, -362, -208, 27, -925, -157, -495, -177, -580, 9, 531, -752, 94, 107, 820, 769, -500, 852, 617, 145, 355, 34, -463, -265, -709, -111, -855, -405, 560, 470, 3, -177, -164, -249, 450, 662, 841, -689, -509, 987, -33, 769, 234, -2, 203, 780, 744, -895, 497, -432, -406, -264,\ +-71, 124, 778, -897, 495, 127, -76, 52, -768, 205, 464, -992, 801, -83, -806, 545, -316, 146, 772, 786, 289, -936, 145, -30, -722, -455, 270, 444, 427, -482, 383, -861, 36, 630, -404, 83, 864, 743, -351, -846, 315, -837, 357, -195, 450, -715, 227, -942, 740, -519, 476, 716, 713, 169, 492, -112, -49, -931, 866, 95, -725, 198, -50, -17, -660, 356, -142, -781, 53, 431, 720, 143, -416, 446, -497, 490, -96, 157, 239, 487, -337, -224, -445, 813, 92, -22, 603, 424, 952, -632, -367, 898, -927,\ +884, -277, -187, -777, 537, -575, -313, 347, -33, 800, 672, -919, -541, 5, -270, -94, -265, -793, -183, -761, -516, -608, -218, 57, -889, -912, 508, 93, -90, 34, 530, 201, 999, -37, -186, -62, -980, 239, 902, 983, -287, -634, 524, -772, 470, -961, 32, 162, 315, -411, 400, -235, -283, -787, -703, 869, 792, 543, -274, 239, 733, -439, 306, 349, 579, -200, -201, -824, 384, -246, 133, -508, 770, -102, 957, -825, 740, 748, -376, 183, -426, 46, 668, -886, -43, -174, 672, -419, 390, 927, 1000,\ +318, 886, 47, 908, -540, -825, -5, 314, -999, 354, -603, 966, -633, -689, 985, 534, -290, 167, -652, -797, -612, -79, 488, 622, -464, -950, 595, 897, 704, -238, -395, 125, 831, -180, 226, -379, 310, 564, 56, -978, 895, -61, 686, -251, 434, -417, 161, -512, 752, 528, -589, -425, 66, -925, -157, 1000, 96, 256, -239, -784, -882, -464, -909, 663, -177, -678, -441, 669, -564, -201, -121, -743, 187, -107, -768, -682, 355, 161, 411, 984, -954, 166, -842, -755, 267, -709, 372, -699, -272, -850,\ +403, -839, 949, 622, -62, 51, 917, 70, 528, -558, -632, 832, 276, 61, -445, -195, 960, 846, -474, 764, 879, -411, 948, -62, -592, -123, -96, -551, -555, -724, 849, 250, -808, -732, 797, -839, -554, 306, -919, 888, 484, -728, 152, -122, -287, 16, -345, -396, -268, -963, -500, 433, 343, 418, -480, 828, 594, 821, -9, 933, -230, 707, -847, -610, -748, -234, 688, 935, 713, 865, -743, 293, -143, -20, 928, -906, -762, 528, 722, 412, -70, 622, -245, 539, -686, 730, -866, -705, 28, -916, -623,\ +-768, -614, -915, -123, -183, 680, -223, 515, -37, -235, -5, 260, 347, -239, -322, -861, -848, -936, 945, 721, -580, -639, 780, -153, -26, 685, 177, 587, 307, -915, 435, 658, 539, -229, -719, -171, -858, 162, 734, -539, -437, 246, 639, 765, -477, -342, -209, -284, -779, -414, -452, 914, 338, -83, 759, 567, 266, -485, 14, 225, 347, -432, -242, 997, -365, -764, 119, -641, -416, -388, -436, -388, -54, -649, -571, -920, -477, 714, -363, 836, 369, 702, 869, 503, -287, -679, 46, -666, -202,\ +-602, 71, -259, 967, 601, -571, -830, -993, -271, 281, -494, 482, -180, 572, 587, -651, -566, -448, -228, 511, -924, 832, -52, -712, 402, -644, -533, -865, 269, 965, 56, 675, 179, -338, -272, 614, 602, -283, 303, -70, 909, -942, 117, 839, 468, 813, -765, 884, -697, -813, 352, 374, -705, -295, 633, 211, -754, 597, -941, -142, -393, -469, -653, 688, 996, 911, 214, 431, 453, -141, 874, -81, -258, -735, -3, -110, -338, -929, -182, -306, -104, -840, -588, -759, -157, -801, 848, -698, 627, 914,\ +-33, -353, 425, 150, -798, 553, 934, -778, -196, -132, 808, 745, -894, 144, 213, 662, 273, -79, 454, -60, -467, 48, -15, -807, 69, -930, 749, 559, -867, -103, 258, -677, 750, -303, 846, -227, -936, 744, -770, 770, -434, 594, -477, 589, -612, 535, 357, -623, 683, 369, 905, 980, -410, -663, 762, -888, -563, -845, 843, 353, -491, 996, -255, -336, -132, 695, -823, 289, -143, 365, 916, 877, 245, -530, -848, -804, -118, -108, 847, 620, -355, 499, 881, 92, -640, 542, 38, 626, -260, -34, -378,\ +598, 890, 305, -118, 711, -385, 600, -570, 27, -129, -893, 354, 459, 374, 816, 470, 356, 661, 877, 735, -286, -780, 620, 943, -169, -888, 978, 441, -667, -399, 662, 249, 137, 598, -863, -453, 722, -815, -251, -995, -294, -707, 901, 763, 977, 137, 431, -994, 905, 593, 694, 444, -626, -816, 252, 282, 616, 841, 360, -932, 817, -908, 50, 394, -120, -786, -338, 499, -982, -95, -454, 838, -312, 320, -127, -653, 53, 16, 988, -968, -151, -369, -836, 293, -271, 483, 18, 724, -204, -965, 245, 310,\ +987, 552, -835, -912, -861, 254, 560, 124, 145, 798, 178, 476, 138, -311, 151, -907, -886, -592, 728, -43, -489, 873, -422, -439, -489, 375, -703, -459, 338, 418, -25, 332, -454, 730, -604, -800, 37, -172, -197, -568, -563, -332, 228, -182, 994, -123, 444, -567, 98, 78, 0, -504, -150, 88, -936, 199, -651, -776, 192, 46, 526, -727, -991, 534, -659, -738, 256, -894, 965, -76, 816, 435, -418, 800, 838, 67, -733, 570, 112, -514, -416\r\ +"; + thread_local! { + static TEST_TERM_AND_POINT: (Term, AlacPoint) = + build_test_term(&LINE, 5, 50); + } + TEST_TERM_AND_POINT.with(|(term, point)| { + assert_eq!( + find_from_grid_point_bench(term, *point) + .map(|(path, ..)| path) + .unwrap_or_default(), + "392", + "Hyperlink should have been found" + ); + }); + } + + #[perf] + // https://github.com/zed-industries/zed/issues/44510 + pub fn issue_44510_hyperlink_benchmark() { + const LINE: &str = "..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................\ +..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................\ +..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................\ +..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................\ +..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................\ +..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................\ +..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................\ +..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................\ +..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................\ +..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................\ +..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................\ +..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................\ +..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................\ +..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................\ +...............................................E.\r\ +"; + thread_local! { + static TEST_TERM_AND_POINT: (Term, AlacPoint) = + build_test_term(&LINE, 5, 50); + } + TEST_TERM_AND_POINT.with(|(term, point)| { + assert_eq!( + find_from_grid_point_bench(term, *point) + .map(|(path, ..)| path) + .unwrap_or_default(), + LINE.trim_end_matches(['.', '\r', '\n']), "Hyperlink should have been found" ); }); diff --git a/crates/terminal_view/src/terminal_element.rs b/crates/terminal_view/src/terminal_element.rs index fd9568b0c582d4c191267183e296976f3d429eb3..b5324b7c6c7e0c467c657b122717fbf17cf9f7b9 100644 --- a/crates/terminal_view/src/terminal_element.rs +++ b/crates/terminal_view/src/terminal_element.rs @@ -632,7 +632,7 @@ impl TerminalElement { ) -> impl Fn(&E, &mut Window, &mut App) { move |event, window, cx| { if steal_focus { - window.focus(&focus_handle); + window.focus(&focus_handle, cx); } else if !focus_handle.is_focused(window) { return; } @@ -661,7 +661,7 @@ impl TerminalElement { let terminal_view = terminal_view.clone(); move |e, window, cx| { - window.focus(&focus); + window.focus(&focus, cx); let scroll_top = terminal_view.read(cx).scroll_top; terminal.update(cx, |terminal, cx| { diff --git a/crates/terminal_view/src/terminal_panel.rs b/crates/terminal_view/src/terminal_panel.rs index fb660e759c75aee9752cbaa3bdc8c8e0a47615e3..738a0b4502642423377bdf69b49d26250536761f 100644 --- a/crates/terminal_view/src/terminal_panel.rs +++ b/crates/terminal_view/src/terminal_panel.rs @@ -351,7 +351,7 @@ impl TerminalPanel { } else if let Some(focus_on_pane) = focus_on_pane.as_ref().or_else(|| self.center.panes().pop()) { - focus_on_pane.focus_handle(cx).focus(window); + focus_on_pane.focus_handle(cx).focus(window, cx); } } pane::Event::ZoomIn => { @@ -397,7 +397,7 @@ impl TerminalPanel { .center .split(&pane, &new_pane, direction, cx) .log_err(); - window.focus(&new_pane.focus_handle(cx)); + window.focus(&new_pane.focus_handle(cx), cx); }) .ok(); }) @@ -419,7 +419,7 @@ impl TerminalPanel { pane.add_item(item, true, true, None, window, cx); }); self.center.split(&pane, &new_pane, direction, cx).log_err(); - window.focus(&new_pane.focus_handle(cx)); + window.focus(&new_pane.focus_handle(cx), cx); } } pane::Event::Focus => { @@ -790,8 +790,7 @@ impl TerminalPanel { } pane.update(cx, |pane, cx| { - let focus = pane.has_focus(window, cx) - || matches!(reveal_strategy, RevealStrategy::Always); + let focus = matches!(reveal_strategy, RevealStrategy::Always); pane.add_item(terminal_view, true, focus, None, window, cx); }); @@ -853,8 +852,7 @@ impl TerminalPanel { } pane.update(cx, |pane, cx| { - let focus = pane.has_focus(window, cx) - || matches!(reveal_strategy, RevealStrategy::Always); + let focus = matches!(reveal_strategy, RevealStrategy::Always); pane.add_item(terminal_view, true, focus, None, window, cx); }); @@ -941,7 +939,6 @@ impl TerminalPanel { cx: &mut Context, ) -> Task>> { let reveal = spawn_task.reveal; - let reveal_target = spawn_task.reveal_target; let task_workspace = self.workspace.clone(); cx.spawn_in(window, async move |terminal_panel, cx| { let project = terminal_panel.update(cx, |this, cx| { @@ -957,6 +954,14 @@ impl TerminalPanel { terminal_to_replace.set_terminal(new_terminal.clone(), window, cx); })?; + let reveal_target = terminal_panel.update(cx, |panel, _| { + if panel.center.panes().iter().any(|p| **p == task_pane) { + RevealTarget::Dock + } else { + RevealTarget::Center + } + })?; + match reveal { RevealStrategy::Always => match reveal_target { RevealTarget::Center => { @@ -998,7 +1003,7 @@ impl TerminalPanel { RevealStrategy::NoFocus => match reveal_target { RevealTarget::Center => { task_workspace.update_in(cx, |workspace, window, cx| { - workspace.active_pane().focus_handle(cx).focus(window); + workspace.active_pane().focus_handle(cx).focus(window, cx); })?; } RevealTarget::Dock => { @@ -1053,7 +1058,7 @@ impl TerminalPanel { .center .find_pane_in_direction(&self.active_pane, direction, cx) { - window.focus(&pane.focus_handle(cx)); + window.focus(&pane.focus_handle(cx), cx); } else { self.workspace .update(cx, |workspace, cx| { @@ -1171,64 +1176,67 @@ pub fn new_terminal_pane( let source = tab.pane.clone(); let item_id_to_move = item.item_id(); - let Ok(new_split_pane) = pane - .drag_split_direction() - .map(|split_direction| { - drop_closure_terminal_panel.update(cx, |terminal_panel, cx| { - let is_zoomed = if terminal_panel.active_pane == this_pane { - pane.is_zoomed() - } else { - terminal_panel.active_pane.read(cx).is_zoomed() - }; - let new_pane = new_terminal_pane( - workspace.clone(), - project.clone(), - is_zoomed, - window, - cx, - ); - terminal_panel.apply_tab_bar_buttons(&new_pane, cx); - terminal_panel.center.split( - &this_pane, - &new_pane, - split_direction, - cx, - )?; - anyhow::Ok(new_pane) - }) - }) - .transpose() - else { - return ControlFlow::Break(()); + // If no split direction, let the regular pane drop handler take care of it + let Some(split_direction) = pane.drag_split_direction() else { + return ControlFlow::Continue(()); }; - match new_split_pane.transpose() { - // Source pane may be the one currently updated, so defer the move. - Ok(Some(new_pane)) => cx - .spawn_in(window, async move |_, cx| { - cx.update(|window, cx| { - move_item( - &source, + // Gather data synchronously before deferring + let is_zoomed = drop_closure_terminal_panel + .upgrade() + .map(|terminal_panel| { + let terminal_panel = terminal_panel.read(cx); + if terminal_panel.active_pane == this_pane { + pane.is_zoomed() + } else { + terminal_panel.active_pane.read(cx).is_zoomed() + } + }) + .unwrap_or(false); + + let workspace = workspace.clone(); + let terminal_panel = drop_closure_terminal_panel.clone(); + + // Defer the split operation to avoid re-entrancy panic. + // The pane may be the one currently being updated, so we cannot + // call mark_positions (via split) synchronously. + cx.spawn_in(window, async move |_, cx| { + cx.update(|window, cx| { + let Ok(new_pane) = + terminal_panel.update(cx, |terminal_panel, cx| { + let new_pane = new_terminal_pane( + workspace, project, is_zoomed, window, cx, + ); + terminal_panel.apply_tab_bar_buttons(&new_pane, cx); + terminal_panel.center.split( + &this_pane, &new_pane, - item_id_to_move, - new_pane.read(cx).active_item_index(), - true, - window, + split_direction, cx, - ); + )?; + anyhow::Ok(new_pane) }) - .ok(); - }) - .detach(), - // If we drop into existing pane or current pane, - // regular pane drop handler will take care of it, - // using the right tab index for the operation. - Ok(None) => return ControlFlow::Continue(()), - err @ Err(_) => { - err.log_err(); - return ControlFlow::Break(()); - } - }; + else { + return; + }; + + let Some(new_pane) = new_pane.log_err() else { + return; + }; + + move_item( + &source, + &new_pane, + item_id_to_move, + new_pane.read(cx).active_item_index(), + true, + window, + cx, + ); + }) + .ok(); + }) + .detach(); } else if let Some(project_path) = item.project_path(cx) && let Some(entry_path) = project.read(cx).absolute_path(&project_path, cx) { @@ -1297,7 +1305,7 @@ fn add_paths_to_terminal( .active_item() .and_then(|item| item.downcast::()) { - window.focus(&terminal_view.focus_handle(cx)); + window.focus(&terminal_view.focus_handle(cx), cx); let mut new_text = paths.iter().map(|path| format!(" {path:?}")).join(""); new_text.push(' '); terminal_view.update(cx, |terminal_view, cx| { @@ -1451,7 +1459,7 @@ impl Render for TerminalPanel { .position(|pane| **pane == terminal_panel.active_pane) { let next_ix = (ix + 1) % panes.len(); - window.focus(&panes[next_ix].focus_handle(cx)); + window.focus(&panes[next_ix].focus_handle(cx), cx); } }), ) @@ -1463,7 +1471,7 @@ impl Render for TerminalPanel { .position(|pane| **pane == terminal_panel.active_pane) { let prev_ix = cmp::min(ix.wrapping_sub(1), panes.len() - 1); - window.focus(&panes[prev_ix].focus_handle(cx)); + window.focus(&panes[prev_ix].focus_handle(cx), cx); } }, )) @@ -1471,7 +1479,7 @@ impl Render for TerminalPanel { cx.listener(|terminal_panel, action: &ActivatePane, window, cx| { let panes = terminal_panel.center.panes(); if let Some(&pane) = panes.get(action.0) { - window.focus(&pane.read(cx).focus_handle(cx)); + window.focus(&pane.read(cx).focus_handle(cx), cx); } else { let future = terminal_panel.new_pane_with_cloned_active_terminal(window, cx); @@ -1490,7 +1498,7 @@ impl Render for TerminalPanel { ) .log_err(); let new_pane = new_pane.read(cx); - window.focus(&new_pane.focus_handle(cx)); + window.focus(&new_pane.focus_handle(cx), cx); }, ); } diff --git a/crates/terminal_view/src/terminal_scrollbar.rs b/crates/terminal_view/src/terminal_scrollbar.rs index 871bb602306cccc92b8cffe62c4912c42b7a87e2..82ca0b4097dad1be899879b0241aed50d8e60bfa 100644 --- a/crates/terminal_view/src/terminal_scrollbar.rs +++ b/crates/terminal_view/src/terminal_scrollbar.rs @@ -50,28 +50,24 @@ impl ScrollableHandle for TerminalScrollHandle { let state = self.state.borrow(); size( Pixels::ZERO, - state - .total_lines - .checked_sub(state.viewport_lines) - .unwrap_or(0) as f32 - * state.line_height, + state.total_lines.saturating_sub(state.viewport_lines) as f32 * state.line_height, ) } fn offset(&self) -> Point { let state = self.state.borrow(); - let scroll_offset = state.total_lines - state.viewport_lines - state.display_offset; - Point::new( - Pixels::ZERO, - -(scroll_offset as f32 * self.state.borrow().line_height), - ) + let scroll_offset = state + .total_lines + .saturating_sub(state.viewport_lines) + .saturating_sub(state.display_offset); + Point::new(Pixels::ZERO, -(scroll_offset as f32 * state.line_height)) } fn set_offset(&self, point: Point) { let state = self.state.borrow(); let offset_delta = (point.y / state.line_height).round() as i32; - let max_offset = state.total_lines - state.viewport_lines; + let max_offset = state.total_lines.saturating_sub(state.viewport_lines); let display_offset = (max_offset as i32 + offset_delta).clamp(0, max_offset as i32); self.future_display_offset diff --git a/crates/terminal_view/src/terminal_view.rs b/crates/terminal_view/src/terminal_view.rs index 98f7a17a2778e05b258f2ab6135cb94ba91ba547..e7e60ff4b31dfbdd16b7de8841285d81fc311fc5 100644 --- a/crates/terminal_view/src/terminal_view.rs +++ b/crates/terminal_view/src/terminal_view.rs @@ -8,8 +8,8 @@ mod terminal_slash_command; use assistant_slash_command::SlashCommandRegistry; use editor::{EditorSettings, actions::SelectAll, blink_manager::BlinkManager}; use gpui::{ - Action, AnyElement, App, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, - KeyContext, KeyDownEvent, Keystroke, MouseButton, MouseDownEvent, Pixels, Render, + Action, AnyElement, App, ClipboardEntry, DismissEvent, Entity, EventEmitter, FocusHandle, + Focusable, KeyContext, KeyDownEvent, Keystroke, MouseButton, MouseDownEvent, Pixels, Render, ScrollWheelEvent, Styled, Subscription, Task, WeakEntity, actions, anchored, deferred, div, }; use persistence::TERMINAL_DB; @@ -409,7 +409,7 @@ impl TerminalView { ) }); - window.focus(&context_menu.focus_handle(cx)); + window.focus(&context_menu.focus_handle(cx), cx); let subscription = cx.subscribe_in( &context_menu, window, @@ -687,12 +687,32 @@ impl TerminalView { ///Attempt to paste the clipboard into the terminal fn paste(&mut self, _: &Paste, _: &mut Window, cx: &mut Context) { - if let Some(clipboard_string) = cx.read_from_clipboard().and_then(|item| item.text()) { + let Some(clipboard) = cx.read_from_clipboard() else { + return; + }; + + if clipboard.entries().iter().any(|entry| match entry { + ClipboardEntry::Image(image) => !image.bytes.is_empty(), + _ => false, + }) { + self.forward_ctrl_v(cx); + return; + } + + if let Some(text) = clipboard.text() { self.terminal - .update(cx, |terminal, _cx| terminal.paste(&clipboard_string)); + .update(cx, |terminal, _cx| terminal.paste(&text)); } } + /// Emits a raw Ctrl+V so TUI agents can read the OS clipboard directly + /// and attach images using their native workflows. + fn forward_ctrl_v(&self, cx: &mut Context) { + self.terminal.update(cx, |term, _| { + term.input(vec![0x16]); + }); + } + fn send_text(&mut self, text: &SendText, _: &mut Window, cx: &mut Context) { self.clear_bell(cx); self.terminal.update(cx, |term, _| { diff --git a/crates/title_bar/build.rs b/crates/title_bar/build.rs new file mode 100644 index 0000000000000000000000000000000000000000..ef70268ad3127baf113824348cb3e8685392a52b --- /dev/null +++ b/crates/title_bar/build.rs @@ -0,0 +1,28 @@ +#![allow(clippy::disallowed_methods, reason = "build scripts are exempt")] + +fn main() { + println!("cargo::rustc-check-cfg=cfg(macos_sdk_26)"); + + #[cfg(target_os = "macos")] + { + use std::process::Command; + + let output = Command::new("xcrun") + .args(["--sdk", "macosx", "--show-sdk-version"]) + .output() + .unwrap(); + + let sdk_version = String::from_utf8(output.stdout).unwrap(); + let major_version: Option = sdk_version + .trim() + .split('.') + .next() + .and_then(|v| v.parse().ok()); + + if let Some(major) = major_version + && major >= 26 + { + println!("cargo:rustc-cfg=macos_sdk_26"); + } + } +} diff --git a/crates/title_bar/src/application_menu.rs b/crates/title_bar/src/application_menu.rs index 817b73c45ecd2df4a76e9a67f425b2b459c0c026..579e4dadbd590981a4aee15019bbe73e2bb28d5c 100644 --- a/crates/title_bar/src/application_menu.rs +++ b/crates/title_bar/src/application_menu.rs @@ -1,12 +1,7 @@ -use gpui::{Entity, OwnedMenu, OwnedMenuItem}; +use gpui::{Action, Entity, OwnedMenu, OwnedMenuItem, actions}; use settings::Settings; -#[cfg(not(target_os = "macos"))] -use gpui::{Action, actions}; - -#[cfg(not(target_os = "macos"))] use schemars::JsonSchema; -#[cfg(not(target_os = "macos"))] use serde::Deserialize; use smallvec::SmallVec; @@ -14,18 +9,23 @@ use ui::{ContextMenu, PopoverMenu, PopoverMenuHandle, Tooltip, prelude::*}; use crate::title_bar_settings::TitleBarSettings; -#[cfg(not(target_os = "macos"))] actions!( app_menu, [ - /// Navigates to the menu item on the right. + /// Activates the menu on the right in the client-side application menu. + /// + /// Does not apply to platform menu bars (e.g. on macOS). ActivateMenuRight, - /// Navigates to the menu item on the left. + /// Activates the menu on the left in the client-side application menu. + /// + /// Does not apply to platform menu bars (e.g. on macOS). ActivateMenuLeft ] ); -#[cfg(not(target_os = "macos"))] +/// Opens the named menu in the client-side application menu. +/// +/// Does not apply to platform menu bars (e.g. on macOS). #[derive(Clone, Deserialize, JsonSchema, PartialEq, Default, Action)] #[action(namespace = app_menu)] pub struct OpenApplicationMenu(String); diff --git a/crates/title_bar/src/platforms/platform_mac.rs b/crates/title_bar/src/platforms/platform_mac.rs index c7becde6c1af48bf37e06c0d2dcf991ad3c9f19f..5e8e4e5087054e59f66527915ae97e352a9ff525 100644 --- a/crates/title_bar/src/platforms/platform_mac.rs +++ b/crates/title_bar/src/platforms/platform_mac.rs @@ -1,6 +1,10 @@ -/// Use pixels here instead of a rem-based size because the macOS traffic -/// lights are a static size, and don't scale with the rest of the UI. -/// -/// Magic number: There is one extra pixel of padding on the left side due to -/// the 1px border around the window on macOS apps. +// Use pixels here instead of a rem-based size because the macOS traffic +// lights are a static size, and don't scale with the rest of the UI. +// +// Magic number: There is one extra pixel of padding on the left side due to +// the 1px border around the window on macOS apps. +#[cfg(macos_sdk_26)] +pub const TRAFFIC_LIGHT_PADDING: f32 = 78.; + +#[cfg(not(macos_sdk_26))] pub const TRAFFIC_LIGHT_PADDING: f32 = 71.; diff --git a/crates/title_bar/src/title_bar.rs b/crates/title_bar/src/title_bar.rs index 608fea7383176460cb4b7519824cd2dc118dbb69..9b75d35eccafa3c30f23329d9c0ee890ed2b2405 100644 --- a/crates/title_bar/src/title_bar.rs +++ b/crates/title_bar/src/title_bar.rs @@ -166,11 +166,11 @@ impl Render for TitleBar { .when(title_bar_settings.show_project_items, |title_bar| { title_bar .children(self.render_restricted_mode(cx)) - .children(self.render_project_host(cx)) - .child(self.render_project_name(cx)) + .children(self.render_project_host(window, cx)) + .child(self.render_project_name(window, cx)) }) .when(title_bar_settings.show_branch_name, |title_bar| { - title_bar.children(self.render_project_repo(cx)) + title_bar.children(self.render_project_repo(window, cx)) }) }) }) @@ -350,7 +350,14 @@ impl TitleBar { .next() } - fn render_remote_project_connection(&self, cx: &mut Context) -> Option { + fn render_remote_project_connection( + &self, + window: &mut Window, + cx: &mut Context, + ) -> Option { + let workspace = self.workspace.clone(); + let is_picker_open = self.is_picker_open(window, cx); + let options = self.project.read(cx).remote_connection_options(cx)?; let host: SharedString = options.display_name().into(); @@ -395,7 +402,7 @@ impl TitleBar { let meta = SharedString::from(meta); Some( - ButtonLike::new("ssh-server-icon") + ButtonLike::new("remote_project") .child( h_flex() .gap_2() @@ -410,26 +417,35 @@ impl TitleBar { ) .child(Label::new(nickname).size(LabelSize::Small).truncate()), ) - .tooltip(move |_window, cx| { - Tooltip::with_meta( - tooltip_title, - Some(&OpenRemote { - from_existing_connection: false, - create_new_window: false, - }), - meta.clone(), - cx, - ) + .when(!is_picker_open, |this| { + this.tooltip(move |_window, cx| { + Tooltip::with_meta( + tooltip_title, + Some(&OpenRemote { + from_existing_connection: false, + create_new_window: false, + }), + meta.clone(), + cx, + ) + }) }) - .on_click(|_, window, cx| { - window.dispatch_action( - OpenRemote { - from_existing_connection: false, - create_new_window: false, - } - .boxed_clone(), - cx, - ); + .on_click(move |event, window, cx| { + let position = event.position(); + let _ = workspace.update(cx, |this, cx| { + this.set_next_modal_placement(workspace::ModalPlacement::Anchored { + position, + }); + + window.dispatch_action( + OpenRemote { + from_existing_connection: false, + create_new_window: false, + } + .boxed_clone(), + cx, + ); + }); }) .into_any_element(), ) @@ -447,39 +463,47 @@ impl TitleBar { return None; } - Some( - Button::new("restricted_mode_trigger", "Restricted Mode") - .style(ButtonStyle::Tinted(TintColor::Warning)) - .label_size(LabelSize::Small) - .color(Color::Warning) - .icon(IconName::Warning) - .icon_color(Color::Warning) - .icon_size(IconSize::Small) - .icon_position(IconPosition::Start) - .tooltip(|_, cx| { - Tooltip::with_meta( - "You're in Restricted Mode", - Some(&ToggleWorktreeSecurity), - "Mark this project as trusted and unlock all features", - cx, - ) - }) - .on_click({ - cx.listener(move |this, _, window, cx| { - this.workspace - .update(cx, |workspace, cx| { - workspace.show_worktree_trust_security_modal(true, window, cx) - }) - .log_err(); - }) + let button = Button::new("restricted_mode_trigger", "Restricted Mode") + .style(ButtonStyle::Tinted(TintColor::Warning)) + .label_size(LabelSize::Small) + .color(Color::Warning) + .icon(IconName::Warning) + .icon_color(Color::Warning) + .icon_size(IconSize::Small) + .icon_position(IconPosition::Start) + .tooltip(|_, cx| { + Tooltip::with_meta( + "You're in Restricted Mode", + Some(&ToggleWorktreeSecurity), + "Mark this project as trusted and unlock all features", + cx, + ) + }) + .on_click({ + cx.listener(move |this, _, window, cx| { + this.workspace + .update(cx, |workspace, cx| { + workspace.show_worktree_trust_security_modal(true, window, cx) + }) + .log_err(); }) - .into_any_element(), - ) + }); + + if cfg!(macos_sdk_26) { + // Make up for Tahoe's traffic light buttons having less spacing around them + Some(div().child(button).ml_0p5().into_any_element()) + } else { + Some(button.into_any_element()) + } } - pub fn render_project_host(&self, cx: &mut Context) -> Option { + pub fn render_project_host( + &self, + window: &mut Window, + cx: &mut Context, + ) -> Option { if self.project.read(cx).is_via_remote_server() { - return self.render_remote_project_connection(cx); + return self.render_remote_project_connection(window, cx); } if self.project.read(cx).is_disconnected(cx) { @@ -487,7 +511,6 @@ impl TitleBar { Button::new("disconnected", "Disconnected") .disabled(true) .color(Color::Disabled) - .style(ButtonStyle::Subtle) .label_size(LabelSize::Small) .into_any_element(), ); @@ -500,15 +523,19 @@ impl TitleBar { .read(cx) .participant_indices() .get(&host_user.id)?; + Some( Button::new("project_owner_trigger", host_user.github_login.clone()) .color(Color::Player(participant_index.0)) - .style(ButtonStyle::Subtle) .label_size(LabelSize::Small) - .tooltip(Tooltip::text(format!( - "{} is sharing this project. Click to follow.", - host_user.github_login - ))) + .tooltip(move |_, cx| { + let tooltip_title = format!( + "{} is sharing this project. Click to follow.", + host_user.github_login + ); + + Tooltip::with_meta(tooltip_title, None, "Click to Follow", cx) + }) .on_click({ let host_peer_id = host.peer_id; cx.listener(move |this, _, window, cx| { @@ -523,7 +550,14 @@ impl TitleBar { ) } - pub fn render_project_name(&self, cx: &mut Context) -> impl IntoElement { + pub fn render_project_name( + &self, + window: &mut Window, + cx: &mut Context, + ) -> impl IntoElement { + let workspace = self.workspace.clone(); + let is_picker_open = self.is_picker_open(window, cx); + let name = self.project_name(cx); let is_project_selected = name.is_some(); let name = if let Some(name) = name { @@ -533,19 +567,25 @@ impl TitleBar { }; Button::new("project_name_trigger", name) - .when(!is_project_selected, |b| b.color(Color::Muted)) - .style(ButtonStyle::Subtle) .label_size(LabelSize::Small) - .tooltip(move |_window, cx| { - Tooltip::for_action( - "Recent Projects", - &zed_actions::OpenRecent { - create_new_window: false, - }, - cx, - ) + .when(!is_project_selected, |s| s.color(Color::Muted)) + .when(!is_picker_open, |this| { + this.tooltip(move |_window, cx| { + Tooltip::for_action( + "Recent Projects", + &zed_actions::OpenRecent { + create_new_window: false, + }, + cx, + ) + }) }) - .on_click(cx.listener(move |_, _, window, cx| { + .on_click(move |event, window, cx| { + let position = event.position(); + let _ = workspace.update(cx, |this, _cx| { + this.set_next_modal_placement(workspace::ModalPlacement::Anchored { position }) + }); + window.dispatch_action( OpenRecent { create_new_window: false, @@ -553,84 +593,102 @@ impl TitleBar { .boxed_clone(), cx, ); - })) + }) } - pub fn render_project_repo(&self, cx: &mut Context) -> Option { - let settings = TitleBarSettings::get_global(cx); + pub fn render_project_repo( + &self, + window: &mut Window, + cx: &mut Context, + ) -> Option { let repository = self.project.read(cx).active_repository(cx)?; let repository_count = self.project.read(cx).repositories(cx).len(); let workspace = self.workspace.upgrade()?; - let repo = repository.read(cx); - let branch_name = repo - .branch - .as_ref() - .map(|branch| branch.name()) - .map(|name| util::truncate_and_trailoff(name, MAX_BRANCH_NAME_LENGTH)) - .or_else(|| { - repo.head_commit.as_ref().map(|commit| { - commit - .sha - .chars() - .take(MAX_SHORT_SHA_LENGTH) - .collect::() - }) - })?; - let project_name = self.project_name(cx); - let repo_name = repo - .work_directory_abs_path - .file_name() - .and_then(|name| name.to_str()) - .map(SharedString::new); - let show_repo_name = - repository_count > 1 && repo.branch.is_some() && repo_name != project_name; - let branch_name = if let Some(repo_name) = repo_name.filter(|_| show_repo_name) { - format!("{repo_name}/{branch_name}") - } else { - branch_name + + let (branch_name, icon_info) = { + let repo = repository.read(cx); + let branch_name = repo + .branch + .as_ref() + .map(|branch| branch.name()) + .map(|name| util::truncate_and_trailoff(name, MAX_BRANCH_NAME_LENGTH)) + .or_else(|| { + repo.head_commit.as_ref().map(|commit| { + commit + .sha + .chars() + .take(MAX_SHORT_SHA_LENGTH) + .collect::() + }) + }); + + let branch_name = branch_name?; + + let project_name = self.project_name(cx); + let repo_name = repo + .work_directory_abs_path + .file_name() + .and_then(|name| name.to_str()) + .map(SharedString::new); + let show_repo_name = + repository_count > 1 && repo.branch.is_some() && repo_name != project_name; + let branch_name = if let Some(repo_name) = repo_name.filter(|_| show_repo_name) { + format!("{repo_name}/{branch_name}") + } else { + branch_name + }; + + let status = repo.status_summary(); + let tracked = status.index + status.worktree; + let icon_info = if status.conflict > 0 { + (IconName::Warning, Color::VersionControlConflict) + } else if tracked.modified > 0 { + (IconName::SquareDot, Color::VersionControlModified) + } else if tracked.added > 0 || status.untracked > 0 { + (IconName::SquarePlus, Color::VersionControlAdded) + } else if tracked.deleted > 0 { + (IconName::SquareMinus, Color::VersionControlDeleted) + } else { + (IconName::GitBranch, Color::Muted) + }; + + (branch_name, icon_info) }; + let is_picker_open = self.is_picker_open(window, cx); + let settings = TitleBarSettings::get_global(cx); + Some( Button::new("project_branch_trigger", branch_name) - .color(Color::Muted) - .style(ButtonStyle::Subtle) .label_size(LabelSize::Small) - .tooltip(move |_window, cx| { - Tooltip::with_meta( - "Recent Branches", - Some(&zed_actions::git::Branch), - "Local branches only", - cx, - ) - }) - .on_click(move |_, window, cx| { - let _ = workspace.update(cx, |this, cx| { - window.focus(&this.active_pane().focus_handle(cx)); - window.dispatch_action(zed_actions::git::Branch.boxed_clone(), cx); - }); + .color(Color::Muted) + .when(!is_picker_open, |this| { + this.tooltip(move |_window, cx| { + Tooltip::with_meta( + "Recent Branches", + Some(&zed_actions::git::Branch), + "Local branches only", + cx, + ) + }) }) .when(settings.show_branch_icon, |branch_button| { - let (icon, icon_color) = { - let status = repo.status_summary(); - let tracked = status.index + status.worktree; - if status.conflict > 0 { - (IconName::Warning, Color::VersionControlConflict) - } else if tracked.modified > 0 { - (IconName::SquareDot, Color::VersionControlModified) - } else if tracked.added > 0 || status.untracked > 0 { - (IconName::SquarePlus, Color::VersionControlAdded) - } else if tracked.deleted > 0 { - (IconName::SquareMinus, Color::VersionControlDeleted) - } else { - (IconName::GitBranch, Color::Muted) - } - }; - + let (icon, icon_color) = icon_info; branch_button .icon(icon) .icon_position(IconPosition::Start) .icon_color(icon_color) .icon_size(IconSize::Indicator) + }) + .on_click(move |event, window, cx| { + let position = event.position(); + let _ = workspace.update(cx, |this, cx| { + this.set_next_modal_placement(workspace::ModalPlacement::Anchored { + position, + }); + window.focus(&this.active_pane().focus_handle(cx), cx); + window.dispatch_action(zed_actions::git::Branch.boxed_clone(), cx); + }); }), ) } @@ -722,7 +780,7 @@ impl TitleBar { pub fn render_sign_in_button(&mut self, _: &mut Context) -> Button { let client = self.client.clone(); - Button::new("sign_in", "Sign in") + Button::new("sign_in", "Sign In") .label_size(LabelSize::Small) .on_click(move |_, window, cx| { let client = client.clone(); @@ -844,4 +902,10 @@ impl TitleBar { }) .anchor(gpui::Corner::TopRight) } + + fn is_picker_open(&self, window: &mut Window, cx: &mut Context) -> bool { + self.workspace + .update(cx, |workspace, cx| workspace.has_active_modal(window, cx)) + .unwrap_or(false) + } } diff --git a/crates/toolchain_selector/src/active_toolchain.rs b/crates/toolchain_selector/src/active_toolchain.rs index 03c152e3fd3df0c62ab2f5c7e4a4746875ac955a..06f7d1cdf3e27f43bdb5013038b943b9e5193680 100644 --- a/crates/toolchain_selector/src/active_toolchain.rs +++ b/crates/toolchain_selector/src/active_toolchain.rs @@ -198,10 +198,17 @@ impl ActiveToolchain { .or_else(|| toolchains.toolchains.first()) .cloned(); if let Some(toolchain) = &default_choice { + let worktree_root_path = project + .read_with(cx, |this, cx| { + this.worktree_for_id(worktree_id, cx) + .map(|worktree| worktree.read(cx).abs_path()) + }) + .ok() + .flatten()?; workspace::WORKSPACE_DB .set_toolchain( workspace_id, - worktree_id, + worktree_root_path, relative_path.clone(), toolchain.clone(), ) diff --git a/crates/toolchain_selector/src/toolchain_selector.rs b/crates/toolchain_selector/src/toolchain_selector.rs index b58b2f8d699f59c15525c452543cf5bdf071ad2c..36ef2b960a8abfe684628cea465b68e6eab5e463 100644 --- a/crates/toolchain_selector/src/toolchain_selector.rs +++ b/crates/toolchain_selector/src/toolchain_selector.rs @@ -1,6 +1,7 @@ mod active_toolchain; pub use active_toolchain::ActiveToolchain; +use anyhow::Context as _; use convert_case::Casing as _; use editor::Editor; use file_finder::OpenPathDelegate; @@ -62,6 +63,7 @@ struct AddToolchainState { language_name: LanguageName, root_path: ProjectPath, weak: WeakEntity, + worktree_root_path: Arc, } struct ScopePickerState { @@ -99,12 +101,17 @@ impl AddToolchainState { root_path: ProjectPath, window: &mut Window, cx: &mut Context, - ) -> Entity { + ) -> anyhow::Result> { let weak = cx.weak_entity(); - - cx.new(|cx| { + let worktree_root_path = project + .read(cx) + .worktree_for_id(root_path.worktree_id, cx) + .map(|worktree| worktree.read(cx).abs_path()) + .context("Could not find worktree")?; + Ok(cx.new(|cx| { let (lister, rx) = Self::create_path_browser_delegate(project.clone(), cx); let picker = cx.new(|cx| Picker::uniform_list(lister, window, cx)); + Self { state: AddState::Path { _subscription: cx.subscribe(&picker, |_, _, _: &DismissEvent, cx| { @@ -118,8 +125,9 @@ impl AddToolchainState { language_name, root_path, weak, + worktree_root_path, } - }) + })) } fn create_path_browser_delegate( @@ -225,7 +233,7 @@ impl AddToolchainState { ); }); *input_state = Self::wait_for_path(rx, window, cx); - this.focus_handle(cx).focus(window); + this.focus_handle(cx).focus(window, cx); } }); return Err(anyhow::anyhow!("Failed to resolve toolchain")); @@ -237,7 +245,15 @@ impl AddToolchainState { // Suggest a default scope based on the applicability. let scope = if let Some(project_path) = resolved_toolchain_path { if !root_path.path.as_ref().is_empty() && project_path.starts_with(&root_path) { - ToolchainScope::Subproject(root_path.worktree_id, root_path.path) + let worktree_root_path = project + .read_with(cx, |this, cx| { + this.worktree_for_id(root_path.worktree_id, cx) + .map(|worktree| worktree.read(cx).abs_path()) + }) + .ok() + .flatten() + .context("Could not find a worktree with a given worktree ID")?; + ToolchainScope::Subproject(worktree_root_path, root_path.path) } else { ToolchainScope::Project } @@ -260,7 +276,7 @@ impl AddToolchainState { toolchain, scope_picker, }; - this.focus_handle(cx).focus(window); + this.focus_handle(cx).focus(window, cx); }); Result::<_, anyhow::Error>::Ok(()) @@ -333,7 +349,7 @@ impl AddToolchainState { }); _ = self.weak.update(cx, |this, cx| { this.state = State::Search((this.create_search_state)(window, cx)); - this.focus_handle(cx).focus(window); + this.focus_handle(cx).focus(window, cx); cx.notify(); }); } @@ -383,7 +399,7 @@ impl Render for AddToolchainState { &weak, |this: &mut ToolchainSelector, _: &menu::Cancel, window, cx| { this.state = State::Search((this.create_search_state)(window, cx)); - this.state.focus_handle(cx).focus(window); + this.state.focus_handle(cx).focus(window, cx); cx.notify(); }, )) @@ -400,7 +416,7 @@ impl Render for AddToolchainState { ToolchainScope::Global, ToolchainScope::Project, ToolchainScope::Subproject( - self.root_path.worktree_id, + self.worktree_root_path.clone(), self.root_path.path.clone(), ), ]; @@ -693,7 +709,7 @@ impl ToolchainSelector { cx: &mut Context, ) { if matches!(self.state, State::Search(_)) { - self.state = State::AddToolchain(AddToolchainState::new( + let Ok(state) = AddToolchainState::new( self.project.clone(), self.language_name.clone(), ProjectPath { @@ -702,8 +718,11 @@ impl ToolchainSelector { }, window, cx, - )); - self.state.focus_handle(cx).focus(window); + ) else { + return; + }; + self.state = State::AddToolchain(state); + self.state.focus_handle(cx).focus(window, cx); cx.notify(); } } @@ -899,11 +918,17 @@ impl PickerDelegate for ToolchainSelectorDelegate { { let workspace = self.workspace.clone(); let worktree_id = self.worktree_id; + let worktree_abs_path_root = self.worktree_abs_path_root.clone(); let path = self.relative_path.clone(); let relative_path = self.relative_path.clone(); cx.spawn_in(window, async move |_, cx| { workspace::WORKSPACE_DB - .set_toolchain(workspace_id, worktree_id, relative_path, toolchain.clone()) + .set_toolchain( + workspace_id, + worktree_abs_path_root, + relative_path, + toolchain.clone(), + ) .await .log_err(); workspace diff --git a/crates/ui/src/components.rs b/crates/ui/src/components.rs index c9cb943277c6c6a5e6bc1b472040c31d9caac45c..c08e46c5882cf3c9e0a8e205c8b23224d3a7a8e1 100644 --- a/crates/ui/src/components.rs +++ b/crates/ui/src/components.rs @@ -17,7 +17,6 @@ mod icon; mod image; mod indent_guides; mod indicator; -mod inline_code; mod keybinding; mod keybinding_hint; mod label; @@ -64,7 +63,6 @@ pub use icon::*; pub use image::*; pub use indent_guides::*; pub use indicator::*; -pub use inline_code::*; pub use keybinding::*; pub use keybinding_hint::*; pub use label::*; diff --git a/crates/ui/src/components/callout.rs b/crates/ui/src/components/callout.rs index 4eb849d7f640aca78b70645f5f93301281ca6627..de95e5db2bcee2e7acbadf5570de09d9cdedbf4d 100644 --- a/crates/ui/src/components/callout.rs +++ b/crates/ui/src/components/callout.rs @@ -121,7 +121,7 @@ impl RenderOnce for Callout { Severity::Info => ( IconName::Info, Color::Muted, - cx.theme().colors().panel_background.opacity(0.), + cx.theme().status().info_background.opacity(0.1), ), Severity::Success => ( IconName::Check, diff --git a/crates/ui/src/components/context_menu.rs b/crates/ui/src/components/context_menu.rs index a4bae647408f860ec8425266a26efc173099f225..7e5e9032c9d4b0521f972b47d90d24cd502faf7b 100644 --- a/crates/ui/src/components/context_menu.rs +++ b/crates/ui/src/components/context_menu.rs @@ -562,7 +562,7 @@ impl ContextMenu { action: Some(action.boxed_clone()), handler: Rc::new(move |context, window, cx| { if let Some(context) = &context { - window.focus(context); + window.focus(context, cx); } window.dispatch_action(action.boxed_clone(), cx); }), @@ -594,7 +594,7 @@ impl ContextMenu { action: Some(action.boxed_clone()), handler: Rc::new(move |context, window, cx| { if let Some(context) = &context { - window.focus(context); + window.focus(context, cx); } window.dispatch_action(action.boxed_clone(), cx); }), @@ -893,39 +893,57 @@ impl ContextMenu { entry_render, handler, selectable, + documentation_aside, .. } => { let handler = handler.clone(); let menu = cx.entity().downgrade(); let selectable = *selectable; - ListItem::new(ix) - .inset(true) - .toggle_state(if selectable { - Some(ix) == self.selected_index - } else { - false + + div() + .id(("context-menu-child", ix)) + .when_some(documentation_aside.clone(), |this, documentation_aside| { + this.occlude() + .on_hover(cx.listener(move |menu, hovered, _, cx| { + if *hovered { + menu.documentation_aside = Some((ix, documentation_aside.clone())); + } else if matches!(menu.documentation_aside, Some((id, _)) if id == ix) + { + menu.documentation_aside = None; + } + cx.notify(); + })) }) - .selectable(selectable) - .when(selectable, |item| { - item.on_click({ - let context = self.action_context.clone(); - let keep_open_on_confirm = self.keep_open_on_confirm; - move |_, window, cx| { - handler(context.as_ref(), window, cx); - menu.update(cx, |menu, cx| { - menu.clicked = true; - - if keep_open_on_confirm { - menu.rebuild(window, cx); - } else { - cx.emit(DismissEvent); + .child( + ListItem::new(ix) + .inset(true) + .toggle_state(if selectable { + Some(ix) == self.selected_index + } else { + false + }) + .selectable(selectable) + .when(selectable, |item| { + item.on_click({ + let context = self.action_context.clone(); + let keep_open_on_confirm = self.keep_open_on_confirm; + move |_, window, cx| { + handler(context.as_ref(), window, cx); + menu.update(cx, |menu, cx| { + menu.clicked = true; + + if keep_open_on_confirm { + menu.rebuild(window, cx); + } else { + cx.emit(DismissEvent); + } + }) + .ok(); } }) - .ok(); - } - }) - }) - .child(entry_render(window, cx)) + }) + .child(entry_render(window, cx)), + ) .into_any_element() } } diff --git a/crates/ui/src/components/icon.rs b/crates/ui/src/components/icon.rs index 1c8e36ec18d6184b38eb6772e8f5a13be181ae00..9d2c7ae3b515744125879f4a2c0e0d3e9a4fb841 100644 --- a/crates/ui/src/components/icon.rs +++ b/crates/ui/src/components/icon.rs @@ -126,17 +126,6 @@ enum IconSource { ExternalSvg(SharedString), } -impl IconSource { - fn from_path(path: impl Into) -> Self { - let path = path.into(); - if path.starts_with("icons/") { - Self::Embedded(path) - } else { - Self::External(Arc::from(PathBuf::from(path.as_ref()))) - } - } -} - #[derive(IntoElement, RegisterComponent)] pub struct Icon { source: IconSource, @@ -155,9 +144,18 @@ impl Icon { } } + /// Create an icon from a path. Uses a heuristic to determine if it's embedded or external: + /// - Paths starting with "icons/" are treated as embedded SVGs + /// - Other paths are treated as external raster images (from icon themes) pub fn from_path(path: impl Into) -> Self { + let path = path.into(); + let source = if path.starts_with("icons/") { + IconSource::Embedded(path) + } else { + IconSource::External(Arc::from(PathBuf::from(path.as_ref()))) + }; Self { - source: IconSource::from_path(path), + source, color: Color::default(), size: IconSize::default().rems(), transformation: Transformation::default(), diff --git a/crates/ui/src/components/inline_code.rs b/crates/ui/src/components/inline_code.rs deleted file mode 100644 index 43507127fef478e5a38cfad2d84446673af15f2e..0000000000000000000000000000000000000000 --- a/crates/ui/src/components/inline_code.rs +++ /dev/null @@ -1,64 +0,0 @@ -use crate::prelude::*; -use gpui::{AnyElement, IntoElement, ParentElement, Styled}; - -/// InlineCode mimics the way inline code is rendered when wrapped in backticks in Markdown. -/// -/// # Usage Example -/// -/// ``` -/// use ui::InlineCode; -/// -/// let InlineCode = InlineCode::new("
hey
"); -/// ``` -#[derive(IntoElement, RegisterComponent)] -pub struct InlineCode { - label: SharedString, - label_size: LabelSize, -} - -impl InlineCode { - pub fn new(label: impl Into) -> Self { - Self { - label: label.into(), - label_size: LabelSize::Default, - } - } - - /// Sets the size of the label. - pub fn label_size(mut self, size: LabelSize) -> Self { - self.label_size = size; - self - } -} - -impl RenderOnce for InlineCode { - fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { - h_flex() - .min_w_0() - .px_0p5() - .overflow_hidden() - .bg(cx.theme().colors().text.opacity(0.05)) - .child(Label::new(self.label).size(self.label_size).buffer_font(cx)) - } -} - -impl Component for InlineCode { - fn scope() -> ComponentScope { - ComponentScope::DataDisplay - } - - fn preview(_window: &mut Window, _cx: &mut App) -> Option { - Some( - v_flex() - .gap_6() - .child( - example_group(vec![single_example( - "Simple", - InlineCode::new("zed.dev").into_any_element(), - )]) - .vertical(), - ) - .into_any_element(), - ) - } -} diff --git a/crates/ui/src/components/label/label.rs b/crates/ui/src/components/label/label.rs index 49e2de94a1f86196c10e41879797b02070517e65..d0f50c00336eb971621e2da7bbaf53cf09569caa 100644 --- a/crates/ui/src/components/label/label.rs +++ b/crates/ui/src/components/label/label.rs @@ -56,6 +56,12 @@ impl Label { pub fn set_text(&mut self, text: impl Into) { self.label = text.into(); } + + /// Truncates the label from the start, keeping the end visible. + pub fn truncate_start(mut self) -> Self { + self.base = self.base.truncate_start(); + self + } } // Style methods. @@ -256,7 +262,8 @@ impl Component for Label { "Special Cases", vec![ single_example("Single Line", Label::new("Line 1\nLine 2\nLine 3").single_line().into_any_element()), - single_example("Text Ellipsis", div().max_w_24().child(Label::new("This is a very long file name that should be truncated: very_long_file_name_with_many_words.rs").truncate()).into_any_element()), + single_example("Regular Truncation", div().max_w_24().child(Label::new("This is a very long file name that should be truncated: very_long_file_name_with_many_words.rs").truncate()).into_any_element()), + single_example("Start Truncation", div().max_w_24().child(Label::new("zed/crates/ui/src/components/label/truncate/label/label.rs").truncate_start()).into_any_element()), ], ), ]) diff --git a/crates/ui/src/components/label/label_like.rs b/crates/ui/src/components/label/label_like.rs index 31fb7bfd88f1343ac6145c86f228bdcbd6a22e10..03fde4083d5e9a8e07f38c830edd5116f14e6d70 100644 --- a/crates/ui/src/components/label/label_like.rs +++ b/crates/ui/src/components/label/label_like.rs @@ -56,7 +56,7 @@ pub trait LabelCommon { /// Sets the alpha property of the label, overwriting the alpha value of the color. fn alpha(self, alpha: f32) -> Self; - /// Truncates overflowing text with an ellipsis (`…`) if needed. + /// Truncates overflowing text with an ellipsis (`…`) at the end if needed. fn truncate(self) -> Self; /// Sets the label to render as a single line. @@ -88,6 +88,7 @@ pub struct LabelLike { underline: bool, single_line: bool, truncate: bool, + truncate_start: bool, } impl Default for LabelLike { @@ -113,6 +114,7 @@ impl LabelLike { underline: false, single_line: false, truncate: false, + truncate_start: false, } } } @@ -126,6 +128,12 @@ impl LabelLike { gpui::margin_style_methods!({ visibility: pub }); + + /// Truncates overflowing text with an ellipsis (`…`) at the start if needed. + pub fn truncate_start(mut self) -> Self { + self.truncate_start = true; + self + } } impl LabelCommon for LabelLike { @@ -169,7 +177,7 @@ impl LabelCommon for LabelLike { self } - /// Truncates overflowing text with an ellipsis (`…`) if needed. + /// Truncates overflowing text with an ellipsis (`…`) at the end if needed. fn truncate(mut self) -> Self { self.truncate = true; self @@ -233,7 +241,16 @@ impl RenderOnce for LabelLike { .when(self.strikethrough, |this| this.line_through()) .when(self.single_line, |this| this.whitespace_nowrap()) .when(self.truncate, |this| { - this.overflow_x_hidden().text_ellipsis() + this.min_w_0() + .overflow_x_hidden() + .whitespace_nowrap() + .text_ellipsis() + }) + .when(self.truncate_start, |this| { + this.min_w_0() + .overflow_x_hidden() + .whitespace_nowrap() + .text_ellipsis_start() }) .text_color(color) .font_weight( diff --git a/crates/ui/src/components/navigable.rs b/crates/ui/src/components/navigable.rs index a592bcc36f4cc490c4676a83660ace050025ee39..07e761f9c0c14daf551d272c1a1894da84e1b3cf 100644 --- a/crates/ui/src/components/navigable.rs +++ b/crates/ui/src/components/navigable.rs @@ -75,7 +75,7 @@ impl RenderOnce for Navigable { }) .unwrap_or(0); if let Some(entry) = children.get(target) { - entry.focus_handle.focus(window); + entry.focus_handle.focus(window, cx); if let Some(anchor) = &entry.scroll_anchor { anchor.scroll_to(window, cx); } @@ -89,7 +89,7 @@ impl RenderOnce for Navigable { .and_then(|index| index.checked_sub(1)) .or(children.len().checked_sub(1)); if let Some(entry) = target.and_then(|target| children.get(target)) { - entry.focus_handle.focus(window); + entry.focus_handle.focus(window, cx); if let Some(anchor) = &entry.scroll_anchor { anchor.scroll_to(window, cx); } diff --git a/crates/ui/src/components/popover_menu.rs b/crates/ui/src/components/popover_menu.rs index b1a52bec8fdf1f7030b5b321bed7702d602ff212..cd79e50ce01b1f4e697b252801c2ae76765726d2 100644 --- a/crates/ui/src/components/popover_menu.rs +++ b/crates/ui/src/components/popover_menu.rs @@ -281,13 +281,25 @@ fn show_menu( if modal.focus_handle(cx).contains_focused(window, cx) && let Some(previous_focus_handle) = previous_focus_handle.as_ref() { - window.focus(previous_focus_handle); + window.focus(previous_focus_handle, cx); } *menu2.borrow_mut() = None; window.refresh(); }) .detach(); - window.focus(&new_menu.focus_handle(cx)); + + // Since menus are rendered in a deferred fashion, their focus handles are + // not linked in the dispatch tree until after the deferred draw callback + // runs. We need to wait for that to happen before focusing it, so that + // calling `contains_focused` on the parent's focus handle returns `true` + // when the menu is focused. This prevents the pane's tab bar buttons from + // flickering when opening popover menus. + let focus_handle = new_menu.focus_handle(cx); + window.on_next_frame(move |window, _cx| { + window.on_next_frame(move |window, cx| { + window.focus(&focus_handle, cx); + }); + }); *menu.borrow_mut() = Some(new_menu); window.refresh(); diff --git a/crates/ui/src/components/right_click_menu.rs b/crates/ui/src/components/right_click_menu.rs index dff423073710121bb0bc0fafdb8ab3108b746bde..faf2cb3429b610727209e13188656c174aefb655 100644 --- a/crates/ui/src/components/right_click_menu.rs +++ b/crates/ui/src/components/right_click_menu.rs @@ -253,13 +253,25 @@ impl Element for RightClickMenu { && let Some(previous_focus_handle) = previous_focus_handle.as_ref() { - window.focus(previous_focus_handle); + window.focus(previous_focus_handle, cx); } *menu2.borrow_mut() = None; window.refresh(); }) .detach(); - window.focus(&new_menu.focus_handle(cx)); + + // Since menus are rendered in a deferred fashion, their focus handles are + // not linked in the dispatch tree until after the deferred draw callback + // runs. We need to wait for that to happen before focusing it, so that + // calling `contains_focused` on the parent's focus handle returns `true` + // when the menu is focused. This prevents the pane's tab bar buttons from + // flickering when opening menus. + let focus_handle = new_menu.focus_handle(cx); + window.on_next_frame(move |window, _cx| { + window.on_next_frame(move |window, cx| { + window.focus(&focus_handle, cx); + }); + }); *menu.borrow_mut() = Some(new_menu); *position.borrow_mut() = if let Some(child_bounds) = child_bounds { if let Some(attach) = attach { diff --git a/crates/ui_input/src/number_field.rs b/crates/ui_input/src/number_field.rs index ee5c57b43b7c44db1c2ded122d3d4272a541c32e..2d596a2498f445f6a0d18ce48b02bddf20aee8da 100644 --- a/crates/ui_input/src/number_field.rs +++ b/crates/ui_input/src/number_field.rs @@ -476,7 +476,7 @@ impl RenderOnce for NumberField { if let Some(previous) = previous_focus_handle.as_ref() { - window.focus(previous); + window.focus(previous, cx); } on_change(&new_value, window, cx); }; @@ -485,7 +485,7 @@ impl RenderOnce for NumberField { }) .detach(); - window.focus(&editor.focus_handle(cx)); + window.focus(&editor.focus_handle(cx), cx); editor } diff --git a/crates/util/src/redact.rs b/crates/util/src/redact.rs index 6b297dfb58bb0b4537d4032d8f9cf4db845f9d78..ad11f7618b1cf57c27e7367845cf66e9d0e6bd0b 100644 --- a/crates/util/src/redact.rs +++ b/crates/util/src/redact.rs @@ -1,3 +1,9 @@ +use std::sync::LazyLock; + +static REDACT_REGEX: LazyLock = LazyLock::new(|| { + regex::Regex::new(r#"([A-Z_][A-Z0-9_]*)=("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|\S+)"#).unwrap() +}); + /// Whether a given environment variable name should have its value redacted pub fn should_redact(env_var_name: &str) -> bool { const REDACTED_SUFFIXES: &[&str] = &[ @@ -13,3 +19,31 @@ pub fn should_redact(env_var_name: &str) -> bool { .iter() .any(|suffix| env_var_name.ends_with(suffix)) } + +/// Redact a string which could include a command with environment variables +pub fn redact_command(command: &str) -> String { + REDACT_REGEX + .replace_all(command, |caps: ®ex::Captures| { + let var_name = &caps[1]; + let value = &caps[2]; + if should_redact(var_name) { + format!(r#"{}="[REDACTED]""#, var_name) + } else { + format!("{}={}", var_name, value) + } + }) + .to_string() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_redact_string_with_multiple_env_vars() { + let input = r#"failed to spawn command cd "/code/something" && ANTHROPIC_API_KEY="sk-ant-api03-WOOOO" COMMAND_MODE="unix2003" GEMINI_API_KEY="AIGEMINIFACE" HOME="/Users/foo""#; + let result = redact_command(input); + let expected = r#"failed to spawn command cd "/code/something" && ANTHROPIC_API_KEY="[REDACTED]" COMMAND_MODE="unix2003" GEMINI_API_KEY="[REDACTED]" HOME="/Users/foo""#; + assert_eq!(result, expected); + } +} diff --git a/crates/vim/src/command.rs b/crates/vim/src/command.rs index 5bf0fca041cf274f38c84031e35903c9e339cc24..2228c23f02beb954bdb26b2b36f078249e423d7d 100644 --- a/crates/vim/src/command.rs +++ b/crates/vim/src/command.rs @@ -230,6 +230,14 @@ struct VimEdit { pub filename: String, } +/// Pastes the specified file's contents. +#[derive(Clone, PartialEq, Action)] +#[action(namespace = vim, no_json, no_register)] +struct VimRead { + pub range: Option, + pub filename: String, +} + #[derive(Clone, PartialEq, Action)] #[action(namespace = vim, no_json, no_register)] struct VimNorm { @@ -330,10 +338,12 @@ pub fn register(editor: &mut Editor, cx: &mut Context) { let Some(range) = range.buffer_range(vim, editor, window, cx).ok() else { return; }; - let Some((line_ending, text, whole_buffer)) = editor.buffer().update(cx, |multi, cx| { + let Some((line_ending, encoding, has_bom, text, whole_buffer)) = editor.buffer().update(cx, |multi, cx| { Some(multi.as_singleton()?.update(cx, |buffer, _| { ( buffer.line_ending(), + buffer.encoding(), + buffer.has_bom(), buffer.as_rope().slice_rows(range.start.0..range.end.0 + 1), range.start.0 == 0 && range.end.0 + 1 >= buffer.row_count(), ) @@ -429,7 +439,7 @@ pub fn register(editor: &mut Editor, cx: &mut Context) { return; }; worktree - .write_file(path.into_arc(), text.clone(), line_ending, cx) + .write_file(path.into_arc(), text.clone(), line_ending, encoding, has_bom, cx) .detach_and_prompt_err("Failed to write lines", window, cx, |_, _, _| None); }); }) @@ -641,6 +651,107 @@ pub fn register(editor: &mut Editor, cx: &mut Context) { }); }); + Vim::action(editor, cx, |vim, action: &VimRead, window, cx| { + vim.update_editor(cx, |vim, editor, cx| { + let snapshot = editor.buffer().read(cx).snapshot(cx); + let end = if let Some(range) = action.range.clone() { + let Some(multi_range) = range.buffer_range(vim, editor, window, cx).log_err() + else { + return; + }; + + match &range.start { + // inserting text above the first line uses the command ":0r {name}" + Position::Line { row: 0, offset: 0 } if range.end.is_none() => { + snapshot.clip_point(Point::new(0, 0), Bias::Right) + } + _ => snapshot.clip_point(Point::new(multi_range.end.0 + 1, 0), Bias::Right), + } + } else { + let end_row = editor + .selections + .newest::(&editor.display_snapshot(cx)) + .range() + .end + .row; + snapshot.clip_point(Point::new(end_row + 1, 0), Bias::Right) + }; + let is_end_of_file = end == snapshot.max_point(); + let edit_range = snapshot.anchor_before(end)..snapshot.anchor_before(end); + + let mut text = if is_end_of_file { + String::from('\n') + } else { + String::new() + }; + + let mut task = None; + if action.filename.is_empty() { + text.push_str( + &editor + .buffer() + .read(cx) + .as_singleton() + .map(|buffer| buffer.read(cx).text()) + .unwrap_or_default(), + ); + } else { + if let Some(project) = editor.project().cloned() { + project.update(cx, |project, cx| { + let Some(worktree) = project.visible_worktrees(cx).next() else { + return; + }; + let path_style = worktree.read(cx).path_style(); + let Some(path) = + RelPath::new(Path::new(&action.filename), path_style).log_err() + else { + return; + }; + task = + Some(worktree.update(cx, |worktree, cx| worktree.load_file(&path, cx))); + }); + } else { + return; + } + }; + + cx.spawn_in(window, async move |editor, cx| { + if let Some(task) = task { + text.push_str( + &task + .await + .log_err() + .map(|loaded_file| loaded_file.text) + .unwrap_or_default(), + ); + } + + if !text.is_empty() && !is_end_of_file { + text.push('\n'); + } + + let _ = editor.update_in(cx, |editor, window, cx| { + editor.transact(window, cx, |editor, window, cx| { + editor.edit([(edit_range.clone(), text)], cx); + let snapshot = editor.buffer().read(cx).snapshot(cx); + editor.change_selections(Default::default(), window, cx, |s| { + let point = if is_end_of_file { + Point::new( + edit_range.start.to_point(&snapshot).row.saturating_add(1), + 0, + ) + } else { + Point::new(edit_range.start.to_point(&snapshot).row, 0) + }; + s.select_ranges([point..point]); + }) + }); + }); + }) + .detach(); + }); + }); + Vim::action(editor, cx, |vim, action: &VimNorm, window, cx| { let keystrokes = action .command @@ -1336,6 +1447,27 @@ fn generate_commands(_: &App) -> Vec { VimCommand::new(("e", "dit"), editor::actions::ReloadFile) .bang(editor::actions::ReloadFile) .filename(|_, filename| Some(VimEdit { filename }.boxed_clone())), + VimCommand::new( + ("r", "ead"), + VimRead { + range: None, + filename: "".into(), + }, + ) + .filename(|_, filename| { + Some( + VimRead { + range: None, + filename, + } + .boxed_clone(), + ) + }) + .range(|action, range| { + let mut action: VimRead = action.as_any().downcast_ref::().unwrap().clone(); + action.range.replace(range.clone()); + Some(Box::new(action)) + }), VimCommand::new(("sp", "lit"), workspace::SplitHorizontal).filename(|_, filename| { Some( VimSplit { @@ -2573,6 +2705,76 @@ mod test { assert_eq!(fs.load(path).await.unwrap().replace("\r\n", "\n"), "@@\n"); } + #[gpui::test] + async fn test_command_read(cx: &mut TestAppContext) { + let mut cx = VimTestContext::new(cx, true).await; + + let fs = cx.workspace(|workspace, _, cx| workspace.project().read(cx).fs().clone()); + let path = Path::new(path!("/root/dir/other.rs")); + fs.as_fake().insert_file(path, "1\n2\n3".into()).await; + + cx.workspace(|workspace, _, cx| { + assert_active_item(workspace, path!("/root/dir/file.rs"), "", cx); + }); + + // File without trailing newline + cx.set_state("one\ntwo\nthreeˇ", Mode::Normal); + cx.simulate_keystrokes(": r space d i r / o t h e r . r s"); + cx.simulate_keystrokes("enter"); + cx.assert_state("one\ntwo\nthree\nˇ1\n2\n3", Mode::Normal); + + cx.set_state("oneˇ\ntwo\nthree", Mode::Normal); + cx.simulate_keystrokes(": r space d i r / o t h e r . r s"); + cx.simulate_keystrokes("enter"); + cx.assert_state("one\nˇ1\n2\n3\ntwo\nthree", Mode::Normal); + + cx.set_state("one\nˇtwo\nthree", Mode::Normal); + cx.simulate_keystrokes(": 0 r space d i r / o t h e r . r s"); + cx.simulate_keystrokes("enter"); + cx.assert_state("ˇ1\n2\n3\none\ntwo\nthree", Mode::Normal); + + cx.set_state("one\n«ˇtwo\nthree\nfour»\nfive", Mode::Visual); + cx.simulate_keystrokes(": r space d i r / o t h e r . r s"); + cx.simulate_keystrokes("enter"); + cx.run_until_parked(); + cx.assert_state("one\ntwo\nthree\nfour\nˇ1\n2\n3\nfive", Mode::Normal); + + // Empty filename + cx.set_state("oneˇ\ntwo\nthree", Mode::Normal); + cx.simulate_keystrokes(": r"); + cx.simulate_keystrokes("enter"); + cx.assert_state("one\nˇone\ntwo\nthree\ntwo\nthree", Mode::Normal); + + // File with trailing newline + fs.as_fake().insert_file(path, "1\n2\n3\n".into()).await; + cx.set_state("one\ntwo\nthreeˇ", Mode::Normal); + cx.simulate_keystrokes(": r space d i r / o t h e r . r s"); + cx.simulate_keystrokes("enter"); + cx.assert_state("one\ntwo\nthree\nˇ1\n2\n3\n", Mode::Normal); + + cx.set_state("oneˇ\ntwo\nthree", Mode::Normal); + cx.simulate_keystrokes(": r space d i r / o t h e r . r s"); + cx.simulate_keystrokes("enter"); + cx.assert_state("one\nˇ1\n2\n3\n\ntwo\nthree", Mode::Normal); + + cx.set_state("one\n«ˇtwo\nthree\nfour»\nfive", Mode::Visual); + cx.simulate_keystrokes(": r space d i r / o t h e r . r s"); + cx.simulate_keystrokes("enter"); + cx.assert_state("one\ntwo\nthree\nfour\nˇ1\n2\n3\n\nfive", Mode::Normal); + + cx.set_state("«one\ntwo\nthreeˇ»", Mode::Visual); + cx.simulate_keystrokes(": r space d i r / o t h e r . r s"); + cx.simulate_keystrokes("enter"); + cx.assert_state("one\ntwo\nthree\nˇ1\n2\n3\n", Mode::Normal); + + // Empty file + fs.as_fake().insert_file(path, "".into()).await; + cx.set_state("ˇone\ntwo\nthree", Mode::Normal); + cx.simulate_keystrokes(": r space d i r / o t h e r . r s"); + cx.simulate_keystrokes("enter"); + cx.assert_state("one\nˇtwo\nthree", Mode::Normal); + } + #[gpui::test] async fn test_command_quit(cx: &mut TestAppContext) { let mut cx = VimTestContext::new(cx, true).await; diff --git a/crates/vim/src/object.rs b/crates/vim/src/object.rs index 02150332405c6d5ea4d5dd78f477348be968fddf..e9a2f4fc63d31f78a9a7abce8aac785b56eb1fd4 100644 --- a/crates/vim/src/object.rs +++ b/crates/vim/src/object.rs @@ -3407,4 +3407,390 @@ mod test { .assert_eq(" ˇf = (x: unknown) => {"); cx.shared_clipboard().await.assert_eq("const "); } + + #[gpui::test] + async fn test_arrow_function_text_object(cx: &mut gpui::TestAppContext) { + let mut cx = VimTestContext::new_typescript(cx).await; + + cx.set_state( + indoc! {" + const foo = () => { + return ˇ1; + }; + "}, + Mode::Normal, + ); + cx.simulate_keystrokes("v a f"); + cx.assert_state( + indoc! {" + «const foo = () => { + return 1; + };ˇ» + "}, + Mode::VisualLine, + ); + + cx.set_state( + indoc! {" + arr.map(() => { + return ˇ1; + }); + "}, + Mode::Normal, + ); + cx.simulate_keystrokes("v a f"); + cx.assert_state( + indoc! {" + arr.map(«() => { + return 1; + }ˇ»); + "}, + Mode::VisualLine, + ); + + cx.set_state( + indoc! {" + const foo = () => { + return ˇ1; + }; + "}, + Mode::Normal, + ); + cx.simulate_keystrokes("v i f"); + cx.assert_state( + indoc! {" + const foo = () => { + «return 1;ˇ» + }; + "}, + Mode::Visual, + ); + + cx.set_state( + indoc! {" + (() => { + console.log(ˇ1); + })(); + "}, + Mode::Normal, + ); + cx.simulate_keystrokes("v a f"); + cx.assert_state( + indoc! {" + («() => { + console.log(1); + }ˇ»)(); + "}, + Mode::VisualLine, + ); + + cx.set_state( + indoc! {" + const foo = () => { + return ˇ1; + }; + export { foo }; + "}, + Mode::Normal, + ); + cx.simulate_keystrokes("v a f"); + cx.assert_state( + indoc! {" + «const foo = () => { + return 1; + };ˇ» + export { foo }; + "}, + Mode::VisualLine, + ); + + cx.set_state( + indoc! {" + let bar = () => { + return ˇ2; + }; + "}, + Mode::Normal, + ); + cx.simulate_keystrokes("v a f"); + cx.assert_state( + indoc! {" + «let bar = () => { + return 2; + };ˇ» + "}, + Mode::VisualLine, + ); + + cx.set_state( + indoc! {" + var baz = () => { + return ˇ3; + }; + "}, + Mode::Normal, + ); + cx.simulate_keystrokes("v a f"); + cx.assert_state( + indoc! {" + «var baz = () => { + return 3; + };ˇ» + "}, + Mode::VisualLine, + ); + + cx.set_state( + indoc! {" + const add = (a, b) => a + ˇb; + "}, + Mode::Normal, + ); + cx.simulate_keystrokes("v a f"); + cx.assert_state( + indoc! {" + «const add = (a, b) => a + b;ˇ» + "}, + Mode::VisualLine, + ); + + cx.set_state( + indoc! {" + const add = ˇ(a, b) => a + b; + "}, + Mode::Normal, + ); + cx.simulate_keystrokes("v a f"); + cx.assert_state( + indoc! {" + «const add = (a, b) => a + b;ˇ» + "}, + Mode::VisualLine, + ); + + cx.set_state( + indoc! {" + const add = (a, b) => a + bˇ; + "}, + Mode::Normal, + ); + cx.simulate_keystrokes("v a f"); + cx.assert_state( + indoc! {" + «const add = (a, b) => a + b;ˇ» + "}, + Mode::VisualLine, + ); + + cx.set_state( + indoc! {" + const add = (a, b) =ˇ> a + b; + "}, + Mode::Normal, + ); + cx.simulate_keystrokes("v a f"); + cx.assert_state( + indoc! {" + «const add = (a, b) => a + b;ˇ» + "}, + Mode::VisualLine, + ); + } + + #[gpui::test] + async fn test_arrow_function_in_jsx(cx: &mut gpui::TestAppContext) { + let mut cx = VimTestContext::new_tsx(cx).await; + + cx.set_state( + indoc! {r#" + export const MyComponent = () => { + return ( +
+
{ + alert("Hello world!"); + console.log(ˇ"clicked"); + }}>Hello world!
+
+ ); + }; + "#}, + Mode::Normal, + ); + cx.simulate_keystrokes("v a f"); + cx.assert_state( + indoc! {r#" + export const MyComponent = () => { + return ( +
+
{ + alert("Hello world!"); + console.log("clicked"); + }ˇ»}>Hello world!
+
+ ); + }; + "#}, + Mode::VisualLine, + ); + + cx.set_state( + indoc! {r#" + export const MyComponent = () => { + return ( +
+
console.log("clickˇed")}>Hello world!
+
+ ); + }; + "#}, + Mode::Normal, + ); + cx.simulate_keystrokes("v a f"); + cx.assert_state( + indoc! {r#" + export const MyComponent = () => { + return ( +
+
console.log("clicked")ˇ»}>Hello world!
+
+ ); + }; + "#}, + Mode::VisualLine, + ); + + cx.set_state( + indoc! {r#" + export const MyComponent = () => { + return ( +
+
console.log("clicked")}>Hello world!
+
+ ); + }; + "#}, + Mode::Normal, + ); + cx.simulate_keystrokes("v a f"); + cx.assert_state( + indoc! {r#" + export const MyComponent = () => { + return ( +
+
console.log("clicked")ˇ»}>Hello world!
+
+ ); + }; + "#}, + Mode::VisualLine, + ); + + cx.set_state( + indoc! {r#" + export const MyComponent = () => { + return ( +
+
console.log("clicked"ˇ)}>Hello world!
+
+ ); + }; + "#}, + Mode::Normal, + ); + cx.simulate_keystrokes("v a f"); + cx.assert_state( + indoc! {r#" + export const MyComponent = () => { + return ( +
+
console.log("clicked")ˇ»}>Hello world!
+
+ ); + }; + "#}, + Mode::VisualLine, + ); + + cx.set_state( + indoc! {r#" + export const MyComponent = () => { + return ( +
+
console.log("clicked")}>Hello world!
+
+ ); + }; + "#}, + Mode::Normal, + ); + cx.simulate_keystrokes("v a f"); + cx.assert_state( + indoc! {r#" + export const MyComponent = () => { + return ( +
+
console.log("clicked")ˇ»}>Hello world!
+
+ ); + }; + "#}, + Mode::VisualLine, + ); + + cx.set_state( + indoc! {r#" + export const MyComponent = () => { + return ( +
+
{ + console.log("cliˇcked"); + }}>Hello world!
+
+ ); + }; + "#}, + Mode::Normal, + ); + cx.simulate_keystrokes("v a f"); + cx.assert_state( + indoc! {r#" + export const MyComponent = () => { + return ( +
+
{ + console.log("clicked"); + }ˇ»}>Hello world!
+
+ ); + }; + "#}, + Mode::VisualLine, + ); + + cx.set_state( + indoc! {r#" + export const MyComponent = () => { + return ( +
+
fˇoo()}>Hello world!
+
+ ); + }; + "#}, + Mode::Normal, + ); + cx.simulate_keystrokes("v a f"); + cx.assert_state( + indoc! {r#" + export const MyComponent = () => { + return ( +
+
foo()ˇ»}>Hello world!
+
+ ); + }; + "#}, + Mode::VisualLine, + ); + } } diff --git a/crates/vim/src/visual.rs b/crates/vim/src/visual.rs index 3c6f237435e3924a907e059ed1a878641c287e7e..5667190bb7239ee3e534a5556d96452a7c68b1ef 100644 --- a/crates/vim/src/visual.rs +++ b/crates/vim/src/visual.rs @@ -522,12 +522,16 @@ impl Vim { selection.start = original_point.to_display_point(map) } } else { - selection.end = movement::saturating_right( - map, - original_point.to_display_point(map), - ); - if original_point.column > 0 { - selection.reversed = true + let original_display_point = + original_point.to_display_point(map); + if selection.end <= original_display_point { + selection.end = movement::saturating_right( + map, + original_display_point, + ); + if original_point.column > 0 { + selection.reversed = true + } } } } diff --git a/crates/which_key/Cargo.toml b/crates/which_key/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..f53ba45dd71abc972ce23efb8871f485dfe47207 --- /dev/null +++ b/crates/which_key/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "which_key" +version = "0.1.0" +edition.workspace = true +publish.workspace = true +license = "GPL-3.0-or-later" + +[lints] +workspace = true + +[lib] +path = "src/which_key.rs" +doctest = false + +[dependencies] +command_palette.workspace = true +gpui.workspace = true +serde.workspace = true +settings.workspace = true +theme.workspace = true +ui.workspace = true +util.workspace = true +workspace.workspace = true diff --git a/crates/which_key/LICENSE-GPL b/crates/which_key/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/which_key/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/which_key/src/which_key.rs b/crates/which_key/src/which_key.rs new file mode 100644 index 0000000000000000000000000000000000000000..70889c100f33020a3ceaa8af1ba8812d5e7d4adb --- /dev/null +++ b/crates/which_key/src/which_key.rs @@ -0,0 +1,98 @@ +//! Which-key support for Zed. + +mod which_key_modal; +mod which_key_settings; + +use gpui::{App, Keystroke}; +use settings::Settings; +use std::{sync::LazyLock, time::Duration}; +use util::ResultExt; +use which_key_modal::WhichKeyModal; +use which_key_settings::WhichKeySettings; +use workspace::Workspace; + +pub fn init(cx: &mut App) { + WhichKeySettings::register(cx); + + cx.observe_new(|_: &mut Workspace, window, cx| { + let Some(window) = window else { + return; + }; + let mut timer = None; + cx.observe_pending_input(window, move |workspace, window, cx| { + if window.pending_input_keystrokes().is_none() { + if let Some(modal) = workspace.active_modal::(cx) { + modal.update(cx, |modal, cx| modal.dismiss(cx)); + }; + timer.take(); + return; + } + + let which_key_settings = WhichKeySettings::get_global(cx); + if !which_key_settings.enabled { + return; + } + + let delay_ms = which_key_settings.delay_ms; + + timer.replace(cx.spawn_in(window, async move |workspace_handle, cx| { + cx.background_executor() + .timer(Duration::from_millis(delay_ms)) + .await; + workspace_handle + .update_in(cx, |workspace, window, cx| { + if workspace.active_modal::(cx).is_some() { + return; + }; + + workspace.toggle_modal(window, cx, |window, cx| { + WhichKeyModal::new(workspace_handle.clone(), window, cx) + }); + }) + .log_err(); + })); + }) + .detach(); + }) + .detach(); +} + +// Hard-coded list of keystrokes to filter out from which-key display +pub static FILTERED_KEYSTROKES: LazyLock>> = LazyLock::new(|| { + [ + // Modifiers on normal vim commands + "g h", + "g j", + "g k", + "g l", + "g $", + "g ^", + // Duplicate keys with "ctrl" held, e.g. "ctrl-w ctrl-a" is duplicate of "ctrl-w a" + "ctrl-w ctrl-a", + "ctrl-w ctrl-c", + "ctrl-w ctrl-h", + "ctrl-w ctrl-j", + "ctrl-w ctrl-k", + "ctrl-w ctrl-l", + "ctrl-w ctrl-n", + "ctrl-w ctrl-o", + "ctrl-w ctrl-p", + "ctrl-w ctrl-q", + "ctrl-w ctrl-s", + "ctrl-w ctrl-v", + "ctrl-w ctrl-w", + "ctrl-w ctrl-]", + "ctrl-w ctrl-shift-w", + "ctrl-w ctrl-g t", + "ctrl-w ctrl-g shift-t", + ] + .iter() + .filter_map(|s| { + let keystrokes: Result, _> = s + .split(' ') + .map(|keystroke_str| Keystroke::parse(keystroke_str)) + .collect(); + keystrokes.ok() + }) + .collect() +}); diff --git a/crates/which_key/src/which_key_modal.rs b/crates/which_key/src/which_key_modal.rs new file mode 100644 index 0000000000000000000000000000000000000000..238431b90a8eafdd0e085a3f109e8f812fbe709b --- /dev/null +++ b/crates/which_key/src/which_key_modal.rs @@ -0,0 +1,308 @@ +//! Modal implementation for the which-key display. + +use gpui::prelude::FluentBuilder; +use gpui::{ + App, Context, DismissEvent, EventEmitter, FocusHandle, Focusable, FontWeight, Keystroke, + ScrollHandle, Subscription, WeakEntity, Window, +}; +use settings::Settings; +use std::collections::HashMap; +use theme::ThemeSettings; +use ui::{ + Divider, DividerColor, DynamicSpacing, LabelSize, WithScrollbar, prelude::*, + text_for_keystrokes, +}; +use workspace::{ModalView, Workspace}; + +use crate::FILTERED_KEYSTROKES; + +pub struct WhichKeyModal { + _workspace: WeakEntity, + focus_handle: FocusHandle, + scroll_handle: ScrollHandle, + bindings: Vec<(SharedString, SharedString)>, + pending_keys: SharedString, + _pending_input_subscription: Subscription, + _focus_out_subscription: Subscription, +} + +impl WhichKeyModal { + pub fn new( + workspace: WeakEntity, + window: &mut Window, + cx: &mut Context, + ) -> Self { + // Keep focus where it currently is + let focus_handle = window.focused(cx).unwrap_or(cx.focus_handle()); + + let handle = cx.weak_entity(); + let mut this = Self { + _workspace: workspace, + focus_handle: focus_handle.clone(), + scroll_handle: ScrollHandle::new(), + bindings: Vec::new(), + pending_keys: SharedString::new_static(""), + _pending_input_subscription: cx.observe_pending_input( + window, + |this: &mut Self, window, cx| { + this.update_pending_keys(window, cx); + }, + ), + _focus_out_subscription: window.on_focus_out(&focus_handle, cx, move |_, _, cx| { + handle.update(cx, |_, cx| cx.emit(DismissEvent)).ok(); + }), + }; + this.update_pending_keys(window, cx); + this + } + + pub fn dismiss(&self, cx: &mut Context) { + cx.emit(DismissEvent) + } + + fn update_pending_keys(&mut self, window: &mut Window, cx: &mut Context) { + let Some(pending_keys) = window.pending_input_keystrokes() else { + cx.emit(DismissEvent); + return; + }; + let bindings = window.possible_bindings_for_input(pending_keys); + + let mut binding_data = bindings + .iter() + .map(|binding| { + // Map to keystrokes + ( + binding + .keystrokes() + .iter() + .map(|k| k.inner().to_owned()) + .collect::>(), + binding.action(), + ) + }) + .filter(|(keystrokes, _action)| { + // Check if this binding matches any filtered keystroke pattern + !FILTERED_KEYSTROKES.iter().any(|filtered| { + keystrokes.len() >= filtered.len() + && keystrokes[..filtered.len()] == filtered[..] + }) + }) + .map(|(keystrokes, action)| { + // Map to remaining keystrokes and action name + let remaining_keystrokes = keystrokes[pending_keys.len()..].to_vec(); + let action_name: SharedString = + command_palette::humanize_action_name(action.name()).into(); + (remaining_keystrokes, action_name) + }) + .collect(); + + binding_data = group_bindings(binding_data); + + // Sort bindings from shortest to longest, with groups last + // Using stable sort to preserve relative order of equal elements + binding_data.sort_by(|(keystrokes_a, action_a), (keystrokes_b, action_b)| { + // Groups (actions starting with "+") should go last + let is_group_a = action_a.starts_with('+'); + let is_group_b = action_b.starts_with('+'); + + // First, separate groups from non-groups + let group_cmp = is_group_a.cmp(&is_group_b); + if group_cmp != std::cmp::Ordering::Equal { + return group_cmp; + } + + // Then sort by keystroke count + let keystroke_cmp = keystrokes_a.len().cmp(&keystrokes_b.len()); + if keystroke_cmp != std::cmp::Ordering::Equal { + return keystroke_cmp; + } + + // Finally sort by text length, then lexicographically for full stability + let text_a = text_for_keystrokes(keystrokes_a, cx); + let text_b = text_for_keystrokes(keystrokes_b, cx); + let text_len_cmp = text_a.len().cmp(&text_b.len()); + if text_len_cmp != std::cmp::Ordering::Equal { + return text_len_cmp; + } + text_a.cmp(&text_b) + }); + binding_data.dedup(); + self.pending_keys = text_for_keystrokes(&pending_keys, cx).into(); + self.bindings = binding_data + .into_iter() + .map(|(keystrokes, action)| (text_for_keystrokes(&keystrokes, cx).into(), action)) + .collect(); + } +} + +impl Render for WhichKeyModal { + fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { + let has_rows = !self.bindings.is_empty(); + let viewport_size = window.viewport_size(); + + let max_panel_width = px((f32::from(viewport_size.width) * 0.5).min(480.0)); + let max_content_height = px(f32::from(viewport_size.height) * 0.4); + + // Push above status bar when visible + let status_height = self + ._workspace + .upgrade() + .and_then(|workspace| { + workspace.read_with(cx, |workspace, cx| { + if workspace.status_bar_visible(cx) { + Some( + DynamicSpacing::Base04.px(cx) * 2.0 + + ThemeSettings::get_global(cx).ui_font_size(cx), + ) + } else { + None + } + }) + }) + .unwrap_or(px(0.)); + + let margin_bottom = px(16.); + let bottom_offset = margin_bottom + status_height; + + // Title section + let title_section = { + let mut column = v_flex().gap(px(0.)).child( + div() + .child( + Label::new(self.pending_keys.clone()) + .size(LabelSize::Default) + .weight(FontWeight::MEDIUM) + .color(Color::Accent), + ) + .mb(px(2.)), + ); + + if has_rows { + column = column.child( + div() + .child(Divider::horizontal().color(DividerColor::BorderFaded)) + .mb(px(2.)), + ); + } + + column + }; + + let content = h_flex() + .items_start() + .id("which-key-content") + .gap(px(8.)) + .overflow_y_scroll() + .track_scroll(&self.scroll_handle) + .h_full() + .max_h(max_content_height) + .child( + // Keystrokes column + v_flex() + .gap(px(4.)) + .flex_shrink_0() + .children(self.bindings.iter().map(|(keystrokes, _)| { + div() + .child( + Label::new(keystrokes.clone()) + .size(LabelSize::Default) + .color(Color::Accent), + ) + .text_align(gpui::TextAlign::Right) + })), + ) + .child( + // Actions column + v_flex() + .gap(px(4.)) + .flex_1() + .min_w_0() + .children(self.bindings.iter().map(|(_, action_name)| { + let is_group = action_name.starts_with('+'); + let label_color = if is_group { + Color::Success + } else { + Color::Default + }; + + div().child( + Label::new(action_name.clone()) + .size(LabelSize::Default) + .color(label_color) + .single_line() + .truncate(), + ) + })), + ); + + div() + .id("which-key-buffer-panel-scroll") + .occlude() + .absolute() + .bottom(bottom_offset) + .right(px(16.)) + .min_w(px(220.)) + .max_w(max_panel_width) + .elevation_3(cx) + .px(px(12.)) + .child(v_flex().child(title_section).when(has_rows, |el| { + el.child( + div() + .max_h(max_content_height) + .child(content) + .vertical_scrollbar_for(&self.scroll_handle, window, cx), + ) + })) + } +} + +impl EventEmitter for WhichKeyModal {} + +impl Focusable for WhichKeyModal { + fn focus_handle(&self, _cx: &App) -> gpui::FocusHandle { + self.focus_handle.clone() + } +} + +impl ModalView for WhichKeyModal { + fn render_bare(&self) -> bool { + true + } +} + +fn group_bindings( + binding_data: Vec<(Vec, SharedString)>, +) -> Vec<(Vec, SharedString)> { + let mut groups: HashMap, Vec<(Vec, SharedString)>> = + HashMap::new(); + + // Group bindings by their first keystroke + for (remaining_keystrokes, action_name) in binding_data { + let first_key = remaining_keystrokes.first().cloned(); + groups + .entry(first_key) + .or_default() + .push((remaining_keystrokes, action_name)); + } + + let mut result = Vec::new(); + + for (first_key, mut group_bindings) in groups { + // Remove duplicates within each group + group_bindings.dedup_by_key(|(keystrokes, _)| keystrokes.clone()); + + if let Some(first_key) = first_key + && group_bindings.len() > 1 + { + // This is a group - create a single entry with just the first keystroke + let first_keystroke = vec![first_key]; + let count = group_bindings.len(); + result.push((first_keystroke, format!("+{} keybinds", count).into())); + } else { + // Not a group or empty keystrokes - add all bindings as-is + result.append(&mut group_bindings); + } + } + + result +} diff --git a/crates/which_key/src/which_key_settings.rs b/crates/which_key/src/which_key_settings.rs new file mode 100644 index 0000000000000000000000000000000000000000..be19ab1521f4793305efca79b7026f79fd9064e2 --- /dev/null +++ b/crates/which_key/src/which_key_settings.rs @@ -0,0 +1,18 @@ +use settings::{RegisterSetting, Settings, SettingsContent, WhichKeySettingsContent}; + +#[derive(Debug, Clone, Copy, RegisterSetting)] +pub struct WhichKeySettings { + pub enabled: bool, + pub delay_ms: u64, +} + +impl Settings for WhichKeySettings { + fn from_settings(content: &SettingsContent) -> Self { + let which_key: &WhichKeySettingsContent = content.which_key.as_ref().unwrap(); + + Self { + enabled: which_key.enabled.unwrap(), + delay_ms: which_key.delay_ms.unwrap(), + } + } +} diff --git a/crates/workspace/src/dock.rs b/crates/workspace/src/dock.rs index edc5705a28ecd7d378c0f959ac82a6493c82d325..7f4b09df0f94fa421c399ed9d70163f7cc2ba203 100644 --- a/crates/workspace/src/dock.rs +++ b/crates/workspace/src/dock.rs @@ -1,5 +1,4 @@ use crate::persistence::model::DockData; -use crate::utility_pane::utility_slot_for_dock_position; use crate::{DraggedDock, Event, ModalLayer, Pane}; use crate::{Workspace, status_bar::StatusItemView}; use anyhow::Context as _; @@ -350,7 +349,7 @@ impl Dock { let focus_subscription = cx.on_focus(&focus_handle, window, |dock: &mut Dock, window, cx| { if let Some(active_entry) = dock.active_panel_entry() { - active_entry.panel.panel_focus_handle(cx).focus(window) + active_entry.panel.panel_focus_handle(cx).focus(window, cx) } }); let zoom_subscription = cx.subscribe(&workspace, |dock, workspace, e: &Event, cx| { @@ -593,7 +592,7 @@ impl Dock { this.set_panel_zoomed(&panel.to_any(), true, window, cx); if !PanelHandle::panel_focus_handle(panel, cx).contains_focused(window, cx) { - window.focus(&panel.focus_handle(cx)); + window.focus(&panel.focus_handle(cx), cx); } workspace .update(cx, |workspace, cx| { @@ -625,7 +624,7 @@ impl Dock { { this.set_open(true, window, cx); this.activate_panel(ix, window, cx); - window.focus(&panel.read(cx).focus_handle(cx)); + window.focus(&panel.read(cx).focus_handle(cx), cx); } } PanelEvent::Close => { @@ -705,7 +704,7 @@ impl Dock { panel: &Entity, window: &mut Window, cx: &mut Context, - ) { + ) -> bool { if let Some(panel_ix) = self .panel_entries .iter() @@ -724,15 +723,12 @@ impl Dock { } } - let slot = utility_slot_for_dock_position(self.position); - if let Some(workspace) = self.workspace.upgrade() { - workspace.update(cx, |workspace, cx| { - workspace.clear_utility_pane_if_provider(slot, Entity::entity_id(panel), cx); - }); - } - self.panel_entries.remove(panel_ix); cx.notify(); + + true + } else { + false } } @@ -1052,7 +1048,7 @@ impl Render for PanelButtons { name = name, toggle_state = !is_open ); - window.focus(&focus_handle); + window.focus(&focus_handle, cx); window.dispatch_action(action.boxed_clone(), cx) } }) diff --git a/crates/workspace/src/item.rs b/crates/workspace/src/item.rs index bb4b10fa63dc884b8cf0ab8eee8e3bc34880b2a5..6e415c23454388bc7931ff9d5e499924d6b8f55d 100644 --- a/crates/workspace/src/item.rs +++ b/crates/workspace/src/item.rs @@ -76,7 +76,13 @@ impl Settings for ItemSettings { fn from_settings(content: &settings::SettingsContent) -> Self { let tabs = content.tabs.as_ref().unwrap(); Self { - git_status: tabs.git_status.unwrap(), + git_status: tabs.git_status.unwrap() + && content + .git + .unwrap() + .enabled + .unwrap() + .is_git_status_enabled(), close_position: tabs.close_position.unwrap(), activate_on_close: tabs.activate_on_close.unwrap(), file_icons: tabs.file_icons.unwrap(), @@ -886,8 +892,12 @@ impl ItemHandle for Entity { // Only trigger autosave if focus has truly left the item. // If focus is still within the item's hierarchy (e.g., moved to a context menu), // don't trigger autosave to avoid unwanted formatting and cursor jumps. + // Also skip autosave if focus moved to a modal (e.g., command palette), + // since the user is still interacting with the workspace. let focus_handle = item.item_focus_handle(cx); - if !focus_handle.contains_focused(window, cx) { + if !focus_handle.contains_focused(window, cx) + && !workspace.has_active_modal(window, cx) + { Pane::autosave_item(&item, workspace.project.clone(), window, cx) .detach_and_log_err(cx); } @@ -1042,7 +1052,7 @@ impl ItemHandle for Entity { fn relay_action(&self, action: Box, window: &mut Window, cx: &mut App) { self.update(cx, |this, cx| { - this.focus_handle(cx).focus(window); + this.focus_handle(cx).focus(window, cx); window.dispatch_action(action, cx); }) } diff --git a/crates/workspace/src/modal_layer.rs b/crates/workspace/src/modal_layer.rs index d6f10f703100d89bef5babd4baa590df5fa0c8fd..4087e1a398ac2b89257fea6b4dce53278d0872a8 100644 --- a/crates/workspace/src/modal_layer.rs +++ b/crates/workspace/src/modal_layer.rs @@ -1,9 +1,18 @@ use gpui::{ AnyView, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable as _, ManagedView, - MouseButton, Subscription, + MouseButton, Pixels, Point, Subscription, }; use ui::prelude::*; +#[derive(Debug, Clone, Copy, Default)] +pub enum ModalPlacement { + #[default] + Centered, + Anchored { + position: Point, + }, +} + #[derive(Debug)] pub enum DismissDecision { Dismiss(bool), @@ -22,12 +31,17 @@ pub trait ModalView: ManagedView { fn fade_out_background(&self) -> bool { false } + + fn render_bare(&self) -> bool { + false + } } trait ModalViewHandle { fn on_before_dismiss(&mut self, window: &mut Window, cx: &mut App) -> DismissDecision; fn view(&self) -> AnyView; fn fade_out_background(&self, cx: &mut App) -> bool; + fn render_bare(&self, cx: &mut App) -> bool; } impl ModalViewHandle for Entity { @@ -42,6 +56,10 @@ impl ModalViewHandle for Entity { fn fade_out_background(&self, cx: &mut App) -> bool { self.read(cx).fade_out_background() } + + fn render_bare(&self, cx: &mut App) -> bool { + self.read(cx).render_bare() + } } pub struct ActiveModal { @@ -49,6 +67,7 @@ pub struct ActiveModal { _subscriptions: [Subscription; 2], previous_focus_handle: Option, focus_handle: FocusHandle, + placement: ModalPlacement, } pub struct ModalLayer { @@ -78,6 +97,19 @@ impl ModalLayer { where V: ModalView, B: FnOnce(&mut Window, &mut Context) -> V, + { + self.toggle_modal_with_placement(window, cx, ModalPlacement::Centered, build_view); + } + + pub fn toggle_modal_with_placement( + &mut self, + window: &mut Window, + cx: &mut Context, + placement: ModalPlacement, + build_view: B, + ) where + V: ModalView, + B: FnOnce(&mut Window, &mut Context) -> V, { if let Some(active_modal) = &self.active_modal { let is_close = active_modal.modal.view().downcast::().is_ok(); @@ -87,12 +119,17 @@ impl ModalLayer { } } let new_modal = cx.new(|cx| build_view(window, cx)); - self.show_modal(new_modal, window, cx); + self.show_modal(new_modal, placement, window, cx); cx.emit(ModalOpenedEvent); } - fn show_modal(&mut self, new_modal: Entity, window: &mut Window, cx: &mut Context) - where + fn show_modal( + &mut self, + new_modal: Entity, + placement: ModalPlacement, + window: &mut Window, + cx: &mut Context, + ) where V: ModalView, { let focus_handle = cx.focus_handle(); @@ -114,9 +151,10 @@ impl ModalLayer { ], previous_focus_handle: window.focused(cx), focus_handle, + placement, }); cx.defer_in(window, move |_, window, cx| { - window.focus(&new_modal.focus_handle(cx)); + window.focus(&new_modal.focus_handle(cx), cx); }); cx.notify(); } @@ -144,7 +182,7 @@ impl ModalLayer { if let Some(previous_focus) = active_modal.previous_focus_handle && active_modal.focus_handle.contains_focused(window, cx) { - previous_focus.focus(window); + previous_focus.focus(window, cx); } cx.notify(); } @@ -167,7 +205,35 @@ impl ModalLayer { impl Render for ModalLayer { fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { let Some(active_modal) = &self.active_modal else { - return div(); + return div().into_any_element(); + }; + + if active_modal.modal.render_bare(cx) { + return active_modal.modal.view().into_any_element(); + } + + let content = h_flex() + .occlude() + .child(active_modal.modal.view()) + .on_mouse_down(MouseButton::Left, |_, _, cx| { + cx.stop_propagation(); + }); + + let positioned = match active_modal.placement { + ModalPlacement::Centered => v_flex() + .h(px(0.0)) + .top_20() + .items_center() + .track_focus(&active_modal.focus_handle) + .child(content) + .into_any_element(), + ModalPlacement::Anchored { position } => div() + .absolute() + .left(position.x) + .top(position.y - px(20.)) + .track_focus(&active_modal.focus_handle) + .child(content) + .into_any_element(), }; div() @@ -180,20 +246,13 @@ impl Render for ModalLayer { background.fade_out(0.2); this.bg(background) }) - .child( - v_flex() - .h(px(0.0)) - .top_20() - .items_center() - .track_focus(&active_modal.focus_handle) - .child( - h_flex() - .occlude() - .child(active_modal.modal.view()) - .on_mouse_down(MouseButton::Left, |_, _, cx| { - cx.stop_propagation(); - }), - ), + .on_mouse_down( + MouseButton::Left, + cx.listener(|this, _, window, cx| { + this.hide_modal(window, cx); + }), ) + .child(positioned) + .into_any_element() } } diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rs index 036723c13755ff2a7b2b10e9684d822f239a8e0b..dd17c338a935571f4d0fe9d46b3b10fac9ffe218 100644 --- a/crates/workspace/src/pane.rs +++ b/crates/workspace/src/pane.rs @@ -625,11 +625,11 @@ impl Pane { self.last_focus_handle_by_item.get(&active_item.item_id()) && let Some(focus_handle) = weak_last_focus_handle.upgrade() { - focus_handle.focus(window); + focus_handle.focus(window, cx); return; } - active_item.item_focus_handle(cx).focus(window); + active_item.item_focus_handle(cx).focus(window, cx); } else if let Some(focused) = window.focused(cx) && !self.context_menu_focused(window, cx) { @@ -638,7 +638,7 @@ impl Pane { } } else if let Some(welcome_page) = self.welcome_page.as_ref() { if self.focus_handle.is_focused(window) { - welcome_page.read(cx).focus_handle(cx).focus(window); + welcome_page.read(cx).focus_handle(cx).focus(window, cx); } } } @@ -1846,6 +1846,7 @@ impl Pane { } for item_to_close in items_to_close { + let mut should_close = true; let mut should_save = true; if save_intent == SaveIntent::Close { workspace.update(cx, |workspace, cx| { @@ -1861,7 +1862,7 @@ impl Pane { { Ok(success) => { if !success { - break; + should_close = false; } } Err(err) => { @@ -1880,23 +1881,25 @@ impl Pane { })?; match answer.await { Ok(0) => {} - Ok(1..) | Err(_) => break, + Ok(1..) | Err(_) => should_close = false, } } } } // Remove the item from the pane. - pane.update_in(cx, |pane, window, cx| { - pane.remove_item( - item_to_close.item_id(), - false, - pane.close_pane_if_empty, - window, - cx, - ); - }) - .ok(); + if should_close { + pane.update_in(cx, |pane, window, cx| { + pane.remove_item( + item_to_close.item_id(), + false, + pane.close_pane_if_empty, + window, + cx, + ); + }) + .ok(); + } } pane.update(cx, |_, cx| cx.notify()).ok(); @@ -1999,7 +2002,7 @@ impl Pane { let should_activate = activate_pane || self.has_focus(window, cx); if self.items.len() == 1 && should_activate { - self.focus_handle.focus(window); + self.focus_handle.focus(window, cx); } else { self.activate_item( index_to_activate, @@ -2350,7 +2353,7 @@ impl Pane { pub fn focus_active_item(&mut self, window: &mut Window, cx: &mut Context) { if let Some(active_item) = self.active_item() { let focus_handle = active_item.item_focus_handle(cx); - window.focus(&focus_handle); + window.focus(&focus_handle, cx); } } @@ -6614,6 +6617,60 @@ mod tests { cx.simulate_prompt_answer("Discard all"); save.await.unwrap(); assert_item_labels(&pane, [], cx); + + add_labeled_item(&pane, "A", true, cx).update(cx, |item, cx| { + item.project_items + .push(TestProjectItem::new_dirty(1, "A.txt", cx)) + }); + add_labeled_item(&pane, "B", true, cx).update(cx, |item, cx| { + item.project_items + .push(TestProjectItem::new_dirty(2, "B.txt", cx)) + }); + add_labeled_item(&pane, "C", true, cx).update(cx, |item, cx| { + item.project_items + .push(TestProjectItem::new_dirty(3, "C.txt", cx)) + }); + assert_item_labels(&pane, ["A^", "B^", "C*^"], cx); + + let close_task = pane.update_in(cx, |pane, window, cx| { + pane.close_all_items( + &CloseAllItems { + save_intent: None, + close_pinned: false, + }, + window, + cx, + ) + }); + + cx.executor().run_until_parked(); + cx.simulate_prompt_answer("Discard all"); + close_task.await.unwrap(); + assert_item_labels(&pane, [], cx); + + add_labeled_item(&pane, "Clean1", false, cx); + add_labeled_item(&pane, "Dirty", true, cx).update(cx, |item, cx| { + item.project_items + .push(TestProjectItem::new_dirty(1, "Dirty.txt", cx)) + }); + add_labeled_item(&pane, "Clean2", false, cx); + assert_item_labels(&pane, ["Clean1", "Dirty^", "Clean2*"], cx); + + let close_task = pane.update_in(cx, |pane, window, cx| { + pane.close_all_items( + &CloseAllItems { + save_intent: None, + close_pinned: false, + }, + window, + cx, + ) + }); + + cx.executor().run_until_parked(); + cx.simulate_prompt_answer("Cancel"); + close_task.await.unwrap(); + assert_item_labels(&pane, ["Dirty*^"], cx); } #[gpui::test] diff --git a/crates/workspace/src/persistence.rs b/crates/workspace/src/persistence.rs index a992a9e1a20d1346a0c201afd72bb51327f00381..8d20339ec952020416e4b8d5846bf44f5f8e9b98 100644 --- a/crates/workspace/src/persistence.rs +++ b/crates/workspace/src/persistence.rs @@ -24,7 +24,6 @@ use project::{ }; use language::{LanguageName, Toolchain, ToolchainScope}; -use project::WorktreeId; use remote::{ DockerConnectionOptions, RemoteConnectionOptions, SshConnectionOptions, WslConnectionOptions, }; @@ -845,6 +844,44 @@ impl Domain for WorkspaceDb { host_name TEXT ) STRICT; ), + sql!(CREATE TABLE toolchains2 ( + workspace_id INTEGER, + worktree_root_path TEXT NOT NULL, + language_name TEXT NOT NULL, + name TEXT NOT NULL, + path TEXT NOT NULL, + raw_json TEXT NOT NULL, + relative_worktree_path TEXT NOT NULL, + PRIMARY KEY (workspace_id, worktree_root_path, language_name, relative_worktree_path)) STRICT; + INSERT OR REPLACE INTO toolchains2 + // The `instr(paths, '\n') = 0` part allows us to find all + // workspaces that have a single worktree, as `\n` is used as a + // separator when serializing the workspace paths, so if no `\n` is + // found, we know we have a single worktree. + SELECT toolchains.workspace_id, paths, language_name, name, path, raw_json, relative_worktree_path FROM toolchains INNER JOIN workspaces ON toolchains.workspace_id = workspaces.workspace_id AND instr(paths, '\n') = 0; + DROP TABLE toolchains; + ALTER TABLE toolchains2 RENAME TO toolchains; + ), + sql!(CREATE TABLE user_toolchains2 ( + remote_connection_id INTEGER, + workspace_id INTEGER NOT NULL, + worktree_root_path TEXT NOT NULL, + relative_worktree_path TEXT NOT NULL, + language_name TEXT NOT NULL, + name TEXT NOT NULL, + path TEXT NOT NULL, + raw_json TEXT NOT NULL, + + PRIMARY KEY (workspace_id, worktree_root_path, relative_worktree_path, language_name, name, path, raw_json)) STRICT; + INSERT OR REPLACE INTO user_toolchains2 + // The `instr(paths, '\n') = 0` part allows us to find all + // workspaces that have a single worktree, as `\n` is used as a + // separator when serializing the workspace paths, so if no `\n` is + // found, we know we have a single worktree. + SELECT user_toolchains.remote_connection_id, user_toolchains.workspace_id, paths, relative_worktree_path, language_name, name, path, raw_json FROM user_toolchains INNER JOIN workspaces ON user_toolchains.workspace_id = workspaces.workspace_id AND instr(paths, '\n') = 0; + DROP TABLE user_toolchains; + ALTER TABLE user_toolchains2 RENAME TO user_toolchains; + ), ]; // Allow recovering from bad migration that was initially shipped to nightly @@ -1030,11 +1067,11 @@ impl WorkspaceDb { workspace_id: WorkspaceId, remote_connection_id: Option, ) -> BTreeMap> { - type RowKind = (WorkspaceId, u64, String, String, String, String, String); + type RowKind = (WorkspaceId, String, String, String, String, String, String); let toolchains: Vec = self .select_bound(sql! { - SELECT workspace_id, worktree_id, relative_worktree_path, + SELECT workspace_id, worktree_root_path, relative_worktree_path, language_name, name, path, raw_json FROM user_toolchains WHERE remote_connection_id IS ?1 AND ( workspace_id IN (0, ?2) @@ -1048,7 +1085,7 @@ impl WorkspaceDb { for ( _workspace_id, - worktree_id, + worktree_root_path, relative_worktree_path, language_name, name, @@ -1058,22 +1095,24 @@ impl WorkspaceDb { { // INTEGER's that are primary keys (like workspace ids, remote connection ids and such) start at 1, so we're safe to let scope = if _workspace_id == WorkspaceId(0) { - debug_assert_eq!(worktree_id, u64::MAX); + debug_assert_eq!(worktree_root_path, String::default()); debug_assert_eq!(relative_worktree_path, String::default()); ToolchainScope::Global } else { debug_assert_eq!(workspace_id, _workspace_id); debug_assert_eq!( - worktree_id == u64::MAX, + worktree_root_path == String::default(), relative_worktree_path == String::default() ); let Some(relative_path) = RelPath::unix(&relative_worktree_path).log_err() else { continue; }; - if worktree_id != u64::MAX && relative_worktree_path != String::default() { + if worktree_root_path != String::default() + && relative_worktree_path != String::default() + { ToolchainScope::Subproject( - WorktreeId::from_usize(worktree_id as usize), + Arc::from(worktree_root_path.as_ref()), relative_path.into(), ) } else { @@ -1159,13 +1198,13 @@ impl WorkspaceDb { for (scope, toolchains) in workspace.user_toolchains { for toolchain in toolchains { - let query = sql!(INSERT OR REPLACE INTO user_toolchains(remote_connection_id, workspace_id, worktree_id, relative_worktree_path, language_name, name, path, raw_json) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)); - let (workspace_id, worktree_id, relative_worktree_path) = match scope { - ToolchainScope::Subproject(worktree_id, ref path) => (Some(workspace.id), Some(worktree_id), Some(path.as_unix_str().to_owned())), + let query = sql!(INSERT OR REPLACE INTO user_toolchains(remote_connection_id, workspace_id, worktree_root_path, relative_worktree_path, language_name, name, path, raw_json) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)); + let (workspace_id, worktree_root_path, relative_worktree_path) = match scope { + ToolchainScope::Subproject(ref worktree_root_path, ref path) => (Some(workspace.id), Some(worktree_root_path.to_string_lossy().into_owned()), Some(path.as_unix_str().to_owned())), ToolchainScope::Project => (Some(workspace.id), None, None), ToolchainScope::Global => (None, None, None), }; - let args = (remote_connection_id, workspace_id.unwrap_or(WorkspaceId(0)), worktree_id.map_or(usize::MAX,|id| id.to_usize()), relative_worktree_path.unwrap_or_default(), + let args = (remote_connection_id, workspace_id.unwrap_or(WorkspaceId(0)), worktree_root_path.unwrap_or_default(), relative_worktree_path.unwrap_or_default(), toolchain.language_name.as_ref().to_owned(), toolchain.name.to_string(), toolchain.path.to_string(), toolchain.as_json.to_string()); if let Err(err) = conn.exec_bound(query)?(args) { log::error!("{err}"); @@ -1844,24 +1883,24 @@ impl WorkspaceDb { pub(crate) async fn toolchains( &self, workspace_id: WorkspaceId, - ) -> Result)>> { + ) -> Result, Arc)>> { self.write(move |this| { let mut select = this .select_bound(sql!( SELECT - name, path, worktree_id, relative_worktree_path, language_name, raw_json + name, path, worktree_root_path, relative_worktree_path, language_name, raw_json FROM toolchains WHERE workspace_id = ? )) .context("select toolchains")?; - let toolchain: Vec<(String, String, u64, String, String, String)> = + let toolchain: Vec<(String, String, String, String, String, String)> = select(workspace_id)?; Ok(toolchain .into_iter() .filter_map( - |(name, path, worktree_id, relative_worktree_path, language, json)| { + |(name, path, worktree_root_path, relative_worktree_path, language, json)| { Some(( Toolchain { name: name.into(), @@ -1869,7 +1908,7 @@ impl WorkspaceDb { language_name: LanguageName::new(&language), as_json: serde_json::Value::from_str(&json).ok()?, }, - WorktreeId::from_proto(worktree_id), + Arc::from(worktree_root_path.as_ref()), RelPath::from_proto(&relative_worktree_path).log_err()?, )) }, @@ -1882,18 +1921,18 @@ impl WorkspaceDb { pub async fn set_toolchain( &self, workspace_id: WorkspaceId, - worktree_id: WorktreeId, + worktree_root_path: Arc, relative_worktree_path: Arc, toolchain: Toolchain, ) -> Result<()> { log::debug!( - "Setting toolchain for workspace, worktree: {worktree_id:?}, relative path: {relative_worktree_path:?}, toolchain: {}", + "Setting toolchain for workspace, worktree: {worktree_root_path:?}, relative path: {relative_worktree_path:?}, toolchain: {}", toolchain.name ); self.write(move |conn| { let mut insert = conn .exec_bound(sql!( - INSERT INTO toolchains(workspace_id, worktree_id, relative_worktree_path, language_name, name, path, raw_json) VALUES (?, ?, ?, ?, ?, ?, ?) + INSERT INTO toolchains(workspace_id, worktree_root_path, relative_worktree_path, language_name, name, path, raw_json) VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT DO UPDATE SET name = ?5, @@ -1904,7 +1943,7 @@ impl WorkspaceDb { insert(( workspace_id, - worktree_id.to_usize(), + worktree_root_path.to_string_lossy().into_owned(), relative_worktree_path.as_unix_str(), toolchain.language_name.as_ref(), toolchain.name.as_ref(), @@ -1919,7 +1958,6 @@ impl WorkspaceDb { pub(crate) async fn save_trusted_worktrees( &self, trusted_worktrees: HashMap, HashSet>, - trusted_workspaces: HashSet>, ) -> anyhow::Result<()> { use anyhow::Context as _; use db::sqlez::statement::Statement; @@ -1936,7 +1974,6 @@ impl WorkspaceDb { .into_iter() .map(move |abs_path| (Some(abs_path), host.clone())) }) - .chain(trusted_workspaces.into_iter().map(|host| (None, host))) .collect::>(); let mut first_worktree; let mut last_worktree = 0_usize; @@ -2001,7 +2038,7 @@ VALUES {placeholders};"# let trusted_worktrees = DB.trusted_worktrees()?; Ok(trusted_worktrees .into_iter() - .map(|(abs_path, user_name, host_name)| { + .filter_map(|(abs_path, user_name, host_name)| { let db_host = match (user_name, host_name) { (_, None) => None, (None, Some(host_name)) => Some(RemoteHostLocation { @@ -2014,21 +2051,17 @@ VALUES {placeholders};"# }), }; - match abs_path { - Some(abs_path) => { - if db_host != host { - (db_host, PathTrust::AbsPath(abs_path)) - } else if let Some(worktree_store) = &worktree_store { - find_worktree_in_store(worktree_store.read(cx), &abs_path, cx) - .map(PathTrust::Worktree) - .map(|trusted_worktree| (host.clone(), trusted_worktree)) - .unwrap_or_else(|| (db_host.clone(), PathTrust::AbsPath(abs_path))) - } else { - (db_host, PathTrust::AbsPath(abs_path)) - } - } - None => (db_host, PathTrust::Workspace), - } + let abs_path = abs_path?; + Some(if db_host != host { + (db_host, PathTrust::AbsPath(abs_path)) + } else if let Some(worktree_store) = &worktree_store { + find_worktree_in_store(worktree_store.read(cx), &abs_path, cx) + .map(PathTrust::Worktree) + .map(|trusted_worktree| (host.clone(), trusted_worktree)) + .unwrap_or_else(|| (db_host.clone(), PathTrust::AbsPath(abs_path))) + } else { + (db_host, PathTrust::AbsPath(abs_path)) + }) }) .fold(HashMap::default(), |mut acc, (remote_host, path_trust)| { acc.entry(remote_host) @@ -3302,4 +3335,53 @@ mod tests { assert_eq!(workspace.center_group, new_workspace.center_group); } + + #[gpui::test] + async fn test_empty_workspace_window_bounds() { + zlog::init_test(); + + let db = WorkspaceDb::open_test_db("test_empty_workspace_window_bounds").await; + let id = db.next_id().await.unwrap(); + + // Create a workspace with empty paths (empty workspace) + let empty_paths: &[&str] = &[]; + let display_uuid = Uuid::new_v4(); + let window_bounds = SerializedWindowBounds(WindowBounds::Windowed(Bounds { + origin: point(px(100.0), px(200.0)), + size: size(px(800.0), px(600.0)), + })); + + let workspace = SerializedWorkspace { + id, + paths: PathList::new(empty_paths), + location: SerializedWorkspaceLocation::Local, + center_group: Default::default(), + window_bounds: None, + display: None, + docks: Default::default(), + breakpoints: Default::default(), + centered_layout: false, + session_id: None, + window_id: None, + user_toolchains: Default::default(), + }; + + // Save the workspace (this creates the record with empty paths) + db.save_workspace(workspace.clone()).await; + + // Save window bounds separately (as the actual code does via set_window_open_status) + db.set_window_open_status(id, window_bounds, display_uuid) + .await + .unwrap(); + + // Retrieve it using empty paths + let retrieved = db.workspace_for_roots(empty_paths).unwrap(); + + // Verify window bounds were persisted + assert_eq!(retrieved.id, id); + assert!(retrieved.window_bounds.is_some()); + assert_eq!(retrieved.window_bounds.unwrap().0, window_bounds.0); + assert!(retrieved.display.is_some()); + assert_eq!(retrieved.display.unwrap(), display_uuid); + } } diff --git a/crates/workspace/src/security_modal.rs b/crates/workspace/src/security_modal.rs index 1b5509d4d64e5b1377c9675fb49d2981e8173668..bb1482d7cce2a9849a78a9512598e389a6e5eea0 100644 --- a/crates/workspace/src/security_modal.rs +++ b/crates/workspace/src/security_modal.rs @@ -23,7 +23,7 @@ use ui::{ use crate::{DismissDecision, ModalView, ToggleWorktreeSecurity}; pub struct SecurityModal { - restricted_paths: HashMap, RestrictedPath>, + restricted_paths: HashMap, home_dir: Option, trust_parents: bool, worktree_store: WeakEntity, @@ -34,7 +34,7 @@ pub struct SecurityModal { #[derive(Debug, PartialEq, Eq)] struct RestrictedPath { - abs_path: Option>, + abs_path: Arc, is_file: bool, host: Option, } @@ -102,48 +102,31 @@ impl Render for SecurityModal { .child(Icon::new(IconName::Warning).color(Color::Warning)) .child(Label::new(header_label)), ) - .children(self.restricted_paths.values().map(|restricted_path| { - let abs_path = restricted_path.abs_path.as_ref().and_then(|abs_path| { - if restricted_path.is_file { - abs_path.parent() - } else { - Some(abs_path.as_ref()) - } - }); - - let label = match abs_path { - Some(abs_path) => match &restricted_path.host { - Some(remote_host) => match &remote_host.user_name { - Some(user_name) => format!( - "{} ({}@{})", - self.shorten_path(abs_path).display(), - user_name, - remote_host.host_identifier - ), - None => format!( - "{} ({})", - self.shorten_path(abs_path).display(), - remote_host.host_identifier - ), - }, - None => self.shorten_path(abs_path).display().to_string(), - }, - None => match &restricted_path.host { - Some(remote_host) => match &remote_host.user_name { - Some(user_name) => format!( - "Workspace trust ({}@{})", - user_name, remote_host.host_identifier - ), - None => { - format!("Workspace trust ({})", remote_host.host_identifier) - } - }, - None => "Workspace trust".to_string(), + .children(self.restricted_paths.values().filter_map(|restricted_path| { + let abs_path = if restricted_path.is_file { + restricted_path.abs_path.parent() + } else { + Some(restricted_path.abs_path.as_ref()) + }?; + let label = match &restricted_path.host { + Some(remote_host) => match &remote_host.user_name { + Some(user_name) => format!( + "{} ({}@{})", + self.shorten_path(abs_path).display(), + user_name, + remote_host.host_identifier + ), + None => format!( + "{} ({})", + self.shorten_path(abs_path).display(), + remote_host.host_identifier + ), }, + None => self.shorten_path(abs_path).display().to_string(), }; - h_flex() + Some(h_flex() .pl(IconSize::default().rems() + rems(0.5)) - .child(Label::new(label).color(Color::Muted)) + .child(Label::new(label).color(Color::Muted))) })), ) .child( @@ -254,7 +237,7 @@ impl SecurityModal { has_restricted_files |= restricted_path.is_file; !restricted_path.is_file }) - .filter_map(|restricted_path| restricted_path.abs_path.as_ref()?.parent()) + .filter_map(|restricted_path| restricted_path.abs_path.parent()) .collect::>(); match available_parents.len() { 0 => { @@ -289,19 +272,17 @@ impl SecurityModal { let mut paths_to_trust = self .restricted_paths .keys() - .map(|worktree_id| match worktree_id { - Some(worktree_id) => PathTrust::Worktree(*worktree_id), - None => PathTrust::Workspace, - }) + .copied() + .map(PathTrust::Worktree) .collect::>(); if self.trust_parents { paths_to_trust.extend(self.restricted_paths.values().filter_map( |restricted_paths| { if restricted_paths.is_file { - Some(PathTrust::Workspace) + None } else { let parent_abs_path = - restricted_paths.abs_path.as_ref()?.parent()?.to_owned(); + restricted_paths.abs_path.parent()?.to_owned(); Some(PathTrust::AbsPath(parent_abs_path)) } }, @@ -322,42 +303,22 @@ impl SecurityModal { pub fn refresh_restricted_paths(&mut self, cx: &mut Context) { if let Some(trusted_worktrees) = TrustedWorktrees::try_get_global(cx) { if let Some(worktree_store) = self.worktree_store.upgrade() { - let mut new_restricted_worktrees = trusted_worktrees + let new_restricted_worktrees = trusted_worktrees .read(cx) - .restricted_worktrees(worktree_store.read(cx), self.remote_host.clone(), cx) + .restricted_worktrees(worktree_store.read(cx), cx) .into_iter() - .filter_map(|restricted_path| { - let restricted_path = match restricted_path { - Some((worktree_id, abs_path)) => { - let worktree = - worktree_store.read(cx).worktree_for_id(worktree_id, cx)?; - ( - Some(worktree_id), - RestrictedPath { - abs_path: Some(abs_path), - is_file: worktree.read(cx).is_single_file(), - host: self.remote_host.clone(), - }, - ) - } - None => ( - None, - RestrictedPath { - abs_path: None, - is_file: false, - host: self.remote_host.clone(), - }, - ), - }; - Some(restricted_path) + .filter_map(|(worktree_id, abs_path)| { + let worktree = worktree_store.read(cx).worktree_for_id(worktree_id, cx)?; + Some(( + worktree_id, + RestrictedPath { + abs_path, + is_file: worktree.read(cx).is_single_file(), + host: self.remote_host.clone(), + }, + )) }) .collect::>(); - // Do not clutter the UI: - // * trusting regular local worktrees assumes the workspace is trusted either, on the same host. - // * trusting a workspace trusts all single-file worktrees on the same host. - if new_restricted_worktrees.len() > 1 { - new_restricted_worktrees.remove(&None); - } if self.restricted_paths != new_restricted_worktrees { self.trust_parents = false; diff --git a/crates/workspace/src/welcome.rs b/crates/workspace/src/welcome.rs index 93ff1ea266ff9f40b64064ea03d9bd1b91161300..4d84f3072f87ffa3246a313cbc749ddd61287d25 100644 --- a/crates/workspace/src/welcome.rs +++ b/crates/workspace/src/welcome.rs @@ -250,12 +250,12 @@ impl WelcomePage { } fn select_next(&mut self, _: &SelectNext, window: &mut Window, cx: &mut Context) { - window.focus_next(); + window.focus_next(cx); cx.notify(); } fn select_previous(&mut self, _: &SelectPrevious, window: &mut Window, cx: &mut Context) { - window.focus_prev(); + window.focus_prev(cx); cx.notify(); } diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 516dc867ae14f7138dff0a968e210e214d0beb29..fa8e3a3dc2af33054907ea8a8c1ba095a3259207 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -135,7 +135,9 @@ pub use workspace_settings::{ use zed_actions::{Spawn, feedback::FileBugReport}; use crate::{ - item::ItemBufferKind, notifications::NotificationId, utility_pane::UTILITY_PANE_MIN_WIDTH, + item::ItemBufferKind, + notifications::NotificationId, + utility_pane::{UTILITY_PANE_MIN_WIDTH, utility_slot_for_dock_position}, }; use crate::{ persistence::{ @@ -986,6 +988,7 @@ impl AppState { #[cfg(any(test, feature = "test-support"))] pub fn test(cx: &mut App) -> Arc { + use fs::Fs; use node_runtime::NodeRuntime; use session::Session; use settings::SettingsStore; @@ -996,6 +999,7 @@ impl AppState { } let fs = fs::FakeFs::new(cx.background_executor().clone()); + ::set_global(fs.clone(), cx); let languages = Arc::new(LanguageRegistry::test(cx.background_executor().clone())); let clock = Arc::new(clock::FakeSystemClock::new()); let http_client = http_client::FakeHttpClient::with_404_response(); @@ -1200,6 +1204,7 @@ pub struct Workspace { last_open_dock_positions: Vec, removing: bool, utility_panes: UtilityPaneState, + next_modal_placement: Option, } impl EventEmitter for Workspace {} @@ -1233,21 +1238,18 @@ impl Workspace { if let Some(trusted_worktrees) = TrustedWorktrees::try_get_global(cx) { cx.subscribe(&trusted_worktrees, |workspace, worktrees_store, e, cx| { if let TrustedWorktreesEvent::Trusted(..) = e { - let (new_trusted_workspaces, new_trusted_worktrees) = worktrees_store - .update(cx, |worktrees_store, cx| { - worktrees_store.trusted_paths_for_serialization(cx) - }); // Do not persist auto trusted worktrees if !ProjectSettings::get_global(cx).session.trust_all_worktrees { + let new_trusted_worktrees = + worktrees_store.update(cx, |worktrees_store, cx| { + worktrees_store.trusted_paths_for_serialization(cx) + }); let timeout = cx.background_executor().timer(SERIALIZATION_THROTTLE_TIME); workspace._schedule_serialize_worktree_trust = cx.background_spawn(async move { timeout.await; persistence::DB - .save_trusted_worktrees( - new_trusted_worktrees, - new_trusted_workspaces, - ) + .save_trusted_worktrees(new_trusted_worktrees) .await .log_err(); }); @@ -1393,7 +1395,7 @@ impl Workspace { cx.on_focus_lost(window, |this, window, cx| { let focus_handle = this.focus_handle(cx); - window.focus(&focus_handle); + window.focus(&focus_handle, cx); }) .detach(); @@ -1417,7 +1419,7 @@ impl Workspace { cx.subscribe_in(¢er_pane, window, Self::handle_pane_event) .detach(); - window.focus(¢er_pane.focus_handle(cx)); + window.focus(¢er_pane.focus_handle(cx), cx); cx.emit(Event::PaneAdded(center_pane.clone())); @@ -1619,6 +1621,7 @@ impl Workspace { last_open_dock_positions: Vec::new(), removing: false, utility_panes: UtilityPaneState::default(), + next_modal_placement: None, } } @@ -1696,8 +1699,22 @@ impl Workspace { let toolchains = DB.toolchains(workspace_id).await?; - for (toolchain, worktree_id, path) in toolchains { + for (toolchain, worktree_path, path) in toolchains { let toolchain_path = PathBuf::from(toolchain.path.clone().to_string()); + let Some(worktree_id) = project_handle.read_with(cx, |this, cx| { + this.find_worktree(&worktree_path, cx) + .and_then(|(worktree, rel_path)| { + if rel_path.is_empty() { + Some(worktree.read(cx).id()) + } else { + None + } + }) + })? + else { + // We did not find a worktree with a given path, but that's whatever. + continue; + }; if !app_state.fs.is_file(toolchain_path.as_path()).await { continue; } @@ -1747,26 +1764,18 @@ impl Workspace { window } else { let window_bounds_override = window_bounds_env_override(); - let is_empty_workspace = project_paths.is_empty(); let (window_bounds, display) = if let Some(bounds) = window_bounds_override { (Some(WindowBounds::Windowed(bounds)), None) - } else if let Some(workspace) = serialized_workspace.as_ref() { + } else if let Some(workspace) = serialized_workspace.as_ref() + && let Some(display) = workspace.display + && let Some(bounds) = workspace.window_bounds.as_ref() + { // Reopening an existing workspace - restore its saved bounds - if let (Some(display), Some(bounds)) = - (workspace.display, workspace.window_bounds.as_ref()) - { - (Some(bounds.0), Some(display)) - } else { - (None, None) - } - } else if is_empty_workspace { - // Empty workspace - try to restore the last known no-project window bounds - if let Some((display, bounds)) = persistence::read_default_window_bounds() { - (Some(bounds), Some(display)) - } else { - (None, None) - } + (Some(bounds.0), Some(display)) + } else if let Some((display, bounds)) = persistence::read_default_window_bounds() { + // New or empty workspace - use the last known window bounds + (Some(bounds), Some(display)) } else { // New window - let GPUI's default_bounds() handle cascading (None, None) @@ -1893,10 +1902,18 @@ impl Workspace { window: &mut Window, cx: &mut Context, ) { + let mut found_in_dock = None; for dock in [&self.left_dock, &self.bottom_dock, &self.right_dock] { - dock.update(cx, |dock, cx| { - dock.remove_panel(panel, window, cx); - }) + let found = dock.update(cx, |dock, cx| dock.remove_panel(panel, window, cx)); + + if found { + found_in_dock = Some(dock.clone()); + } + } + if let Some(found_in_dock) = found_in_dock { + let position = found_in_dock.read(cx).position(); + let slot = utility_slot_for_dock_position(position); + self.clear_utility_pane_if_provider(slot, Entity::entity_id(panel), cx); } } @@ -2057,7 +2074,7 @@ impl Workspace { ) -> Task> { let to_load = if let Some(pane) = pane.upgrade() { pane.update(cx, |pane, cx| { - window.focus(&pane.focus_handle(cx)); + window.focus(&pane.focus_handle(cx), cx); loop { // Retrieve the weak item handle from the history. let entry = pane.nav_history_mut().pop(mode, cx)?; @@ -3176,7 +3193,7 @@ impl Workspace { } } else { let focus_handle = &active_panel.panel_focus_handle(cx); - window.focus(focus_handle); + window.focus(focus_handle, cx); reveal_dock = true; } } @@ -3188,7 +3205,7 @@ impl Workspace { if focus_center { self.active_pane - .update(cx, |pane, cx| window.focus(&pane.focus_handle(cx))) + .update(cx, |pane, cx| window.focus(&pane.focus_handle(cx), cx)) } cx.notify(); @@ -3356,7 +3373,7 @@ impl Workspace { if let Some(panel) = panel.as_ref() { if should_focus(&**panel, window, cx) { dock.set_open(true, window, cx); - panel.panel_focus_handle(cx).focus(window); + panel.panel_focus_handle(cx).focus(window, cx); } else { focus_center = true; } @@ -3366,7 +3383,7 @@ impl Workspace { if focus_center { self.active_pane - .update(cx, |pane, cx| window.focus(&pane.focus_handle(cx))) + .update(cx, |pane, cx| window.focus(&pane.focus_handle(cx), cx)) } result_panel = panel; @@ -3440,7 +3457,7 @@ impl Workspace { if focus_center { self.active_pane - .update(cx, |pane, cx| window.focus(&pane.focus_handle(cx))) + .update(cx, |pane, cx| window.focus(&pane.focus_handle(cx), cx)) } if self.zoomed_position != dock_to_reveal { @@ -3471,7 +3488,7 @@ impl Workspace { .detach(); self.panes.push(pane.clone()); - window.focus(&pane.focus_handle(cx)); + window.focus(&pane.focus_handle(cx), cx); cx.emit(Event::PaneAdded(pane.clone())); pane @@ -3866,7 +3883,7 @@ impl Workspace { ) { let panes = self.center.panes(); if let Some(pane) = panes.get(action.0).map(|p| (*p).clone()) { - window.focus(&pane.focus_handle(cx)); + window.focus(&pane.focus_handle(cx), cx); } else { self.split_and_clone(self.active_pane.clone(), SplitDirection::Right, window, cx) .detach(); @@ -3936,7 +3953,7 @@ impl Workspace { if let Some(ix) = panes.iter().position(|pane| **pane == self.active_pane) { let next_ix = (ix + 1) % panes.len(); let next_pane = panes[next_ix].clone(); - window.focus(&next_pane.focus_handle(cx)); + window.focus(&next_pane.focus_handle(cx), cx); } } @@ -3945,7 +3962,7 @@ impl Workspace { if let Some(ix) = panes.iter().position(|pane| **pane == self.active_pane) { let prev_ix = cmp::min(ix.wrapping_sub(1), panes.len() - 1); let prev_pane = panes[prev_ix].clone(); - window.focus(&prev_pane.focus_handle(cx)); + window.focus(&prev_pane.focus_handle(cx), cx); } } @@ -4041,7 +4058,7 @@ impl Workspace { Some(ActivateInDirectionTarget::Pane(pane)) => { let pane = pane.read(cx); if let Some(item) = pane.active_item() { - item.item_focus_handle(cx).focus(window); + item.item_focus_handle(cx).focus(window, cx); } else { log::error!( "Could not find a focus target when in switching focus in {direction} direction for a pane", @@ -4053,7 +4070,7 @@ impl Workspace { window.defer(cx, move |window, cx| { let dock = dock.read(cx); if let Some(panel) = dock.active_panel() { - panel.panel_focus_handle(cx).focus(window); + panel.panel_focus_handle(cx).focus(window, cx); } else { log::error!("Could not find a focus target when in switching focus in {direction} direction for a {:?} dock", dock.position()); } @@ -4222,7 +4239,7 @@ impl Workspace { cx: &mut Context, ) { self.active_pane = pane.clone(); - self.active_item_path_changed(window, cx); + self.active_item_path_changed(true, window, cx); self.last_active_center_pane = Some(pane.downgrade()); } @@ -4279,7 +4296,7 @@ impl Workspace { } serialize_workspace = *focus_changed || pane != self.active_pane(); if pane == self.active_pane() { - self.active_item_path_changed(window, cx); + self.active_item_path_changed(*focus_changed, window, cx); self.update_active_view_for_followers(window, cx); } else if *local { self.set_active_pane(pane, window, cx); @@ -4295,7 +4312,7 @@ impl Workspace { } pane::Event::ChangeItemTitle => { if *pane == self.active_pane { - self.active_item_path_changed(window, cx); + self.active_item_path_changed(false, window, cx); } serialize_workspace = false; } @@ -4464,7 +4481,7 @@ impl Workspace { cx.notify(); } else { - self.active_item_path_changed(window, cx); + self.active_item_path_changed(true, window, cx); } cx.emit(Event::PaneRemoved); } @@ -4673,7 +4690,7 @@ impl Workspace { // if you're already following, find the right pane and focus it. if let Some(follower_state) = self.follower_states.get(&leader_id) { - window.focus(&follower_state.pane().focus_handle(cx)); + window.focus(&follower_state.pane().focus_handle(cx), cx); return; } @@ -4718,14 +4735,19 @@ impl Workspace { self.follower_states.contains_key(&id.into()) } - fn active_item_path_changed(&mut self, window: &mut Window, cx: &mut Context) { + fn active_item_path_changed( + &mut self, + focus_changed: bool, + window: &mut Window, + cx: &mut Context, + ) { cx.emit(Event::ActiveItemChanged); let active_entry = self.active_project_path(cx); self.project.update(cx, |project, cx| { project.set_active_path(active_entry.clone(), cx) }); - if let Some(project_path) = &active_entry { + if focus_changed && let Some(project_path) = &active_entry { let git_store_entity = self.project.read(cx).git_store().clone(); git_store_entity.update(cx, |git_store, cx| { git_store.set_active_repo_for_path(project_path, cx); @@ -5485,12 +5507,12 @@ impl Workspace { ) { self.panes.retain(|p| p != pane); if let Some(focus_on) = focus_on { - focus_on.update(cx, |pane, cx| window.focus(&pane.focus_handle(cx))); + focus_on.update(cx, |pane, cx| window.focus(&pane.focus_handle(cx), cx)); } else if self.active_pane() == pane { self.panes .last() .unwrap() - .update(cx, |pane, cx| window.focus(&pane.focus_handle(cx))); + .update(cx, |pane, cx| window.focus(&pane.focus_handle(cx), cx)); } if self.last_active_center_pane == Some(pane.downgrade()) { self.last_active_center_pane = None; @@ -5664,12 +5686,24 @@ impl Workspace { persistence::DB.save_workspace(serialized_workspace).await; }) } - WorkspaceLocation::DetachFromSession => window.spawn(cx, async move |_| { - persistence::DB - .set_session_id(database_id, None) - .await - .log_err(); - }), + WorkspaceLocation::DetachFromSession => { + let window_bounds = SerializedWindowBounds(window.window_bounds()); + let display = window.display(cx).and_then(|d| d.uuid().ok()); + window.spawn(cx, async move |_| { + persistence::DB + .set_window_open_status( + database_id, + window_bounds, + display.unwrap_or_default(), + ) + .await + .log_err(); + persistence::DB + .set_session_id(database_id, None) + .await + .log_err(); + }) + } WorkspaceLocation::None => Task::ready(()), } } @@ -6248,7 +6282,7 @@ impl Workspace { let workspace = Self::new(Default::default(), project, app_state, window, cx); workspace .active_pane - .update(cx, |pane, cx| window.focus(&pane.focus_handle(cx))); + .update(cx, |pane, cx| window.focus(&pane.focus_handle(cx), cx)); workspace } @@ -6294,12 +6328,25 @@ impl Workspace { self.modal_layer.read(cx).active_modal() } + pub fn is_modal_open(&self, cx: &App) -> bool { + self.modal_layer.read(cx).active_modal::().is_some() + } + + pub fn set_next_modal_placement(&mut self, placement: ModalPlacement) { + self.next_modal_placement = Some(placement); + } + + fn take_next_modal_placement(&mut self) -> ModalPlacement { + self.next_modal_placement.take().unwrap_or_default() + } + pub fn toggle_modal(&mut self, window: &mut Window, cx: &mut App, build: B) where B: FnOnce(&mut Window, &mut Context) -> V, { + let placement = self.take_next_modal_placement(); self.modal_layer.update(cx, |modal_layer, cx| { - modal_layer.toggle_modal(window, cx, build) + modal_layer.toggle_modal_with_placement(window, cx, placement, build) }) } @@ -8199,9 +8246,22 @@ async fn open_remote_project_inner( cx: &mut AsyncApp, ) -> Result>>> { let toolchains = DB.toolchains(workspace_id).await?; - for (toolchain, worktree_id, path) in toolchains { + for (toolchain, worktree_path, path) in toolchains { project .update(cx, |this, cx| { + let Some(worktree_id) = + this.find_worktree(&worktree_path, cx) + .and_then(|(worktree, rel_path)| { + if rel_path.is_empty() { + Some(worktree.read(cx).id()) + } else { + None + } + }) + else { + return Task::ready(None); + }; + this.activate_toolchain(ProjectPath { worktree_id, path }, toolchain, cx) })? .await; @@ -8722,7 +8782,7 @@ fn move_all_items( // This automatically removes duplicate items in the pane to_pane.update(cx, |destination, cx| { destination.add_item(item_handle, true, true, None, window, cx); - window.focus(&destination.focus_handle(cx)) + window.focus(&destination.focus_handle(cx), cx) }); } } @@ -8766,7 +8826,7 @@ pub fn move_item( cx, ); if activate { - window.focus(&destination.focus_handle(cx)) + window.focus(&destination.focus_handle(cx), cx) } }); } @@ -9364,7 +9424,7 @@ mod tests { let right_pane = right_pane.await.unwrap(); cx.focus(&right_pane); - let mut close = right_pane.update_in(cx, |pane, window, cx| { + let close = right_pane.update_in(cx, |pane, window, cx| { pane.close_all_items(&CloseAllItems::default(), window, cx) .unwrap() }); @@ -9376,9 +9436,16 @@ mod tests { assert!(!msg.contains("3.txt")); assert!(!msg.contains("4.txt")); + // With best-effort close, cancelling item 1 keeps it open but items 4 + // and (3,4) still close since their entries exist in left pane. cx.simulate_prompt_answer("Cancel"); close.await; + right_pane.read_with(cx, |pane, _| { + assert_eq!(pane.items_len(), 1); + }); + + // Remove item 3 from left pane, making (2,3) the only item with entry 3. left_pane .update_in(cx, |left_pane, window, cx| { left_pane.close_item_by_id( @@ -9391,26 +9458,25 @@ mod tests { .await .unwrap(); - close = right_pane.update_in(cx, |pane, window, cx| { + let close = left_pane.update_in(cx, |pane, window, cx| { pane.close_all_items(&CloseAllItems::default(), window, cx) .unwrap() }); cx.executor().run_until_parked(); let details = cx.pending_prompt().unwrap().1; - assert!(details.contains("1.txt")); - assert!(!details.contains("2.txt")); + assert!(details.contains("0.txt")); assert!(details.contains("3.txt")); - // ideally this assertion could be made, but today we can only - // save whole items not project items, so the orphaned item 3 causes - // 4 to be saved too. - // assert!(!details.contains("4.txt")); + assert!(details.contains("4.txt")); + // Ideally 2.txt wouldn't appear since entry 2 still exists in item 2. + // But we can only save whole items, so saving (2,3) for entry 3 includes 2. + // assert!(!details.contains("2.txt")); cx.simulate_prompt_answer("Save all"); - cx.executor().run_until_parked(); close.await; - right_pane.read_with(cx, |pane, _| { + + left_pane.read_with(cx, |pane, _| { assert_eq!(pane.items_len(), 0); }); } diff --git a/crates/worktree/Cargo.toml b/crates/worktree/Cargo.toml index 6d132fbd2cb8c7a1282bffcea6577260a15c4572..e7d3ac34e1886bd76e0a0f5d23ea981b6626909a 100644 --- a/crates/worktree/Cargo.toml +++ b/crates/worktree/Cargo.toml @@ -25,8 +25,10 @@ test-support = [ [dependencies] anyhow.workspace = true async-lock.workspace = true +chardetng.workspace = true clock.workspace = true collections.workspace = true +encoding_rs.workspace = true fs.workspace = true futures.workspace = true fuzzy.workspace = true diff --git a/crates/worktree/src/worktree.rs b/crates/worktree/src/worktree.rs index 6ec19493840da0b9de3eb55ac483488339ec5e8d..7145bccd514fbb5d6093efda765a826162c91260 100644 --- a/crates/worktree/src/worktree.rs +++ b/crates/worktree/src/worktree.rs @@ -5,8 +5,10 @@ mod worktree_tests; use ::ignore::gitignore::{Gitignore, GitignoreBuilder}; use anyhow::{Context as _, Result, anyhow}; +use chardetng::EncodingDetector; use clock::ReplicaId; use collections::{HashMap, HashSet, VecDeque}; +use encoding_rs::Encoding; use fs::{Fs, MTime, PathEvent, RemoveOptions, Watcher, copy_recursive, read_dir_items}; use futures::{ FutureExt as _, Stream, StreamExt, @@ -105,6 +107,8 @@ pub enum CreatedEntry { pub struct LoadedFile { pub file: Arc, pub text: String, + pub encoding: &'static Encoding, + pub has_bom: bool, } pub struct LoadedBinaryFile { @@ -741,10 +745,14 @@ impl Worktree { path: Arc, text: Rope, line_ending: LineEnding, + encoding: &'static Encoding, + has_bom: bool, cx: &Context, ) -> Task>> { match self { - Worktree::Local(this) => this.write_file(path, text, line_ending, cx), + Worktree::Local(this) => { + this.write_file(path, text, line_ending, encoding, has_bom, cx) + } Worktree::Remote(_) => { Task::ready(Err(anyhow!("remote worktree can't yet write files"))) } @@ -1351,7 +1359,9 @@ impl LocalWorktree { anyhow::bail!("File is too large to load"); } } - let text = fs.load(&abs_path).await?; + + let content = fs.load_bytes(&abs_path).await?; + let (text, encoding, has_bom) = decode_byte(content); let worktree = this.upgrade().context("worktree was dropped")?; let file = match entry.await? { @@ -1379,7 +1389,12 @@ impl LocalWorktree { } }; - Ok(LoadedFile { file, text }) + Ok(LoadedFile { + file, + text, + encoding, + has_bom, + }) }) } @@ -1462,6 +1477,8 @@ impl LocalWorktree { path: Arc, text: Rope, line_ending: LineEnding, + encoding: &'static Encoding, + has_bom: bool, cx: &Context, ) -> Task>> { let fs = self.fs.clone(); @@ -1471,7 +1488,49 @@ impl LocalWorktree { let write = cx.background_spawn({ let fs = fs.clone(); let abs_path = abs_path.clone(); - async move { fs.save(&abs_path, &text, line_ending).await } + async move { + let bom_bytes = if has_bom { + if encoding == encoding_rs::UTF_16LE { + vec![0xFF, 0xFE] + } else if encoding == encoding_rs::UTF_16BE { + vec![0xFE, 0xFF] + } else if encoding == encoding_rs::UTF_8 { + vec![0xEF, 0xBB, 0xBF] + } else { + vec![] + } + } else { + vec![] + }; + + // For UTF-8, use the optimized `fs.save` which writes Rope chunks directly to disk + // without allocating a contiguous string. + if encoding == encoding_rs::UTF_8 && !has_bom { + return fs.save(&abs_path, &text, line_ending).await; + } + // For legacy encodings (e.g. Shift-JIS), we fall back to converting the entire Rope + // to a String/Bytes in memory before writing. + // + // Note: This is inefficient for very large files compared to the streaming approach above, + // but supporting streaming writes for arbitrary encodings would require a significant + // refactor of the `fs` crate to expose a Writer interface. + let text_string = text.to_string(); + let normalized_text = match line_ending { + LineEnding::Unix => text_string, + LineEnding::Windows => text_string.replace('\n', "\r\n"), + }; + + let (cow, _, _) = encoding.encode(&normalized_text); + let bytes = if !bom_bytes.is_empty() { + let mut bytes = bom_bytes; + bytes.extend_from_slice(&cow); + bytes.into() + } else { + cow + }; + + fs.write(&abs_path, &bytes).await + } }); cx.spawn(async move |this, cx| { @@ -5782,3 +5841,40 @@ impl fs::Watcher for NullWatcher { Ok(()) } } + +fn decode_byte(bytes: Vec) -> (String, &'static Encoding, bool) { + // check BOM + if let Some((encoding, _bom_len)) = Encoding::for_bom(&bytes) { + let (cow, _) = encoding.decode_with_bom_removal(&bytes); + return (cow.into_owned(), encoding, true); + } + + fn detect_encoding(bytes: Vec) -> (String, &'static Encoding) { + let mut detector = EncodingDetector::new(); + detector.feed(&bytes, true); + + let encoding = detector.guess(None, true); // Use None for TLD hint to ensure neutral detection logic. + + let (cow, _, _) = encoding.decode(&bytes); + (cow.into_owned(), encoding) + } + + match String::from_utf8(bytes) { + Ok(text) => { + // ISO-2022-JP (and other ISO-2022 variants) consists entirely of 7-bit ASCII bytes, + // so it is valid UTF-8. However, it contains escape sequences starting with '\x1b'. + // If we find an escape character, we double-check the encoding to prevent + // displaying raw escape sequences instead of the correct characters. + if text.contains('\x1b') { + let (s, enc) = detect_encoding(text.into_bytes()); + (s, enc, false) + } else { + (text, encoding_rs::UTF_8, false) + } + } + Err(e) => { + let (s, enc) = detect_encoding(e.into_bytes()); + (s, enc, false) + } + } +} diff --git a/crates/worktree/src/worktree_tests.rs b/crates/worktree/src/worktree_tests.rs index 12f2863aab6c4b4376157f3499fa332051a4822f..094a6d52ea4168752578eab06cea511a57e65c10 100644 --- a/crates/worktree/src/worktree_tests.rs +++ b/crates/worktree/src/worktree_tests.rs @@ -1,5 +1,6 @@ use crate::{Entry, EntryKind, Event, PathChange, Worktree, WorktreeModelHandle}; -use anyhow::Result; +use anyhow::{Context as _, Result}; +use encoding_rs; use fs::{FakeFs, Fs, RealFs, RemoveOptions}; use git::{DOT_GIT, GITIGNORE, REPO_EXCLUDE}; use gpui::{AppContext as _, BackgroundExecutor, BorrowAppContext, Context, Task, TestAppContext}; @@ -19,6 +20,7 @@ use std::{ }; use util::{ ResultExt, path, + paths::PathStyle, rel_path::{RelPath, rel_path}, test::TempTree, }; @@ -723,6 +725,8 @@ async fn test_write_file(cx: &mut TestAppContext) { rel_path("tracked-dir/file.txt").into(), "hello".into(), Default::default(), + encoding_rs::UTF_8, + false, cx, ) }) @@ -734,6 +738,8 @@ async fn test_write_file(cx: &mut TestAppContext) { rel_path("ignored-dir/file.txt").into(), "world".into(), Default::default(), + encoding_rs::UTF_8, + false, cx, ) }) @@ -2035,8 +2041,14 @@ fn randomly_mutate_worktree( }) } else { log::info!("overwriting file {:?} ({})", &entry.path, entry.id.0); - let task = - worktree.write_file(entry.path.clone(), "".into(), Default::default(), cx); + let task = worktree.write_file( + entry.path.clone(), + "".into(), + Default::default(), + encoding_rs::UTF_8, + false, + cx, + ); cx.background_spawn(async move { task.await?; Ok(()) @@ -2552,3 +2564,176 @@ fn init_test(cx: &mut gpui::TestAppContext) { cx.set_global(settings_store); }); } + +#[gpui::test] +async fn test_load_file_encoding(cx: &mut TestAppContext) { + init_test(cx); + let test_cases: Vec<(&str, &[u8], &str)> = vec![ + ("utf8.txt", "こんにちは".as_bytes(), "こんにちは"), // "こんにちは" is Japanese "Hello" + ( + "sjis.txt", + &[0x82, 0xb1, 0x82, 0xf1, 0x82, 0xc9, 0x82, 0xbf, 0x82, 0xcd], + "こんにちは", + ), + ( + "eucjp.txt", + &[0xa4, 0xb3, 0xa4, 0xf3, 0xa4, 0xcb, 0xa4, 0xc1, 0xa4, 0xcf], + "こんにちは", + ), + ( + "iso2022jp.txt", + &[ + 0x1b, 0x24, 0x42, 0x24, 0x33, 0x24, 0x73, 0x24, 0x4b, 0x24, 0x41, 0x24, 0x4f, 0x1b, + 0x28, 0x42, + ], + "こんにちは", + ), + // Western Europe (Windows-1252) + // "Café" -> 0xE9 is 'é' in Windows-1252 (it is typically 0xC3 0xA9 in UTF-8) + ("win1252.txt", &[0x43, 0x61, 0x66, 0xe9], "Café"), + // Chinese Simplified (GBK) + // Note: We use a slightly longer string here because short byte sequences can be ambiguous + // in multi-byte encodings. Providing more context helps the heuristic detector guess correctly. + // Text: "今天天气不错" (Today's weather is not bad / nice) + // Bytes: + // 今: BD F1 + // 天: CC EC + // 天: CC EC + // 气: C6 F8 + // 不: B2 BB + // 错: B4 ED + ( + "gbk.txt", + &[ + 0xbd, 0xf1, 0xcc, 0xec, 0xcc, 0xec, 0xc6, 0xf8, 0xb2, 0xbb, 0xb4, 0xed, + ], + "今天天气不错", + ), + ( + "utf16le_bom.txt", + &[ + 0xFF, 0xFE, // BOM + 0x53, 0x30, // こ + 0x93, 0x30, // ん + 0x6B, 0x30, // に + 0x61, 0x30, // ち + 0x6F, 0x30, // は + ], + "こんにちは", + ), + ( + "utf8_bom.txt", + &[ + 0xEF, 0xBB, 0xBF, // UTF-8 BOM + 0xE3, 0x81, 0x93, // こ + 0xE3, 0x82, 0x93, // ん + 0xE3, 0x81, 0xAB, // に + 0xE3, 0x81, 0xA1, // ち + 0xE3, 0x81, 0xAF, // は + ], + "こんにちは", + ), + ]; + + let root_path = if cfg!(windows) { + Path::new("C:\\root") + } else { + Path::new("/root") + }; + + let fs = FakeFs::new(cx.background_executor.clone()); + + let mut files_json = serde_json::Map::new(); + for (name, _, _) in &test_cases { + files_json.insert(name.to_string(), serde_json::Value::String("".to_string())); + } + + for (name, bytes, _) in &test_cases { + let path = root_path.join(name); + fs.write(&path, bytes).await.unwrap(); + } + + let tree = Worktree::local( + root_path, + true, + fs, + Default::default(), + true, + &mut cx.to_async(), + ) + .await + .unwrap(); + + cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete()) + .await; + + for (name, _, expected) in test_cases { + let loaded = tree + .update(cx, |tree, cx| tree.load_file(rel_path(name), cx)) + .await + .with_context(|| format!("Failed to load {}", name)) + .unwrap(); + + assert_eq!( + loaded.text, expected, + "Encoding mismatch for file: {}", + name + ); + } +} + +#[gpui::test] +async fn test_write_file_encoding(cx: &mut gpui::TestAppContext) { + init_test(cx); + let fs = FakeFs::new(cx.executor()); + let root_path = if cfg!(windows) { + Path::new("C:\\root") + } else { + Path::new("/root") + }; + fs.create_dir(root_path).await.unwrap(); + let file_path = root_path.join("test.txt"); + + fs.insert_file(&file_path, "initial".into()).await; + + let worktree = Worktree::local( + root_path, + true, + fs.clone(), + Default::default(), + true, + &mut cx.to_async(), + ) + .await + .unwrap(); + + let path: Arc = Path::new("test.txt").into(); + let rel_path = RelPath::new(&path, PathStyle::local()).unwrap().into_arc(); + + let text = text::Rope::from("こんにちは"); + + let task = worktree.update(cx, |wt, cx| { + wt.write_file( + rel_path, + text, + text::LineEnding::Unix, + encoding_rs::SHIFT_JIS, + false, + cx, + ) + }); + + task.await.unwrap(); + + let bytes = fs.load_bytes(&file_path).await.unwrap(); + + let expected_bytes = vec![ + 0x82, 0xb1, // こ + 0x82, 0xf1, // ん + 0x82, 0xc9, // に + 0x82, 0xbf, // ち + 0x82, 0xcd, // は + ]; + + assert_eq!(bytes, expected_bytes, "Should be saved as Shift-JIS"); +} diff --git a/crates/worktree_benchmarks/src/main.rs b/crates/worktree_benchmarks/src/main.rs index 00f268b75fc5f1e7d6033ec46f3718ea39cdccda..c1b76f9e3c483ec6c989cc255a11c5320d4b49f7 100644 --- a/crates/worktree_benchmarks/src/main.rs +++ b/crates/worktree_benchmarks/src/main.rs @@ -5,8 +5,7 @@ use std::{ use fs::RealFs; use gpui::Application; -use settings::Settings; -use worktree::{Worktree, WorktreeSettings}; +use worktree::Worktree; fn main() { let Some(worktree_root_path) = std::env::args().nth(1) else { @@ -27,6 +26,7 @@ fn main() { true, fs, Arc::new(AtomicUsize::new(0)), + true, cx, ) .await diff --git a/crates/zed/Cargo.toml b/crates/zed/Cargo.toml index 955540843489ac21d79042854eb6fcebf5f64318..80eca20e00309bb8d22552287a1c39cb9891307d 100644 --- a/crates/zed/Cargo.toml +++ b/crates/zed/Cargo.toml @@ -15,10 +15,6 @@ tracy = ["ztracing/tracy"] [[bin]] name = "zed" -path = "src/zed-main.rs" - -[lib] -name = "zed" path = "src/main.rs" [dependencies] @@ -163,6 +159,7 @@ vim_mode_setting.workspace = true watch.workspace = true web_search.workspace = true web_search_providers.workspace = true +which_key.workspace = true workspace.workspace = true zed_actions.workspace = true zed_env_vars.workspace = true @@ -195,6 +192,10 @@ terminal_view = { workspace = true, features = ["test-support"] } tree-sitter-md.workspace = true tree-sitter-rust.workspace = true workspace = { workspace = true, features = ["test-support"] } +agent_ui = { workspace = true, features = ["test-support"] } +agent_ui_v2 = { workspace = true, features = ["test-support"] } +search = { workspace = true, features = ["test-support"] } + [package.metadata.bundle-dev] icon = ["resources/app-icon-dev@2x.png", "resources/app-icon-dev.png"] diff --git a/crates/zed/resources/Document.icns b/crates/zed/resources/Document.icns new file mode 100644 index 0000000000000000000000000000000000000000..5d0185c81a32c214f213f12243aeab01e32830e1 Binary files /dev/null and b/crates/zed/resources/Document.icns differ diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index c8137a71c0f2a8524f6310d7cd711978ed833d1a..03e02bb0107d736c07eb3fc9626856943f8d80a6 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -1,3 +1,6 @@ +// Disable command line from opening on release mode +#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] + mod reliability; mod zed; @@ -15,11 +18,13 @@ use extension::ExtensionHostProxy; use fs::{Fs, RealFs}; use futures::{StreamExt, channel::oneshot, future}; use git::GitHostingProviderRegistry; +use git_ui::clone::clone_and_open; use gpui::{App, AppContext, Application, AsyncApp, Focusable as _, QuitMode, UpdateGlobal as _}; use gpui_tokio::Tokio; use language::LanguageRegistry; use onboarding::{FIRST_OPEN, show_onboarding_view}; +use project_panel::ProjectPanel; use prompt_store::PromptBuilder; use remote::RemoteConnectionOptions; use reqwest_client::ReqwestClient; @@ -33,11 +38,12 @@ use release_channel::{AppCommitSha, AppVersion, ReleaseChannel}; use session::{AppSession, Session}; use settings::{BaseKeymap, Settings, SettingsStore, watch_config_file}; use std::{ + cell::RefCell, env, io::{self, IsTerminal}, path::{Path, PathBuf}, - pin::Pin, process, + rc::Rc, sync::{Arc, OnceLock}, time::Instant, }; @@ -164,9 +170,9 @@ fn fail_to_open_window(e: anyhow::Error, _cx: &mut App) { .detach(); } } -pub static STARTUP_TIME: OnceLock = OnceLock::new(); +static STARTUP_TIME: OnceLock = OnceLock::new(); -pub fn main() { +fn main() { STARTUP_TIME.get_or_init(|| Instant::now()); #[cfg(unix)] @@ -484,14 +490,7 @@ pub fn main() { }) .detach(); - let trust_task = trusted_worktrees::wait_for_default_workspace_trust("Node runtime", cx) - .map(|trust_task| Box::pin(trust_task) as Pin>); - let node_runtime = NodeRuntime::new( - client.http_client(), - Some(shell_env_loaded_rx), - rx, - trust_task, - ); + let node_runtime = NodeRuntime::new(client.http_client(), Some(shell_env_loaded_rx), rx); debug_adapter_extension::init(extension_host_proxy.clone(), cx); languages::init(languages.clone(), fs.clone(), node_runtime.clone(), cx); @@ -664,6 +663,7 @@ pub fn main() { inspector_ui::init(app_state.clone(), cx); json_schema_store::init(cx); miniprofiler_ui::init(*STARTUP_TIME.get().unwrap(), cx); + which_key::init(cx); cx.observe_global::({ let http = app_state.client.http_client(); @@ -819,7 +819,7 @@ fn handle_open_request(request: OpenRequest, app_state: Arc, cx: &mut workspace::get_any_active_workspace(app_state, cx.clone()).await?; workspace.update(cx, |workspace, window, cx| { if let Some(panel) = workspace.panel::(cx) { - panel.focus_handle(cx).focus(window); + panel.focus_handle(cx).focus(window, cx); } }) }) @@ -900,6 +900,79 @@ fn handle_open_request(request: OpenRequest, app_state: Arc, cx: &mut }) .detach_and_log_err(cx); } + OpenRequestKind::GitClone { repo_url } => { + workspace::with_active_or_new_workspace(cx, |_workspace, window, cx| { + if window.is_window_active() { + clone_and_open( + repo_url, + cx.weak_entity(), + window, + cx, + Arc::new(|workspace: &mut workspace::Workspace, window, cx| { + workspace.focus_panel::(window, cx); + }), + ); + return; + } + + let subscription = Rc::new(RefCell::new(None)); + subscription.replace(Some(cx.observe_in(&cx.entity(), window, { + let subscription = subscription.clone(); + let repo_url = repo_url; + move |_, workspace_entity, window, cx| { + if window.is_window_active() && subscription.take().is_some() { + clone_and_open( + repo_url.clone(), + workspace_entity.downgrade(), + window, + cx, + Arc::new(|workspace: &mut workspace::Workspace, window, cx| { + workspace.focus_panel::(window, cx); + }), + ); + } + } + }))); + }); + } + OpenRequestKind::GitCommit { sha } => { + cx.spawn(async move |cx| { + let paths_with_position = + derive_paths_with_position(app_state.fs.as_ref(), request.open_paths).await; + let (workspace, _results) = open_paths_with_positions( + &paths_with_position, + &[], + app_state, + workspace::OpenOptions::default(), + cx, + ) + .await?; + + workspace + .update(cx, |workspace, window, cx| { + let Some(repo) = workspace.project().read(cx).active_repository(cx) + else { + log::error!("no active repository found for commit view"); + return Err(anyhow::anyhow!("no active repository found")); + }; + + git_ui::commit_view::CommitView::open( + sha, + repo.downgrade(), + workspace.weak_handle(), + None, + None, + window, + cx, + ); + Ok(()) + }) + .log_err(); + + anyhow::Ok(()) + }) + .detach_and_log_err(cx); + } } return; @@ -1270,7 +1343,7 @@ fn init_paths() -> HashMap> { }) } -pub fn stdout_is_a_pty() -> bool { +fn stdout_is_a_pty() -> bool { std::env::var(FORCE_CLI_MODE_ENV_VAR_NAME).ok().is_none() && io::stdout().is_terminal() } @@ -1516,14 +1589,14 @@ fn dump_all_gpui_actions() { struct ActionDef { name: &'static str, human_name: String, - aliases: &'static [&'static str], + deprecated_aliases: &'static [&'static str], documentation: Option<&'static str>, } let mut actions = gpui::generate_list_of_all_registered_actions() .map(|action| ActionDef { name: action.name, human_name: command_palette::humanize_action_name(action.name), - aliases: action.deprecated_aliases, + deprecated_aliases: action.deprecated_aliases, documentation: action.documentation, }) .collect::>(); diff --git a/crates/zed/src/zed-main.rs b/crates/zed/src/zed-main.rs deleted file mode 100644 index 6c49c197dda01e97828c3662aa09ecf57804dfbc..0000000000000000000000000000000000000000 --- a/crates/zed/src/zed-main.rs +++ /dev/null @@ -1,8 +0,0 @@ -// Disable command line from opening on release mode -#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] - -pub fn main() { - // separated out so that the file containing the main function can be imported by other crates, - // while having all gpui resources that are registered in main (primarily actions) initialized - zed::main(); -} diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index d2764c5c334ba32730982fc55e80d6197de3a2aa..3441cb88d96b06dfdbb65a58553d2c58f435d157 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -477,7 +477,7 @@ pub fn initialize_workspace( initialize_panels(prompt_builder.clone(), window, cx); register_actions(app_state.clone(), workspace, window, cx); - workspace.focus_handle(cx).focus(window); + workspace.focus_handle(cx).focus(window, cx); }) .detach(); } @@ -707,7 +707,6 @@ fn setup_or_teardown_ai_panel( .disable_ai || cfg!(test); let existing_panel = workspace.panel::

(cx); - match (disable_ai, existing_panel) { (false, None) => cx.spawn_in(window, async move |workspace, cx| { let panel = load_panel(workspace.clone(), cx.clone()).await?; @@ -2327,7 +2326,7 @@ mod tests { use project::{Project, ProjectPath}; use semver::Version; use serde_json::json; - use settings::{SettingsStore, watch_config_file}; + use settings::{SaturatingBool, SettingsStore, watch_config_file}; use std::{ path::{Path, PathBuf}, time::Duration, @@ -4781,7 +4780,6 @@ mod tests { "activity_indicator", "agent", "agents", - #[cfg(not(target_os = "macos"))] "app_menu", "assistant", "assistant2", @@ -5171,6 +5169,28 @@ mod tests { ); } + #[gpui::test] + async fn test_disable_ai_crash(cx: &mut gpui::TestAppContext) { + let app_state = init_test(cx); + cx.update(init); + let project = Project::test(app_state.fs.clone(), [], cx).await; + let _window = cx.add_window(|window, cx| Workspace::test_new(project, window, cx)); + + cx.run_until_parked(); + + cx.update(|cx| { + SettingsStore::update_global(cx, |settings_store, cx| { + settings_store.update_user_settings(cx, |settings| { + settings.disable_ai = Some(SaturatingBool(true)); + }); + }); + }); + + cx.run_until_parked(); + + // If this panics, the test has failed + } + #[gpui::test] async fn test_prefer_focused_window(cx: &mut gpui::TestAppContext) { let app_state = init_test(cx); diff --git a/crates/zed/src/zed/component_preview.rs b/crates/zed/src/zed/component_preview.rs index 14a46d8882d1d3d371c50e9886062a124917a48d..e3c7fc8df542448d5b8b290e96405546be7b4b1e 100644 --- a/crates/zed/src/zed/component_preview.rs +++ b/crates/zed/src/zed/component_preview.rs @@ -161,7 +161,7 @@ impl ComponentPreview { component_preview.update_component_list(cx); let focus_handle = component_preview.filter_editor.read(cx).focus_handle(cx); - window.focus(&focus_handle); + window.focus(&focus_handle, cx); Ok(component_preview) } @@ -770,7 +770,7 @@ impl Item for ComponentPreview { self.workspace_id = workspace.database_id(); let focus_handle = self.filter_editor.read(cx).focus_handle(cx); - window.focus(&focus_handle); + window.focus(&focus_handle, cx); } } diff --git a/crates/zed/src/zed/edit_prediction_registry.rs b/crates/zed/src/zed/edit_prediction_registry.rs index 77a1f71596f9cf1d2f4e32137580d0e3648359f5..51327bfc9ab715a1b11aa3c639ffd60b6b0a0ea8 100644 --- a/crates/zed/src/zed/edit_prediction_registry.rs +++ b/crates/zed/src/zed/edit_prediction_registry.rs @@ -145,23 +145,6 @@ fn register_backward_compatible_actions(editor: &mut Editor, cx: &mut Context| { - editor.next_edit_prediction(&Default::default(), window, cx); - }, - )) - .detach(); - editor - .register_action(cx.listener( - |editor, - _: &copilot::PreviousSuggestion, - window: &mut Window, - cx: &mut Context| { - editor.previous_edit_prediction(&Default::default(), window, cx); - }, - )) - .detach(); } fn assign_edit_prediction_provider( diff --git a/crates/zed/src/zed/open_listener.rs b/crates/zed/src/zed/open_listener.rs index 6352c20e5c0dcd0bd25063ca3a7bbcae87e48e3f..842f98520133c70f711d84d3f490bec1ec59e16f 100644 --- a/crates/zed/src/zed/open_listener.rs +++ b/crates/zed/src/zed/open_listener.rs @@ -25,6 +25,7 @@ use std::path::{Path, PathBuf}; use std::sync::Arc; use std::thread; use std::time::Duration; +use ui::SharedString; use util::ResultExt; use util::paths::PathWithPosition; use workspace::PathList; @@ -58,6 +59,12 @@ pub enum OpenRequestKind { /// `None` opens settings without navigating to a specific path. setting_path: Option, }, + GitClone { + repo_url: SharedString, + }, + GitCommit { + sha: String, + }, } impl OpenRequest { @@ -110,6 +117,10 @@ impl OpenRequest { this.kind = Some(OpenRequestKind::Setting { setting_path: Some(setting_path.to_string()), }); + } else if let Some(clone_path) = url.strip_prefix("zed://git/clone") { + this.parse_git_clone_url(clone_path)? + } else if let Some(commit_path) = url.strip_prefix("zed://git/commit/") { + this.parse_git_commit_url(commit_path)? } else if url.starts_with("ssh://") { this.parse_ssh_file_path(&url, cx)? } else if let Some(zed_link) = parse_zed_link(&url, cx) { @@ -138,6 +149,48 @@ impl OpenRequest { } } + fn parse_git_clone_url(&mut self, clone_path: &str) -> Result<()> { + // Format: /?repo= or ?repo= + let clone_path = clone_path.strip_prefix('/').unwrap_or(clone_path); + + let query = clone_path + .strip_prefix('?') + .context("invalid git clone url: missing query string")?; + + let repo_url = url::form_urlencoded::parse(query.as_bytes()) + .find_map(|(key, value)| (key == "repo").then_some(value)) + .filter(|s| !s.is_empty()) + .context("invalid git clone url: missing repo query parameter")? + .to_string() + .into(); + + self.kind = Some(OpenRequestKind::GitClone { repo_url }); + + Ok(()) + } + + fn parse_git_commit_url(&mut self, commit_path: &str) -> Result<()> { + // Format: ?repo= + let (sha, query) = commit_path + .split_once('?') + .context("invalid git commit url: missing query string")?; + anyhow::ensure!(!sha.is_empty(), "invalid git commit url: missing sha"); + + let repo = url::form_urlencoded::parse(query.as_bytes()) + .find_map(|(key, value)| (key == "repo").then_some(value)) + .filter(|s| !s.is_empty()) + .context("invalid git commit url: missing repo query parameter")? + .to_string(); + + self.open_paths.push(repo); + + self.kind = Some(OpenRequestKind::GitCommit { + sha: sha.to_string(), + }); + + Ok(()) + } + fn parse_ssh_file_path(&mut self, file: &str, cx: &App) -> Result<()> { let url = url::Url::parse(file)?; let host = url @@ -688,6 +741,86 @@ mod tests { assert_eq!(request.open_paths, vec!["/"]); } + #[gpui::test] + fn test_parse_git_commit_url(cx: &mut TestAppContext) { + let _app_state = init_test(cx); + + // Test basic git commit URL + let request = cx.update(|cx| { + OpenRequest::parse( + RawOpenRequest { + urls: vec!["zed://git/commit/abc123?repo=path/to/repo".into()], + ..Default::default() + }, + cx, + ) + .unwrap() + }); + + match request.kind.unwrap() { + OpenRequestKind::GitCommit { sha } => { + assert_eq!(sha, "abc123"); + } + _ => panic!("expected GitCommit variant"), + } + // Verify path was added to open_paths for workspace routing + assert_eq!(request.open_paths, vec!["path/to/repo"]); + + // Test with URL encoded path + let request = cx.update(|cx| { + OpenRequest::parse( + RawOpenRequest { + urls: vec!["zed://git/commit/def456?repo=path%20with%20spaces".into()], + ..Default::default() + }, + cx, + ) + .unwrap() + }); + + match request.kind.unwrap() { + OpenRequestKind::GitCommit { sha } => { + assert_eq!(sha, "def456"); + } + _ => panic!("expected GitCommit variant"), + } + assert_eq!(request.open_paths, vec!["path with spaces"]); + + // Test with empty path + cx.update(|cx| { + assert!( + OpenRequest::parse( + RawOpenRequest { + urls: vec!["zed://git/commit/abc123?repo=".into()], + ..Default::default() + }, + cx, + ) + .unwrap_err() + .to_string() + .contains("missing repo") + ); + }); + + // Test error case: missing SHA + let result = cx.update(|cx| { + OpenRequest::parse( + RawOpenRequest { + urls: vec!["zed://git/commit/abc123?foo=bar".into()], + ..Default::default() + }, + cx, + ) + }); + assert!(result.is_err()); + assert!( + result + .unwrap_err() + .to_string() + .contains("missing repo query parameter") + ); + } + #[gpui::test] async fn test_open_workspace_with_directory(cx: &mut TestAppContext) { let app_state = init_test(cx); @@ -980,4 +1113,80 @@ mod tests { assert!(!errored_reuse); } + + #[gpui::test] + fn test_parse_git_clone_url(cx: &mut TestAppContext) { + let _app_state = init_test(cx); + + let request = cx.update(|cx| { + OpenRequest::parse( + RawOpenRequest { + urls: vec![ + "zed://git/clone/?repo=https://github.com/zed-industries/zed.git".into(), + ], + ..Default::default() + }, + cx, + ) + .unwrap() + }); + + match request.kind { + Some(OpenRequestKind::GitClone { repo_url }) => { + assert_eq!(repo_url, "https://github.com/zed-industries/zed.git"); + } + _ => panic!("Expected GitClone kind"), + } + } + + #[gpui::test] + fn test_parse_git_clone_url_without_slash(cx: &mut TestAppContext) { + let _app_state = init_test(cx); + + let request = cx.update(|cx| { + OpenRequest::parse( + RawOpenRequest { + urls: vec![ + "zed://git/clone?repo=https://github.com/zed-industries/zed.git".into(), + ], + ..Default::default() + }, + cx, + ) + .unwrap() + }); + + match request.kind { + Some(OpenRequestKind::GitClone { repo_url }) => { + assert_eq!(repo_url, "https://github.com/zed-industries/zed.git"); + } + _ => panic!("Expected GitClone kind"), + } + } + + #[gpui::test] + fn test_parse_git_clone_url_with_encoding(cx: &mut TestAppContext) { + let _app_state = init_test(cx); + + let request = cx.update(|cx| { + OpenRequest::parse( + RawOpenRequest { + urls: vec![ + "zed://git/clone/?repo=https%3A%2F%2Fgithub.com%2Fzed-industries%2Fzed.git" + .into(), + ], + ..Default::default() + }, + cx, + ) + .unwrap() + }); + + match request.kind { + Some(OpenRequestKind::GitClone { repo_url }) => { + assert_eq!(repo_url, "https://github.com/zed-industries/zed.git"); + } + _ => panic!("Expected GitClone kind"), + } + } } diff --git a/crates/zed_actions/src/lib.rs b/crates/zed_actions/src/lib.rs index 458ca10ecdf8915eef3ee69c6334b1a14cc0c219..85b6d4d37d06d5f1c229fc852dd5bad117bbd9d7 100644 --- a/crates/zed_actions/src/lib.rs +++ b/crates/zed_actions/src/lib.rs @@ -354,6 +354,8 @@ pub mod agent { ResetAgentZoom, /// Toggles the utility/agent pane open/closed state. ToggleAgentPane, + /// Pastes clipboard content without any formatting. + PasteRaw, ] ); } diff --git a/crates/ztracing/src/lib.rs b/crates/ztracing/src/lib.rs index b9b318cc3565d8ced2a5496f1240409542d23c5a..c9007be1ed43150ef877d51c882aee77845e5bd6 100644 --- a/crates/ztracing/src/lib.rs +++ b/crates/ztracing/src/lib.rs @@ -1,8 +1,8 @@ -pub use tracing::Level; +pub use tracing::{Level, field}; #[cfg(ztracing)] pub use tracing::{ - debug_span, error_span, event, info_span, instrument, span, trace_span, warn_span, + Span, debug_span, error_span, event, info_span, instrument, span, trace_span, warn_span, }; #[cfg(not(ztracing))] pub use ztracing_macro::instrument; @@ -26,17 +26,23 @@ pub use __consume_all_tokens as span; #[macro_export] macro_rules! __consume_all_tokens { ($($t:tt)*) => { - $crate::FakeSpan + $crate::Span }; } -pub struct FakeSpan; -impl FakeSpan { +#[cfg(not(ztracing))] +pub struct Span; + +#[cfg(not(ztracing))] +impl Span { + pub fn current() -> Self { + Self + } + pub fn enter(&self) {} -} -// #[cfg(not(ztracing))] -// pub use span; + pub fn record(&self, _t: T, _s: S) {} +} #[cfg(ztracing)] pub fn init() { diff --git a/docs/.rules b/docs/.rules index 17c0e97450ce50f6846c865d58289257f2008f5c..4e6ca312f13b12a54a73d736ffeed8a8e09061ef 100644 --- a/docs/.rules +++ b/docs/.rules @@ -17,6 +17,7 @@ - Apologetic tone for missing features—state the limitation and move on - Comparisons that disparage other tools—be factual, not competitive - Meta-commentary about honesty ("the honest take is...", "to be frank...", "honestly...")—let honesty show through frank assessments, not announcements +- LLM-isms and filler words ("entirely," "certainly,", "deeply," "definitely," "actually")—these add nothing ## Content Structure diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index d6dd7a0aef9737fb095e87705e813f87fd0ed683..a82ddac990c4379df03db2b4bdcd8272eb8715e9 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -46,6 +46,7 @@ - [Tasks](./tasks.md) - [Tab Switcher](./tab-switcher.md) - [Remote Development](./remote-development.md) +- [Dev Containers](./dev-containers.md) - [Environment Variables](./environment.md) - [REPL](./repl.md) @@ -91,6 +92,9 @@ - [VS Code](./migrate/vs-code.md) - [IntelliJ IDEA](./migrate/intellij.md) +- [PyCharm](./migrate/pycharm.md) +- [WebStorm](./migrate/webstorm.md) +- [RustRover](./migrate/rustrover.md) # Language Support @@ -173,7 +177,6 @@ - [Linux](./development/linux.md) - [Windows](./development/windows.md) - [FreeBSD](./development/freebsd.md) - - [Local Collaboration](./development/local-collaboration.md) - [Using Debuggers](./development/debuggers.md) - [Performance](./performance.md) - [Glossary](./development/glossary.md) diff --git a/docs/src/ai/rules.md b/docs/src/ai/rules.md index 4169920425e66eb41a895deb60da3a198d74df08..972bbc94e82937502739cf585cc8f60dbcda8808 100644 --- a/docs/src/ai/rules.md +++ b/docs/src/ai/rules.md @@ -46,7 +46,7 @@ Having a series of rules files specifically tailored to prompt engineering can a Here are a couple of helpful resources for writing better rules: -- [Anthropic: Prompt Engineering](https://docs.anthropic.com/en/docs/build-with-claude/prompt-engineering/overview) +- [Anthropic: Prompt Engineering](https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/overview) - [OpenAI: Prompt Engineering](https://platform.openai.com/docs/guides/prompt-engineering) ### Editing the Default Rules {#default-rules} diff --git a/docs/src/completions.md b/docs/src/completions.md index ff96ede7503cd461bbd3d7b4afdedcaa2f36a2e5..7b35ec2d09d91a7ba7dc5ae4b968157e0184227f 100644 --- a/docs/src/completions.md +++ b/docs/src/completions.md @@ -15,6 +15,10 @@ When there is an appropriate language server available, Zed will provide complet You can manually trigger completions with `ctrl-space` or by triggering the `editor::ShowCompletions` action from the command palette. +> Note: Using `ctrl-space` in Zed requires disabling the macOS global shortcut. +> Open **System Settings** > **Keyboard** > **Keyboard Shortcut**s > +> **Input Sources** and uncheck **Select the previous input source**. + For more information, see: - [Configuring Supported Languages](./configuring-languages.md) diff --git a/docs/src/configuring-zed.md b/docs/src/configuring-zed.md index 8a638d9f7857e1a55aaa5589a77110a7b803bbfe..81318aa8885fe883acc394e7fe983d7721dd33a5 100644 --- a/docs/src/configuring-zed.md +++ b/docs/src/configuring-zed.md @@ -1585,6 +1585,26 @@ Positive `integer` value between 1 and 32. Values outside of this range will be `boolean` values +## Extend List On Newline + +- Description: Whether to continue lists when pressing Enter at the end of a list item. Supports unordered, ordered, and task lists. Pressing Enter on an empty list item removes the marker and exits the list. +- Setting: `extend_list_on_newline` +- Default: `true` + +**Options** + +`boolean` values + +## Indent List On Tab + +- Description: Whether to indent list items when pressing Tab on a line containing only a list marker. This enables quick creation of nested lists. +- Setting: `indent_list_on_tab` +- Default: `true` + +**Options** + +`boolean` values + ## Status Bar - Description: Control various elements in the status bar. Note that some items in the status bar have their own settings set elsewhere. diff --git a/docs/src/dev-containers.md b/docs/src/dev-containers.md new file mode 100644 index 0000000000000000000000000000000000000000..c87b204ee9cded48edb95752dd234fa55df71338 --- /dev/null +++ b/docs/src/dev-containers.md @@ -0,0 +1,50 @@ +# Dev Containers + +Dev Containers provide a consistent, reproducible development environment by defining your project's dependencies, tools, and settings in a container configuration. + +If your repository includes a `.devcontainer/devcontainer.json` file, Zed can open a project inside a development container. + +## Requirements + +- Docker must be installed and available in your `PATH`. Zed requires the `docker` command to be present. If you use Podman, you can alias it to `docker` (e.g., `alias docker=podman`). +- Your project must contain a `.devcontainer/devcontainer.json` directory/file. + +## Using Dev Containers in Zed + +### Automatic prompt + +When you open a project that contains the `.devcontainer/devcontainer.json` directory/file, Zed will display a prompt asking whether to open the project inside the dev container. Choosing "Open in Container" will: + +1. Build the dev container image (if needed). +2. Launch the container. +3. Reopen the project connected to the container environment. + +### Manual open + +If you dismiss the prompt or want to reopen the project inside a container later, you can use Zed's command palette to run the "Project: Open Remote" command and select the option to open the project in a dev container. +Alternatively, you can reach for the Remote Projects modal (through the {#kb projects::OpenRemote} binding) and choose the "Connect Dev Container" option. + +## Editing the dev container configuration + +If you modify `.devcontainer/devcontainer.json`, Zed does not currently rebuild or reload the container automatically. After changing configuration: + +- Stop or kill the existing container manually (e.g., via `docker kill `). +- Reopen the project in the container. + +## Working in a Dev Container + +Once connected, Zed operates inside the container environment for tasks, terminals, and language servers. +Files are linked from your workspace into the container according to the dev container specification. + +## Known Limitations + +> **Note:** This feature is still in development. + +- **Extensions:** Zed does not yet manage extensions separately for container environments. The host's extensions are used as-is. +- **Port forwarding:** Only the `appPort` field is supported. `forwardPorts` and other advanced port-forwarding features are not implemented. +- **Configuration changes:** Updates to `devcontainer.json` do not trigger automatic rebuilds or reloads; containers must be manually restarted. + +## See also + +- [Remote Development](./remote-development.md) for connecting to remote servers over SSH. +- [Tasks](./tasks.md) for running commands in the integrated terminal. diff --git a/docs/src/development.md b/docs/src/development.md index 31bb245ac42f80c830a0faba405323d1097e3f51..8f341dbb1506d4a6fa6c3ffa21960191ec5ecfcf 100644 --- a/docs/src/development.md +++ b/docs/src/development.md @@ -6,10 +6,6 @@ See the platform-specific instructions for building Zed from source: - [Linux](./development/linux.md) - [Windows](./development/windows.md) -If you'd like to develop collaboration features, additionally see: - -- [Local Collaboration](./development/local-collaboration.md) - ## Keychain access Zed stores secrets in the system keychain. diff --git a/docs/src/development/glossary.md b/docs/src/development/glossary.md index 34172ec9a590fdae537ff78920e1fadda2c331fa..0e0f984e214fe1a46e0aff790ab5e85bb46a8674 100644 --- a/docs/src/development/glossary.md +++ b/docs/src/development/glossary.md @@ -73,7 +73,7 @@ h_flex() - `Window`: A struct in zed representing a zed window in your desktop environment (see image below). There can be multiple if you have multiple zed instances open. Mostly passed around for rendering. - `Modal`: A UI element that floats on top of the rest of the UI -- `Picker`: A struct representing a list of items in floating on top of the UI (Modal). You can select an item and confirm. What happens on select or confirm is determined by the picker's delegate. (The 'Model' in the image below is a picker.) +- `Picker`: A struct representing a list of items floating on top of the UI (Modal). You can select an item and confirm. What happens on select or confirm is determined by the picker's delegate. (The 'Modal' in the image below is a picker.) - `PickerDelegate`: A trait used to specialize behavior for a `Picker`. The `Picker` stores the `PickerDelegate` in the field delegate. - `Center`: The middle of the zed window, the center is split into multiple `Pane`s. In the codebase this is a field on the `Workspace` struct. (see image below). - `Pane`: An area in the `Center` where we can place items, such as an editor, multi-buffer or terminal (see image below). diff --git a/docs/src/development/linux.md b/docs/src/development/linux.md index df3b840fa17a547efd4324f3bdaa119b8ade8738..3269d4b4dd51b224ab2b0cf7cfe15333232d0915 100644 --- a/docs/src/development/linux.md +++ b/docs/src/development/linux.md @@ -16,10 +16,6 @@ Clone down the [Zed repository](https://github.com/zed-industries/zed). If you prefer to install the system libraries manually, you can find the list of required packages in the `script/linux` file. -### Backend Dependencies (optional) {#backend-dependencies} - -If you are looking to develop Zed collaboration features using a local collaboration server, please see: [Local Collaboration](./local-collaboration.md) docs. - ### Linkers {#linker} On Linux, Rust's default linker is [LLVM's `lld`](https://blog.rust-lang.org/2025/09/18/Rust-1.90.0/). Alternative linkers, especially [Wild](https://github.com/davidlattimore/wild) and [Mold](https://github.com/rui314/mold) can significantly improve clean and incremental build time. diff --git a/docs/src/development/local-collaboration.md b/docs/src/development/local-collaboration.md deleted file mode 100644 index 393c6f0bbf797cf9aa86d297633734444bdfb328..0000000000000000000000000000000000000000 --- a/docs/src/development/local-collaboration.md +++ /dev/null @@ -1,207 +0,0 @@ -# Local Collaboration - -1. Ensure you have access to our cloud infrastructure. If you don't have access, you can't collaborate locally at this time. - -2. Make sure you've installed Zed's dependencies for your platform: - -- [macOS](#macos) -- [Linux](#linux) -- [Windows](#backend-windows) - -Note that `collab` can be compiled only with MSVC toolchain on Windows - -3. Clone down our cloud repository and follow the instructions in the cloud README - -4. Setup the local database for your platform: - -- [macOS & Linux](#database-unix) -- [Windows](#database-windows) - -5. Run collab: - -- [macOS & Linux](#run-collab-unix) -- [Windows](#run-collab-windows) - -## Backend Dependencies - -If you are developing collaborative features of Zed, you'll need to install the dependencies of zed's `collab` server: - -- PostgreSQL -- LiveKit -- Foreman - -You can install these dependencies natively or run them under Docker. - -### macOS - -1. Install [Postgres.app](https://postgresapp.com) or [postgresql via homebrew](https://formulae.brew.sh/formula/postgresql@15): - - ```sh - brew install postgresql@15 - ``` - -2. Install [Livekit](https://formulae.brew.sh/formula/livekit) and [Foreman](https://formulae.brew.sh/formula/foreman) - - ```sh - brew install livekit foreman - ``` - -- Follow the steps in the [collab README](https://github.com/zed-industries/zed/blob/main/crates/collab/README.md) to configure the Postgres database for integration tests - -Alternatively, if you have [Docker](https://www.docker.com/) installed you can bring up all the `collab` dependencies using Docker Compose. - -### Linux - -1. Install [Postgres](https://www.postgresql.org/download/linux/) - - ```sh - sudo apt-get install postgresql # Ubuntu/Debian - sudo pacman -S postgresql # Arch Linux - sudo dnf install postgresql postgresql-server # RHEL/Fedora - sudo zypper install postgresql postgresql-server # OpenSUSE - ``` - -2. Install [Livekit](https://github.com/livekit/livekit-cli) - - ```sh - curl -sSL https://get.livekit.io/cli | bash - ``` - -3. Install [Foreman](https://theforeman.org/manuals/3.15/quickstart_guide.html) - -### Windows {#backend-windows} - -> This section is still in development. The instructions are not yet complete. - -- Install [Postgres](https://www.postgresql.org/download/windows/) -- Install [Livekit](https://github.com/livekit/livekit), optionally you can add the `livekit-server` binary to your `PATH`. - -Alternatively, if you have [Docker](https://www.docker.com/) installed you can bring up all the `collab` dependencies using Docker Compose. - -### Docker {#Docker} - -If you have docker or podman available, you can run the backend dependencies inside containers with Docker Compose: - -```sh -docker compose up -d -``` - -## Database setup - -Before you can run the `collab` server locally, you'll need to set up a `zed` Postgres database. - -### On macOS and Linux {#database-unix} - -```sh -script/bootstrap -``` - -This script will set up the `zed` Postgres database, and populate it with some users. It requires internet access, because it fetches some users from the GitHub API. - -The script will seed the database with various content defined by: - -```sh -cat crates/collab/seed.default.json -``` - -To use a different set of admin users, you can create your own version of that json file and export the `SEED_PATH` environment variable. Note that the usernames listed in the admins list currently must correspond to valid GitHub users. - -```json [settings] -{ - "admins": ["admin1", "admin2"], - "channels": ["zed"] -} -``` - -### On Windows {#database-windows} - -```powershell -.\script\bootstrap.ps1 -``` - -## Testing collaborative features locally - -### On macOS and Linux {#run-collab-unix} - -Ensure that Postgres is configured and running, then run Zed's collaboration server and the `livekit` dev server: - -```sh -foreman start -# OR -docker compose up -``` - -Alternatively, if you're not testing voice and screenshare, you can just run `collab` and `cloud`, and not the `livekit` dev server: - -```sh -cargo run -p collab -- serve all -``` - -```sh -cd ../cloud; cargo make dev -``` - -In a new terminal, run two or more instances of Zed. - -```sh -script/zed-local -3 -``` - -This script starts one to four instances of Zed, depending on the `-2`, `-3` or `-4` flags. Each instance will be connected to the local `collab` server, signed in as a different user from `.admins.json` or `.admins.default.json`. - -### On Windows {#run-collab-windows} - -Since `foreman` is not available on Windows, you can run the following commands in separate terminals: - -```powershell -cargo run --package=collab -- serve all -``` - -If you have added the `livekit-server` binary to your `PATH`, you can run: - -```powershell -livekit-server --dev -``` - -Otherwise, - -```powershell -.\path\to\livekit-serve.exe --dev -``` - -You'll also need to start the cloud server: - -```powershell -cd ..\cloud; cargo make dev -``` - -In a new terminal, run two or more instances of Zed. - -```powershell -node .\script\zed-local -2 -``` - -Note that this requires `node.exe` to be in your `PATH`. - -## Running a local collab server - -> [!NOTE] -> Because of recent changes to our authentication system, Zed will not be able to authenticate itself with, and therefore use, a local collab server. - -If you want to run your own version of the zed collaboration service, you can, but note that this is still under development, and there is no support for authentication nor extensions. - -Configuration is done through environment variables. By default it will read the configuration from [`.env.toml`](https://github.com/zed-industries/zed/blob/main/crates/collab/.env.toml) and you should use that as a guide for setting this up. - -By default Zed assumes that the DATABASE_URL is a Postgres database, but you can make it use Sqlite by compiling with `--features sqlite` and using a sqlite DATABASE_URL with `?mode=rwc`. - -To authenticate you must first configure the server by creating a seed.json file that contains at a minimum your github handle. This will be used to create the user on demand. - -```json [settings] -{ - "admins": ["nathansobo"] -} -``` - -By default the collab server will seed the database when first creating it, but if you want to add more users you can explicitly reseed them with `SEED_PATH=./seed.json cargo run -p collab seed` - -Then when running the zed client you must specify two environment variables, `ZED_ADMIN_API_TOKEN` (which should match the value of `API_TOKEN` in .env.toml) and `ZED_IMPERSONATE` (which should match one of the users in your seed.json) diff --git a/docs/src/development/macos.md b/docs/src/development/macos.md index 9c99e5f8da62594c774e109e15f914788f51793d..9e2908dd6e393acd8d3903d86743dcbc4e9ae9eb 100644 --- a/docs/src/development/macos.md +++ b/docs/src/development/macos.md @@ -31,10 +31,6 @@ Clone down the [Zed repository](https://github.com/zed-industries/zed). brew install cmake ``` -### Backend Dependencies (optional) {#backend-dependencies} - -If you are looking to develop Zed collaboration features using a local collaboration server, please see: [Local Collaboration](./local-collaboration.md) docs. - ## Building Zed from Source Once you have the dependencies installed, you can build Zed using [Cargo](https://doc.rust-lang.org/cargo/). diff --git a/docs/src/development/windows.md b/docs/src/development/windows.md index 17382e0bee5b97c2ffc2d74794cf3881a3cb98a1..509f30a05b45175f7e66026aec5b5d433b928e4d 100644 --- a/docs/src/development/windows.md +++ b/docs/src/development/windows.md @@ -66,10 +66,6 @@ The list can be obtained as follows: - Click on `More` in the `Installed` tab - Click on `Export configuration` -### Backend Dependencies (optional) {#backend-dependencies} - -If you are looking to develop Zed collaboration features using a local collaboration server, please see: [Local Collaboration](./local-collaboration.md) docs. - ### Notes You should modify the `pg_hba.conf` file in the `data` directory to use `trust` instead of `scram-sha-256` for the `host` method. Otherwise, the connection will fail with the error `password authentication failed`. The `pg_hba.conf` file typically locates at `C:\Program Files\PostgreSQL\17\data\pg_hba.conf`. After the modification, the file should look like this: diff --git a/docs/src/languages/javascript.md b/docs/src/languages/javascript.md index 1b87dac5553f0dc44153d4706be1dd4bd2e341d5..f043c642b305a8dba2b0985a75954438bb024c4c 100644 --- a/docs/src/languages/javascript.md +++ b/docs/src/languages/javascript.md @@ -175,6 +175,34 @@ You can configure ESLint's `workingDirectory` setting: } ``` +## Using the Tailwind CSS Language Server with JavaScript + +To get all the features (autocomplete, linting, etc.) from the [Tailwind CSS language server](https://github.com/tailwindlabs/tailwindcss-intellisense/tree/HEAD/packages/tailwindcss-language-server#readme) in vanilla JavaScript files (`.js`), you can customize the `classRegex` field under it in your `settings.json`: + +```json [settings] +{ + "lsp": { + "tailwindcss-language-server": { + "settings": { + "experimental": { + "classRegex": [ + "\\.className\\s*[+]?=\\s*['\"]([^'\"]*)['\"]", + "\\.setAttributeNS\\(.*,\\s*['\"]class['\"],\\s*['\"]([^'\"]*)['\"]", + "\\.setAttribute\\(['\"]class['\"],\\s*['\"]([^'\"]*)['\"]", + "\\.classList\\.add\\(['\"]([^'\"]*)['\"]", + "\\.classList\\.remove\\(['\"]([^'\"]*)['\"]", + "\\.classList\\.toggle\\(['\"]([^'\"]*)['\"]", + "\\.classList\\.contains\\(['\"]([^'\"]*)['\"]", + "\\.classList\\.replace\\(\\s*['\"]([^'\"]*)['\"]", + "\\.classList\\.replace\\([^,)]+,\\s*['\"]([^'\"]*)['\"]" + ] + } + } + } + } +} +``` + ## Debugging Zed supports debugging JavaScript code out of the box with `vscode-js-debug`. @@ -186,7 +214,7 @@ The following can be debugged without writing additional configuration: Run {#action debugger::Start} ({#kb debugger::Start}) to see a contextual list of these predefined debug tasks. > **Note:** Bun test is automatically detected when `@types/bun` is present in `package.json`. -> + > **Note:** Node test is automatically detected when `@types/node` is present in `package.json` (requires Node.js 20+). As for all languages, configurations from `.vscode/launch.json` are also available for debugging in Zed. diff --git a/docs/src/languages/markdown.md b/docs/src/languages/markdown.md index 36ce734f7cfbcc066bb8026568209738655a6be9..64c9e7070569a23daa5bcb8aa4dace12e0021b03 100644 --- a/docs/src/languages/markdown.md +++ b/docs/src/languages/markdown.md @@ -33,6 +33,40 @@ Zed supports using Prettier to automatically re-format Markdown documents. You c }, ``` +### List Continuation + +Zed automatically continues lists when you press Enter at the end of a list item. Supported list types: + +- Unordered lists (`-`, `*`, or `+` markers) +- Ordered lists (numbers are auto-incremented) +- Task lists (`- [ ]` and `- [x]`) + +Pressing Enter on an empty list item removes the marker and exits the list. + +To disable this behavior: + +```json [settings] + "languages": { + "Markdown": { + "extend_list_on_newline": false + } + }, +``` + +### List Indentation + +Zed indents list items when you press Tab while the cursor is on a line containing only a list marker. This allows you to quickly create nested lists. + +To disable this behavior: + +```json [settings] + "languages": { + "Markdown": { + "indent_list_on_tab": false + } + }, +``` + ### Trailing Whitespace By default Zed will remove trailing whitespace on save. If you rely on invisible trailing whitespace being converted to `
` in Markdown files you can disable this behavior with: diff --git a/docs/src/languages/ruby.md b/docs/src/languages/ruby.md index 7e072ac5d32ab990584a2c2b0be57eb3076b1ec9..f7f0ccce83354fb24372f6916f27c63156f8cb3c 100644 --- a/docs/src/languages/ruby.md +++ b/docs/src/languages/ruby.md @@ -258,17 +258,10 @@ To enable Steep, add `\"steep\"` to the `language_servers` list for Ruby in your ## Using the Tailwind CSS Language Server with Ruby -It's possible to use the [Tailwind CSS Language Server](https://github.com/tailwindlabs/tailwindcss-intellisense/tree/HEAD/packages/tailwindcss-language-server#readme) in Ruby and ERB files. - -In order to do that, you need to configure the language server so that it knows about where to look for CSS classes in Ruby/ERB files by adding the following to your `settings.json`: +To get all the features (autocomplete, linting, etc.) from the [Tailwind CSS language server](https://github.com/tailwindlabs/tailwindcss-intellisense/tree/HEAD/packages/tailwindcss-language-server#readme) in Ruby/ERB files, you need to configure the language server so that it knows about where to look for CSS classes by adding the following to your `settings.json`: ```json [settings] { - "languages": { - "Ruby": { - "language_servers": ["tailwindcss-language-server", "..."] - } - }, "lsp": { "tailwindcss-language-server": { "settings": { @@ -281,7 +274,7 @@ In order to do that, you need to configure the language server so that it knows } ``` -With these settings you will get completions for Tailwind CSS classes in HTML attributes inside ERB files and inside Ruby/ERB strings that are coming after a `class:` key. Examples: +With these settings, you will get completions for Tailwind CSS classes in HTML attributes inside ERB files and inside Ruby/ERB strings that are coming after a `class:` key. Examples: ```rb # Ruby file: diff --git a/docs/src/languages/tailwindcss.md b/docs/src/languages/tailwindcss.md index be9c9437d1382dfd356120663ebea2c1fe012684..457c71f9768610f5bfdf345e72c27311632f1bef 100644 --- a/docs/src/languages/tailwindcss.md +++ b/docs/src/languages/tailwindcss.md @@ -4,9 +4,23 @@ Zed has built-in support for Tailwind CSS autocomplete, linting, and hover previ - Language Server: [tailwindlabs/tailwindcss-intellisense](https://github.com/tailwindlabs/tailwindcss-intellisense) +Languages which can be used with Tailwind CSS in Zed: + +- [Astro](./astro.md) +- [CSS](./css.md) +- [ERB](./ruby.md) +- [Gleam](./gleam.md) +- [HEEx](./elixir.md#heex) +- [HTML](./html.md) +- [TypeScript](./typescript.md) +- [JavaScript](./javascript.md) +- [PHP](./php.md) +- [Svelte](./svelte.md) +- [Vue](./vue.md) + ## Configuration -To configure the Tailwind CSS language server, refer [to the extension settings](https://github.com/tailwindlabs/tailwindcss-intellisense?tab=readme-ov-file#extension-settings) and add them to the `lsp` section of your `settings.json`: +If by default the language server isn't enough to make Tailwind work for a given language, you can configure the language server settings and add them to the `lsp` section of your `settings.json`: ```json [settings] { @@ -23,19 +37,7 @@ To configure the Tailwind CSS language server, refer [to the extension settings] } ``` -Languages which can be used with Tailwind CSS in Zed: - -- [Astro](./astro.md) -- [CSS](./css.md) -- [ERB](./ruby.md) -- [Gleam](./gleam.md) -- [HEEx](./elixir.md#heex) -- [HTML](./html.md) -- [TypeScript](./typescript.md) -- [JavaScript](./javascript.md) -- [PHP](./php.md) -- [Svelte](./svelte.md) -- [Vue](./vue.md) +Refer to [the Tailwind CSS language server settings docs](https://github.com/tailwindlabs/tailwindcss-intellisense?tab=readme-ov-file#extension-settings) for more information. ### Prettier Plugin diff --git a/docs/src/languages/typescript.md b/docs/src/languages/typescript.md index a6ec5b71ecb1815aeb4ff3811eec6f9a5c57a54b..d4fccc38f8a460e9ec097dee249a6441bd34a344 100644 --- a/docs/src/languages/typescript.md +++ b/docs/src/languages/typescript.md @@ -45,6 +45,34 @@ Prettier will also be used for TypeScript files by default. To disable this: } ``` +## Using the Tailwind CSS Language Server with TypeScript + +To get all the features (autocomplete, linting, etc.) from the [Tailwind CSS language server](https://github.com/tailwindlabs/tailwindcss-intellisense/tree/HEAD/packages/tailwindcss-language-server#readme) in vanilla TypeScript files (`.ts`), you can customize the `classRegex` field under it in your `settings.json`: + +```json [settings] +{ + "lsp": { + "tailwindcss-language-server": { + "settings": { + "experimental": { + "classRegex": [ + "\\.className\\s*[+]?=\\s*['\"]([^'\"]*)['\"]", + "\\.setAttributeNS\\(.*,\\s*['\"]class['\"],\\s*['\"]([^'\"]*)['\"]", + "\\.setAttribute\\(['\"]class['\"],\\s*['\"]([^'\"]*)['\"]", + "\\.classList\\.add\\(['\"]([^'\"]*)['\"]", + "\\.classList\\.remove\\(['\"]([^'\"]*)['\"]", + "\\.classList\\.toggle\\(['\"]([^'\"]*)['\"]", + "\\.classList\\.contains\\(['\"]([^'\"]*)['\"]", + "\\.classList\\.replace\\(\\s*['\"]([^'\"]*)['\"]", + "\\.classList\\.replace\\([^,)]+,\\s*['\"]([^'\"]*)['\"]" + ] + } + } + } + } +} +``` + ## Large projects `vtsls` may run out of memory on very large projects. We default the limit to 8092 (8 GiB) vs. the default of 3072 but this may not be sufficient for you: @@ -167,7 +195,7 @@ The following can be debugged without writing additional configuration: Run {#action debugger::Start} ({#kb debugger::Start}) to see a contextual list of these predefined debug tasks. > **Note:** Bun test is automatically detected when `@types/bun` is present in `package.json`. -> + > **Note:** Node test is automatically detected when `@types/node` is present in `package.json` (requires Node.js 20+). As for all languages, configurations from `.vscode/launch.json` are also available for debugging in Zed. diff --git a/docs/src/migrate/_research-notes.md b/docs/src/migrate/_research-notes.md new file mode 100644 index 0000000000000000000000000000000000000000..e23a3d3529a9762368e1721f97a6720382cd764b --- /dev/null +++ b/docs/src/migrate/_research-notes.md @@ -0,0 +1,73 @@ + + +# Migration Research Notes + +## Completed Guides + +All three JetBrains migration guides have been populated with full content: + +1. **pycharm.md** - Python development, virtual environments, Ruff/Pyright, Django/Flask workflows +2. **webstorm.md** - JavaScript/TypeScript development, npm workflows, framework considerations +3. **rustrover.md** - Rust development, rust-analyzer parity, Cargo workflows, licensing notes + +## Key Sources Used + +- IntelliJ IDEA migration doc (structural template) +- JetBrains PyCharm Getting Started docs +- JetBrains WebStorm Getting Started docs +- JetBrains RustRover Quick Start Guide +- External community feedback (Reddit, Hacker News, Medium) + +## External Quotes Incorporated + +### WebStorm Guide + +> "I work for AWS and the applications I deal with are massive. Often I need to keep many projects open due to tight dependencies. I'm talking about complex microservices and micro frontend infrastructure which oftentimes lead to 2-15 minutes of indexing wait time whenever I open a project or build the system locally." + +### RustRover Guide + +- Noted rust-analyzer shared foundation between RustRover and Zed +- Addressed licensing/telemetry concerns that motivate some users to switch +- Included debugger caveats based on community feedback + +## Cross-Cutting Themes Applied to All Guides + +### Universal Pain Points Addressed + +1. Indexing (instant in Zed) +2. Resource usage (Zed is lightweight) +3. Startup time (Zed is near-instant) +4. UI clutter (Zed is minimal by design) + +### Universal Missing Features Documented + +- No project model / SDK management +- No database tools +- No framework-specific integration +- No visual run configurations (use tasks) +- No built-in HTTP client + +### JetBrains Keymap Emphasized + +All three guides emphasize: + +- Select JetBrains keymap during onboarding or in settings +- `Shift Shift` for Search Everywhere works +- Most familiar shortcuts preserved + +## Next Steps (Optional Enhancements) + +- [ ] Cross-link guides to JetBrains docs for users who want to reference original IDE features +- [ ] Add a consolidated "hub page" linking to all migration guides +- [ ] Consider adding VS Code migration guide using similar structure +- [ ] Review for tone consistency against Zed Documentation Guidelines diff --git a/docs/src/migrate/intellij.md b/docs/src/migrate/intellij.md index d931fde4f1c2cd98db6b154d7009feff6fcb6a5b..24c85774ec5686f605d1d781913d0873ac0abd7f 100644 --- a/docs/src/migrate/intellij.md +++ b/docs/src/migrate/intellij.md @@ -33,7 +33,7 @@ This opens the current directory in Zed. If you're coming from IntelliJ, the fastest way to feel at home is to use the JetBrains keymap. During onboarding, you can select it as your base keymap. If you missed that step, you can change it anytime: 1. Open Settings with `Cmd+,` (macOS) or `Ctrl+,` (Linux/Windows) -2. Search for `base_keymap` +2. Search for `Base Keymap` 3. Select `JetBrains` Or add this directly to your `settings.json`: @@ -147,7 +147,7 @@ If you've used IntelliJ on large projects, you know the wait: "Indexing..." can Zed doesn't index. You open a folder and start working immediately. File search and navigation work instantly regardless of project size. -The trade-off is real: IntelliJ's index powers features like finding all usages across your entire codebase, understanding class hierarchies, and detecting dead code. Zed delegates this work to language servers, which may not analyze as deeply or as broadly. +IntelliJ's index powers features like finding all usages across your entire codebase, understanding class hierarchies, and detecting dead code. Zed delegates this work to language servers, which may not analyze at the same depth. **How to adapt:** @@ -158,7 +158,7 @@ The trade-off is real: IntelliJ's index powers features like finding all usages ### LSP vs. Native Language Intelligence -IntelliJ has its own language analysis engine built from scratch for each supported language. For Java, Kotlin, and other JVM languages, this engine understands your code deeply: it resolves types, tracks data flow, knows about framework annotations, and offers dozens of specialized refactorings. +IntelliJ has its own language analysis engine built from scratch for each supported language. For Java, Kotlin, and other JVM languages, this engine understands your code thoroughly: it resolves types, tracks data flow, knows about framework annotations, and offers dozens of specialized refactorings. Zed uses the Language Server Protocol (LSP) for code intelligence. Each language has its own server: `jdtls` for Java, `rust-analyzer` for Rust, and so on. diff --git a/docs/src/migrate/pycharm.md b/docs/src/migrate/pycharm.md new file mode 100644 index 0000000000000000000000000000000000000000..636bc69eeba1c09b3e0e8a0d74ccd859aedbb342 --- /dev/null +++ b/docs/src/migrate/pycharm.md @@ -0,0 +1,438 @@ +# How to Migrate from PyCharm to Zed + +This guide covers how to set up Zed if you're coming from PyCharm, including keybindings, settings, and the differences you should expect. + +## Install Zed + +Zed is available on macOS, Windows, and Linux. + +For macOS, you can download it from zed.dev/download, or install via Homebrew: + +```sh +brew install --cask zed +``` + +For Windows, download the installer from zed.dev/download, or install via winget: + +```sh +winget install Zed.Zed +``` + +For most Linux users, the easiest way to install Zed is through our installation script: + +```sh +curl -f https://zed.dev/install.sh | sh +``` + +After installation, you can launch Zed from your Applications folder (macOS), Start menu (Windows), or directly from the terminal using: +`zed .` +This opens the current directory in Zed. + +## Set Up the JetBrains Keymap + +If you're coming from PyCharm, the fastest way to feel at home is to use the JetBrains keymap. During onboarding, you can select it as your base keymap. If you missed that step, you can change it anytime: + +1. Open Settings with `Cmd+,` (macOS) or `Ctrl+,` (Linux/Windows) +2. Search for `Base Keymap` +3. Select `JetBrains` + +Or add this directly to your `settings.json`: + +```json +{ + "base_keymap": "JetBrains" +} +``` + +This maps familiar shortcuts like `Shift Shift` for Search Everywhere, `Cmd+O` for Go to Class, and `Cmd+Shift+A` for Find Action. + +## Set Up Editor Preferences + +You can configure settings manually in the Settings Editor. + +To edit your settings: + +1. `Cmd+,` to open the Settings Editor. +2. Run `zed: open settings` in the Command Palette. + +Settings PyCharm users typically configure first: + +| Zed Setting | What it does | +| ----------------------- | ------------------------------------------------------------------------------- | +| `format_on_save` | Auto-format when saving. Set to `"on"` to enable. | +| `soft_wrap` | Wrap long lines. Options: `"none"`, `"editor_width"`, `"preferred_line_length"` | +| `preferred_line_length` | Column width for wrapping and rulers. Default is 80, PEP 8 recommends 79. | +| `inlay_hints` | Show parameter names and type hints inline, like PyCharm's hints. | +| `relative_line_numbers` | Useful if you're coming from IdeaVim. | + +Zed also supports per-project settings. Create a `.zed/settings.json` file in your project root to override global settings for that project, similar to how you might use `.idea` folders in PyCharm. + +> **Tip:** If you're joining an existing project, check `format_on_save` before making your first commit. Otherwise you might accidentally reformat an entire file when you only meant to change one line. + +## Open or Create a Project + +After setup, press `Cmd+Shift+O` (with JetBrains keymap) to open a folder. This becomes your workspace in Zed. Unlike PyCharm, there's no project configuration wizard, no interpreter selection dialog, and no project structure setup required. + +To start a new project, create a directory using your terminal or file manager, then open it in Zed. The editor will treat that folder as the root of your project. + +You can also launch Zed from the terminal inside any folder with: +`zed .` + +Once inside a project: + +- Use `Cmd+Shift+O` or `Cmd+E` to jump between files quickly (like PyCharm's "Recent Files") +- Use `Cmd+Shift+A` or `Shift Shift` to open the Command Palette (like PyCharm's "Search Everywhere") +- Use `Cmd+O` to search for symbols (like PyCharm's "Go to Symbol") + +Open buffers appear as tabs across the top. The sidebar shows your file tree and Git status. Toggle it with `Cmd+1` (just like PyCharm's Project tool window). + +## Differences in Keybindings + +If you chose the JetBrains keymap during onboarding, most of your shortcuts should already feel familiar. Here's a quick reference for how Zed compares to PyCharm. + +### Common Shared Keybindings + +| Action | Shortcut | +| ----------------------------- | ----------------------- | +| Search Everywhere | `Shift Shift` | +| Find Action / Command Palette | `Cmd + Shift + A` | +| Go to File | `Cmd + Shift + O` | +| Go to Symbol | `Cmd + O` | +| Recent Files | `Cmd + E` | +| Go to Definition | `Cmd + B` | +| Find Usages | `Alt + F7` | +| Rename Symbol | `Shift + F6` | +| Reformat Code | `Cmd + Alt + L` | +| Toggle Project Panel | `Cmd + 1` | +| Toggle Terminal | `Alt + F12` | +| Duplicate Line | `Cmd + D` | +| Delete Line | `Cmd + Backspace` | +| Move Line Up/Down | `Shift + Alt + Up/Down` | +| Expand/Shrink Selection | `Alt + Up/Down` | +| Comment Line | `Cmd + /` | +| Go Back / Forward | `Cmd + [` / `Cmd + ]` | +| Toggle Breakpoint | `Ctrl + F8` | + +### Different Keybindings (PyCharm → Zed) + +| Action | PyCharm | Zed (JetBrains keymap) | +| ---------------------- | ----------- | ------------------------ | +| File Structure | `Cmd + F12` | `Cmd + F12` (outline) | +| Navigate to Next Error | `F2` | `F2` | +| Run | `Ctrl + R` | `Ctrl + Alt + R` (tasks) | +| Debug | `Ctrl + D` | `Alt + Shift + F9` | +| Stop | `Cmd + F2` | `Ctrl + F2` | + +### Unique to Zed + +| Action | Shortcut | Notes | +| ----------------- | -------------------------- | ------------------------------ | +| Toggle Right Dock | `Cmd + R` | Assistant panel, notifications | +| Split Panes | `Cmd + K`, then arrow keys | Create splits in any direction | + +### How to Customize Keybindings + +- Open the Command Palette (`Cmd+Shift+A` or `Shift Shift`) +- Run `Zed: Open Keymap Editor` + +This opens a list of all available bindings. You can override individual shortcuts or remove conflicts. + +Zed also supports key sequences (multi-key shortcuts). + +## Differences in User Interfaces + +### No Indexing + +If you've used PyCharm on large projects, you know the wait: "Indexing..." can take anywhere from 30 seconds to several minutes depending on project size and dependencies. PyCharm builds a comprehensive index of your entire codebase to power its code intelligence, and it re-indexes when dependencies change or when you install new packages. + +Zed doesn't index. You open a folder and start working immediately. File search and navigation work instantly regardless of project size. For many PyCharm users, this alone is reason enough to switch—no more waiting, no more "Indexing paused" interruptions. + +PyCharm's index powers features like finding all usages across your entire codebase, understanding class hierarchies, and detecting unused imports project-wide. Zed delegates this work to language servers, which may not analyze as deeply or as broadly. + +**How to adapt:** + +- For project-wide symbol search, use `Cmd+O` / Go to Symbol (relies on your language server) +- For finding files by name, use `Cmd+Shift+O` / Go to File +- For text search across files, use `Cmd+Shift+F`—this is fast even on large codebases +- For deep static analysis, consider running tools like `mypy`, `pylint`, or `ruff check` from the terminal + +### LSP vs. Native Language Intelligence + +PyCharm has its own language analysis engine built specifically for Python. This engine understands your code deeply: it resolves types without annotations, tracks data flow, knows about Django models and Flask routes, and offers specialized refactorings. + +Zed uses the Language Server Protocol (LSP) for code intelligence. For Python, Zed provides several language servers out of the box: + +- **basedpyright** (default) — Fast type checking and completions +- **Ruff** (default) — Linting and formatting +- **Ty** — Up-and-coming language server from Astral, built for speed +- **Pyright** — Microsoft's type checker +- **PyLSP** — Plugin-based server with tool integrations + +The LSP experience for Python is strong. basedpyright provides accurate completions, type checking, and navigation. Ruff handles formatting and linting with excellent performance. + +Where you might notice differences: + +- Framework-specific intelligence (Django ORM, Flask routes) isn't built-in +- Some complex refactorings (extract method with proper scope analysis) may be less sophisticated +- Auto-import suggestions depend on what the language server knows about your environment + +**How to adapt:** + +- Use `Alt+Enter` for available code actions—the list will vary by language server +- Ensure your virtual environment is selected so the language server can resolve your dependencies +- Use Ruff for fast, consistent formatting (it's enabled by default) +- For code inspection similar to PyCharm's "Inspect Code," run `ruff check .` or check the Diagnostics panel (`Cmd+6`)—basedpyright and Ruff together catch many of the same issues + +### Virtual Environments and Interpreters + +In PyCharm, you select a Python interpreter through a GUI, and PyCharm manages the connection between your project and that interpreter. It shows available packages, lets you install new ones, and keeps track of which environment each project uses. + +Zed handles virtual environments through its toolchain system: + +- Zed automatically discovers virtual environments in common locations (`.venv`, `venv`, `.env`, `env`) +- When a virtual environment is detected, the terminal auto-activates it +- Language servers are automatically configured to use the discovered environment +- You can manually select a toolchain if auto-detection picks the wrong one + +**How to adapt:** + +- Create your virtual environment with `python -m venv .venv` or `uv sync` +- Open the folder in Zed—it will detect the environment automatically +- If you need to switch environments, use the toolchain selector +- For conda environments, ensure they're activated in your shell before launching Zed + +> **Tip:** If basedpyright shows import errors for packages you've installed, check that Zed has selected the correct virtual environment. Use the toolchain selector to verify or change the active environment. + +### No Project Model + +PyCharm manages projects through `.idea` folders containing XML configuration files, interpreter assignments, and run configurations. This model lets PyCharm remember your interpreter choice, manage dependencies through the UI, and persist complex run/debug setups. + +Zed has no project model. A project is a folder. There's no wizard, no interpreter selection screen, no project structure configuration. + +This means: + +- Run configurations don't exist. You define tasks or use the terminal. Your existing PyCharm run configs in `.idea/` won't be read—you'll recreate the ones you need in `tasks.json`. +- Interpreter management is external. Zed discovers environments but doesn't create them. +- Dependencies are managed through pip, uv, poetry, or conda—not through the editor. +- There's no Python Console (interactive REPL) panel. Use `python` or `ipython` in the terminal instead. + +**How to adapt:** + +- Create a `.zed/settings.json` in your project root for project-specific settings +- Define common commands in `tasks.json` (open via Command Palette: `zed: open tasks`): + +```json +[ + { + "label": "run", + "command": "python main.py" + }, + { + "label": "test", + "command": "pytest" + }, + { + "label": "test current file", + "command": "pytest $ZED_FILE" + } +] +``` + +- Use `Ctrl+Alt+R` to run tasks quickly +- Lean on your terminal (`Alt+F12`) for anything tasks don't cover + +### No Framework Integration + +PyCharm Professional's value for web development comes largely from its framework integration. Django templates are understood and navigable. Flask routes are indexed. SQLAlchemy models get special treatment. Template variables autocomplete. + +Zed has none of this. The language server sees Python code as Python code—it doesn't understand that `@app.route` defines an endpoint or that a Django model class creates database tables. + +**How to adapt:** + +- Use grep and file search liberally. `Cmd+Shift+F` with a regex can find route definitions, model classes, or template usages. +- Rely on your language server's "find references" (`Alt+F7`) for navigation—it works, just without framework context +- Consider using framework-specific CLI tools (`python manage.py`, `flask routes`) from Zed's terminal + +> **Tip:** For database work, pick up a dedicated tool like DataGrip, DBeaver, or TablePlus. Many developers who switch to Zed keep DataGrip around specifically for SQL. + +### Tool Windows vs. Docks + +PyCharm organizes auxiliary views into numbered tool windows (Project = 1, Python Console = 4, Terminal = Alt+F12, etc.). Zed uses a similar concept called "docks": + +| PyCharm Tool Window | Zed Equivalent | Shortcut (JetBrains keymap) | +| ------------------- | -------------- | --------------------------- | +| Project (1) | Project Panel | `Cmd + 1` | +| Git (9 or Cmd+0) | Git Panel | `Cmd + 0` | +| Terminal (Alt+F12) | Terminal Panel | `Alt + F12` | +| Structure (7) | Outline Panel | `Cmd + 7` | +| Problems (6) | Diagnostics | `Cmd + 6` | +| Debug (5) | Debug Panel | `Cmd + 5` | + +Zed has three dock positions: left, bottom, and right. Panels can be moved between docks by dragging or through settings. + +### Debugging + +Both PyCharm and Zed offer integrated debugging, but the experience differs: + +- Zed uses `debugpy` (the same debug adapter that VS Code uses) +- Set breakpoints with `Ctrl+F8` +- Start debugging with `Alt+Shift+F9` or press `F4` and select a debug target +- Step through code with `F7` (step into), `F8` (step over), `Shift+F8` (step out) +- Continue execution with `F9` + +Zed can automatically detect debuggable entry points. Press `F4` to see available options, including: + +- Python scripts +- Modules +- pytest tests + +For more control, create a `.zed/debug.json` file: + +```json +[ + { + "label": "Debug Current File", + "adapter": "Debugpy", + "program": "$ZED_FILE", + "request": "launch" + }, + { + "label": "Debug Flask App", + "adapter": "Debugpy", + "request": "launch", + "module": "flask", + "args": ["run", "--debug"], + "env": { + "FLASK_APP": "app.py" + } + } +] +``` + +### Running Tests + +PyCharm has a dedicated test runner with a visual interface showing pass/fail status for each test. Zed provides test running through: + +- **Gutter icons** — Click the play button next to test functions or classes +- **Tasks** — Define pytest or unittest commands in `tasks.json` +- **Terminal** — Run `pytest` directly + +The test output appears in the terminal panel. For pytest, use `--tb=short` for concise tracebacks or `-v` for verbose output. + +### Extensions vs. Plugins + +PyCharm has a plugin ecosystem covering everything from additional language support to database tools to deployment integrations. + +Zed's extension ecosystem is smaller and more focused: + +- Language support and syntax highlighting +- Themes +- Slash commands for AI +- Context servers + +Several features that require plugins in PyCharm are built into Zed: + +- Real-time collaboration with voice chat +- AI coding assistance +- Built-in terminal +- Task runner +- LSP-based code intelligence +- Ruff formatting and linting + +### What's Not in Zed + +To set expectations clearly, here's what PyCharm offers that Zed doesn't have: + +- **Scientific Mode / Jupyter integration** — For notebooks and data science workflows, use JupyterLab or VS Code with the Jupyter extension alongside Zed for your Python editing +- **Database tools** — Use DataGrip, DBeaver, or TablePlus +- **Django/Flask template navigation** — Use file search and grep +- **Visual package manager** — Use pip, uv, or poetry from the terminal +- **Remote interpreters** — Zed has remote development, but it works differently +- **Profiler integration** — Use cProfile, py-spy, or similar tools externally + +## Collaboration in Zed vs. PyCharm + +PyCharm offers Code With Me as a separate plugin for collaboration. Zed has collaboration built into the core experience. + +- Open the Collab Panel in the left dock +- Create a channel and [invite your collaborators](https://zed.dev/docs/collaboration#inviting-a-collaborator) to join +- [Share your screen or your codebase](https://zed.dev/docs/collaboration#share-a-project) directly + +Once connected, you'll see each other's cursors, selections, and edits in real time. Voice chat is included. There's no need for separate tools or third-party logins. + +## Using AI in Zed + +If you're used to AI assistants in PyCharm (like GitHub Copilot or JetBrains AI Assistant), Zed offers similar capabilities with more flexibility. + +### Configuring GitHub Copilot + +1. Open Settings with `Cmd+,` (macOS) or `Ctrl+,` (Linux/Windows) +2. Navigate to **AI → Edit Predictions** +3. Click **Configure** next to "Configure Providers" +4. Under **GitHub Copilot**, click **Sign in to GitHub** + +Once signed in, just start typing. Zed will offer suggestions inline for you to accept. + +### Additional AI Options + +To use other AI models in Zed, you have several options: + +- Use Zed's hosted models, with higher rate limits. Requires [authentication](https://zed.dev/docs/accounts.html) and subscription to [Zed Pro](https://zed.dev/docs/ai/subscription.html). +- Bring your own [API keys](https://zed.dev/docs/ai/llm-providers.html), no authentication needed +- Use [external agents like Claude Code](https://zed.dev/docs/ai/external-agents.html) + +## Advanced Config and Productivity Tweaks + +Zed exposes advanced settings for power users who want to fine-tune their environment. + +Here are a few useful tweaks: + +**Format on Save:** + +```json +"format_on_save": "on" +``` + +**Enable direnv support (useful for Python projects using direnv):** + +```json +"load_direnv": "shell_hook" +``` + +**Customize virtual environment detection:** + +```json +{ + "terminal": { + "detect_venv": { + "on": { + "directories": [".venv", "venv", ".env", "env"], + "activate_script": "default" + } + } + } +} +``` + +**Configure basedpyright type checking strictness:** + +If you find basedpyright too strict or too lenient, configure it in your project's `pyrightconfig.json`: + +```json +{ + "typeCheckingMode": "basic" +} +``` + +Options are `"off"`, `"basic"`, `"standard"` (default), `"strict"`, or `"all"`. + +## Next Steps + +Now that you're set up, here are some resources to help you get the most out of Zed: + +- [Configuring Zed](../configuring-zed.md) — Customize settings, themes, and editor behavior +- [Key Bindings](../key-bindings.md) — Learn how to customize and extend your keymap +- [Tasks](../tasks.md) — Set up build and run commands for your projects +- [AI Features](../ai/overview.md) — Explore Zed's AI capabilities beyond code completion +- [Collaboration](../collaboration/overview.md) — Share your projects and code together in real time +- [Python in Zed](../languages/python.md) — Python-specific setup and configuration diff --git a/docs/src/migrate/rustrover.md b/docs/src/migrate/rustrover.md new file mode 100644 index 0000000000000000000000000000000000000000..4d0e85cfe9b981243044290929070e87876987d3 --- /dev/null +++ b/docs/src/migrate/rustrover.md @@ -0,0 +1,501 @@ +# How to Migrate from RustRover to Zed + +This guide covers how to set up Zed if you're coming from RustRover, including keybindings, settings, and the differences you should expect as a Rust developer. + +## Install Zed + +Zed is available on macOS, Windows, and Linux. + +For macOS, you can download it from zed.dev/download, or install via Homebrew: + +```sh +brew install --cask zed +``` + +For Windows, download the installer from zed.dev/download, or install via winget: + +```sh +winget install Zed.Zed +``` + +For most Linux users, the easiest way to install Zed is through our installation script: + +```sh +curl -f https://zed.dev/install.sh | sh +``` + +After installation, you can launch Zed from your Applications folder (macOS), Start menu (Windows), or directly from the terminal using: +`zed .` +This opens the current directory in Zed. + +## Set Up the JetBrains Keymap + +If you're coming from RustRover, the fastest way to feel at home is to use the JetBrains keymap. During onboarding, you can select it as your base keymap. If you missed that step, you can change it anytime: + +1. Open Settings with `Cmd+,` (macOS) or `Ctrl+,` (Linux/Windows) +2. Search for `Base Keymap` +3. Select `JetBrains` + +Or add this directly to your `settings.json`: + +```json +{ + "base_keymap": "JetBrains" +} +``` + +This maps familiar shortcuts like `Shift Shift` for Search Everywhere, `Cmd+O` for Go to Class, and `Cmd+Shift+A` for Find Action. + +## Set Up Editor Preferences + +You can configure settings manually in the Settings Editor. + +To edit your settings: + +1. `Cmd+,` to open the Settings Editor. +2. Run `zed: open settings` in the Command Palette. + +Settings RustRover users typically configure first: + +| Zed Setting | What it does | +| ----------------------- | ------------------------------------------------------------------------------- | +| `format_on_save` | Auto-format when saving. Set to `"on"` to enable (uses rustfmt by default). | +| `soft_wrap` | Wrap long lines. Options: `"none"`, `"editor_width"`, `"preferred_line_length"` | +| `preferred_line_length` | Column width for wrapping and rulers. Rust convention is 100. | +| `inlay_hints` | Show type hints, parameter names, and chaining hints inline. | +| `relative_line_numbers` | Useful if you're coming from IdeaVim. | + +Zed also supports per-project settings. Create a `.zed/settings.json` file in your project root to override global settings for that project, similar to how you might use `.idea` folders in RustRover. + +> **Tip:** If you're joining an existing project, check `format_on_save` before making your first commit. Otherwise you might accidentally reformat an entire file when you only meant to change one line. + +## Open or Create a Project + +After setup, press `Cmd+Shift+O` (with JetBrains keymap) to open a folder. This becomes your workspace in Zed. Unlike RustRover, there's no project configuration wizard, no toolchain selection dialog, and no Cargo project setup screen. + +To start a new project, use Cargo from the terminal: + +```sh +cargo new my_project +cd my_project +zed . +``` + +Or for a library: + +```sh +cargo new --lib my_library +``` + +You can also launch Zed from the terminal inside any existing Cargo project with: +`zed .` + +Once inside a project: + +- Use `Cmd+Shift+O` or `Cmd+E` to jump between files quickly (like RustRover's "Recent Files") +- Use `Cmd+Shift+A` or `Shift Shift` to open the Command Palette (like RustRover's "Search Everywhere") +- Use `Cmd+O` to search for symbols (like RustRover's "Go to Symbol") + +Open buffers appear as tabs across the top. The sidebar shows your file tree and Git status. Toggle it with `Cmd+1` (just like RustRover's Project tool window). + +## Differences in Keybindings + +If you chose the JetBrains keymap during onboarding, most of your shortcuts should already feel familiar. Here's a quick reference for how Zed compares to RustRover. + +### Common Shared Keybindings + +| Action | Shortcut | +| ----------------------------- | ----------------------- | +| Search Everywhere | `Shift Shift` | +| Find Action / Command Palette | `Cmd + Shift + A` | +| Go to File | `Cmd + Shift + O` | +| Go to Symbol | `Cmd + O` | +| Recent Files | `Cmd + E` | +| Go to Definition | `Cmd + B` | +| Find Usages | `Alt + F7` | +| Rename Symbol | `Shift + F6` | +| Reformat Code | `Cmd + Alt + L` | +| Toggle Project Panel | `Cmd + 1` | +| Toggle Terminal | `Alt + F12` | +| Duplicate Line | `Cmd + D` | +| Delete Line | `Cmd + Backspace` | +| Move Line Up/Down | `Shift + Alt + Up/Down` | +| Expand/Shrink Selection | `Alt + Up/Down` | +| Comment Line | `Cmd + /` | +| Go Back / Forward | `Cmd + [` / `Cmd + ]` | +| Toggle Breakpoint | `Ctrl + F8` | + +### Different Keybindings (RustRover → Zed) + +| Action | RustRover | Zed (JetBrains keymap) | +| ---------------------- | ----------- | ------------------------ | +| File Structure | `Cmd + F12` | `Cmd + F12` (outline) | +| Navigate to Next Error | `F2` | `F2` | +| Run | `Ctrl + R` | `Ctrl + Alt + R` (tasks) | +| Debug | `Ctrl + D` | `Alt + Shift + F9` | +| Stop | `Cmd + F2` | `Ctrl + F2` | +| Expand Macro | `Alt+Enter` | `Cmd + Shift + M` | + +### Unique to Zed + +| Action | Shortcut | Notes | +| ----------------- | -------------------------- | ------------------------------ | +| Toggle Right Dock | `Cmd + R` | Assistant panel, notifications | +| Split Panes | `Cmd + K`, then arrow keys | Create splits in any direction | + +### How to Customize Keybindings + +- Open the Command Palette (`Cmd+Shift+A` or `Shift Shift`) +- Run `Zed: Open Keymap Editor` + +This opens a list of all available bindings. You can override individual shortcuts or remove conflicts. + +Zed also supports key sequences (multi-key shortcuts). + +## Differences in User Interfaces + +### No Indexing + +RustRover indexes your project when you first open it to build a model of your codebase. This process runs whenever you open a project or when dependencies change via Cargo. + +Zed skips the indexing step. You open a folder and start working right away. Since both editors rely on rust-analyzer for Rust intelligence, the analysis still happens—but in Zed it runs in the background without blocking the UI or showing modal progress dialogs. + +**How to adapt:** + +- Use `Cmd+O` to search symbols across your crate (rust-analyzer handles this) +- Jump to files by name with `Cmd+Shift+O` +- `Cmd+Shift+F` gives you fast text search across the entire project +- For linting and deeper checks, run `cargo clippy` in the terminal + +### rust-analyzer: Shared Foundation, Different Integration + +Here's what makes the RustRover-to-Zed transition unique: **both editors use rust-analyzer** for Rust language intelligence. This means the core code analysis—completions, go-to-definition, find references, type inference—is fundamentally the same. + +RustRover integrates rust-analyzer into its JetBrains platform, adding a GUI layer, additional refactorings, and its own indexing on top. Zed uses rust-analyzer more directly through the Language Server Protocol (LSP). + +What this means for you: + +- **Completions** — Same quality, powered by rust-analyzer +- **Type inference** — Identical, it's the same engine +- **Go to definition / Find usages** — Works the same way +- **Macro expansion** — Available in both (use `Cmd+Shift+M` in Zed) +- **Inlay hints** — Both support type hints, parameter hints, and chaining hints + +Where you might notice differences: + +- Some refactorings available in RustRover may not have rust-analyzer equivalents +- RustRover's GUI for configuring rust-analyzer is replaced by JSON configuration in Zed +- RustRover-specific inspections (beyond Clippy) won't exist in Zed + +**How to adapt:** + +- Use `Alt+Enter` for available code actions—rust-analyzer provides many +- Configure rust-analyzer settings in `.zed/settings.json` for project-specific needs +- Run `cargo clippy` for linting (it integrates with rust-analyzer diagnostics) + +### No Project Model + +RustRover manages projects through `.idea` folders containing XML configuration files, toolchain assignments, and run configurations. The Cargo tool window provides a visual interface for your project structure, targets, and dependencies. + +Zed keeps it simpler: a project is a folder with a `Cargo.toml`. No project wizard, no toolchain dialogs, no visual Cargo management layer. + +In practice: + +- Run configurations don't carry over. Your `.idea/` setup stays behind—define the commands you need in `tasks.json` instead. +- Toolchains are managed externally via `rustup`. +- Dependencies live in `Cargo.toml`. Edit the file directly; rust-analyzer provides completions for crate names and versions. + +**How to adapt:** + +- Create a `.zed/settings.json` in your project root for project-specific settings +- Define common commands in `tasks.json` (open via Command Palette: `zed: open tasks`): + +```json +[ + { + "label": "cargo run", + "command": "cargo run" + }, + { + "label": "cargo build", + "command": "cargo build" + }, + { + "label": "cargo test", + "command": "cargo test" + }, + { + "label": "cargo clippy", + "command": "cargo clippy" + }, + { + "label": "cargo run --release", + "command": "cargo run --release" + } +] +``` + +- Use `Ctrl+Alt+R` to run tasks quickly +- Lean on your terminal (`Alt+F12`) for anything tasks don't cover + +### No Cargo Integration UI + +RustRover's Cargo tool window provides visual access to your project's targets, dependencies, and common Cargo commands. You can run builds, tests, and benchmarks with a click. + +Zed doesn't have a Cargo GUI. You work with Cargo through: + +- **Terminal** — Run any Cargo command directly +- **Tasks** — Define shortcuts for common commands +- **Gutter icons** — Run tests and binaries with clickable icons + +**How to adapt:** + +- Get comfortable with Cargo CLI commands: `cargo build`, `cargo run`, `cargo test`, `cargo clippy`, `cargo doc` +- Use tasks for commands you run frequently +- For dependency management, edit `Cargo.toml` directly (rust-analyzer provides completions for crate names and versions) + +### Tool Windows vs. Docks + +RustRover organizes auxiliary views into numbered tool windows (Project = 1, Cargo = Alt+1, Terminal = Alt+F12, etc.). Zed uses a similar concept called "docks": + +| RustRover Tool Window | Zed Equivalent | Shortcut (JetBrains keymap) | +| --------------------- | -------------- | --------------------------- | +| Project (1) | Project Panel | `Cmd + 1` | +| Git (9 or Cmd+0) | Git Panel | `Cmd + 0` | +| Terminal (Alt+F12) | Terminal Panel | `Alt + F12` | +| Structure (7) | Outline Panel | `Cmd + 7` | +| Problems (6) | Diagnostics | `Cmd + 6` | +| Debug (5) | Debug Panel | `Cmd + 5` | + +Zed has three dock positions: left, bottom, and right. Panels can be moved between docks by dragging or through settings. + +Note that there's no dedicated Cargo tool window in Zed. Use the terminal or define tasks for your common Cargo commands. + +### Debugging + +Both RustRover and Zed offer integrated debugging for Rust, but using different backends: + +- RustRover uses its own debugger integration +- Zed uses **CodeLLDB** (the same debug adapter popular in VS Code) + +To debug Rust code in Zed: + +- Set breakpoints with `Ctrl+F8` +- Start debugging with `Alt+Shift+F9` or press `F4` and select a debug target +- Step through code with `F7` (step into), `F8` (step over), `Shift+F8` (step out) +- Continue execution with `F9` + +Zed can automatically detect debuggable targets in your Cargo project. Press `F4` to see available options. + +For more control, create a `.zed/debug.json` file: + +```json +[ + { + "label": "Debug Binary", + "adapter": "CodeLLDB", + "request": "launch", + "program": "${workspaceFolder}/target/debug/my_project" + }, + { + "label": "Debug Tests", + "adapter": "CodeLLDB", + "request": "launch", + "cargo": { + "args": ["test", "--no-run"], + "filter": { + "kind": "test" + } + } + }, + { + "label": "Debug with Arguments", + "adapter": "CodeLLDB", + "request": "launch", + "program": "${workspaceFolder}/target/debug/my_project", + "args": ["--config", "dev.toml"] + } +] +``` + +> **Note:** Some users have reported that RustRover's debugger can have issues with variable inspection and breakpoints in certain scenarios. CodeLLDB in Zed provides a solid alternative, though debugging Rust can be challenging in any editor due to optimizations and macro-generated code. + +### Running Tests + +RustRover has a dedicated test runner with a visual interface showing pass/fail status for each test. Zed provides test running through: + +- **Gutter icons** — Click the play button next to `#[test]` functions or test modules +- **Tasks** — Define `cargo test` commands in `tasks.json` +- **Terminal** — Run `cargo test` directly + +The test output appears in the terminal panel. For more detailed output, use: + +- `cargo test -- --nocapture` to see println! output +- `cargo test -- --test-threads=1` for sequential test execution +- `cargo test specific_test_name` to run a single test + +### Extensions vs. Plugins + +RustRover has a plugin ecosystem, though it's more limited than other JetBrains IDEs since Rust support is built-in. + +Zed's extension ecosystem is smaller and more focused: + +- Language support and syntax highlighting +- Themes +- Slash commands for AI +- Context servers + +Several features that might require plugins in other editors are built into Zed: + +- Real-time collaboration with voice chat +- AI coding assistance +- Built-in terminal +- Task runner +- rust-analyzer integration +- rustfmt formatting + +### What's Not in Zed + +To set expectations clearly, here's what RustRover offers that Zed doesn't have: + +- **Cargo.toml GUI editor** — Edit the file directly (rust-analyzer helps with completions) +- **Visual dependency management** — Use `cargo add`, `cargo remove`, or edit `Cargo.toml` +- **Profiler integration** — Use `cargo flamegraph`, `perf`, or external profiling tools +- **Database tools** — Use DataGrip, DBeaver, or TablePlus +- **HTTP Client** — Use tools like `curl`, `httpie`, or Postman +- **Coverage visualization** — Use `cargo tarpaulin` or `cargo llvm-cov` externally + +## A Note on Licensing and Telemetry + +If you're moving from RustRover partly due to licensing concerns or telemetry policies, you should know: + +- **Zed is open source** (MIT licensed for the editor, AGPL for collaboration services) +- **Telemetry is optional** and can be disabled during onboarding or in settings +- **No license tiers**: All features are available to everyone + +## Collaboration in Zed vs. RustRover + +RustRover offers Code With Me as a separate feature for collaboration. Zed has collaboration built into the core experience. + +- Open the Collab Panel in the left dock +- Create a channel and [invite your collaborators](https://zed.dev/docs/collaboration#inviting-a-collaborator) to join +- [Share your screen or your codebase](https://zed.dev/docs/collaboration#share-a-project) directly + +Once connected, you'll see each other's cursors, selections, and edits in real time. Voice chat is included. There's no need for separate tools or third-party logins. + +## Using AI in Zed + +If you're used to AI assistants in RustRover (like JetBrains AI Assistant), Zed offers similar capabilities with more flexibility. + +### Configuring GitHub Copilot + +1. Open Settings with `Cmd+,` (macOS) or `Ctrl+,` (Linux/Windows) +2. Navigate to **AI → Edit Predictions** +3. Click **Configure** next to "Configure Providers" +4. Under **GitHub Copilot**, click **Sign in to GitHub** + +Once signed in, just start typing. Zed will offer suggestions inline for you to accept. + +### Additional AI Options + +To use other AI models in Zed, you have several options: + +- Use Zed's hosted models, with higher rate limits. Requires [authentication](https://zed.dev/docs/accounts.html) and subscription to [Zed Pro](https://zed.dev/docs/ai/subscription.html). +- Bring your own [API keys](https://zed.dev/docs/ai/llm-providers.html), no authentication needed +- Use [external agents like Claude Code](https://zed.dev/docs/ai/external-agents.html) + +## Advanced Config and Productivity Tweaks + +Zed exposes advanced settings for power users who want to fine-tune their environment. + +Here are a few useful tweaks for Rust developers: + +**Format on Save (uses rustfmt by default):** + +```json +"format_on_save": "on" +``` + +**Configure inlay hints for Rust:** + +```json +{ + "inlay_hints": { + "enabled": true, + "show_type_hints": true, + "show_parameter_hints": true, + "show_other_hints": true + } +} +``` + +**Configure rust-analyzer settings:** + +```json +{ + "lsp": { + "rust-analyzer": { + "initialization_options": { + "checkOnSave": { + "command": "clippy" + }, + "cargo": { + "allFeatures": true + }, + "procMacro": { + "enable": true + } + } + } + } +} +``` + +**Use a separate target directory for rust-analyzer (faster builds):** + +```json +{ + "lsp": { + "rust-analyzer": { + "initialization_options": { + "rust-analyzer.cargo.targetDir": true + } + } + } +} +``` + +This tells rust-analyzer to use `target/rust-analyzer` instead of `target`, so IDE analysis doesn't conflict with your manual `cargo build` commands. + +**Enable direnv support (useful for Rust projects using direnv):** + +```json +"load_direnv": "shell_hook" +``` + +**Configure linked projects for workspaces:** + +If you work with multiple Cargo projects that aren't in a workspace, you can tell rust-analyzer about them: + +```json +{ + "lsp": { + "rust-analyzer": { + "initialization_options": { + "linkedProjects": ["./project-a/Cargo.toml", "./project-b/Cargo.toml"] + } + } + } +} +``` + +## Next Steps + +Now that you're set up, here are some resources to help you get the most out of Zed: + +- [Configuring Zed](../configuring-zed.md) — Customize settings, themes, and editor behavior +- [Key Bindings](../key-bindings.md) — Learn how to customize and extend your keymap +- [Tasks](../tasks.md) — Set up build and run commands for your projects +- [AI Features](../ai/overview.md) — Explore Zed's AI capabilities beyond code completion +- [Collaboration](../collaboration/overview.md) — Share your projects and code together in real time +- [Rust in Zed](../languages/rust.md) — Rust-specific setup and configuration diff --git a/docs/src/migrate/webstorm.md b/docs/src/migrate/webstorm.md new file mode 100644 index 0000000000000000000000000000000000000000..78b80b355b47370a821f08fd6108d947182f0acf --- /dev/null +++ b/docs/src/migrate/webstorm.md @@ -0,0 +1,455 @@ +# How to Migrate from WebStorm to Zed + +This guide covers how to set up Zed if you're coming from WebStorm, including keybindings, settings, and the differences you should expect as a JavaScript/TypeScript developer. + +## Install Zed + +Zed is available on macOS, Windows, and Linux. + +For macOS, you can download it from zed.dev/download, or install via Homebrew: + +```sh +brew install --cask zed +``` + +For Windows, download the installer from zed.dev/download, or install via winget: + +```sh +winget install Zed.Zed +``` + +For most Linux users, the easiest way to install Zed is through our installation script: + +```sh +curl -f https://zed.dev/install.sh | sh +``` + +After installation, you can launch Zed from your Applications folder (macOS), Start menu (Windows), or directly from the terminal using: +`zed .` +This opens the current directory in Zed. + +## Set Up the JetBrains Keymap + +If you're coming from WebStorm, the fastest way to feel at home is to use the JetBrains keymap. During onboarding, you can select it as your base keymap. If you missed that step, you can change it anytime: + +1. Open Settings with `Cmd+,` (macOS) or `Ctrl+,` (Linux/Windows) +2. Search for `Base Keymap` +3. Select `JetBrains` + +Or add this directly to your `settings.json`: + +```json +{ + "base_keymap": "JetBrains" +} +``` + +This maps familiar shortcuts like `Shift Shift` for Search Everywhere, `Cmd+O` for Go to Class, and `Cmd+Shift+A` for Find Action. + +## Set Up Editor Preferences + +You can configure settings manually in the Settings Editor. + +To edit your settings: + +1. `Cmd+,` to open the Settings Editor. +2. Run `zed: open settings` in the Command Palette. + +Settings WebStorm users typically configure first: + +| Zed Setting | What it does | +| ----------------------- | ------------------------------------------------------------------------------- | +| `format_on_save` | Auto-format when saving. Set to `"on"` to enable. | +| `soft_wrap` | Wrap long lines. Options: `"none"`, `"editor_width"`, `"preferred_line_length"` | +| `preferred_line_length` | Column width for wrapping and rulers. Default is 80. | +| `inlay_hints` | Show parameter names and type hints inline, like WebStorm's hints. | +| `relative_line_numbers` | Useful if you're coming from IdeaVim. | + +Zed also supports per-project settings. Create a `.zed/settings.json` file in your project root to override global settings for that project, similar to how you might use `.idea` folders in WebStorm. + +> **Tip:** If you're joining an existing project, check `format_on_save` before making your first commit. Otherwise you might accidentally reformat an entire file when you only meant to change one line. + +## Open or Create a Project + +After setup, press `Cmd+Shift+O` (with JetBrains keymap) to open a folder. This becomes your workspace in Zed. Unlike WebStorm, there's no project configuration wizard, no framework selection dialog, and no project structure setup required. + +To start a new project, create a directory using your terminal or file manager, then open it in Zed. The editor will treat that folder as the root of your project. For new projects, you'd typically run `npm init`, `pnpm create`, or your framework's CLI tool first, then open the resulting folder in Zed. + +You can also launch Zed from the terminal inside any folder with: +`zed .` + +Once inside a project: + +- Use `Cmd+Shift+O` or `Cmd+E` to jump between files quickly (like WebStorm's "Recent Files") +- Use `Cmd+Shift+A` or `Shift Shift` to open the Command Palette (like WebStorm's "Search Everywhere") +- Use `Cmd+O` to search for symbols (like WebStorm's "Go to Symbol") + +Open buffers appear as tabs across the top. The sidebar shows your file tree and Git status. Toggle it with `Cmd+1` (just like WebStorm's Project tool window). + +## Differences in Keybindings + +If you chose the JetBrains keymap during onboarding, most of your shortcuts should already feel familiar. Here's a quick reference for how Zed compares to WebStorm. + +### Common Shared Keybindings + +| Action | Shortcut | +| ----------------------------- | ----------------------- | +| Search Everywhere | `Shift Shift` | +| Find Action / Command Palette | `Cmd + Shift + A` | +| Go to File | `Cmd + Shift + O` | +| Go to Symbol | `Cmd + O` | +| Recent Files | `Cmd + E` | +| Go to Definition | `Cmd + B` | +| Find Usages | `Alt + F7` | +| Rename Symbol | `Shift + F6` | +| Reformat Code | `Cmd + Alt + L` | +| Toggle Project Panel | `Cmd + 1` | +| Toggle Terminal | `Alt + F12` | +| Duplicate Line | `Cmd + D` | +| Delete Line | `Cmd + Backspace` | +| Move Line Up/Down | `Shift + Alt + Up/Down` | +| Expand/Shrink Selection | `Alt + Up/Down` | +| Comment Line | `Cmd + /` | +| Go Back / Forward | `Cmd + [` / `Cmd + ]` | +| Toggle Breakpoint | `Ctrl + F8` | + +### Different Keybindings (WebStorm → Zed) + +| Action | WebStorm | Zed (JetBrains keymap) | +| ---------------------- | ----------- | ------------------------ | +| File Structure | `Cmd + F12` | `Cmd + F12` (outline) | +| Navigate to Next Error | `F2` | `F2` | +| Run | `Ctrl + R` | `Ctrl + Alt + R` (tasks) | +| Debug | `Ctrl + D` | `Alt + Shift + F9` | +| Stop | `Cmd + F2` | `Ctrl + F2` | + +### Unique to Zed + +| Action | Shortcut | Notes | +| ----------------- | -------------------------- | ------------------------------ | +| Toggle Right Dock | `Cmd + R` | Assistant panel, notifications | +| Split Panes | `Cmd + K`, then arrow keys | Create splits in any direction | + +### How to Customize Keybindings + +- Open the Command Palette (`Cmd+Shift+A` or `Shift Shift`) +- Run `Zed: Open Keymap Editor` + +This opens a list of all available bindings. You can override individual shortcuts or remove conflicts. + +Zed also supports key sequences (multi-key shortcuts). + +## Differences in User Interfaces + +### No Indexing + +If you've used WebStorm on large projects, you know the wait. Opening a project with many dependencies can mean watching "Indexing..." for anywhere from 30 seconds to several minutes. WebStorm indexes your entire codebase and `node_modules` to power its code intelligence, and re-indexes when dependencies change. + +Zed doesn't index. You open a folder and start coding immediately—no progress bars, no "Indexing paused" banners. File search and navigation stay fast regardless of project size or how many `node_modules` dependencies you have. + +WebStorm's index enables features like finding all usages across your entire codebase, tracking import hierarchies, and flagging unused exports project-wide. Zed relies on language servers for this analysis, which may not cover as much ground. + +**How to adapt:** + +- Search symbols across the project with `Cmd+O` (powered by the TypeScript language server) +- Find files by name with `Cmd+Shift+O` +- Use `Cmd+Shift+F` for text search—it stays fast even in large monorepos +- Run `tsc --noEmit` or `eslint .` from the terminal when you need deeper project-wide analysis + +### LSP vs. Native Language Intelligence + +WebStorm has its own JavaScript and TypeScript analysis engine built by JetBrains. This engine understands your code deeply: it resolves types, tracks data flow, knows about framework-specific patterns, and offers specialized refactorings. + +Zed uses the Language Server Protocol (LSP) for code intelligence. For JavaScript and TypeScript, Zed supports: + +- **vtsls** (default) — Fast TypeScript language server with excellent performance +- **typescript-language-server** — The standard TypeScript LSP implementation +- **ESLint** — Linting integration +- **Prettier** — Code formatting (built-in) + +The TypeScript LSP experience is mature and robust. You get accurate completions, type checking, go-to-definition, and find-references. The experience is comparable to VS Code, which uses the same underlying TypeScript services. + +Where you might notice differences: + +- Framework-specific intelligence (Angular templates, Vue SFCs) may be less integrated +- Some complex refactorings (extract component with proper imports) may be less sophisticated +- Auto-import suggestions depend on what the language server knows about your project + +**How to adapt:** + +- Use `Alt+Enter` for available code actions—the list will vary by language server +- Ensure your `tsconfig.json` is properly configured so the language server understands your project structure +- Use Prettier for consistent formatting (it's enabled by default for JS/TS) +- For code inspection similar to WebStorm's "Inspect Code," check the Diagnostics panel (`Cmd+6`)—ESLint and TypeScript together catch many of the same issues + +### No Project Model + +WebStorm manages projects through `.idea` folders containing XML configuration files, framework detection, and run configurations. This model lets WebStorm remember your project settings, manage npm scripts through the UI, and persist run/debug setups. + +Zed takes a different approach: a project is just a folder. There's no setup wizard, no framework detection dialog, no project structure to configure. + +What this means in practice: + +- Run configurations aren't a thing. Define reusable commands in `tasks.json` instead. Note that your existing `.idea/` configurations won't carry over—you'll set up the ones you need fresh. +- npm scripts live in the terminal. Run `npm run dev`, `pnpm build`, or `yarn test` directly—there's no dedicated npm panel. +- No framework detection. Zed treats React, Angular, Vue, and vanilla JS/TS the same way. + +**How to adapt:** + +- Create a `.zed/settings.json` in your project root for project-specific settings +- Define common commands in `tasks.json` (open via Command Palette: `zed: open tasks`): + +```json +[ + { + "label": "dev", + "command": "npm run dev" + }, + { + "label": "build", + "command": "npm run build" + }, + { + "label": "test", + "command": "npm test" + }, + { + "label": "test current file", + "command": "npm test -- $ZED_FILE" + } +] +``` + +- Use `Ctrl+Alt+R` to run tasks quickly +- Lean on your terminal (`Alt+F12`) for anything tasks don't cover + +### No Framework Integration + +WebStorm's value for web development comes largely from its framework integration. React components get special treatment. Angular has dedicated tooling. Vue single-file components are fully understood. The npm tool window shows all your scripts. + +Zed has none of this built-in. The TypeScript language server sees your code as TypeScript—it doesn't understand that a function is a React component or that a file is an Angular service. + +**How to adapt:** + +- Use grep and file search liberally. `Cmd+Shift+F` with a regex can find component definitions, route configurations, or API endpoints. +- Rely on your language server's "find references" (`Alt+F7`) for navigation—it works, just without framework context +- Consider using framework-specific CLI tools (`ng`, `next`, `vite`) from Zed's terminal +- For React, JSX/TSX syntax and TypeScript types still provide good intelligence + +> **Tip:** For projects with complex configurations, keep your framework's documentation handy. Zed's speed comes with less hand-holding for framework-specific features. + +### Tool Windows vs. Docks + +WebStorm organizes auxiliary views into numbered tool windows (Project = 1, npm = Alt+F11, Terminal = Alt+F12, etc.). Zed uses a similar concept called "docks": + +| WebStorm Tool Window | Zed Equivalent | Shortcut (JetBrains keymap) | +| -------------------- | -------------- | --------------------------- | +| Project (1) | Project Panel | `Cmd + 1` | +| Git (9 or Cmd+0) | Git Panel | `Cmd + 0` | +| Terminal (Alt+F12) | Terminal Panel | `Alt + F12` | +| Structure (7) | Outline Panel | `Cmd + 7` | +| Problems (6) | Diagnostics | `Cmd + 6` | +| Debug (5) | Debug Panel | `Cmd + 5` | + +Zed has three dock positions: left, bottom, and right. Panels can be moved between docks by dragging or through settings. + +Note that there's no dedicated npm tool window in Zed. Use the terminal or define tasks for your common npm scripts. + +### Debugging + +Both WebStorm and Zed offer integrated debugging for JavaScript and TypeScript: + +- Zed uses `vscode-js-debug` (the same debug adapter that VS Code uses) +- Set breakpoints with `Ctrl+F8` +- Start debugging with `Alt+Shift+F9` or press `F4` and select a debug target +- Step through code with `F7` (step into), `F8` (step over), `Shift+F8` (step out) +- Continue execution with `F9` + +Zed can debug: + +- Node.js applications and scripts +- Chrome/browser JavaScript +- Jest, Mocha, Vitest, and other test frameworks +- Next.js (both server and client-side) + +For more control, create a `.zed/debug.json` file: + +```json +[ + { + "label": "Debug Current File", + "adapter": "JavaScript", + "program": "$ZED_FILE", + "request": "launch" + }, + { + "label": "Debug Node Server", + "adapter": "JavaScript", + "request": "launch", + "program": "${workspaceFolder}/src/server.js" + }, + { + "label": "Attach to Chrome", + "adapter": "JavaScript", + "request": "attach", + "port": 9222 + } +] +``` + +Zed also recognizes `.vscode/launch.json` configurations, so existing VS Code debug setups often work out of the box. + +### Running Tests + +WebStorm has a dedicated test runner with a visual interface showing pass/fail status for each test. Zed provides test running through: + +- **Gutter icons** — Click the play button next to test functions or describe blocks +- **Tasks** — Define test commands in `tasks.json` +- **Terminal** — Run `npm test`, `jest`, `vitest`, etc. directly + +Zed supports auto-detection for common test frameworks: + +- Jest +- Mocha +- Vitest +- Jasmine +- Bun test +- Node.js test runner + +The test output appears in the terminal panel. For Jest, use `--verbose` for detailed output or `--watch` for continuous testing during development. + +### Extensions vs. Plugins + +WebStorm has a plugin ecosystem covering additional language support, themes, and tool integrations. + +Zed's extension ecosystem is smaller and more focused: + +- Language support and syntax highlighting +- Themes +- Slash commands for AI +- Context servers + +Several features that require plugins in WebStorm are built into Zed: + +- Real-time collaboration with voice chat +- AI coding assistance +- Built-in terminal +- Task runner +- LSP-based code intelligence +- Prettier formatting +- ESLint integration + +### What's Not in Zed + +To set expectations clearly, here's what WebStorm offers that Zed doesn't have: + +- **npm tool window** — Use the terminal or tasks instead +- **HTTP Client** — Use tools like Postman, Insomnia, or curl +- **Database tools** — Use DataGrip, DBeaver, or TablePlus +- **Framework-specific tooling** (Angular schematics, React refactorings) — Use CLI tools +- **Visual package.json editor** — Edit the file directly +- **Built-in REST client** — Use external tools or extensions +- **Profiler integration** — Use Chrome DevTools or Node.js profiling tools + +## Collaboration in Zed vs. WebStorm + +WebStorm offers Code With Me as a separate feature for collaboration. Zed has collaboration built into the core experience. + +- Open the Collab Panel in the left dock +- Create a channel and [invite your collaborators](https://zed.dev/docs/collaboration#inviting-a-collaborator) to join +- [Share your screen or your codebase](https://zed.dev/docs/collaboration#share-a-project) directly + +Once connected, you'll see each other's cursors, selections, and edits in real time. Voice chat is included. There's no need for separate tools or third-party logins. + +## Using AI in Zed + +If you're used to AI assistants in WebStorm (like GitHub Copilot, JetBrains AI Assistant, or Junie), Zed offers similar capabilities with more flexibility. + +### Configuring GitHub Copilot + +1. Open Settings with `Cmd+,` (macOS) or `Ctrl+,` (Linux/Windows) +2. Navigate to **AI → Edit Predictions** +3. Click **Configure** next to "Configure Providers" +4. Under **GitHub Copilot**, click **Sign in to GitHub** + +Once signed in, just start typing. Zed will offer suggestions inline for you to accept. + +### Additional AI Options + +To use other AI models in Zed, you have several options: + +- Use Zed's hosted models, with higher rate limits. Requires [authentication](https://zed.dev/docs/accounts.html) and subscription to [Zed Pro](https://zed.dev/docs/ai/subscription.html). +- Bring your own [API keys](https://zed.dev/docs/ai/llm-providers.html), no authentication needed +- Use [external agents like Claude Code](https://zed.dev/docs/ai/external-agents.html) + +## Advanced Config and Productivity Tweaks + +Zed exposes advanced settings for power users who want to fine-tune their environment. + +Here are a few useful tweaks for JavaScript/TypeScript developers: + +**Format on Save:** + +```json +"format_on_save": "on" +``` + +**Configure Prettier as the default formatter:** + +```json +{ + "formatter": { + "external": { + "command": "prettier", + "arguments": ["--stdin-filepath", "{buffer_path}"] + } + } +} +``` + +**Enable ESLint code actions:** + +```json +{ + "lsp": { + "eslint": { + "settings": { + "codeActionOnSave": { + "rules": ["import/order"] + } + } + } + } +} +``` + +**Configure TypeScript strict mode hints:** + +In your `tsconfig.json`, enable strict mode for better type checking: + +```json +{ + "compilerOptions": { + "strict": true, + "noUncheckedIndexedAccess": true + } +} +``` + +**Enable direnv support (useful for projects using direnv for environment variables):** + +```json +"load_direnv": "shell_hook" +``` + +## Next Steps + +Now that you're set up, here are some resources to help you get the most out of Zed: + +- [Configuring Zed](../configuring-zed.md) — Customize settings, themes, and editor behavior +- [Key Bindings](../key-bindings.md) — Learn how to customize and extend your keymap +- [Tasks](../tasks.md) — Set up build and run commands for your projects +- [AI Features](../ai/overview.md) — Explore Zed's AI capabilities beyond code completion +- [Collaboration](../collaboration/overview.md) — Share your projects and code together in real time +- [JavaScript in Zed](../languages/javascript.md) — JavaScript-specific setup and configuration +- [TypeScript in Zed](../languages/typescript.md) — TypeScript-specific setup and configuration diff --git a/docs/src/worktree-trust.md b/docs/src/worktree-trust.md index 158851117bfdc4d00746594d74e1e6dae0bb84dc..590f063a75ac5d77e60d50f03af4795d6ec2961f 100644 --- a/docs/src/worktree-trust.md +++ b/docs/src/worktree-trust.md @@ -4,11 +4,11 @@ A worktree in Zed is either a directory or a single file that Zed opens as a sta Zed opens a worktree every time `zed some/path` is invoked, on drag and dropping a file or directory into Zed, on opening user settings.json, etc. Every worktree opened may contain a `.zed/settings.json` file with extra configuration options that may require installing and spawning language servers or MCP servers. -Note that the Zed workspace itself may also perform user-configured MCP server installation and spawning, even if no worktrees are open. +In order to provide users the opportunity to make their own choices according to their unique threat model and risk tolerance, all worktrees will be started in Restricted mode, which prevents download and execution of any related items from `.zed/settings.json`. Until configured to trust the worktree(s), Zed will not perform any related untrusted actions and will wait for user confirmation. This gives users a chance to review and understand any pre-configured settings, MCP servers, or language servers associated with a project. -In order to provide users the opportunity to make their own choices according to their unique threat model and risk tolerance, the workspace and all worktrees will be started in Restricted mode, which prevents download and execution of any related items. Until configured to trust the workspace and/or worktrees, Zed will not perform any untrusted actions and will wait for user confirmation. This gives users a chance to review and understand any pre-configured settings, MCP servers, or language servers associated with a project. +Note that at this point, Zed trusts the tools it installs itself, hence global entities such as global MCP servers, language servers like prettier and copilot are still in installed and started as usual, independent of worktree trust. -If a worktree is not trusted, Zed will indicate this with an exclamation mark icon in the title bar and a message in the Agent panel. Clicking this icon or using `workspace::ToggleWorktreeSecurity` action will bring up the security modal that allows the user to trust the worktree. +If a worktree is not trusted, Zed will indicate this with an exclamation mark icon in the title bar. Clicking this icon or using `workspace::ToggleWorktreeSecurity` action will bring up the security modal that allows the user to trust the worktree. Trusting any worktree will persist this information between restarts. It's possible to clear all trusted worktrees with `workspace::ClearTrustedWorktrees` command. This command will restart Zed, to ensure no untrusted settings, language servers or MCP servers persist. @@ -25,7 +25,7 @@ Restricted Mode prevents: ## Configuring broad worktree trust -By default, Zed won't trust any new worktrees and users will be required to trust each new worktree. Though not recommended, users may elect to trust all worktrees and the current workspace for a given session by configuring the following setting: +By default, Zed won't trust any new worktrees and users will be required to trust each new worktree. Though not recommended, users may elect to trust all worktrees by configuring the following setting: ```json [settings] "session": { @@ -47,20 +47,12 @@ A typical scenario where a directory might be open and a single file is subseque Spawning a language server presents a risk should the language server experience a supply-chain attack; therefore, Zed restricts that by default. Each single file worktree requires a separate trust grant, unless the directory containing it is trusted or all worktrees are trusted. -- "workspace" - -Even an empty Zed workspace with no files or directories open presents a risk if new MCP servers are locally configured by the user without review. For instance, opening an Assistant Panel and creating a new external agent thread might require installing and running new user-configured [Model Context Protocol servers](./ai/mcp.md). By default, zed will restrict a new MCP server until the user elects to trust the local workspace. Users may also disable the entire Agent panel if preferred; see [AI Configuration](./ai/configuration.md) for more details. - -Workspace trust, permitted by trusting Zed with no worktrees open, allows locally configured resources to be downloaded and executed. Workspace trust is per host and also trusts all single file worktrees from the same host in order to permit all local user-configured MCP and language servers to start. - - "directory worktree" If a directory is open in Zed, it's a full worktree which may spawn multiple language servers associated with it or spawn MCP servers if contained in a project settings file.Therefore, each directory worktree requires a separate trust grant unless a parent directory worktree trust is granted (see below). -When a directory worktree is trusted, language and MCP servers are permitted to be downloaded and started, hence we also enable workspace trust for the host in question automatically when this occurs. +When a directory worktree is trusted, language and MCP servers are permitted to be downloaded and started, hence we also enable single file worktree trust for the host in question automatically when this occurs: this helps when opening single files when using language server features in the trusted directory worktree. - "parent directory worktree" To permit trust decisions for multiple directory worktrees at once, it's possible to trust all subdirectories of a given parent directory worktree opened in Zed by checking the appropriate checkbox. This will grant trust to all its subdirectories, including all current and potential directory worktrees. - -This also automatically enables workspace trust to permit the newly trusted resources to download and start. diff --git a/script/bundle-mac b/script/bundle-mac index c6c925f073600336f4aa3114a732609481ade26e..93ea07b162612d27784dbd0eb54598b0aa2252c3 100755 --- a/script/bundle-mac +++ b/script/bundle-mac @@ -106,6 +106,17 @@ mv Cargo.toml.backup Cargo.toml popd echo "Bundled ${app_path}" +# DocumentTypes.plist references CFBundleTypeIconFile "Document", so the bundle must contain Document.icns. +# We use the app icon as a placeholder document icon for now. +document_icon_source="crates/zed/resources/Document.icns" +document_icon_target="${app_path}/Contents/Resources/Document.icns" +if [[ -f "${document_icon_source}" ]]; then + mkdir -p "$(dirname "${document_icon_target}")" + cp "${document_icon_source}" "${document_icon_target}" +else + echo "cargo::warning=Missing ${document_icon_source}; macOS document icons may not appear in Finder." +fi + if [[ -n "${MACOS_CERTIFICATE:-}" && -n "${MACOS_CERTIFICATE_PASSWORD:-}" && -n "${APPLE_NOTARIZATION_KEY:-}" && -n "${APPLE_NOTARIZATION_KEY_ID:-}" && -n "${APPLE_NOTARIZATION_ISSUER_ID:-}" ]]; then can_code_sign=true diff --git a/script/danger/dangerfile.ts b/script/danger/dangerfile.ts index 88dc5c5e71c640a83315ac5f1b14c216763023fd..7151985021f3fdfb75c01c6e2f8c964fa51d3740 100644 --- a/script/danger/dangerfile.ts +++ b/script/danger/dangerfile.ts @@ -6,6 +6,9 @@ prHygiene({ rules: { // Don't enable this rule just yet, as it can have false positives. useImperativeMood: "off", + noConventionalCommits: { + bannedTypes: ["feat", "fix", "style", "refactor", "perf", "test", "chore", "build", "revert"], + }, }, }); diff --git a/script/danger/package.json b/script/danger/package.json index eaa1035e89c97da8ef2089e97eb638d649ee6877..be44da6233a1c5ee87f8445e13953031497acfa5 100644 --- a/script/danger/package.json +++ b/script/danger/package.json @@ -8,6 +8,6 @@ }, "devDependencies": { "danger": "13.0.4", - "danger-plugin-pr-hygiene": "0.6.1" + "danger-plugin-pr-hygiene": "0.7.1" } } diff --git a/script/danger/pnpm-lock.yaml b/script/danger/pnpm-lock.yaml index fd6b3f66acb627d57520e4ca928cc8ce2793b4b9..eea293cfed78fcf43ed926484b2f13b5b9c74843 100644 --- a/script/danger/pnpm-lock.yaml +++ b/script/danger/pnpm-lock.yaml @@ -12,8 +12,8 @@ importers: specifier: 13.0.4 version: 13.0.4 danger-plugin-pr-hygiene: - specifier: 0.6.1 - version: 0.6.1 + specifier: 0.7.1 + version: 0.7.1 packages: @@ -134,8 +134,8 @@ packages: core-js@3.45.1: resolution: {integrity: sha512-L4NPsJlCfZsPeXukyzHFlg/i7IIVwHSItR0wg0FLNqYClJ4MQYTYLbC7EkjKYRLZF2iof2MUgN0EGy7MdQFChg==} - danger-plugin-pr-hygiene@0.6.1: - resolution: {integrity: sha512-nb+iUQvirE3BlKXI1WoOND6sujyGzHar590mJm5tt4RLi65HXFaU5hqONxgDoWFujJNHYnXse9yaZdxnxEi4QA==} + danger-plugin-pr-hygiene@0.7.1: + resolution: {integrity: sha512-ll070nNaL3OeO2nooYWflPE/CRKLeq8GiH2C68u5zM3gW4gepH89GhVv0sYNNGLx4cYwa1zZ/TuiYYhC49z06Q==} danger@13.0.4: resolution: {integrity: sha512-IAdQ5nSJyIs4zKj6AN35ixt2B0Ce3WZUm3IFe/CMnL/Op7wV7IGg4D348U0EKNaNPP58QgXbdSk9pM+IXP1QXg==} @@ -573,7 +573,7 @@ snapshots: core-js@3.45.1: {} - danger-plugin-pr-hygiene@0.6.1: {} + danger-plugin-pr-hygiene@0.7.1: {} danger@13.0.4: dependencies: diff --git a/script/generate-action-metadata b/script/generate-action-metadata new file mode 100755 index 0000000000000000000000000000000000000000..146b1f0d78ef92c47322a70dccf0e9e1f3f530d3 --- /dev/null +++ b/script/generate-action-metadata @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +set -euo pipefail + +cd "$(dirname "$0")/.." + +echo "Generating action metadata..." +cargo run -p zed -- --dump-all-actions > crates/docs_preprocessor/actions.json + +echo "Generated crates/docs_preprocessor/actions.json with $(grep -c '"name":' crates/docs_preprocessor/actions.json) actions" diff --git a/script/verify-macos-document-icon b/script/verify-macos-document-icon new file mode 100755 index 0000000000000000000000000000000000000000..de2581c9df764ee2019740048381d6d66dc3499d --- /dev/null +++ b/script/verify-macos-document-icon @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +set -euo pipefail + +usage() { + cat <<'USAGE' +Usage: + script/verify-macos-document-icon /path/to/Zed.app + +Verifies that the given macOS app bundle's Info.plist references a document icon +named "Document" and that the corresponding icon file exists in the bundle. + +Specifically checks: + - CFBundleDocumentTypes[*].CFBundleTypeIconFile includes "Document" + - Contents/Resources/Document.icns exists + +Exit codes: + 0 - success + 1 - verification failed + 2 - invalid usage / missing prerequisites +USAGE +} + +fail() { + echo "error: $*" >&2 + exit 1 +} + +if [[ $# -ne 1 ]]; then + usage >&2 + exit 2 +fi + +app_path="$1" + +if [[ ! -d "${app_path}" ]]; then + fail "app bundle not found: ${app_path}" +fi + +info_plist="${app_path}/Contents/Info.plist" +if [[ ! -f "${info_plist}" ]]; then + fail "missing Info.plist: ${info_plist}" +fi + +if ! command -v plutil >/dev/null 2>&1; then + fail "plutil not found (required on macOS to read Info.plist)" +fi + +# Convert to JSON for robust parsing. plutil outputs JSON to stdout in this mode. +info_json="$(plutil -convert json -o - "${info_plist}")" + +# Check that CFBundleDocumentTypes exists and that at least one entry references "Document". +# We use Python for JSON parsing; macOS ships with Python 3 on many setups, but not all. +# If python3 isn't available, fall back to a simpler grep-based check. +has_document_icon_ref="false" +if command -v python3 >/dev/null 2>&1; then + has_document_icon_ref="$(python3 -c "import json,sys; d=json.load(sys.stdin); types=d.get('CFBundleDocumentTypes', []); vals=[t.get('CFBundleTypeIconFile') for t in types if isinstance(t, dict)]; print('true' if 'Document' in vals else 'false')" <<<"${info_json}")" +else + # This is a best-effort fallback. It may produce false negatives if the JSON formatting differs. + if echo "${info_json}" | grep -q '"CFBundleTypeIconFile"[[:space:]]*:[[:space:]]*"Document"'; then + has_document_icon_ref="true" + fi +fi + +if [[ "${has_document_icon_ref}" != "true" ]]; then + echo "Verification failed for: ${app_path}" >&2 + echo "Expected Info.plist to reference CFBundleTypeIconFile \"Document\" in CFBundleDocumentTypes." >&2 + echo "Tip: This bundle may be missing DocumentTypes.plist extensions or may have different icon naming." >&2 + exit 1 +fi + +document_icon_path="${app_path}/Contents/Resources/Document.icns" +if [[ ! -f "${document_icon_path}" ]]; then + echo "Verification failed for: ${app_path}" >&2 + echo "Expected document icon to exist: ${document_icon_path}" >&2 + echo "Tip: The bundle script should copy crates/zed/resources/Document.icns into Contents/Resources/Document.icns." >&2 + exit 1 +fi + +echo "OK: ${app_path}" +echo " - Info.plist references CFBundleTypeIconFile \"Document\"" +echo " - Found ${document_icon_path}" diff --git a/tooling/xtask/src/tasks/workflows/autofix_pr.rs b/tooling/xtask/src/tasks/workflows/autofix_pr.rs index 44dd0f9ea9b840163767e15b973192e72b57f4a8..ab59e735225dfb4f9658960a35a992553642b4c2 100644 --- a/tooling/xtask/src/tasks/workflows/autofix_pr.rs +++ b/tooling/xtask/src/tasks/workflows/autofix_pr.rs @@ -18,6 +18,12 @@ pub fn autofix_pr() -> Workflow { .add_input(pr_number.name, pr_number.input()) .add_input(run_clippy.name, run_clippy.input()), )) + .concurrency( + Concurrency::new(Expression::new(format!( + "${{{{ github.workflow }}}}-{pr_number}" + ))) + .cancel_in_progress(true), + ) .add_job(run_autofix.name.clone(), run_autofix.job) .add_job(commit_changes.name, commit_changes.job) } @@ -103,19 +109,6 @@ fn run_autofix(pr_number: &WorkflowInput, run_clippy: &WorkflowInput) -> NamedJo } fn commit_changes(pr_number: &WorkflowInput, autofix_job: &NamedJob) -> NamedJob { - fn authenticate_as_zippy() -> (Step, StepOutput) { - let step = named::uses( - "actions", - "create-github-app-token", - "bef1eaf1c0ac2b148ee2a0a74c65fbe6db0631f1", - ) - .add_with(("app-id", vars::ZED_ZIPPY_APP_ID)) - .add_with(("private-key", vars::ZED_ZIPPY_APP_PRIVATE_KEY)) - .id("get-app-token"); - let output = StepOutput::new(&step, "token"); - (step, output) - } - fn checkout_pr(pr_number: &WorkflowInput, token: &StepOutput) -> Step { named::bash(&format!("gh pr checkout {pr_number}")).add_env(("GITHUB_TOKEN", token)) } @@ -142,7 +135,7 @@ fn commit_changes(pr_number: &WorkflowInput, autofix_job: &NamedJob) -> NamedJob .add_env(("GITHUB_TOKEN", token)) } - let (authenticate, token) = authenticate_as_zippy(); + let (authenticate, token) = steps::authenticate_as_zippy(); named::job( Job::default() diff --git a/tooling/xtask/src/tasks/workflows/cherry_pick.rs b/tooling/xtask/src/tasks/workflows/cherry_pick.rs index 105bf74c4194a46ad4ca62991fae3a945eea150d..eaa786837f84ebf4d4f7e1a579db0c7b4dcc5040 100644 --- a/tooling/xtask/src/tasks/workflows/cherry_pick.rs +++ b/tooling/xtask/src/tasks/workflows/cherry_pick.rs @@ -3,7 +3,7 @@ use gh_workflow::*; use crate::tasks::workflows::{ runners, steps::{self, NamedJob, named}, - vars::{self, StepOutput, WorkflowInput}, + vars::{StepOutput, WorkflowInput}, }; pub fn cherry_pick() -> Workflow { @@ -29,19 +29,6 @@ fn run_cherry_pick( commit: &WorkflowInput, channel: &WorkflowInput, ) -> NamedJob { - fn authenticate_as_zippy() -> (Step, StepOutput) { - let step = named::uses( - "actions", - "create-github-app-token", - "bef1eaf1c0ac2b148ee2a0a74c65fbe6db0631f1", - ) // v2 - .add_with(("app-id", vars::ZED_ZIPPY_APP_ID)) - .add_with(("private-key", vars::ZED_ZIPPY_APP_PRIVATE_KEY)) - .id("get-app-token"); - let output = StepOutput::new(&step, "token"); - (step, output) - } - fn cherry_pick( branch: &WorkflowInput, commit: &WorkflowInput, @@ -54,7 +41,7 @@ fn run_cherry_pick( .add_env(("GITHUB_TOKEN", token)) } - let (authenticate, token) = authenticate_as_zippy(); + let (authenticate, token) = steps::authenticate_as_zippy(); named::job( Job::default() diff --git a/tooling/xtask/src/tasks/workflows/release.rs b/tooling/xtask/src/tasks/workflows/release.rs index e06a71340192c036d442d65d9572e52ed2983cae..80fb075f7f6445b1a6a078d9defba2018a406851 100644 --- a/tooling/xtask/src/tasks/workflows/release.rs +++ b/tooling/xtask/src/tasks/workflows/release.rs @@ -97,17 +97,20 @@ pub(crate) fn create_sentry_release() -> Step { } fn auto_release_preview(deps: &[&NamedJob; 1]) -> NamedJob { + let (authenticate, token) = steps::authenticate_as_zippy(); + named::job( dependant_job(deps) .runs_on(runners::LINUX_SMALL) .cond(Expression::new(indoc::indoc!( r#"startsWith(github.ref, 'refs/tags/v') && endsWith(github.ref, '-pre') && !endsWith(github.ref, '.0-pre')"# ))) + .add_step(authenticate) .add_step( steps::script( r#"gh release edit "$GITHUB_REF_NAME" --repo=zed-industries/zed --draft=false"#, ) - .add_env(("GITHUB_TOKEN", vars::GITHUB_TOKEN)), + .add_env(("GITHUB_TOKEN", &token)), ) ) } diff --git a/tooling/xtask/src/tasks/workflows/run_tests.rs b/tooling/xtask/src/tasks/workflows/run_tests.rs index 0bb3e152fb390e044ebac456fd3347707c66f612..aceb575b647e7ea0b2d8a74da9fbc153767d149d 100644 --- a/tooling/xtask/src/tasks/workflows/run_tests.rs +++ b/tooling/xtask/src/tasks/workflows/run_tests.rs @@ -236,11 +236,11 @@ fn check_style() -> NamedJob { .add_step(steps::checkout_repo()) .add_step(steps::cache_rust_dependencies_namespace()) .add_step(steps::setup_pnpm()) - .add_step(steps::script("./script/prettier")) + .add_step(steps::prettier()) + .add_step(steps::cargo_fmt()) .add_step(steps::script("./script/check-todos")) .add_step(steps::script("./script/check-keymaps")) - .add_step(check_for_typos()) - .add_step(steps::cargo_fmt()), + .add_step(check_for_typos()), ) } @@ -448,6 +448,7 @@ fn check_docs() -> NamedJob { lychee_link_check("./docs/src/**/*"), // check markdown links ) .map(steps::install_linux_dependencies) + .add_step(steps::script("./script/generate-action-metadata")) .add_step(install_mdbook()) .add_step(build_docs()) .add_step( diff --git a/tooling/xtask/src/tasks/workflows/steps.rs b/tooling/xtask/src/tasks/workflows/steps.rs index 7d55df2db433d6e6eae96a5ae62a0c033689d904..a0b071cd6c31654b42adddbba47dd24c60da7df2 100644 --- a/tooling/xtask/src/tasks/workflows/steps.rs +++ b/tooling/xtask/src/tasks/workflows/steps.rs @@ -54,6 +54,10 @@ pub fn setup_sentry() -> Step { .add_with(("token", vars::SENTRY_AUTH_TOKEN)) } +pub fn prettier() -> Step { + named::bash("./script/prettier") +} + pub fn cargo_fmt() -> Step { named::bash("cargo fmt --all -- --check") } @@ -344,3 +348,16 @@ pub fn git_checkout(ref_name: &dyn std::fmt::Display) -> Step { "git fetch origin {ref_name} && git checkout {ref_name}" )) } + +pub fn authenticate_as_zippy() -> (Step, StepOutput) { + let step = named::uses( + "actions", + "create-github-app-token", + "bef1eaf1c0ac2b148ee2a0a74c65fbe6db0631f1", + ) + .add_with(("app-id", vars::ZED_ZIPPY_APP_ID)) + .add_with(("private-key", vars::ZED_ZIPPY_APP_PRIVATE_KEY)) + .id("get-app-token"); + let output = StepOutput::new(&step, "token"); + (step, output) +}