diff --git a/.github/actions/check_style/action.yml b/.github/actions/check_style/action.yml index 25020e4e4c5399026c1ab32622903a3779ba86b2..74bb871bf58fe222c150754c88440d1667b0aef1 100644 --- a/.github/actions/check_style/action.yml +++ b/.github/actions/check_style/action.yml @@ -15,6 +15,7 @@ runs: # will have more fixes & suppression for the standard lint set run: | cargo clippy --workspace --all-features --all-targets -- -A clippy::all -D clippy::dbg_macro -D clippy::todo + cargo clippy -p gpui - name: Find modified migrations shell: bash -euxo pipefail {0} diff --git a/.github/actions/run_tests/action.yml b/.github/actions/run_tests/action.yml index af37af7fc429cc9311d033de6b22016ff1d3e24f..4e6664a6fb6fa2ab810adeae478885cdeb9b3519 100644 --- a/.github/actions/run_tests/action.yml +++ b/.github/actions/run_tests/action.yml @@ -18,10 +18,6 @@ runs: shell: bash -euxo pipefail {0} run: script/clear-target-dir-if-larger-than 100 - - name: Run check - shell: bash -euxo pipefail {0} - run: cargo check --tests --workspace - - name: Run tests shell: bash -euxo pipefail {0} run: cargo nextest run --workspace --no-fail-fast diff --git a/.github/workflows/randomized_tests.yml b/.github/workflows/randomized_tests.yml index a1704d58bdcffec9ce51779bf9af47b8be7fec13..a5e2d983edb5f32d43d913824c1f80d34cbdf1da 100644 --- a/.github/workflows/randomized_tests.yml +++ b/.github/workflows/randomized_tests.yml @@ -3,36 +3,35 @@ name: Randomized Tests concurrency: randomized-tests on: - push: - branches: - - randomized-tests-runner - # schedule: - # - cron: '0 * * * *' + push: + branches: + - randomized-tests-runner + # schedule: + # - cron: '0 * * * *' env: - CARGO_TERM_COLOR: always - CARGO_INCREMENTAL: 0 - RUST_BACKTRACE: 1 - ZED_SERVER_URL: https://zed.dev - ZED_CLIENT_SECRET_TOKEN: ${{ secrets.ZED_CLIENT_SECRET_TOKEN }} + CARGO_TERM_COLOR: always + CARGO_INCREMENTAL: 0 + RUST_BACKTRACE: 1 + ZED_SERVER_URL: https://zed.dev jobs: - tests: - name: Run randomized tests - runs-on: - - self-hosted - - randomized-tests - steps: - - name: Install Node - uses: actions/setup-node@v3 - with: - node-version: "18" + tests: + name: Run randomized tests + runs-on: + - self-hosted + - randomized-tests + steps: + - name: Install Node + uses: actions/setup-node@v3 + with: + node-version: "18" - - name: Checkout repo - uses: actions/checkout@v3 - with: - clean: false - submodules: "recursive" + - name: Checkout repo + uses: actions/checkout@v3 + with: + clean: false + submodules: "recursive" - - name: Run randomized tests - run: script/randomized-test-ci + - name: Run randomized tests + run: script/randomized-test-ci diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000000000000000000000000000000000000..57e3cc7c59ff77a09061ba9e28ebaabf593d9c11 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,3 @@ +# Code of Conduct + +The Code of Conduct for this repository can be found online at [zed.dev/docs/code-of-conduct](https://zed.dev/docs/code-of-conduct). diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a85be833214d83d9cd6dcb24d6a41d3974a56595..eeac6835e79a102e4088977325b53e2f4cce0659 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,25 +1,44 @@ -# CONTRIBUTING +# Contributing to Zed Thanks for your interest in contributing to Zed, the collaborative platform that is also a code editor! -We want to ensure that no one ends up spending time on a pull request that may not be accepted, so we ask that you discuss your ideas with the team and community before starting on a contribution. +We want to avoid anyone spending time on a pull request that may not be accepted, so we suggest you discuss your ideas with the team and community before starting on major changes. Bug fixes, however, are almost always welcome. -All activity in Zed communities is subject to our [Code of Conduct](https://docs.zed.dev/community/code-of-conduct). Contributors to Zed must sign our Contributor License Agreement (link coming soon) before their contributions can be merged. +All activity in Zed forums is subject to our [Code of Conduct](https://docs.zed.dev/community/code-of-conduct). Additionally, contributors must sign our [Contributor License Agreement](https://zed.dev/cla) before their contributions can be merged. ## Contribution ideas -If you already have an idea of what you'd like to contribute, you can skip this section, otherwise, here are a few resources to help you find something to work on: +If you're looking for ideas about what to work on, check out: -- Our public roadmap (link coming soon!) details what features we plan to add to Zed. -- Our [Top-Ranking Issues issue](https://github.com/zed-industries/community/issues/52) shows the most popular feature requests and issues, as voted on by the community. +- Our public roadmap (link coming soon!) contains a rough outline of our near-term priorities for Zed. +- Our [top-ranking issues](https://github.com/zed-industries/community/issues/52) based on votes by the community. -At the moment, we are generally not looking to extend Zed's language or theme support by directly adding these features to Zed - we really want to build a plugin system to handle making the editor extensible going forward. +Outside of a handful of extremely popular languages and themes, we are generally not looking to extend Zed's language or theme support by directly building them into Zed. We really want to build a plugin system to handle making the editor extensible going forward. If you are passionate about shipping new languages or themes we suggest contributing to the extension system to help us get there faster. -If you are passionate about shipping new languages or themes we suggest contributing to the extension system to help us get there faster. +## Proposing changes -## Resources +The best way to propose a change is to [start a discussion on our GitHub repository](https://github.com/zed-industries/zed/discussions). -### Bird-eye's view of Zed +First, write a short **problem statement**, which *clearly* and *briefly* describes the problem you want to solve independently from any specific solution. It doesn't need to be long or formal, but it's difficult to consider a solution in absence of a clear understanding of the problem. + +Next, write a short **solution proposal**. How can the problem (or set of problems) you have stated above be addressed? What are the pros and cons of your approach? Again, keep it brief and informal. This isn't a specification, but rather a starting point for a conversation. + +By effectively engaging with the Zed team and community early in your process, we're better positioned to give you feedback and understand your pull request once you open it. If the first thing we see from you is a big changeset, we're much less likely to respond to it in a timely manner. + +## Pair programming + +We plan to set aside time each week to pair program with contributors on promising pull requests in Zed. This will be an experiment. We tend to prefer pairing over async code review on our team, and we'd like to see how well it works in an open source setting. If we're finding it difficult to get on the same page with async review, we may ask you to pair with us if you're open to it. The closer a contribution is to the goals outlined in our roadmap, the more likely we'll be to spend time pairing on it. + +## Tips to improve the chances of your PR getting reviewed and merged + +- Discuss your plans ahead of time with the team +- Small, focused, incremental pull requests are much easier to review +- Spend time explaining your changes in the pull request body +- Add test coverage and documentation +- Choose tasks that align with our roadmap +- Pair with us and watch us code to learn the codebase + +## Bird-eye's view of Zed Zed is made up of several smaller crates - let's go over those you're most likely to interact with: @@ -34,24 +53,3 @@ Zed is made up of several smaller crates - let's go over those you're most likel - [rpc](/crates/rpc) defines messages to be exchanged with collaboration server. - [theme](/crates/theme) defines the theme system and provides a default theme. - [ui](/crates/ui) is a collection of UI components and common patterns used throughout Zed. - -### Proposal & Discussion - -Before starting on a contribution, we ask that you look to see if there is any existing PRs, or in-Zed discussions about the thing you want to implement. If there is no existing work, find a public channel that is relevant to your contribution, check the channel notes to see which Zed team members typically work in that channel, and post a message in the chat. If you're not sure which channel is best, you can start a discussion, ask a team member or another contributor. - -*Please remember contributions not discussed with the team ahead of time likely have a lower chance of being merged or looked at in a timely manner.* - -## Implementation & Help - -When you start working on your contribution if you find you are struggling with something specific feel free to reach out to the team for help. - -Remember the team is more likely to be available to help if you have already discussed your contribution or are working on something that is higher priority, like something on the roadmap or a top-ranking issue. - -We're happy to pair with you to help you learn the codebase and get your contribution merged. - -**Zed makes heavy use of unit and integration testing, it is highly likely that contributions without any unit tests will be rejected** - -Reviewing code in a pull request, after the fact, is hard and tedious - the team generally likes to build trust and review code through pair programming. -We'd prefer have conversations about the code, through Zed, while it is being written, so decisions can be made in real-time and less time is spent on fixing things after the fact. Ideally, GitHub is only used to merge code that has already been discussed and reviewed in Zed. - -Remember that smaller, incremental PRs are easier to review and merge than large PRs. diff --git a/Cargo.lock b/Cargo.lock index 04134707439abe48ac4b6547329d14cd3d678ddf..1be9e828e656ede4c643858c06ec71d552a49585 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1452,7 +1452,7 @@ dependencies = [ [[package]] name = "collab" -version = "0.38.0" +version = "0.40.1" dependencies = [ "anyhow", "async-trait", @@ -1463,6 +1463,7 @@ dependencies = [ "base64 0.13.1", "call", "channel", + "chrono", "clap 3.2.25", "client", "clock", diff --git a/Cargo.toml b/Cargo.toml index 79d28821d4b040f35fc1ab1dd391cddeee93bc47..eea2b4fb47711f1783a3dc4fed972846ce1b0f69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -93,6 +93,7 @@ resolver = "2" anyhow = { version = "1.0.57" } async-trait = { version = "0.1" } async-compression = { version = "0.4", features = ["gzip", "futures-io"] } +chrono = { version = "0.4", features = ["serde"] } ctor = "0.2.6" derive_more = { version = "0.99.17" } env_logger = { version = "0.9" } diff --git a/LICENSE-AGPL b/LICENSE-AGPL new file mode 100644 index 0000000000000000000000000000000000000000..66a5b0854f2d8ff8da74ff353db52e87c0b4a370 --- /dev/null +++ b/LICENSE-AGPL @@ -0,0 +1,788 @@ +Copyright 2022 - 2024 Zed Industries, Inc. + + + + +This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. +This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. +You should have received a copy of the GNU Affero General Public License along with this program. If not, see . + + + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 2007 + + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + Preamble + + + The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +our General Public Licenses are intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. + + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + + Developers that use our General Public Licenses protect your rights +with two steps: (1) assert copyright on the software, and (2) offer +you this License which gives you legal permission to copy, distribute +and/or modify the software. + + + A secondary benefit of defending all users' freedom is that +improvements made in alternate versions of the program, if they +receive widespread use, become available for other developers to +incorporate. Many developers of free software are heartened and +encouraged by the resulting cooperation. However, in the case of +software used on network servers, this result may fail to come about. +The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its +source code to the public. + + + The GNU Affero General Public License is designed specifically to +ensure that, in such cases, the modified source code becomes available +to the community. It requires the operator of a network server to +provide the source code of the modified version running there to the +users of that server. Therefore, public use of a modified version, on +a publicly accessible server, gives the public access to the source +code of the modified version. + + + An older license, called the Affero General Public License and +published by Affero, was designed to accomplish similar goals. This is +a different license, not a version of the Affero GPL, but Affero has +released a new version of the Affero GPL which permits relicensing under +this license. + + + The precise terms and conditions for copying, distribution and +modification follow. + + + TERMS AND CONDITIONS + + + 0. Definitions. + + + "This License" refers to version 3 of the GNU Affero General Public License. + + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + + A "covered work" means either the unmodified Program or a work based +on the Program. + + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + + 1. Source Code. + + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + + The Corresponding Source for a work in source code form is that +same work. + + + 2. Basic Permissions. + + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + + 4. Conveying Verbatim Copies. + + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + + 5. Conveying Modified Source Versions. + + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + + 6. Conveying Non-Source Forms. + + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + + 7. Additional Terms. + + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + + 8. Termination. + + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + + 9. Acceptance Not Required for Having Copies. + + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + + 10. Automatic Licensing of Downstream Recipients. + + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + + 11. Patents. + + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + + 12. No Surrender of Others' Freedom. + + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + + 13. Remote Network Interaction; Use with the GNU General Public License. + + + Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users +interacting with it remotely through a computer network (if your version +supports such interaction) an opportunity to receive the Corresponding +Source of your version by providing access to the Corresponding Source +from a network server at no charge, through some standard or customary +means of facilitating copying of software. This Corresponding Source +shall include the Corresponding Source for any work covered by version 3 +of the GNU General Public License that is incorporated pursuant to the +following paragraph. + + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the work with which it is combined will remain governed by version +3 of the GNU General Public License. + + + 14. Revised Versions of this License. + + + The Free Software Foundation may publish revised and/or new versions of +the GNU Affero General Public License from time to time. Such new versions +will be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU Affero General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU Affero General Public License, you may choose any version ever published +by the Free Software Foundation. + + + If the Program specifies that a proxy can decide which future +versions of the GNU Affero General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + + 15. Disclaimer of Warranty. + + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + + 16. Limitation of Liability. + + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + + 17. Interpretation of Sections 15 and 16. + + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + + END OF TERMS AND CONDITIONS + + + How to Apply These Terms to Your New Programs + + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + + Copyright (C) + + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + + +Also add information on how to contact you by electronic and paper mail. + + + If your software can interact with users remotely through a computer +network, you should also make sure that it provides a way for users to +get its source. For example, if your program is a web application, its +interface could display a "Source" link that leads users to an archive +of the code. There are many ways you could offer source, and different +solutions will be better for different programs; see section 13 for the +specific requirements. + + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU AGPL, see +. diff --git a/LICENSE-APACHE b/LICENSE-APACHE new file mode 100644 index 0000000000000000000000000000000000000000..f5f68b62cd2d20b692788f9f19e4f14ee9d7b024 --- /dev/null +++ b/LICENSE-APACHE @@ -0,0 +1,222 @@ +Copyright 2022 - 2024 Zed Industries, Inc. + + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + + http://www.apache.org/licenses/LICENSE-2.0 + + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + + + +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + + 1. Definitions. + + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + + END OF TERMS AND CONDITIONS diff --git a/LICENSE-GPL b/LICENSE-GPL new file mode 100644 index 0000000000000000000000000000000000000000..e62ec04cdeece724caeeeeaeb6ae1f6af1bb6b9a --- /dev/null +++ b/LICENSE-GPL @@ -0,0 +1,674 @@ +GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/README.md b/README.md index 737615623194a958f4d8b8d129dfab50475e2e8b..aa325db454b99ae67e89700a8bdf21ee3b3c10a6 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,3 @@ -# 🚧 TODO 🚧 - -- [ ] Add intro -- [ ] Add link to contributing guide -- [ ] Add barebones running zed from source instructions -- [ ] Link out to further dev docs - # Zed [![CI](https://github.com/zed-industries/zed/actions/workflows/ci.yml/badge.svg)](https://github.com/zed-industries/zed/actions/workflows/ci.yml) @@ -16,7 +9,11 @@ Welcome to Zed, a high-performance, multiplayer code editor from the creators of - [Building Zed](./docs/src/developing_zed__building_zed.md) - [Running Collaboration Locally](./docs/src/developing_zed__local_collaboration.md) -### Licensing +## Contributing + +See [CONTRIBUTING.md](./CONTRIBUTING.md) for ways you can contribute to Zed. + +## Licensing License information for third party dependencies must be correctly provided for CI to pass. diff --git a/assets/fonts/plex/IBMPlexSans-Bold.ttf b/assets/fonts/plex/IBMPlexSans-Bold.ttf deleted file mode 100644 index 72b190a849c9c275b44de7466c5bbd101e4e0403..0000000000000000000000000000000000000000 Binary files a/assets/fonts/plex/IBMPlexSans-Bold.ttf and /dev/null differ diff --git a/assets/fonts/plex/IBMPlexSans-Italic.ttf b/assets/fonts/plex/IBMPlexSans-Italic.ttf deleted file mode 100644 index a997a20562517421ed0b19885bca198a86f9597c..0000000000000000000000000000000000000000 Binary files a/assets/fonts/plex/IBMPlexSans-Italic.ttf and /dev/null differ diff --git a/assets/fonts/plex/IBMPlexSans-Regular.ttf b/assets/fonts/plex/IBMPlexSans-Regular.ttf deleted file mode 100644 index 9e4b0a754a9e211c119c3727d330f5a46120fc67..0000000000000000000000000000000000000000 Binary files a/assets/fonts/plex/IBMPlexSans-Regular.ttf and /dev/null differ diff --git a/assets/fonts/plex/LICENSE.txt b/assets/fonts/plex/LICENSE.txt deleted file mode 100644 index c35c4c618fab33da8695177b3a6cefe0810b7b28..0000000000000000000000000000000000000000 --- a/assets/fonts/plex/LICENSE.txt +++ /dev/null @@ -1,93 +0,0 @@ -Copyright © 2017 IBM Corp. with Reserved Font Name "Plex" - -This Font Software is licensed under the SIL Open Font License, Version 1.1. - -This license is copied below, and is also available with a FAQ at: http://scripts.sil.org/OFL - - ------------------------------------------------------------ -SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 ------------------------------------------------------------ - -PREAMBLE -The goals of the Open Font License (OFL) are to stimulate worldwide -development of collaborative font projects, to support the font creation -efforts of academic and linguistic communities, and to provide a free and -open framework in which fonts may be shared and improved in partnership -with others. - -The OFL allows the licensed fonts to be used, studied, modified and -redistributed freely as long as they are not sold by themselves. The -fonts, including any derivative works, can be bundled, embedded, -redistributed and/or sold with any software provided that any reserved -names are not used by derivative works. The fonts and derivatives, -however, cannot be released under any other type of license. The -requirement for fonts to remain under this license does not apply -to any document created using the fonts or their derivatives. - -DEFINITIONS -"Font Software" refers to the set of files released by the Copyright -Holder(s) under this license and clearly marked as such. This may -include source files, build scripts and documentation. - -"Reserved Font Name" refers to any names specified as such after the -copyright statement(s). - -"Original Version" refers to the collection of Font Software components as -distributed by the Copyright Holder(s). - -"Modified Version" refers to any derivative made by adding to, deleting, -or substituting -- in part or in whole -- any of the components of the -Original Version, by changing formats or by porting the Font Software to a -new environment. - -"Author" refers to any designer, engineer, programmer, technical -writer or other person who contributed to the Font Software. - -PERMISSION & CONDITIONS -Permission is hereby granted, free of charge, to any person obtaining -a copy of the Font Software, to use, study, copy, merge, embed, modify, -redistribute, and sell modified and unmodified copies of the Font -Software, subject to the following conditions: - -1) Neither the Font Software nor any of its individual components, -in Original or Modified Versions, may be sold by itself. - -2) Original or Modified Versions of the Font Software may be bundled, -redistributed and/or sold with any software, provided that each copy -contains the above copyright notice and this license. These can be -included either as stand-alone text files, human-readable headers or -in the appropriate machine-readable metadata fields within text or -binary files as long as those fields can be easily viewed by the user. - -3) No Modified Version of the Font Software may use the Reserved Font -Name(s) unless explicit written permission is granted by the corresponding -Copyright Holder. This restriction only applies to the primary font name as -presented to the users. - -4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font -Software shall not be used to promote, endorse or advertise any -Modified Version, except to acknowledge the contribution(s) of the -Copyright Holder(s) and the Author(s) or with their explicit written -permission. - -5) The Font Software, modified or unmodified, in part or in whole, -must be distributed entirely under this license, and must not be -distributed under any other license. The requirement for fonts to -remain under this license does not apply to any document created -using the Font Software. - -TERMINATION -This license becomes null and void if any of the above conditions are -not met. - -DISCLAIMER -THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT -OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE -COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL -DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM -OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/assets/keymaps/jetbrains.json b/assets/keymaps/jetbrains.json index b2ed144a3f364822fd5e0375a75e07bb50d0ab20..6be7289d8aeefc4dd837a90adfb576d3006efb16 100644 --- a/assets/keymaps/jetbrains.json +++ b/assets/keymaps/jetbrains.json @@ -79,5 +79,11 @@ "cmd-1": "workspace::ToggleLeftDock", "cmd-6": "diagnostics::Deploy" } + }, + { + "context": "ProjectPanel", + "bindings": { + "enter": "project_panel::Open" + } } ] diff --git a/assets/screenshots/staff_usage_of_channels.png b/assets/screenshots/staff_usage_of_channels.png deleted file mode 100644 index b9e607e59cfdd31c9477756e1f40283dde9ccbe0..0000000000000000000000000000000000000000 Binary files a/assets/screenshots/staff_usage_of_channels.png and /dev/null differ diff --git a/crates/activity_indicator/Cargo.toml b/crates/activity_indicator/Cargo.toml index 55672d095697eec2880c32dcf7ed4e5bbfd05735..45d275eed76d33f4c4e09b56b38f20f9f2f47f5d 100644 --- a/crates/activity_indicator/Cargo.toml +++ b/crates/activity_indicator/Cargo.toml @@ -3,6 +3,8 @@ name = "activity_indicator" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/activity_indicator.rs" diff --git a/crates/activity_indicator/LICENSE-GPL b/crates/activity_indicator/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/activity_indicator/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/ai/Cargo.toml b/crates/ai/Cargo.toml index 6516e07cd43e402b78998330cd7f536f958f5bc5..62df8998c95c7105304ad86a4647b712075695db 100644 --- a/crates/ai/Cargo.toml +++ b/crates/ai/Cargo.toml @@ -3,6 +3,8 @@ name = "ai" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/ai.rs" diff --git a/crates/ai/LICENSE-GPL b/crates/ai/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/ai/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/ai/src/auth.rs b/crates/ai/src/auth.rs index 1ea49bd615999a7f0318d3e205d3f86cee9c64a8..62556d718360a92bdd21cd20155c2ea8ca5e5c57 100644 --- a/crates/ai/src/auth.rs +++ b/crates/ai/src/auth.rs @@ -1,3 +1,4 @@ +use futures::future::BoxFuture; use gpui::AppContext; #[derive(Clone, Debug)] @@ -9,7 +10,14 @@ pub enum ProviderCredential { pub trait CredentialProvider: Send + Sync { fn has_credentials(&self) -> bool; - fn retrieve_credentials(&self, cx: &mut AppContext) -> ProviderCredential; - fn save_credentials(&self, cx: &mut AppContext, credential: ProviderCredential); - fn delete_credentials(&self, cx: &mut AppContext); + #[must_use] + fn retrieve_credentials(&self, cx: &mut AppContext) -> BoxFuture; + #[must_use] + fn save_credentials( + &self, + cx: &mut AppContext, + credential: ProviderCredential, + ) -> BoxFuture<()>; + #[must_use] + fn delete_credentials(&self, cx: &mut AppContext) -> BoxFuture<()>; } diff --git a/crates/ai/src/providers/open_ai/completion.rs b/crates/ai/src/providers/open_ai/completion.rs index 0e325ee62489b41fb599a75b7b64efcbe864d269..aa5895011323e67ea4618bbb2fb4164737dc53a2 100644 --- a/crates/ai/src/providers/open_ai/completion.rs +++ b/crates/ai/src/providers/open_ai/completion.rs @@ -201,8 +201,10 @@ pub struct OpenAICompletionProvider { } impl OpenAICompletionProvider { - pub fn new(model_name: &str, executor: BackgroundExecutor) -> Self { - let model = OpenAILanguageModel::load(model_name); + pub async fn new(model_name: String, executor: BackgroundExecutor) -> Self { + let model = executor + .spawn(async move { OpenAILanguageModel::load(&model_name) }) + .await; let credential = Arc::new(RwLock::new(ProviderCredential::NoCredentials)); Self { model, @@ -220,45 +222,70 @@ impl CredentialProvider for OpenAICompletionProvider { } } - fn retrieve_credentials(&self, cx: &mut AppContext) -> ProviderCredential { + fn retrieve_credentials(&self, cx: &mut AppContext) -> BoxFuture { let existing_credential = self.credential.read().clone(); let retrieved_credential = match existing_credential { - ProviderCredential::Credentials { .. } => existing_credential.clone(), + ProviderCredential::Credentials { .. } => { + return async move { existing_credential }.boxed() + } _ => { if let Some(api_key) = env::var("OPENAI_API_KEY").log_err() { - ProviderCredential::Credentials { api_key } - } else if let Some(Some((_, api_key))) = - cx.read_credentials(OPENAI_API_URL).log_err() - { - if let Some(api_key) = String::from_utf8(api_key).log_err() { - ProviderCredential::Credentials { api_key } - } else { - ProviderCredential::NoCredentials - } + async move { ProviderCredential::Credentials { api_key } }.boxed() } else { - ProviderCredential::NoCredentials + let credentials = cx.read_credentials(OPENAI_API_URL); + async move { + if let Some(Some((_, api_key))) = credentials.await.log_err() { + if let Some(api_key) = String::from_utf8(api_key).log_err() { + ProviderCredential::Credentials { api_key } + } else { + ProviderCredential::NoCredentials + } + } else { + ProviderCredential::NoCredentials + } + } + .boxed() } } }; - *self.credential.write() = retrieved_credential.clone(); - retrieved_credential + + async move { + let retrieved_credential = retrieved_credential.await; + *self.credential.write() = retrieved_credential.clone(); + retrieved_credential + } + .boxed() } - fn save_credentials(&self, cx: &mut AppContext, credential: ProviderCredential) { + fn save_credentials( + &self, + cx: &mut AppContext, + credential: ProviderCredential, + ) -> BoxFuture<()> { *self.credential.write() = credential.clone(); let credential = credential.clone(); - match credential { + let write_credentials = match credential { ProviderCredential::Credentials { api_key } => { - cx.write_credentials(OPENAI_API_URL, "Bearer", api_key.as_bytes()) - .log_err(); + Some(cx.write_credentials(OPENAI_API_URL, "Bearer", api_key.as_bytes())) + } + _ => None, + }; + + async move { + if let Some(write_credentials) = write_credentials { + write_credentials.await.log_err(); } - _ => {} } + .boxed() } - fn delete_credentials(&self, cx: &mut AppContext) { - cx.delete_credentials(OPENAI_API_URL).log_err(); + fn delete_credentials(&self, cx: &mut AppContext) -> BoxFuture<()> { *self.credential.write() = ProviderCredential::NoCredentials; + let delete_credentials = cx.delete_credentials(OPENAI_API_URL); + async move { + delete_credentials.await.log_err(); + } + .boxed() } } diff --git a/crates/ai/src/providers/open_ai/embedding.rs b/crates/ai/src/providers/open_ai/embedding.rs index 0a9b6ba969c7c519d337ae27db45af12252efa0b..89aebb1b7616643c683cdd419ec0ebd1d51b5142 100644 --- a/crates/ai/src/providers/open_ai/embedding.rs +++ b/crates/ai/src/providers/open_ai/embedding.rs @@ -1,6 +1,8 @@ use anyhow::{anyhow, Result}; use async_trait::async_trait; +use futures::future::BoxFuture; use futures::AsyncReadExt; +use futures::FutureExt; use gpui::AppContext; use gpui::BackgroundExecutor; use isahc::http::StatusCode; @@ -67,11 +69,14 @@ struct OpenAIEmbeddingUsage { } impl OpenAIEmbeddingProvider { - pub fn new(client: Arc, executor: BackgroundExecutor) -> Self { + pub async fn new(client: Arc, executor: BackgroundExecutor) -> Self { let (rate_limit_count_tx, rate_limit_count_rx) = watch::channel_with(None); let rate_limit_count_tx = Arc::new(Mutex::new(rate_limit_count_tx)); - let model = OpenAILanguageModel::load("text-embedding-ada-002"); + // Loading the model is expensive, so ensure this runs off the main thread. + let model = executor + .spawn(async move { OpenAILanguageModel::load("text-embedding-ada-002") }) + .await; let credential = Arc::new(RwLock::new(ProviderCredential::NoCredentials)); OpenAIEmbeddingProvider { @@ -154,46 +159,71 @@ impl CredentialProvider for OpenAIEmbeddingProvider { _ => false, } } - fn retrieve_credentials(&self, cx: &mut AppContext) -> ProviderCredential { - let existing_credential = self.credential.read().clone(); + fn retrieve_credentials(&self, cx: &mut AppContext) -> BoxFuture { + let existing_credential = self.credential.read().clone(); let retrieved_credential = match existing_credential { - ProviderCredential::Credentials { .. } => existing_credential.clone(), + ProviderCredential::Credentials { .. } => { + return async move { existing_credential }.boxed() + } _ => { if let Some(api_key) = env::var("OPENAI_API_KEY").log_err() { - ProviderCredential::Credentials { api_key } - } else if let Some(Some((_, api_key))) = - cx.read_credentials(OPENAI_API_URL).log_err() - { - if let Some(api_key) = String::from_utf8(api_key).log_err() { - ProviderCredential::Credentials { api_key } - } else { - ProviderCredential::NoCredentials - } + async move { ProviderCredential::Credentials { api_key } }.boxed() } else { - ProviderCredential::NoCredentials + let credentials = cx.read_credentials(OPENAI_API_URL); + async move { + if let Some(Some((_, api_key))) = credentials.await.log_err() { + if let Some(api_key) = String::from_utf8(api_key).log_err() { + ProviderCredential::Credentials { api_key } + } else { + ProviderCredential::NoCredentials + } + } else { + ProviderCredential::NoCredentials + } + } + .boxed() } } }; - *self.credential.write() = retrieved_credential.clone(); - retrieved_credential + async move { + let retrieved_credential = retrieved_credential.await; + *self.credential.write() = retrieved_credential.clone(); + retrieved_credential + } + .boxed() } - fn save_credentials(&self, cx: &mut AppContext, credential: ProviderCredential) { + fn save_credentials( + &self, + cx: &mut AppContext, + credential: ProviderCredential, + ) -> BoxFuture<()> { *self.credential.write() = credential.clone(); - match credential { + let credential = credential.clone(); + let write_credentials = match credential { ProviderCredential::Credentials { api_key } => { - cx.write_credentials(OPENAI_API_URL, "Bearer", api_key.as_bytes()) - .log_err(); + Some(cx.write_credentials(OPENAI_API_URL, "Bearer", api_key.as_bytes())) + } + _ => None, + }; + + async move { + if let Some(write_credentials) = write_credentials { + write_credentials.await.log_err(); } - _ => {} } + .boxed() } - fn delete_credentials(&self, cx: &mut AppContext) { - cx.delete_credentials(OPENAI_API_URL).log_err(); + fn delete_credentials(&self, cx: &mut AppContext) -> BoxFuture<()> { *self.credential.write() = ProviderCredential::NoCredentials; + let delete_credentials = cx.delete_credentials(OPENAI_API_URL); + async move { + delete_credentials.await.log_err(); + } + .boxed() } } diff --git a/crates/ai/src/test.rs b/crates/ai/src/test.rs index 3d59febbe912b42f7be11625ff0fbc2952184291..89edc71b0bc51f4d08c8f5fba8c3621ddb6ce000 100644 --- a/crates/ai/src/test.rs +++ b/crates/ai/src/test.rs @@ -104,11 +104,22 @@ impl CredentialProvider for FakeEmbeddingProvider { fn has_credentials(&self) -> bool { true } - fn retrieve_credentials(&self, _cx: &mut AppContext) -> ProviderCredential { - ProviderCredential::NotNeeded + + fn retrieve_credentials(&self, _cx: &mut AppContext) -> BoxFuture { + async { ProviderCredential::NotNeeded }.boxed() + } + + fn save_credentials( + &self, + _cx: &mut AppContext, + _credential: ProviderCredential, + ) -> BoxFuture<()> { + async {}.boxed() + } + + fn delete_credentials(&self, _cx: &mut AppContext) -> BoxFuture<()> { + async {}.boxed() } - fn save_credentials(&self, _cx: &mut AppContext, _credential: ProviderCredential) {} - fn delete_credentials(&self, _cx: &mut AppContext) {} } #[async_trait] @@ -165,11 +176,22 @@ impl CredentialProvider for FakeCompletionProvider { fn has_credentials(&self) -> bool { true } - fn retrieve_credentials(&self, _cx: &mut AppContext) -> ProviderCredential { - ProviderCredential::NotNeeded + + fn retrieve_credentials(&self, _cx: &mut AppContext) -> BoxFuture { + async { ProviderCredential::NotNeeded }.boxed() + } + + fn save_credentials( + &self, + _cx: &mut AppContext, + _credential: ProviderCredential, + ) -> BoxFuture<()> { + async {}.boxed() + } + + fn delete_credentials(&self, _cx: &mut AppContext) -> BoxFuture<()> { + async {}.boxed() } - fn save_credentials(&self, _cx: &mut AppContext, _credential: ProviderCredential) {} - fn delete_credentials(&self, _cx: &mut AppContext) {} } impl CompletionProvider for FakeCompletionProvider { diff --git a/crates/assets/Cargo.toml b/crates/assets/Cargo.toml index 7ebae21d7dddb868d85dd9f11ae7fd8a34a277f7..69958e2a746f4a7282a02db86e22a6ef20c7fff5 100644 --- a/crates/assets/Cargo.toml +++ b/crates/assets/Cargo.toml @@ -3,6 +3,8 @@ name = "assets" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/crates/assets/LICENSE-GPL b/crates/assets/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/assets/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/assistant/Cargo.toml b/crates/assistant/Cargo.toml index 9588932c250edf6b7bd4194d2c4cc17ec0108bf5..1b3dde814c2e8f94ebac2ea64be6c574ebf35c3f 100644 --- a/crates/assistant/Cargo.toml +++ b/crates/assistant/Cargo.toml @@ -3,6 +3,8 @@ name = "assistant" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/assistant.rs" @@ -30,7 +32,7 @@ workspace = { path = "../workspace" } uuid.workspace = true log.workspace = true anyhow.workspace = true -chrono = { version = "0.4", features = ["serde"] } +chrono.workspace = true futures.workspace = true indoc.workspace = true isahc.workspace = true diff --git a/crates/assistant/LICENSE-GPL b/crates/assistant/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/assistant/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/assistant/src/assistant_panel.rs b/crates/assistant/src/assistant_panel.rs index 1f57e52032b1e4e76f7297f577d4db82cf970eb3..b2c539fcc21dc1ccb05532e5a5aa7e9ca7012b0a 100644 --- a/crates/assistant/src/assistant_panel.rs +++ b/crates/assistant/src/assistant_panel.rs @@ -6,14 +6,12 @@ use crate::{ NewConversation, QuoteSelection, ResetKey, Role, SavedConversation, SavedConversationMetadata, SavedMessage, Split, ToggleFocus, ToggleIncludeConversation, ToggleRetrieveContext, }; - +use ai::prompts::repository_context::PromptCodeSnippet; use ai::{ auth::ProviderCredential, completion::{CompletionProvider, CompletionRequest}, providers::open_ai::{OpenAICompletionProvider, OpenAIRequest, RequestMessage}, }; - -use ai::prompts::repository_context::PromptCodeSnippet; use anyhow::{anyhow, Result}; use chrono::{DateTime, Local}; use client::telemetry::AssistantKind; @@ -31,9 +29,9 @@ use fs::Fs; use futures::StreamExt; use gpui::{ canvas, div, point, relative, rems, uniform_list, Action, AnyElement, AppContext, - AsyncWindowContext, AvailableSpace, ClipboardItem, Context, EventEmitter, FocusHandle, - FocusableView, FontStyle, FontWeight, HighlightStyle, InteractiveElement, IntoElement, Model, - ModelContext, ParentElement, Pixels, PromptLevel, Render, SharedString, + AsyncAppContext, AsyncWindowContext, AvailableSpace, ClipboardItem, Context, EventEmitter, + FocusHandle, FocusableView, FontStyle, FontWeight, HighlightStyle, InteractiveElement, + IntoElement, Model, ModelContext, ParentElement, Pixels, PromptLevel, Render, SharedString, StatefulInteractiveElement, Styled, Subscription, Task, TextStyle, UniformListScrollHandle, View, ViewContext, VisualContext, WeakModel, WeakView, WhiteSpace, WindowContext, }; @@ -123,6 +121,10 @@ impl AssistantPanel { .await .log_err() .unwrap_or_default(); + // Defaulting currently to GPT4, allow for this to be set via config. + let completion_provider = + OpenAICompletionProvider::new("gpt-4".into(), cx.background_executor().clone()) + .await; // TODO: deserialize state. let workspace_handle = workspace.clone(); @@ -156,11 +158,6 @@ impl AssistantPanel { }); let semantic_index = SemanticIndex::global(cx); - // Defaulting currently to GPT4, allow for this to be set via config. - let completion_provider = Arc::new(OpenAICompletionProvider::new( - "gpt-4", - cx.background_executor().clone(), - )); let focus_handle = cx.focus_handle(); cx.on_focus_in(&focus_handle, Self::focus_in).detach(); @@ -176,7 +173,7 @@ impl AssistantPanel { zoomed: false, focus_handle, toolbar, - completion_provider, + completion_provider: Arc::new(completion_provider), api_key_editor: None, languages: workspace.app_state().languages.clone(), fs: workspace.app_state().fs.clone(), @@ -221,23 +218,9 @@ impl AssistantPanel { _: &InlineAssist, cx: &mut ViewContext, ) { - let this = if let Some(this) = workspace.panel::(cx) { - if this.update(cx, |assistant, cx| { - if !assistant.has_credentials() { - assistant.load_credentials(cx); - }; - - assistant.has_credentials() - }) { - this - } else { - workspace.focus_panel::(cx); - return; - } - } else { + let Some(assistant) = workspace.panel::(cx) else { return; }; - let active_editor = if let Some(active_editor) = workspace .active_item(cx) .and_then(|item| item.act_as::(cx)) @@ -246,12 +229,32 @@ impl AssistantPanel { } else { return; }; + let project = workspace.project().clone(); - let project = workspace.project(); + if assistant.update(cx, |assistant, _| assistant.has_credentials()) { + assistant.update(cx, |assistant, cx| { + assistant.new_inline_assist(&active_editor, cx, &project) + }); + } else { + let assistant = assistant.downgrade(); + cx.spawn(|workspace, mut cx| async move { + assistant + .update(&mut cx, |assistant, cx| assistant.load_credentials(cx))? + .await; + if assistant.update(&mut cx, |assistant, _| assistant.has_credentials())? { + assistant.update(&mut cx, |assistant, cx| { + assistant.new_inline_assist(&active_editor, cx, &project) + })?; + } else { + workspace.update(&mut cx, |workspace, cx| { + workspace.focus_panel::(cx) + })?; + } - this.update(cx, |assistant, cx| { - assistant.new_inline_assist(&active_editor, cx, project) - }); + anyhow::Ok(()) + }) + .detach_and_log_err(cx) + } } fn new_inline_assist( @@ -291,9 +294,6 @@ impl AssistantPanel { let inline_assist_id = post_inc(&mut self.next_inline_assist_id); let provider = self.completion_provider.clone(); - // Retrieve Credentials Authenticates the Provider - provider.retrieve_credentials(cx); - let codegen = cx.new_model(|cx| { Codegen::new(editor.read(cx).buffer().clone(), codegen_kind, provider, cx) }); @@ -846,11 +846,18 @@ impl AssistantPanel { api_key: api_key.clone(), }; - self.completion_provider.save_credentials(cx, credential); + let completion_provider = self.completion_provider.clone(); + cx.spawn(|this, mut cx| async move { + cx.update(|cx| completion_provider.save_credentials(cx, credential))? + .await; - self.api_key_editor.take(); - self.focus_handle.focus(cx); - cx.notify(); + this.update(&mut cx, |this, cx| { + this.api_key_editor.take(); + this.focus_handle.focus(cx); + cx.notify(); + }) + }) + .detach_and_log_err(cx); } } else { cx.propagate(); @@ -858,10 +865,17 @@ impl AssistantPanel { } fn reset_credentials(&mut self, _: &ResetKey, cx: &mut ViewContext) { - self.completion_provider.delete_credentials(cx); - self.api_key_editor = Some(build_api_key_editor(cx)); - self.focus_handle.focus(cx); - cx.notify(); + let completion_provider = self.completion_provider.clone(); + cx.spawn(|this, mut cx| async move { + cx.update(|cx| completion_provider.delete_credentials(cx))? + .await; + this.update(&mut cx, |this, cx| { + this.api_key_editor = Some(build_api_key_editor(cx)); + this.focus_handle.focus(cx); + cx.notify(); + }) + }) + .detach_and_log_err(cx); } fn toggle_zoom(&mut self, _: &workspace::ToggleZoom, cx: &mut ViewContext) { @@ -1079,9 +1093,9 @@ impl AssistantPanel { cx.spawn(|this, mut cx| async move { let saved_conversation = fs.load(&path).await?; let saved_conversation = serde_json::from_str(&saved_conversation)?; - let conversation = cx.new_model(|cx| { - Conversation::deserialize(saved_conversation, path.clone(), languages, cx) - })?; + let conversation = + Conversation::deserialize(saved_conversation, path.clone(), languages, &mut cx) + .await?; this.update(&mut cx, |this, cx| { // If, by the time we've loaded the conversation, the user has already opened // the same conversation, we don't want to open it again. @@ -1108,8 +1122,16 @@ impl AssistantPanel { self.completion_provider.has_credentials() } - fn load_credentials(&mut self, cx: &mut ViewContext) { - self.completion_provider.retrieve_credentials(cx); + fn load_credentials(&mut self, cx: &mut ViewContext) -> Task<()> { + let completion_provider = self.completion_provider.clone(); + cx.spawn(|_, mut cx| async move { + if let Some(retrieve_credentials) = cx + .update(|cx| completion_provider.retrieve_credentials(cx)) + .log_err() + { + retrieve_credentials.await; + } + }) } } @@ -1315,11 +1337,16 @@ impl Panel for AssistantPanel { fn set_active(&mut self, active: bool, cx: &mut ViewContext) { if active { - self.load_credentials(cx); - - if self.editors.is_empty() { - self.new_conversation(cx); - } + let load_credentials = self.load_credentials(cx); + cx.spawn(|this, mut cx| async move { + load_credentials.await; + this.update(&mut cx, |this, cx| { + if this.editors.is_empty() { + this.new_conversation(cx); + } + }) + }) + .detach_and_log_err(cx); } } @@ -1462,21 +1489,25 @@ impl Conversation { } } - fn deserialize( + async fn deserialize( saved_conversation: SavedConversation, path: PathBuf, language_registry: Arc, - cx: &mut ModelContext, - ) -> Self { + cx: &mut AsyncAppContext, + ) -> Result> { let id = match saved_conversation.id { Some(id) => Some(id), None => Some(Uuid::new_v4().to_string()), }; let model = saved_conversation.model; let completion_provider: Arc = Arc::new( - OpenAICompletionProvider::new(model.full_name(), cx.background_executor().clone()), + OpenAICompletionProvider::new( + model.full_name().into(), + cx.background_executor().clone(), + ) + .await, ); - completion_provider.retrieve_credentials(cx); + cx.update(|cx| completion_provider.retrieve_credentials(cx))?; let markdown = language_registry.language_for_name("Markdown"); let mut message_anchors = Vec::new(); let mut next_message_id = MessageId(0); @@ -1499,32 +1530,34 @@ impl Conversation { }) .detach_and_log_err(cx); buffer - }); - - let mut this = Self { - id, - message_anchors, - messages_metadata: saved_conversation.message_metadata, - next_message_id, - summary: Some(Summary { - text: saved_conversation.summary, - done: true, - }), - pending_summary: Task::ready(None), - completion_count: Default::default(), - pending_completions: Default::default(), - token_count: None, - max_token_count: tiktoken_rs::model::get_context_size(&model.full_name()), - pending_token_count: Task::ready(None), - model, - _subscriptions: vec![cx.subscribe(&buffer, Self::handle_buffer_event)], - pending_save: Task::ready(Ok(())), - path: Some(path), - buffer, - completion_provider, - }; - this.count_remaining_tokens(cx); - this + })?; + + cx.new_model(|cx| { + let mut this = Self { + id, + message_anchors, + messages_metadata: saved_conversation.message_metadata, + next_message_id, + summary: Some(Summary { + text: saved_conversation.summary, + done: true, + }), + pending_summary: Task::ready(None), + completion_count: Default::default(), + pending_completions: Default::default(), + token_count: None, + max_token_count: tiktoken_rs::model::get_context_size(&model.full_name()), + pending_token_count: Task::ready(None), + model, + _subscriptions: vec![cx.subscribe(&buffer, Self::handle_buffer_event)], + pending_save: Task::ready(Ok(())), + path: Some(path), + buffer, + completion_provider, + }; + this.count_remaining_tokens(cx); + this + }) } fn handle_buffer_event( @@ -3169,7 +3202,7 @@ mod tests { use super::*; use crate::MessageId; use ai::test::FakeCompletionProvider; - use gpui::AppContext; + use gpui::{AppContext, TestAppContext}; use settings::SettingsStore; #[gpui::test] @@ -3487,16 +3520,17 @@ mod tests { } #[gpui::test] - fn test_serialization(cx: &mut AppContext) { - let settings_store = SettingsStore::test(cx); + async fn test_serialization(cx: &mut TestAppContext) { + let settings_store = cx.update(SettingsStore::test); cx.set_global(settings_store); - init(cx); + cx.update(init); let registry = Arc::new(LanguageRegistry::test()); let completion_provider = Arc::new(FakeCompletionProvider::new()); let conversation = cx.new_model(|cx| Conversation::new(registry.clone(), cx, completion_provider)); - let buffer = conversation.read(cx).buffer.clone(); - let message_0 = conversation.read(cx).message_anchors[0].id; + let buffer = conversation.read_with(cx, |conversation, _| conversation.buffer.clone()); + let message_0 = + conversation.read_with(cx, |conversation, _| conversation.message_anchors[0].id); let message_1 = conversation.update(cx, |conversation, cx| { conversation .insert_message_after(message_0, Role::Assistant, MessageStatus::Done, cx) @@ -3517,9 +3551,9 @@ mod tests { .unwrap() }); buffer.update(cx, |buffer, cx| buffer.undo(cx)); - assert_eq!(buffer.read(cx).text(), "a\nb\nc\n"); + assert_eq!(buffer.read_with(cx, |buffer, _| buffer.text()), "a\nb\nc\n"); assert_eq!( - messages(&conversation, cx), + cx.read(|cx| messages(&conversation, cx)), [ (message_0, Role::User, 0..2), (message_1.id, Role::Assistant, 2..6), @@ -3527,18 +3561,22 @@ mod tests { ] ); - let deserialized_conversation = cx.new_model(|cx| { - Conversation::deserialize( - conversation.read(cx).serialize(cx), - Default::default(), - registry.clone(), - cx, - ) - }); - let deserialized_buffer = deserialized_conversation.read(cx).buffer.clone(); - assert_eq!(deserialized_buffer.read(cx).text(), "a\nb\nc\n"); + let deserialized_conversation = Conversation::deserialize( + conversation.read_with(cx, |conversation, cx| conversation.serialize(cx)), + Default::default(), + registry.clone(), + &mut cx.to_async(), + ) + .await + .unwrap(); + let deserialized_buffer = + deserialized_conversation.read_with(cx, |conversation, _| conversation.buffer.clone()); + assert_eq!( + deserialized_buffer.read_with(cx, |buffer, _| buffer.text()), + "a\nb\nc\n" + ); assert_eq!( - messages(&deserialized_conversation, cx), + cx.read(|cx| messages(&deserialized_conversation, cx)), [ (message_0, Role::User, 0..2), (message_1.id, Role::Assistant, 2..6), diff --git a/crates/audio/Cargo.toml b/crates/audio/Cargo.toml index 8b38cdd10386e24cbdba7b5a4eb53f0383dc9124..2946dfffc5e7d9814c453d9eedd3452e6b4064f8 100644 --- a/crates/audio/Cargo.toml +++ b/crates/audio/Cargo.toml @@ -3,6 +3,8 @@ name = "audio" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/audio.rs" diff --git a/crates/audio/LICENSE-GPL b/crates/audio/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/audio/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/auto_update/Cargo.toml b/crates/auto_update/Cargo.toml index 5f0224aa7b2590cc0bfe4dc0d24a88a73206d879..5f00ca21c6c508b43cf2b57e00e4b7a722b26f47 100644 --- a/crates/auto_update/Cargo.toml +++ b/crates/auto_update/Cargo.toml @@ -3,6 +3,8 @@ name = "auto_update" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/auto_update.rs" diff --git a/crates/auto_update/LICENSE-GPL b/crates/auto_update/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/auto_update/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/auto_update/src/auto_update.rs b/crates/auto_update/src/auto_update.rs index 61fa8dcb75c15341e97ddf36ac2a8855bc96f275..67ca15e295b2f05b2f0481bc6a4ecd3b03014c52 100644 --- a/crates/auto_update/src/auto_update.rs +++ b/crates/auto_update/src/auto_update.rs @@ -1,7 +1,7 @@ mod update_notification; use anyhow::{anyhow, Context, Result}; -use client::{Client, TelemetrySettings, ZED_APP_PATH, ZED_APP_VERSION, ZED_SECRET_CLIENT_TOKEN}; +use client::{Client, TelemetrySettings, ZED_APP_PATH, ZED_APP_VERSION}; use db::kvp::KEY_VALUE_STORE; use db::RELEASE_CHANNEL; use gpui::{ @@ -248,9 +248,7 @@ impl AutoUpdater { ) })?; - let mut url_string = format!( - "{server_url}/api/releases/latest?token={ZED_SECRET_CLIENT_TOKEN}&asset=Zed.dmg" - ); + let mut url_string = format!("{server_url}/api/releases/latest?asset=Zed.dmg"); cx.update(|cx| { if let Some(param) = cx .try_global::() diff --git a/crates/breadcrumbs/Cargo.toml b/crates/breadcrumbs/Cargo.toml index e8663f9bc7c9d26877cb3b8e84b60ffce9ca1fb5..3082b3cbe6db0ce78d6660428d0a0a100b126dea 100644 --- a/crates/breadcrumbs/Cargo.toml +++ b/crates/breadcrumbs/Cargo.toml @@ -3,6 +3,8 @@ name = "breadcrumbs" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/breadcrumbs.rs" diff --git a/crates/breadcrumbs/LICENSE-GPL b/crates/breadcrumbs/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/breadcrumbs/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/call/Cargo.toml b/crates/call/Cargo.toml index 7d200a0d210aba8e8db8bdf463b1db88650d183b..ae19cd333dbb36ca86236388578d1fc23f6f0a31 100644 --- a/crates/call/Cargo.toml +++ b/crates/call/Cargo.toml @@ -3,6 +3,8 @@ name = "call" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/call.rs" diff --git a/crates/call/LICENSE-GPL b/crates/call/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/call/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/channel/Cargo.toml b/crates/channel/Cargo.toml index 6bd177bed573bc8a034dbb50588c77b111422242..1c5f7986a9e4b6308edc07e9b4e7f025528e2d0f 100644 --- a/crates/channel/Cargo.toml +++ b/crates/channel/Cargo.toml @@ -3,6 +3,8 @@ name = "channel" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/channel.rs" diff --git a/crates/channel/LICENSE-GPL b/crates/channel/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/channel/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 2b4a375a5bf9a2e58ffc889f9f7786bb4c35a324..b0feeef6c294c92d00254e7925a122dae617e0b3 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -3,6 +3,8 @@ name = "cli" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/cli.rs" diff --git a/crates/cli/LICENSE-GPL b/crates/cli/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/cli/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml index 03d6c06fe399842cad6a6de6057503d5f43a5bbb..951fb0dd57539098c13cb02a003573d139df0c00 100644 --- a/crates/client/Cargo.toml +++ b/crates/client/Cargo.toml @@ -3,6 +3,8 @@ name = "client" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/client.rs" diff --git a/crates/client/LICENSE-GPL b/crates/client/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/client/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/client/src/client.rs b/crates/client/src/client.rs index 3a48fdba860d1ae9878462a9e31fd4e877e08366..370a2ba6ffdb8ab13d8279c19e9207990b160b20 100644 --- a/crates/client/src/client.rs +++ b/crates/client/src/client.rs @@ -68,7 +68,6 @@ lazy_static! { std::env::var("ZED_ALWAYS_ACTIVE").map_or(false, |e| e.len() > 0); } -pub const ZED_SECRET_CLIENT_TOKEN: &str = "618033988749894"; pub const INITIAL_RECONNECTION_DELAY: Duration = Duration::from_millis(100); pub const CONNECTION_TIMEOUT: Duration = Duration::from_secs(5); @@ -701,8 +700,8 @@ impl Client { } } - pub fn has_keychain_credentials(&self, cx: &AsyncAppContext) -> bool { - read_credentials_from_keychain(cx).is_some() + pub async fn has_keychain_credentials(&self, cx: &AsyncAppContext) -> bool { + read_credentials_from_keychain(cx).await.is_some() } #[async_recursion(?Send)] @@ -733,7 +732,7 @@ impl Client { let mut read_from_keychain = false; let mut credentials = self.state.read().credentials.clone(); if credentials.is_none() && try_keychain { - credentials = read_credentials_from_keychain(cx); + credentials = read_credentials_from_keychain(cx).await; read_from_keychain = credentials.is_some(); } if credentials.is_none() { @@ -771,7 +770,7 @@ impl Client { Ok(conn) => { self.state.write().credentials = Some(credentials.clone()); if !read_from_keychain && IMPERSONATE_LOGIN.is_none() { - write_credentials_to_keychain(credentials, cx).log_err(); + write_credentials_to_keychain(credentials, cx).await.log_err(); } futures::select_biased! { @@ -785,7 +784,7 @@ impl Client { Err(EstablishConnectionError::Unauthorized) => { self.state.write().credentials.take(); if read_from_keychain { - delete_credentials_from_keychain(cx).log_err(); + delete_credentials_from_keychain(cx).await.log_err(); self.set_status(Status::SignedOut, cx); self.authenticate_and_connect(false, cx).await } else { @@ -1351,14 +1350,16 @@ impl Client { } } -fn read_credentials_from_keychain(cx: &AsyncAppContext) -> Option { +async fn read_credentials_from_keychain(cx: &AsyncAppContext) -> Option { if IMPERSONATE_LOGIN.is_some() { return None; } let (user_id, access_token) = cx - .update(|cx| cx.read_credentials(&ZED_SERVER_URL).log_err().flatten()) - .ok()??; + .update(|cx| cx.read_credentials(&ZED_SERVER_URL)) + .log_err()? + .await + .log_err()??; Some(Credentials { user_id: user_id.parse().ok()?, @@ -1366,7 +1367,10 @@ fn read_credentials_from_keychain(cx: &AsyncAppContext) -> Option { }) } -fn write_credentials_to_keychain(credentials: Credentials, cx: &AsyncAppContext) -> Result<()> { +async fn write_credentials_to_keychain( + credentials: Credentials, + cx: &AsyncAppContext, +) -> Result<()> { cx.update(move |cx| { cx.write_credentials( &ZED_SERVER_URL, @@ -1374,10 +1378,12 @@ fn write_credentials_to_keychain(credentials: Credentials, cx: &AsyncAppContext) credentials.access_token.as_bytes(), ) })? + .await } -fn delete_credentials_from_keychain(cx: &AsyncAppContext) -> Result<()> { +async fn delete_credentials_from_keychain(cx: &AsyncAppContext) -> Result<()> { cx.update(move |cx| cx.delete_credentials(&ZED_SERVER_URL))? + .await } const WORKTREE_URL_PREFIX: &str = "zed://worktrees/"; diff --git a/crates/client/src/telemetry.rs b/crates/client/src/telemetry.rs index 3412910705433c4a69205717cf8a73241071118f..824ac3f9eacca37348ee3839103d9e26de1dbe9d 100644 --- a/crates/client/src/telemetry.rs +++ b/crates/client/src/telemetry.rs @@ -1,6 +1,6 @@ mod event_coalescer; -use crate::{TelemetrySettings, ZED_SECRET_CLIENT_TOKEN, ZED_SERVER_URL}; +use crate::{TelemetrySettings, ZED_SERVER_URL}; use chrono::{DateTime, Utc}; use futures::Future; use gpui::{AppContext, AppMetadata, BackgroundExecutor, Task}; @@ -51,7 +51,6 @@ lazy_static! { #[derive(Serialize, Debug)] struct EventRequestBody { - token: &'static str, installation_id: Option>, session_id: Option>, is_staff: Option, @@ -527,7 +526,6 @@ impl Telemetry { { let state = this.state.lock(); let request_body = EventRequestBody { - token: ZED_SECRET_CLIENT_TOKEN, installation_id: state.installation_id.clone(), session_id: state.session_id.clone(), is_staff: state.is_staff.clone(), diff --git a/crates/client/src/user.rs b/crates/client/src/user.rs index dcab0e53942032045d5dcdc117e1c0e3adbe7c9e..122046827ac60273e689528b659c482ba74d04ef 100644 --- a/crates/client/src/user.rs +++ b/crates/client/src/user.rs @@ -146,7 +146,13 @@ impl UserStore { }), _maintain_current_user: cx.spawn(|this, mut cx| async move { let mut status = client.status(); + let weak = Arc::downgrade(&client); + drop(client); while let Some(status) = status.next().await { + // if the client is dropped, the app is shutting down. + let Some(client) = weak.upgrade() else { + return Ok(()); + }; match status { Status::Connected { .. } => { if let Some(user_id) = client.user_id() { diff --git a/crates/clock/Cargo.toml b/crates/clock/Cargo.toml index 2fc79db3b32efc6bf633a740c520b7e7dcdd86c6..200ff45ffcf78e54c8389be0f8fb8ed875ef0215 100644 --- a/crates/clock/Cargo.toml +++ b/crates/clock/Cargo.toml @@ -3,6 +3,8 @@ name = "clock" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/clock.rs" diff --git a/crates/clock/LICENSE-GPL b/crates/clock/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/clock/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/collab/Cargo.toml b/crates/collab/Cargo.toml index e30622adc3d360f40bb3c832cca063f6d4f6a84e..2a82323c38b15fbdca3621b5997014b5ab9bc0d9 100644 --- a/crates/collab/Cargo.toml +++ b/crates/collab/Cargo.toml @@ -3,8 +3,10 @@ authors = ["Nathan Sobo "] default-run = "collab" edition = "2021" name = "collab" -version = "0.38.0" +version = "0.40.1" publish = false +license = "AGPL-3.0-only" + [[bin]] name = "collab" @@ -27,6 +29,7 @@ axum = { version = "0.5", features = ["json", "headers", "ws"] } axum-extra = { version = "0.3", features = ["erased-json"] } base64 = "0.13" clap = { version = "3.1", features = ["derive"], optional = true } +chrono.workspace = true dashmap = "5.4" envy = "0.4.2" futures.workspace = true diff --git a/crates/collab/LICENSE-AGPL b/crates/collab/LICENSE-AGPL new file mode 120000 index 0000000000000000000000000000000000000000..5f5cf25dc458e75f4050c7378c186fca9b68fd19 --- /dev/null +++ b/crates/collab/LICENSE-AGPL @@ -0,0 +1 @@ +../../LICENSE-AGPL \ No newline at end of file diff --git a/crates/collab/migrations.sqlite/20221109000000_test_schema.sql b/crates/collab/migrations.sqlite/20221109000000_test_schema.sql index 8d8f523c94c2caa2e70212f3e08ae6f4f493599a..04676106086d5c3c92daf8728ef40b98d0555818 100644 --- a/crates/collab/migrations.sqlite/20221109000000_test_schema.sql +++ b/crates/collab/migrations.sqlite/20221109000000_test_schema.sql @@ -7,7 +7,7 @@ CREATE TABLE "users" ( "invite_count" INTEGER NOT NULL DEFAULT 0, "inviter_id" INTEGER REFERENCES users (id), "connected_once" BOOLEAN NOT NULL DEFAULT false, - "created_at" TIMESTAMP NOT NULL DEFAULT now, + "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, "metrics_id" TEXT, "github_user_id" INTEGER ); @@ -196,7 +196,8 @@ CREATE TABLE "channels" ( "name" VARCHAR NOT NULL, "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, "visibility" VARCHAR NOT NULL, - "parent_path" TEXT + "parent_path" TEXT, + "requires_zed_cla" BOOLEAN NOT NULL DEFAULT FALSE ); CREATE INDEX "index_channels_on_parent_path" ON "channels" ("parent_path"); @@ -344,3 +345,9 @@ CREATE INDEX "index_notifications_on_recipient_id_is_read_kind_entity_id" ON "notifications" ("recipient_id", "is_read", "kind", "entity_id"); + +CREATE TABLE contributors ( + user_id INTEGER REFERENCES users(id), + signed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (user_id) +); diff --git a/crates/collab/migrations/20240122174606_add_contributors.sql b/crates/collab/migrations/20240122174606_add_contributors.sql new file mode 100644 index 0000000000000000000000000000000000000000..16bec82d4f2bd0a1b3f4221366cd822ebcd70bb1 --- /dev/null +++ b/crates/collab/migrations/20240122174606_add_contributors.sql @@ -0,0 +1,5 @@ +CREATE TABLE contributors ( + user_id INTEGER REFERENCES users(id), + signed_at TIMESTAMP NOT NULL DEFAULT NOW(), + PRIMARY KEY (user_id) +); diff --git a/crates/collab/migrations/20240122224506_add_requires_zed_cla_column_to_channels.sql b/crates/collab/migrations/20240122224506_add_requires_zed_cla_column_to_channels.sql new file mode 100644 index 0000000000000000000000000000000000000000..a9248d294a2178b73986ab20cd06383d0397626b --- /dev/null +++ b/crates/collab/migrations/20240122224506_add_requires_zed_cla_column_to_channels.sql @@ -0,0 +1 @@ +ALTER TABLE "channels" ADD COLUMN "requires_zed_cla" BOOLEAN NOT NULL DEFAULT FALSE; diff --git a/crates/collab/src/api.rs b/crates/collab/src/api.rs index 6bdbd7357fb857c4db90bfc0f5583023d3b76daf..59d176b047b508d9748ba44e4aad700e60a1f58a 100644 --- a/crates/collab/src/api.rs +++ b/crates/collab/src/api.rs @@ -1,6 +1,6 @@ use crate::{ auth, - db::{User, UserId}, + db::{ContributorSelector, User, UserId}, rpc, AppState, Error, Result, }; use anyhow::anyhow; @@ -14,6 +14,7 @@ use axum::{ Extension, Json, Router, }; use axum_extra::response::ErasedJson; +use chrono::SecondsFormat; use serde::{Deserialize, Serialize}; use std::sync::Arc; use tower::ServiceBuilder; @@ -25,6 +26,8 @@ pub fn routes(rpc_server: Arc, state: Arc) -> Router>) -> Result>> { + Ok(Json(app.db.get_contributors().await?)) +} + +#[derive(Debug, Deserialize)] +struct CheckIsContributorParams { + github_user_id: Option, + github_login: Option, +} + +impl CheckIsContributorParams { + fn as_contributor_selector(self) -> Result { + if let Some(github_user_id) = self.github_user_id { + return Ok(ContributorSelector::GitHubUserId { github_user_id }); + } + + if let Some(github_login) = self.github_login { + return Ok(ContributorSelector::GitHubLogin { github_login }); + } + + Err(anyhow!( + "must be one of `github_user_id` or `github_login`." + ))? + } +} + +#[derive(Debug, Serialize)] +struct CheckIsContributorResponse { + signed_at: Option, +} + +async fn check_is_contributor( + Extension(app): Extension>, + Query(params): Query, +) -> Result> { + let params = params.as_contributor_selector()?; + Ok(Json(CheckIsContributorResponse { + signed_at: app + .db + .get_contributor_sign_timestamp(¶ms) + .await? + .map(|ts| ts.and_utc().to_rfc3339_opts(SecondsFormat::Millis, true)), + })) +} + +async fn add_contributor( + Json(params): Json, + Extension(app): Extension>, +) -> Result<()> { + Ok(app + .db + .add_contributor( + ¶ms.github_login, + params.github_user_id, + params.github_email.as_deref(), + ) + .await?) +} + #[derive(Deserialize)] struct CreateAccessTokenQueryParams { public_key: String, diff --git a/crates/collab/src/db.rs b/crates/collab/src/db.rs index 480dcf6f8595143659069739104608dac4c15fd8..f3eeb68afc8db31633f549fbd40161bcf5242530 100644 --- a/crates/collab/src/db.rs +++ b/crates/collab/src/db.rs @@ -44,6 +44,7 @@ use tables::*; use tokio::sync::{Mutex, OwnedMutexGuard}; pub use ids::*; +pub use queries::contributors::ContributorSelector; pub use sea_orm::ConnectOptions; pub use tables::user::Model as User; diff --git a/crates/collab/src/db/ids.rs b/crates/collab/src/db/ids.rs index a920265b5703e55ef482a88c03facea95194265b..9a6a1e78f3cf03660697acdd463d803825e84e36 100644 --- a/crates/collab/src/db/ids.rs +++ b/crates/collab/src/db/ids.rs @@ -173,6 +173,14 @@ impl ChannelRole { Banned => false, } } + + pub fn requires_cla(&self) -> bool { + use ChannelRole::*; + match self { + Admin | Member => true, + Banned | Guest => false, + } + } } impl From for ChannelRole { diff --git a/crates/collab/src/db/queries.rs b/crates/collab/src/db/queries.rs index 629e26f1a9e2ac1479f80984d2f9ae3efe7e9ab7..f6bba13ede5fee59b313a602fbf25d3a3d9b3ace 100644 --- a/crates/collab/src/db/queries.rs +++ b/crates/collab/src/db/queries.rs @@ -4,6 +4,7 @@ pub mod access_tokens; pub mod buffers; pub mod channels; pub mod contacts; +pub mod contributors; pub mod messages; pub mod notifications; pub mod projects; diff --git a/crates/collab/src/db/queries/channels.rs b/crates/collab/src/db/queries/channels.rs index 7ff9f00bc119c12a17987b2b7dff24e354286c33..c2428150fc4e53d5450330a58db01afad1f76c4c 100644 --- a/crates/collab/src/db/queries/channels.rs +++ b/crates/collab/src/db/queries/channels.rs @@ -67,6 +67,7 @@ impl Database { .as_ref() .map_or(String::new(), |parent| parent.path()), ), + requires_zed_cla: ActiveValue::NotSet, } .insert(&*tx) .await?; @@ -261,6 +262,22 @@ impl Database { .await } + #[cfg(test)] + pub async fn set_channel_requires_zed_cla( + &self, + channel_id: ChannelId, + requires_zed_cla: bool, + ) -> Result<()> { + self.transaction(move |tx| async move { + let channel = self.get_channel_internal(channel_id, &*tx).await?; + let mut model = channel.into_active_model(); + model.requires_zed_cla = ActiveValue::Set(requires_zed_cla); + model.update(&*tx).await?; + Ok(()) + }) + .await + } + /// Deletes the channel with the specified ID. pub async fn delete_channel( &self, diff --git a/crates/collab/src/db/queries/contributors.rs b/crates/collab/src/db/queries/contributors.rs new file mode 100644 index 0000000000000000000000000000000000000000..0972779ce957038310723e64f8d4b3a0f77aebe9 --- /dev/null +++ b/crates/collab/src/db/queries/contributors.rs @@ -0,0 +1,94 @@ +use super::*; + +#[derive(Debug)] +pub enum ContributorSelector { + GitHubUserId { github_user_id: i32 }, + GitHubLogin { github_login: String }, +} + +impl Database { + /// Retrieves the GitHub logins of all users who have signed the CLA. + pub async fn get_contributors(&self) -> Result> { + self.transaction(|tx| async move { + #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] + enum QueryGithubLogin { + GithubLogin, + } + + Ok(contributor::Entity::find() + .inner_join(user::Entity) + .order_by_asc(contributor::Column::SignedAt) + .select_only() + .column(user::Column::GithubLogin) + .into_values::<_, QueryGithubLogin>() + .all(&*tx) + .await?) + }) + .await + } + + /// Records that a given user has signed the CLA. + pub async fn get_contributor_sign_timestamp( + &self, + selector: &ContributorSelector, + ) -> Result> { + self.transaction(|tx| async move { + let condition = match selector { + ContributorSelector::GitHubUserId { github_user_id } => { + user::Column::GithubUserId.eq(*github_user_id) + } + ContributorSelector::GitHubLogin { github_login } => { + user::Column::GithubLogin.eq(github_login) + } + }; + + if let Some(user) = user::Entity::find().filter(condition).one(&*tx).await? { + if user.admin { + return Ok(Some(user.created_at)); + } + + if let Some(contributor) = + contributor::Entity::find_by_id(user.id).one(&*tx).await? + { + return Ok(Some(contributor.signed_at)); + } + } + + Ok(None) + }) + .await + } + + /// Records that a given user has signed the CLA. + pub async fn add_contributor( + &self, + github_login: &str, + github_user_id: Option, + github_email: Option<&str>, + ) -> Result<()> { + self.transaction(|tx| async move { + let user = self + .get_or_create_user_by_github_account_tx( + github_login, + github_user_id, + github_email, + &*tx, + ) + .await?; + + contributor::Entity::insert(contributor::ActiveModel { + user_id: ActiveValue::Set(user.id), + signed_at: ActiveValue::NotSet, + }) + .on_conflict( + OnConflict::column(contributor::Column::UserId) + .do_nothing() + .to_owned(), + ) + .exec_without_returning(&*tx) + .await?; + Ok(()) + }) + .await + } +} diff --git a/crates/collab/src/db/queries/rooms.rs b/crates/collab/src/db/queries/rooms.rs index 7434e2d20d006242ef572a5605c597fa13d71ade..c6aa5da125d5c74c0a86ea6b4d7cbed8fc1c07a0 100644 --- a/crates/collab/src/db/queries/rooms.rs +++ b/crates/collab/src/db/queries/rooms.rs @@ -1029,6 +1029,11 @@ impl Database { .await? .ok_or_else(|| anyhow!("only admins can set participant role"))?; + if role.requires_cla() { + self.check_user_has_signed_cla(user_id, room_id, &*tx) + .await?; + } + let result = room_participant::Entity::update_many() .filter( Condition::all() @@ -1050,6 +1055,45 @@ impl Database { .await } + async fn check_user_has_signed_cla( + &self, + user_id: UserId, + room_id: RoomId, + tx: &DatabaseTransaction, + ) -> Result<()> { + let channel = room::Entity::find_by_id(room_id) + .one(&*tx) + .await? + .ok_or_else(|| anyhow!("could not find room"))? + .find_related(channel::Entity) + .one(&*tx) + .await?; + + if let Some(channel) = channel { + let requires_zed_cla = channel.requires_zed_cla + || channel::Entity::find() + .filter( + channel::Column::Id + .is_in(channel.ancestors()) + .and(channel::Column::RequiresZedCla.eq(true)), + ) + .count(&*tx) + .await? + > 0; + if requires_zed_cla { + if contributor::Entity::find() + .filter(contributor::Column::UserId.eq(user_id)) + .one(&*tx) + .await? + .is_none() + { + Err(anyhow!("user has not signed the Zed CLA"))?; + } + } + } + Ok(()) + } + pub async fn connection_lost(&self, connection: ConnectionId) -> Result<()> { self.transaction(|tx| async move { self.room_connection_lost(connection, &*tx).await?; diff --git a/crates/collab/src/db/queries/users.rs b/crates/collab/src/db/queries/users.rs index d6dfe480427fc8ed6dce8f460b5b307e7735317e..f0768a3a9ca6acf81df9c141115a4d4859830450 100644 --- a/crates/collab/src/db/queries/users.rs +++ b/crates/collab/src/db/queries/users.rs @@ -74,51 +74,68 @@ impl Database { github_login: &str, github_user_id: Option, github_email: Option<&str>, - ) -> Result> { + ) -> Result { self.transaction(|tx| async move { - let tx = &*tx; - if let Some(github_user_id) = github_user_id { - if let Some(user_by_github_user_id) = user::Entity::find() - .filter(user::Column::GithubUserId.eq(github_user_id)) - .one(tx) - .await? - { - let mut user_by_github_user_id = user_by_github_user_id.into_active_model(); - user_by_github_user_id.github_login = ActiveValue::set(github_login.into()); - Ok(Some(user_by_github_user_id.update(tx).await?)) - } else if let Some(user_by_github_login) = user::Entity::find() - .filter(user::Column::GithubLogin.eq(github_login)) - .one(tx) - .await? - { - let mut user_by_github_login = user_by_github_login.into_active_model(); - user_by_github_login.github_user_id = ActiveValue::set(Some(github_user_id)); - Ok(Some(user_by_github_login.update(tx).await?)) - } else { - let user = user::Entity::insert(user::ActiveModel { - email_address: ActiveValue::set(github_email.map(|email| email.into())), - github_login: ActiveValue::set(github_login.into()), - github_user_id: ActiveValue::set(Some(github_user_id)), - admin: ActiveValue::set(false), - invite_count: ActiveValue::set(0), - invite_code: ActiveValue::set(None), - metrics_id: ActiveValue::set(Uuid::new_v4()), - ..Default::default() - }) - .exec_with_returning(&*tx) - .await?; - Ok(Some(user)) - } - } else { - Ok(user::Entity::find() - .filter(user::Column::GithubLogin.eq(github_login)) - .one(tx) - .await?) - } + self.get_or_create_user_by_github_account_tx( + github_login, + github_user_id, + github_email, + &*tx, + ) + .await }) .await } + pub async fn get_or_create_user_by_github_account_tx( + &self, + github_login: &str, + github_user_id: Option, + github_email: Option<&str>, + tx: &DatabaseTransaction, + ) -> Result { + if let Some(github_user_id) = github_user_id { + if let Some(user_by_github_user_id) = user::Entity::find() + .filter(user::Column::GithubUserId.eq(github_user_id)) + .one(tx) + .await? + { + let mut user_by_github_user_id = user_by_github_user_id.into_active_model(); + user_by_github_user_id.github_login = ActiveValue::set(github_login.into()); + Ok(user_by_github_user_id.update(tx).await?) + } else if let Some(user_by_github_login) = user::Entity::find() + .filter(user::Column::GithubLogin.eq(github_login)) + .one(tx) + .await? + { + let mut user_by_github_login = user_by_github_login.into_active_model(); + user_by_github_login.github_user_id = ActiveValue::set(Some(github_user_id)); + Ok(user_by_github_login.update(tx).await?) + } else { + let user = user::Entity::insert(user::ActiveModel { + email_address: ActiveValue::set(github_email.map(|email| email.into())), + github_login: ActiveValue::set(github_login.into()), + github_user_id: ActiveValue::set(Some(github_user_id)), + admin: ActiveValue::set(false), + invite_count: ActiveValue::set(0), + invite_code: ActiveValue::set(None), + metrics_id: ActiveValue::set(Uuid::new_v4()), + ..Default::default() + }) + .exec_with_returning(&*tx) + .await?; + Ok(user) + } + } else { + let user = user::Entity::find() + .filter(user::Column::GithubLogin.eq(github_login)) + .one(tx) + .await? + .ok_or_else(|| anyhow!("no such user {}", github_login))?; + Ok(user) + } + } + /// get_all_users returns the next page of users. To get more call again with /// the same limit and the page incremented by 1. pub async fn get_all_users(&self, page: u32, limit: u32) -> Result> { diff --git a/crates/collab/src/db/tables.rs b/crates/collab/src/db/tables.rs index 4f28ce4fbd4f6a5c3214270001efb11a6885c293..646447c91f6e3c56016786a5d39f81aa8f5e8eef 100644 --- a/crates/collab/src/db/tables.rs +++ b/crates/collab/src/db/tables.rs @@ -9,6 +9,7 @@ pub mod channel_member; pub mod channel_message; pub mod channel_message_mention; pub mod contact; +pub mod contributor; pub mod feature_flag; pub mod follower; pub mod language_server; diff --git a/crates/collab/src/db/tables/channel.rs b/crates/collab/src/db/tables/channel.rs index e30ec9af61674a4ebab941239989aa87463b016b..a35913a705bd8bd63bcc0ec2bfe3cae7901a10d9 100644 --- a/crates/collab/src/db/tables/channel.rs +++ b/crates/collab/src/db/tables/channel.rs @@ -9,6 +9,7 @@ pub struct Model { pub name: String, pub visibility: ChannelVisibility, pub parent_path: String, + pub requires_zed_cla: bool, } impl Model { diff --git a/crates/collab/src/db/tables/contributor.rs b/crates/collab/src/db/tables/contributor.rs new file mode 100644 index 0000000000000000000000000000000000000000..3ae96a62d910e38823828a7eb502c0d9840111c2 --- /dev/null +++ b/crates/collab/src/db/tables/contributor.rs @@ -0,0 +1,30 @@ +use crate::db::UserId; +use sea_orm::entity::prelude::*; +use serde::Serialize; + +/// A user who has signed the CLA. +#[derive(Clone, Debug, Default, PartialEq, Eq, DeriveEntityModel, Serialize)] +#[sea_orm(table_name = "contributors")] +pub struct Model { + #[sea_orm(primary_key)] + pub user_id: UserId, + pub signed_at: DateTime, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::user::Entity", + from = "Column::UserId", + to = "super::user::Column::Id" + )] + User, +} + +impl ActiveModelBehavior for ActiveModel {} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::User.def() + } +} diff --git a/crates/collab/src/db/tables/user.rs b/crates/collab/src/db/tables/user.rs index 53866b5c54f96a2e3b42c06515acba4a341bead3..5c9166adab52e09056bf543e971b79918ca6b0b4 100644 --- a/crates/collab/src/db/tables/user.rs +++ b/crates/collab/src/db/tables/user.rs @@ -17,6 +17,7 @@ pub struct Model { pub inviter_id: Option, pub connected_once: bool, pub metrics_id: Uuid, + pub created_at: DateTime, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] @@ -31,6 +32,8 @@ pub enum Relation { ChannelMemberships, #[sea_orm(has_many = "super::user_feature::Entity")] UserFeatures, + #[sea_orm(has_one = "super::contributor::Entity")] + Contributor, } impl Related for Entity { diff --git a/crates/collab/src/db/tests.rs b/crates/collab/src/db/tests.rs index 56e37abc1d9248a885724a50dcd0a8ca25ec93dc..4a9c98f02240964fc27b462cb6764fdbedc9c567 100644 --- a/crates/collab/src/db/tests.rs +++ b/crates/collab/src/db/tests.rs @@ -1,5 +1,6 @@ mod buffer_tests; mod channel_tests; +mod contributor_tests; mod db_tests; mod feature_flag_tests; mod message_tests; diff --git a/crates/collab/src/db/tests/contributor_tests.rs b/crates/collab/src/db/tests/contributor_tests.rs new file mode 100644 index 0000000000000000000000000000000000000000..c826f0083a9a73db8af02b6570ac375ca702c22e --- /dev/null +++ b/crates/collab/src/db/tests/contributor_tests.rs @@ -0,0 +1,37 @@ +use super::Database; +use crate::{db::NewUserParams, test_both_dbs}; +use std::sync::Arc; + +test_both_dbs!( + test_contributors, + test_contributors_postgres, + test_contributors_sqlite +); + +async fn test_contributors(db: &Arc) { + db.create_user( + &format!("user1@example.com"), + false, + NewUserParams { + github_login: format!("user1"), + github_user_id: 1, + }, + ) + .await + .unwrap() + .user_id; + + assert_eq!(db.get_contributors().await.unwrap(), Vec::::new()); + + db.add_contributor("user1", Some(1), None).await.unwrap(); + assert_eq!( + db.get_contributors().await.unwrap(), + vec!["user1".to_string()] + ); + + db.add_contributor("user2", Some(2), None).await.unwrap(); + assert_eq!( + db.get_contributors().await.unwrap(), + vec!["user1".to_string(), "user2".to_string()] + ); +} diff --git a/crates/collab/src/db/tests/db_tests.rs b/crates/collab/src/db/tests/db_tests.rs index 3e1bdede71e3691dc1da0e0df0fb59c6c92ac83b..a76e33402a86942e5d46e5af20ba5962d7c076e2 100644 --- a/crates/collab/src/db/tests/db_tests.rs +++ b/crates/collab/src/db/tests/db_tests.rs @@ -31,44 +31,42 @@ async fn test_get_users(db: &Arc) { } assert_eq!( - db.get_users_by_ids(user_ids.clone()).await.unwrap(), + db.get_users_by_ids(user_ids.clone()) + .await + .unwrap() + .into_iter() + .map(|user| ( + user.id, + user.github_login, + user.github_user_id, + user.email_address + )) + .collect::>(), vec![ - User { - id: user_ids[0], - github_login: "user1".to_string(), - github_user_id: Some(1), - email_address: Some("user1@example.com".to_string()), - admin: false, - metrics_id: user_metric_ids[0].parse().unwrap(), - ..Default::default() - }, - User { - id: user_ids[1], - github_login: "user2".to_string(), - github_user_id: Some(2), - email_address: Some("user2@example.com".to_string()), - admin: false, - metrics_id: user_metric_ids[1].parse().unwrap(), - ..Default::default() - }, - User { - id: user_ids[2], - github_login: "user3".to_string(), - github_user_id: Some(3), - email_address: Some("user3@example.com".to_string()), - admin: false, - metrics_id: user_metric_ids[2].parse().unwrap(), - ..Default::default() - }, - User { - id: user_ids[3], - github_login: "user4".to_string(), - github_user_id: Some(4), - email_address: Some("user4@example.com".to_string()), - admin: false, - metrics_id: user_metric_ids[3].parse().unwrap(), - ..Default::default() - } + ( + user_ids[0], + "user1".to_string(), + Some(1), + Some("user1@example.com".to_string()), + ), + ( + user_ids[1], + "user2".to_string(), + Some(2), + Some("user2@example.com".to_string()), + ), + ( + user_ids[2], + "user3".to_string(), + Some(3), + Some("user3@example.com".to_string()), + ), + ( + user_ids[3], + "user4".to_string(), + Some(4), + Some("user4@example.com".to_string()), + ) ] ); } @@ -80,18 +78,17 @@ test_both_dbs!( ); async fn test_get_or_create_user_by_github_account(db: &Arc) { - let user_id1 = db - .create_user( - "user1@example.com", - false, - NewUserParams { - github_login: "login1".into(), - github_user_id: 101, - }, - ) - .await - .unwrap() - .user_id; + db.create_user( + "user1@example.com", + false, + NewUserParams { + github_login: "login1".into(), + github_user_id: 101, + }, + ) + .await + .unwrap() + .user_id; let user_id2 = db .create_user( "user2@example.com", @@ -105,25 +102,9 @@ async fn test_get_or_create_user_by_github_account(db: &Arc) { .unwrap() .user_id; - let user = db - .get_or_create_user_by_github_account("login1", None, None) - .await - .unwrap() - .unwrap(); - assert_eq!(user.id, user_id1); - assert_eq!(&user.github_login, "login1"); - assert_eq!(user.github_user_id, Some(101)); - - assert!(db - .get_or_create_user_by_github_account("non-existent-login", None, None) - .await - .unwrap() - .is_none()); - let user = db .get_or_create_user_by_github_account("the-new-login2", Some(102), None) .await - .unwrap() .unwrap(); assert_eq!(user.id, user_id2); assert_eq!(&user.github_login, "the-new-login2"); @@ -132,7 +113,6 @@ async fn test_get_or_create_user_by_github_account(db: &Arc) { let user = db .get_or_create_user_by_github_account("login3", Some(103), Some("user3@example.com")) .await - .unwrap() .unwrap(); assert_eq!(&user.github_login, "login3"); assert_eq!(user.github_user_id, Some(103)); diff --git a/crates/collab/src/tests/channel_guest_tests.rs b/crates/collab/src/tests/channel_guest_tests.rs index f3326cd6922b7482f6c9d953a5a57a89676b717d..26e9c56a4be806da69bbbbb895a1ed583c703a1f 100644 --- a/crates/collab/src/tests/channel_guest_tests.rs +++ b/crates/collab/src/tests/channel_guest_tests.rs @@ -1,4 +1,4 @@ -use crate::tests::TestServer; +use crate::{db::ChannelId, tests::TestServer}; use call::ActiveCall; use editor::Editor; use gpui::{BackgroundExecutor, TestAppContext}; @@ -159,3 +159,103 @@ async fn test_channel_guest_promotion(cx_a: &mut TestAppContext, cx_b: &mut Test .await .is_err()); } + +#[gpui::test] +async fn test_channel_requires_zed_cla(cx_a: &mut TestAppContext, cx_b: &mut TestAppContext) { + let mut server = TestServer::start(cx_a.executor()).await; + + server + .app_state + .db + .get_or_create_user_by_github_account("user_b", Some(100), None) + .await + .unwrap(); + + let client_a = server.create_client(cx_a, "user_a").await; + let client_b = server.create_client(cx_b, "user_b").await; + let active_call_a = cx_a.read(ActiveCall::global); + let active_call_b = cx_b.read(ActiveCall::global); + + // Create a parent channel that requires the Zed CLA + let parent_channel_id = server + .make_channel("the-channel", None, (&client_a, cx_a), &mut []) + .await; + server + .app_state + .db + .set_channel_requires_zed_cla(ChannelId::from_proto(parent_channel_id), true) + .await + .unwrap(); + + // Create a public channel that is a child of the parent channel. + let channel_id = client_a + .channel_store() + .update(cx_a, |store, cx| { + store.create_channel("the-sub-channel", Some(parent_channel_id), cx) + }) + .await + .unwrap(); + client_a + .channel_store() + .update(cx_a, |store, cx| { + store.set_channel_visibility(channel_id, proto::ChannelVisibility::Public, cx) + }) + .await + .unwrap(); + + // Users A and B join the channel. B is a guest. + active_call_a + .update(cx_a, |call, cx| call.join_channel(channel_id, cx)) + .await + .unwrap(); + active_call_b + .update(cx_b, |call, cx| call.join_channel(channel_id, cx)) + .await + .unwrap(); + cx_a.run_until_parked(); + let room_b = cx_b + .read(ActiveCall::global) + .update(cx_b, |call, _| call.room().unwrap().clone()); + assert!(room_b.read_with(cx_b, |room, _| room.read_only())); + + // A tries to grant write access to B, but cannot because B has not + // yet signed the zed CLA. + active_call_a + .update(cx_a, |call, cx| { + call.room().unwrap().update(cx, |room, cx| { + room.set_participant_role( + client_b.user_id().unwrap(), + proto::ChannelRole::Member, + cx, + ) + }) + }) + .await + .unwrap_err(); + cx_a.run_until_parked(); + assert!(room_b.read_with(cx_b, |room, _| room.read_only())); + + // User B signs the zed CLA. + server + .app_state + .db + .add_contributor("user_b", Some(100), None) + .await + .unwrap(); + + // A can now grant write access to B. + active_call_a + .update(cx_a, |call, cx| { + call.room().unwrap().update(cx, |room, cx| { + room.set_participant_role( + client_b.user_id().unwrap(), + proto::ChannelRole::Member, + cx, + ) + }) + }) + .await + .unwrap(); + cx_a.run_until_parked(); + assert!(room_b.read_with(cx_b, |room, _| !room.read_only())); +} diff --git a/crates/collab/src/tests/test_server.rs b/crates/collab/src/tests/test_server.rs index 8efd9535b09bfb5e6243852a648c6cb1d5ad0450..c0386f47852f5ffb0b5f444f89bd645dbdf22db4 100644 --- a/crates/collab/src/tests/test_server.rs +++ b/crates/collab/src/tests/test_server.rs @@ -43,6 +43,7 @@ pub struct TestServer { pub app_state: Arc, pub test_live_kit_server: Arc, server: Arc, + next_github_user_id: i32, connection_killers: Arc>>>, forbid_connections: Arc, _test_db: TestDb, @@ -108,6 +109,7 @@ impl TestServer { server, connection_killers: Default::default(), forbid_connections: Default::default(), + next_github_user_id: 0, _test_db: test_db, test_live_kit_server: live_kit_server, } @@ -157,6 +159,8 @@ impl TestServer { { user.id } else { + let github_user_id = self.next_github_user_id; + self.next_github_user_id += 1; self.app_state .db .create_user( @@ -164,7 +168,7 @@ impl TestServer { false, NewUserParams { github_login: name.into(), - github_user_id: 0, + github_user_id, }, ) .await @@ -742,9 +746,9 @@ impl TestClient { let window = cx.update(|cx| cx.active_window().unwrap().downcast::().unwrap()); let view = window.root_view(cx).unwrap(); - let cx = Box::new(VisualTestContext::from_window(*window.deref(), cx)); + let cx = VisualTestContext::from_window(*window.deref(), cx).as_mut(); // it might be nice to try and cleanup these at the end of each test. - (view, Box::leak(cx)) + (view, cx) } } diff --git a/crates/collab_ui/Cargo.toml b/crates/collab_ui/Cargo.toml index 0fbf7deb7836c36be047a10f14cec419eadc5d8d..c6bdf9c9e3de8624eb1a5dabeb1a475aeb8587f4 100644 --- a/crates/collab_ui/Cargo.toml +++ b/crates/collab_ui/Cargo.toml @@ -3,6 +3,8 @@ name = "collab_ui" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/collab_ui.rs" diff --git a/crates/collab_ui/LICENSE-GPL b/crates/collab_ui/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/collab_ui/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/collab_ui/src/collab_panel/channel_modal.rs b/crates/collab_ui/src/collab_panel/channel_modal.rs index e92422d76d79e7b2706aed83b892031f9b4b3a96..3d7facf2e8d917d7ea93a9f2e0e57fa306a9bf5d 100644 --- a/crates/collab_ui/src/collab_panel/channel_modal.rs +++ b/crates/collab_ui/src/collab_panel/channel_modal.rs @@ -10,10 +10,11 @@ use gpui::{ WeakView, }; use picker::{Picker, PickerDelegate}; +use rpc::proto::channel_member; use std::sync::Arc; use ui::{prelude::*, Avatar, Checkbox, ContextMenu, ListItem, ListItemSpacing}; use util::TryFutureExt; -use workspace::ModalView; +use workspace::{notifications::NotifyTaskExt, ModalView}; actions!( channel_modal, @@ -347,15 +348,13 @@ impl PickerDelegate for ChannelModalDelegate { } fn confirm(&mut self, _: bool, cx: &mut ViewContext>) { - if let Some((selected_user, role)) = self.user_at_index(self.selected_index) { + if let Some(selected_user) = self.user_at_index(self.selected_index) { if Some(selected_user.id) == self.user_store.read(cx).current_user().map(|user| user.id) { return; } match self.mode { - Mode::ManageMembers => { - self.show_context_menu(selected_user, role.unwrap_or(ChannelRole::Member), cx) - } + Mode::ManageMembers => self.show_context_menu(self.selected_index, cx), Mode::InviteMembers => match self.member_status(selected_user.id, cx) { Some(proto::channel_member::Kind::Invitee) => { self.remove_member(selected_user.id, cx); @@ -385,7 +384,8 @@ impl PickerDelegate for ChannelModalDelegate { selected: bool, cx: &mut ViewContext>, ) -> Option { - let (user, role) = self.user_at_index(ix)?; + let user = self.user_at_index(ix)?; + let membership = self.member_at_index(ix); let request_status = self.member_status(user.id, cx); let is_me = self.user_store.read(cx).current_user().map(|user| user.id) == Some(user.id); @@ -402,11 +402,15 @@ impl PickerDelegate for ChannelModalDelegate { .children( if request_status == Some(proto::channel_member::Kind::Invitee) { Some(Label::new("Invited")) + } else if membership.map(|m| m.kind) + == Some(channel_member::Kind::AncestorMember) + { + Some(Label::new("Parent")) } else { None }, ) - .children(match role { + .children(match membership.map(|m| m.role) { Some(ChannelRole::Admin) => Some(Label::new("Admin")), Some(ChannelRole::Guest) => Some(Label::new("Guest")), _ => None, @@ -419,7 +423,7 @@ impl PickerDelegate for ChannelModalDelegate { if let (Some((menu, _)), true) = (&self.context_menu, selected) { Some( overlay() - .anchor(gpui::AnchorCorner::TopLeft) + .anchor(gpui::AnchorCorner::TopRight) .child(menu.clone()), ) } else { @@ -458,16 +462,19 @@ impl ChannelModalDelegate { }) } - fn user_at_index(&self, ix: usize) -> Option<(Arc, Option)> { + fn member_at_index(&self, ix: usize) -> Option<&ChannelMembership> { + self.matching_member_indices + .get(ix) + .and_then(|ix| self.members.get(*ix)) + } + + fn user_at_index(&self, ix: usize) -> Option> { match self.mode { Mode::ManageMembers => self.matching_member_indices.get(ix).and_then(|ix| { let channel_membership = self.members.get(*ix)?; - Some(( - channel_membership.user.clone(), - Some(channel_membership.role), - )) + Some(channel_membership.user.clone()) }), - Mode::InviteMembers => Some((self.matching_users.get(ix).cloned()?, None)), + Mode::InviteMembers => self.matching_users.get(ix).cloned(), } } @@ -491,7 +498,7 @@ impl ChannelModalDelegate { cx.notify(); }) }) - .detach_and_log_err(cx); + .detach_and_notify_err(cx); Some(()) } @@ -523,7 +530,7 @@ impl ChannelModalDelegate { cx.notify(); }) }) - .detach_and_log_err(cx); + .detach_and_notify_err(cx); Some(()) } @@ -549,19 +556,66 @@ impl ChannelModalDelegate { cx.notify(); }) }) - .detach_and_log_err(cx); + .detach_and_notify_err(cx); } - fn show_context_menu( - &mut self, - user: Arc, - role: ChannelRole, - cx: &mut ViewContext>, - ) { - let user_id = user.id; + fn show_context_menu(&mut self, ix: usize, cx: &mut ViewContext>) { + let Some(membership) = self.member_at_index(ix) else { + return; + }; + if membership.kind == proto::channel_member::Kind::AncestorMember { + return; + } + let user_id = membership.user.id; let picker = cx.view().clone(); let context_menu = ContextMenu::build(cx, |mut menu, _cx| { - menu = menu.entry("Remove Member", None, { + if membership.kind == channel_member::Kind::AncestorMember { + return menu.entry("Inherited membership", None, |_| {}); + }; + + let role = membership.role; + + if role == ChannelRole::Admin || role == ChannelRole::Member { + let picker = picker.clone(); + menu = menu.entry("Demote to Guest", None, move |cx| { + picker.update(cx, |picker, cx| { + picker + .delegate + .set_user_role(user_id, ChannelRole::Guest, cx); + }) + }); + } + + if role == ChannelRole::Admin || role == ChannelRole::Guest { + let picker = picker.clone(); + let label = if role == ChannelRole::Guest { + "Promote to Member" + } else { + "Demote to Member" + }; + + menu = menu.entry(label, None, move |cx| { + picker.update(cx, |picker, cx| { + picker + .delegate + .set_user_role(user_id, ChannelRole::Member, cx); + }) + }); + } + + if role == ChannelRole::Member || role == ChannelRole::Guest { + let picker = picker.clone(); + menu = menu.entry("Promote to Admin", None, move |cx| { + picker.update(cx, |picker, cx| { + picker + .delegate + .set_user_role(user_id, ChannelRole::Admin, cx); + }) + }); + }; + + menu = menu.separator(); + menu = menu.entry("Remove from Channel", None, { let picker = picker.clone(); move |cx| { picker.update(cx, |picker, cx| { @@ -569,30 +623,6 @@ impl ChannelModalDelegate { }) } }); - - let picker = picker.clone(); - match role { - ChannelRole::Admin => { - menu = menu.entry("Revoke Admin", None, move |cx| { - picker.update(cx, |picker, cx| { - picker - .delegate - .set_user_role(user_id, ChannelRole::Member, cx); - }) - }); - } - ChannelRole::Member => { - menu = menu.entry("Make Admin", None, move |cx| { - picker.update(cx, |picker, cx| { - picker - .delegate - .set_user_role(user_id, ChannelRole::Admin, cx); - }) - }); - } - _ => {} - }; - menu }); cx.focus_view(&context_menu); diff --git a/crates/collections/Cargo.toml b/crates/collections/Cargo.toml index 64db1161bbdc27cd7b167fd51b1849e9f27cfc6c..5f135d7ad3b2fd298cc77ea186c7bb1254a82f18 100644 --- a/crates/collections/Cargo.toml +++ b/crates/collections/Cargo.toml @@ -3,6 +3,8 @@ name = "collections" version = "0.1.0" edition = "2021" publish = false +license = "Apache-2.0" + [lib] path = "src/collections.rs" diff --git a/crates/collections/LICENSE-APACHE b/crates/collections/LICENSE-APACHE new file mode 120000 index 0000000000000000000000000000000000000000..1cd601d0a3affae83854be02a0afdec3b7a9ec4d --- /dev/null +++ b/crates/collections/LICENSE-APACHE @@ -0,0 +1 @@ +../../LICENSE-APACHE \ No newline at end of file diff --git a/crates/color/Cargo.toml b/crates/color/Cargo.toml index c6416f9691b3ca417c1f7426ace5359c199be88b..5a086e70882945fa9cdc95646b78a1bf49ae4a9d 100644 --- a/crates/color/Cargo.toml +++ b/crates/color/Cargo.toml @@ -3,6 +3,8 @@ name = "color" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [features] default = [] diff --git a/crates/color/LICENSE-GPL b/crates/color/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/color/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/command_palette/Cargo.toml b/crates/command_palette/Cargo.toml index c762af7c487e1603b4c3b38f7887f38a56e99276..b1cb70a65979a91cf1be8048cf5b3a83791e480f 100644 --- a/crates/command_palette/Cargo.toml +++ b/crates/command_palette/Cargo.toml @@ -3,6 +3,8 @@ name = "command_palette" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/command_palette.rs" diff --git a/crates/command_palette/LICENSE-GPL b/crates/command_palette/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/command_palette/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/copilot/Cargo.toml b/crates/copilot/Cargo.toml index fefd49090fd2020f4dc6da8aa1c2a1c5d119ea96..8c63e63bb80212f67ef29009966f659427514f4d 100644 --- a/crates/copilot/Cargo.toml +++ b/crates/copilot/Cargo.toml @@ -3,6 +3,8 @@ name = "copilot" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/copilot.rs" diff --git a/crates/copilot/LICENSE-GPL b/crates/copilot/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/copilot/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/copilot_ui/Cargo.toml b/crates/copilot_ui/Cargo.toml index 491f4f3cdec3d2ebd20fe1d6a2536471f862b90b..293cffe3ec599bd908ceba0a9a2d11078851ea7c 100644 --- a/crates/copilot_ui/Cargo.toml +++ b/crates/copilot_ui/Cargo.toml @@ -3,6 +3,8 @@ name = "copilot_ui" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/copilot_ui.rs" diff --git a/crates/copilot_ui/LICENSE-GPL b/crates/copilot_ui/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/copilot_ui/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/db/Cargo.toml b/crates/db/Cargo.toml index b49078e860ff0d502c7ff1fbe5cdfa26df5fac38..e87c4b7551e003d6fa420bdb132125fc00f1c534 100644 --- a/crates/db/Cargo.toml +++ b/crates/db/Cargo.toml @@ -3,6 +3,8 @@ name = "db" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/db.rs" diff --git a/crates/db/LICENSE-GPL b/crates/db/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/db/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/diagnostics/Cargo.toml b/crates/diagnostics/Cargo.toml index c393a41152ae19485551fffd96d96981c9f6b950..2560daa9ea0ee0fd9342ca64cb09f62386af945f 100644 --- a/crates/diagnostics/Cargo.toml +++ b/crates/diagnostics/Cargo.toml @@ -3,6 +3,8 @@ name = "diagnostics" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/diagnostics.rs" diff --git a/crates/diagnostics/LICENSE-GPL b/crates/diagnostics/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/diagnostics/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index bf753a1784d02e38c9e4589157b47e02b0bfe53d..e737b697178238dab32d1a9b14ae9d11a558e203 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -1603,6 +1603,7 @@ mod tests { gutter_width: px(0.), line_height: px(0.), em_width: px(0.), + max_width: px(0.), block_id: ix, view: editor_view, editor_style: &editor::EditorStyle::default(), diff --git a/crates/editor/Cargo.toml b/crates/editor/Cargo.toml index 3c0269d41089bde2a75ab7f560745d45201ca028..68de8d58ad9f84aecadcabd97ecd736877c6d652 100644 --- a/crates/editor/Cargo.toml +++ b/crates/editor/Cargo.toml @@ -3,6 +3,8 @@ name = "editor" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/editor.rs" diff --git a/crates/editor/LICENSE-GPL b/crates/editor/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/editor/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/editor/src/display_map/block_map.rs b/crates/editor/src/display_map/block_map.rs index 1b51e55352b1feb08079c54eb8f45800ae7241d4..4ed50e9b610e6b1145abdf28be4de13779c1cbd3 100644 --- a/crates/editor/src/display_map/block_map.rs +++ b/crates/editor/src/display_map/block_map.rs @@ -84,6 +84,7 @@ pub struct BlockContext<'a, 'b> { pub context: &'b mut ElementContext<'a>, pub view: View, pub anchor_x: Pixels, + pub max_width: Pixels, pub gutter_width: Pixels, pub gutter_padding: Pixels, pub em_width: Pixels, diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index e378425666c9550e30c18d447d67baf3b0e05c76..b8c152ae4043f2e9bdfa8a935573b48531d251c4 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -281,7 +281,7 @@ pub enum SelectPhase { Update { position: DisplayPoint, goal_column: u32, - scroll_position: gpui::Point, + scroll_delta: gpui::Point, }, End, } @@ -1928,8 +1928,8 @@ impl Editor { SelectPhase::Update { position, goal_column, - scroll_position, - } => self.update_selection(position, goal_column, scroll_position, cx), + scroll_delta, + } => self.update_selection(position, goal_column, scroll_delta, cx), SelectPhase::End => self.end_selection(cx), } } @@ -2063,7 +2063,7 @@ impl Editor { &mut self, position: DisplayPoint, goal_column: u32, - scroll_position: gpui::Point, + scroll_delta: gpui::Point, cx: &mut ViewContext, ) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); @@ -2152,7 +2152,7 @@ impl Editor { return; } - self.set_scroll_position(scroll_position, cx); + self.apply_scroll_delta(scroll_delta, cx); cx.notify(); } @@ -9593,31 +9593,33 @@ pub fn diagnostic_block_renderer(diagnostic: Diagnostic, _is_valid: bool) -> Ren let (text_without_backticks, code_ranges) = highlight_diagnostic_message(&diagnostic); Arc::new(move |cx: &mut BlockContext| { - let color = Some(cx.theme().colors().text_accent); let group_id: SharedString = cx.block_id.to_string().into(); - // TODO: Nate: We should tint the background of the block with the severity color - // We need to extend the theme before we can do this + + let mut text_style = cx.text_style().clone(); + text_style.color = diagnostic_style(diagnostic.severity, true, cx.theme().status()); + h_flex() .id(cx.block_id) .group(group_id.clone()) .relative() - .pl(cx.anchor_x) .size_full() - .gap_2() - .child( + .pl(cx.gutter_width) + .w(cx.max_width + cx.gutter_width) + .child(div().flex().w(cx.anchor_x - cx.gutter_width).flex_shrink()) + .child(div().flex().flex_shrink_0().child( StyledText::new(text_without_backticks.clone()).with_highlights( - &cx.text_style(), + &text_style, code_ranges.iter().map(|range| { ( range.clone(), HighlightStyle { - color, + font_weight: Some(FontWeight::BOLD), ..Default::default() }, ) }), ), - ) + )) .child( IconButton::new(("copy-block", cx.block_id), IconName::Copy) .icon_color(Color::Muted) diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index eeb8263f30fa1449350bf7872d45866cadadb54f..dadcc62842360b1be401a2b0900b5a1a1d824dc8 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -531,8 +531,7 @@ impl EditorElement { SelectPhase::Update { position: point_for_position.previous_valid, goal_column: point_for_position.exact_unclipped.column(), - scroll_position: (position_map.snapshot.scroll_position() + scroll_delta) - .clamp(&gpui::Point::default(), &position_map.scroll_max), + scroll_delta, }, cx, ); @@ -2075,6 +2074,7 @@ impl EditorElement { &snapshot, bounds.size.width, scroll_width, + text_width, gutter_dimensions.padding, gutter_dimensions.width, em_width, @@ -2153,14 +2153,18 @@ impl EditorElement { .max(MIN_POPOVER_LINE_HEIGHT * line_height), // Apply minimum height of 4 lines ); - let hover = editor.hover_state.render( + let hover = if context_menu.is_some() { + None + } else { + editor.hover_state.render( &snapshot, &style, visible_rows, max_size, editor.workspace.as_ref().map(|(w, _)| w.clone()), cx, - ); + ) + }; let editor_view = cx.view().clone(); let fold_indicators = cx.with_element_context(|cx| { @@ -2257,6 +2261,7 @@ impl EditorElement { snapshot: &EditorSnapshot, editor_width: Pixels, scroll_width: Pixels, + text_width: Pixels, gutter_padding: Pixels, gutter_width: Pixels, em_width: Pixels, @@ -2306,6 +2311,7 @@ impl EditorElement { gutter_width, em_width, block_id, + max_width: scroll_width.max(text_width), view: editor_view.clone(), editor_style: &self.style, }) diff --git a/crates/editor/src/hover_popover.rs b/crates/editor/src/hover_popover.rs index f311f20ae6d0faf9bc42193245fa42ae14cc5fac..668d00f1aabcf67cd9293e1bfaf1d3abe5c57091 100644 --- a/crates/editor/src/hover_popover.rs +++ b/crates/editor/src/hover_popover.rs @@ -394,6 +394,26 @@ async fn parse_blocks( } } + let leading_space = text.chars().take_while(|c| c.is_whitespace()).count(); + if leading_space > 0 { + highlights = highlights + .into_iter() + .map(|(range, style)| { + ( + range.start.saturating_sub(leading_space) + ..range.end.saturating_sub(leading_space), + style, + ) + }) + .collect(); + region_ranges = region_ranges + .into_iter() + .map(|range| { + range.start.saturating_sub(leading_space)..range.end.saturating_sub(leading_space) + }) + .collect(); + } + ParsedMarkdown { text: text.trim().to_string(), highlights, diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index 36a48b293788ab22f509f941c7b2a66591614d9a..11ea07ff5c83fbde36dca67b5a352711bbbc9e64 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -277,7 +277,7 @@ impl FollowableItem for Editor { .extend(ids.iter().map(ExcerptId::to_proto)); true } - EditorEvent::ScrollPositionChanged { .. } => { + EditorEvent::ScrollPositionChanged { autoscroll, .. } if !autoscroll => { let scroll_anchor = self.scroll_manager.anchor(); update.scroll_top_anchor = Some(serialize_anchor(&scroll_anchor.anchor)); update.scroll_x = scroll_anchor.offset.x; diff --git a/crates/editor/src/scroll.rs b/crates/editor/src/scroll.rs index f68004109e8889600c08b91ffe1edc5cc54ddee1..d7f1456bd673efdf609b9e5b88d9122f280b2f49 100644 --- a/crates/editor/src/scroll.rs +++ b/crates/editor/src/scroll.rs @@ -202,7 +202,7 @@ impl ScrollManager { ScrollAnchor { anchor: top_anchor, offset: point( - scroll_position.x, + scroll_position.x.max(0.), scroll_position.y - top_anchor.to_display_point(&map).row() as f32, ), }, @@ -327,6 +327,16 @@ impl Editor { } } + pub fn apply_scroll_delta( + &mut self, + scroll_delta: gpui::Point, + cx: &mut ViewContext, + ) { + let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); + let position = self.scroll_manager.anchor.scroll_position(&display_map) + scroll_delta; + self.set_scroll_position_taking_display_map(position, true, false, display_map, cx); + } + pub fn set_scroll_position( &mut self, scroll_position: gpui::Point, @@ -343,12 +353,22 @@ impl Editor { cx: &mut ViewContext, ) { let map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); + self.set_scroll_position_taking_display_map(scroll_position, local, autoscroll, map, cx); + } + fn set_scroll_position_taking_display_map( + &mut self, + scroll_position: gpui::Point, + local: bool, + autoscroll: bool, + display_map: DisplaySnapshot, + cx: &mut ViewContext, + ) { hide_hover(self, cx); let workspace_id = self.workspace.as_ref().map(|workspace| workspace.1); self.scroll_manager.set_scroll_position( scroll_position, - &map, + &display_map, local, autoscroll, workspace_id, diff --git a/crates/feature_flags/Cargo.toml b/crates/feature_flags/Cargo.toml index af273fe4033c7fbca36df2ccc8a2daae86eec19b..1e52e9a772469eb63f1b8c9b38c0c0e92c4e08ce 100644 --- a/crates/feature_flags/Cargo.toml +++ b/crates/feature_flags/Cargo.toml @@ -3,6 +3,8 @@ name = "feature_flags" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/feature_flags.rs" diff --git a/crates/feature_flags/LICENSE-GPL b/crates/feature_flags/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/feature_flags/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/feedback/Cargo.toml b/crates/feedback/Cargo.toml index 32ecee529c2bcd1de88b778105cffa5e58706e29..1aeeb71ebe83e8153ea8ebe645c9ee70b62c2f54 100644 --- a/crates/feedback/Cargo.toml +++ b/crates/feedback/Cargo.toml @@ -3,6 +3,8 @@ name = "feedback" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/feedback.rs" diff --git a/crates/feedback/LICENSE-GPL b/crates/feedback/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/feedback/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/feedback/src/feedback_modal.rs b/crates/feedback/src/feedback_modal.rs index 80722580b7afe9c37de4e1fb7edde97f3515db76..99c96fe8808c51af845b5fb261a75d88405d4ef7 100644 --- a/crates/feedback/src/feedback_modal.rs +++ b/crates/feedback/src/feedback_modal.rs @@ -2,7 +2,7 @@ use std::{ops::RangeInclusive, sync::Arc, time::Duration}; use anyhow::{anyhow, bail}; use bitflags::bitflags; -use client::{Client, ZED_SECRET_CLIENT_TOKEN, ZED_SERVER_URL}; +use client::{Client, ZED_SERVER_URL}; use db::kvp::KEY_VALUE_STORE; use editor::{Editor, EditorEvent}; use futures::AsyncReadExt; @@ -46,7 +46,6 @@ struct FeedbackRequestBody<'a> { installation_id: Option>, system_specs: SystemSpecs, is_staff: bool, - token: &'a str, } bitflags! { @@ -305,7 +304,6 @@ impl FeedbackModal { installation_id, system_specs, is_staff: is_staff.unwrap_or(false), - token: ZED_SECRET_CLIENT_TOKEN, }; let json_bytes = serde_json::to_vec(&request)?; let request = Request::post(feedback_endpoint) diff --git a/crates/file_finder/Cargo.toml b/crates/file_finder/Cargo.toml index e80e310c707895352184ba903e146359922dd831..1a670ae3389d48a56033d924b2c2361ce76d9e40 100644 --- a/crates/file_finder/Cargo.toml +++ b/crates/file_finder/Cargo.toml @@ -3,6 +3,8 @@ name = "file_finder" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/file_finder.rs" diff --git a/crates/file_finder/LICENSE-GPL b/crates/file_finder/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/file_finder/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/file_finder/src/file_finder.rs b/crates/file_finder/src/file_finder.rs index 8484843c87253c6b1aa6b801e46435048d1558a4..672ed6272e57fe9d2caf9c3d6429626e9c7dad2c 100644 --- a/crates/file_finder/src/file_finder.rs +++ b/crates/file_finder/src/file_finder.rs @@ -1,3 +1,6 @@ +#[cfg(test)] +mod file_finder_tests; + use collections::HashMap; use editor::{scroll::Autoscroll, Bias, Editor}; use fuzzy::{CharBag, PathMatch, PathMatchCandidate}; @@ -112,11 +115,13 @@ impl FileFinder { } impl EventEmitter for FileFinder {} + impl FocusableView for FileFinder { fn focus_handle(&self, cx: &AppContext) -> FocusHandle { self.picker.focus_handle(cx) } } + impl Render for FileFinder { fn render(&mut self, _cx: &mut ViewContext) -> impl IntoElement { v_flex().w(rems(34.)).child(self.picker.clone()) @@ -352,10 +357,15 @@ impl FileFinderDelegate { let include_root_name = worktrees.len() > 1; let candidate_sets = worktrees .into_iter() - .map(|worktree| PathMatchCandidateSet { - snapshot: worktree.read(cx).snapshot(), - include_ignored: true, - include_root_name, + .map(|worktree| { + let worktree = worktree.read(cx); + PathMatchCandidateSet { + snapshot: worktree.snapshot(), + include_ignored: worktree + .root_entry() + .map_or(false, |entry| entry.is_ignored), + include_root_name, + } }) .collect::>(); @@ -377,6 +387,7 @@ impl FileFinderDelegate { let did_cancel = cancel_flag.load(atomic::Ordering::Relaxed); picker .update(&mut cx, |picker, cx| { + picker.delegate.selected_index.take(); picker .delegate .set_search_matches(search_id, did_cancel, query, matches, cx) @@ -615,6 +626,7 @@ impl PickerDelegate for FileFinderDelegate { if raw_query.is_empty() { let project = self.project.read(cx); self.latest_search_id = post_inc(&mut self.search_count); + self.selected_index.take(); self.matches = Matches { history: self .history_items @@ -793,1324 +805,3 @@ impl PickerDelegate for FileFinderDelegate { ) } } - -#[cfg(test)] -mod tests { - use std::{assert_eq, path::Path, time::Duration}; - - use super::*; - use editor::Editor; - use gpui::{Entity, TestAppContext, VisualTestContext}; - use menu::{Confirm, SelectNext}; - use serde_json::json; - use workspace::{AppState, Workspace}; - - #[ctor::ctor] - fn init_logger() { - if std::env::var("RUST_LOG").is_ok() { - env_logger::init(); - } - } - - #[gpui::test] - async fn test_matching_paths(cx: &mut TestAppContext) { - let app_state = init_test(cx); - app_state - .fs - .as_fake() - .insert_tree( - "/root", - json!({ - "a": { - "banana": "", - "bandana": "", - } - }), - ) - .await; - - let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await; - - let (picker, workspace, cx) = build_find_picker(project, cx); - - cx.simulate_input("bna"); - picker.update(cx, |picker, _| { - assert_eq!(picker.delegate.matches.len(), 2); - }); - cx.dispatch_action(SelectNext); - cx.dispatch_action(Confirm); - cx.read(|cx| { - let active_editor = workspace.read(cx).active_item_as::(cx).unwrap(); - assert_eq!(active_editor.read(cx).title(cx), "bandana"); - }); - - for bandana_query in [ - "bandana", - " bandana", - "bandana ", - " bandana ", - " ndan ", - " band ", - ] { - picker - .update(cx, |picker, cx| { - picker - .delegate - .update_matches(bandana_query.to_string(), cx) - }) - .await; - picker.update(cx, |picker, _| { - assert_eq!( - picker.delegate.matches.len(), - 1, - "Wrong number of matches for bandana query '{bandana_query}'" - ); - }); - cx.dispatch_action(SelectNext); - cx.dispatch_action(Confirm); - cx.read(|cx| { - let active_editor = workspace.read(cx).active_item_as::(cx).unwrap(); - assert_eq!( - active_editor.read(cx).title(cx), - "bandana", - "Wrong match for bandana query '{bandana_query}'" - ); - }); - } - } - - #[gpui::test] - async fn test_absolute_paths(cx: &mut TestAppContext) { - let app_state = init_test(cx); - app_state - .fs - .as_fake() - .insert_tree( - "/root", - json!({ - "a": { - "file1.txt": "", - "b": { - "file2.txt": "", - }, - } - }), - ) - .await; - - let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await; - - let (picker, workspace, cx) = build_find_picker(project, cx); - - let matching_abs_path = "/root/a/b/file2.txt"; - picker - .update(cx, |picker, cx| { - picker - .delegate - .update_matches(matching_abs_path.to_string(), cx) - }) - .await; - picker.update(cx, |picker, _| { - assert_eq!( - collect_search_results(picker), - vec![PathBuf::from("a/b/file2.txt")], - "Matching abs path should be the only match" - ) - }); - cx.dispatch_action(SelectNext); - cx.dispatch_action(Confirm); - cx.read(|cx| { - let active_editor = workspace.read(cx).active_item_as::(cx).unwrap(); - assert_eq!(active_editor.read(cx).title(cx), "file2.txt"); - }); - - let mismatching_abs_path = "/root/a/b/file1.txt"; - picker - .update(cx, |picker, cx| { - picker - .delegate - .update_matches(mismatching_abs_path.to_string(), cx) - }) - .await; - picker.update(cx, |picker, _| { - assert_eq!( - collect_search_results(picker), - Vec::::new(), - "Mismatching abs path should produce no matches" - ) - }); - } - - #[gpui::test] - async fn test_complex_path(cx: &mut TestAppContext) { - let app_state = init_test(cx); - app_state - .fs - .as_fake() - .insert_tree( - "/root", - json!({ - "其他": { - "S数据表格": { - "task.xlsx": "some content", - }, - } - }), - ) - .await; - - let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await; - - let (picker, workspace, cx) = build_find_picker(project, cx); - - cx.simulate_input("t"); - picker.update(cx, |picker, _| { - assert_eq!(picker.delegate.matches.len(), 1); - assert_eq!( - collect_search_results(picker), - vec![PathBuf::from("其他/S数据表格/task.xlsx")], - ) - }); - cx.dispatch_action(SelectNext); - cx.dispatch_action(Confirm); - cx.read(|cx| { - let active_editor = workspace.read(cx).active_item_as::(cx).unwrap(); - assert_eq!(active_editor.read(cx).title(cx), "task.xlsx"); - }); - } - - #[gpui::test] - async fn test_row_column_numbers_query_inside_file(cx: &mut TestAppContext) { - let app_state = init_test(cx); - - let first_file_name = "first.rs"; - let first_file_contents = "// First Rust file"; - app_state - .fs - .as_fake() - .insert_tree( - "/src", - json!({ - "test": { - first_file_name: first_file_contents, - "second.rs": "// Second Rust file", - } - }), - ) - .await; - - let project = Project::test(app_state.fs.clone(), ["/src".as_ref()], cx).await; - - let (picker, workspace, cx) = build_find_picker(project, cx); - - let file_query = &first_file_name[..3]; - let file_row = 1; - let file_column = 3; - assert!(file_column <= first_file_contents.len()); - let query_inside_file = format!("{file_query}:{file_row}:{file_column}"); - picker - .update(cx, |finder, cx| { - finder - .delegate - .update_matches(query_inside_file.to_string(), cx) - }) - .await; - picker.update(cx, |finder, _| { - let finder = &finder.delegate; - assert_eq!(finder.matches.len(), 1); - let latest_search_query = finder - .latest_search_query - .as_ref() - .expect("Finder should have a query after the update_matches call"); - assert_eq!(latest_search_query.path_like.raw_query, query_inside_file); - assert_eq!( - latest_search_query.path_like.file_query_end, - Some(file_query.len()) - ); - assert_eq!(latest_search_query.row, Some(file_row)); - assert_eq!(latest_search_query.column, Some(file_column as u32)); - }); - - cx.dispatch_action(SelectNext); - cx.dispatch_action(Confirm); - - let editor = cx.update(|cx| workspace.read(cx).active_item_as::(cx).unwrap()); - cx.executor().advance_clock(Duration::from_secs(2)); - - editor.update(cx, |editor, cx| { - let all_selections = editor.selections.all_adjusted(cx); - assert_eq!( - all_selections.len(), - 1, - "Expected to have 1 selection (caret) after file finder confirm, but got: {all_selections:?}" - ); - let caret_selection = all_selections.into_iter().next().unwrap(); - assert_eq!(caret_selection.start, caret_selection.end, - "Caret selection should have its start and end at the same position"); - assert_eq!(file_row, caret_selection.start.row + 1, - "Query inside file should get caret with the same focus row"); - assert_eq!(file_column, caret_selection.start.column as usize + 1, - "Query inside file should get caret with the same focus column"); - }); - } - - #[gpui::test] - async fn test_row_column_numbers_query_outside_file(cx: &mut TestAppContext) { - let app_state = init_test(cx); - - let first_file_name = "first.rs"; - let first_file_contents = "// First Rust file"; - app_state - .fs - .as_fake() - .insert_tree( - "/src", - json!({ - "test": { - first_file_name: first_file_contents, - "second.rs": "// Second Rust file", - } - }), - ) - .await; - - let project = Project::test(app_state.fs.clone(), ["/src".as_ref()], cx).await; - - let (picker, workspace, cx) = build_find_picker(project, cx); - - let file_query = &first_file_name[..3]; - let file_row = 200; - let file_column = 300; - assert!(file_column > first_file_contents.len()); - let query_outside_file = format!("{file_query}:{file_row}:{file_column}"); - picker - .update(cx, |picker, cx| { - picker - .delegate - .update_matches(query_outside_file.to_string(), cx) - }) - .await; - picker.update(cx, |finder, _| { - let delegate = &finder.delegate; - assert_eq!(delegate.matches.len(), 1); - let latest_search_query = delegate - .latest_search_query - .as_ref() - .expect("Finder should have a query after the update_matches call"); - assert_eq!(latest_search_query.path_like.raw_query, query_outside_file); - assert_eq!( - latest_search_query.path_like.file_query_end, - Some(file_query.len()) - ); - assert_eq!(latest_search_query.row, Some(file_row)); - assert_eq!(latest_search_query.column, Some(file_column as u32)); - }); - - cx.dispatch_action(SelectNext); - cx.dispatch_action(Confirm); - - let editor = cx.update(|cx| workspace.read(cx).active_item_as::(cx).unwrap()); - cx.executor().advance_clock(Duration::from_secs(2)); - - editor.update(cx, |editor, cx| { - let all_selections = editor.selections.all_adjusted(cx); - assert_eq!( - all_selections.len(), - 1, - "Expected to have 1 selection (caret) after file finder confirm, but got: {all_selections:?}" - ); - let caret_selection = all_selections.into_iter().next().unwrap(); - assert_eq!(caret_selection.start, caret_selection.end, - "Caret selection should have its start and end at the same position"); - assert_eq!(0, caret_selection.start.row, - "Excessive rows (as in query outside file borders) should get trimmed to last file row"); - assert_eq!(first_file_contents.len(), caret_selection.start.column as usize, - "Excessive columns (as in query outside file borders) should get trimmed to selected row's last column"); - }); - } - - #[gpui::test] - async fn test_matching_cancellation(cx: &mut TestAppContext) { - let app_state = init_test(cx); - app_state - .fs - .as_fake() - .insert_tree( - "/dir", - json!({ - "hello": "", - "goodbye": "", - "halogen-light": "", - "happiness": "", - "height": "", - "hi": "", - "hiccup": "", - }), - ) - .await; - - let project = Project::test(app_state.fs.clone(), ["/dir".as_ref()], cx).await; - - let (picker, _, cx) = build_find_picker(project, cx); - - let query = test_path_like("hi"); - picker - .update(cx, |picker, cx| { - picker.delegate.spawn_search(query.clone(), cx) - }) - .await; - - picker.update(cx, |picker, _cx| { - assert_eq!(picker.delegate.matches.len(), 5) - }); - - picker.update(cx, |picker, cx| { - let delegate = &mut picker.delegate; - assert!( - delegate.matches.history.is_empty(), - "Search matches expected" - ); - let matches = delegate.matches.search.clone(); - - // Simulate a search being cancelled after the time limit, - // returning only a subset of the matches that would have been found. - drop(delegate.spawn_search(query.clone(), cx)); - delegate.set_search_matches( - delegate.latest_search_id, - true, // did-cancel - query.clone(), - vec![matches[1].clone(), matches[3].clone()], - cx, - ); - - // Simulate another cancellation. - drop(delegate.spawn_search(query.clone(), cx)); - delegate.set_search_matches( - delegate.latest_search_id, - true, // did-cancel - query.clone(), - vec![matches[0].clone(), matches[2].clone(), matches[3].clone()], - cx, - ); - - assert!( - delegate.matches.history.is_empty(), - "Search matches expected" - ); - assert_eq!(delegate.matches.search.as_slice(), &matches[0..4]); - }); - } - - #[gpui::test] - async fn test_ignored_root(cx: &mut TestAppContext) { - let app_state = init_test(cx); - app_state - .fs - .as_fake() - .insert_tree( - "/ancestor", - json!({ - ".gitignore": "ignored-root", - "ignored-root": { - "happiness": "", - "height": "", - "hi": "", - "hiccup": "", - }, - "tracked-root": { - ".gitignore": "height", - "happiness": "", - "height": "", - "hi": "", - "hiccup": "", - }, - }), - ) - .await; - - let project = Project::test( - app_state.fs.clone(), - [ - "/ancestor/tracked-root".as_ref(), - "/ancestor/ignored-root".as_ref(), - ], - cx, - ) - .await; - - let (picker, _, cx) = build_find_picker(project, cx); - - picker - .update(cx, |picker, cx| { - picker.delegate.spawn_search(test_path_like("hi"), cx) - }) - .await; - picker.update(cx, |picker, _| { - assert_eq!( - collect_search_results(picker), - vec![ - PathBuf::from("ignored-root/happiness"), - PathBuf::from("ignored-root/height"), - PathBuf::from("ignored-root/hi"), - PathBuf::from("ignored-root/hiccup"), - PathBuf::from("tracked-root/happiness"), - PathBuf::from("tracked-root/height"), - PathBuf::from("tracked-root/hi"), - PathBuf::from("tracked-root/hiccup"), - ], - "All files in all roots (including gitignored) should be searched" - ) - }); - } - - #[gpui::test] - async fn test_ignored_files(cx: &mut TestAppContext) { - let app_state = init_test(cx); - app_state - .fs - .as_fake() - .insert_tree( - "/root", - json!({ - ".git": {}, - ".gitignore": "ignored_a\n.env\n", - "a": { - "banana_env": "11", - "bandana_env": "12", - }, - "ignored_a": { - "ignored_banana_env": "21", - "ignored_bandana_env": "22", - "ignored_nested": { - "ignored_nested_banana_env": "31", - "ignored_nested_bandana_env": "32", - }, - }, - ".env": "something", - }), - ) - .await; - - let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await; - - let (picker, workspace, cx) = build_find_picker(project, cx); - - cx.simulate_input("env"); - picker.update(cx, |picker, _| { - assert_eq!( - collect_search_results(picker), - vec![ - PathBuf::from(".env"), - PathBuf::from("a/banana_env"), - PathBuf::from("a/bandana_env"), - ], - "Root gitignored files and all non-gitignored files should be searched" - ) - }); - - let _ = workspace - .update(cx, |workspace, cx| { - workspace.open_abs_path( - PathBuf::from("/root/ignored_a/ignored_banana_env"), - true, - cx, - ) - }) - .await - .unwrap(); - cx.run_until_parked(); - cx.simulate_input("env"); - picker.update(cx, |picker, _| { - assert_eq!( - collect_search_results(picker), - vec![ - PathBuf::from(".env"), - PathBuf::from("a/banana_env"), - PathBuf::from("a/bandana_env"), - PathBuf::from("ignored_a/ignored_banana_env"), - PathBuf::from("ignored_a/ignored_bandana_env"), - ], - "Root gitignored dir got listed and its entries got into worktree, but all gitignored dirs below it were not listed. Old entries + new listed gitignored entries should be searched" - ) - }); - } - - #[gpui::test] - async fn test_single_file_worktrees(cx: &mut TestAppContext) { - let app_state = init_test(cx); - app_state - .fs - .as_fake() - .insert_tree("/root", json!({ "the-parent-dir": { "the-file": "" } })) - .await; - - let project = Project::test( - app_state.fs.clone(), - ["/root/the-parent-dir/the-file".as_ref()], - cx, - ) - .await; - - let (picker, _, cx) = build_find_picker(project, cx); - - // Even though there is only one worktree, that worktree's filename - // is included in the matching, because the worktree is a single file. - picker - .update(cx, |picker, cx| { - picker.delegate.spawn_search(test_path_like("thf"), cx) - }) - .await; - cx.read(|cx| { - let picker = picker.read(cx); - let delegate = &picker.delegate; - assert!( - delegate.matches.history.is_empty(), - "Search matches expected" - ); - let matches = delegate.matches.search.clone(); - assert_eq!(matches.len(), 1); - - let (file_name, file_name_positions, full_path, full_path_positions) = - delegate.labels_for_path_match(&matches[0]); - assert_eq!(file_name, "the-file"); - assert_eq!(file_name_positions, &[0, 1, 4]); - assert_eq!(full_path, "the-file"); - assert_eq!(full_path_positions, &[0, 1, 4]); - }); - - // Since the worktree root is a file, searching for its name followed by a slash does - // not match anything. - picker - .update(cx, |f, cx| { - f.delegate.spawn_search(test_path_like("thf/"), cx) - }) - .await; - picker.update(cx, |f, _| assert_eq!(f.delegate.matches.len(), 0)); - } - - #[gpui::test] - async fn test_path_distance_ordering(cx: &mut TestAppContext) { - let app_state = init_test(cx); - app_state - .fs - .as_fake() - .insert_tree( - "/root", - json!({ - "dir1": { "a.txt": "" }, - "dir2": { - "a.txt": "", - "b.txt": "" - } - }), - ) - .await; - - let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await; - let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project, cx)); - - let worktree_id = cx.read(|cx| { - let worktrees = workspace.read(cx).worktrees(cx).collect::>(); - assert_eq!(worktrees.len(), 1); - WorktreeId::from_usize(worktrees[0].entity_id().as_u64() as usize) - }); - - // When workspace has an active item, sort items which are closer to that item - // first when they have the same name. In this case, b.txt is closer to dir2's a.txt - // so that one should be sorted earlier - let b_path = ProjectPath { - worktree_id, - path: Arc::from(Path::new("dir2/b.txt")), - }; - workspace - .update(cx, |workspace, cx| { - workspace.open_path(b_path, None, true, cx) - }) - .await - .unwrap(); - let finder = open_file_picker(&workspace, cx); - finder - .update(cx, |f, cx| { - f.delegate.spawn_search(test_path_like("a.txt"), cx) - }) - .await; - - finder.update(cx, |f, _| { - let delegate = &f.delegate; - assert!( - delegate.matches.history.is_empty(), - "Search matches expected" - ); - let matches = delegate.matches.search.clone(); - assert_eq!(matches[0].path.as_ref(), Path::new("dir2/a.txt")); - assert_eq!(matches[1].path.as_ref(), Path::new("dir1/a.txt")); - }); - } - - #[gpui::test] - async fn test_search_worktree_without_files(cx: &mut TestAppContext) { - let app_state = init_test(cx); - app_state - .fs - .as_fake() - .insert_tree( - "/root", - json!({ - "dir1": {}, - "dir2": { - "dir3": {} - } - }), - ) - .await; - - let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await; - let (picker, _workspace, cx) = build_find_picker(project, cx); - - picker - .update(cx, |f, cx| { - f.delegate.spawn_search(test_path_like("dir"), cx) - }) - .await; - cx.read(|cx| { - let finder = picker.read(cx); - assert_eq!(finder.delegate.matches.len(), 0); - }); - } - - #[gpui::test] - async fn test_query_history(cx: &mut gpui::TestAppContext) { - let app_state = init_test(cx); - - app_state - .fs - .as_fake() - .insert_tree( - "/src", - json!({ - "test": { - "first.rs": "// First Rust file", - "second.rs": "// Second Rust file", - "third.rs": "// Third Rust file", - } - }), - ) - .await; - - let project = Project::test(app_state.fs.clone(), ["/src".as_ref()], cx).await; - let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project, cx)); - let worktree_id = cx.read(|cx| { - let worktrees = workspace.read(cx).worktrees(cx).collect::>(); - assert_eq!(worktrees.len(), 1); - WorktreeId::from_usize(worktrees[0].entity_id().as_u64() as usize) - }); - - // Open and close panels, getting their history items afterwards. - // Ensure history items get populated with opened items, and items are kept in a certain order. - // The history lags one opened buffer behind, since it's updated in the search panel only on its reopen. - // - // TODO: without closing, the opened items do not propagate their history changes for some reason - // it does work in real app though, only tests do not propagate. - workspace.update(cx, |_, cx| cx.focused()); - - let initial_history = open_close_queried_buffer("fir", 1, "first.rs", &workspace, cx).await; - assert!( - initial_history.is_empty(), - "Should have no history before opening any files" - ); - - let history_after_first = - open_close_queried_buffer("sec", 1, "second.rs", &workspace, cx).await; - assert_eq!( - history_after_first, - vec![FoundPath::new( - ProjectPath { - worktree_id, - path: Arc::from(Path::new("test/first.rs")), - }, - Some(PathBuf::from("/src/test/first.rs")) - )], - "Should show 1st opened item in the history when opening the 2nd item" - ); - - let history_after_second = - open_close_queried_buffer("thi", 1, "third.rs", &workspace, cx).await; - assert_eq!( - history_after_second, - vec![ - FoundPath::new( - ProjectPath { - worktree_id, - path: Arc::from(Path::new("test/second.rs")), - }, - Some(PathBuf::from("/src/test/second.rs")) - ), - FoundPath::new( - ProjectPath { - worktree_id, - path: Arc::from(Path::new("test/first.rs")), - }, - Some(PathBuf::from("/src/test/first.rs")) - ), - ], - "Should show 1st and 2nd opened items in the history when opening the 3rd item. \ - 2nd item should be the first in the history, as the last opened." - ); - - let history_after_third = - open_close_queried_buffer("sec", 1, "second.rs", &workspace, cx).await; - assert_eq!( - history_after_third, - vec![ - FoundPath::new( - ProjectPath { - worktree_id, - path: Arc::from(Path::new("test/third.rs")), - }, - Some(PathBuf::from("/src/test/third.rs")) - ), - FoundPath::new( - ProjectPath { - worktree_id, - path: Arc::from(Path::new("test/second.rs")), - }, - Some(PathBuf::from("/src/test/second.rs")) - ), - FoundPath::new( - ProjectPath { - worktree_id, - path: Arc::from(Path::new("test/first.rs")), - }, - Some(PathBuf::from("/src/test/first.rs")) - ), - ], - "Should show 1st, 2nd and 3rd opened items in the history when opening the 2nd item again. \ - 3rd item should be the first in the history, as the last opened." - ); - - let history_after_second_again = - open_close_queried_buffer("thi", 1, "third.rs", &workspace, cx).await; - assert_eq!( - history_after_second_again, - vec![ - FoundPath::new( - ProjectPath { - worktree_id, - path: Arc::from(Path::new("test/second.rs")), - }, - Some(PathBuf::from("/src/test/second.rs")) - ), - FoundPath::new( - ProjectPath { - worktree_id, - path: Arc::from(Path::new("test/third.rs")), - }, - Some(PathBuf::from("/src/test/third.rs")) - ), - FoundPath::new( - ProjectPath { - worktree_id, - path: Arc::from(Path::new("test/first.rs")), - }, - Some(PathBuf::from("/src/test/first.rs")) - ), - ], - "Should show 1st, 2nd and 3rd opened items in the history when opening the 3rd item again. \ - 2nd item, as the last opened, 3rd item should go next as it was opened right before." - ); - } - - #[gpui::test] - async fn test_external_files_history(cx: &mut gpui::TestAppContext) { - let app_state = init_test(cx); - - app_state - .fs - .as_fake() - .insert_tree( - "/src", - json!({ - "test": { - "first.rs": "// First Rust file", - "second.rs": "// Second Rust file", - } - }), - ) - .await; - - app_state - .fs - .as_fake() - .insert_tree( - "/external-src", - json!({ - "test": { - "third.rs": "// Third Rust file", - "fourth.rs": "// Fourth Rust file", - } - }), - ) - .await; - - let project = Project::test(app_state.fs.clone(), ["/src".as_ref()], cx).await; - cx.update(|cx| { - project.update(cx, |project, cx| { - project.find_or_create_local_worktree("/external-src", false, cx) - }) - }) - .detach(); - cx.background_executor.run_until_parked(); - - let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project, cx)); - let worktree_id = cx.read(|cx| { - let worktrees = workspace.read(cx).worktrees(cx).collect::>(); - assert_eq!(worktrees.len(), 1,); - - WorktreeId::from_usize(worktrees[0].entity_id().as_u64() as usize) - }); - workspace - .update(cx, |workspace, cx| { - workspace.open_abs_path(PathBuf::from("/external-src/test/third.rs"), false, cx) - }) - .detach(); - cx.background_executor.run_until_parked(); - let external_worktree_id = cx.read(|cx| { - let worktrees = workspace.read(cx).worktrees(cx).collect::>(); - assert_eq!( - worktrees.len(), - 2, - "External file should get opened in a new worktree" - ); - - WorktreeId::from_usize( - worktrees - .into_iter() - .find(|worktree| { - worktree.entity_id().as_u64() as usize != worktree_id.to_usize() - }) - .expect("New worktree should have a different id") - .entity_id() - .as_u64() as usize, - ) - }); - cx.dispatch_action(workspace::CloseActiveItem { save_intent: None }); - - let initial_history_items = - open_close_queried_buffer("sec", 1, "second.rs", &workspace, cx).await; - assert_eq!( - initial_history_items, - vec![FoundPath::new( - ProjectPath { - worktree_id: external_worktree_id, - path: Arc::from(Path::new("")), - }, - Some(PathBuf::from("/external-src/test/third.rs")) - )], - "Should show external file with its full path in the history after it was open" - ); - - let updated_history_items = - open_close_queried_buffer("fir", 1, "first.rs", &workspace, cx).await; - assert_eq!( - updated_history_items, - vec![ - FoundPath::new( - ProjectPath { - worktree_id, - path: Arc::from(Path::new("test/second.rs")), - }, - Some(PathBuf::from("/src/test/second.rs")) - ), - FoundPath::new( - ProjectPath { - worktree_id: external_worktree_id, - path: Arc::from(Path::new("")), - }, - Some(PathBuf::from("/external-src/test/third.rs")) - ), - ], - "Should keep external file with history updates", - ); - } - - #[gpui::test] - async fn test_toggle_panel_new_selections(cx: &mut gpui::TestAppContext) { - let app_state = init_test(cx); - - app_state - .fs - .as_fake() - .insert_tree( - "/src", - json!({ - "test": { - "first.rs": "// First Rust file", - "second.rs": "// Second Rust file", - "third.rs": "// Third Rust file", - } - }), - ) - .await; - - let project = Project::test(app_state.fs.clone(), ["/src".as_ref()], cx).await; - let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project, cx)); - - // generate some history to select from - open_close_queried_buffer("fir", 1, "first.rs", &workspace, cx).await; - cx.executor().run_until_parked(); - open_close_queried_buffer("sec", 1, "second.rs", &workspace, cx).await; - open_close_queried_buffer("thi", 1, "third.rs", &workspace, cx).await; - let current_history = - open_close_queried_buffer("sec", 1, "second.rs", &workspace, cx).await; - - for expected_selected_index in 0..current_history.len() { - cx.dispatch_action(Toggle); - let picker = active_file_picker(&workspace, cx); - let selected_index = picker.update(cx, |picker, _| picker.delegate.selected_index()); - assert_eq!( - selected_index, expected_selected_index, - "Should select the next item in the history" - ); - } - - cx.dispatch_action(Toggle); - let selected_index = workspace.update(cx, |workspace, cx| { - workspace - .active_modal::(cx) - .unwrap() - .read(cx) - .picker - .read(cx) - .delegate - .selected_index() - }); - assert_eq!( - selected_index, 0, - "Should wrap around the history and start all over" - ); - } - - #[gpui::test] - async fn test_search_preserves_history_items(cx: &mut gpui::TestAppContext) { - let app_state = init_test(cx); - - app_state - .fs - .as_fake() - .insert_tree( - "/src", - json!({ - "test": { - "first.rs": "// First Rust file", - "second.rs": "// Second Rust file", - "third.rs": "// Third Rust file", - "fourth.rs": "// Fourth Rust file", - } - }), - ) - .await; - - let project = Project::test(app_state.fs.clone(), ["/src".as_ref()], cx).await; - let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project, cx)); - let worktree_id = cx.read(|cx| { - let worktrees = workspace.read(cx).worktrees(cx).collect::>(); - assert_eq!(worktrees.len(), 1,); - - WorktreeId::from_usize(worktrees[0].entity_id().as_u64() as usize) - }); - - // generate some history to select from - open_close_queried_buffer("fir", 1, "first.rs", &workspace, cx).await; - open_close_queried_buffer("sec", 1, "second.rs", &workspace, cx).await; - open_close_queried_buffer("thi", 1, "third.rs", &workspace, cx).await; - open_close_queried_buffer("sec", 1, "second.rs", &workspace, cx).await; - - let finder = open_file_picker(&workspace, cx); - let first_query = "f"; - finder - .update(cx, |finder, cx| { - finder.delegate.update_matches(first_query.to_string(), cx) - }) - .await; - finder.update(cx, |finder, _| { - let delegate = &finder.delegate; - assert_eq!(delegate.matches.history.len(), 1, "Only one history item contains {first_query}, it should be present and others should be filtered out"); - let history_match = delegate.matches.history.first().unwrap(); - assert!(history_match.1.is_some(), "Should have path matches for history items after querying"); - assert_eq!(history_match.0, FoundPath::new( - ProjectPath { - worktree_id, - path: Arc::from(Path::new("test/first.rs")), - }, - Some(PathBuf::from("/src/test/first.rs")) - )); - assert_eq!(delegate.matches.search.len(), 1, "Only one non-history item contains {first_query}, it should be present"); - assert_eq!(delegate.matches.search.first().unwrap().path.as_ref(), Path::new("test/fourth.rs")); - }); - - let second_query = "fsdasdsa"; - let finder = active_file_picker(&workspace, cx); - finder - .update(cx, |finder, cx| { - finder.delegate.update_matches(second_query.to_string(), cx) - }) - .await; - finder.update(cx, |finder, _| { - let delegate = &finder.delegate; - assert!( - delegate.matches.history.is_empty(), - "No history entries should match {second_query}" - ); - assert!( - delegate.matches.search.is_empty(), - "No search entries should match {second_query}" - ); - }); - - let first_query_again = first_query; - - let finder = active_file_picker(&workspace, cx); - finder - .update(cx, |finder, cx| { - finder - .delegate - .update_matches(first_query_again.to_string(), cx) - }) - .await; - finder.update(cx, |finder, _| { - let delegate = &finder.delegate; - assert_eq!(delegate.matches.history.len(), 1, "Only one history item contains {first_query_again}, it should be present and others should be filtered out, even after non-matching query"); - let history_match = delegate.matches.history.first().unwrap(); - assert!(history_match.1.is_some(), "Should have path matches for history items after querying"); - assert_eq!(history_match.0, FoundPath::new( - ProjectPath { - worktree_id, - path: Arc::from(Path::new("test/first.rs")), - }, - Some(PathBuf::from("/src/test/first.rs")) - )); - assert_eq!(delegate.matches.search.len(), 1, "Only one non-history item contains {first_query_again}, it should be present, even after non-matching query"); - assert_eq!(delegate.matches.search.first().unwrap().path.as_ref(), Path::new("test/fourth.rs")); - }); - } - - #[gpui::test] - async fn test_history_items_vs_very_good_external_match(cx: &mut gpui::TestAppContext) { - let app_state = init_test(cx); - - app_state - .fs - .as_fake() - .insert_tree( - "/src", - json!({ - "collab_ui": { - "first.rs": "// First Rust file", - "second.rs": "// Second Rust file", - "third.rs": "// Third Rust file", - "collab_ui.rs": "// Fourth Rust file", - } - }), - ) - .await; - - let project = Project::test(app_state.fs.clone(), ["/src".as_ref()], cx).await; - let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project, cx)); - // generate some history to select from - open_close_queried_buffer("fir", 1, "first.rs", &workspace, cx).await; - open_close_queried_buffer("sec", 1, "second.rs", &workspace, cx).await; - open_close_queried_buffer("thi", 1, "third.rs", &workspace, cx).await; - open_close_queried_buffer("sec", 1, "second.rs", &workspace, cx).await; - - let finder = open_file_picker(&workspace, cx); - let query = "collab_ui"; - cx.simulate_input(query); - finder.update(cx, |finder, _| { - let delegate = &finder.delegate; - assert!( - delegate.matches.history.is_empty(), - "History items should not math query {query}, they should be matched by name only" - ); - - let search_entries = delegate - .matches - .search - .iter() - .map(|path_match| path_match.path.to_path_buf()) - .collect::>(); - assert_eq!( - search_entries, - vec![ - PathBuf::from("collab_ui/collab_ui.rs"), - PathBuf::from("collab_ui/third.rs"), - PathBuf::from("collab_ui/first.rs"), - PathBuf::from("collab_ui/second.rs"), - ], - "Despite all search results having the same directory name, the most matching one should be on top" - ); - }); - } - - #[gpui::test] - async fn test_nonexistent_history_items_not_shown(cx: &mut gpui::TestAppContext) { - let app_state = init_test(cx); - - app_state - .fs - .as_fake() - .insert_tree( - "/src", - json!({ - "test": { - "first.rs": "// First Rust file", - "nonexistent.rs": "// Second Rust file", - "third.rs": "// Third Rust file", - } - }), - ) - .await; - - let project = Project::test(app_state.fs.clone(), ["/src".as_ref()], cx).await; - let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project, cx)); // generate some history to select from - open_close_queried_buffer("fir", 1, "first.rs", &workspace, cx).await; - open_close_queried_buffer("non", 1, "nonexistent.rs", &workspace, cx).await; - open_close_queried_buffer("thi", 1, "third.rs", &workspace, cx).await; - open_close_queried_buffer("fir", 1, "first.rs", &workspace, cx).await; - - let picker = open_file_picker(&workspace, cx); - cx.simulate_input("rs"); - - picker.update(cx, |finder, _| { - let history_entries = finder.delegate - .matches - .history - .iter() - .map(|(_, path_match)| path_match.as_ref().expect("should have a path match").path.to_path_buf()) - .collect::>(); - assert_eq!( - history_entries, - vec![ - PathBuf::from("test/first.rs"), - PathBuf::from("test/third.rs"), - ], - "Should have all opened files in the history, except the ones that do not exist on disk" - ); - }); - } - - async fn open_close_queried_buffer( - input: &str, - expected_matches: usize, - expected_editor_title: &str, - workspace: &View, - cx: &mut gpui::VisualTestContext, - ) -> Vec { - let picker = open_file_picker(&workspace, cx); - cx.simulate_input(input); - - let history_items = picker.update(cx, |finder, _| { - assert_eq!( - finder.delegate.matches.len(), - expected_matches, - "Unexpected number of matches found for query {input}" - ); - finder.delegate.history_items.clone() - }); - - cx.dispatch_action(SelectNext); - cx.dispatch_action(Confirm); - - cx.read(|cx| { - let active_editor = workspace.read(cx).active_item_as::(cx).unwrap(); - let active_editor_title = active_editor.read(cx).title(cx); - assert_eq!( - expected_editor_title, active_editor_title, - "Unexpected editor title for query {input}" - ); - }); - - cx.dispatch_action(workspace::CloseActiveItem { save_intent: None }); - - history_items - } - - fn init_test(cx: &mut TestAppContext) -> Arc { - cx.update(|cx| { - let state = AppState::test(cx); - theme::init(theme::LoadThemes::JustBase, cx); - language::init(cx); - super::init(cx); - editor::init(cx); - workspace::init_settings(cx); - Project::init_settings(cx); - state - }) - } - - fn test_path_like(test_str: &str) -> PathLikeWithPosition { - PathLikeWithPosition::parse_str(test_str, |path_like_str| { - Ok::<_, std::convert::Infallible>(FileSearchQuery { - raw_query: test_str.to_owned(), - file_query_end: if path_like_str == test_str { - None - } else { - Some(path_like_str.len()) - }, - }) - }) - .unwrap() - } - - fn build_find_picker( - project: Model, - cx: &mut TestAppContext, - ) -> ( - View>, - View, - &mut VisualTestContext, - ) { - let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project, cx)); - let picker = open_file_picker(&workspace, cx); - (picker, workspace, cx) - } - - #[track_caller] - fn open_file_picker( - workspace: &View, - cx: &mut VisualTestContext, - ) -> View> { - cx.dispatch_action(Toggle); - active_file_picker(workspace, cx) - } - - #[track_caller] - fn active_file_picker( - workspace: &View, - cx: &mut VisualTestContext, - ) -> View> { - workspace.update(cx, |workspace, cx| { - workspace - .active_modal::(cx) - .unwrap() - .read(cx) - .picker - .clone() - }) - } - - fn collect_search_results(picker: &Picker) -> Vec { - let matches = &picker.delegate.matches; - assert!( - matches.history.is_empty(), - "Should have no history matches, but got: {:?}", - matches.history - ); - let mut results = matches - .search - .iter() - .map(|path_match| Path::new(path_match.path_prefix.as_ref()).join(&path_match.path)) - .collect::>(); - results.sort(); - results - } -} diff --git a/crates/file_finder/src/file_finder_tests.rs b/crates/file_finder/src/file_finder_tests.rs new file mode 100644 index 0000000000000000000000000000000000000000..107fe891beac2e72bd8409cbeef0cb26a1e921e1 --- /dev/null +++ b/crates/file_finder/src/file_finder_tests.rs @@ -0,0 +1,1227 @@ +use std::{assert_eq, path::Path, time::Duration}; + +use super::*; +use editor::Editor; +use gpui::{Entity, TestAppContext, VisualTestContext}; +use menu::{Confirm, SelectNext}; +use serde_json::json; +use workspace::{AppState, Workspace}; + +#[ctor::ctor] +fn init_logger() { + if std::env::var("RUST_LOG").is_ok() { + env_logger::init(); + } +} + +#[gpui::test] +async fn test_matching_paths(cx: &mut TestAppContext) { + let app_state = init_test(cx); + app_state + .fs + .as_fake() + .insert_tree( + "/root", + json!({ + "a": { + "banana": "", + "bandana": "", + } + }), + ) + .await; + + let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await; + + let (picker, workspace, cx) = build_find_picker(project, cx); + + cx.simulate_input("bna"); + picker.update(cx, |picker, _| { + assert_eq!(picker.delegate.matches.len(), 2); + }); + cx.dispatch_action(SelectNext); + cx.dispatch_action(Confirm); + cx.read(|cx| { + let active_editor = workspace.read(cx).active_item_as::(cx).unwrap(); + assert_eq!(active_editor.read(cx).title(cx), "bandana"); + }); + + for bandana_query in [ + "bandana", + " bandana", + "bandana ", + " bandana ", + " ndan ", + " band ", + ] { + picker + .update(cx, |picker, cx| { + picker + .delegate + .update_matches(bandana_query.to_string(), cx) + }) + .await; + picker.update(cx, |picker, _| { + assert_eq!( + picker.delegate.matches.len(), + 1, + "Wrong number of matches for bandana query '{bandana_query}'" + ); + }); + cx.dispatch_action(SelectNext); + cx.dispatch_action(Confirm); + cx.read(|cx| { + let active_editor = workspace.read(cx).active_item_as::(cx).unwrap(); + assert_eq!( + active_editor.read(cx).title(cx), + "bandana", + "Wrong match for bandana query '{bandana_query}'" + ); + }); + } +} + +#[gpui::test] +async fn test_absolute_paths(cx: &mut TestAppContext) { + let app_state = init_test(cx); + app_state + .fs + .as_fake() + .insert_tree( + "/root", + json!({ + "a": { + "file1.txt": "", + "b": { + "file2.txt": "", + }, + } + }), + ) + .await; + + let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await; + + let (picker, workspace, cx) = build_find_picker(project, cx); + + let matching_abs_path = "/root/a/b/file2.txt"; + picker + .update(cx, |picker, cx| { + picker + .delegate + .update_matches(matching_abs_path.to_string(), cx) + }) + .await; + picker.update(cx, |picker, _| { + assert_eq!( + collect_search_results(picker), + vec![PathBuf::from("a/b/file2.txt")], + "Matching abs path should be the only match" + ) + }); + cx.dispatch_action(SelectNext); + cx.dispatch_action(Confirm); + cx.read(|cx| { + let active_editor = workspace.read(cx).active_item_as::(cx).unwrap(); + assert_eq!(active_editor.read(cx).title(cx), "file2.txt"); + }); + + let mismatching_abs_path = "/root/a/b/file1.txt"; + picker + .update(cx, |picker, cx| { + picker + .delegate + .update_matches(mismatching_abs_path.to_string(), cx) + }) + .await; + picker.update(cx, |picker, _| { + assert_eq!( + collect_search_results(picker), + Vec::::new(), + "Mismatching abs path should produce no matches" + ) + }); +} + +#[gpui::test] +async fn test_complex_path(cx: &mut TestAppContext) { + let app_state = init_test(cx); + app_state + .fs + .as_fake() + .insert_tree( + "/root", + json!({ + "其他": { + "S数据表格": { + "task.xlsx": "some content", + }, + } + }), + ) + .await; + + let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await; + + let (picker, workspace, cx) = build_find_picker(project, cx); + + cx.simulate_input("t"); + picker.update(cx, |picker, _| { + assert_eq!(picker.delegate.matches.len(), 1); + assert_eq!( + collect_search_results(picker), + vec![PathBuf::from("其他/S数据表格/task.xlsx")], + ) + }); + cx.dispatch_action(SelectNext); + cx.dispatch_action(Confirm); + cx.read(|cx| { + let active_editor = workspace.read(cx).active_item_as::(cx).unwrap(); + assert_eq!(active_editor.read(cx).title(cx), "task.xlsx"); + }); +} + +#[gpui::test] +async fn test_row_column_numbers_query_inside_file(cx: &mut TestAppContext) { + let app_state = init_test(cx); + + let first_file_name = "first.rs"; + let first_file_contents = "// First Rust file"; + app_state + .fs + .as_fake() + .insert_tree( + "/src", + json!({ + "test": { + first_file_name: first_file_contents, + "second.rs": "// Second Rust file", + } + }), + ) + .await; + + let project = Project::test(app_state.fs.clone(), ["/src".as_ref()], cx).await; + + let (picker, workspace, cx) = build_find_picker(project, cx); + + let file_query = &first_file_name[..3]; + let file_row = 1; + let file_column = 3; + assert!(file_column <= first_file_contents.len()); + let query_inside_file = format!("{file_query}:{file_row}:{file_column}"); + picker + .update(cx, |finder, cx| { + finder + .delegate + .update_matches(query_inside_file.to_string(), cx) + }) + .await; + picker.update(cx, |finder, _| { + let finder = &finder.delegate; + assert_eq!(finder.matches.len(), 1); + let latest_search_query = finder + .latest_search_query + .as_ref() + .expect("Finder should have a query after the update_matches call"); + assert_eq!(latest_search_query.path_like.raw_query, query_inside_file); + assert_eq!( + latest_search_query.path_like.file_query_end, + Some(file_query.len()) + ); + assert_eq!(latest_search_query.row, Some(file_row)); + assert_eq!(latest_search_query.column, Some(file_column as u32)); + }); + + cx.dispatch_action(SelectNext); + cx.dispatch_action(Confirm); + + let editor = cx.update(|cx| workspace.read(cx).active_item_as::(cx).unwrap()); + cx.executor().advance_clock(Duration::from_secs(2)); + + editor.update(cx, |editor, cx| { + let all_selections = editor.selections.all_adjusted(cx); + assert_eq!( + all_selections.len(), + 1, + "Expected to have 1 selection (caret) after file finder confirm, but got: {all_selections:?}" + ); + let caret_selection = all_selections.into_iter().next().unwrap(); + assert_eq!(caret_selection.start, caret_selection.end, + "Caret selection should have its start and end at the same position"); + assert_eq!(file_row, caret_selection.start.row + 1, + "Query inside file should get caret with the same focus row"); + assert_eq!(file_column, caret_selection.start.column as usize + 1, + "Query inside file should get caret with the same focus column"); + }); +} + +#[gpui::test] +async fn test_row_column_numbers_query_outside_file(cx: &mut TestAppContext) { + let app_state = init_test(cx); + + let first_file_name = "first.rs"; + let first_file_contents = "// First Rust file"; + app_state + .fs + .as_fake() + .insert_tree( + "/src", + json!({ + "test": { + first_file_name: first_file_contents, + "second.rs": "// Second Rust file", + } + }), + ) + .await; + + let project = Project::test(app_state.fs.clone(), ["/src".as_ref()], cx).await; + + let (picker, workspace, cx) = build_find_picker(project, cx); + + let file_query = &first_file_name[..3]; + let file_row = 200; + let file_column = 300; + assert!(file_column > first_file_contents.len()); + let query_outside_file = format!("{file_query}:{file_row}:{file_column}"); + picker + .update(cx, |picker, cx| { + picker + .delegate + .update_matches(query_outside_file.to_string(), cx) + }) + .await; + picker.update(cx, |finder, _| { + let delegate = &finder.delegate; + assert_eq!(delegate.matches.len(), 1); + let latest_search_query = delegate + .latest_search_query + .as_ref() + .expect("Finder should have a query after the update_matches call"); + assert_eq!(latest_search_query.path_like.raw_query, query_outside_file); + assert_eq!( + latest_search_query.path_like.file_query_end, + Some(file_query.len()) + ); + assert_eq!(latest_search_query.row, Some(file_row)); + assert_eq!(latest_search_query.column, Some(file_column as u32)); + }); + + cx.dispatch_action(SelectNext); + cx.dispatch_action(Confirm); + + let editor = cx.update(|cx| workspace.read(cx).active_item_as::(cx).unwrap()); + cx.executor().advance_clock(Duration::from_secs(2)); + + editor.update(cx, |editor, cx| { + let all_selections = editor.selections.all_adjusted(cx); + assert_eq!( + all_selections.len(), + 1, + "Expected to have 1 selection (caret) after file finder confirm, but got: {all_selections:?}" + ); + let caret_selection = all_selections.into_iter().next().unwrap(); + assert_eq!(caret_selection.start, caret_selection.end, + "Caret selection should have its start and end at the same position"); + assert_eq!(0, caret_selection.start.row, + "Excessive rows (as in query outside file borders) should get trimmed to last file row"); + assert_eq!(first_file_contents.len(), caret_selection.start.column as usize, + "Excessive columns (as in query outside file borders) should get trimmed to selected row's last column"); + }); +} + +#[gpui::test] +async fn test_matching_cancellation(cx: &mut TestAppContext) { + let app_state = init_test(cx); + app_state + .fs + .as_fake() + .insert_tree( + "/dir", + json!({ + "hello": "", + "goodbye": "", + "halogen-light": "", + "happiness": "", + "height": "", + "hi": "", + "hiccup": "", + }), + ) + .await; + + let project = Project::test(app_state.fs.clone(), ["/dir".as_ref()], cx).await; + + let (picker, _, cx) = build_find_picker(project, cx); + + let query = test_path_like("hi"); + picker + .update(cx, |picker, cx| { + picker.delegate.spawn_search(query.clone(), cx) + }) + .await; + + picker.update(cx, |picker, _cx| { + assert_eq!(picker.delegate.matches.len(), 5) + }); + + picker.update(cx, |picker, cx| { + let delegate = &mut picker.delegate; + assert!( + delegate.matches.history.is_empty(), + "Search matches expected" + ); + let matches = delegate.matches.search.clone(); + + // Simulate a search being cancelled after the time limit, + // returning only a subset of the matches that would have been found. + drop(delegate.spawn_search(query.clone(), cx)); + delegate.set_search_matches( + delegate.latest_search_id, + true, // did-cancel + query.clone(), + vec![matches[1].clone(), matches[3].clone()], + cx, + ); + + // Simulate another cancellation. + drop(delegate.spawn_search(query.clone(), cx)); + delegate.set_search_matches( + delegate.latest_search_id, + true, // did-cancel + query.clone(), + vec![matches[0].clone(), matches[2].clone(), matches[3].clone()], + cx, + ); + + assert!( + delegate.matches.history.is_empty(), + "Search matches expected" + ); + assert_eq!(delegate.matches.search.as_slice(), &matches[0..4]); + }); +} + +#[gpui::test] +async fn test_ignored_root(cx: &mut TestAppContext) { + let app_state = init_test(cx); + app_state + .fs + .as_fake() + .insert_tree( + "/ancestor", + json!({ + ".gitignore": "ignored-root", + "ignored-root": { + "happiness": "", + "height": "", + "hi": "", + "hiccup": "", + }, + "tracked-root": { + ".gitignore": "height", + "happiness": "", + "height": "", + "hi": "", + "hiccup": "", + }, + }), + ) + .await; + + let project = Project::test( + app_state.fs.clone(), + [ + "/ancestor/tracked-root".as_ref(), + "/ancestor/ignored-root".as_ref(), + ], + cx, + ) + .await; + + let (picker, _, cx) = build_find_picker(project, cx); + + picker + .update(cx, |picker, cx| { + picker.delegate.spawn_search(test_path_like("hi"), cx) + }) + .await; + picker.update(cx, |picker, _| assert_eq!(picker.delegate.matches.len(), 7)); +} + +#[gpui::test] +async fn test_single_file_worktrees(cx: &mut TestAppContext) { + let app_state = init_test(cx); + app_state + .fs + .as_fake() + .insert_tree("/root", json!({ "the-parent-dir": { "the-file": "" } })) + .await; + + let project = Project::test( + app_state.fs.clone(), + ["/root/the-parent-dir/the-file".as_ref()], + cx, + ) + .await; + + let (picker, _, cx) = build_find_picker(project, cx); + + // Even though there is only one worktree, that worktree's filename + // is included in the matching, because the worktree is a single file. + picker + .update(cx, |picker, cx| { + picker.delegate.spawn_search(test_path_like("thf"), cx) + }) + .await; + cx.read(|cx| { + let picker = picker.read(cx); + let delegate = &picker.delegate; + assert!( + delegate.matches.history.is_empty(), + "Search matches expected" + ); + let matches = delegate.matches.search.clone(); + assert_eq!(matches.len(), 1); + + let (file_name, file_name_positions, full_path, full_path_positions) = + delegate.labels_for_path_match(&matches[0]); + assert_eq!(file_name, "the-file"); + assert_eq!(file_name_positions, &[0, 1, 4]); + assert_eq!(full_path, "the-file"); + assert_eq!(full_path_positions, &[0, 1, 4]); + }); + + // Since the worktree root is a file, searching for its name followed by a slash does + // not match anything. + picker + .update(cx, |f, cx| { + f.delegate.spawn_search(test_path_like("thf/"), cx) + }) + .await; + picker.update(cx, |f, _| assert_eq!(f.delegate.matches.len(), 0)); +} + +#[gpui::test] +async fn test_path_distance_ordering(cx: &mut TestAppContext) { + let app_state = init_test(cx); + app_state + .fs + .as_fake() + .insert_tree( + "/root", + json!({ + "dir1": { "a.txt": "" }, + "dir2": { + "a.txt": "", + "b.txt": "" + } + }), + ) + .await; + + let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await; + let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project, cx)); + + let worktree_id = cx.read(|cx| { + let worktrees = workspace.read(cx).worktrees(cx).collect::>(); + assert_eq!(worktrees.len(), 1); + WorktreeId::from_usize(worktrees[0].entity_id().as_u64() as usize) + }); + + // When workspace has an active item, sort items which are closer to that item + // first when they have the same name. In this case, b.txt is closer to dir2's a.txt + // so that one should be sorted earlier + let b_path = ProjectPath { + worktree_id, + path: Arc::from(Path::new("dir2/b.txt")), + }; + workspace + .update(cx, |workspace, cx| { + workspace.open_path(b_path, None, true, cx) + }) + .await + .unwrap(); + let finder = open_file_picker(&workspace, cx); + finder + .update(cx, |f, cx| { + f.delegate.spawn_search(test_path_like("a.txt"), cx) + }) + .await; + + finder.update(cx, |f, _| { + let delegate = &f.delegate; + assert!( + delegate.matches.history.is_empty(), + "Search matches expected" + ); + let matches = delegate.matches.search.clone(); + assert_eq!(matches[0].path.as_ref(), Path::new("dir2/a.txt")); + assert_eq!(matches[1].path.as_ref(), Path::new("dir1/a.txt")); + }); +} + +#[gpui::test] +async fn test_search_worktree_without_files(cx: &mut TestAppContext) { + let app_state = init_test(cx); + app_state + .fs + .as_fake() + .insert_tree( + "/root", + json!({ + "dir1": {}, + "dir2": { + "dir3": {} + } + }), + ) + .await; + + let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await; + let (picker, _workspace, cx) = build_find_picker(project, cx); + + picker + .update(cx, |f, cx| { + f.delegate.spawn_search(test_path_like("dir"), cx) + }) + .await; + cx.read(|cx| { + let finder = picker.read(cx); + assert_eq!(finder.delegate.matches.len(), 0); + }); +} + +#[gpui::test] +async fn test_query_history(cx: &mut gpui::TestAppContext) { + let app_state = init_test(cx); + + app_state + .fs + .as_fake() + .insert_tree( + "/src", + json!({ + "test": { + "first.rs": "// First Rust file", + "second.rs": "// Second Rust file", + "third.rs": "// Third Rust file", + } + }), + ) + .await; + + let project = Project::test(app_state.fs.clone(), ["/src".as_ref()], cx).await; + let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project, cx)); + let worktree_id = cx.read(|cx| { + let worktrees = workspace.read(cx).worktrees(cx).collect::>(); + assert_eq!(worktrees.len(), 1); + WorktreeId::from_usize(worktrees[0].entity_id().as_u64() as usize) + }); + + // Open and close panels, getting their history items afterwards. + // Ensure history items get populated with opened items, and items are kept in a certain order. + // The history lags one opened buffer behind, since it's updated in the search panel only on its reopen. + // + // TODO: without closing, the opened items do not propagate their history changes for some reason + // it does work in real app though, only tests do not propagate. + workspace.update(cx, |_, cx| cx.focused()); + + let initial_history = open_close_queried_buffer("fir", 1, "first.rs", &workspace, cx).await; + assert!( + initial_history.is_empty(), + "Should have no history before opening any files" + ); + + let history_after_first = + open_close_queried_buffer("sec", 1, "second.rs", &workspace, cx).await; + assert_eq!( + history_after_first, + vec![FoundPath::new( + ProjectPath { + worktree_id, + path: Arc::from(Path::new("test/first.rs")), + }, + Some(PathBuf::from("/src/test/first.rs")) + )], + "Should show 1st opened item in the history when opening the 2nd item" + ); + + let history_after_second = + open_close_queried_buffer("thi", 1, "third.rs", &workspace, cx).await; + assert_eq!( + history_after_second, + vec![ + FoundPath::new( + ProjectPath { + worktree_id, + path: Arc::from(Path::new("test/second.rs")), + }, + Some(PathBuf::from("/src/test/second.rs")) + ), + FoundPath::new( + ProjectPath { + worktree_id, + path: Arc::from(Path::new("test/first.rs")), + }, + Some(PathBuf::from("/src/test/first.rs")) + ), + ], + "Should show 1st and 2nd opened items in the history when opening the 3rd item. \ + 2nd item should be the first in the history, as the last opened." + ); + + let history_after_third = + open_close_queried_buffer("sec", 1, "second.rs", &workspace, cx).await; + assert_eq!( + history_after_third, + vec![ + FoundPath::new( + ProjectPath { + worktree_id, + path: Arc::from(Path::new("test/third.rs")), + }, + Some(PathBuf::from("/src/test/third.rs")) + ), + FoundPath::new( + ProjectPath { + worktree_id, + path: Arc::from(Path::new("test/second.rs")), + }, + Some(PathBuf::from("/src/test/second.rs")) + ), + FoundPath::new( + ProjectPath { + worktree_id, + path: Arc::from(Path::new("test/first.rs")), + }, + Some(PathBuf::from("/src/test/first.rs")) + ), + ], + "Should show 1st, 2nd and 3rd opened items in the history when opening the 2nd item again. \ + 3rd item should be the first in the history, as the last opened." + ); + + let history_after_second_again = + open_close_queried_buffer("thi", 1, "third.rs", &workspace, cx).await; + assert_eq!( + history_after_second_again, + vec![ + FoundPath::new( + ProjectPath { + worktree_id, + path: Arc::from(Path::new("test/second.rs")), + }, + Some(PathBuf::from("/src/test/second.rs")) + ), + FoundPath::new( + ProjectPath { + worktree_id, + path: Arc::from(Path::new("test/third.rs")), + }, + Some(PathBuf::from("/src/test/third.rs")) + ), + FoundPath::new( + ProjectPath { + worktree_id, + path: Arc::from(Path::new("test/first.rs")), + }, + Some(PathBuf::from("/src/test/first.rs")) + ), + ], + "Should show 1st, 2nd and 3rd opened items in the history when opening the 3rd item again. \ + 2nd item, as the last opened, 3rd item should go next as it was opened right before." + ); +} + +#[gpui::test] +async fn test_external_files_history(cx: &mut gpui::TestAppContext) { + let app_state = init_test(cx); + + app_state + .fs + .as_fake() + .insert_tree( + "/src", + json!({ + "test": { + "first.rs": "// First Rust file", + "second.rs": "// Second Rust file", + } + }), + ) + .await; + + app_state + .fs + .as_fake() + .insert_tree( + "/external-src", + json!({ + "test": { + "third.rs": "// Third Rust file", + "fourth.rs": "// Fourth Rust file", + } + }), + ) + .await; + + let project = Project::test(app_state.fs.clone(), ["/src".as_ref()], cx).await; + cx.update(|cx| { + project.update(cx, |project, cx| { + project.find_or_create_local_worktree("/external-src", false, cx) + }) + }) + .detach(); + cx.background_executor.run_until_parked(); + + let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project, cx)); + let worktree_id = cx.read(|cx| { + let worktrees = workspace.read(cx).worktrees(cx).collect::>(); + assert_eq!(worktrees.len(), 1,); + + WorktreeId::from_usize(worktrees[0].entity_id().as_u64() as usize) + }); + workspace + .update(cx, |workspace, cx| { + workspace.open_abs_path(PathBuf::from("/external-src/test/third.rs"), false, cx) + }) + .detach(); + cx.background_executor.run_until_parked(); + let external_worktree_id = cx.read(|cx| { + let worktrees = workspace.read(cx).worktrees(cx).collect::>(); + assert_eq!( + worktrees.len(), + 2, + "External file should get opened in a new worktree" + ); + + WorktreeId::from_usize( + worktrees + .into_iter() + .find(|worktree| worktree.entity_id().as_u64() as usize != worktree_id.to_usize()) + .expect("New worktree should have a different id") + .entity_id() + .as_u64() as usize, + ) + }); + cx.dispatch_action(workspace::CloseActiveItem { save_intent: None }); + + let initial_history_items = + open_close_queried_buffer("sec", 1, "second.rs", &workspace, cx).await; + assert_eq!( + initial_history_items, + vec![FoundPath::new( + ProjectPath { + worktree_id: external_worktree_id, + path: Arc::from(Path::new("")), + }, + Some(PathBuf::from("/external-src/test/third.rs")) + )], + "Should show external file with its full path in the history after it was open" + ); + + let updated_history_items = + open_close_queried_buffer("fir", 1, "first.rs", &workspace, cx).await; + assert_eq!( + updated_history_items, + vec![ + FoundPath::new( + ProjectPath { + worktree_id, + path: Arc::from(Path::new("test/second.rs")), + }, + Some(PathBuf::from("/src/test/second.rs")) + ), + FoundPath::new( + ProjectPath { + worktree_id: external_worktree_id, + path: Arc::from(Path::new("")), + }, + Some(PathBuf::from("/external-src/test/third.rs")) + ), + ], + "Should keep external file with history updates", + ); +} + +#[gpui::test] +async fn test_toggle_panel_new_selections(cx: &mut gpui::TestAppContext) { + let app_state = init_test(cx); + + app_state + .fs + .as_fake() + .insert_tree( + "/src", + json!({ + "test": { + "first.rs": "// First Rust file", + "second.rs": "// Second Rust file", + "third.rs": "// Third Rust file", + } + }), + ) + .await; + + let project = Project::test(app_state.fs.clone(), ["/src".as_ref()], cx).await; + let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project, cx)); + + // generate some history to select from + open_close_queried_buffer("fir", 1, "first.rs", &workspace, cx).await; + cx.executor().run_until_parked(); + open_close_queried_buffer("sec", 1, "second.rs", &workspace, cx).await; + open_close_queried_buffer("thi", 1, "third.rs", &workspace, cx).await; + let current_history = open_close_queried_buffer("sec", 1, "second.rs", &workspace, cx).await; + + for expected_selected_index in 0..current_history.len() { + cx.dispatch_action(Toggle); + let picker = active_file_picker(&workspace, cx); + let selected_index = picker.update(cx, |picker, _| picker.delegate.selected_index()); + assert_eq!( + selected_index, expected_selected_index, + "Should select the next item in the history" + ); + } + + cx.dispatch_action(Toggle); + let selected_index = workspace.update(cx, |workspace, cx| { + workspace + .active_modal::(cx) + .unwrap() + .read(cx) + .picker + .read(cx) + .delegate + .selected_index() + }); + assert_eq!( + selected_index, 0, + "Should wrap around the history and start all over" + ); +} + +#[gpui::test] +async fn test_search_preserves_history_items(cx: &mut gpui::TestAppContext) { + let app_state = init_test(cx); + + app_state + .fs + .as_fake() + .insert_tree( + "/src", + json!({ + "test": { + "first.rs": "// First Rust file", + "second.rs": "// Second Rust file", + "third.rs": "// Third Rust file", + "fourth.rs": "// Fourth Rust file", + } + }), + ) + .await; + + let project = Project::test(app_state.fs.clone(), ["/src".as_ref()], cx).await; + let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project, cx)); + let worktree_id = cx.read(|cx| { + let worktrees = workspace.read(cx).worktrees(cx).collect::>(); + assert_eq!(worktrees.len(), 1,); + + WorktreeId::from_usize(worktrees[0].entity_id().as_u64() as usize) + }); + + // generate some history to select from + open_close_queried_buffer("fir", 1, "first.rs", &workspace, cx).await; + open_close_queried_buffer("sec", 1, "second.rs", &workspace, cx).await; + open_close_queried_buffer("thi", 1, "third.rs", &workspace, cx).await; + open_close_queried_buffer("sec", 1, "second.rs", &workspace, cx).await; + + let finder = open_file_picker(&workspace, cx); + let first_query = "f"; + finder + .update(cx, |finder, cx| { + finder.delegate.update_matches(first_query.to_string(), cx) + }) + .await; + finder.update(cx, |finder, _| { + let delegate = &finder.delegate; + assert_eq!(delegate.matches.history.len(), 1, "Only one history item contains {first_query}, it should be present and others should be filtered out"); + let history_match = delegate.matches.history.first().unwrap(); + assert!(history_match.1.is_some(), "Should have path matches for history items after querying"); + assert_eq!(history_match.0, FoundPath::new( + ProjectPath { + worktree_id, + path: Arc::from(Path::new("test/first.rs")), + }, + Some(PathBuf::from("/src/test/first.rs")) + )); + assert_eq!(delegate.matches.search.len(), 1, "Only one non-history item contains {first_query}, it should be present"); + assert_eq!(delegate.matches.search.first().unwrap().path.as_ref(), Path::new("test/fourth.rs")); + }); + + let second_query = "fsdasdsa"; + let finder = active_file_picker(&workspace, cx); + finder + .update(cx, |finder, cx| { + finder.delegate.update_matches(second_query.to_string(), cx) + }) + .await; + finder.update(cx, |finder, _| { + let delegate = &finder.delegate; + assert!( + delegate.matches.history.is_empty(), + "No history entries should match {second_query}" + ); + assert!( + delegate.matches.search.is_empty(), + "No search entries should match {second_query}" + ); + }); + + let first_query_again = first_query; + + let finder = active_file_picker(&workspace, cx); + finder + .update(cx, |finder, cx| { + finder + .delegate + .update_matches(first_query_again.to_string(), cx) + }) + .await; + finder.update(cx, |finder, _| { + let delegate = &finder.delegate; + assert_eq!(delegate.matches.history.len(), 1, "Only one history item contains {first_query_again}, it should be present and others should be filtered out, even after non-matching query"); + let history_match = delegate.matches.history.first().unwrap(); + assert!(history_match.1.is_some(), "Should have path matches for history items after querying"); + assert_eq!(history_match.0, FoundPath::new( + ProjectPath { + worktree_id, + path: Arc::from(Path::new("test/first.rs")), + }, + Some(PathBuf::from("/src/test/first.rs")) + )); + assert_eq!(delegate.matches.search.len(), 1, "Only one non-history item contains {first_query_again}, it should be present, even after non-matching query"); + assert_eq!(delegate.matches.search.first().unwrap().path.as_ref(), Path::new("test/fourth.rs")); + }); +} + +#[gpui::test] +async fn test_history_items_vs_very_good_external_match(cx: &mut gpui::TestAppContext) { + let app_state = init_test(cx); + + app_state + .fs + .as_fake() + .insert_tree( + "/src", + json!({ + "collab_ui": { + "first.rs": "// First Rust file", + "second.rs": "// Second Rust file", + "third.rs": "// Third Rust file", + "collab_ui.rs": "// Fourth Rust file", + } + }), + ) + .await; + + let project = Project::test(app_state.fs.clone(), ["/src".as_ref()], cx).await; + let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project, cx)); + // generate some history to select from + open_close_queried_buffer("fir", 1, "first.rs", &workspace, cx).await; + open_close_queried_buffer("sec", 1, "second.rs", &workspace, cx).await; + open_close_queried_buffer("thi", 1, "third.rs", &workspace, cx).await; + open_close_queried_buffer("sec", 1, "second.rs", &workspace, cx).await; + + let finder = open_file_picker(&workspace, cx); + let query = "collab_ui"; + cx.simulate_input(query); + finder.update(cx, |finder, _| { + let delegate = &finder.delegate; + assert!( + delegate.matches.history.is_empty(), + "History items should not math query {query}, they should be matched by name only" + ); + + let search_entries = delegate + .matches + .search + .iter() + .map(|path_match| path_match.path.to_path_buf()) + .collect::>(); + assert_eq!( + search_entries, + vec![ + PathBuf::from("collab_ui/collab_ui.rs"), + PathBuf::from("collab_ui/third.rs"), + PathBuf::from("collab_ui/first.rs"), + PathBuf::from("collab_ui/second.rs"), + ], + "Despite all search results having the same directory name, the most matching one should be on top" + ); + }); +} + +#[gpui::test] +async fn test_nonexistent_history_items_not_shown(cx: &mut gpui::TestAppContext) { + let app_state = init_test(cx); + + app_state + .fs + .as_fake() + .insert_tree( + "/src", + json!({ + "test": { + "first.rs": "// First Rust file", + "nonexistent.rs": "// Second Rust file", + "third.rs": "// Third Rust file", + } + }), + ) + .await; + + let project = Project::test(app_state.fs.clone(), ["/src".as_ref()], cx).await; + let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project, cx)); // generate some history to select from + open_close_queried_buffer("fir", 1, "first.rs", &workspace, cx).await; + open_close_queried_buffer("non", 1, "nonexistent.rs", &workspace, cx).await; + open_close_queried_buffer("thi", 1, "third.rs", &workspace, cx).await; + open_close_queried_buffer("fir", 1, "first.rs", &workspace, cx).await; + + let picker = open_file_picker(&workspace, cx); + cx.simulate_input("rs"); + + picker.update(cx, |finder, _| { + let history_entries = finder.delegate + .matches + .history + .iter() + .map(|(_, path_match)| path_match.as_ref().expect("should have a path match").path.to_path_buf()) + .collect::>(); + assert_eq!( + history_entries, + vec![ + PathBuf::from("test/first.rs"), + PathBuf::from("test/third.rs"), + ], + "Should have all opened files in the history, except the ones that do not exist on disk" + ); + }); +} + +async fn open_close_queried_buffer( + input: &str, + expected_matches: usize, + expected_editor_title: &str, + workspace: &View, + cx: &mut gpui::VisualTestContext, +) -> Vec { + let picker = open_file_picker(&workspace, cx); + cx.simulate_input(input); + + let history_items = picker.update(cx, |finder, _| { + assert_eq!( + finder.delegate.matches.len(), + expected_matches, + "Unexpected number of matches found for query {input}" + ); + finder.delegate.history_items.clone() + }); + + cx.dispatch_action(SelectNext); + cx.dispatch_action(Confirm); + + cx.read(|cx| { + let active_editor = workspace.read(cx).active_item_as::(cx).unwrap(); + let active_editor_title = active_editor.read(cx).title(cx); + assert_eq!( + expected_editor_title, active_editor_title, + "Unexpected editor title for query {input}" + ); + }); + + cx.dispatch_action(workspace::CloseActiveItem { save_intent: None }); + + history_items +} + +fn init_test(cx: &mut TestAppContext) -> Arc { + cx.update(|cx| { + let state = AppState::test(cx); + theme::init(theme::LoadThemes::JustBase, cx); + language::init(cx); + super::init(cx); + editor::init(cx); + workspace::init_settings(cx); + Project::init_settings(cx); + state + }) +} + +fn test_path_like(test_str: &str) -> PathLikeWithPosition { + PathLikeWithPosition::parse_str(test_str, |path_like_str| { + Ok::<_, std::convert::Infallible>(FileSearchQuery { + raw_query: test_str.to_owned(), + file_query_end: if path_like_str == test_str { + None + } else { + Some(path_like_str.len()) + }, + }) + }) + .unwrap() +} + +fn build_find_picker( + project: Model, + cx: &mut TestAppContext, +) -> ( + View>, + View, + &mut VisualTestContext, +) { + let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project, cx)); + let picker = open_file_picker(&workspace, cx); + (picker, workspace, cx) +} + +#[track_caller] +fn open_file_picker( + workspace: &View, + cx: &mut VisualTestContext, +) -> View> { + cx.dispatch_action(Toggle); + active_file_picker(workspace, cx) +} + +#[track_caller] +fn active_file_picker( + workspace: &View, + cx: &mut VisualTestContext, +) -> View> { + workspace.update(cx, |workspace, cx| { + workspace + .active_modal::(cx) + .unwrap() + .read(cx) + .picker + .clone() + }) +} + +fn collect_search_results(picker: &Picker) -> Vec { + let matches = &picker.delegate.matches; + assert!( + matches.history.is_empty(), + "Should have no history matches, but got: {:?}", + matches.history + ); + let mut results = matches + .search + .iter() + .map(|path_match| Path::new(path_match.path_prefix.as_ref()).join(&path_match.path)) + .collect::>(); + results.sort(); + results +} diff --git a/crates/fs/Cargo.toml b/crates/fs/Cargo.toml index 11a34bcecb2674a652322409a072752cdc167ee6..ef8d2380e2f6bdb422b744ea42d992be2a2f6b31 100644 --- a/crates/fs/Cargo.toml +++ b/crates/fs/Cargo.toml @@ -3,6 +3,8 @@ name = "fs" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/fs.rs" diff --git a/crates/fs/LICENSE-GPL b/crates/fs/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/fs/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/fsevent/Cargo.toml b/crates/fsevent/Cargo.toml index 61207426968bee807991e677d101315e7fea9f25..d5c56e99ada5d55a5eab9f89c0a0d69cf0c7e9fb 100644 --- a/crates/fsevent/Cargo.toml +++ b/crates/fsevent/Cargo.toml @@ -5,6 +5,7 @@ license = "MIT" edition = "2021" publish = false + [lib] path = "src/fsevent.rs" doctest = false diff --git a/crates/fsevent/LICENSE-GPL b/crates/fsevent/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/fsevent/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/fuzzy/Cargo.toml b/crates/fuzzy/Cargo.toml index 553c0497a5815e6e8d024a12dd084cf37c632fbd..f096baf9ba21195915021dad292f493736c05db3 100644 --- a/crates/fuzzy/Cargo.toml +++ b/crates/fuzzy/Cargo.toml @@ -3,6 +3,8 @@ name = "fuzzy" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/fuzzy.rs" diff --git a/crates/fuzzy/LICENSE-GPL b/crates/fuzzy/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/fuzzy/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/git/Cargo.toml b/crates/git/Cargo.toml index 72668ba766dadfaa9238b605d91c797d88de7244..fdb36afeace2b85e4661b2463c1a7c91e80eddcd 100644 --- a/crates/git/Cargo.toml +++ b/crates/git/Cargo.toml @@ -3,6 +3,8 @@ name = "git" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/git.rs" diff --git a/crates/git/LICENSE-GPL b/crates/git/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/git/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/go_to_line/Cargo.toml b/crates/go_to_line/Cargo.toml index 64ec7cebfdadd5f911dc472e6777ada11fa5065e..e44374390761085b15ce58a580fa91bf94a3a049 100644 --- a/crates/go_to_line/Cargo.toml +++ b/crates/go_to_line/Cargo.toml @@ -3,6 +3,8 @@ name = "go_to_line" version = "0.1.0" edition = "2021" publish = false +license = "GPL-3.0-only" + [lib] path = "src/go_to_line.rs" diff --git a/crates/go_to_line/LICENSE-GPL b/crates/go_to_line/LICENSE-GPL new file mode 120000 index 0000000000000000000000000000000000000000..89e542f750cd3860a0598eff0dc34b56d7336dc4 --- /dev/null +++ b/crates/go_to_line/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/gpui/Cargo.toml b/crates/gpui/Cargo.toml index 70608ccb0c549e72b0757a72381e38681ebc4335..4cd4f5c13394931dcfb00bee7f5ad4eea44e9e84 100644 --- a/crates/gpui/Cargo.toml +++ b/crates/gpui/Cargo.toml @@ -5,6 +5,8 @@ edition = "2021" authors = ["Nathan Sobo "] description = "Zed's GPU-accelerated UI framework" publish = false +license = "Apache-2.0" + [features] test-support = ["backtrace", "dhat", "env_logger", "collections/test-support", "util/test-support"] diff --git a/crates/gpui/LICENSE-APACHE b/crates/gpui/LICENSE-APACHE new file mode 120000 index 0000000000000000000000000000000000000000..1cd601d0a3affae83854be02a0afdec3b7a9ec4d --- /dev/null +++ b/crates/gpui/LICENSE-APACHE @@ -0,0 +1 @@ +../../LICENSE-APACHE \ No newline at end of file diff --git a/crates/gpui/README.md b/crates/gpui/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9b087a9752a05586b97a67962b20e86c953672ec --- /dev/null +++ b/crates/gpui/README.md @@ -0,0 +1,40 @@ +# Welcome to GPUI! + +GPUI is a hybrid immediate and retained mode, GPU accelerated, UI framework +for Rust, designed to support a wide variety of applications. + +## Getting Started + +GPUI is still in active development as we work on the Zed code editor and isn't yet on crates.io. You'll also need to use the latest version of stable rust and be on macOS. Add the following to your Cargo.toml: + +``` +gpui = { git = "https://github.com/zed-industries/zed" } +``` + +Everything in GPUI starts with an `App`. You can create one with `App::new()`, and kick off your application by passing a callback to `App::run()`. Inside this callback, you can create a new window with `AppContext::open_window()`, and register your first root view. See [gpui.rs](https://www.gpui.rs/) for a complete example. + +## The Big Picture + +GPUI offers three different registers(https://en.wikipedia.org/wiki/Register_(sociolinguistics)) depending on your needs: + +- State management and communication with Models. Whenever you need to store application state that communicates between different parts of your application, you'll want to use GPUI's models. Models are owned by GPUI and are only accessible through an owned smart pointer similar to an `Rc`. See the `app::model_context` module for more information. + +- High level, declarative UI with Views. All UI in GPUI starts with a View. A view is simply a model that can be rendered, via the `Render` trait. At the start of each frame, GPUI will call this render method on the root view of a given window. Views build a tree of `elements`, lay them out and style them with a tailwind-style API, and then give them to GPUI to turn into pixels. See the `div` element for an all purpose swiss-army knife of rendering. + +- Low level, imperative UI with Elements. Elements are the building blocks of UI in GPUI, and they provide a nice wrapper around an imperative API that provides as much flexibility and control as you need. Elements have total control over how they and their child elements are rendered and and can be used for making efficient views into large lists, implement custom layouting for a code editor, and anything else you can think of. See the `element` module for more information. + +Each of these registers has one or more corresponding contexts that can be accessed from all GPUI services. This context is your main interface to GPUI, and is used extensively throughout the framework. + +## Other Resources + +In addition to the systems above, GPUI provides a range of smaller services that are useful for building complex applications: + +- Actions are user-defined structs that are used for converting keystrokes into logical operations in your UI. Use this for implementing keyboard shortcuts, such as cmd-q. See the `action` module for more information. + +- Platform services, such as `quit the app` or `open a URL` are available as methods on the `app::AppContext`. + +- An async executor that is integrated with the platform's event loop. See the `executor` module for more information., + +- The `[gpui::test]` macro provides a convenient way to write tests for your GPUI applications. Tests also have their own kind of context, a `TestAppContext` which provides ways of simulating common platform input. See `app::test_context` and `test` modules for more details. + +Currently, the best way to learn about these APIs is to read the Zed source code, ask us about it at a fireside hack, or drop a question in the [Zed Discord](https://discord.gg/U4qhCEhMXP). We're working on improving the documentation, creating more examples, and will be publishing more guides to GPUI on our [blog](https://zed.dev/blog). diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 7b349d92568e72b9b4929655e93dd1683636964e..6f75eafaf2a898ecc7d33a4d96365a42e41e712f 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -25,13 +25,12 @@ use crate::{ use anyhow::{anyhow, Result}; use collections::{FxHashMap, FxHashSet, VecDeque}; use futures::{channel::oneshot, future::LocalBoxFuture, Future}; -use parking_lot::Mutex; + use slotmap::SlotMap; use std::{ any::{type_name, TypeId}, cell::{Ref, RefCell, RefMut}, marker::PhantomData, - mem, ops::{Deref, DerefMut}, path::{Path, PathBuf}, rc::{Rc, Weak}, @@ -109,6 +108,7 @@ pub struct App(Rc); /// configured, you'll start the app with `App::run`. impl App { /// Builds an app with the given asset source. + #[allow(clippy::new_without_default)] pub fn new() -> Self { Self(AppContext::new( current_platform(), @@ -224,7 +224,7 @@ pub struct AppContext { pub(crate) entities: EntityMap, pub(crate) new_view_observers: SubscriberSet, pub(crate) windows: SlotMap>, - pub(crate) keymap: Arc>, + pub(crate) keymap: Rc>, pub(crate) global_action_listeners: FxHashMap>>, pending_effects: VecDeque, @@ -242,6 +242,7 @@ pub struct AppContext { } impl AppContext { + #[allow(clippy::new_ret_no_self)] pub(crate) fn new( platform: Rc, asset_source: Arc, @@ -285,7 +286,7 @@ impl AppContext { entities, new_view_observers: SubscriberSet::new(), windows: SlotMap::with_key(), - keymap: Arc::new(Mutex::new(Keymap::default())), + keymap: Rc::new(RefCell::new(Keymap::default())), global_action_listeners: FxHashMap::default(), pending_effects: VecDeque::new(), pending_notifications: FxHashSet::default(), @@ -522,17 +523,22 @@ impl AppContext { } /// Writes credentials to the platform keychain. - pub fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()> { + pub fn write_credentials( + &self, + url: &str, + username: &str, + password: &[u8], + ) -> Task> { self.platform.write_credentials(url, username, password) } /// Reads credentials from the platform keychain. - pub fn read_credentials(&self, url: &str) -> Result)>> { + pub fn read_credentials(&self, url: &str) -> Task)>>> { self.platform.read_credentials(url) } /// Deletes credentials from the platform keychain. - pub fn delete_credentials(&self, url: &str) -> Result<()> { + pub fn delete_credentials(&self, url: &str) -> Task> { self.platform.delete_credentials(url) } @@ -763,7 +769,7 @@ impl AppContext { /// so it can be held across `await` points. pub fn to_async(&self) -> AsyncAppContext { AsyncAppContext { - app: unsafe { mem::transmute(self.this.clone()) }, + app: self.this.clone(), background_executor: self.background_executor.clone(), foreground_executor: self.foreground_executor.clone(), } @@ -996,13 +1002,13 @@ impl AppContext { /// Register key bindings. pub fn bind_keys(&mut self, bindings: impl IntoIterator) { - self.keymap.lock().add_bindings(bindings); + self.keymap.borrow_mut().add_bindings(bindings); self.pending_effects.push_back(Effect::Refresh); } /// Clear all key bindings in the app. pub fn clear_key_bindings(&mut self) { - self.keymap.lock().clear(); + self.keymap.borrow_mut().clear(); self.pending_effects.push_back(Effect::Refresh); } @@ -1106,7 +1112,7 @@ impl AppContext { /// Sets the menu bar for this application. This will replace any existing menu bar. pub fn set_menus(&mut self, menus: Vec) { - self.platform.set_menus(menus, &self.keymap.lock()); + self.platform.set_menus(menus, &self.keymap.borrow()); } /// Dispatch an action to the currently active window or global action handler diff --git a/crates/gpui/src/app/async_context.rs b/crates/gpui/src/app/async_context.rs index 7c36aebf57b0236d54f286789d03b60ec547cab5..f1bfe7ef4ec18da777982b510192523de93d4bd2 100644 --- a/crates/gpui/src/app/async_context.rs +++ b/crates/gpui/src/app/async_context.rs @@ -154,6 +154,7 @@ impl AsyncAppContext { } /// Reads the global state of the specified type, passing it to the given callback. + /// /// Panics if no global state of the specified type has been assigned. /// Returns an error if the `AppContext` has been dropped. pub fn read_global(&self, read: impl FnOnce(&G, &AppContext) -> R) -> Result { @@ -166,7 +167,10 @@ impl AsyncAppContext { } /// Reads the global state of the specified type, passing it to the given callback. - /// Similar to [read_global], but returns an error instead of panicking if no state of the specified type has been assigned. + /// + /// Similar to [`AsyncAppContext::read_global`], but returns an error instead of panicking + /// if no state of the specified type has been assigned. + /// /// Returns an error if no state of the specified type has been assigned the `AppContext` has been dropped. pub fn try_read_global( &self, @@ -212,12 +216,12 @@ impl AsyncWindowContext { self.window } - /// A convenience method for [WindowContext::update()] + /// A convenience method for [`AppContext::update_window`]. pub fn update(&mut self, update: impl FnOnce(&mut WindowContext) -> R) -> Result { self.app.update_window(self.window, |_, cx| update(cx)) } - /// A convenience method for [WindowContext::update()] + /// A convenience method for [`AppContext::update_window`]. pub fn update_root( &mut self, update: impl FnOnce(AnyView, &mut WindowContext) -> R, @@ -225,12 +229,12 @@ impl AsyncWindowContext { self.app.update_window(self.window, update) } - /// A convenience method for [WindowContext::on_next_frame()] + /// A convenience method for [`WindowContext::on_next_frame`]. pub fn on_next_frame(&mut self, f: impl FnOnce(&mut WindowContext) + 'static) { self.window.update(self, |_, cx| cx.on_next_frame(f)).ok(); } - /// A convenience method for [AppContext::global()] + /// A convenience method for [`AppContext::global`]. pub fn read_global( &mut self, read: impl FnOnce(&G, &WindowContext) -> R, @@ -238,7 +242,7 @@ impl AsyncWindowContext { self.window.update(self, |_, cx| read(cx.global(), cx)) } - /// A convenience method for [AppContext::update_global()] + /// A convenience method for [`AppContext::update_global`]. /// for updating the global state of the specified type. pub fn update_global( &mut self, diff --git a/crates/gpui/src/app/entity_map.rs b/crates/gpui/src/app/entity_map.rs index db5d844d17c173bbc0541d49aaf5f67e70f139a0..6383cdad7b5efba1859f34fc4ce16c11520d4576 100644 --- a/crates/gpui/src/app/entity_map.rs +++ b/crates/gpui/src/app/entity_map.rs @@ -242,7 +242,7 @@ impl Clone for AnyModel { assert_ne!(prev_count, 0, "Detected over-release of a model."); } - let this = Self { + Self { entity_id: self.entity_id, entity_type: self.entity_type, entity_map: self.entity_map.clone(), @@ -254,8 +254,7 @@ impl Clone for AnyModel { .write() .leak_detector .handle_created(self.entity_id), - }; - this + } } } diff --git a/crates/gpui/src/app/model_context.rs b/crates/gpui/src/app/model_context.rs index 268410245e95aae8e1f1a091f9b98d0ce359dfdb..38caa1b260ffa285702d7381a9ccc0033c7bb917 100644 --- a/crates/gpui/src/app/model_context.rs +++ b/crates/gpui/src/app/model_context.rs @@ -42,7 +42,8 @@ impl<'a, T: 'static> ModelContext<'a, T> { self.model_state.clone() } - /// Arranges for the given function to be called whenever [ModelContext::notify] or [ViewContext::notify] is called with the given model or view. + /// Arranges for the given function to be called whenever [`ModelContext::notify`] or + /// [`ViewContext::notify`](crate::ViewContext::notify) is called with the given model or view. pub fn observe( &mut self, entity: &E, @@ -150,7 +151,7 @@ impl<'a, T: 'static> ModelContext<'a, T> { } /// Arrange for the given function to be invoked whenever the application is quit. - /// The future returned from this callback will be polled for up to [gpui::SHUTDOWN_TIMEOUT] until the app fully quits. + /// The future returned from this callback will be polled for up to [crate::SHUTDOWN_TIMEOUT] until the app fully quits. pub fn on_app_quit( &mut self, mut on_quit: impl FnMut(&mut T, &mut ModelContext) -> Fut + 'static, diff --git a/crates/gpui/src/app/test_context.rs b/crates/gpui/src/app/test_context.rs index 4d588a668c1a06a688b15cc56514f3989b2ee78a..a33105492bfb2fb489a7419b4f3ee9afbe110bc9 100644 --- a/crates/gpui/src/app/test_context.rs +++ b/crates/gpui/src/app/test_context.rs @@ -7,7 +7,7 @@ use crate::{ }; use anyhow::{anyhow, bail}; use futures::{Stream, StreamExt}; -use std::{future::Future, ops::Deref, rc::Rc, sync::Arc, time::Duration}; +use std::{cell::RefCell, future::Future, ops::Deref, rc::Rc, sync::Arc, time::Duration}; /// A TestAppContext is provided to tests created with `#[gpui::test]`, it provides /// an implementation of `Context` with additional methods that are useful in tests. @@ -24,6 +24,7 @@ pub struct TestAppContext { test_platform: Rc, text_system: Arc, fn_name: Option<&'static str>, + on_quit: Rc>>>, } impl Context for TestAppContext { @@ -101,6 +102,7 @@ impl TestAppContext { test_platform: platform, text_system, fn_name, + on_quit: Rc::new(RefCell::new(Vec::default())), } } @@ -119,11 +121,18 @@ impl TestAppContext { Self::new(self.dispatcher.clone(), self.fn_name) } - /// Simulates quitting the app. + /// Called by the test helper to end the test. + /// public so the macro can call it. pub fn quit(&self) { + self.on_quit.borrow_mut().drain(..).for_each(|f| f()); self.app.borrow_mut().shutdown(); } + /// Register cleanup to run when the test ends. + pub fn on_quit(&mut self, f: impl FnOnce() + 'static) { + self.on_quit.borrow_mut().push(Box::new(f)); + } + /// Schedules all windows to be redrawn on the next effect cycle. pub fn refresh(&mut self) -> Result<()> { let mut app = self.app.borrow_mut(); @@ -169,10 +178,9 @@ impl TestAppContext { let mut cx = self.app.borrow_mut(); let window = cx.open_window(WindowOptions::default(), |cx| cx.new_view(|_| ())); drop(cx); - let cx = Box::new(VisualTestContext::from_window(*window.deref(), self)); + let cx = VisualTestContext::from_window(*window.deref(), self).as_mut(); cx.run_until_parked(); - // it might be nice to try and cleanup these at the end of each test. - Box::leak(cx) + cx } /// Adds a new window, and returns its root view and a `VisualTestContext` which can be used @@ -187,10 +195,11 @@ impl TestAppContext { let window = cx.open_window(WindowOptions::default(), |cx| cx.new_view(build_window)); drop(cx); let view = window.root_view(self).unwrap(); - let cx = Box::new(VisualTestContext::from_window(*window.deref(), self)); + let cx = VisualTestContext::from_window(*window.deref(), self).as_mut(); cx.run_until_parked(); + // it might be nice to try and cleanup these at the end of each test. - (view, Box::leak(cx)) + (view, cx) } /// returns the TextSystem @@ -695,6 +704,20 @@ impl<'a> VisualTestContext { false } } + + /// Get an &mut VisualTestContext (which is mostly what you need to pass to other methods). + /// This method internally retains the VisualTestContext until the end of the test. + pub fn as_mut(self) -> &'static mut Self { + let ptr = Box::into_raw(Box::new(self)); + // safety: on_quit will be called after the test has finished. + // the executor will ensure that all tasks related to the test have stopped. + // so there is no way for cx to be accessed after on_quit is called. + let cx = Box::leak(unsafe { Box::from_raw(ptr) }); + cx.on_quit(move || unsafe { + drop(Box::from_raw(ptr)); + }); + cx + } } impl Context for VisualTestContext { diff --git a/crates/gpui/src/arena.rs b/crates/gpui/src/arena.rs index b3d7f9b0ecf530a2dbe2c8f9d4dd286ac971a2d9..4ddeaaff65eb6247595fd15a263a985a01d2ae23 100644 --- a/crates/gpui/src/arena.rs +++ b/crates/gpui/src/arena.rs @@ -44,6 +44,14 @@ impl Arena { } } + pub fn len(&self) -> usize { + self.offset as usize - self.start as usize + } + + pub fn capacity(&self) -> usize { + self.end as usize - self.start as usize + } + pub fn clear(&mut self) { self.valid.set(false); self.valid = Rc::new(Cell::new(true)); diff --git a/crates/gpui/src/color.rs b/crates/gpui/src/color.rs index e76c31d6f15db5a7690aab3bafd0b950f79e2824..caf7cddf69a09c314096e56115de00f60be7aac5 100644 --- a/crates/gpui/src/color.rs +++ b/crates/gpui/src/color.rs @@ -3,11 +3,11 @@ use serde::de::{self, Deserialize, Deserializer, Visitor}; use std::fmt; /// Convert an RGB hex color code number to a color type -pub fn rgb>(hex: u32) -> C { +pub fn rgb(hex: u32) -> Rgba { let r = ((hex >> 16) & 0xFF) as f32 / 255.0; let g = ((hex >> 8) & 0xFF) as f32 / 255.0; let b = (hex & 0xFF) as f32 / 255.0; - Rgba { r, g, b, a: 1.0 }.into() + Rgba { r, g, b, a: 1.0 } } /// Convert an RGBA hex color code number to [`Rgba`] @@ -40,7 +40,6 @@ impl fmt::Debug for Rgba { impl Rgba { /// Create a new [`Rgba`] color by blending this and another color together - /// TODO!(docs): find the source for this algorithm pub fn blend(&self, other: Rgba) -> Self { if other.a >= 1.0 { other @@ -203,20 +202,16 @@ impl PartialEq for Hsla { impl PartialOrd for Hsla { fn partial_cmp(&self, other: &Self) -> Option { - // SAFETY: The total ordering relies on this always being Some() - Some( - self.h - .total_cmp(&other.h) - .then(self.s.total_cmp(&other.s)) - .then(self.l.total_cmp(&other.l).then(self.a.total_cmp(&other.a))), - ) + Some(self.cmp(other)) } } impl Ord for Hsla { fn cmp(&self, other: &Self) -> std::cmp::Ordering { - // SAFETY: The partial comparison is a total comparison - unsafe { self.partial_cmp(other).unwrap_unchecked() } + self.h + .total_cmp(&other.h) + .then(self.s.total_cmp(&other.s)) + .then(self.l.total_cmp(&other.l).then(self.a.total_cmp(&other.a))) } } diff --git a/crates/gpui/src/element.rs b/crates/gpui/src/element.rs index cd5e6ea9dcd2af407e1de2cfa2f6f25e3e6c392c..b91d5381a0e8cb1476042d52a0c4e6085902d5a2 100644 --- a/crates/gpui/src/element.rs +++ b/crates/gpui/src/element.rs @@ -133,8 +133,25 @@ pub trait Render: 'static + Sized { } impl Render for () { - fn render(&mut self, _cx: &mut ViewContext) -> impl IntoElement { - () + fn render(&mut self, _cx: &mut ViewContext) -> impl IntoElement {} +} + +/// A quick way to create a [`Render`]able view without having to define a new type. +#[cfg(any(test, feature = "test-support"))] +pub struct TestView(Box) -> AnyElement>); + +#[cfg(any(test, feature = "test-support"))] +impl TestView { + /// Construct a TestView from a render closure. + pub fn new) -> AnyElement + 'static>(f: F) -> Self { + Self(Box::new(f)) + } +} + +#[cfg(any(test, feature = "test-support"))] +impl Render for TestView { + fn render(&mut self, cx: &mut ViewContext) -> impl IntoElement { + (self.0)(cx) } } diff --git a/crates/gpui/src/elements/div.rs b/crates/gpui/src/elements/div.rs index b1d936546b91f0387bfacf907d309a78d79d4a44..a0bbf6fc7948095adcc175f9cfb81ef3c1c88743 100644 --- a/crates/gpui/src/elements/div.rs +++ b/crates/gpui/src/elements/div.rs @@ -1,7 +1,7 @@ //! Div is the central, reusable element that most GPUI trees will be built from. //! It functions as a container for other elements, and provides a number of //! useful features for laying out and styling its children as well as binding -//! mouse events and action handlers. It is meant to be similar to the HTML
+//! mouse events and action handlers. It is meant to be similar to the HTML `
` //! element, but for GPUI. //! //! # Build your own div @@ -14,13 +14,6 @@ //! as several associated traits. Together, these provide the full suite of Dom-like events //! and Tailwind-like styling that you can use to build your own custom elements. Div is //! constructed by combining these two systems into an all-in-one element. -//! -//! # Capturing and bubbling -//! -//! Note that while event dispatch in GPUI uses similar names and concepts to the web -//! even API, the details are very different. See the documentation in [TODO!(docs) -//! DOCUMENT EVENT DISPATCH SOMEWHERE IN WINDOW CONTEXT] for more details -//! use crate::{ point, px, size, Action, AnyDrag, AnyElement, AnyTooltip, AnyView, AppContext, Bounds, @@ -85,7 +78,7 @@ impl Interactivity { /// Bind the given callback to the mouse down event for the given mouse button, during the bubble phase /// The imperative API equivalent of [`InteractiveElement::on_mouse_down`] /// - /// See [`ViewContext::listener()`] to get access to the view state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to the view state from this callback. pub fn on_mouse_down( &mut self, button: MouseButton, @@ -105,7 +98,7 @@ impl Interactivity { /// Bind the given callback to the mouse down event for any button, during the capture phase /// The imperative API equivalent of [`InteractiveElement::capture_any_mouse_down`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn capture_any_mouse_down( &mut self, listener: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static, @@ -119,9 +112,9 @@ impl Interactivity { } /// Bind the given callback to the mouse down event for any button, during the bubble phase - /// the imperative API equivalent to [`InteractiveElement::on_any_mouse_down()`] + /// the imperative API equivalent to [`InteractiveElement::on_any_mouse_down`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn on_any_mouse_down( &mut self, listener: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static, @@ -135,9 +128,9 @@ impl Interactivity { } /// Bind the given callback to the mouse up event for the given button, during the bubble phase - /// the imperative API equivalent to [`InteractiveElement::on_mouse_up()`] + /// the imperative API equivalent to [`InteractiveElement::on_mouse_up`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn on_mouse_up( &mut self, button: MouseButton, @@ -155,9 +148,9 @@ impl Interactivity { } /// Bind the given callback to the mouse up event for any button, during the capture phase - /// the imperative API equivalent to [`InteractiveElement::capture_any_mouse_up()`] + /// the imperative API equivalent to [`InteractiveElement::capture_any_mouse_up`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn capture_any_mouse_up( &mut self, listener: impl Fn(&MouseUpEvent, &mut WindowContext) + 'static, @@ -171,9 +164,9 @@ impl Interactivity { } /// Bind the given callback to the mouse up event for any button, during the bubble phase - /// the imperative API equivalent to [`InteractiveElement::on_any_mouse_up()`] + /// the imperative API equivalent to [`Interactivity::on_any_mouse_up`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn on_any_mouse_up( &mut self, listener: impl Fn(&MouseUpEvent, &mut WindowContext) + 'static, @@ -188,9 +181,9 @@ impl Interactivity { /// Bind the given callback to the mouse down event, on any button, during the capture phase, /// when the mouse is outside of the bounds of this element. - /// The imperative API equivalent to [`InteractiveElement::on_mouse_down_out()`] + /// The imperative API equivalent to [`InteractiveElement::on_mouse_down_out`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn on_mouse_down_out( &mut self, listener: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static, @@ -206,9 +199,9 @@ impl Interactivity { /// Bind the given callback to the mouse up event, for the given button, during the capture phase, /// when the mouse is outside of the bounds of this element. - /// The imperative API equivalent to [`InteractiveElement::on_mouse_up_out()`] + /// The imperative API equivalent to [`InteractiveElement::on_mouse_up_out`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn on_mouse_up_out( &mut self, button: MouseButton, @@ -226,9 +219,9 @@ impl Interactivity { } /// Bind the given callback to the mouse move event, during the bubble phase - /// The imperative API equivalent to [`InteractiveElement::on_mouse_move()`] + /// The imperative API equivalent to [`InteractiveElement::on_mouse_move`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn on_mouse_move( &mut self, listener: impl Fn(&MouseMoveEvent, &mut WindowContext) + 'static, @@ -245,9 +238,9 @@ impl Interactivity { /// will be called for all move events, inside or outside of this element, as long as the /// drag was started with this element under the mouse. Useful for implementing draggable /// UIs that don't conform to a drag and drop style interaction, like resizing. - /// The imperative API equivalent to [`InteractiveElement::on_drag_move()`] + /// The imperative API equivalent to [`InteractiveElement::on_drag_move`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn on_drag_move( &mut self, listener: impl Fn(&DragMoveEvent, &mut WindowContext) + 'static, @@ -275,9 +268,9 @@ impl Interactivity { } /// Bind the given callback to scroll wheel events during the bubble phase - /// The imperative API equivalent to [`InteractiveElement::on_scroll_wheel()`] + /// The imperative API equivalent to [`InteractiveElement::on_scroll_wheel`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn on_scroll_wheel( &mut self, listener: impl Fn(&ScrollWheelEvent, &mut WindowContext) + 'static, @@ -291,9 +284,9 @@ impl Interactivity { } /// Bind the given callback to an action dispatch during the capture phase - /// The imperative API equivalent to [`InteractiveElement::capture_action()`] + /// The imperative API equivalent to [`InteractiveElement::capture_action`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn capture_action( &mut self, listener: impl Fn(&A, &mut WindowContext) + 'static, @@ -310,9 +303,9 @@ impl Interactivity { } /// Bind the given callback to an action dispatch during the bubble phase - /// The imperative API equivalent to [`InteractiveElement::on_action()`] + /// The imperative API equivalent to [`InteractiveElement::on_action`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn on_action(&mut self, listener: impl Fn(&A, &mut WindowContext) + 'static) { self.action_listeners.push(( TypeId::of::(), @@ -328,9 +321,9 @@ impl Interactivity { /// Bind the given callback to an action dispatch, based on a dynamic action parameter /// instead of a type parameter. Useful for component libraries that want to expose /// action bindings to their users. - /// The imperative API equivalent to [`InteractiveElement::on_boxed_action()`] + /// The imperative API equivalent to [`InteractiveElement::on_boxed_action`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn on_boxed_action( &mut self, action: &dyn Action, @@ -348,9 +341,9 @@ impl Interactivity { } /// Bind the given callback to key down events during the bubble phase - /// The imperative API equivalent to [`InteractiveElement::on_key_down()`] + /// The imperative API equivalent to [`InteractiveElement::on_key_down`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn on_key_down(&mut self, listener: impl Fn(&KeyDownEvent, &mut WindowContext) + 'static) { self.key_down_listeners .push(Box::new(move |event, phase, cx| { @@ -361,9 +354,9 @@ impl Interactivity { } /// Bind the given callback to key down events during the capture phase - /// The imperative API equivalent to [`InteractiveElement::capture_key_down()`] + /// The imperative API equivalent to [`InteractiveElement::capture_key_down`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn capture_key_down( &mut self, listener: impl Fn(&KeyDownEvent, &mut WindowContext) + 'static, @@ -377,9 +370,9 @@ impl Interactivity { } /// Bind the given callback to key up events during the bubble phase - /// The imperative API equivalent to [`InteractiveElement::on_key_up()`] + /// The imperative API equivalent to [`InteractiveElement::on_key_up`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn on_key_up(&mut self, listener: impl Fn(&KeyUpEvent, &mut WindowContext) + 'static) { self.key_up_listeners .push(Box::new(move |event, phase, cx| { @@ -390,9 +383,9 @@ impl Interactivity { } /// Bind the given callback to key up events during the capture phase - /// The imperative API equivalent to [`InteractiveElement::on_key_up()`] + /// The imperative API equivalent to [`InteractiveElement::on_key_up`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn capture_key_up(&mut self, listener: impl Fn(&KeyUpEvent, &mut WindowContext) + 'static) { self.key_up_listeners .push(Box::new(move |event, phase, cx| { @@ -403,9 +396,9 @@ impl Interactivity { } /// Bind the given callback to drop events of the given type, whether or not the drag started on this element - /// The imperative API equivalent to [`InteractiveElement::on_drop()`] + /// The imperative API equivalent to [`InteractiveElement::on_drop`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn on_drop(&mut self, listener: impl Fn(&T, &mut WindowContext) + 'static) { self.drop_listeners.push(( TypeId::of::(), @@ -416,15 +409,15 @@ impl Interactivity { } /// Use the given predicate to determine whether or not a drop event should be dispatched to this element - /// The imperative API equivalent to [`InteractiveElement::can_drop()`] + /// The imperative API equivalent to [`InteractiveElement::can_drop`] pub fn can_drop(&mut self, predicate: impl Fn(&dyn Any, &mut WindowContext) -> bool + 'static) { self.can_drop_predicate = Some(Box::new(predicate)); } /// Bind the given callback to click events of this element - /// The imperative API equivalent to [`InteractiveElement::on_click()`] + /// The imperative API equivalent to [`StatefulInteractiveElement::on_click`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn on_click(&mut self, listener: impl Fn(&ClickEvent, &mut WindowContext) + 'static) where Self: Sized, @@ -435,10 +428,10 @@ impl Interactivity { /// On drag initiation, this callback will be used to create a new view to render the dragged value for a /// drag and drop operation. This API should also be used as the equivalent of 'on drag start' with - /// the [`Self::on_drag_move()`] API - /// The imperative API equivalent to [`InteractiveElement::on_drag()`] + /// the [`Self::on_drag_move`] API + /// The imperative API equivalent to [`StatefulInteractiveElement::on_drag`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn on_drag( &mut self, value: T, @@ -460,9 +453,9 @@ impl Interactivity { /// Bind the given callback on the hover start and end events of this element. Note that the boolean /// passed to the callback is true when the hover starts and false when it ends. - /// The imperative API equivalent to [`InteractiveElement::on_drag()`] + /// The imperative API equivalent to [`StatefulInteractiveElement::on_drag`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. pub fn on_hover(&mut self, listener: impl Fn(&bool, &mut WindowContext) + 'static) where Self: Sized, @@ -475,7 +468,7 @@ impl Interactivity { } /// Use the given callback to construct a new tooltip view when the mouse hovers over this element. - /// The imperative API equivalent to [`InteractiveElement::tooltip()`] + /// The imperative API equivalent to [`InteractiveElement::tooltip`] pub fn tooltip(&mut self, build_tooltip: impl Fn(&mut WindowContext) -> AnyView + 'static) where Self: Sized, @@ -488,7 +481,7 @@ impl Interactivity { } /// Block the mouse from interacting with this element or any of it's children - /// The imperative API equivalent to [`InteractiveElement::block_mouse()`] + /// The imperative API equivalent to [`InteractiveElement::block_mouse`] pub fn block_mouse(&mut self) { self.block_mouse = true; } @@ -559,9 +552,9 @@ pub trait InteractiveElement: Sized { } /// Bind the given callback to the mouse down event for the given mouse button, - /// the fluent API equivalent to [`Interactivity::on_mouse_down()`] + /// the fluent API equivalent to [`Interactivity::on_mouse_down`] /// - /// See [`ViewContext::listener()`] to get access to the view state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to the view state from this callback. fn on_mouse_down( mut self, button: MouseButton, @@ -573,7 +566,7 @@ pub trait InteractiveElement: Sized { #[cfg(any(test, feature = "test-support"))] /// Set a key that can be used to look up this element's bounds - /// in the [`VisualTestContext::debug_bounds()`] map + /// in the [`VisualTestContext::debug_bounds`] map /// This is a noop in release builds fn debug_selector(mut self, f: impl FnOnce() -> String) -> Self { self.interactivity().debug_selector = Some(f()); @@ -582,7 +575,7 @@ pub trait InteractiveElement: Sized { #[cfg(not(any(test, feature = "test-support")))] /// Set a key that can be used to look up this element's bounds - /// in the [`VisualTestContext::debug_bounds()`] map + /// in the [`VisualTestContext::debug_bounds`] map /// This is a noop in release builds #[inline] fn debug_selector(self, _: impl FnOnce() -> String) -> Self { @@ -590,9 +583,9 @@ pub trait InteractiveElement: Sized { } /// Bind the given callback to the mouse down event for any button, during the capture phase - /// the fluent API equivalent to [`Interactivity::capture_any_mouse_down()`] + /// the fluent API equivalent to [`Interactivity::capture_any_mouse_down`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn capture_any_mouse_down( mut self, listener: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static, @@ -602,9 +595,9 @@ pub trait InteractiveElement: Sized { } /// Bind the given callback to the mouse down event for any button, during the capture phase - /// the fluent API equivalent to [`Interactivity::on_any_mouse_down()`] + /// the fluent API equivalent to [`Interactivity::on_any_mouse_down`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn on_any_mouse_down( mut self, listener: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static, @@ -614,9 +607,9 @@ pub trait InteractiveElement: Sized { } /// Bind the given callback to the mouse up event for the given button, during the bubble phase - /// the fluent API equivalent to [`Interactivity::on_mouse_up()`] + /// the fluent API equivalent to [`Interactivity::on_mouse_up`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn on_mouse_up( mut self, button: MouseButton, @@ -627,9 +620,9 @@ pub trait InteractiveElement: Sized { } /// Bind the given callback to the mouse up event for any button, during the capture phase - /// the fluent API equivalent to [`Interactivity::capture_any_mouse_up()`] + /// the fluent API equivalent to [`Interactivity::capture_any_mouse_up`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn capture_any_mouse_up( mut self, listener: impl Fn(&MouseUpEvent, &mut WindowContext) + 'static, @@ -640,9 +633,9 @@ pub trait InteractiveElement: Sized { /// Bind the given callback to the mouse down event, on any button, during the capture phase, /// when the mouse is outside of the bounds of this element. - /// The fluent API equivalent to [`Interactivity::on_mouse_down_out()`] + /// The fluent API equivalent to [`Interactivity::on_mouse_down_out`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn on_mouse_down_out( mut self, listener: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static, @@ -653,9 +646,9 @@ pub trait InteractiveElement: Sized { /// Bind the given callback to the mouse up event, for the given button, during the capture phase, /// when the mouse is outside of the bounds of this element. - /// The fluent API equivalent to [`Interactivity::on_mouse_up_out()`] + /// The fluent API equivalent to [`Interactivity::on_mouse_up_out`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn on_mouse_up_out( mut self, button: MouseButton, @@ -666,9 +659,9 @@ pub trait InteractiveElement: Sized { } /// Bind the given callback to the mouse move event, during the bubble phase - /// The fluent API equivalent to [`Interactivity::on_mouse_move()`] + /// The fluent API equivalent to [`Interactivity::on_mouse_move`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn on_mouse_move( mut self, listener: impl Fn(&MouseMoveEvent, &mut WindowContext) + 'static, @@ -681,9 +674,9 @@ pub trait InteractiveElement: Sized { /// will be called for all move events, inside or outside of this element, as long as the /// drag was started with this element under the mouse. Useful for implementing draggable /// UIs that don't conform to a drag and drop style interaction, like resizing. - /// The fluent API equivalent to [`Interactivity::on_drag_move()`] + /// The fluent API equivalent to [`Interactivity::on_drag_move`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn on_drag_move( mut self, listener: impl Fn(&DragMoveEvent, &mut WindowContext) + 'static, @@ -696,9 +689,9 @@ pub trait InteractiveElement: Sized { } /// Bind the given callback to scroll wheel events during the bubble phase - /// The fluent API equivalent to [`Interactivity::on_scroll_wheel()`] + /// The fluent API equivalent to [`Interactivity::on_scroll_wheel`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn on_scroll_wheel( mut self, listener: impl Fn(&ScrollWheelEvent, &mut WindowContext) + 'static, @@ -708,9 +701,9 @@ pub trait InteractiveElement: Sized { } /// Capture the given action, before normal action dispatch can fire - /// The fluent API equivalent to [`Interactivity::on_scroll_wheel()`] + /// The fluent API equivalent to [`Interactivity::on_scroll_wheel`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn capture_action( mut self, listener: impl Fn(&A, &mut WindowContext) + 'static, @@ -720,9 +713,9 @@ pub trait InteractiveElement: Sized { } /// Bind the given callback to an action dispatch during the bubble phase - /// The fluent API equivalent to [`Interactivity::on_action()`] + /// The fluent API equivalent to [`Interactivity::on_action`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn on_action(mut self, listener: impl Fn(&A, &mut WindowContext) + 'static) -> Self { self.interactivity().on_action(listener); self @@ -731,9 +724,9 @@ pub trait InteractiveElement: Sized { /// Bind the given callback to an action dispatch, based on a dynamic action parameter /// instead of a type parameter. Useful for component libraries that want to expose /// action bindings to their users. - /// The fluent API equivalent to [`Interactivity::on_boxed_action()`] + /// The fluent API equivalent to [`Interactivity::on_boxed_action`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn on_boxed_action( mut self, action: &dyn Action, @@ -744,9 +737,9 @@ pub trait InteractiveElement: Sized { } /// Bind the given callback to key down events during the bubble phase - /// The fluent API equivalent to [`Interactivity::on_key_down()`] + /// The fluent API equivalent to [`Interactivity::on_key_down`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn on_key_down( mut self, listener: impl Fn(&KeyDownEvent, &mut WindowContext) + 'static, @@ -756,9 +749,9 @@ pub trait InteractiveElement: Sized { } /// Bind the given callback to key down events during the capture phase - /// The fluent API equivalent to [`Interactivity::capture_key_down()`] + /// The fluent API equivalent to [`Interactivity::capture_key_down`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn capture_key_down( mut self, listener: impl Fn(&KeyDownEvent, &mut WindowContext) + 'static, @@ -768,18 +761,18 @@ pub trait InteractiveElement: Sized { } /// Bind the given callback to key up events during the bubble phase - /// The fluent API equivalent to [`Interactivity::on_key_up()`] + /// The fluent API equivalent to [`Interactivity::on_key_up`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn on_key_up(mut self, listener: impl Fn(&KeyUpEvent, &mut WindowContext) + 'static) -> Self { self.interactivity().on_key_up(listener); self } /// Bind the given callback to key up events during the capture phase - /// The fluent API equivalent to [`Interactivity::capture_key_up()`] + /// The fluent API equivalent to [`Interactivity::capture_key_up`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn capture_key_up( mut self, listener: impl Fn(&KeyUpEvent, &mut WindowContext) + 'static, @@ -813,16 +806,16 @@ pub trait InteractiveElement: Sized { } /// Bind the given callback to drop events of the given type, whether or not the drag started on this element - /// The fluent API equivalent to [`Interactivity::on_drop()`] + /// The fluent API equivalent to [`Interactivity::on_drop`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn on_drop(mut self, listener: impl Fn(&T, &mut WindowContext) + 'static) -> Self { self.interactivity().on_drop(listener); self } /// Use the given predicate to determine whether or not a drop event should be dispatched to this element - /// The fluent API equivalent to [`Interactivity::can_drop()`] + /// The fluent API equivalent to [`Interactivity::can_drop`] fn can_drop( mut self, predicate: impl Fn(&dyn Any, &mut WindowContext) -> bool + 'static, @@ -832,7 +825,7 @@ pub trait InteractiveElement: Sized { } /// Block the mouse from interacting with this element or any of it's children - /// The fluent API equivalent to [`Interactivity::block_mouse()`] + /// The fluent API equivalent to [`Interactivity::block_mouse`] fn block_mouse(mut self) -> Self { self.interactivity().block_mouse(); self @@ -899,9 +892,9 @@ pub trait StatefulInteractiveElement: InteractiveElement { } /// Bind the given callback to click events of this element - /// The fluent API equivalent to [`Interactivity::on_click()`] + /// The fluent API equivalent to [`Interactivity::on_click`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn on_click(mut self, listener: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self where Self: Sized, @@ -912,10 +905,10 @@ pub trait StatefulInteractiveElement: InteractiveElement { /// On drag initiation, this callback will be used to create a new view to render the dragged value for a /// drag and drop operation. This API should also be used as the equivalent of 'on drag start' with - /// the [`Self::on_drag_move()`] API - /// The fluent API equivalent to [`Interactivity::on_drag()`] + /// the [`Self::on_drag_move`] API + /// The fluent API equivalent to [`Interactivity::on_drag`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn on_drag( mut self, value: T, @@ -932,9 +925,9 @@ pub trait StatefulInteractiveElement: InteractiveElement { /// Bind the given callback on the hover start and end events of this element. Note that the boolean /// passed to the callback is true when the hover starts and false when it ends. - /// The fluent API equivalent to [`Interactivity::on_hover()`] + /// The fluent API equivalent to [`Interactivity::on_hover`] /// - /// See [`ViewContext::listener()`] to get access to a view's state from this callback + /// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback. fn on_hover(mut self, listener: impl Fn(&bool, &mut WindowContext) + 'static) -> Self where Self: Sized, @@ -944,7 +937,7 @@ pub trait StatefulInteractiveElement: InteractiveElement { } /// Use the given callback to construct a new tooltip view when the mouse hovers over this element. - /// The fluent API equivalent to [`Interactivity::tooltip()`] + /// The fluent API equivalent to [`Interactivity::tooltip`] fn tooltip(mut self, build_tooltip: impl Fn(&mut WindowContext) -> AnyView + 'static) -> Self where Self: Sized, @@ -1008,10 +1001,9 @@ pub(crate) type ActionListener = Box Div { #[cfg(debug_assertions)] - let interactivity = { - let mut interactivity = Interactivity::default(); - interactivity.location = Some(*core::panic::Location::caller()); - interactivity + let interactivity = Interactivity { + location: Some(*core::panic::Location::caller()), + ..Default::default() }; #[cfg(not(debug_assertions))] diff --git a/crates/gpui/src/elements/img.rs b/crates/gpui/src/elements/img.rs index 191cadf2939834eb68c4b70abf18b92c8bdc6ff8..e7377373fe18cd7d6d26a610c2d5b776e02874fc 100644 --- a/crates/gpui/src/elements/img.rs +++ b/crates/gpui/src/elements/img.rs @@ -104,7 +104,7 @@ impl Element for Img { cx.with_z_index(1, |cx| { match source { ImageSource::Uri(uri) => { - let image_future = cx.image_cache.get(uri.clone()); + let image_future = cx.image_cache.get(uri.clone(), cx); if let Some(data) = image_future .clone() .now_or_never() diff --git a/crates/gpui/src/elements/overlay.rs b/crates/gpui/src/elements/overlay.rs index 61c34bd9385f6580feb3930dd3b3ea4b1494e919..9db75b75ba0d8138d75fd4a9aa90071e15689a10 100644 --- a/crates/gpui/src/elements/overlay.rs +++ b/crates/gpui/src/elements/overlay.rs @@ -222,7 +222,7 @@ impl OverlayPositionMode { ) -> (Point, Bounds) { match self { OverlayPositionMode::Window => { - let anchor_position = anchor_position.unwrap_or_else(|| bounds.origin); + let anchor_position = anchor_position.unwrap_or(bounds.origin); let bounds = anchor_corner.get_bounds(anchor_position, size); (anchor_position, bounds) } diff --git a/crates/gpui/src/elements/text.rs b/crates/gpui/src/elements/text.rs index 13fc6200766edc068af5f23defc3d9c2e73c4214..2b5bf9166ef2182dca7cae92f93a3e7a91af5925 100644 --- a/crates/gpui/src/elements/text.rs +++ b/crates/gpui/src/elements/text.rs @@ -46,6 +46,18 @@ impl IntoElement for &'static str { } } +impl IntoElement for String { + type Element = SharedString; + + fn element_id(&self) -> Option { + None + } + + fn into_element(self) -> Self::Element { + self.into() + } +} + impl Element for SharedString { type State = TextState; diff --git a/crates/gpui/src/elements/uniform_list.rs b/crates/gpui/src/elements/uniform_list.rs index 108e669f7550a2e6387641aaa4c1dae6e59f6313..ce32b993a5592732f763712b84fa33f24e2738f4 100644 --- a/crates/gpui/src/elements/uniform_list.rs +++ b/crates/gpui/src/elements/uniform_list.rs @@ -181,7 +181,7 @@ impl Element for UniformList { let shared_scroll_offset = element_state .interactive .scroll_offset - .get_or_insert_with(|| Rc::default()) + .get_or_insert_with(Rc::default) .clone(); let item_height = self.measure_item(Some(padded_bounds.size.width), cx).height; diff --git a/crates/gpui/src/geometry.rs b/crates/gpui/src/geometry.rs index d986f26be4475a72b0da8b75de8b3847e3fce588..dd826b68f541c98c88d364156dafc641bdbc8afa 100644 --- a/crates/gpui/src/geometry.rs +++ b/crates/gpui/src/geometry.rs @@ -1,3 +1,7 @@ +//! The GPUI geometry module is a collection of types and traits that +//! can be used to describe common units, concepts, and the relationships +//! between them. + use core::fmt::Debug; use derive_more::{Add, AddAssign, Div, DivAssign, Mul, Neg, Sub, SubAssign}; use refineable::Refineable; @@ -8,13 +12,17 @@ use std::{ ops::{Add, Div, Mul, MulAssign, Sub}, }; +/// An axis along which a measurement can be made. #[derive(Copy, Clone, PartialEq, Eq, Debug)] pub enum Axis { + /// The y axis, or up and down Vertical, + /// The x axis, or left and right Horizontal, } impl Axis { + /// Swap this axis to the opposite axis. pub fn invert(&self) -> Self { match self { Axis::Vertical => Axis::Horizontal, @@ -23,11 +31,15 @@ impl Axis { } } +/// A trait for accessing the given unit along a certain axis. pub trait Along { + /// The unit associated with this type type Unit; + /// Returns the unit along the given axis. fn along(&self, axis: Axis) -> Self::Unit; + /// Applies the given function to the unit along the given axis and returns a new value. fn apply_along(&self, axis: Axis, f: impl FnOnce(Self::Unit) -> Self::Unit) -> Self; } @@ -47,7 +59,9 @@ pub trait Along { #[refineable(Debug)] #[repr(C)] pub struct Point { + /// The x coordinate of the point. pub x: T, + /// The y coordinate of the point. pub y: T, } @@ -334,7 +348,9 @@ impl Clone for Point { #[refineable(Debug)] #[repr(C)] pub struct Size { + /// The width component of the size. pub width: T, + /// The height component of the size. pub height: T, } @@ -640,7 +656,9 @@ impl Size { #[refineable(Debug)] #[repr(C)] pub struct Bounds { + /// The origin point of this area. pub origin: Point, + /// The size of the rectangle. pub size: Size, } @@ -1192,9 +1210,13 @@ impl Copy for Bounds {} #[refineable(Debug)] #[repr(C)] pub struct Edges { + /// The size of the top edge. pub top: T, + /// The size of the right edge. pub right: T, + /// The size of the bottom edge. pub bottom: T, + /// The size of the left edge. pub left: T, } @@ -1600,9 +1622,13 @@ impl From for Edges { #[refineable(Debug)] #[repr(C)] pub struct Corners { + /// The value associated with the top left corner. pub top_left: T, + /// The value associated with the top right corner. pub top_right: T, + /// The value associated with the bottom right corner. pub bottom_right: T, + /// The value associated with the bottom left corner. pub bottom_left: T, } @@ -2020,13 +2046,13 @@ impl Eq for Pixels {} impl PartialOrd for Pixels { fn partial_cmp(&self, other: &Self) -> Option { - self.0.partial_cmp(&other.0) + Some(self.cmp(other)) } } impl Ord for Pixels { fn cmp(&self, other: &Self) -> cmp::Ordering { - self.partial_cmp(other).unwrap() + self.0.total_cmp(&other.0) } } diff --git a/crates/gpui/src/gpui.rs b/crates/gpui/src/gpui.rs index 929e9ebfb4f262088e90e7ab5b99477a9933d998..638e94de3969360f94b5594f06e45710cace1495 100644 --- a/crates/gpui/src/gpui.rs +++ b/crates/gpui/src/gpui.rs @@ -1,30 +1,67 @@ //! # Welcome to GPUI! //! //! GPUI is a hybrid immediate and retained mode, GPU accelerated, UI framework -//! for Rust, designed to support a wide variety of applications. GPUI is currently -//! being actively developed and improved for the [Zed code editor](https://zed.dev/), and new versions -//! will have breaking changes. You'll probably need to use the latest stable version -//! of rust to use GPUI. +//! for Rust, designed to support a wide variety of applications. //! -//! # Getting started with GPUI +//! ## Getting Started //! -//! TODO!(docs): Write a code sample showing how to create a window and render a simple -//! div +//! GPUI is still in active development as we work on the Zed code editor and isn't yet on crates.io. +//! You'll also need to use the latest version of stable rust and be on macOS. Add the following to your +//! Cargo.toml: //! -//! # Drawing interesting things +//! ``` +//! gpui = { git = "https://github.com/zed-industries/zed" } +//! ``` //! -//! TODO!(docs): Expand demo to show how to draw a more interesting scene, with -//! a counter to store state and a button to increment it. +//! Everything in GPUI starts with an [`App`]. You can create one with [`App::new`], and +//! kick off your application by passing a callback to [`App::run`]. Inside this callback, +//! you can create a new window with [`AppContext::open_window`], and register your first root +//! view. See [gpui.rs](https://www.gpui.rs/) for a complete example. //! -//! # Interacting with your application state +//! ## The Big Picture //! -//! TODO!(docs): Expand demo to show GPUI entity interactions, like subscriptions and entities -//! maybe make a network request to show async stuff? +//! GPUI offers three different [registers](https://en.wikipedia.org/wiki/Register_(sociolinguistics)) depending on your needs: //! -//! # Conclusion +//! - State management and communication with Models. Whenever you need to store application state +//! that communicates between different parts of your application, you'll want to use GPUI's +//! models. Models are owned by GPUI and are only accessible through an owned smart pointer +//! similar to an [`Rc`]. See the [`app::model_context`] module for more information. //! -//! TODO!(docs): Wrap up with a conclusion and links to other places? Zed / GPUI website? -//! Discord for chatting about it? Other tutorials or references? +//! - High level, declarative UI with Views. All UI in GPUI starts with a View. A view is simply +//! a model that can be rendered, via the [`Render`] trait. At the start of each frame, GPUI +//! will call this render method on the root view of a given window. Views build a tree of +//! `elements`, lay them out and style them with a tailwind-style API, and then give them to +//! GPUI to turn into pixels. See the [`elements::Div`] element for an all purpose swiss-army +//! knife for UI. +//! +//! - Low level, imperative UI with Elements. Elements are the building blocks of UI in GPUI, and they +//! provide a nice wrapper around an imperative API that provides as much flexibility and control as +//! you need. Elements have total control over how they and their child elements are rendered and and +//! can be used for making efficient views into large lists, implement custom layouting for a code editor, +//! and anything else you can think of. See the [`element`] module for more information. +//! +//! Each of these registers has one or more corresponding contexts that can be accessed from all GPUI services. +//! This context is your main interface to GPUI, and is used extensively throughout the framework. +//! +//! ## Other Resources +//! +//! In addition to the systems above, GPUI provides a range of smaller services that are useful for building +//! complex applications: +//! +//! - Actions are user-defined structs that are used for converting keystrokes into logical operations in your UI. +//! Use this for implementing keyboard shortcuts, such as cmd-q. See the [`action`] module for more information. +//! - Platform services, such as `quit the app` or `open a URL` are available as methods on the [`app::AppContext`]. +//! - An async executor that is integrated with the platform's event loop. See the [`executor`] module for more information., +//! - The [gpui::test] macro provides a convenient way to write tests for your GPUI applications. Tests also have their +//! own kind of context, a [`TestAppContext`] which provides ways of simulating common platform input. See [`app::test_context`] +//! and [`test`] modules for more details. +//! +//! Currently, the best way to learn about these APIs is to read the Zed source code, ask us about it at a fireside hack, or drop +//! a question in the [Zed Discord](https://discord.gg/U4qhCEhMXP). We're working on improving the documentation, creating more examples, +//! and will be publishing more guides to GPUI on our [blog](https://zed.dev/blog). + +#![deny(missing_docs)] +#![allow(clippy::type_complexity)] #[macro_use] mod action; diff --git a/crates/gpui/src/image_cache.rs b/crates/gpui/src/image_cache.rs index dd7b7b571e43c20609e6920f0da59093b5aa010f..95b41c3b2c0619c24deb4207e41b01d23e615b21 100644 --- a/crates/gpui/src/image_cache.rs +++ b/crates/gpui/src/image_cache.rs @@ -1,9 +1,6 @@ -use crate::{ImageData, ImageId, SharedUrl}; +use crate::{AppContext, ImageData, ImageId, SharedUrl, Task}; use collections::HashMap; -use futures::{ - future::{BoxFuture, Shared}, - AsyncReadExt, FutureExt, TryFutureExt, -}; +use futures::{future::Shared, AsyncReadExt, FutureExt, TryFutureExt}; use image::ImageError; use parking_lot::Mutex; use std::sync::Arc; @@ -44,10 +41,10 @@ impl From for Error { pub(crate) struct ImageCache { client: Arc, - images: Arc>>, + images: Arc>>, } -type FetchImageFuture = Shared, Error>>>; +type FetchImageTask = Shared, Error>>>; impl ImageCache { pub fn new(client: Arc) -> Self { @@ -57,10 +54,7 @@ impl ImageCache { } } - pub fn get( - &self, - uri: impl Into, - ) -> Shared, Error>>> { + pub fn get(&self, uri: impl Into, cx: &AppContext) -> FetchImageTask { let uri = uri.into(); let mut images = self.images.lock(); @@ -68,36 +62,39 @@ impl ImageCache { Some(future) => future.clone(), None => { let client = self.client.clone(); - let future = { - let uri = uri.clone(); - async move { - let mut response = client.get(uri.as_ref(), ().into(), true).await?; - let mut body = Vec::new(); - response.body_mut().read_to_end(&mut body).await?; + let future = cx + .background_executor() + .spawn( + { + let uri = uri.clone(); + async move { + let mut response = + client.get(uri.as_ref(), ().into(), true).await?; + let mut body = Vec::new(); + response.body_mut().read_to_end(&mut body).await?; - if !response.status().is_success() { - return Err(Error::BadStatus { - status: response.status(), - body: String::from_utf8_lossy(&body).into_owned(), - }); - } - - let format = image::guess_format(&body)?; - let image = - image::load_from_memory_with_format(&body, format)?.into_bgra8(); - Ok(Arc::new(ImageData::new(image))) - } - } - .map_err({ - let uri = uri.clone(); + if !response.status().is_success() { + return Err(Error::BadStatus { + status: response.status(), + body: String::from_utf8_lossy(&body).into_owned(), + }); + } - move |error| { - log::log!(log::Level::Error, "{:?} {:?}", &uri, &error); - error - } - }) - .boxed() - .shared(); + let format = image::guess_format(&body)?; + let image = image::load_from_memory_with_format(&body, format)? + .into_bgra8(); + Ok(Arc::new(ImageData::new(image))) + } + } + .map_err({ + let uri = uri.clone(); + move |error| { + log::log!(log::Level::Error, "{:?} {:?}", &uri, &error); + error + } + }), + ) + .shared(); images.insert(uri, future.clone()); future diff --git a/crates/gpui/src/interactive.rs b/crates/gpui/src/interactive.rs index 4faa92ce043ae32e6e795b296daeb9ad281475f1..1bc32717c4b7702ca37ee413092d13e12fc6114f 100644 --- a/crates/gpui/src/interactive.rs +++ b/crates/gpui/src/interactive.rs @@ -343,7 +343,7 @@ impl ExternalPaths { impl Render for ExternalPaths { fn render(&mut self, _: &mut ViewContext) -> impl IntoElement { - () // Intentionally left empty because the platform will render icons for the dragged files + // Intentionally left empty because the platform will render icons for the dragged files } } diff --git a/crates/gpui/src/key_dispatch.rs b/crates/gpui/src/key_dispatch.rs index dd5a7ab84e25e267012de5e9dfb216404b8dfa7d..c6a2e1788453cc1a5c654f0e7c2f4fef70cfabb4 100644 --- a/crates/gpui/src/key_dispatch.rs +++ b/crates/gpui/src/key_dispatch.rs @@ -54,15 +54,24 @@ use crate::{ KeyContext, Keymap, KeymatchResult, Keystroke, KeystrokeMatcher, WindowContext, }; use collections::FxHashMap; -use parking_lot::Mutex; use smallvec::{smallvec, SmallVec}; use std::{ any::{Any, TypeId}, + cell::RefCell, mem, rc::Rc, - sync::Arc, }; +/// KeymatchMode controls how keybindings are resolved in the case of conflicting pending keystrokes. +/// When `Sequenced`, gpui will wait for 1s for sequences to complete. +/// When `Immediate`, gpui will immediately resolve the keybinding. +#[derive(Default, PartialEq)] +pub enum KeymatchMode { + #[default] + Sequenced, + Immediate, +} + #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] pub(crate) struct DispatchNodeId(usize); @@ -73,8 +82,9 @@ pub(crate) struct DispatchTree { focusable_node_ids: FxHashMap, view_node_ids: FxHashMap, keystroke_matchers: FxHashMap, KeystrokeMatcher>, - keymap: Arc>, + keymap: Rc>, action_registry: Rc, + pub(crate) keymatch_mode: KeymatchMode, } #[derive(Default)] @@ -96,7 +106,7 @@ pub(crate) struct DispatchActionListener { } impl DispatchTree { - pub fn new(keymap: Arc>, action_registry: Rc) -> Self { + pub fn new(keymap: Rc>, action_registry: Rc) -> Self { Self { node_stack: Vec::new(), context_stack: Vec::new(), @@ -106,6 +116,7 @@ impl DispatchTree { keystroke_matchers: FxHashMap::default(), keymap, action_registry, + keymatch_mode: KeymatchMode::Sequenced, } } @@ -116,6 +127,7 @@ impl DispatchTree { self.focusable_node_ids.clear(); self.view_node_ids.clear(); self.keystroke_matchers.clear(); + self.keymatch_mode = KeymatchMode::Sequenced; } pub fn push_node( @@ -307,7 +319,7 @@ impl DispatchTree { action: &dyn Action, context_stack: &Vec, ) -> Vec { - let keymap = self.keymap.lock(); + let keymap = self.keymap.borrow(); keymap .bindings_for_action(action) .filter(|binding| { @@ -440,9 +452,7 @@ impl DispatchTree { #[cfg(test)] mod tests { - use std::{rc::Rc, sync::Arc}; - - use parking_lot::Mutex; + use std::{cell::RefCell, rc::Rc}; use crate::{Action, ActionRegistry, DispatchTree, KeyBinding, KeyContext, Keymap}; @@ -496,7 +506,7 @@ mod tests { registry.load_action::(); - let keymap = Arc::new(Mutex::new(keymap)); + let keymap = Rc::new(RefCell::new(keymap)); let tree = DispatchTree::new(keymap, Rc::new(registry)); diff --git a/crates/gpui/src/keymap/keymap.rs b/crates/gpui/src/keymap.rs similarity index 97% rename from crates/gpui/src/keymap/keymap.rs rename to crates/gpui/src/keymap.rs index 2eefbb841ee5be0b499e7b32aaf28aa7da36a072..45e0ebbe951fe4a260cf869787e24322a47b1bd0 100644 --- a/crates/gpui/src/keymap/keymap.rs +++ b/crates/gpui/src/keymap.rs @@ -1,4 +1,12 @@ -use crate::{Action, KeyBinding, KeyBindingContextPredicate, KeyContext, Keystroke, NoAction}; +mod binding; +mod context; +mod matcher; + +pub use binding::*; +pub use context::*; +pub(crate) use matcher::*; + +use crate::{Action, Keystroke, NoAction}; use collections::HashSet; use smallvec::SmallVec; use std::{ diff --git a/crates/gpui/src/keymap/context.rs b/crates/gpui/src/keymap/context.rs index 05ef67ba2bc9d260215fc0df89a8a6e3039450f5..6c22fa9fd69bfacf650e549d786ebfc0cb83de21 100644 --- a/crates/gpui/src/keymap/context.rs +++ b/crates/gpui/src/keymap/context.rs @@ -267,8 +267,8 @@ impl KeyBindingContextPredicate { '(' => { source = skip_whitespace(&source[1..]); let (predicate, rest) = Self::parse_expr(source, 0)?; - if rest.starts_with(')') { - source = skip_whitespace(&rest[1..]); + if let Some(stripped) = rest.strip_prefix(')') { + source = skip_whitespace(stripped); Ok((predicate, source)) } else { Err(anyhow!("expected a ')'")) diff --git a/crates/gpui/src/keymap/matcher.rs b/crates/gpui/src/keymap/matcher.rs index 09ba281a0d7ebc1e032100eb9463f32767afa403..c2dec94a5134d0f5879776f4cf56e2e776437ec1 100644 --- a/crates/gpui/src/keymap/matcher.rs +++ b/crates/gpui/src/keymap/matcher.rs @@ -1,11 +1,10 @@ use crate::{KeyBinding, KeyContext, Keymap, KeymapVersion, Keystroke}; -use parking_lot::Mutex; use smallvec::SmallVec; -use std::sync::Arc; +use std::{cell::RefCell, rc::Rc}; pub(crate) struct KeystrokeMatcher { pending_keystrokes: Vec, - keymap: Arc>, + keymap: Rc>, keymap_version: KeymapVersion, } @@ -15,8 +14,8 @@ pub struct KeymatchResult { } impl KeystrokeMatcher { - pub fn new(keymap: Arc>) -> Self { - let keymap_version = keymap.lock().version(); + pub fn new(keymap: Rc>) -> Self { + let keymap_version = keymap.borrow().version(); Self { pending_keystrokes: Vec::new(), keymap_version, @@ -42,7 +41,8 @@ impl KeystrokeMatcher { keystroke: &Keystroke, context_stack: &[KeyContext], ) -> KeymatchResult { - let keymap = self.keymap.lock(); + let keymap = self.keymap.borrow(); + // Clear pending keystrokes if the keymap has changed since the last matched keystroke. if keymap.version() != self.keymap_version { self.keymap_version = keymap.version(); @@ -72,7 +72,7 @@ impl KeystrokeMatcher { } } - if bindings.len() == 0 && pending_key.is_none() && self.pending_keystrokes.len() > 0 { + if bindings.is_empty() && pending_key.is_none() && !self.pending_keystrokes.is_empty() { drop(keymap); self.pending_keystrokes.remove(0); return self.match_keystroke(keystroke, context_stack); diff --git a/crates/gpui/src/keymap/mod.rs b/crates/gpui/src/keymap/mod.rs deleted file mode 100644 index 6f1a018322b1a7f55349af70729860b9d85bde60..0000000000000000000000000000000000000000 --- a/crates/gpui/src/keymap/mod.rs +++ /dev/null @@ -1,9 +0,0 @@ -mod binding; -mod context; -mod keymap; -mod matcher; - -pub use binding::*; -pub use context::*; -pub use keymap::*; -pub(crate) use matcher::*; diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs index e623742740259f62db547b6b2c4791cec15051ca..3d2679dd7ecabe5d92e31038fa9c5d4a02f9d55c 100644 --- a/crates/gpui/src/platform.rs +++ b/crates/gpui/src/platform.rs @@ -9,7 +9,7 @@ use crate::{ Action, AnyWindowHandle, AsyncWindowContext, BackgroundExecutor, Bounds, DevicePixels, Font, FontId, FontMetrics, FontRun, ForegroundExecutor, GlobalPixels, GlyphId, Keymap, LineLayout, Pixels, PlatformInput, Point, RenderGlyphParams, RenderImageParams, RenderSvgParams, Result, - Scene, SharedString, Size, TaskLabel, WindowContext, + Scene, SharedString, Size, Task, TaskLabel, WindowContext, }; use anyhow::anyhow; use async_task::Runnable; @@ -108,9 +108,9 @@ pub(crate) trait Platform: 'static { fn write_to_clipboard(&self, item: ClipboardItem); fn read_from_clipboard(&self) -> Option; - fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()>; - fn read_credentials(&self, url: &str) -> Result)>>; - fn delete_credentials(&self, url: &str) -> Result<()>; + fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Task>; + fn read_credentials(&self, url: &str) -> Task)>>>; + fn delete_credentials(&self, url: &str) -> Task>; } /// A handle to a platform's display, e.g. a monitor or laptop screen. @@ -397,7 +397,7 @@ impl PlatformInputHandler { let Some(range) = self.handler.selected_text_range(cx) else { return; }; - self.handler.replace_text_in_range(Some(range), &input, cx); + self.handler.replace_text_in_range(Some(range), input, cx); } } diff --git a/crates/gpui/src/platform/mac/platform.rs b/crates/gpui/src/platform/mac/platform.rs index e4a688c2fdb35c1dd3adec829fd66a4baf1e4441..58a759865ead18dda81d4a8f2090dbab6c05e379 100644 --- a/crates/gpui/src/platform/mac/platform.rs +++ b/crates/gpui/src/platform/mac/platform.rs @@ -3,7 +3,7 @@ use crate::{ Action, AnyWindowHandle, BackgroundExecutor, ClipboardItem, CursorStyle, DisplayId, ForegroundExecutor, Keymap, MacDispatcher, MacDisplay, MacDisplayLinker, MacTextSystem, MacWindow, Menu, MenuItem, PathPromptOptions, Platform, PlatformDisplay, PlatformInput, - PlatformTextSystem, PlatformWindow, Result, SemanticVersion, WindowOptions, + PlatformTextSystem, PlatformWindow, Result, SemanticVersion, Task, WindowOptions, }; use anyhow::anyhow; use block::ConcreteBlock; @@ -856,104 +856,115 @@ impl Platform for MacPlatform { } } - fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()> { - let url = CFString::from(url); - let username = CFString::from(username); - let password = CFData::from_buffer(password); - - unsafe { - use security::*; - - // First, check if there are already credentials for the given server. If so, then - // update the username and password. - let mut verb = "updating"; - let mut query_attrs = CFMutableDictionary::with_capacity(2); - query_attrs.set(kSecClass as *const _, kSecClassInternetPassword as *const _); - query_attrs.set(kSecAttrServer as *const _, url.as_CFTypeRef()); - - let mut attrs = CFMutableDictionary::with_capacity(4); - attrs.set(kSecClass as *const _, kSecClassInternetPassword as *const _); - attrs.set(kSecAttrServer as *const _, url.as_CFTypeRef()); - attrs.set(kSecAttrAccount as *const _, username.as_CFTypeRef()); - attrs.set(kSecValueData as *const _, password.as_CFTypeRef()); - - let mut status = SecItemUpdate( - query_attrs.as_concrete_TypeRef(), - attrs.as_concrete_TypeRef(), - ); + fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Task> { + let url = url.to_string(); + let username = username.to_string(); + let password = password.to_vec(); + self.background_executor().spawn(async move { + unsafe { + use security::*; + + let url = CFString::from(url.as_str()); + let username = CFString::from(username.as_str()); + let password = CFData::from_buffer(&password); + + // First, check if there are already credentials for the given server. If so, then + // update the username and password. + let mut verb = "updating"; + let mut query_attrs = CFMutableDictionary::with_capacity(2); + query_attrs.set(kSecClass as *const _, kSecClassInternetPassword as *const _); + query_attrs.set(kSecAttrServer as *const _, url.as_CFTypeRef()); + + let mut attrs = CFMutableDictionary::with_capacity(4); + attrs.set(kSecClass as *const _, kSecClassInternetPassword as *const _); + attrs.set(kSecAttrServer as *const _, url.as_CFTypeRef()); + attrs.set(kSecAttrAccount as *const _, username.as_CFTypeRef()); + attrs.set(kSecValueData as *const _, password.as_CFTypeRef()); + + let mut status = SecItemUpdate( + query_attrs.as_concrete_TypeRef(), + attrs.as_concrete_TypeRef(), + ); - // If there were no existing credentials for the given server, then create them. - if status == errSecItemNotFound { - verb = "creating"; - status = SecItemAdd(attrs.as_concrete_TypeRef(), ptr::null_mut()); - } + // If there were no existing credentials for the given server, then create them. + if status == errSecItemNotFound { + verb = "creating"; + status = SecItemAdd(attrs.as_concrete_TypeRef(), ptr::null_mut()); + } - if status != errSecSuccess { - return Err(anyhow!("{} password failed: {}", verb, status)); + if status != errSecSuccess { + return Err(anyhow!("{} password failed: {}", verb, status)); + } } - } - Ok(()) - } - - fn read_credentials(&self, url: &str) -> Result)>> { - let url = CFString::from(url); - let cf_true = CFBoolean::true_value().as_CFTypeRef(); + Ok(()) + }) + } + + fn read_credentials(&self, url: &str) -> Task)>>> { + let url = url.to_string(); + self.background_executor().spawn(async move { + let url = CFString::from(url.as_str()); + let cf_true = CFBoolean::true_value().as_CFTypeRef(); + + unsafe { + use security::*; + + // Find any credentials for the given server URL. + let mut attrs = CFMutableDictionary::with_capacity(5); + attrs.set(kSecClass as *const _, kSecClassInternetPassword as *const _); + attrs.set(kSecAttrServer as *const _, url.as_CFTypeRef()); + attrs.set(kSecReturnAttributes as *const _, cf_true); + attrs.set(kSecReturnData as *const _, cf_true); + + let mut result = CFTypeRef::from(ptr::null()); + let status = SecItemCopyMatching(attrs.as_concrete_TypeRef(), &mut result); + match status { + security::errSecSuccess => {} + security::errSecItemNotFound | security::errSecUserCanceled => return Ok(None), + _ => return Err(anyhow!("reading password failed: {}", status)), + } - unsafe { - use security::*; - - // Find any credentials for the given server URL. - let mut attrs = CFMutableDictionary::with_capacity(5); - attrs.set(kSecClass as *const _, kSecClassInternetPassword as *const _); - attrs.set(kSecAttrServer as *const _, url.as_CFTypeRef()); - attrs.set(kSecReturnAttributes as *const _, cf_true); - attrs.set(kSecReturnData as *const _, cf_true); - - let mut result = CFTypeRef::from(ptr::null()); - let status = SecItemCopyMatching(attrs.as_concrete_TypeRef(), &mut result); - match status { - security::errSecSuccess => {} - security::errSecItemNotFound | security::errSecUserCanceled => return Ok(None), - _ => return Err(anyhow!("reading password failed: {}", status)), + let result = CFType::wrap_under_create_rule(result) + .downcast::() + .ok_or_else(|| anyhow!("keychain item was not a dictionary"))?; + let username = result + .find(kSecAttrAccount as *const _) + .ok_or_else(|| anyhow!("account was missing from keychain item"))?; + let username = CFType::wrap_under_get_rule(*username) + .downcast::() + .ok_or_else(|| anyhow!("account was not a string"))?; + let password = result + .find(kSecValueData as *const _) + .ok_or_else(|| anyhow!("password was missing from keychain item"))?; + let password = CFType::wrap_under_get_rule(*password) + .downcast::() + .ok_or_else(|| anyhow!("password was not a string"))?; + + Ok(Some((username.to_string(), password.bytes().to_vec()))) } - - let result = CFType::wrap_under_create_rule(result) - .downcast::() - .ok_or_else(|| anyhow!("keychain item was not a dictionary"))?; - let username = result - .find(kSecAttrAccount as *const _) - .ok_or_else(|| anyhow!("account was missing from keychain item"))?; - let username = CFType::wrap_under_get_rule(*username) - .downcast::() - .ok_or_else(|| anyhow!("account was not a string"))?; - let password = result - .find(kSecValueData as *const _) - .ok_or_else(|| anyhow!("password was missing from keychain item"))?; - let password = CFType::wrap_under_get_rule(*password) - .downcast::() - .ok_or_else(|| anyhow!("password was not a string"))?; - - Ok(Some((username.to_string(), password.bytes().to_vec()))) - } + }) } - fn delete_credentials(&self, url: &str) -> Result<()> { - let url = CFString::from(url); + fn delete_credentials(&self, url: &str) -> Task> { + let url = url.to_string(); - unsafe { - use security::*; + self.background_executor().spawn(async move { + unsafe { + use security::*; - let mut query_attrs = CFMutableDictionary::with_capacity(2); - query_attrs.set(kSecClass as *const _, kSecClassInternetPassword as *const _); - query_attrs.set(kSecAttrServer as *const _, url.as_CFTypeRef()); + let url = CFString::from(url.as_str()); + let mut query_attrs = CFMutableDictionary::with_capacity(2); + query_attrs.set(kSecClass as *const _, kSecClassInternetPassword as *const _); + query_attrs.set(kSecAttrServer as *const _, url.as_CFTypeRef()); - let status = SecItemDelete(query_attrs.as_concrete_TypeRef()); + let status = SecItemDelete(query_attrs.as_concrete_TypeRef()); - if status != errSecSuccess { - return Err(anyhow!("delete password failed: {}", status)); + if status != errSecSuccess { + return Err(anyhow!("delete password failed: {}", status)); + } } - } - Ok(()) + Ok(()) + }) } } diff --git a/crates/gpui/src/platform/mac/text_system.rs b/crates/gpui/src/platform/mac/text_system.rs index ba434026f65f9573acaf73c14c6abb21d6448b35..e8c8b45311e628fe96549fca5a4f9a10b399473e 100644 --- a/crates/gpui/src/platform/mac/text_system.rs +++ b/crates/gpui/src/platform/mac/text_system.rs @@ -81,13 +81,11 @@ impl PlatformTextSystem for MacTextSystem { fn all_font_names(&self) -> Vec { let collection = core_text::font_collection::create_for_all_families(); let Some(descriptors) = collection.get_descriptors() else { - return vec![]; + return Vec::new(); }; let mut names = BTreeSet::new(); for descriptor in descriptors.into_iter() { - names.insert(descriptor.font_name()); - names.insert(descriptor.family_name()); - names.insert(descriptor.style_name()); + names.extend(lenient_font_attributes::family_name(&descriptor)); } if let Ok(fonts_in_memory) = self.0.read().memory_source.all_families() { names.extend(fonts_in_memory); @@ -145,7 +143,7 @@ impl PlatformTextSystem for MacTextSystem { fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> Result> { Ok(self.0.read().fonts[font_id.0] - .typographic_bounds(glyph_id.into())? + .typographic_bounds(glyph_id.0)? .into()) } @@ -223,11 +221,11 @@ impl MacTextSystemState { } fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result> { - Ok(self.fonts[font_id.0].advance(glyph_id.into())?.into()) + Ok(self.fonts[font_id.0].advance(glyph_id.0)?.into()) } fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option { - self.fonts[font_id.0].glyph_for_char(ch).map(Into::into) + self.fonts[font_id.0].glyph_for_char(ch).map(GlyphId) } fn id_for_native_font(&mut self, requested_font: CTFont) -> FontId { @@ -261,7 +259,7 @@ impl MacTextSystemState { let scale = Transform2F::from_scale(params.scale_factor); Ok(font .raster_bounds( - params.glyph_id.into(), + params.glyph_id.0, params.font_size.into(), scale, HintingOptions::None, @@ -336,7 +334,7 @@ impl MacTextSystemState { .native_font() .clone_with_font_size(f32::from(params.font_size) as CGFloat) .draw_glyphs( - &[u32::from(params.glyph_id) as CGGlyph], + &[params.glyph_id.0 as CGGlyph], &[CGPoint::new( (subpixel_shift.x / params.scale_factor) as CGFloat, (subpixel_shift.y / params.scale_factor) as CGFloat, @@ -421,7 +419,7 @@ impl MacTextSystemState { let glyph_utf16_ix = usize::try_from(*glyph_utf16_ix).unwrap(); ix_converter.advance_to_utf16_ix(glyph_utf16_ix); glyphs.push(ShapedGlyph { - id: (*glyph_id).into(), + id: GlyphId(*glyph_id as u32), position: point(position.x as f32, position.y as f32).map(px), index: ix_converter.utf8_ix, is_emoji: self.is_emoji(font_id), @@ -612,9 +610,48 @@ impl From for FontkitStyle { } } +// Some fonts may have no attributest despite `core_text` requiring them (and panicking). +// This is the same version as `core_text` has without `expect` calls. +mod lenient_font_attributes { + use core_foundation::{ + base::{CFRetain, CFType, TCFType}, + string::{CFString, CFStringRef}, + }; + use core_text::font_descriptor::{ + kCTFontFamilyNameAttribute, CTFontDescriptor, CTFontDescriptorCopyAttribute, + }; + + pub fn family_name(descriptor: &CTFontDescriptor) -> Option { + unsafe { get_string_attribute(descriptor, kCTFontFamilyNameAttribute) } + } + + fn get_string_attribute( + descriptor: &CTFontDescriptor, + attribute: CFStringRef, + ) -> Option { + unsafe { + let value = CTFontDescriptorCopyAttribute(descriptor.as_concrete_TypeRef(), attribute); + if value.is_null() { + return None; + } + + let value = CFType::wrap_under_create_rule(value); + assert!(value.instance_of::()); + let s = wrap_under_get_rule(value.as_CFTypeRef() as CFStringRef); + Some(s.to_string()) + } + } + + unsafe fn wrap_under_get_rule(reference: CFStringRef) -> CFString { + assert!(!reference.is_null(), "Attempted to create a NULL object."); + let reference = CFRetain(reference as *const ::std::os::raw::c_void) as CFStringRef; + TCFType::wrap_under_create_rule(reference) + } +} + #[cfg(test)] mod tests { - use crate::{font, px, FontRun, MacTextSystem, PlatformTextSystem}; + use crate::{font, px, FontRun, GlyphId, MacTextSystem, PlatformTextSystem}; #[test] fn test_wrap_line() { @@ -653,8 +690,8 @@ mod tests { assert_eq!(layout.len, line.len()); assert_eq!(layout.runs.len(), 1); assert_eq!(layout.runs[0].glyphs.len(), 2); - assert_eq!(layout.runs[0].glyphs[0].id, 68u32.into()); // a - // There's no glyph for \u{feff} - assert_eq!(layout.runs[0].glyphs[1].id, 69u32.into()); // b + assert_eq!(layout.runs[0].glyphs[0].id, GlyphId(68u32)); // a + // There's no glyph for \u{feff} + assert_eq!(layout.runs[0].glyphs[1].id, GlyphId(69u32)); // b } } diff --git a/crates/gpui/src/platform/test/platform.rs b/crates/gpui/src/platform/test/platform.rs index 9a33b4b3b58bf67591746d1c1f000089db315098..5aadc4b760aeacf9bb03ee3bef4694a4d73fea4d 100644 --- a/crates/gpui/src/platform/test/platform.rs +++ b/crates/gpui/src/platform/test/platform.rs @@ -1,6 +1,7 @@ use crate::{ AnyWindowHandle, BackgroundExecutor, ClipboardItem, CursorStyle, DisplayId, ForegroundExecutor, - Keymap, Platform, PlatformDisplay, PlatformTextSystem, TestDisplay, TestWindow, WindowOptions, + Keymap, Platform, PlatformDisplay, PlatformTextSystem, Task, TestDisplay, TestWindow, + WindowOptions, }; use anyhow::{anyhow, Result}; use collections::VecDeque; @@ -280,16 +281,16 @@ impl Platform for TestPlatform { self.current_clipboard_item.lock().clone() } - fn write_credentials(&self, _url: &str, _username: &str, _password: &[u8]) -> Result<()> { - Ok(()) + fn write_credentials(&self, _url: &str, _username: &str, _password: &[u8]) -> Task> { + Task::ready(Ok(())) } - fn read_credentials(&self, _url: &str) -> Result)>> { - Ok(None) + fn read_credentials(&self, _url: &str) -> Task)>>> { + Task::ready(Ok(None)) } - fn delete_credentials(&self, _url: &str) -> Result<()> { - Ok(()) + fn delete_credentials(&self, _url: &str) -> Task> { + Task::ready(Ok(())) } fn double_click_interval(&self) -> std::time::Duration { diff --git a/crates/gpui/src/scene.rs b/crates/gpui/src/scene.rs index 4ba65ef1e98e3c16761fc3da886d6de27edad60e..70e24030b1db92a5d3bfafbbe766a59e5b5fdad9 100644 --- a/crates/gpui/src/scene.rs +++ b/crates/gpui/src/scene.rs @@ -39,6 +39,7 @@ impl From for EntityId { #[derive(Default)] pub(crate) struct Scene { + last_layer: Option<(StackingOrder, LayerId)>, layers_by_order: BTreeMap, orders_by_layer: BTreeMap, pub(crate) shadows: Vec, @@ -52,6 +53,7 @@ pub(crate) struct Scene { impl Scene { pub fn clear(&mut self) { + self.last_layer = None; self.layers_by_order.clear(); self.orders_by_layer.clear(); self.shadows.clear(); @@ -139,14 +141,22 @@ impl Scene { } fn layer_id_for_order(&mut self, order: &StackingOrder) -> LayerId { - if let Some(layer_id) = self.layers_by_order.get(order) { + if let Some((last_order, last_layer_id)) = self.last_layer.as_ref() { + if order == last_order { + return *last_layer_id; + } + } + + let layer_id = if let Some(layer_id) = self.layers_by_order.get(order) { *layer_id } else { let next_id = self.layers_by_order.len() as LayerId; self.layers_by_order.insert(order.clone(), next_id); self.orders_by_layer.insert(next_id, order.clone()); next_id - } + }; + self.last_layer = Some((order.clone(), layer_id)); + layer_id } pub fn reuse_views(&mut self, views: &FxHashSet, prev_scene: &mut Self) { diff --git a/crates/gpui/src/shared_string.rs b/crates/gpui/src/shared_string.rs index d196b19636f3d2ecf0c62fa6bc457c3af1087e48..8c12c1c970c279279c4476d9ecf25012b65edab8 100644 --- a/crates/gpui/src/shared_string.rs +++ b/crates/gpui/src/shared_string.rs @@ -4,7 +4,7 @@ use std::{borrow::Borrow, sync::Arc}; use util::arc_cow::ArcCow; /// A shared string is an immutable string that can be cheaply cloned in GPUI -/// tasks. Essentially an abstraction over an Arc and &'static str, +/// tasks. Essentially an abstraction over an `Arc` and `&'static str`, #[derive(Deref, DerefMut, Eq, PartialEq, Hash, Clone)] pub struct SharedString(ArcCow<'static, str>); diff --git a/crates/gpui/src/style.rs b/crates/gpui/src/style.rs index e1d82adea8751a8f0baf60c40dac4efbec993b1b..a12bb6df12836390855c6dfd6da078481425fabc 100644 --- a/crates/gpui/src/style.rs +++ b/crates/gpui/src/style.rs @@ -7,7 +7,7 @@ use crate::{ SizeRefinement, Styled, TextRun, }; use collections::HashSet; -use refineable::{Cascade, Refineable}; +use refineable::Refineable; use smallvec::SmallVec; pub use taffy::style::{ AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, JustifyContent, @@ -15,10 +15,12 @@ pub use taffy::style::{ }; #[cfg(debug_assertions)] +/// Use this struct for interfacing with the 'debug_below' styling from your own elements. +/// If a parent element has this style set on it, then this struct will be set as a global in +/// GPUI. pub struct DebugBelow; -pub type StyleCascade = Cascade