Compare commits

..

736 Commits
v0.6 ... v0.12

Author SHA1 Message Date
Max Brunsfeld
e27fafb824 v0.12 2022-01-14 12:03:29 -08:00
Max Brunsfeld
485554cd0c Merge pull request #342 from zed-industries/symbolic-nav
Introduce outline view
2022-01-14 12:02:43 -08:00
Max Brunsfeld
f3239fe1d5 Apply scroll_max after uniform list autoscrolls 2022-01-14 11:56:28 -08:00
Max Brunsfeld
dd8e5ee543 Add bottom margin to the outline view 2022-01-14 11:01:20 -08:00
Max Brunsfeld
5de5e4b6f2 Avoid panic in OutlineView when active item isn't an editor 2022-01-14 10:51:26 -08:00
Max Brunsfeld
b7561c6cef Add select_first and select_last bindings to outline view
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-14 10:45:37 -08:00
Max Brunsfeld
ea69dcd42a Match on names only when outline query has no spaces
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-14 09:16:29 -08:00
Antonio Scandurra
ce51196eab Center the selected item when updating outline query
Co-Authored-By: Max Brunsfeld <max@zed.dev>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-14 17:53:06 +01:00
Antonio Scandurra
e4c0fc6ad5 Dismiss outline view when the query editor is blurred
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-14 17:25:24 +01:00
Nathan Sobo
b52db22544 Only enable smart case if the query contains an uppercase character
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2022-01-14 09:16:09 -07:00
Nathan Sobo
f934370e7f Match full path when query contains spaces
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2022-01-14 09:02:04 -07:00
Antonio Scandurra
be24e58926 Associate StringMatchCandidate with an id
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-14 14:55:03 +01:00
Antonio Scandurra
e538beb920 Highlight matches by increasing the font weight
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-14 14:39:15 +01:00
Antonio Scandurra
a64ba8b687 Allow matching of context items in outline view 2022-01-14 11:09:02 +01:00
Antonio Scandurra
e7f1398f3a 💄 2022-01-14 10:20:04 +01:00
Antonio Scandurra
b0033bb6d4 Don't emit duplicate text when mixing syntax highlighting and match indices 2022-01-14 10:08:08 +01:00
Antonio Scandurra
ecba761e18 Make mod a @context 2022-01-14 09:22:20 +01:00
Antonio Scandurra
deb679b8f5 Report all matching strings in fuzzy matcher even if they're duplicates 2022-01-14 09:16:39 +01:00
Antonio Scandurra
9c1f58ee89 Maintain order of outline items when filling out tree's missing parts 2022-01-14 09:12:30 +01:00
Max Brunsfeld
adeb7e6864 Incorporate syntax highlighting into symbol outline view
Still need to figure out how to style the fuzzy match characters
now that there's syntax highlighting. Right now, they are
underlined in red.
2022-01-13 18:10:02 -08:00
Max Brunsfeld
7913a1ea22 Include highlighting runs in Outline 2022-01-13 14:46:15 -08:00
Max Brunsfeld
3e1c559b2d Allow multiple disjoint nodes to be captured as matcheable in the outline query 2022-01-13 14:04:25 -08:00
Max Brunsfeld
950b06674f Add more items to rust outline query 2022-01-13 12:01:36 -08:00
Max Brunsfeld
f2cef0b795 Implement navigation via outline modal
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2022-01-13 11:48:44 -08:00
Max Brunsfeld
373fe6fadf Change Editor::set_highlighted_row to take a row range
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2022-01-13 09:49:46 -08:00
Antonio Scandurra
055d48cfb2 Select the closest outline item when the outline view's query is empty
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2022-01-13 18:43:49 +01:00
Antonio Scandurra
2660d37ad8 Return Outline<Anchor> from MultiBuffer::outline
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2022-01-13 18:24:00 +01:00
Antonio Scandurra
e165f1e16c Use OutlineItem::depth to include ancestors of matching candidates
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-13 16:59:52 +01:00
Antonio Scandurra
aee3bb98f2 Implement selecting prev and next in outline view
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-13 16:44:06 +01:00
Antonio Scandurra
8d7a57a01e Merge pull request #340 from zed-industries/split-project-diagnostics
Allow splitting of project diagnostics
2022-01-13 16:20:19 +01:00
Antonio Scandurra
d74658fdb5 Allow searching of outline items 2022-01-13 15:10:50 +01:00
Antonio Scandurra
06ba1c64cf Implement Outline::search 2022-01-13 15:10:29 +01:00
Antonio Scandurra
5e64f1aca8 Report the candidate's index when matching strings 2022-01-13 15:09:27 +01:00
Antonio Scandurra
5f2ac61401 Use only lowercase characters to determine if query matches a candidate 2022-01-13 15:07:48 +01:00
Antonio Scandurra
d6ed2ba642 Start on rendering the outline view 2022-01-13 12:01:11 +01:00
Antonio Scandurra
ef596c64f8 Add OutlineItem::depth so that we can render a tree in the outline view 2022-01-13 11:35:43 +01:00
Antonio Scandurra
08c3fddc65 Allow splitting of project diagnostics 2022-01-13 10:24:41 +01:00
Antonio Scandurra
bb3fc8efd7 Merge pull request #339 from zed-industries/update-curl
Update curl to avoid setting `MACOSX_DEPLOYMENT_TARGET` on server
2022-01-13 09:05:21 +01:00
Antonio Scandurra
9422e27f97 Update curl to avoid setting MACOSX_DEPLOYMENT_TARGET on server 2022-01-13 08:52:42 +01:00
Max Brunsfeld
63a401ac5d Add Buffer::outline method
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-12 18:17:49 -08:00
Nathan Sobo
057dc62b90 Merge pull request #338 from zed-industries/use-wildcard-cert
Associate staging and production load balancers with wildcard cert
2022-01-12 17:23:04 -07:00
Nathan Sobo
a93502bb64 Add placeholder environment variables to our deploy
We have the ability to define environment-specific environment variables, but don't current need it. This keeps these files in place while avoiding noise from running `export` with no args due to the files being empty.

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2022-01-12 16:33:41 -07:00
Nathan Sobo
e71b989041 Use the same wildcard certificate on production and staging
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2022-01-12 16:28:09 -07:00
Nathan Sobo
3a82d0d8e1 Use a wildcard cert on DigitalOcean in the staging environment
If this works, we'll use it for production as well and delete the other certs.

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2022-01-12 16:17:21 -07:00
Max Brunsfeld
abd05cc82e Merge pull request #337 from zed-industries/project-diagnostics-styling
Restructure the project diagnostics view to match some aspects of current designs
2022-01-12 12:22:23 -08:00
Max Brunsfeld
1a672929e0 Adjust BlockMap tests to reflect new tiebreaking behavior
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-12 12:11:55 -08:00
Max Brunsfeld
ed88fdcea2 Add unit test for diagnostic + path header ordering 2022-01-12 11:34:57 -08:00
Max Brunsfeld
6ad9ff10c1 Ensure path headers appear before first diagnostic header
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-12 10:51:28 -08:00
Max Brunsfeld
ac0d55222f Adjust project diagnostics test to reflect new block structure 2022-01-12 10:51:08 -08:00
Max Brunsfeld
9ccf2f3f58 Tweak theming of project diagnostics 2022-01-12 10:51:08 -08:00
Max Brunsfeld
b5ee095da9 Deduplicate path names in the project diagnostics view 2022-01-12 10:51:08 -08:00
Max Brunsfeld
a9937ee8be Expand block decorations' bounds to include the gutter 2022-01-12 10:51:08 -08:00
Nathan Sobo
d346b1bfd9 Merge pull request #336 from zed-industries/format-on-save
Format on save
2022-01-12 11:48:12 -07:00
Nathan Sobo
30225678c0 Test ordering of responses with respect to uni-directional messages
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2022-01-12 11:19:17 -07:00
Antonio Scandurra
66694b4c9a Fix failing tests
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2022-01-12 18:43:23 +01:00
Antonio Scandurra
8b53868f8a Preserve the order of responses with respect to all other incoming messages
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2022-01-12 18:26:00 +01:00
Antonio Scandurra
9e4b118214 Use synchronous locks for Peer state
We hold these locks for a short amount of time anyway, and using an
async lock could cause parallel sends to happen in an order different
than the order in which `send`/`request` was called.

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-12 18:02:41 +01:00
Antonio Scandurra
310def2923 Implement Buffer::format
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-12 18:01:20 +01:00
Antonio Scandurra
67991b413c Merge pull request #335 from zed-industries/fix-refresh-selections
Fix panic in `Editor::refresh_selections` due to calling `summaries_for_anchors` without sorting the anchors
2022-01-12 10:03:33 +01:00
Antonio Scandurra
6fbbbab7ba Process selection anchors in a sorted fashion when refreshing them 2022-01-12 09:28:09 +01:00
Antonio Scandurra
b768a3977c Add unit test reproducing a panic when refreshing selections 2022-01-12 09:27:03 +01:00
Antonio Scandurra
7daa4b5b04 Don't return a Result in test-only method select_display_ranges 2022-01-12 09:14:48 +01:00
Nathan Sobo
a6dd9a20d4 Fix binding to dump element JSON 2022-01-11 17:52:26 -07:00
Max Brunsfeld
9602bc6f8e Remove stray dbg! calls 2022-01-11 13:56:07 -08:00
Max Brunsfeld
5941f5fca0 Upgrade tree-sitter-markdown 2022-01-11 10:36:31 -08:00
Max Brunsfeld
5a889b04df Merge pull request #329 from zed-industries/fix-newline-in-multibuffer
Fix cursor position when inserting newlines on a repeated excerpt
2022-01-11 09:39:05 -08:00
Antonio Scandurra
89ead1c44d Merge pull request #314 from zed-industries/auto-connect
Auto-connect to server on startup if credentials are on the keychain
2022-01-11 18:34:20 +01:00
Antonio Scandurra
c16820166b Fix cursor position when inserting newlines on a repeated excerpt
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2022-01-11 18:30:25 +01:00
Antonio Scandurra
58e45dd9be Merge pull request #328 from zed-industries/fix-multibuffer-anchors
Randomize test multibuffer anchors and fix resulting issues
2022-01-11 17:42:53 +01:00
Antonio Scandurra
aa543a4b0a Ensure selections stay sorted after refreshing them
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-11 17:16:45 +01:00
Antonio Scandurra
e70b728758 Verify Anchor::buffer_id before resolving it or comparing it
This commit also verifies some properties about anchor resolution in the
multibuffer randomized test.

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-11 16:40:12 +01:00
Antonio Scandurra
2d5e72251e Merge pull request #325 from zed-industries/fix-more-subscription-panics
Don't register an entity ID extractor for non-entity subscriptions
2022-01-11 15:29:23 +01:00
Antonio Scandurra
d7fcb049d4 Don't register an entity ID extractor for non-entity subscriptions
This commit fixes a panic that could occur when registering N subscriptions for
N entities of the same kind. Before, when dropping the first of the
subscriptions, we would remove the entity ID extractor as well. This was,
however, used by all the other N - 1 subscriptions which would then start
losing messages. In addition, dropping yet another subscription of that kind
would result in a panic, because we wouldn't find the extractor in the map
upon invoking `Subscription::drop`.

With this change we will avoid removing the ID extractor when dropping a
subscription. Crucially, we also avoid inserting extractors for simple message
subscriptions. This enables these non-entity subscriptions to be dropped and
re-registered without seeing a "registered handler for the same message twice"
panic.
2022-01-11 08:21:35 +01:00
Max Brunsfeld
2ea78c5ade Merge pull request #320 from zed-industries/more-diagnostics-polish
Keep the cursor at the top when first opening the project diagnostics view
2022-01-10 16:43:20 -08:00
Max Brunsfeld
a0a558318c In diagnostics editor, attempt to open excerpts in a different pane
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-10 16:33:25 -08:00
Max Brunsfeld
747d9e8784 Add files to project diagnostics view in order
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-10 16:17:56 -08:00
Max Brunsfeld
c7eb6a6a60 Tweak color of share icon in titlebar 2022-01-10 11:26:07 -08:00
Max Brunsfeld
7244fe9c7f Merge pull request #315 from zed-industries/fix-outdent-column-0
Fix outdent not working when cursor is at column 0
2022-01-10 11:16:30 -08:00
Max Brunsfeld
8ee106e6aa Merge pull request #316 from zed-industries/fix-subscription-panic
Fix `rpc::Client` subscription panics
2022-01-10 11:15:17 -08:00
Antonio Scandurra
4992a8a407 🎨 2022-01-10 16:10:29 +01:00
Antonio Scandurra
b44ae46559 Fix panic if subscribing after dropping a subscription for the same message 2022-01-10 16:09:06 +01:00
Antonio Scandurra
dff812b38e Don't panic when dropping a subscription in a subscription handler 2022-01-10 16:04:49 +01:00
Antonio Scandurra
9f6c53b547 Fix dev dependencies in client/Cargo.toml 2022-01-10 16:03:55 +01:00
Antonio Scandurra
b1de9a945d Fix outdent not working when cursor is at column 0 2022-01-10 15:32:28 +01:00
Antonio Scandurra
e8bbd370e4 Auto-connect to server on startup if credentials are on the keychain 2022-01-10 15:06:38 +01:00
Antonio Scandurra
8d7bb8b1a3 Merge pull request #313 from zed-industries/polish-project-diagnostics
Polish project diagnostics UX
2022-01-10 14:33:26 +01:00
Antonio Scandurra
5c3ae8808b Fix diagnostic unit test assertions 2022-01-10 14:28:25 +01:00
Antonio Scandurra
eb353648e6 🎨 2022-01-10 12:14:52 +01:00
Antonio Scandurra
a1597578ff Compare singleton buffers in test_open_and_save_new_file 2022-01-10 11:56:00 +01:00
Antonio Scandurra
0742640b39 Correctly report line boundaries when a map contains both folds and wraps
This fixes the randomized test failures that were occurring on main.
2022-01-10 11:26:48 +01:00
Antonio Scandurra
1a53d5b7ba Use a new Workspace::activate_item API in project diagnostics
Previously, we would only activate the pane without switching the
pane's *active item*.
2022-01-10 10:10:11 +01:00
Max Brunsfeld
f933d54469 When selections lose their excerpts, move them to the next primary diagnostic 2022-01-07 14:53:33 -08:00
Max Brunsfeld
ce6f3d7f3e Reuse views when moving between diagnostic view and editors
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-07 11:00:12 -08:00
Max Brunsfeld
ea263822fa Finish implementing ProjectDiagnostics::open_excerpts
* Build workspace item views with a reference to the workspace
* Add randomized test for MultiBuffer::excerpted_buffers and fix a small bug

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-07 09:59:27 -08:00
Antonio Scandurra
e5c520a265 Use Buffer handles instead of MultiBuffer as editor workspace items
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-07 17:48:31 +01:00
Antonio Scandurra
794d214eee Refactor opening workspace items
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-07 17:38:37 +01:00
Antonio Scandurra
3cab32d201 WIP: Add keybinding to open buffers under cursors
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-07 16:12:50 +01:00
Antonio Scandurra
cf62d26ed8 Display a "Checking..." message when running disk-based diagnostics
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-07 15:03:19 +01:00
Antonio Scandurra
e39be35e17 Show status bar item for project diagnostic summary
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-07 14:14:21 +01:00
Antonio Scandurra
56496c2585 Move back diagnostic_summaries into Worktree
This fixes an issue where updating the snapshot's entries would
override the diagnostic summaries received on the remote side.

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-07 13:38:20 +01:00
Antonio Scandurra
089542c6f4 Avoid removing diagnostics from Worktree after opening a buffer
This allows re-opening the same buffer and supplying the previous
diagnostics.
2022-01-07 10:33:21 +01:00
Antonio Scandurra
67f672d0cc Clear selections on other excerpted buffers when setting active selections 2022-01-07 10:19:28 +01:00
Max Brunsfeld
94e9c7fd5b Give a full-width background to the diagnostic headers 2022-01-06 17:55:56 -08:00
Max Brunsfeld
2b36ab0de7 Introduce Expanded element
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-06 17:35:45 -08:00
Max Brunsfeld
1f762e482d Unify Flexible and Expanded elements
We'll use the name Expanded for something else now.

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-06 17:29:34 -08:00
Max Brunsfeld
b19d92e918 Keep selections at the top of the project diagnostics view when it is first populated
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-06 17:01:13 -08:00
Max Brunsfeld
9bbe67f0ea Don't clobber diagnostics when getting new snapshot from background scanner
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-06 15:04:06 -08:00
Max Brunsfeld
7357b3ff2a Revert "Remove special handling of multi-line primary diagnostic messages and fix tests"
This reverts commit ce4142eab3.
2022-01-06 14:38:13 -08:00
Max Brunsfeld
10548c2038 Always group diagnostics the way they're grouped in the LSP message
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-06 14:22:28 -08:00
Nathan Sobo
943571af2a Report backtraces of pending conditions when deterministic executor illegally parks
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2022-01-06 13:33:55 -07:00
Nathan Sobo
2dbee1d914 Send diagnostic summaries to guests when they join the project
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2022-01-06 12:12:09 -07:00
Nathan Sobo
d7a78e14ac Allow disk-based diagnostic progress begin/end events to interleave
When multiple saves occur, we can have multiple start events followed by multiple end events. We don't want to update our project diagnostics view until all pending progress is finished.

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2022-01-06 09:32:08 -07:00
Nathan Sobo
571d0386e2 Re-focus diagnostics editor when transitioning from an empty to a populated state
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2022-01-06 08:35:31 -07:00
Antonio Scandurra
1875a0e349 Polish rendering of inline errors
- Don't soft-wrap
- Render multiple lines

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-06 16:17:53 +01:00
Antonio Scandurra
d0f7e5f075 Maintain excerpt ordering correctly when some errors don't change 2022-01-06 15:33:02 +01:00
Antonio Scandurra
f37f839330 Wire up refresh_anchors in Editor::refresh_selections and call it 2022-01-06 15:32:37 +01:00
Max Brunsfeld
7340e83059 WIP - MultiBuffer::refresh_anchors 2022-01-05 21:12:49 -08:00
Max Brunsfeld
fee7657fd7 Merge branch 'main' into polish-project-diagnostics 2022-01-05 20:38:20 -08:00
Nathan Sobo
b10f06d084 Merge pull request #308 from zed-industries/fix-collaboration-regressions
Fix collaboration regressions
2022-01-05 19:56:51 -07:00
Max Brunsfeld
f9f75e98f8 Fix Locator::from_index
Enhance language::tests::test_random_collaborators so that it checks buffer invariants.

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-05 17:58:24 -08:00
Max Brunsfeld
e5faaeb2f2 Fix Global::gt and rename it to changed_since
A false negative return value of `gt` was preventing guests' multibuffers from
syncing correctly.

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-05 17:36:12 -08:00
Max Brunsfeld
5a53eeef63 Don't scroll editors away from the top of their buffer when content changes
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-05 12:31:00 -08:00
Max Brunsfeld
85a13fa477 Fix panic when resolving anchors after an excerpt id has been recycled
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2022-01-05 11:28:49 -08:00
Max Brunsfeld
8728d3292d Merge branch 'main' into polish-project-diagnostics
Also fix false failure in ModelHandle::condition when parking is not forbidden.
2022-01-05 10:53:18 -08:00
Antonio Scandurra
29b63ae4c6 Remove invalid excerpts as opposed to styling them differently 2022-01-05 18:21:17 +01:00
Antonio Scandurra
4b22e49ce1 Merge pull request #306 from zed-industries/serialize-buffer-undos
Fix buffer replication bugs that would lead to divergence among replicas
2022-01-05 18:14:09 +01:00
Antonio Scandurra
fe28abe8cf Show a message when no diagnostics are available 2022-01-05 17:25:03 +01:00
Antonio Scandurra
e56609cf0c Ensure prior, deferred selections don't override newer selections 2022-01-05 15:43:26 +01:00
Antonio Scandurra
eb65a5d29a Fix TreeMap::get always returning None 2022-01-05 15:41:30 +01:00
Antonio Scandurra
f8c2620166 Fix Buffer::remote_selections_in_range at query range boundaries 2022-01-05 15:04:50 +01:00
Antonio Scandurra
587a908225 Populate deferred operations when an operation can't be applied 2022-01-05 15:04:19 +01:00
Antonio Scandurra
bf044506ed Verify that selections are replicated correctly in randomized test 2022-01-05 15:04:08 +01:00
Antonio Scandurra
870fa5f278 Serialize deferred operations 2022-01-05 11:52:41 +01:00
Antonio Scandurra
d383ff30ce Introduce randomized test for collaboration on buffers
This test will exercise serialization of operations as well as peers
replicating from an existing buffer.
2022-01-05 11:51:41 +01:00
Antonio Scandurra
7bc8eb4f3d Fix compile errors and get serialization unit test passing 2022-01-05 10:29:29 +01:00
Max Brunsfeld
984e366c32 WIP - Serialize buffer in terms of its state, not its base text + ops
The main reason for this is that we need to include information about
a buffer's UndoMap into its protobuf representation. But it's a bit
complex to correctly incorporate this information into the current
protobuf representation.

If we want to continue reusing `Buffer::apply_remote_edit` for
incorporating the historical operations, we need to either make
that method capable of incorporating already-undone edits, or
serialize the UndoMap into undo *operations*, so that we can apply
these undo operations after the fact when deserializing. But this is
not trivial, because an UndoOperation requires information about
the full offset ranges that were undone.
2022-01-04 18:06:16 -08:00
Max Brunsfeld
0bcd0a3f08 Forward events from remote worktrees to their projects 2022-01-04 16:01:25 -08:00
Max Brunsfeld
d7ecbdcc1d Add unit test showing problem with serialization of undo ops 2022-01-04 16:01:12 -08:00
Max Brunsfeld
d8b888c9cb Replicate diagnostic summaries
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2022-01-04 14:29:22 -08:00
Antonio Scandurra
b2f0c78924 Merge branch 'main' into polish-project-diagnostics
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2022-01-04 18:35:28 +01:00
Antonio Scandurra
5d45c5711d Merge pull request #303 from zed-industries/project-sharing-fixes
Miscellaneous bug fixes after switching to a project-centric sharing model
2022-01-04 18:29:46 +01:00
Antonio Scandurra
b3b56c36d0 Release v0.11.0
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2022-01-04 18:27:35 +01:00
Antonio Scandurra
ad1db117e6 Re-enable cargo check for rust-analyzer 2022-01-04 18:26:16 +01:00
Antonio Scandurra
508b9dc024 Rip out "diagnostic providers" 2022-01-04 16:32:17 +01:00
Antonio Scandurra
496066db59 Run Project::diagnose when registering a new language on Worktree 2022-01-04 15:17:37 +01:00
Antonio Scandurra
2b31a48ef9 Clip right when moving to next word in case we land on a block line 2022-01-04 15:17:37 +01:00
Antonio Scandurra
ed361f2d1a Position selections correctly when duplicating lines in a multi-buffer 2022-01-04 15:17:37 +01:00
Antonio Scandurra
8dc9197324 Position cursors correctly in Editor::delete_line in a multi-buffer 2022-01-04 15:17:37 +01:00
Antonio Scandurra
05a6137549 Capture a new buffer snapshot for excerpts whose selections got updated 2022-01-04 12:22:51 +01:00
Max Brunsfeld
a4027aacb5 Introduce a WorktreeId struct, fix incorrect use of remote worktrees' handle id 2022-01-04 11:28:44 +01:00
Max Brunsfeld
7f8e76e0f1 Remove worktree-specific methods from language::File trait
Use downcasting instead for accessing worktree-specific state of the Files.

This will allow us to introduce a WorktreeId type and use that everywhere
for identifying worktrees. It also just removes some unnecessary coupling
between the language crate and the worktree.
2022-01-04 11:28:44 +01:00
Antonio Scandurra
8270e8e758 Merge pull request #302 from zed-industries/sign-in-redirect
Allow the zed app to connect to both the old and new rpc endpoints
2022-01-04 10:39:28 +01:00
Max Brunsfeld
a080ae98c6 Allow the zed app to connect to both the old and new rpc endpoints
In the case of the new Next.js app, the app will follow a redirect
from 'zed.dev/rpc' to the subdomain where the rust service is hosted.
Until then, the app will connect directly to zed.dev/rpc.
2022-01-03 15:29:26 -08:00
Nathan Sobo
f499a1dfc2 Merge pull request #301 from zed-industries/move-lines-in-multibuffer
Support moving lines up and down in multi-buffers
2021-12-30 01:13:31 -08:00
Nathan Sobo
6d6a82655a Create blocks with anchors to allow a bias to be specified
This allows us to respect the bias on anchors we use to create excerpt headers so that they always remain above any content inserted at the start of an excerpt.
2021-12-30 01:03:19 -08:00
Nathan Sobo
ba75007259 Merge pull request #300 from zed-industries/fix-move-line-panic
Fix panics when moving lines with block decorations and simplify line boundary detection
2021-12-29 23:47:25 -08:00
Nathan Sobo
984378e12c Use anchors for line movement edits to support multi-buffers
Because multi-buffers can contain the same content multiple times, we need to use anchors to track our desired insertion and removal locations when moving lines. This is because deleting a line in order to move it might end up deleting *multiple* lines.
2021-12-29 23:47:03 -08:00
Nathan Sobo
7c9e4e513c Provide an accurate panic message when translating points off the end of a line
Maybe we should fail more gracefully in this case, but I think we should at least make the message accurate and see how we do.
2021-12-29 23:11:54 -08:00
Max Brunsfeld
137fbd0088 Update editor element to use new {next,prev}_line_boundary methods
Since these methods take buffer points instead of display points, this adjusts
the logic for retrieving the visible selections, so that they are initially returned
in terms of buffer points.
2021-12-28 13:47:09 -08:00
Nathan Sobo
7f786ca8a6 WIP: Start moving toward a simpler interface for detecting prev/next line boundaries 2021-12-27 22:11:05 -08:00
Nathan Sobo
89bbfb8154 wip 2021-12-27 21:14:23 -08:00
Max Brunsfeld
6057d819b0 Add a unit test showing panic in move_line_down 2021-12-27 20:58:01 -08:00
Nathan Sobo
93a516d588 Fix warning 2021-12-27 18:00:15 -08:00
Max Brunsfeld
accf90e843 Add MultiBufferSnapshot::range_contains_excerpt_boundary
Use this method to disable move_line_down across excerpt boundaries.

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-27 17:08:31 -08:00
Nathan Sobo
cbc162acf5 WIP: Allow lines to be moved down across excerpts
This is still a bit weird because we can't remove the last line of an excerpt but we still move it into another buffer. There also seem to be issues with undo.
2021-12-27 15:46:19 -08:00
Nathan Sobo
835af35839 Simplify prev/next_row_boundary methods
We added clipping of points against the buffer when excerpt headers were in the buffer, but now that they're just blocks, I think we can avoid the potential to panic in these methods by going back to not clipping.
2021-12-27 15:46:19 -08:00
Max Brunsfeld
d3521650d3 Merge pull request #296 from zed-industries/fix-autoindent
Fix regressions that happened when moving selections into Editor
2021-12-27 15:45:58 -08:00
Max Brunsfeld
3040cfece1 Fix Editor::newest_selection
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-27 15:42:37 -08:00
Max Brunsfeld
f5d4e26799 Remove unused context variable 2021-12-27 15:34:07 -08:00
Max Brunsfeld
cbd9e186b5 Store selections with a right start bias so that autoindent moves them
Previously, cursors at column 0 had to be explicitly moved when those lines
were autoindented. This behavior was lost when we moved selections from
the buffer to the editor. Now, with the right bias, we get this behavior automatically.

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-27 15:33:57 -08:00
Max Brunsfeld
43db9e826b Clear autoindent requests when applying autoindent
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-27 13:33:48 -08:00
Nathan Sobo
6f26fa013a Merge pull request #295 from zed-industries/api
Add remaining API endpoints needed to support the new website
2021-12-27 12:54:34 -08:00
Nathan Sobo
13ed9dc1f1 Document database setup and fix issue in script/seed-db
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-12-27 12:52:15 -08:00
Nathan Sobo
8937d877e3 💄 2021-12-27 12:22:59 -08:00
Nathan Sobo
63238a2938 Destroy access tokens before destroying users and word-smith method names 2021-12-25 17:46:02 -07:00
Nathan Sobo
b949b30f24 Add delete user endpoint 2021-12-25 11:57:37 -07:00
Nathan Sobo
56930972fe Add endpoints for listing, creating, and updating users 2021-12-25 11:55:10 -07:00
Nathan Sobo
07a4cfeefd Streamline running both next and collab servers in development
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2021-12-25 11:55:10 -07:00
Nathan Sobo
fe5465a265 Enable authentication via the NextJS site 2021-12-25 11:55:10 -07:00
Nathan Sobo
6dd23c250b Merge pull request #294 from zed-industries/simplify-keymap-contexts
Don't merge keymap contexts from containing elements
2021-12-24 16:54:47 -07:00
Nathan Sobo
e9a750be71 Don't merge keymap contexts from containing elements
Co-Authored-By: Aaron Hillegass <charmedliferaft@gmail.com>
2021-12-24 16:44:35 -07:00
Nathan Sobo
9fc2ddb8da Merge pull request #293 from zed-industries/project-diagnostics
Project diagnostics: First pass
2021-12-24 16:42:00 -07:00
Nathan Sobo
cf81f5a555 Update tests to reflect that we no longer attempt to recycle group ids 2021-12-24 16:36:21 -07:00
Nathan Sobo
ce4142eab3 Remove special handling of multi-line primary diagnostic messages and fix tests 2021-12-24 13:47:45 -07:00
Nathan Sobo
a3df597155 Make diagnostics disk-based in test 2021-12-24 13:33:11 -07:00
Antonio Scandurra
adeea9da66 Parse children from cargo check output to provide hints
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-24 17:06:18 +01:00
Antonio Scandurra
a85e400b35 Start on a DiagnosticProvider implementation for Rust
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-24 16:22:22 +01:00
Antonio Scandurra
393009a05c Implement Buffer::diagnostic_group 2021-12-24 12:08:55 +01:00
Antonio Scandurra
11e3874b4a Remove duplication when assigning diagnostics and hardcode provider names 2021-12-24 12:07:26 +01:00
Antonio Scandurra
3149a4297c Add API_TOKEN environment variable to manifest 2021-12-24 10:02:24 +01:00
Max Brunsfeld
4f774e2bde wip 2021-12-23 23:10:28 -08:00
Nate Butler
78564dcc68 Add job post to Zed.dev 2021-12-23 23:22:57 -05:00
Max Brunsfeld
d5a17053df Get code compiling with some todos 2021-12-23 14:21:10 -08:00
Nathan Sobo
e3ecd87081 WIP
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-12-23 11:24:35 -07:00
Antonio Scandurra
7b453beebc WIP: Use cargo check for on-disk diagnostics
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-12-23 18:35:50 +01:00
Antonio Scandurra
b9d1ca4341 Show only disk-based diagnostics in ProjectDiagnosticsEditor
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-23 16:47:54 +01:00
Antonio Scandurra
304afc1813 Only preserve excerpts for invalid diagnostics if they contain cursors
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-23 16:33:50 +01:00
Antonio Scandurra
dcf26acaac Use a different invalidation strategy for project-wide diagnostics 2021-12-23 15:41:43 +01:00
Antonio Scandurra
da460edb8b Remove BufferState when the last buffer's excerpt is removed 2021-12-23 09:59:39 +01:00
Antonio Scandurra
9164c5f239 Emit an UpdateDiagnostics from Worktree when buffer diagnostics change 2021-12-23 09:57:50 +01:00
Max Brunsfeld
c47340000d Fix remove_excerpts when removing the last N excerpts, N > 1
Also, generalize the randomized test to remove multiple excerpts at a time
2021-12-22 21:02:20 -08:00
Max Brunsfeld
3e59c61a34 Use MultiBuffer::insert_excerpt_after to update project diagnostics view 2021-12-22 18:00:53 -08:00
Max Brunsfeld
435d405d10 Implement MultiBuffer::insert_excerpt_after 2021-12-22 17:59:44 -08:00
Max Brunsfeld
a86ba57983 Add Editor::remove_blocks 2021-12-22 17:30:14 -08:00
Max Brunsfeld
5d8ed535be Clear out old disk-based diagnostics after 2 seconds 2021-12-22 15:51:51 -08:00
Max Brunsfeld
b9551ae8b1 Preserve group ids when updating diagnostics 2021-12-22 14:50:51 -08:00
Max Brunsfeld
06d2cdc20d Remove unused multi_buffer::FromAnchor trait 2021-12-22 13:27:43 -08:00
Max Brunsfeld
0faf5308ac Add a unit test for preserving disk-based diagnostics 2021-12-22 13:27:43 -08:00
Max Brunsfeld
1544da887e Start work on preserving continuity of disk-based diagnostics 2021-12-22 12:52:50 -08:00
Max Brunsfeld
e31205c95e Revert "Implement MultiBuffer::remove_excerpts by inserting tombstones"
This reverts commit 275b7e8d4f.
2021-12-22 10:18:33 -08:00
Antonio Scandurra
275b7e8d4f Implement MultiBuffer::remove_excerpts by inserting tombstones
This will make it easier to use anchors in the presence of deletes.
2021-12-22 17:57:36 +01:00
Max Brunsfeld
2c3efdea8c WIP - Start work on updating project diagnostics view 2021-12-21 16:39:23 -08:00
Max Brunsfeld
a888620e5f Implement MultiBuffer::remove_excerpts
We'll need this for updating project diagnostics
2021-12-21 15:25:57 -08:00
Max Brunsfeld
a93f5e5fb4 Avoid repeated subscriptions + clones when adding another excerpt for same buffer 2021-12-21 14:28:23 -08:00
Max Brunsfeld
3c26f67ea3 Minor cleanup in Buffer::update_diagnostics 2021-12-21 14:07:50 -08:00
Max Brunsfeld
bc906fef9c Store worktree's diagnostics summaries ordered by path 2021-12-21 14:07:09 -08:00
Max Brunsfeld
699dafbbd4 Avoid cloning diagnostic messages from language server 2021-12-21 14:06:17 -08:00
Max Brunsfeld
8492c6e7ac Fix maintenance of MultiBuffer's buffer states 2021-12-21 13:07:43 -08:00
Max Brunsfeld
13ecd16685 Index max buffer row on MultiBuffer 2021-12-21 12:36:46 -08:00
Nathan Sobo
61b806e485 Add an endpoint for creating an access token for a GitHub login 2021-12-21 13:05:32 -07:00
Max Brunsfeld
04d577e326 Fix context line handline in project diagnostic view 2021-12-21 11:46:47 -08:00
Max Brunsfeld
60f7169008 Remove header heights from multibuffer randomized test 2021-12-21 10:24:01 -08:00
Antonio Scandurra
eec1748dc7 Render excerpt headers using DisplayMap::insert_blocks
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-12-21 18:32:27 +01:00
Antonio Scandurra
91c786a8db WIP: Insert blocks in BlockMap for MultiBuffer headers 2021-12-21 17:38:03 +01:00
Antonio Scandurra
8534a9cc41 Don't insert headers in MultiBuffer
This lays the groundwork to insert headers in the block map instead.
2021-12-21 16:38:18 +01:00
Antonio Scandurra
99317bbd62 Delete unit test
Sharing/unsharing is already exercised via the integration tests.
2021-12-21 14:51:09 +01:00
Antonio Scandurra
89c0b358a7 Allow sharing/unsharing of projects 2021-12-21 12:45:20 +01:00
Antonio Scandurra
17094ec542 Allow opening of remote projects via the contacts panel 2021-12-21 12:05:38 +01:00
Antonio Scandurra
5d2c4807db Fix invalid theme variables 2021-12-21 10:25:37 +01:00
Antonio Scandurra
c6dd797f4e Drop project instead of worktree to simulate client leaving 2021-12-21 10:20:05 +01:00
Antonio Scandurra
afec4152f4 Update contacts as projects/worktrees get registered/unregistered 2021-12-21 10:17:26 +01:00
Antonio Scandurra
40da3b233f Get more integration tests passing 2021-12-21 09:50:11 +01:00
Antonio Scandurra
1e8ef8a4c1 Register local worktrees after acquiring a project remote id 2021-12-21 09:49:13 +01:00
Antonio Scandurra
4053d683d9 Re-enable commented out worktree test 2021-12-21 08:35:08 +01:00
Max Brunsfeld
788bb4a368 Get some RPC integration tests passing 2021-12-20 18:08:53 -08:00
Max Brunsfeld
636931373e Add missing RPC handlers for local projects 2021-12-20 18:08:06 -08:00
Max Brunsfeld
870b73aa36 Send a LeaveProject message when dropping a remote project 2021-12-20 18:07:51 -08:00
Max Brunsfeld
a138955943 Fix logic for waiting for project's remote id 2021-12-20 18:07:34 -08:00
Max Brunsfeld
5d8d7de68d Fix accidental usages of local worktree id instead of remote id 2021-12-20 18:06:58 -08:00
Max Brunsfeld
55910c0d79 Get the server and integration tests compiling 2021-12-20 16:30:29 -08:00
Max Brunsfeld
466a377e1d Merge branch 'main' into share-project
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-20 11:42:59 -08:00
Max Brunsfeld
614ee4eac7 Send worktree info only when sharing worktree
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-20 11:36:59 -08:00
Antonio Scandurra
697e641e8e Get back to a compiling state for client
This temporarily disables some tests and there are still some server-side
compiler errors.
2021-12-20 16:08:34 +01:00
Nathan Sobo
323e1f7367 Add the fetching of user JSON by github login with a token header 2021-12-19 09:43:13 -07:00
Nathan Sobo
f4b9772ec2 Relocate admin routes to make room for API
I want to use the top-level /users route for the API that we'll access from the front-end site running on Vercel, and this is the easiest way to make space. Eventually we won't have admin pages, but I want to be additive for now.
2021-12-19 09:06:57 -07:00
Nathan Sobo
29bc2db6e8 Fix journal format strings 2021-12-18 12:15:07 -07:00
Nathan Sobo
34edbc7934 Merge pull request #287 from zed-industries/journal
Add a simple journaling feature
2021-12-18 11:17:18 -07:00
Nathan Sobo
0a37d40fad Pad single-digit months, days, and minutes with a leading zero 2021-12-18 11:11:04 -07:00
Nathan Sobo
ab5db0bc1e Fix warnings and tests 2021-12-18 11:06:07 -07:00
Nathan Sobo
e4f18947de Insert a time heading when creating a journal entry 2021-12-18 10:38:54 -07:00
Nathan Sobo
9e8ef31452 Return item handles when opening items
This will support interacting with the opened item. Although I think I should probably return the ItemView rather than the Item. Next commit.
2021-12-18 08:26:57 -07:00
Nathan Sobo
ca0d7e5e1f Add journal crate and move supporting logic into workspace crate
I needed to interact with the workspace to open a file from the journal crate, so I moved a bunch of logic out of main related to opening new workspaces and paths.
2021-12-18 08:12:59 -07:00
Nathan Sobo
cd65031cda Halt keystroke dispatch immediately when we call a global action handler
Someday, we may want to define a global action context that allows us to propagate the action, but this isn't currently supported. Previous to this commit, we were invoking the same global action handler multiple times, once for each view in the responder chain.
2021-12-18 08:12:08 -07:00
Max Brunsfeld
c41b958829 WIP - start restructuring collaboration around entire projects
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-17 22:00:39 -08:00
Antonio Scandurra
88d663a253 Allow saving of all buffers contained in project diagnostics editor
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-17 16:48:16 +01:00
Antonio Scandurra
f0fe346e15 Gracefully degrade diagnostics_in_range, diagnostic_group and file
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-17 16:37:12 +01:00
Antonio Scandurra
6685d5aa7d Implement MultiBuffer::save
This is still not integrated with `workspace::ItemView`.
2021-12-17 16:11:18 +01:00
Antonio Scandurra
7d2b74a93b Implement MultiBuffer::{is_dirty,has_conflict} 2021-12-17 15:33:09 +01:00
Antonio Scandurra
5f819b6edc Implement MultiBuffer::enclosing_bracket_ranges 2021-12-17 15:05:05 +01:00
Antonio Scandurra
c9cbc2fe1e Implement MultiBuffer::range_for_syntax_ancestor 2021-12-17 14:57:42 +01:00
Antonio Scandurra
a2ee38f37b Make MultiBuffer::is_parsing a test-only method 2021-12-17 14:57:24 +01:00
Antonio Scandurra
3914d1d072 Display filename on the first excerpt's header for a group 2021-12-17 13:49:21 +01:00
Antonio Scandurra
63f171200e Enhance diagnostic unit test and correctly display primary diagnostic
That is, if the diagnostic has more than one line we will display the
first line in the header and all the other message lines at the error
location.
2021-12-17 12:16:09 +01:00
Max Brunsfeld
528d64d3cc WIP - Improve project diagnostic context rendering 2021-12-16 18:34:29 -08:00
Max Brunsfeld
fb492a9fb8 Correctly incorporate editor settings into diagnostic header rendering 2021-12-16 16:36:33 -08:00
Max Brunsfeld
ae147a379d Don't terminate on an empty input chunk in ExcerptChunks 2021-12-16 16:05:28 -08:00
Max Brunsfeld
31eeffa5a7 Autoscroll after inserting blocks 2021-12-16 14:20:01 -08:00
Max Brunsfeld
9cd4e5ba04 Transfer focus from ProjectDiagnostics view to its editor 2021-12-16 14:14:22 -08:00
Max Brunsfeld
6444fcd442 Integrate MultiBuffer::buffer_rows into the display map 2021-12-16 13:53:32 -08:00
Max Brunsfeld
db33e4935a Implement MultiBuffer::buffer_rows 2021-12-16 12:17:47 -08:00
Max Brunsfeld
a293e9c0c5 Suppress unused field warnings 2021-12-16 11:17:06 -08:00
Max Brunsfeld
38df091b06 Fix up/down movement across excerpt headers
Implement these movements in terms of clipping, instead of with explicit loops
2021-12-16 11:16:48 -08:00
Max Brunsfeld
dcd05ef96b Resolve Anchor::min and ::max to valid positions
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2021-12-16 10:25:09 -08:00
Max Brunsfeld
80f3173fbd Always panic if invalid point is passed to {prev,next}_row_boundary
Co-Authored-By: Antonio Scandurra <antonio@zed.dev>
2021-12-16 10:23:45 -08:00
Antonio Scandurra
0fc2db6d6e Account for folds when inserting/removing block decorations 2021-12-16 16:44:15 +01:00
Antonio Scandurra
7660159164 Test blocks in display map randomized tests
This highlighted some errors in the implementation.
2021-12-16 16:15:14 +01:00
Antonio Scandurra
de679cae78 Re-enable creating multiple blocks at once in BlockMap tests 2021-12-16 12:41:48 +01:00
Antonio Scandurra
abf96e6ad6 Fix movement tests in DisplayMap 2021-12-16 12:36:27 +01:00
Antonio Scandurra
64e2f6d506 Ensure BlockMap::clip_point always yield a valid buffer location 2021-12-16 12:29:37 +01:00
Antonio Scandurra
ec39c9d335 Allow specifying MAX_EXCERPTS via an env variable in random tests 2021-12-16 12:28:54 +01:00
Max Brunsfeld
3e2f684545 Fix prev_row_boundary when a wrap follows a fold
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-15 18:30:09 -08:00
Max Brunsfeld
4c22774694 Always clip buffer points when clipping display points
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-15 17:58:24 -08:00
Max Brunsfeld
f898dc6dae Guard against inverted ranges when building edits in unfold
The multibuffer lets you refer to offsets inside of headers,
so it's possible to create a fold that appears non-empty,
but which spans zero characters in the underlying buffers.

Fold ranges are biased inward: the start is biased right, and
the end is biased left.

Because of these two things, it's possible to create a fold
that becomes "inverted" when you insert text at that position.
2021-12-15 17:29:15 -08:00
Max Brunsfeld
e8570b5c26 Allow multibuffer to clip to the ends of excerpts, before trailing newlines 2021-12-15 17:04:57 -08:00
Max Brunsfeld
f8ef605cbd Update all MultiBuffer unit tests, removing expected trailing newline 2021-12-15 17:04:57 -08:00
Max Brunsfeld
f4115ddc3c 🎨 point_to_display_point & display_point_to_point 2021-12-15 15:45:02 -08:00
Max Brunsfeld
368b4447ff Clip buffer points in DisplayMap::{prev,next}_row_boundary 2021-12-15 15:41:38 -08:00
Max Brunsfeld
2930ea8fb0 Fix handling of excerpts surrounded by edits in MultiBuffer::edit 2021-12-15 12:12:39 -08:00
Max Brunsfeld
4bea16eb31 Ensure muiltibuffer anchors are contained within their excerpt ranges
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2021-12-15 10:52:27 -08:00
Max Brunsfeld
cec0c5912c Create multiple excerpts in random BlockMap test
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-15 10:21:11 -08:00
Antonio Scandurra
80abd84050 Create MultiBuffers with more than one fragment in more randomized tests
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-12-15 18:38:37 +01:00
Antonio Scandurra
1bdaeda43e Remove disk diagnostics that were invalidated by a buffer edit
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-12-15 18:29:01 +01:00
Antonio Scandurra
4ab307f0a1 Re-enable multi-byte random character generation
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-15 17:51:11 +01:00
Antonio Scandurra
5118f27a90 Overhaul MultiBuffer::chunks
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-15 17:22:04 +01:00
Antonio Scandurra
bcdb4ffd88 Allow edits at the end of MultiBuffer
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-15 16:45:01 +01:00
Antonio Scandurra
7bbaa1d930 Don't insert a newline after the last excerpt of a MultiBuffer
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-15 16:39:09 +01:00
Antonio Scandurra
ae0fa75abe Start testing the integration of display layers with MultiBuffers
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-15 16:05:26 +01:00
Antonio Scandurra
59121a238a Forward notifications from Buffer in MultiBuffer
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-15 15:07:42 +01:00
Nathan Sobo
437145afbe Remove assertion and don't consume 0 bytes
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2021-12-15 06:52:37 -07:00
Nathan Sobo
fbba417f09 Implement MultiBuffer::bytes_in_range
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2021-12-15 06:49:10 -07:00
Antonio Scandurra
95137ecb2a WIP 2021-12-15 13:20:11 +01:00
Antonio Scandurra
e23965e7c9 Implement MultiBuffer::reversed_chars_at 2021-12-15 10:06:45 +01:00
Antonio Scandurra
9cbb680fb2 Fix panic on creation of a left-biased anchor at the end of MultiBuffer 2021-12-15 10:06:43 +01:00
Antonio Scandurra
7bcce23dc9 Fix compile error in server integration tests 2021-12-15 08:48:50 +01:00
Nathan Sobo
6c5b27af1d Group diagnostics by primary
Render primary message above the excerpt and supporting messages as block decorations with a `Below` disposition. This is still super rough.

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-12-14 18:26:42 -07:00
Nathan Sobo
e1a2897d53 Render basic diagnostic messages in project diagnostics view
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-12-14 17:00:57 -07:00
Nathan Sobo
ad05c0cc7a Implement MultiBufferSnapshot::excerpt_headers_in_range
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-12-14 16:24:48 -07:00
Max Brunsfeld
60e2c6bc52 Fix multibuffer anchors before the ends of excerpts 2021-12-14 13:37:05 -08:00
Max Brunsfeld
06e241117c Fix assertions in test for selection restoration after undo/redo
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-14 13:04:51 -08:00
Max Brunsfeld
e38c1814d5 Update selections on text insertion using anchors
The delta-based approach doesn't work for multi-excerpt buffers.

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-14 12:15:26 -08:00
Max Brunsfeld
4ed96bb5a6 Fix assertion in multibuffer history test
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-14 11:47:22 -08:00
Max Brunsfeld
bf9daf1529 Allow left-biased anchors at the beginnings of excerpts
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2021-12-14 09:58:28 -08:00
Antonio Scandurra
358a6ff66c Implement MultiBufferSnapshot::contains_str_at 2021-12-14 17:51:14 +01:00
Antonio Scandurra
08e9f3e1e3 Maintain a different undo/redo stack in MultiBuffer
This only applies to singleton mode.
2021-12-14 17:43:41 +01:00
Antonio Scandurra
523cbe781b Return the transaction id after grouping in end_transaction_at
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-14 17:41:42 +01:00
Antonio Scandurra
119d44caf7 Remove test-only transaction_group_interval method from MultiBuffer 2021-12-14 14:19:04 +01:00
Antonio Scandurra
2d1ff8f606 Clip anchors created on MultiBuffer's trailing newlines or headers 2021-12-14 12:29:05 +01:00
Antonio Scandurra
1b67f19edc Implement MultiBuffer::set_active_selections 2021-12-14 12:13:19 +01:00
Antonio Scandurra
920daa8a8f Remove stray log statement 2021-12-14 12:01:19 +01:00
Antonio Scandurra
163ce95171 Implement MultiBufferSnapshot::remote_selections_in_range 2021-12-14 11:34:26 +01:00
Antonio Scandurra
174b37cdf0 Assume all excerpts in the multi buffer have the same language for now 2021-12-14 11:33:53 +01:00
Antonio Scandurra
04ffca95c6 Keep a separate diagnostic update count and parse count in MultiBuffer 2021-12-14 11:32:49 +01:00
Antonio Scandurra
9e15c57f91 Display a rudimentary project diagnostic view on alt-shift-d 2021-12-14 11:32:05 +01:00
Max Brunsfeld
4efdc53d9f WIP 2021-12-13 17:44:20 -08:00
Max Brunsfeld
0b1c27956b Add Project::open_buffer method 2021-12-13 17:44:15 -08:00
Max Brunsfeld
fe571f1d70 Store diagnostic summaries on worktrees 2021-12-13 16:36:53 -08:00
Max Brunsfeld
6ab795c629 Handle buffer deduping in the worktree instead of in workspace
Previously, buffers were only deduped by file if they were opened
through Workspace::open_entry
2021-12-13 16:35:46 -08:00
Max Brunsfeld
52b8e3d1a2 Get tests passing after diagnostic + selection changes
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-13 11:34:56 -08:00
Max Brunsfeld
418a9a3d66 Get things compiling with diagnostics on worktree
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-13 11:15:03 -08:00
Antonio Scandurra
85674ba506 WIP
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-13 16:46:35 +01:00
Antonio Scandurra
6645e2820c First attempt at implementing MultiBuffer::edit_internal
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-13 15:43:26 +01:00
Antonio Scandurra
c984b39aaa Show remote collaborators' active selections 2021-12-13 11:38:38 +01:00
Antonio Scandurra
2adf11e204 Write a simple unit test for TreeMap and fix bug in remove 2021-12-13 11:20:10 +01:00
Nathan Sobo
cdbcbdfe6d Test undo/redo at editor layer, including selection restoration 2021-12-12 15:04:19 -07:00
Nathan Sobo
44cd0be068 Restore selections upon undo/redo of edits performed in the current editor 2021-12-12 14:12:03 -07:00
Nathan Sobo
1e7184ea07 Get selections rendering again when local selections are owned by Editor 2021-12-11 13:42:46 -07:00
Nathan Sobo
4dd0752e80 More messy progress towards selections in editors 2021-12-11 00:29:34 -07:00
Nathan Sobo
0639c8331c Relax TreeMap value bounds, fix warnings, simplify cmp 2021-12-10 23:35:24 -07:00
Nathan Sobo
49d1c9d1ba Introduce sum_tree::TreeMap<K, V>
I think this will be useful to avoid cloning HashMaps in certain cases such as snapshots.
2021-12-10 23:33:15 -07:00
Nathan Sobo
f5c775fcd1 WIP 2021-12-10 22:16:39 -07:00
Nathan Sobo
8432daef6a WIP: Start on removing selections from buffer in favor of editor 2021-12-10 19:23:34 -07:00
Nathan Sobo
f35c419f43 Return optional transaction ids from undo/redo
This will allow the editor to restore selections that it associated with the start or end of a transaction.
2021-12-10 18:08:26 -07:00
Nathan Sobo
77defe6e28 Return optional transaction ids when starting/ending a transaction
If the transaction was nested, we return None. Otherwise we return the transaction id in preparation for editors to maintain their own selection state.
2021-12-10 18:00:09 -07:00
Nathan Sobo
c8b43e3078 Move multi_buffer to editor crate 2021-12-10 17:37:53 -07:00
Max Brunsfeld
6caf016df9 Get tests passing w/ multibuffer in editor
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-10 15:54:25 -08:00
Max Brunsfeld
75dd37d873 Update multibuffer when buffers' syntax trees or diagnostics change
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-10 15:54:06 -08:00
Max Brunsfeld
ceff57d02f Don't append a trailing newline in singleton MultiBuffer 2021-12-10 14:27:58 -08:00
Max Brunsfeld
a758bd4f8d Fill in some missing methods on MultiBuffer, MultiBufferSnapshot 2021-12-10 14:27:04 -08:00
Antonio Scandurra
5b31c1ba4e Start making MultiBuffer work with a singleton buffer
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-10 18:04:11 +01:00
Antonio Scandurra
7524974f19 Get everything compiling again
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-10 17:15:16 +01:00
Antonio Scandurra
da09247e5e WIP
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-10 16:14:12 +01:00
Antonio Scandurra
9c74deb9ec Finish removing anchor collections from MultiBuffer
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-10 15:58:37 +01:00
Antonio Scandurra
d9da8effd4 Re-implement edits_since_in_range in terms of Locator
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-10 14:22:43 +01:00
Antonio Scandurra
c8d5e19492 Merge branch 'fragment-locators' into project-diagnostics 2021-12-10 14:01:17 +01:00
Antonio Scandurra
cb97b7cd1d Fix diagnostic unit test 2021-12-10 09:43:21 +01:00
Antonio Scandurra
eeba0993aa Optimize anchor comparison and take full advantage of fragment IDs 2021-12-10 09:16:58 +01:00
Max Brunsfeld
5e516f59c0 Merge branch 'fragment-locators' into HEAD 2021-12-09 14:49:04 -08:00
Max Brunsfeld
1ed1ec21dd Batch anchor resolution, avoid cloning fragment ids when seeking 2021-12-09 11:00:40 -08:00
Antonio Scandurra
e9c385e7a6 WIP 2021-12-09 18:27:54 +01:00
Antonio Scandurra
91a7bbbba2 Fix some of the diagnostic tests and make DiagnosticEntry generic
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-09 17:53:08 +01:00
Antonio Scandurra
65711b2256 Remove anchor collections
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-09 17:28:27 +01:00
Antonio Scandurra
67686dd1c2 Don't use an enum for anchors and model min/max more implicitly
This will make it easier to serialize an anchor.
2021-12-09 12:01:17 +01:00
Antonio Scandurra
cbe136c0cb Implement anchor resolution using locators 2021-12-09 11:18:01 +01:00
Antonio Scandurra
b7535dfba4 Store only clock::Local in InsertionFragment 2021-12-09 09:21:52 +01:00
Antonio Scandurra
dc81b5f57a Make remote edit randomized tests pass with locators 2021-12-09 09:15:19 +01:00
Antonio Scandurra
b4ebe179f9 Make local edit randomized tests pass with locators 2021-12-09 09:11:16 +01:00
Nathan Sobo
dd38eb1264 Start on maintaining an insertions tree
I'm correctly assigning fragment ids to all fragments in the fragments tree, but I have a randomized test failure when making sure that the insertions tree matches the state of the fragments tree.
2021-12-08 22:05:13 -07:00
Nathan Sobo
ec54010e3c Sketch in type-level changes to track insertion splits 2021-12-08 21:04:22 -07:00
Nathan Sobo
98f726974e WIP 2021-12-08 20:04:30 -07:00
Nathan Sobo
4ee404a0af Take a cx in MultiBuffer::start_transaction 2021-12-08 19:30:52 -07:00
Nathan Sobo
87d16c271e Get Editor compiling with MultiBuffer as its buffer
There's a bunch of unimplemented methods in MultiBuffer, but everything compiles.

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-12-08 19:23:04 -07:00
Max Brunsfeld
daedf179b2 Implement Anchor, AnchorRangeMap, SelectionSet in multi_buffer
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-08 12:56:09 -08:00
Max Brunsfeld
a7634ccd5f Rename ExcerptList to MultiBuffer
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-08 10:07:15 -08:00
Max Brunsfeld
5f8e406c18 Fill out ExcerptList API
This restores the improvements that we had made on the `project-diagnostics-generic` branch.

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-08 10:04:22 -08:00
Antonio Scandurra
a88cff4fa0 Remove lifetime parameter from TextDimension trait
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2021-12-08 09:45:57 -08:00
Max Brunsfeld
6a44a7448e Consolidate Edit types in editor crate 2021-12-08 09:33:55 -08:00
Max Brunsfeld
fa379885f1 Give more specific names to all snapshot and text iterator types 2021-12-08 09:24:00 -08:00
Max Brunsfeld
bd6e972d0f Merge pull request #281 from zed-industries/fix-release-asset-redirects
Don't pass GH auth header when following redirects for release assets
2021-12-07 12:49:49 -08:00
Max Brunsfeld
6d9bf802e2 Don't pass GH auth header when following redirects for release assets 2021-12-07 12:34:55 -08:00
Max Brunsfeld
ad33111a22 Fix assertion in excerpt unit test after fixing edits 2021-12-06 17:40:32 -08:00
Max Brunsfeld
39cc0cac93 Fix Subscription re-export after moving it into its own module 2021-12-06 17:40:17 -08:00
Max Brunsfeld
102926d171 Implement and randomized test excerpt list point translation and clipping 2021-12-06 17:39:31 -08:00
Max Brunsfeld
09c0c3a0e7 🎨 excerpt_list::Chunks::next 2021-12-06 16:28:44 -08:00
Max Brunsfeld
416033a01c Get random excerpts test passing w/ text in range, edits 2021-12-06 16:17:31 -08:00
Nathan Sobo
02f42f2877 WIP
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-12-06 14:51:23 -07:00
Nathan Sobo
88e3d87098 Get randomized test passing on basic excerpt list features
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-12-06 13:48:07 -07:00
Nathan Sobo
4578938ea1 Implement ExcerptList::subscribe
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-12-06 13:37:17 -07:00
Nathan Sobo
a02a29944c Get the basic ExcerptList unit test passing again
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-12-06 13:01:09 -07:00
Nathan Sobo
6965117dd8 Allow patches to be composed with edit iterators in addition to other Patches
This can avoid an extra allocation in some cases.

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-12-06 13:00:51 -07:00
Nathan Sobo
cff610e1ec Rename FragmentList to ExcerptList
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-12-06 11:59:32 -07:00
Antonio Scandurra
42eba7268d Introduce Buffer::edits_since_in_range
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-12-06 19:48:45 +01:00
Antonio Scandurra
e37908cf3b Start on a simple randomized test for FragmentList 2021-12-06 16:08:17 +01:00
Antonio Scandurra
8354d1520d 🎨 2021-12-06 14:03:38 +01:00
Antonio Scandurra
45d6f5ab04 Start on maintaining edits in FragmentList 2021-12-06 12:10:25 +01:00
Antonio Scandurra
8f90d42723 Merge branch 'main' into project-diagnostics 2021-12-06 09:39:03 +01:00
Antonio Scandurra
703e8e626d Merge pull request #276 from zed-industries/markdown
Add basic markdown highlighting
2021-12-06 08:43:10 +01:00
Max Brunsfeld
b1ed9c88a4 Add tree-sitter-markdown, set up simple markdown higlighting 2021-12-05 21:37:31 -08:00
Max Brunsfeld
026c3476db Upgrade tree-sitter to 0.20.1 2021-12-05 21:37:08 -08:00
Antonio Scandurra
a13e2518b8 Merge pull request #275 from zed-industries/fix-selection-artifacts
Use 16-bit float to store path windings
2021-12-05 12:58:00 +01:00
Antonio Scandurra
45d1690f6e Use 16-bit float to store path windings
Previously, we were using a normalized 8-bit unsigned integer which forced us
to represent each increment of the winding number as a fraction of the max
value (1 / 255) which we would then add up using additive alpha blending.

This had three major drawbacks:

- The max winding number could not be greater than 255.
- Adding up (1 / 255) several times could result in a loss of precision.
- Due to also computing anti-aliasing as a fractional winding number, we had to
  reduce the max winding number to 32. This was still not good enough because
  we would multiply a fractional value with `1 / 32`, thus introducing more and
  more loss of precision.

This commit changes the texture type to an `f16` which doesn't require the
division by 255 and enables greater precision in the computation of the
anti-aliased parts of a curve. Note how this also removes the limitation of 255
windings at most per curve. The tradeoff is paying twice as much memory for
storing the texture, but that seems totally valid to achieve rendering accuracy.

Note that this kind of texture should be compatible with WebGL2 once we start
working on a web version of Zed.
2021-12-05 11:17:26 +01:00
Nathan Sobo
0be897d5ac WIP: Edit one of the excerpted buffers and add an assertion
We'll need to detect edits on the child buffers and understand their impact on the tree.
2021-12-04 07:19:30 -07:00
Nathan Sobo
811696670a Start on a new FragmentList
Here I'm exploring a new approach to the project-wide diagnostics view that can exactly mirror the contents of cargo check. The `FragmentList` composes an arbitrary list of fragments from other buffers and presents them as if they were a single buffer.
2021-12-04 06:57:56 -07:00
Antonio Scandurra
3426d46b69 Clear pending keystrokes after dispatching an action
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-03 17:59:46 +01:00
Antonio Scandurra
0e93bc41dd In add_option_view, avoid bumping view's ref counts if view is None
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-03 17:52:39 +01:00
Max Brunsfeld
bd573e0651 Merge pull request #273 from zed-industries/flexible-blocks
Render blocks as arbitrary elements
2021-12-01 17:18:45 -08:00
Nathan Sobo
5ae46709b0 Fix alignment of blocks adjacent to other blocks
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-12-01 17:58:44 -07:00
Nathan Sobo
ee693a8d2b Get all tests passing with new blocks API
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-12-01 17:52:34 -07:00
Nathan Sobo
512a10b037 Use new BlockMap API to render diagnostics
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-12-01 17:22:40 -07:00
Max Brunsfeld
0c714210ff Start work on generalizing the BlockMap to allow arbitrary elements
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-01 15:25:55 -08:00
Antonio Scandurra
e668ff8bcd Avoid allocating a Patch just to check if there are no edits
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-01 16:47:04 +01:00
Antonio Scandurra
853b636435 Merge pull request #272 from zed-industries/fold-map-edits
Don't rely on `Buffer::edits_since` to keep `FoldMap` up-to-date
2021-12-01 16:42:34 +01:00
Antonio Scandurra
733e0cb21b Use the new buffer subscription API to keep DisplayMap in sync
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-01 16:08:53 +01:00
Antonio Scandurra
3b536f153f Introduce text::Buffer::subscribe
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-12-01 15:55:05 +01:00
Antonio Scandurra
47c467dafc Bump FoldMap's version in FoldMap::sync (and not in DisplayMap) 2021-12-01 12:05:02 +01:00
Antonio Scandurra
b841b3eb79 Don't produce invalid intermediate edits in Patch::compose 2021-12-01 11:44:33 +01:00
Max Brunsfeld
faba276fdc WIP - maintain foldmap with Buffer::edits_since
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-30 17:23:02 -08:00
Max Brunsfeld
2463077b2d Merge pull request #269 from zed-industries/simplify-buffer-content
Use `&'a Snapshot` directly instead of `impl Into<Content<'a>>`
2021-11-30 13:48:15 -08:00
Max Brunsfeld
924e1578ea Use &Snapshot directly instead of impl Into<Content<'a>>
The text::Buffer and its snapshot already used the same representation
for their content, so we can just make Buffer deref to a Snapshot.
2021-11-30 13:32:11 -08:00
Max Brunsfeld
36546463e6 Merge pull request #268 from zed-industries/improve-file-navigation
Improve file navigation by naming the root of every crate after the crate itself
2021-11-30 12:04:07 -08:00
Nathan Sobo
1445ce10b5 Name the root file of every crate after the crate to ease navigation
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-11-30 12:46:39 -07:00
Nathan Sobo
748b1ba602 Fix warning
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-11-30 12:27:00 -07:00
Nathan Sobo
d3f28166cb Rename buffer crate to text and name its entrypoint after the crate
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-11-30 12:26:12 -07:00
Nathan Sobo
eacd2a45bb Bump versions in preparation for release 2021-11-30 09:43:51 -07:00
Nathan Sobo
df1804b215 Merge pull request #267 from zed-industries/fix-soft-wrap-disabled
Avoid building up pending edits when soft wrapping is disabled
2021-11-30 08:39:00 -07:00
Nathan Sobo
0ed488d93b Avoid building up pending edits when soft wrapping is disabled
This was causing us to get slower over time as we stacked up hundreds of thousands of pending edits whenever soft wrap was disabled.

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2021-11-30 08:28:29 -07:00
Antonio Scandurra
fcbd7f9a5a Merge pull request #266 from zed-industries/fix-go-to-line
Use display coordinates for the highlighted row in "go to line"
2021-11-30 11:55:00 +01:00
Antonio Scandurra
2449834868 Use display coordinates for the highlighted row in "go to line" 2021-11-30 11:40:53 +01:00
Nathan Sobo
cb942a0e2f Merge pull request #265 from zed-industries/fix-collaborators-test
Fix flaky contacts test
2021-11-29 17:03:01 -07:00
Nathan Sobo
a1412166f0 Fix randomized test failures by waiting for client_b to be added to worktree_a
We perform an async fetch of client_b's user data on worktree_a, which ends up holding a handle that prevents worktree_a from being released later in the test. By waiting for this fetch to finish before proceeding, we can be sure worktree_a actually gets released.

A more comprehensive fix would be some way to ensure we actually perform a release if an entity is fully dropped outside of an update cycle, but this fixes the issue for now.

Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-11-29 16:48:09 -07:00
Max Brunsfeld
1a91aa8194 Introduce a collections crate w/ deterministic hashmap, hashset in tests
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-29 15:22:45 -08:00
Max Brunsfeld
5ec003530f Consolidate pending effects logic into MutableAppContext::update
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-29 14:16:19 -08:00
Max Brunsfeld
4cc1556ca4 Introduce weak_handle methods on ModelContext and ViewContext
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-29 14:13:22 -08:00
Max Brunsfeld
29b616f4cc Merge pull request #263 from zed-industries/fix-highlighting-when-x-scrolled
Paint highlighted lines correctly when horizontally scrolled
2021-11-29 11:43:08 -08:00
Max Brunsfeld
88e0fe6f88 Paint highlighted lines correctly when horizontally scrolled 2021-11-29 11:28:43 -08:00
Max Brunsfeld
7537c3b6d4 Merge pull request #261 from zed-industries/setting-overrides
Override soft-wrap settings for Markdown and Plain Text
2021-11-29 11:12:40 -08:00
Antonio Scandurra
1803bd77ef Fix test assertions 2021-11-29 18:15:03 +01:00
Antonio Scandurra
9d7039ed51 Embed a plain text grammar and override settings for that too 2021-11-29 18:01:51 +01:00
Antonio Scandurra
2c17ae9aa6 Introduce a new Grammar struct and allow it to be optional
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-29 17:38:59 +01:00
Antonio Scandurra
b9edde7b26 Make settings fields that can be overridden private
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-29 17:08:43 +01:00
Antonio Scandurra
cc78ae14d4 Add a sample markdown grammar to honor the new soft wrap override 2021-11-29 09:39:16 +01:00
Antonio Scandurra
93de2bcfed Allow overriding editor settings on a per-language basis 2021-11-29 09:39:13 +01:00
Nathan Sobo
e0998dbfda Merge pull request #260 from zed-industries/show-collaborators
Show collaborators for the active worktree in the titlebar
2021-11-28 14:16:16 -07:00
Nathan Sobo
815cc7ee91 Give avatar ribbons a rounded top 2021-11-28 14:04:31 -07:00
Nathan Sobo
fbc307cd5e Associate collaborator avatars with "ribbons" corresponding to their cursor color 2021-11-28 13:25:05 -07:00
Nathan Sobo
a5039cad65 Tweak avatar sizes in titlebar 2021-11-28 12:41:46 -07:00
Nathan Sobo
6ce76ca13e Render active worktree collaborator avatars in the titlebar 2021-11-28 12:28:10 -07:00
Nathan Sobo
4bd43e67ef Introduce a TestClient and associate it with a PeerId
This makes it easier to integration test peer interactions because now we know their PeerIds.
2021-11-27 12:33:25 -07:00
Nathan Sobo
b307a7e91d Populate the user data of worktree collaborators
This will make it possible for us to render their avatars. Previously we only had the user ids. During rendering, everything needs to be available synchronously. So now, whenever collaborators are added, we perform the async I/O to fetch their user data prior to adding them to the worktree.
2021-11-26 20:35:50 -07:00
Nathan Sobo
9930e92412 WIP: Give worktrees a reference to the UserStore
This will allow them to fetch user data when peers are added or removed. Still work to do though.
2021-11-26 19:12:12 -07:00
Nathan Sobo
21aba54dc3 Introduce a worktree::Collaborator struct that holds the user_id
We can use this to render avatars.

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2021-11-26 11:28:30 -07:00
Nathan Sobo
d78d5712be Rename PeoplePanel to ContactsPanel
Yeah, it's true they're people, but this is a more specific way in which they're people.

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2021-11-26 11:13:05 -07:00
Nathan Sobo
c8ad5b68e0 Rename collaborator_logins to authorized_logins
Again, this is about reserving the concept of a "collaborator" for actual collaborators on a worktree.

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2021-11-26 11:08:34 -07:00
Nathan Sobo
cd2c3c3606 Rename Collaborators to Contacts
This will allow us to use the word "collaborator" to describe users that are actively collaborating on a worktree.

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2021-11-26 10:59:41 -07:00
Nathan Sobo
9f29eeda03 Associate Peers with a user_id
This will help us render the collaborator avatars for the active worktree when we know its peers.
2021-11-26 10:22:34 -07:00
Nathan Sobo
f453928b44 Associate the project with an active worktree
This is similar to the active entry, but it can remain assigned even if there is no active entry and we can potentially manipulate it in other scenarios such as interaction with the project browser. This prepares the ground to show the collaborators for the active worktree.
2021-11-26 10:21:56 -07:00
Nathan Sobo
74cdd32c58 Merge pull request #257 from zed-industries/go-to-line-plus-mouse
Preserve selection when clicking on editor dismisses go-to-line dialog
2021-11-25 14:17:29 -07:00
Nathan Sobo
f8cf534812 Merge pull request #256 from zed-industries/select-next
Implement select next (cmd-d) and replace selection with next (cmd-k cmd-d)
2021-11-25 14:10:54 -07:00
Nathan Sobo
ad26362a82 Preserve selection when clicking on editor dismisses go-to-line dialog 2021-11-25 14:10:43 -07:00
Nathan Sobo
fc2ae42f4b Implement cmd-k cmd-d to replace selection with next 2021-11-25 13:39:08 -07:00
Nathan Sobo
d249618ee6 Improve range-based selection queries to only resolve the requested selections 2021-11-25 13:19:49 -07:00
Antonio Scandurra
09a53a0c64 WIP 2021-11-25 17:11:30 +01:00
Antonio Scandurra
2f78d93383 Make summaries_for_anchors/summaries_for_anchor_ranges more generic
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-25 17:03:06 +01:00
Antonio Scandurra
2f43ef67fd Allow a single start/end bias per AnchorRangeMap
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-25 16:53:10 +01:00
Antonio Scandurra
f42fd8e1bb Return Selections from Editor::selections_in_range
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-25 16:45:06 +01:00
Antonio Scandurra
861893b7b6 Autoscroll vertically to the newest selection on SelectNext
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-25 16:02:39 +01:00
Antonio Scandurra
10b3fae2c3 Implement SelectNext
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-25 15:44:06 +01:00
Antonio Scandurra
bf7acb5f34 Merge pull request #252 from zed-industries/replace-sign-in-icon-with-text
Replace signed out silhouette with a "Sign in" label
2021-11-25 10:49:37 +01:00
Antonio Scandurra
543ebb7e4e Adjust padding on sign in prompt in titlebar 2021-11-25 10:37:30 +01:00
Antonio Scandurra
0d8c68ae1d Replace signed out silhouette with a "Sign in" label 2021-11-25 10:34:03 +01:00
Nathan Sobo
c47855424f Merge pull request #250 from zed-industries/go-to-line
Implement go-to-line interface
2021-11-24 17:15:52 -07:00
Nathan Sobo
f7532c785e Render the current line, column, and line count in go to line dialog 2021-11-24 16:52:18 -07:00
Nathan Sobo
a07fe3aa58 Improve styling of the go to line modal 2021-11-24 16:16:29 -07:00
Nathan Sobo
1e49b56626 Restore scroll position and selections when cancelling go-to-line
But preserve the line when confirming.
2021-11-24 15:43:48 -07:00
Nathan Sobo
8c0541b455 Fix warnings 2021-11-24 15:24:27 -07:00
Nathan Sobo
0854976691 Highlight the selected line when typing in the go to line dialog 2021-11-24 15:23:45 -07:00
Antonio Scandurra
53a7da9d3f Allow centering selections when requesting autoscroll
We use this new capability in the "go to line" modal.
2021-11-24 19:50:47 +01:00
Antonio Scandurra
cea8107242 WIP: Start on go to line 2021-11-24 18:45:36 +01:00
Antonio Scandurra
a743c2d8d7 Merge pull request #249 from zed-industries/invert-workspace-editor-dependency
Invert dependency between workspace and editor
2021-11-24 17:54:28 +01:00
Antonio Scandurra
afdac15572 Move integration test up into the zed crate 2021-11-24 17:39:15 +01:00
Antonio Scandurra
e88d3bb97e Invert dependency between editor and workspace
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-24 17:07:16 +01:00
Antonio Scandurra
fb17d1ed3f Merge pull request #248 from zed-industries/columnar-selection
Allow creation of columnar selections with the mouse when holding `alt-shift`
2021-11-24 16:18:48 +01:00
Antonio Scandurra
2cf44d30b7 🔥
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-24 16:17:41 +01:00
Antonio Scandurra
03bd6d6c33 Pull up diagnostic and cursor position status bar items creation 2021-11-24 16:04:24 +01:00
Antonio Scandurra
9bb195e177 Introduce "entry openers" but still register editors in workspace 2021-11-24 15:18:15 +01:00
Antonio Scandurra
a7186c643f Skip over block lines when building columnar selections 2021-11-24 10:40:06 +01:00
Antonio Scandurra
3a9b69077e Reverse columnar selections when head moves before tail's column 2021-11-24 10:37:35 +01:00
Antonio Scandurra
d19d3bbe45 Calculate an overshoot when mousing to build columnar selections 2021-11-24 10:28:32 +01:00
Nathan Sobo
2b9db911c7 WIP 2021-11-24 09:18:41 +01:00
Nathan Sobo
e0bf5337ca Merge pull request #244 from zed-industries/mouse-selections
Improve support for selecting text via the mouse
2021-11-23 18:11:04 -07:00
Max Brunsfeld
a6e530511d Merge pull request #243 from zed-industries/outdent
Add outdent command, fix indent bugs
2021-11-23 16:56:54 -08:00
Max Brunsfeld
294769be35 Add outdent command, fix indent bugs 2021-11-23 16:44:05 -08:00
Nathan Sobo
bfecdb7bc0 Remove newest selection when adding a selection with a click count > 1
This prevents selections added in earlier clicks from being rendered under the pending selection.
2021-11-23 16:30:33 -07:00
Nathan Sobo
73afb29b04 Use alt modifier instead of cmd to add selections 2021-11-23 16:23:30 -07:00
Max Brunsfeld
22172be2c0 Merge pull request #241 from zed-industries/toggle-comments
Implement toggle-comments
2021-11-23 15:15:28 -08:00
Nathan Sobo
9e651ee127 Simplify handling of shift-click to extend selections 2021-11-23 16:03:21 -07:00
Nathan Sobo
d969f38850 Implement shift-click to extend the newest selection 2021-11-23 15:42:21 -07:00
Max Brunsfeld
f0db748ba1 Implement toggle-comments 2021-11-23 14:13:28 -08:00
Max Brunsfeld
2e2bce7322 Merge pull request #238 from zed-industries/maximize-window
Maximize new windows
2021-11-23 10:37:18 -08:00
Max Brunsfeld
091ed9ab47 Maximize new windows 2021-11-23 10:24:31 -08:00
Antonio Scandurra
63089badf1 Simulate line-wise selection when clicking on the gutter 2021-11-23 19:14:39 +01:00
Antonio Scandurra
7a79df7a24 Implement line-wise selection 2021-11-23 19:10:15 +01:00
Antonio Scandurra
bcf38e6bb5 Implement word-wise mouse selection
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-23 18:50:17 +01:00
Max Brunsfeld
a0287920e5 Remove cargo-bundle from dev-dependencies
The cargo-bundle crate is installed separately in the
'bundle' script via 'cargo install'.
2021-11-23 09:25:22 -08:00
Antonio Scandurra
3269b9925f WIP: Start integrating SelectMode and movement::surrounding_word 2021-11-23 17:04:58 +01:00
Antonio Scandurra
a0ea5b38a0 Add a new movement::surrounding_word function 2021-11-23 17:04:37 +01:00
Antonio Scandurra
005a7076af Expose a count field on Event::LeftMouseDown 2021-11-23 16:18:17 +01:00
Antonio Scandurra
e1d4bcf013 Merge pull request #236 from zed-industries/soft-wrap-flaky-test
Retry flaky `test_soft_wraps` 5 times before giving up
2021-11-23 11:46:37 +01:00
Antonio Scandurra
6b7ee10287 Retry flaky test_soft_wraps 5 times before giving up
We have other tests that rely on loading fonts that intermittently
fail on CI and for which we used the same mitigation.
2021-11-23 11:25:04 +01:00
Antonio Scandurra
6df266348e Merge pull request #235 from zed-industries/handle-screen-scale-factor-change
Fix incorrect rendering when window moves between displays with different scale factors
2021-11-23 11:15:18 +01:00
Max Brunsfeld
4002be882f Clear sprite cache when scale factor changes 2021-11-22 16:47:51 -08:00
Max Brunsfeld
23fbeaf978 Include scale factor in glyph cache keys 2021-11-22 16:37:01 -08:00
Max Brunsfeld
66e27b7420 Merge pull request #233 from zed-industries/fix-split-selection-into-lines
Place the cursor at end of first line when splitting selections into lines
2021-11-22 16:25:19 -08:00
Max Brunsfeld
ce71ed3959 Adjust assertion in test for split_selection_into_lines 2021-11-22 16:19:24 -08:00
Nathan Sobo
843972ceca Merge pull request #232 from zed-industries/uniform-bias
Use uniform biases in AnchorMap, AnchorRangeMap
2021-11-22 15:55:03 -07:00
Nathan Sobo
68223bdb67 Place the cursor at end of first line when splitting selections into lines 2021-11-22 15:53:37 -07:00
Nathan Sobo
2f39dee28b Use uniform biases in AnchorMap, AnchorRangeMap
Specifying a different bias for every point makes the interface feel pretty unwieldy and we don't really use it.
2021-11-22 15:30:46 -07:00
Max Brunsfeld
612b4404a9 Fix the bias of deserialized selection sets
Fixes #224

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-22 14:14:48 -08:00
Max Brunsfeld
cfe6103daf Fix selection set id mismatch when rendering guest selections
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-22 14:10:17 -08:00
Max Brunsfeld
ca4086b844 0.8.0 2021-11-22 13:33:54 -08:00
Max Brunsfeld
c13a26ff7b Bump RPC protocol version 2021-11-22 13:33:05 -08:00
Max Brunsfeld
cfaab6cfb6 Merge pull request #228 from zed-industries/faster-clock-global
Switch to a dense representation for `clock::Global`
2021-11-22 13:31:27 -08:00
Max Brunsfeld
b621c9b857 Merge pull request #230 from zed-industries/rpc-write-timeout
Avoid server deadlocks
2021-11-22 13:31:08 -08:00
Max Brunsfeld
7474813a17 Impose a timeout on writing RPC messages
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-22 13:23:55 -08:00
Antonio Scandurra
b25c3eb740 Switch to a dense representation for clock::Global
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-22 17:20:43 +01:00
Max Brunsfeld
447f710570 Merge pull request #226 from zed-industries/1d-block-map
Allow full diagnostic messages to be displayed in the editor
2021-11-19 09:16:49 -08:00
Antonio Scandurra
6f5ca6064b Use anchor_after in randomized tests to match BlockMap 2021-11-19 18:04:31 +01:00
Antonio Scandurra
c844fcdc09 Invalidate active diagnostic when its primary range collapses
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-19 16:46:54 +01:00
Antonio Scandurra
b0afc80678 Ignore diagnostics with empty ranges
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-19 16:30:26 +01:00
Antonio Scandurra
a023950f28 Remove unused group_range field
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-19 16:17:50 +01:00
Antonio Scandurra
8e74cc178e Invalidate active diagnostics when they are removed
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-19 16:15:22 +01:00
Antonio Scandurra
61d8848b31 Make BlockMap::sync private
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-19 15:44:19 +01:00
Antonio Scandurra
dfbfa86548 WIP 2021-11-19 15:31:33 +01:00
Antonio Scandurra
2664dad2bc Allow styling of invalid diagnostics
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-19 14:35:00 +01:00
Antonio Scandurra
8d5e3fb159 Allow styling of the gutter for block lines 2021-11-19 11:30:01 +01:00
Max Brunsfeld
8d1a4a6a24 Start work on allowing blocks to be styled 2021-11-18 18:16:35 -08:00
Max Brunsfeld
c04151f999 Bind ShowNextDiagnostic to f8 2021-11-18 14:24:03 -08:00
Max Brunsfeld
0b63d882ce Allow key bindings to F1 through F12 2021-11-18 14:23:36 -08:00
Max Brunsfeld
6aa346dec8 Fix switched input + output rows in BlockSnapshot::buffer_rows 2021-11-18 12:41:58 -08:00
Max Brunsfeld
bef09696f6 Align block text with the anchor's column
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-18 12:17:22 -08:00
Antonio Scandurra
1a8b23e118 Color diagnostic messages based on their severity
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-18 17:47:10 +01:00
Antonio Scandurra
f39942863b Dismiss active diagnostics when hitting escape
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-18 16:57:05 +01:00
Antonio Scandurra
5094380c83 Enhance keyboard navigation when showing next diagnostic
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-18 16:55:18 +01:00
Antonio Scandurra
643545e91e When showing the next diagnostic, advance to the next *primary* one
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-18 16:04:16 +01:00
Antonio Scandurra
0e51365770 In a diagnostic group, mark the highest-severity diagnostic as primary
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-18 15:53:00 +01:00
Antonio Scandurra
401b59be5c Refactor retrieving oldest and newest selection
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-18 15:18:35 +01:00
Antonio Scandurra
0a6293bcda Support highlighting in blocks
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-18 14:08:21 +01:00
Antonio Scandurra
0f1eb3dd2e Skip block lines when moving up and down
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-18 13:45:06 +01:00
Antonio Scandurra
856768a43c Remove aggressive logging from WrapMap update code paths 2021-11-18 11:13:40 +01:00
Antonio Scandurra
08e0444ee4 Use char count instead of byte count to determine longest row 2021-11-18 11:01:12 +01:00
Antonio Scandurra
b80887dabe Don't insert blocks within multi-byte characters in randomized test 2021-11-18 10:54:25 +01:00
Antonio Scandurra
572e571927 Test longest row only when tabs are not present or the tab size is 1
This is because the longest row calculation is best-effort at the moment,
since this information is not indexed in the `TabMap`.
2021-11-18 10:54:22 +01:00
Antonio Scandurra
5a9dea5299 Ensure TabMap works correctly when there are folds 2021-11-18 10:37:04 +01:00
Antonio Scandurra
9ba24794c7 Re-enable tabs, spaces and multi-byte characters in randomized tests 2021-11-18 10:35:31 +01:00
Antonio Scandurra
84d257470a Fix empty range edge case in FoldMap 2021-11-18 10:33:31 +01:00
Antonio Scandurra
4967a8d5ef Trim expanded tabs if they overshoot the provided range 2021-11-18 09:52:42 +01:00
Antonio Scandurra
b10c82c015 Stop at range.end when computing text summary for range in TabMap 2021-11-18 09:52:05 +01:00
Nathan Sobo
213aa36e1c WIP: Track down bugs with longest_row on wrap map
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-11-17 19:45:56 -07:00
Nathan Sobo
c5956a0363 Start at the end of the last transform when catching up to edits during wrapping
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-11-17 19:44:17 -07:00
Max Brunsfeld
8230dd9a3b WIP - BlockSnapshot::longest_row 2021-11-17 16:00:52 -08:00
Max Brunsfeld
cb18131432 Represent scroll position correctly when scrolled mid-block
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-17 14:56:55 -08:00
Max Brunsfeld
707ffe8ff3 Implement BlockSnapshot::line_len, use it in DisplayMap
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-17 14:56:05 -08:00
Max Brunsfeld
00b5cc472e Fix BlockSnapshot::chunks when starting in a block
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-17 14:54:47 -08:00
Max Brunsfeld
1c3bf90a8a Reimplement BlockSnapshot::{clip_point,to_block_point,max_point}
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-17 12:54:14 -08:00
Antonio Scandurra
e60500dd7c Re-enable soft-wrapping in randomized tests
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-11-17 19:46:43 +01:00
Antonio Scandurra
88d0c04444 Implement BlockSnapshot::buffer_rows
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-11-17 19:45:06 +01:00
Antonio Scandurra
198f6694b7 Use options to represent soft-wrapped buffer rows
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-11-17 19:30:40 +01:00
Antonio Scandurra
d9283efbe6 Make BlockMap 1d
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-11-17 19:26:57 +01:00
Nathan Sobo
18354c5e04 Hack in show next diagnostic command
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-11-16 16:51:38 -08:00
Nathan Sobo
52a4c15c14 Eliminate non-highlighted chunks APIs
Now we only have a single code path for chunks across all layers, but highlighting is optional and controlled by a flag.

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-11-16 16:51:38 -08:00
Nathan Sobo
7dd9b9539e WIP 2021-11-16 13:19:04 -07:00
Nathan Sobo
092689ed56 WIP
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-11-16 13:10:06 -07:00
Nathan Sobo
880b3f087f Insert empty isomorphic transforms on empty lines
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2021-11-16 12:14:00 -07:00
Antonio Scandurra
d25ec39a23 Rework BufferRows iterator to pass the randomized tests
...without booleans.

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-11-16 18:18:47 +01:00
Antonio Scandurra
712616d167 Start on a randomized test for BlockMap::buffer_rows 2021-11-16 15:35:35 +01:00
Max Brunsfeld
1cc7615d06 Implement basic version of BlockMap::buffer_rows
Passed 1 simple test

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-15 17:41:13 -08:00
Max Brunsfeld
76ee44748e Fix minor bug in BlockMap::clip_point
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-15 16:25:51 -08:00
Nathan Sobo
7d1ba6455b Implement BlockMapWriter::remove
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-11-15 17:09:26 -07:00
Nathan Sobo
7b12c1c9e0 Enable soft wrap in randomized test of BlockMap
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-11-15 16:59:02 -07:00
Nathan Sobo
862b988d56 Position blocks above/below buffer lines, even when the anchored line is soft-wrapped
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-11-15 16:45:51 -07:00
Max Brunsfeld
2cb8b0fcd3 wip 2021-11-15 15:04:53 -08:00
Max Brunsfeld
3bd4542bce Remove bias parameter when converting display points to buffer points
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-15 14:36:03 -08:00
Max Brunsfeld
213b94afd4 Remove bias parameter from to_display_point
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-15 14:21:55 -08:00
Max Brunsfeld
8b1b35913a Fix group_ids assertions in diagnostics test
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-15 14:18:58 -08:00
Max Brunsfeld
0a704b8d67 Fix infinite loop in BlockMap::highlighted_chunks
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-15 14:18:45 -08:00
Max Brunsfeld
b4bc7906d2 Propagate wrap edits to block map when folding / unfolding
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-15 14:18:24 -08:00
Max Brunsfeld
d2f4d37af8 Get BlockMap randomized test passing w/o soft wraps
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-15 13:36:40 -08:00
Max Brunsfeld
3498e92d1c Get BlockMap unit test passing 2021-11-15 12:39:40 -08:00
Antonio Scandurra
763ab4d5f1 WIP 2021-11-15 19:52:48 +01:00
Antonio Scandurra
53872a6024 WIP 2021-11-15 19:38:06 +01:00
Antonio Scandurra
314c97715d WIP 2021-11-15 18:01:30 +01:00
Antonio Scandurra
131979dff0 WIP 2021-11-15 17:54:28 +01:00
Antonio Scandurra
34f85b5690 WIP 2021-11-15 17:15:30 +01:00
Antonio Scandurra
cebab56c94 Make BlockMap randomized test pass in low-complexity cases
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-15 16:47:30 +01:00
Antonio Scandurra
296944e34d Make BlockMap unit test pass with 2d coordinates
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-15 16:30:58 +01:00
Nathan Sobo
3154ccbafe WIP 2021-11-14 16:24:40 -07:00
Nathan Sobo
e644c0876e WIP: Start moving BlockMap to a 2d indexing scheme 2021-11-14 08:29:41 -07:00
Nathan Sobo
5832153712 Hack: Synthesize a newline before blocks below the last line of the buffer 2021-11-14 07:43:35 -07:00
Nathan Sobo
b6e6dafca7 Account for trailing below blocks in BlockSnapshot::max_point 2021-11-13 19:50:42 -07:00
Nathan Sobo
d6bc05cad0 Fix BlockMap unit test by skipping below blocks when advancing transforms 2021-11-13 18:19:21 -07:00
Nathan Sobo
c9cbeafc05 Start on BlockSnapshot::clip_point
Not sure it works yet. Ran into another failure in the unit tests.
2021-11-13 17:44:09 -07:00
Max Brunsfeld
364fab7b5f wip - wiring up blockmap into displaymap 2021-11-12 17:29:09 -08:00
Max Brunsfeld
c278503166 Make block insertion work in simple cases
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-12 16:40:01 -08:00
Max Brunsfeld
2e61a586b6 Fix compile errors 2021-11-12 13:55:47 -08:00
Nathan Sobo
e605a5ead2 Sketch an initial implementation for block_map::HighlightedChunks
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-11-12 14:51:25 -07:00
Max Brunsfeld
6f97a9be3b wip 2021-11-12 12:09:35 -08:00
Max Brunsfeld
227c612dac BlockMap WIP 2021-11-12 11:49:48 -08:00
Antonio Scandurra
c8e47a8c63 Start on a randomized test for BlockMap
This is currently passing and ensures we maintain the input coordinate
space correctly.

Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-11-12 19:01:39 +01:00
Antonio Scandurra
d721c2ba4b Remove redundant code path in Patch::compose 2021-11-12 17:24:25 +01:00
Antonio Scandurra
3f11b8af56 Maintain row edits since last sync in WrapMap 2021-11-12 17:05:49 +01:00
Antonio Scandurra
4e32fabfdc Add text manipulation facilities to Rope for test purposes 2021-11-12 17:02:51 +01:00
Antonio Scandurra
fe786f3366 Init env_logger in the editor crate for tests only 2021-11-12 17:01:57 +01:00
Antonio Scandurra
b9c459e800 Use log::info instead of println in patch randomized tests 2021-11-12 17:00:44 +01:00
Antonio Scandurra
b2aab0c773 🎨 2021-11-12 16:31:01 +01:00
Antonio Scandurra
f49c9db423 Make Patch::compose work 2021-11-12 15:07:28 +01:00
Antonio Scandurra
6e882bcd02 Avoid composing edits together for now 2021-11-12 09:01:15 +01:00
Nathan Sobo
068aa1adb3 WIP 2021-11-12 00:20:39 -07:00
Nathan Sobo
47ad9baebc wip 2021-11-12 00:03:47 -07:00
Nathan Sobo
84d789b8ac WIP 2021-11-11 23:28:45 -07:00
Max Brunsfeld
0159019850 Simplify assertions in randomized patch test, fix some patch bugs 2021-11-11 18:28:07 -08:00
Max Brunsfeld
1f2eb9ddbc Add patch unit tests, get composition working for ops <= 3
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-11 17:36:30 -08:00
Antonio Scandurra
d75f415b25 WIP 2021-11-11 19:49:01 +01:00
Antonio Scandurra
4fecab6d4b WIP 2021-11-11 19:42:55 +01:00
Antonio Scandurra
e0897cd019 WIP 2021-11-11 18:13:35 +01:00
Antonio Scandurra
a939535d95 WIP
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-11 16:52:14 +01:00
Antonio Scandurra
59bbe43a46 WIP
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-11 16:00:52 +01:00
Antonio Scandurra
b2caf9e905 WIP: Start on BlockMap
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-11 15:04:39 +01:00
Antonio Scandurra
7dcf30c954 WIP 2021-11-11 15:04:31 +01:00
Nathan Sobo
118f137f18 WIP: Rework injection map to be focused solely on block injections 2021-11-10 20:49:06 -07:00
Nathan Sobo
0fff7d9166 WIP: Probably the wrong direction 2021-11-10 17:44:56 -07:00
Nathan Sobo
62ec105bff WIP 2021-11-10 14:00:51 -07:00
Nathan Sobo
c2b44537aa Fix path to templates that's used when minifying tailwind CSS 2021-11-07 07:32:26 -07:00
Nathan Sobo
f33d30cb9d Upgrade builder image to Rust 1.56 2021-11-06 22:10:27 -06:00
Max Brunsfeld
8b9488bacb Add missing group_id fields in rpc test 2021-11-04 17:34:33 -07:00
Antonio Scandurra
2f4d8932dc Allow querying a diagnostic group by its id
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-04 15:16:59 +01:00
Antonio Scandurra
78bbb83448 Assign diagnostics a group_id based on their related_information
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-04 14:52:34 +01:00
Max Brunsfeld
61b9179fb1 Merge pull request #222 from zed-industries/fix-hover-beachball
Give the MouseEventHandlers for each tab bar their own id
2021-11-03 19:38:04 -07:00
Max Brunsfeld
a72bdac7df Fix paths to server crate in scripts and gitignore
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-03 19:22:00 -07:00
Nathan Sobo
0ff87e603f Give the MouseEventHandlers for each tab bar their own id
This fixes a beachball where we oscillate back and forth between hovered and unhovered due to confusing two different tab bars as the same tab bar.
2021-11-03 19:15:55 -06:00
Nathan Sobo
2d6285a6e1 Start on a test for grouped diagnostics 2021-11-03 18:47:18 -06:00
Nathan Sobo
44e0a00734 Only show the first line of diagnostic messages in the status bar
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-11-03 16:52:32 -06:00
Nathan Sobo
595dbd44ae Merge pull request #221 from zed-industries/status-bar-errors
Add a status bar that shows the cursor position and the error under the cursor
2021-11-03 15:46:33 -06:00
Max Brunsfeld
1ec31738e6 Improve styling of diagnostic status bar item 2021-11-03 14:15:22 -07:00
Max Brunsfeld
baf636a4a4 Extend empty diagnostic ranges at the ends of lines 2021-11-03 13:49:39 -07:00
Max Brunsfeld
9384823e47 Remove logging in worktree tests 2021-11-03 12:36:57 -07:00
Max Brunsfeld
8b5089c759 In the status bar, show the diagnostic under the cursor 2021-11-03 12:33:16 -07:00
Antonio Scandurra
941d935c4a End pending selection when starting a transaction
Co-Authored-By: Max Brunsfeld <max@zed.dev>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-03 19:15:54 +01:00
Antonio Scandurra
c07d794249 Avoid ending the pending selection until updating selections
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-11-03 19:07:06 +01:00
Antonio Scandurra
9dc3c74260 Make resolving selections generic
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-11-03 17:51:57 +01:00
Antonio Scandurra
a26b066788 Introduce a status bar and add the cursor position to it
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-11-03 17:27:51 +01:00
Antonio Scandurra
306ebb256c Merge pull request #218 from zed-industries/lsp
Integrate rust-analyzer and highlight diagnostics
2021-11-03 10:17:13 +01:00
Antonio Scandurra
258b89bb70 Request autoscroll when undoing/redoing 2021-11-03 09:44:23 +01:00
Antonio Scandurra
20a77f4c5e Fix test for disk-based diagnostics 2021-11-03 09:39:15 +01:00
Max Brunsfeld
9a7ecfbc4f Use status colors for diagnostic underlines 2021-11-02 17:51:18 -07:00
Max Brunsfeld
8d3f42de52 Start language servers based on buffers' languages
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-02 17:41:01 -07:00
Max Brunsfeld
a66b81d60a Add an integration test for replicating buffer's diagnostics 2021-11-02 14:57:24 -07:00
Max Brunsfeld
89392cd23d Avoid using worktree handle in File's path methods
This avoids a circular model update that was happening
when trying to retrieve the absolute path from a buffer's
file while applying remote operations.
2021-11-02 14:33:55 -07:00
Max Brunsfeld
1995bd89a6 Deserialize buffer's diagnostics
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-02 12:29:03 -07:00
Nathan Sobo
2c57703ad6 Explicitly shut down language servers when quitting the app
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2021-11-02 13:16:25 -06:00
Antonio Scandurra
882c8ce696 Avoid error during deserialization if the result field isn't there 2021-11-02 14:58:00 +01:00
Antonio Scandurra
f5aa07aac9 Remove all windows before quitting
This gives all entities a chance of running `Drop::drop` which,
in turn, could cause them to spawn a critical task. For example,
we use this capability when a language server is dropped and we
need to asynchronously send a shutdown message.
2021-11-02 14:55:31 +01:00
Max Brunsfeld
61e06487b7 Avoid circular model update when sending diagnostics operations 2021-11-01 17:14:22 -07:00
Max Brunsfeld
f0353d6aba Add note in README about rustc bug on macOS monterey 2021-11-01 17:11:40 -07:00
Max Brunsfeld
0e62ddbb65 Replicate diagnostics to remote buffers
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-01 15:28:37 -07:00
Max Brunsfeld
40c861c249 Move protobuf logic from buffer crate to language crate
This will enable us to add operations that only pertain to the language crate.

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-01 14:05:19 -07:00
Max Brunsfeld
78d97a3db2 Use Diagnostic struct in buffer's diagnostics multimap 2021-11-01 12:59:01 -07:00
Max Brunsfeld
1aee7bdb1d Delay quit until language servers are gracefully shut down
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-11-01 11:57:31 -07:00
Max Brunsfeld
b8994c2a89 Add a facility for delaying quit until critical tasks finish
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2021-11-01 11:57:31 -07:00
Antonio Scandurra
6e5ec2a00d Take a &clock::Global instead of cloning in edits_since 2021-11-01 10:48:20 +01:00
Antonio Scandurra
2919cbe9cb Try signing rust-analyzer in order to embed it in the app bundle 2021-11-01 10:30:10 +01:00
Antonio Scandurra
f59be5fecf Always notify when receiving buffer operations
We had changed it to only emit a notification when the buffer was
actually edited, but we also want to notify when we receive non-edit
operations, such as a selection update.
2021-11-01 10:06:23 +01:00
Nathan Sobo
3228a55329 Fix test-support feature propagation for editor and workspace 2021-10-30 07:40:20 -06:00
Antonio Scandurra
b571eae4f3 Extend empty diagnostic ranges by one character
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-10-29 19:09:19 +02:00
Antonio Scandurra
6212ebad9b Communicate with language servers in terms of UTF-16 coordinates
This required indexing UTF-16 positions in `Rope`. We tried opting
into the UTF-8 experimental support but it didn't seem to work
correctly and the standard is UTF-16 anyway.

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-10-29 18:33:23 +02:00
Antonio Scandurra
9c1b01521a Avoid unnecessary conversion to offsets in diagnostics_in_range 2021-10-29 14:41:41 +02:00
Antonio Scandurra
78c158e1a4 Support only UTF-8 for now when communicating with language server 2021-10-29 12:02:42 +02:00
Antonio Scandurra
a82a12fd14 Bundle fat-binary for rust-analyzer 2021-10-29 11:41:07 +02:00
Max Brunsfeld
2cbb313467 Avoid panic when rust language isn't present (project unit tests) 2021-10-28 17:12:17 -07:00
Max Brunsfeld
e1556893f7 Merge branch 'anchor-map-selections' into lsp 2021-10-28 17:08:06 -07:00
Max Brunsfeld
927118726c Merge pull request #216 from zed-industries/anchor-map-selections
Represent selection sets as anchor range maps
2021-10-28 17:07:45 -07:00
Max Brunsfeld
2952f2c905 🎨 anchor.rs 2021-10-28 16:48:07 -07:00
Max Brunsfeld
acb29eb273 Ignore vendor/bin folder 2021-10-28 16:32:56 -07:00
Max Brunsfeld
a1e576343e Rename AnchorRangeSet::to_point_ranges -> point_ranges 2021-10-28 16:32:49 -07:00
Max Brunsfeld
9bc08e446b Fix unit test for on-disk-changes to not rely on selection bias 2021-10-28 16:17:07 -07:00
Max Brunsfeld
f3cd710f21 Create valid disjoint selection sets in selections_in_ranges 2021-10-28 16:04:16 -07:00
Max Brunsfeld
efc85d1b75 Get the Editor crate compiling 2021-10-28 15:42:24 -07:00
Antonio Scandurra
9c74be3bf2 Start fixing compilation errors on Editor
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-10-28 19:36:43 +02:00
Antonio Scandurra
ce8741977b Clip points coming from language server
This avoids panicking in Zed if the points they give us are invalid.

Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-10-28 19:02:26 +02:00
Antonio Scandurra
d12387b753 Ensure start endpoints always come before end endpoints
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-10-28 18:26:32 +02:00
Antonio Scandurra
50afb2d65f Remove stray println! statements in the LSP I/O code 2021-10-28 14:07:14 +02:00
Antonio Scandurra
ee78d6f17b Express multi-cursor edits using the new coordinate space
The language server expects that ranges further in the list of edits
account for the impact of prior changes in the edit list.
2021-10-28 13:40:55 +02:00
Antonio Scandurra
7091e0c567 Add a unit test for disk-based diagnostics 2021-10-28 11:37:24 +02:00
Antonio Scandurra
ac76706aa7 Sort LSP diagnostics by (start, end) 2021-10-28 11:36:33 +02:00
Antonio Scandurra
fcb217b9e8 Report new_lines extent correctly when coalescing edits 2021-10-28 11:32:10 +02:00
Nathan Sobo
9977248926 Adjust disk-based diagnostics based on edits since the last save
Still need to add tests... not sure if this is right yet.

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
2021-10-27 18:58:07 -06:00
Max Brunsfeld
0c10d6c82d Introduce FullOffset type
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
2021-10-27 18:30:00 -06:00
Antonio Scandurra
bc076c1cc1 Update display map snapshots when diagnostics are updated
This is similar to what we do when we receive new parse trees from
tree-sitter.
2021-10-27 12:42:16 +02:00
Antonio Scandurra
a7a73a5b0b Fix bug in to_full_offset when Anchor::version != Content::version 2021-10-27 11:56:04 +02:00
Max Brunsfeld
c539069cbb Include diagnostic info in HighlightedChunks iterator
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-10-26 17:57:50 -07:00
Max Brunsfeld
f1db618be2 Generalize AnchorRangeMultimap's 'intersecting ranges' API
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-10-26 17:56:39 -07:00
Max Brunsfeld
79ba217485 Fix routing of diagnostics to buffers in worktree 2021-10-26 15:46:39 -07:00
Max Brunsfeld
ef4fc42d93 Allow retrieving a buffer's diagnostics 2021-10-26 15:46:08 -07:00
Max Brunsfeld
5bfbeb55c0 Simplify buffer constructor methods
Don't expose the `buffer::History` to callers of `language::Buffer`
2021-10-26 14:26:47 -07:00
Max Brunsfeld
4069db4959 Allow underlines to have different color than the text
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-10-26 12:46:46 -07:00
Max Brunsfeld
7d5425e142 Move lsp configuration into language crate
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-10-26 12:17:51 -07:00
Max Brunsfeld
de8218314c Notify language server when saving a buffer
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-10-26 11:45:59 -07:00
Nathan Sobo
1a92a19954 Remove Anchor from protocol 2021-10-26 12:04:04 -06:00
Antonio Scandurra
0674e76864 WIP 2021-10-26 19:42:40 +02:00
Antonio Scandurra
60abc5f090 Take ToOffset instead of anchors in intersecting_point_ranges
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-10-26 17:04:12 +02:00
Antonio Scandurra
e8a2885721 Introduce Content::anchor_range_multimap 2021-10-26 14:28:02 +02:00
Antonio Scandurra
5dc47c625e Fix compilation errors 2021-10-26 14:27:00 +02:00
Max Brunsfeld
64445c7d1c Start work on AnchorRangeMultimap
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-10-25 21:42:55 -07:00
Max Brunsfeld
50c77daa0b Start work on a test for worktree handling LSP diagnostics 2021-10-25 21:42:55 -07:00
Max Brunsfeld
c3ff489fee Handle initialize request internally in fake lsp server
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-10-25 18:04:27 -07:00
Max Brunsfeld
6384950d56 Merge remote-tracking branch 'origin/main' into lsp 2021-10-25 16:24:08 -07:00
Max Brunsfeld
b49a268031 Add a fake lsp server
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-10-25 15:28:40 -07:00
Antonio Scandurra
2d6d10f920 Log unhandled notifications in LanguageServer
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-10-25 19:46:33 +02:00
Antonio Scandurra
580bad2042 Get a basic end-to-end test for rust-analyzer integration working
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-10-25 18:11:52 +02:00
Antonio Scandurra
9759f9e947 Uncomment script/bundle lines
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-10-25 17:35:45 +02:00
Nathan Sobo
ab4f90a20a Get language and project compiling 2021-10-25 09:26:36 -06:00
Antonio Scandurra
7105589904 Don't send notifications or requests until LSP is initialized 2021-10-25 12:29:28 +02:00
Antonio Scandurra
59ed535cdf Implement a more robust way of locating rust-analyzer
When bundled, we will retrieve it out of the `Resources` folder.
Locally, we're expected to run `script/download-rust-analyzer` and
put `vendor/bin` in our $PATH.
2021-10-25 11:02:35 +02:00
Nathan Sobo
60a8e74430 Get buffer compiling with new SelectionSets based on AnchorRangeMap
One test is failing however.
2021-10-22 14:12:16 -06:00
Nathan Sobo
6ba4af3e26 WIP: Start converting SelectionSet to use AnchorRangeMap 2021-10-22 13:19:19 -06:00
Nathan Sobo
3ae5ba09fd Implement TryFrom<proto::SelectionSet> on SelectionSet
More prep work for changing the selection set representation.
2021-10-22 12:46:02 -06:00
Nathan Sobo
401bdf0ba1 Simplify protocol messages related to selection sets
This prepares the way to switch to using AnchorRangeMaps to store and transmit selection sets.
2021-10-22 12:35:29 -06:00
Nathan Sobo
087ff28d0d Move SelectionSet and Into impl to selection module 2021-10-22 09:56:47 +02:00
Antonio Scandurra
715faaaceb WIP 2021-10-21 19:27:10 +02:00
Antonio Scandurra
2c6aeaed7c Start on integrating rust-analyzer
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
2021-10-21 16:26:37 +02:00
Max Brunsfeld
559774d6ac Merge pull request #213 from zed-industries/language-buffer
Extract Buffer's language-aware behavior into a new `language` crate
2021-10-21 13:22:04 +02:00
Max Brunsfeld
282195b13e Assign new file handles on buffers when their files change on disk
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-10-21 13:08:54 +02:00
Max Brunsfeld
eb9d7c8660 Update buffer's saved mtime when file is reloaded after on-disk change
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-10-21 12:05:44 +02:00
Max Brunsfeld
eea0f35d38 Rename TextBuffer back to Buffer 2021-10-21 11:12:19 +02:00
Max Brunsfeld
37eae2ba67 Remove unnecessary dependencies in buffer and language crates 2021-10-21 09:40:50 +02:00
Max Brunsfeld
81a85e9c79 Extract a language crate 2021-10-20 22:51:40 +02:00
Max Brunsfeld
cdb268e656 Re-enable randomized concurrent edits test 2021-10-20 21:44:26 +02:00
Max Brunsfeld
30e2e2014d Extract a TextBuffer from Buffer, which has no tree or file
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-10-20 00:10:57 +02:00
194 changed files with 36108 additions and 213605 deletions

View File

@@ -32,6 +32,11 @@ jobs:
with:
clean: false
- name: Download rust-analyzer
run: |
script/download-rust-analyzer
echo "$PWD/vendor/bin" >> $GITHUB_PATH
- name: Run tests
run: cargo test --workspace --no-fail-fast
@@ -63,6 +68,9 @@ jobs:
with:
clean: false
- name: Download rust-analyzer
run: script/download-rust-analyzer
- name: Create app bundle
run: script/bundle

5
.gitignore vendored
View File

@@ -2,5 +2,6 @@
/zed.xcworkspace
.DS_Store
/script/node_modules
/server/.env.toml
/server/static/styles.css
/crates/server/.env.toml
/crates/server/static/styles.css
/vendor/bin

1222
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -4,7 +4,6 @@ default-members = ["crates/zed"]
[patch.crates-io]
async-task = { git = "https://github.com/zed-industries/async-task", rev = "341b57d6de98cdfd7b418567b8de2022ca993a6e" }
tree-sitter = { git = "https://github.com/tree-sitter/tree-sitter", rev = "d72771a19f4143530b1cfd23808e344f1276e176" }
# TODO - Remove when a version is released with this PR: https://github.com/servo/core-foundation-rs/pull/457
cocoa = { git = "https://github.com/servo/core-foundation-rs", rev = "025dcb3c0d1ef01530f57ef65f3b1deb948f5737" }
cocoa-foundation = { git = "https://github.com/servo/core-foundation-rs", rev = "025dcb3c0d1ef01530f57ef65f3b1deb948f5737" }

View File

@@ -1,6 +1,6 @@
# syntax = docker/dockerfile:1.2
FROM rust:1.55-bullseye as builder
FROM rust:1.56-bullseye as builder
WORKDIR app
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
RUN apt-get install -y nodejs

2
Procfile Normal file
View File

@@ -0,0 +1,2 @@
web: cd ../zed.dev && PORT=3000 npx next dev
collab: cd crates/server && cargo run

View File

@@ -6,9 +6,41 @@ Welcome to Zed, a lightning-fast, collaborative code editor that makes your drea
## Development tips
### Testing against locally-running servers
Make sure you have `zed.dev` cloned as a sibling to this repo.
```
cd ..
git clone https://github.com/zed-industries/zed.dev
```
Make sure your local database is created, migrated, and seeded with initial data. Install [Postgres](https://postgresapp.com), then from the `zed` repository root, run:
```
script/sqlx database create
script/sqlx migrate run
script/seed-db
```
Run `zed.dev` and the collaboration server.
```
brew install foreman
foreman start
```
If you want to run Zed pointed at the local servers, you can run:
```
script/zed_with_local_servers
# or...
script/zed_with_local_servers --release
```
### Dump element JSON
If you trigger `cmd-shift-i`, Zed will copy a JSON representation of the current window contents to the clipboard. You can paste this in a tool like [DJSON](https://chrome.google.com/webstore/detail/djson-json-viewer-formatt/chaeijjekipecdajnijdldjjipaegdjc?hl=en) to navigate the state of on-screen elements in a structured way.
If you trigger `cmd-alt-i`, Zed will copy a JSON representation of the current window contents to the clipboard. You can paste this in a tool like [DJSON](https://chrome.google.com/webstore/detail/djson-json-viewer-formatt/chaeijjekipecdajnijdldjjipaegdjc?hl=en) to navigate the state of on-screen elements in a structured way.
## Roadmap
@@ -26,12 +58,12 @@ Establish basic infrastructure for building the app bundle and uploading an arti
[Tracking issue](https://github.com/zed-industries/zed/issues/6)
Turn the minimal text editor into a collaborative *code* editor. This will include the minimal features that the Zed team needs to collaborate in Zed to build Zed without net loss in developer productivity. This includes productivity-critical features such as:
Turn the minimal text editor into a collaborative _code_ editor. This will include the minimal features that the Zed team needs to collaborate in Zed to build Zed without net loss in developer productivity. This includes productivity-critical features such as:
* Syntax highlighting and syntax-aware editing and navigation
* The ability to see and edit non-local working copies of a repository
* Language server support for Rust code navigation, refactoring, diagnostics, etc.
* Project browsing and project-wide search and replace
- Syntax highlighting and syntax-aware editing and navigation
- The ability to see and edit non-local working copies of a repository
- Language server support for Rust code navigation, refactoring, diagnostics, etc.
- Project browsing and project-wide search and replace
We want to tackle collaboration fairly early so that the rest of the design of the product can flow around that assumption. We could probably produce a single-player code editor more quickly, but at the risk of having collaboration feel more "bolted on" when we eventually add it.

View File

@@ -1,151 +0,0 @@
use crate::Point;
use super::{Buffer, Content};
use anyhow::Result;
use std::{cmp::Ordering, ops::Range};
use sum_tree::Bias;
#[derive(Clone, Eq, PartialEq, Debug, Hash)]
pub struct Anchor {
pub offset: usize,
pub bias: Bias,
pub version: clock::Global,
}
#[derive(Clone)]
pub struct AnchorMap<T> {
pub(crate) version: clock::Global,
pub(crate) entries: Vec<((usize, Bias), T)>,
}
#[derive(Clone)]
pub struct AnchorSet(pub(crate) AnchorMap<()>);
#[derive(Clone)]
pub struct AnchorRangeMap<T> {
pub(crate) version: clock::Global,
pub(crate) entries: Vec<(Range<(usize, Bias)>, T)>,
}
#[derive(Clone)]
pub struct AnchorRangeSet(pub(crate) AnchorRangeMap<()>);
impl Anchor {
pub fn min() -> Self {
Self {
offset: 0,
bias: Bias::Left,
version: Default::default(),
}
}
pub fn max() -> Self {
Self {
offset: usize::MAX,
bias: Bias::Right,
version: Default::default(),
}
}
pub fn cmp<'a>(&self, other: &Anchor, buffer: impl Into<Content<'a>>) -> Result<Ordering> {
let buffer = buffer.into();
if self == other {
return Ok(Ordering::Equal);
}
let offset_comparison = if self.version == other.version {
self.offset.cmp(&other.offset)
} else {
buffer
.full_offset_for_anchor(self)
.cmp(&buffer.full_offset_for_anchor(other))
};
Ok(offset_comparison.then_with(|| self.bias.cmp(&other.bias)))
}
pub fn bias_left(&self, buffer: &Buffer) -> Anchor {
if self.bias == Bias::Left {
self.clone()
} else {
buffer.anchor_before(self)
}
}
pub fn bias_right(&self, buffer: &Buffer) -> Anchor {
if self.bias == Bias::Right {
self.clone()
} else {
buffer.anchor_after(self)
}
}
}
impl<T> AnchorMap<T> {
pub fn to_points<'a>(
&'a self,
content: impl Into<Content<'a>> + 'a,
) -> impl Iterator<Item = (Point, &'a T)> + 'a {
let content = content.into();
content
.summaries_for_anchors(self)
.map(move |(sum, value)| (sum.lines, value))
}
pub fn version(&self) -> &clock::Global {
&self.version
}
}
impl AnchorSet {
pub fn to_points<'a>(
&'a self,
content: impl Into<Content<'a>> + 'a,
) -> impl Iterator<Item = Point> + 'a {
self.0.to_points(content).map(move |(point, _)| point)
}
}
impl<T> AnchorRangeMap<T> {
pub fn to_point_ranges<'a>(
&'a self,
content: impl Into<Content<'a>> + 'a,
) -> impl Iterator<Item = (Range<Point>, &'a T)> + 'a {
let content = content.into();
content
.summaries_for_anchor_ranges(self)
.map(move |(range, value)| ((range.start.lines..range.end.lines), value))
}
pub fn version(&self) -> &clock::Global {
&self.version
}
}
impl AnchorRangeSet {
pub fn to_point_ranges<'a>(
&'a self,
content: impl Into<Content<'a>> + 'a,
) -> impl Iterator<Item = Range<Point>> + 'a {
self.0.to_point_ranges(content).map(|(range, _)| range)
}
pub fn version(&self) -> &clock::Global {
self.0.version()
}
}
pub trait AnchorRangeExt {
fn cmp<'a>(&self, b: &Range<Anchor>, buffer: impl Into<Content<'a>>) -> Result<Ordering>;
}
impl AnchorRangeExt for Range<Anchor> {
fn cmp<'a>(&self, other: &Range<Anchor>, buffer: impl Into<Content<'a>>) -> Result<Ordering> {
let buffer = buffer.into();
Ok(match self.start.cmp(&other.start, &buffer)? {
Ordering::Equal => other.end.cmp(&self.end, buffer)?,
ord @ _ => ord,
})
}
}

View File

@@ -1,166 +0,0 @@
use crate::HighlightMap;
use anyhow::Result;
use parking_lot::Mutex;
use serde::Deserialize;
use std::{path::Path, str, sync::Arc};
use theme::SyntaxTheme;
use tree_sitter::{Language as Grammar, Query};
pub use tree_sitter::{Parser, Tree};
#[derive(Default, Deserialize)]
pub struct LanguageConfig {
pub name: String,
pub path_suffixes: Vec<String>,
pub brackets: Vec<BracketPair>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct BracketPair {
pub start: String,
pub end: String,
pub close: bool,
pub newline: bool,
}
pub struct Language {
pub(crate) config: LanguageConfig,
pub(crate) grammar: Grammar,
pub(crate) highlights_query: Query,
pub(crate) brackets_query: Query,
pub(crate) indents_query: Query,
pub(crate) highlight_map: Mutex<HighlightMap>,
}
#[derive(Default)]
pub struct LanguageRegistry {
languages: Vec<Arc<Language>>,
}
impl LanguageRegistry {
pub fn new() -> Self {
Self::default()
}
pub fn add(&mut self, language: Arc<Language>) {
self.languages.push(language);
}
pub fn set_theme(&self, theme: &SyntaxTheme) {
for language in &self.languages {
language.set_theme(theme);
}
}
pub fn select_language(&self, path: impl AsRef<Path>) -> Option<&Arc<Language>> {
let path = path.as_ref();
let filename = path.file_name().and_then(|name| name.to_str());
let extension = path.extension().and_then(|name| name.to_str());
let path_suffixes = [extension, filename];
self.languages.iter().find(|language| {
language
.config
.path_suffixes
.iter()
.any(|suffix| path_suffixes.contains(&Some(suffix.as_str())))
})
}
}
impl Language {
pub fn new(config: LanguageConfig, grammar: Grammar) -> Self {
Self {
config,
brackets_query: Query::new(grammar, "").unwrap(),
highlights_query: Query::new(grammar, "").unwrap(),
indents_query: Query::new(grammar, "").unwrap(),
grammar,
highlight_map: Default::default(),
}
}
pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
self.highlights_query = Query::new(self.grammar, source)?;
Ok(self)
}
pub fn with_brackets_query(mut self, source: &str) -> Result<Self> {
self.brackets_query = Query::new(self.grammar, source)?;
Ok(self)
}
pub fn with_indents_query(mut self, source: &str) -> Result<Self> {
self.indents_query = Query::new(self.grammar, source)?;
Ok(self)
}
pub fn name(&self) -> &str {
self.config.name.as_str()
}
pub fn brackets(&self) -> &[BracketPair] {
&self.config.brackets
}
pub fn highlight_map(&self) -> HighlightMap {
self.highlight_map.lock().clone()
}
pub fn set_theme(&self, theme: &SyntaxTheme) {
*self.highlight_map.lock() =
HighlightMap::new(self.highlights_query.capture_names(), theme);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_select_language() {
let grammar = tree_sitter_rust::language();
let registry = LanguageRegistry {
languages: vec![
Arc::new(Language::new(
LanguageConfig {
name: "Rust".to_string(),
path_suffixes: vec!["rs".to_string()],
..Default::default()
},
grammar,
)),
Arc::new(Language::new(
LanguageConfig {
name: "Make".to_string(),
path_suffixes: vec!["Makefile".to_string(), "mk".to_string()],
..Default::default()
},
grammar,
)),
],
};
// matching file extension
assert_eq!(
registry.select_language("zed/lib.rs").map(|l| l.name()),
Some("Rust")
);
assert_eq!(
registry.select_language("zed/lib.mk").map(|l| l.name()),
Some("Make")
);
// matching filename
assert_eq!(
registry.select_language("zed/Makefile").map(|l| l.name()),
Some("Make")
);
// matching suffix that is not the full file extension or filename
assert_eq!(registry.select_language("zed/cars").map(|l| l.name()), None);
assert_eq!(
registry.select_language("zed/a.cars").map(|l| l.name()),
None
);
assert_eq!(registry.select_language("zed/sumk").map(|l| l.name()), None);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,75 +0,0 @@
use crate::{Anchor, Buffer, Point, ToOffset as _, ToPoint as _};
use std::{cmp::Ordering, mem, ops::Range};
pub type SelectionSetId = clock::Lamport;
pub type SelectionsVersion = usize;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum SelectionGoal {
None,
Column(u32),
ColumnRange { start: u32, end: u32 },
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Selection {
pub id: usize,
pub start: Anchor,
pub end: Anchor,
pub reversed: bool,
pub goal: SelectionGoal,
}
impl Selection {
pub fn head(&self) -> &Anchor {
if self.reversed {
&self.start
} else {
&self.end
}
}
pub fn set_head(&mut self, buffer: &Buffer, cursor: Anchor) {
if cursor.cmp(self.tail(), buffer).unwrap() < Ordering::Equal {
if !self.reversed {
mem::swap(&mut self.start, &mut self.end);
self.reversed = true;
}
self.start = cursor;
} else {
if self.reversed {
mem::swap(&mut self.start, &mut self.end);
self.reversed = false;
}
self.end = cursor;
}
}
pub fn tail(&self) -> &Anchor {
if self.reversed {
&self.end
} else {
&self.start
}
}
pub fn point_range(&self, buffer: &Buffer) -> Range<Point> {
let start = self.start.to_point(buffer);
let end = self.end.to_point(buffer);
if self.reversed {
end..start
} else {
start..end
}
}
pub fn offset_range(&self, buffer: &Buffer) -> Range<usize> {
let start = self.start.to_offset(buffer);
let end = self.end.to_offset(buffer);
if self.reversed {
end..start
} else {
start..end
}
}
}

View File

@@ -1,2 +0,0 @@
mod buffer;
mod syntax;

View File

@@ -1,790 +0,0 @@
use crate::*;
use clock::ReplicaId;
use rand::prelude::*;
use std::{
cell::RefCell,
cmp::Ordering,
env,
iter::Iterator,
mem,
rc::Rc,
time::{Duration, Instant},
};
#[gpui::test]
fn test_edit(cx: &mut gpui::MutableAppContext) {
cx.add_model(|cx| {
let mut buffer = Buffer::new(0, "abc", cx);
assert_eq!(buffer.text(), "abc");
buffer.edit(vec![3..3], "def", cx);
assert_eq!(buffer.text(), "abcdef");
buffer.edit(vec![0..0], "ghi", cx);
assert_eq!(buffer.text(), "ghiabcdef");
buffer.edit(vec![5..5], "jkl", cx);
assert_eq!(buffer.text(), "ghiabjklcdef");
buffer.edit(vec![6..7], "", cx);
assert_eq!(buffer.text(), "ghiabjlcdef");
buffer.edit(vec![4..9], "mno", cx);
assert_eq!(buffer.text(), "ghiamnoef");
buffer
});
}
#[gpui::test]
fn test_edit_events(cx: &mut gpui::MutableAppContext) {
let mut now = Instant::now();
let buffer_1_events = Rc::new(RefCell::new(Vec::new()));
let buffer_2_events = Rc::new(RefCell::new(Vec::new()));
let buffer1 = cx.add_model(|cx| Buffer::new(0, "abcdef", cx));
let buffer2 = cx.add_model(|cx| Buffer::new(1, "abcdef", cx));
let buffer_ops = buffer1.update(cx, |buffer, cx| {
let buffer_1_events = buffer_1_events.clone();
cx.subscribe(&buffer1, move |_, _, event, _| {
buffer_1_events.borrow_mut().push(event.clone())
})
.detach();
let buffer_2_events = buffer_2_events.clone();
cx.subscribe(&buffer2, move |_, _, event, _| {
buffer_2_events.borrow_mut().push(event.clone())
})
.detach();
// An edit emits an edited event, followed by a dirtied event,
// since the buffer was previously in a clean state.
buffer.edit(Some(2..4), "XYZ", cx);
// An empty transaction does not emit any events.
buffer.start_transaction(None).unwrap();
buffer.end_transaction(None, cx).unwrap();
// A transaction containing two edits emits one edited event.
now += Duration::from_secs(1);
buffer.start_transaction_at(None, now).unwrap();
buffer.edit(Some(5..5), "u", cx);
buffer.edit(Some(6..6), "w", cx);
buffer.end_transaction_at(None, now, cx).unwrap();
// Undoing a transaction emits one edited event.
buffer.undo(cx);
buffer.operations.clone()
});
// Incorporating a set of remote ops emits a single edited event,
// followed by a dirtied event.
buffer2.update(cx, |buffer, cx| {
buffer.apply_ops(buffer_ops, cx).unwrap();
});
let buffer_1_events = buffer_1_events.borrow();
assert_eq!(
*buffer_1_events,
vec![Event::Edited, Event::Dirtied, Event::Edited, Event::Edited]
);
let buffer_2_events = buffer_2_events.borrow();
assert_eq!(*buffer_2_events, vec![Event::Edited, Event::Dirtied]);
}
#[gpui::test(iterations = 100)]
fn test_random_edits(cx: &mut gpui::MutableAppContext, mut rng: StdRng) {
let operations = env::var("OPERATIONS")
.map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
.unwrap_or(10);
let reference_string_len = rng.gen_range(0..3);
let mut reference_string = RandomCharIter::new(&mut rng)
.take(reference_string_len)
.collect::<String>();
cx.add_model(|cx| {
let mut buffer = Buffer::new(0, reference_string.as_str(), cx);
buffer.history.group_interval = Duration::from_millis(rng.gen_range(0..=200));
let mut buffer_versions = Vec::new();
log::info!(
"buffer text {:?}, version: {:?}",
buffer.text(),
buffer.version()
);
for _i in 0..operations {
let (old_ranges, new_text) = buffer.randomly_mutate(&mut rng, cx);
for old_range in old_ranges.iter().rev() {
reference_string.replace_range(old_range.clone(), &new_text);
}
assert_eq!(buffer.text(), reference_string);
log::info!(
"buffer text {:?}, version: {:?}",
buffer.text(),
buffer.version()
);
if rng.gen_bool(0.25) {
buffer.randomly_undo_redo(&mut rng, cx);
reference_string = buffer.text();
log::info!(
"buffer text {:?}, version: {:?}",
buffer.text(),
buffer.version()
);
}
let range = buffer.random_byte_range(0, &mut rng);
assert_eq!(
buffer.text_summary_for_range(range.clone()),
TextSummary::from(&reference_string[range])
);
if rng.gen_bool(0.3) {
buffer_versions.push(buffer.clone());
}
}
for mut old_buffer in buffer_versions {
let edits = buffer
.edits_since(old_buffer.version.clone())
.collect::<Vec<_>>();
log::info!(
"mutating old buffer version {:?}, text: {:?}, edits since: {:?}",
old_buffer.version(),
old_buffer.text(),
edits,
);
let mut delta = 0_isize;
for edit in edits {
let old_start = (edit.old_bytes.start as isize + delta) as usize;
let new_text: String = buffer.text_for_range(edit.new_bytes.clone()).collect();
old_buffer.edit(
Some(old_start..old_start + edit.deleted_bytes()),
new_text,
cx,
);
delta += edit.delta();
}
assert_eq!(old_buffer.text(), buffer.text());
}
buffer
});
}
#[gpui::test]
fn test_line_len(cx: &mut gpui::MutableAppContext) {
cx.add_model(|cx| {
let mut buffer = Buffer::new(0, "", cx);
buffer.edit(vec![0..0], "abcd\nefg\nhij", cx);
buffer.edit(vec![12..12], "kl\nmno", cx);
buffer.edit(vec![18..18], "\npqrs\n", cx);
buffer.edit(vec![18..21], "\nPQ", cx);
assert_eq!(buffer.line_len(0), 4);
assert_eq!(buffer.line_len(1), 3);
assert_eq!(buffer.line_len(2), 5);
assert_eq!(buffer.line_len(3), 3);
assert_eq!(buffer.line_len(4), 4);
assert_eq!(buffer.line_len(5), 0);
buffer
});
}
#[gpui::test]
fn test_text_summary_for_range(cx: &mut gpui::MutableAppContext) {
cx.add_model(|cx| {
let buffer = Buffer::new(0, "ab\nefg\nhklm\nnopqrs\ntuvwxyz", cx);
assert_eq!(
buffer.text_summary_for_range(1..3),
TextSummary {
bytes: 2,
lines: Point::new(1, 0),
first_line_chars: 1,
last_line_chars: 0,
longest_row: 0,
longest_row_chars: 1,
}
);
assert_eq!(
buffer.text_summary_for_range(1..12),
TextSummary {
bytes: 11,
lines: Point::new(3, 0),
first_line_chars: 1,
last_line_chars: 0,
longest_row: 2,
longest_row_chars: 4,
}
);
assert_eq!(
buffer.text_summary_for_range(0..20),
TextSummary {
bytes: 20,
lines: Point::new(4, 1),
first_line_chars: 2,
last_line_chars: 1,
longest_row: 3,
longest_row_chars: 6,
}
);
assert_eq!(
buffer.text_summary_for_range(0..22),
TextSummary {
bytes: 22,
lines: Point::new(4, 3),
first_line_chars: 2,
last_line_chars: 3,
longest_row: 3,
longest_row_chars: 6,
}
);
assert_eq!(
buffer.text_summary_for_range(7..22),
TextSummary {
bytes: 15,
lines: Point::new(2, 3),
first_line_chars: 4,
last_line_chars: 3,
longest_row: 1,
longest_row_chars: 6,
}
);
buffer
});
}
#[gpui::test]
fn test_chars_at(cx: &mut gpui::MutableAppContext) {
cx.add_model(|cx| {
let mut buffer = Buffer::new(0, "", cx);
buffer.edit(vec![0..0], "abcd\nefgh\nij", cx);
buffer.edit(vec![12..12], "kl\nmno", cx);
buffer.edit(vec![18..18], "\npqrs", cx);
buffer.edit(vec![18..21], "\nPQ", cx);
let chars = buffer.chars_at(Point::new(0, 0));
assert_eq!(chars.collect::<String>(), "abcd\nefgh\nijkl\nmno\nPQrs");
let chars = buffer.chars_at(Point::new(1, 0));
assert_eq!(chars.collect::<String>(), "efgh\nijkl\nmno\nPQrs");
let chars = buffer.chars_at(Point::new(2, 0));
assert_eq!(chars.collect::<String>(), "ijkl\nmno\nPQrs");
let chars = buffer.chars_at(Point::new(3, 0));
assert_eq!(chars.collect::<String>(), "mno\nPQrs");
let chars = buffer.chars_at(Point::new(4, 0));
assert_eq!(chars.collect::<String>(), "PQrs");
// Regression test:
let mut buffer = Buffer::new(0, "", cx);
buffer.edit(vec![0..0], "[workspace]\nmembers = [\n \"xray_core\",\n \"xray_server\",\n \"xray_cli\",\n \"xray_wasm\",\n]\n", cx);
buffer.edit(vec![60..60], "\n", cx);
let chars = buffer.chars_at(Point::new(6, 0));
assert_eq!(chars.collect::<String>(), " \"xray_wasm\",\n]\n");
buffer
});
}
#[gpui::test]
fn test_anchors(cx: &mut gpui::MutableAppContext) {
cx.add_model(|cx| {
let mut buffer = Buffer::new(0, "", cx);
buffer.edit(vec![0..0], "abc", cx);
let left_anchor = buffer.anchor_before(2);
let right_anchor = buffer.anchor_after(2);
buffer.edit(vec![1..1], "def\n", cx);
assert_eq!(buffer.text(), "adef\nbc");
assert_eq!(left_anchor.to_offset(&buffer), 6);
assert_eq!(right_anchor.to_offset(&buffer), 6);
assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 });
assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 1 });
buffer.edit(vec![2..3], "", cx);
assert_eq!(buffer.text(), "adf\nbc");
assert_eq!(left_anchor.to_offset(&buffer), 5);
assert_eq!(right_anchor.to_offset(&buffer), 5);
assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 });
assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 1 });
buffer.edit(vec![5..5], "ghi\n", cx);
assert_eq!(buffer.text(), "adf\nbghi\nc");
assert_eq!(left_anchor.to_offset(&buffer), 5);
assert_eq!(right_anchor.to_offset(&buffer), 9);
assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 });
assert_eq!(right_anchor.to_point(&buffer), Point { row: 2, column: 0 });
buffer.edit(vec![7..9], "", cx);
assert_eq!(buffer.text(), "adf\nbghc");
assert_eq!(left_anchor.to_offset(&buffer), 5);
assert_eq!(right_anchor.to_offset(&buffer), 7);
assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 },);
assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 3 });
// Ensure anchoring to a point is equivalent to anchoring to an offset.
assert_eq!(
buffer.anchor_before(Point { row: 0, column: 0 }),
buffer.anchor_before(0)
);
assert_eq!(
buffer.anchor_before(Point { row: 0, column: 1 }),
buffer.anchor_before(1)
);
assert_eq!(
buffer.anchor_before(Point { row: 0, column: 2 }),
buffer.anchor_before(2)
);
assert_eq!(
buffer.anchor_before(Point { row: 0, column: 3 }),
buffer.anchor_before(3)
);
assert_eq!(
buffer.anchor_before(Point { row: 1, column: 0 }),
buffer.anchor_before(4)
);
assert_eq!(
buffer.anchor_before(Point { row: 1, column: 1 }),
buffer.anchor_before(5)
);
assert_eq!(
buffer.anchor_before(Point { row: 1, column: 2 }),
buffer.anchor_before(6)
);
assert_eq!(
buffer.anchor_before(Point { row: 1, column: 3 }),
buffer.anchor_before(7)
);
assert_eq!(
buffer.anchor_before(Point { row: 1, column: 4 }),
buffer.anchor_before(8)
);
// Comparison between anchors.
let anchor_at_offset_0 = buffer.anchor_before(0);
let anchor_at_offset_1 = buffer.anchor_before(1);
let anchor_at_offset_2 = buffer.anchor_before(2);
assert_eq!(
anchor_at_offset_0
.cmp(&anchor_at_offset_0, &buffer)
.unwrap(),
Ordering::Equal
);
assert_eq!(
anchor_at_offset_1
.cmp(&anchor_at_offset_1, &buffer)
.unwrap(),
Ordering::Equal
);
assert_eq!(
anchor_at_offset_2
.cmp(&anchor_at_offset_2, &buffer)
.unwrap(),
Ordering::Equal
);
assert_eq!(
anchor_at_offset_0
.cmp(&anchor_at_offset_1, &buffer)
.unwrap(),
Ordering::Less
);
assert_eq!(
anchor_at_offset_1
.cmp(&anchor_at_offset_2, &buffer)
.unwrap(),
Ordering::Less
);
assert_eq!(
anchor_at_offset_0
.cmp(&anchor_at_offset_2, &buffer)
.unwrap(),
Ordering::Less
);
assert_eq!(
anchor_at_offset_1
.cmp(&anchor_at_offset_0, &buffer)
.unwrap(),
Ordering::Greater
);
assert_eq!(
anchor_at_offset_2
.cmp(&anchor_at_offset_1, &buffer)
.unwrap(),
Ordering::Greater
);
assert_eq!(
anchor_at_offset_2
.cmp(&anchor_at_offset_0, &buffer)
.unwrap(),
Ordering::Greater
);
buffer
});
}
#[gpui::test]
fn test_anchors_at_start_and_end(cx: &mut gpui::MutableAppContext) {
cx.add_model(|cx| {
let mut buffer = Buffer::new(0, "", cx);
let before_start_anchor = buffer.anchor_before(0);
let after_end_anchor = buffer.anchor_after(0);
buffer.edit(vec![0..0], "abc", cx);
assert_eq!(buffer.text(), "abc");
assert_eq!(before_start_anchor.to_offset(&buffer), 0);
assert_eq!(after_end_anchor.to_offset(&buffer), 3);
let after_start_anchor = buffer.anchor_after(0);
let before_end_anchor = buffer.anchor_before(3);
buffer.edit(vec![3..3], "def", cx);
buffer.edit(vec![0..0], "ghi", cx);
assert_eq!(buffer.text(), "ghiabcdef");
assert_eq!(before_start_anchor.to_offset(&buffer), 0);
assert_eq!(after_start_anchor.to_offset(&buffer), 3);
assert_eq!(before_end_anchor.to_offset(&buffer), 6);
assert_eq!(after_end_anchor.to_offset(&buffer), 9);
buffer
});
}
#[gpui::test]
async fn test_apply_diff(mut cx: gpui::TestAppContext) {
let text = "a\nbb\nccc\ndddd\neeeee\nffffff\n";
let buffer = cx.add_model(|cx| Buffer::new(0, text, cx));
let text = "a\nccc\ndddd\nffffff\n";
let diff = buffer.read_with(&cx, |b, cx| b.diff(text.into(), cx)).await;
buffer.update(&mut cx, |b, cx| b.apply_diff(diff, cx));
cx.read(|cx| assert_eq!(buffer.read(cx).text(), text));
let text = "a\n1\n\nccc\ndd2dd\nffffff\n";
let diff = buffer.read_with(&cx, |b, cx| b.diff(text.into(), cx)).await;
buffer.update(&mut cx, |b, cx| b.apply_diff(diff, cx));
cx.read(|cx| assert_eq!(buffer.read(cx).text(), text));
}
#[gpui::test]
fn test_undo_redo(cx: &mut gpui::MutableAppContext) {
cx.add_model(|cx| {
let mut buffer = Buffer::new(0, "1234", cx);
// Set group interval to zero so as to not group edits in the undo stack.
buffer.history.group_interval = Duration::from_secs(0);
buffer.edit(vec![1..1], "abx", cx);
buffer.edit(vec![3..4], "yzef", cx);
buffer.edit(vec![3..5], "cd", cx);
assert_eq!(buffer.text(), "1abcdef234");
let transactions = buffer.history.undo_stack.clone();
assert_eq!(transactions.len(), 3);
buffer.undo_or_redo(transactions[0].clone(), cx).unwrap();
assert_eq!(buffer.text(), "1cdef234");
buffer.undo_or_redo(transactions[0].clone(), cx).unwrap();
assert_eq!(buffer.text(), "1abcdef234");
buffer.undo_or_redo(transactions[1].clone(), cx).unwrap();
assert_eq!(buffer.text(), "1abcdx234");
buffer.undo_or_redo(transactions[2].clone(), cx).unwrap();
assert_eq!(buffer.text(), "1abx234");
buffer.undo_or_redo(transactions[1].clone(), cx).unwrap();
assert_eq!(buffer.text(), "1abyzef234");
buffer.undo_or_redo(transactions[2].clone(), cx).unwrap();
assert_eq!(buffer.text(), "1abcdef234");
buffer.undo_or_redo(transactions[2].clone(), cx).unwrap();
assert_eq!(buffer.text(), "1abyzef234");
buffer.undo_or_redo(transactions[0].clone(), cx).unwrap();
assert_eq!(buffer.text(), "1yzef234");
buffer.undo_or_redo(transactions[1].clone(), cx).unwrap();
assert_eq!(buffer.text(), "1234");
buffer
});
}
#[gpui::test]
fn test_history(cx: &mut gpui::MutableAppContext) {
cx.add_model(|cx| {
let mut now = Instant::now();
let mut buffer = Buffer::new(0, "123456", cx);
let set_id =
buffer.add_selection_set(buffer.selections_from_ranges(vec![4..4]).unwrap(), cx);
buffer.start_transaction_at(Some(set_id), now).unwrap();
buffer.edit(vec![2..4], "cd", cx);
buffer.end_transaction_at(Some(set_id), now, cx).unwrap();
assert_eq!(buffer.text(), "12cd56");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![4..4]);
buffer.start_transaction_at(Some(set_id), now).unwrap();
buffer
.update_selection_set(
set_id,
buffer.selections_from_ranges(vec![1..3]).unwrap(),
cx,
)
.unwrap();
buffer.edit(vec![4..5], "e", cx);
buffer.end_transaction_at(Some(set_id), now, cx).unwrap();
assert_eq!(buffer.text(), "12cde6");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![1..3]);
now += buffer.history.group_interval + Duration::from_millis(1);
buffer.start_transaction_at(Some(set_id), now).unwrap();
buffer
.update_selection_set(
set_id,
buffer.selections_from_ranges(vec![2..2]).unwrap(),
cx,
)
.unwrap();
buffer.edit(vec![0..1], "a", cx);
buffer.edit(vec![1..1], "b", cx);
buffer.end_transaction_at(Some(set_id), now, cx).unwrap();
assert_eq!(buffer.text(), "ab2cde6");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![3..3]);
// Last transaction happened past the group interval, undo it on its
// own.
buffer.undo(cx);
assert_eq!(buffer.text(), "12cde6");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![1..3]);
// First two transactions happened within the group interval, undo them
// together.
buffer.undo(cx);
assert_eq!(buffer.text(), "123456");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![4..4]);
// Redo the first two transactions together.
buffer.redo(cx);
assert_eq!(buffer.text(), "12cde6");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![1..3]);
// Redo the last transaction on its own.
buffer.redo(cx);
assert_eq!(buffer.text(), "ab2cde6");
assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![3..3]);
buffer.start_transaction_at(None, now).unwrap();
buffer.end_transaction_at(None, now, cx).unwrap();
buffer.undo(cx);
assert_eq!(buffer.text(), "12cde6");
buffer
});
}
#[gpui::test]
fn test_concurrent_edits(cx: &mut gpui::MutableAppContext) {
let text = "abcdef";
let buffer1 = cx.add_model(|cx| Buffer::new(1, text, cx));
let buffer2 = cx.add_model(|cx| Buffer::new(2, text, cx));
let buffer3 = cx.add_model(|cx| Buffer::new(3, text, cx));
let buf1_op = buffer1.update(cx, |buffer, cx| {
buffer.edit(vec![1..2], "12", cx);
assert_eq!(buffer.text(), "a12cdef");
buffer.operations.last().unwrap().clone()
});
let buf2_op = buffer2.update(cx, |buffer, cx| {
buffer.edit(vec![3..4], "34", cx);
assert_eq!(buffer.text(), "abc34ef");
buffer.operations.last().unwrap().clone()
});
let buf3_op = buffer3.update(cx, |buffer, cx| {
buffer.edit(vec![5..6], "56", cx);
assert_eq!(buffer.text(), "abcde56");
buffer.operations.last().unwrap().clone()
});
buffer1.update(cx, |buffer, _| {
buffer.apply_op(buf2_op.clone()).unwrap();
buffer.apply_op(buf3_op.clone()).unwrap();
});
buffer2.update(cx, |buffer, _| {
buffer.apply_op(buf1_op.clone()).unwrap();
buffer.apply_op(buf3_op.clone()).unwrap();
});
buffer3.update(cx, |buffer, _| {
buffer.apply_op(buf1_op.clone()).unwrap();
buffer.apply_op(buf2_op.clone()).unwrap();
});
assert_eq!(buffer1.read(cx).text(), "a12c34e56");
assert_eq!(buffer2.read(cx).text(), "a12c34e56");
assert_eq!(buffer3.read(cx).text(), "a12c34e56");
}
#[gpui::test(iterations = 100)]
fn test_random_concurrent_edits(cx: &mut gpui::MutableAppContext, mut rng: StdRng) {
let peers = env::var("PEERS")
.map(|i| i.parse().expect("invalid `PEERS` variable"))
.unwrap_or(5);
let operations = env::var("OPERATIONS")
.map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
.unwrap_or(10);
let base_text_len = rng.gen_range(0..10);
let base_text = RandomCharIter::new(&mut rng)
.take(base_text_len)
.collect::<String>();
let mut replica_ids = Vec::new();
let mut buffers = Vec::new();
let mut network = Network::new(rng.clone());
for i in 0..peers {
let buffer = cx.add_model(|cx| {
let mut buf = Buffer::new(i as ReplicaId, base_text.as_str(), cx);
buf.history.group_interval = Duration::from_millis(rng.gen_range(0..=200));
buf
});
buffers.push(buffer);
replica_ids.push(i as u16);
network.add_peer(i as u16);
}
log::info!("initial text: {:?}", base_text);
let mut mutation_count = operations;
loop {
let replica_index = rng.gen_range(0..peers);
let replica_id = replica_ids[replica_index];
buffers[replica_index].update(cx, |buffer, cx| match rng.gen_range(0..=100) {
0..=50 if mutation_count != 0 => {
buffer.randomly_mutate(&mut rng, cx);
network.broadcast(buffer.replica_id, mem::take(&mut buffer.operations));
log::info!("buffer {} text: {:?}", buffer.replica_id, buffer.text());
mutation_count -= 1;
}
51..=70 if mutation_count != 0 => {
buffer.randomly_undo_redo(&mut rng, cx);
network.broadcast(buffer.replica_id, mem::take(&mut buffer.operations));
mutation_count -= 1;
}
71..=100 if network.has_unreceived(replica_id) => {
let ops = network.receive(replica_id);
if !ops.is_empty() {
log::info!(
"peer {} applying {} ops from the network.",
replica_id,
ops.len()
);
buffer.apply_ops(ops, cx).unwrap();
}
}
_ => {}
});
if mutation_count == 0 && network.is_idle() {
break;
}
}
let first_buffer = buffers[0].read(cx);
for buffer in &buffers[1..] {
let buffer = buffer.read(cx);
assert_eq!(
buffer.text(),
first_buffer.text(),
"Replica {} text != Replica 0 text",
buffer.replica_id
);
assert_eq!(
buffer.selection_sets().collect::<HashMap<_, _>>(),
first_buffer.selection_sets().collect::<HashMap<_, _>>()
);
assert_eq!(
buffer.all_selection_ranges().collect::<HashMap<_, _>>(),
first_buffer
.all_selection_ranges()
.collect::<HashMap<_, _>>()
);
}
}
#[derive(Clone)]
struct Envelope<T: Clone> {
message: T,
sender: ReplicaId,
}
struct Network<T: Clone, R: rand::Rng> {
inboxes: std::collections::BTreeMap<ReplicaId, Vec<Envelope<T>>>,
all_messages: Vec<T>,
rng: R,
}
impl<T: Clone, R: rand::Rng> Network<T, R> {
fn new(rng: R) -> Self {
Network {
inboxes: Default::default(),
all_messages: Vec::new(),
rng,
}
}
fn add_peer(&mut self, id: ReplicaId) {
self.inboxes.insert(id, Vec::new());
}
fn is_idle(&self) -> bool {
self.inboxes.values().all(|i| i.is_empty())
}
fn broadcast(&mut self, sender: ReplicaId, messages: Vec<T>) {
for (replica, inbox) in self.inboxes.iter_mut() {
if *replica != sender {
for message in &messages {
let min_index = inbox
.iter()
.enumerate()
.rev()
.find_map(|(index, envelope)| {
if sender == envelope.sender {
Some(index + 1)
} else {
None
}
})
.unwrap_or(0);
// Insert one or more duplicates of this message *after* the previous
// message delivered by this replica.
for _ in 0..self.rng.gen_range(1..4) {
let insertion_index = self.rng.gen_range(min_index..inbox.len() + 1);
inbox.insert(
insertion_index,
Envelope {
message: message.clone(),
sender,
},
);
}
}
}
}
self.all_messages.extend(messages);
}
fn has_unreceived(&self, receiver: ReplicaId) -> bool {
!self.inboxes[&receiver].is_empty()
}
fn receive(&mut self, receiver: ReplicaId) -> Vec<T> {
let inbox = self.inboxes.get_mut(&receiver).unwrap();
let count = self.rng.gen_range(0..inbox.len() + 1);
inbox
.drain(0..count)
.map(|envelope| envelope.message)
.collect()
}
}

View File

@@ -1,380 +0,0 @@
use crate::*;
use gpui::{ModelHandle, MutableAppContext};
use unindent::Unindent as _;
#[gpui::test]
async fn test_reparse(mut cx: gpui::TestAppContext) {
let buffer = cx.add_model(|cx| {
let text = "fn a() {}".into();
Buffer::from_history(0, History::new(text), None, Some(rust_lang()), cx)
});
// Wait for the initial text to parse
buffer
.condition(&cx, |buffer, _| !buffer.is_parsing())
.await;
assert_eq!(
get_tree_sexp(&buffer, &cx),
concat!(
"(source_file (function_item name: (identifier) ",
"parameters: (parameters) ",
"body: (block)))"
)
);
buffer.update(&mut cx, |buffer, _| {
buffer.set_sync_parse_timeout(Duration::ZERO)
});
// Perform some edits (add parameter and variable reference)
// Parsing doesn't begin until the transaction is complete
buffer.update(&mut cx, |buf, cx| {
buf.start_transaction(None).unwrap();
let offset = buf.text().find(")").unwrap();
buf.edit(vec![offset..offset], "b: C", cx);
assert!(!buf.is_parsing());
let offset = buf.text().find("}").unwrap();
buf.edit(vec![offset..offset], " d; ", cx);
assert!(!buf.is_parsing());
buf.end_transaction(None, cx).unwrap();
assert_eq!(buf.text(), "fn a(b: C) { d; }");
assert!(buf.is_parsing());
});
buffer
.condition(&cx, |buffer, _| !buffer.is_parsing())
.await;
assert_eq!(
get_tree_sexp(&buffer, &cx),
concat!(
"(source_file (function_item name: (identifier) ",
"parameters: (parameters (parameter pattern: (identifier) type: (type_identifier))) ",
"body: (block (identifier))))"
)
);
// Perform a series of edits without waiting for the current parse to complete:
// * turn identifier into a field expression
// * turn field expression into a method call
// * add a turbofish to the method call
buffer.update(&mut cx, |buf, cx| {
let offset = buf.text().find(";").unwrap();
buf.edit(vec![offset..offset], ".e", cx);
assert_eq!(buf.text(), "fn a(b: C) { d.e; }");
assert!(buf.is_parsing());
});
buffer.update(&mut cx, |buf, cx| {
let offset = buf.text().find(";").unwrap();
buf.edit(vec![offset..offset], "(f)", cx);
assert_eq!(buf.text(), "fn a(b: C) { d.e(f); }");
assert!(buf.is_parsing());
});
buffer.update(&mut cx, |buf, cx| {
let offset = buf.text().find("(f)").unwrap();
buf.edit(vec![offset..offset], "::<G>", cx);
assert_eq!(buf.text(), "fn a(b: C) { d.e::<G>(f); }");
assert!(buf.is_parsing());
});
buffer
.condition(&cx, |buffer, _| !buffer.is_parsing())
.await;
assert_eq!(
get_tree_sexp(&buffer, &cx),
concat!(
"(source_file (function_item name: (identifier) ",
"parameters: (parameters (parameter pattern: (identifier) type: (type_identifier))) ",
"body: (block (call_expression ",
"function: (generic_function ",
"function: (field_expression value: (identifier) field: (field_identifier)) ",
"type_arguments: (type_arguments (type_identifier))) ",
"arguments: (arguments (identifier))))))",
)
);
buffer.update(&mut cx, |buf, cx| {
buf.undo(cx);
assert_eq!(buf.text(), "fn a() {}");
assert!(buf.is_parsing());
});
buffer
.condition(&cx, |buffer, _| !buffer.is_parsing())
.await;
assert_eq!(
get_tree_sexp(&buffer, &cx),
concat!(
"(source_file (function_item name: (identifier) ",
"parameters: (parameters) ",
"body: (block)))"
)
);
buffer.update(&mut cx, |buf, cx| {
buf.redo(cx);
assert_eq!(buf.text(), "fn a(b: C) { d.e::<G>(f); }");
assert!(buf.is_parsing());
});
buffer
.condition(&cx, |buffer, _| !buffer.is_parsing())
.await;
assert_eq!(
get_tree_sexp(&buffer, &cx),
concat!(
"(source_file (function_item name: (identifier) ",
"parameters: (parameters (parameter pattern: (identifier) type: (type_identifier))) ",
"body: (block (call_expression ",
"function: (generic_function ",
"function: (field_expression value: (identifier) field: (field_identifier)) ",
"type_arguments: (type_arguments (type_identifier))) ",
"arguments: (arguments (identifier))))))",
)
);
fn get_tree_sexp(buffer: &ModelHandle<Buffer>, cx: &gpui::TestAppContext) -> String {
buffer.read_with(cx, |buffer, _| {
buffer.syntax_tree().unwrap().root_node().to_sexp()
})
}
}
#[gpui::test]
fn test_enclosing_bracket_ranges(cx: &mut MutableAppContext) {
let buffer = cx.add_model(|cx| {
let text = "
mod x {
mod y {
}
}
"
.unindent()
.into();
Buffer::from_history(0, History::new(text), None, Some(rust_lang()), cx)
});
let buffer = buffer.read(cx);
assert_eq!(
buffer.enclosing_bracket_point_ranges(Point::new(1, 6)..Point::new(1, 6)),
Some((
Point::new(0, 6)..Point::new(0, 7),
Point::new(4, 0)..Point::new(4, 1)
))
);
assert_eq!(
buffer.enclosing_bracket_point_ranges(Point::new(1, 10)..Point::new(1, 10)),
Some((
Point::new(1, 10)..Point::new(1, 11),
Point::new(3, 4)..Point::new(3, 5)
))
);
assert_eq!(
buffer.enclosing_bracket_point_ranges(Point::new(3, 5)..Point::new(3, 5)),
Some((
Point::new(1, 10)..Point::new(1, 11),
Point::new(3, 4)..Point::new(3, 5)
))
);
}
#[gpui::test]
fn test_edit_with_autoindent(cx: &mut MutableAppContext) {
cx.add_model(|cx| {
let text = "fn a() {}".into();
let mut buffer = Buffer::from_history(0, History::new(text), None, Some(rust_lang()), cx);
buffer.edit_with_autoindent([8..8], "\n\n", cx);
assert_eq!(buffer.text(), "fn a() {\n \n}");
buffer.edit_with_autoindent([Point::new(1, 4)..Point::new(1, 4)], "b()\n", cx);
assert_eq!(buffer.text(), "fn a() {\n b()\n \n}");
buffer.edit_with_autoindent([Point::new(2, 4)..Point::new(2, 4)], ".c", cx);
assert_eq!(buffer.text(), "fn a() {\n b()\n .c\n}");
buffer
});
}
#[gpui::test]
fn test_autoindent_moves_selections(cx: &mut MutableAppContext) {
cx.add_model(|cx| {
let text = History::new("fn a() {}".into());
let mut buffer = Buffer::from_history(0, text, None, Some(rust_lang()), cx);
let selection_set_id = buffer.add_selection_set(Vec::new(), cx);
buffer.start_transaction(Some(selection_set_id)).unwrap();
buffer.edit_with_autoindent([5..5, 9..9], "\n\n", cx);
buffer
.update_selection_set(
selection_set_id,
vec![
Selection {
id: 0,
start: buffer.anchor_before(Point::new(1, 0)),
end: buffer.anchor_before(Point::new(1, 0)),
reversed: false,
goal: SelectionGoal::None,
},
Selection {
id: 1,
start: buffer.anchor_before(Point::new(4, 0)),
end: buffer.anchor_before(Point::new(4, 0)),
reversed: false,
goal: SelectionGoal::None,
},
],
cx,
)
.unwrap();
assert_eq!(buffer.text(), "fn a(\n\n) {}\n\n");
// Ending the transaction runs the auto-indent. The selection
// at the start of the auto-indented row is pushed to the right.
buffer.end_transaction(Some(selection_set_id), cx).unwrap();
assert_eq!(buffer.text(), "fn a(\n \n) {}\n\n");
let selection_ranges = buffer
.selection_set(selection_set_id)
.unwrap()
.selections
.iter()
.map(|selection| selection.point_range(&buffer))
.collect::<Vec<_>>();
assert_eq!(selection_ranges[0], empty(Point::new(1, 4)));
assert_eq!(selection_ranges[1], empty(Point::new(4, 0)));
buffer
});
}
#[gpui::test]
fn test_autoindent_does_not_adjust_lines_with_unchanged_suggestion(cx: &mut MutableAppContext) {
cx.add_model(|cx| {
let text = "
fn a() {
c;
d;
}
"
.unindent()
.into();
let mut buffer = Buffer::from_history(0, History::new(text), None, Some(rust_lang()), cx);
// Lines 2 and 3 don't match the indentation suggestion. When editing these lines,
// their indentation is not adjusted.
buffer.edit_with_autoindent([empty(Point::new(1, 1)), empty(Point::new(2, 1))], "()", cx);
assert_eq!(
buffer.text(),
"
fn a() {
c();
d();
}
"
.unindent()
);
// When appending new content after these lines, the indentation is based on the
// preceding lines' actual indentation.
buffer.edit_with_autoindent(
[empty(Point::new(1, 1)), empty(Point::new(2, 1))],
"\n.f\n.g",
cx,
);
assert_eq!(
buffer.text(),
"
fn a() {
c
.f
.g();
d
.f
.g();
}
"
.unindent()
);
buffer
});
}
#[gpui::test]
fn test_autoindent_adjusts_lines_when_only_text_changes(cx: &mut MutableAppContext) {
cx.add_model(|cx| {
let text = History::new(
"
fn a() {}
"
.unindent()
.into(),
);
let mut buffer = Buffer::from_history(0, text, None, Some(rust_lang()), cx);
buffer.edit_with_autoindent([5..5], "\nb", cx);
assert_eq!(
buffer.text(),
"
fn a(
b) {}
"
.unindent()
);
// The indentation suggestion changed because `@end` node (a close paren)
// is now at the beginning of the line.
buffer.edit_with_autoindent([Point::new(1, 4)..Point::new(1, 5)], "", cx);
assert_eq!(
buffer.text(),
"
fn a(
) {}
"
.unindent()
);
buffer
});
}
#[test]
fn test_contiguous_ranges() {
assert_eq!(
contiguous_ranges([1, 2, 3, 5, 6, 9, 10, 11, 12], 100).collect::<Vec<_>>(),
&[1..4, 5..7, 9..13]
);
// Respects the `max_len` parameter
assert_eq!(
contiguous_ranges([2, 3, 4, 5, 6, 7, 8, 9, 23, 24, 25, 26, 30, 31], 3).collect::<Vec<_>>(),
&[2..5, 5..8, 8..10, 23..26, 26..27, 30..32],
);
}
fn rust_lang() -> Arc<Language> {
Arc::new(
Language::new(
LanguageConfig {
name: "Rust".to_string(),
path_suffixes: vec!["rs".to_string()],
..Default::default()
},
tree_sitter_rust::language(),
)
.with_indents_query(
r#"
(call_expression) @indent
(field_expression) @indent
(_ "(" ")" @end) @indent
(_ "{" "}" @end) @indent
"#,
)
.unwrap()
.with_brackets_query(r#" ("{" @open "}" @close) "#)
.unwrap(),
)
}
fn empty(point: Point) -> Range<Point> {
point..point
}

View File

@@ -3,6 +3,9 @@ name = "chat_panel"
version = "0.1.0"
edition = "2018"
[lib]
path = "src/chat_panel.rs"
[dependencies]
client = { path = "../client" }
editor = { path = "../editor" }

View File

@@ -56,13 +56,14 @@ impl ChatPanel {
4,
{
let settings = settings.clone();
move |_| {
Arc::new(move |_| {
let settings = settings.borrow();
EditorSettings {
tab_size: settings.tab_size,
style: settings.theme.chat_panel.input_editor.as_editor(),
soft_wrap: editor::SoftWrap::EditorWidth,
}
}
})
},
cx,
)
@@ -95,7 +96,7 @@ impl ChatPanel {
});
let mut message_list = ListState::new(0, Orientation::Bottom, 1000., {
let this = cx.handle().downgrade();
let this = cx.weak_handle();
move |ix, cx| {
let this = this.upgrade(cx).unwrap().read(cx);
let message = this.active_channel.as_ref().unwrap().0.read(cx).message(ix);
@@ -232,7 +233,7 @@ impl ChatPanel {
Empty::new().boxed()
};
Expanded::new(1., messages).boxed()
Flexible::new(1., true, messages).boxed()
}
fn render_message(&self, message: &ChannelMessage) -> ElementBox {

View File

@@ -3,8 +3,11 @@ name = "client"
version = "0.1.0"
edition = "2018"
[lib]
path = "src/client.rs"
[features]
test-support = ["rpc/test-support"]
test-support = ["gpui/test-support", "rpc/test-support"]
[dependencies]
gpui = { path = "../gpui" }
@@ -13,7 +16,7 @@ rpc = { path = "../rpc" }
sum_tree = { path = "../sum_tree" }
anyhow = "1.0.38"
async-recursion = "0.3"
async-tungstenite = { version = "0.14", features = ["async-tls"] }
async-tungstenite = { version = "0.16", features = ["async-tls"] }
futures = "0.3"
image = "0.23"
lazy_static = "1.4.0"
@@ -26,3 +29,7 @@ surf = "2.2"
thiserror = "1.0.29"
time = "0.3"
tiny_http = "0.8"
[dev-dependencies]
gpui = { path = "../gpui", features = ["test-support"] }
rpc = { path = "../rpc", features = ["test-support"] }

View File

@@ -599,8 +599,8 @@ mod tests {
#[gpui::test]
async fn test_channel_messages(mut cx: TestAppContext) {
let user_id = 5;
let mut client = Client::new();
let http_client = FakeHttpClient::new(|_| async move { Ok(Response::new(404)) });
let mut client = Client::new(http_client.clone());
let server = FakeServer::for_client(user_id, &mut client, &cx).await;
let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));

View File

@@ -11,10 +11,12 @@ use async_tungstenite::tungstenite::{
error::Error as WebsocketError,
http::{Request, StatusCode},
};
use futures::StreamExt;
use gpui::{action, AsyncAppContext, Entity, ModelContext, MutableAppContext, Task};
use http::HttpClient;
use lazy_static::lazy_static;
use parking_lot::RwLock;
use postage::{prelude::Stream, watch};
use postage::watch;
use rand::prelude::*;
use rpc::proto::{AnyTypedEnvelope, EntityMessage, EnvelopedMessage, RequestMessage};
use std::{
@@ -26,7 +28,7 @@ use std::{
sync::{Arc, Weak},
time::{Duration, Instant},
};
use surf::Url;
use surf::{http::Method, Url};
use thiserror::Error;
use util::{ResultExt, TryFutureExt};
@@ -36,7 +38,7 @@ pub use user::*;
lazy_static! {
static ref ZED_SERVER_URL: String =
std::env::var("ZED_SERVER_URL").unwrap_or("https://zed.dev:443".to_string());
std::env::var("ZED_SERVER_URL").unwrap_or("https://zed.dev".to_string());
static ref IMPERSONATE_LOGIN: Option<String> = std::env::var("ZED_IMPERSONATE")
.ok()
.and_then(|s| if s.is_empty() { None } else { Some(s) });
@@ -54,6 +56,7 @@ pub fn init(rpc: Arc<Client>, cx: &mut MutableAppContext) {
pub struct Client {
peer: Arc<Peer>,
http: Arc<dyn HttpClient>,
state: RwLock<ClientState>,
authenticate:
Option<Box<dyn 'static + Send + Sync + Fn(&AsyncAppContext) -> Task<Result<Credentials>>>>,
@@ -122,14 +125,14 @@ struct ClientState {
status: (watch::Sender<Status>, watch::Receiver<Status>),
entity_id_extractors: HashMap<TypeId, Box<dyn Send + Sync + Fn(&dyn AnyTypedEnvelope) -> u64>>,
model_handlers: HashMap<
(TypeId, u64),
Box<dyn Send + Sync + FnMut(Box<dyn AnyTypedEnvelope>, &mut AsyncAppContext)>,
(TypeId, Option<u64>),
Option<Box<dyn Send + Sync + FnMut(Box<dyn AnyTypedEnvelope>, &mut AsyncAppContext)>>,
>,
_maintain_connection: Option<Task<()>>,
heartbeat_interval: Duration,
}
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Credentials {
pub user_id: u64,
pub access_token: String,
@@ -150,28 +153,23 @@ impl Default for ClientState {
pub struct Subscription {
client: Weak<Client>,
id: (TypeId, u64),
id: (TypeId, Option<u64>),
}
impl Drop for Subscription {
fn drop(&mut self) {
if let Some(client) = self.client.upgrade() {
drop(
client
.state
.write()
.model_handlers
.remove(&self.id)
.unwrap(),
);
let mut state = client.state.write();
let _ = state.model_handlers.remove(&self.id).unwrap();
}
}
}
impl Client {
pub fn new() -> Arc<Self> {
pub fn new(http: Arc<dyn HttpClient>) -> Arc<Self> {
Arc::new(Self {
peer: Peer::new(),
http,
state: Default::default(),
authenticate: None,
establish_connection: None,
@@ -269,20 +267,13 @@ impl Client {
+ Sync
+ FnMut(&mut M, TypedEnvelope<T>, Arc<Self>, &mut ModelContext<M>) -> Result<()>,
{
let subscription_id = (TypeId::of::<T>(), Default::default());
let subscription_id = (TypeId::of::<T>(), None);
let client = self.clone();
let mut state = self.state.write();
let model = cx.handle().downgrade();
let prev_extractor = state
.entity_id_extractors
.insert(subscription_id.0, Box::new(|_| Default::default()));
if prev_extractor.is_some() {
panic!("registered a handler for the same entity twice")
}
state.model_handlers.insert(
let model = cx.weak_handle();
let prev_handler = state.model_handlers.insert(
subscription_id,
Box::new(move |envelope, cx| {
Some(Box::new(move |envelope, cx| {
if let Some(model) = model.upgrade(cx) {
let envelope = envelope.into_any().downcast::<TypedEnvelope<T>>().unwrap();
model.update(cx, |model, cx| {
@@ -291,8 +282,11 @@ impl Client {
}
});
}
}),
})),
);
if prev_handler.is_some() {
panic!("registered handler for the same message twice");
}
Subscription {
client: Arc::downgrade(self),
@@ -314,10 +308,10 @@ impl Client {
+ Sync
+ FnMut(&mut M, TypedEnvelope<T>, Arc<Self>, &mut ModelContext<M>) -> Result<()>,
{
let subscription_id = (TypeId::of::<T>(), remote_id);
let subscription_id = (TypeId::of::<T>(), Some(remote_id));
let client = self.clone();
let mut state = self.state.write();
let model = cx.handle().downgrade();
let model = cx.weak_handle();
state
.entity_id_extractors
.entry(subscription_id.0)
@@ -332,7 +326,7 @@ impl Client {
});
let prev_handler = state.model_handlers.insert(
subscription_id,
Box::new(move |envelope, cx| {
Some(Box::new(move |envelope, cx| {
if let Some(model) = model.upgrade(cx) {
let envelope = envelope.into_any().downcast::<TypedEnvelope<T>>().unwrap();
model.update(cx, |model, cx| {
@@ -341,7 +335,7 @@ impl Client {
}
});
}
}),
})),
);
if prev_handler.is_some() {
panic!("registered a handler for the same entity twice")
@@ -353,6 +347,10 @@ impl Client {
}
}
pub fn has_keychain_credentials(&self, cx: &AsyncAppContext) -> bool {
read_credentials_from_keychain(cx).is_some()
}
#[async_recursion(?Send)]
pub async fn authenticate_and_connect(
self: &Arc<Self>,
@@ -403,7 +401,6 @@ impl Client {
match self.establish_connection(&credentials, cx).await {
Ok(conn) => {
log::info!("connected to rpc address {}", *ZED_SERVER_URL);
self.state.write().credentials = Some(credentials.clone());
if !used_keychain && IMPERSONATE_LOGIN.is_none() {
write_credentials_to_keychain(&credentials, cx).log_err();
@@ -440,29 +437,29 @@ impl Client {
let mut cx = cx.clone();
let this = self.clone();
async move {
while let Some(message) = incoming.recv().await {
while let Some(message) = incoming.next().await {
let mut state = this.state.write();
if let Some(extract_entity_id) =
let payload_type_id = message.payload_type_id();
let entity_id = if let Some(extract_entity_id) =
state.entity_id_extractors.get(&message.payload_type_id())
{
let payload_type_id = message.payload_type_id();
let entity_id = (extract_entity_id)(message.as_ref());
let handler_key = (payload_type_id, entity_id);
if let Some(mut handler) = state.model_handlers.remove(&handler_key) {
drop(state); // Avoid deadlocks if the handler interacts with rpc::Client
let start_time = Instant::now();
log::info!("RPC client message {}", message.payload_type_name());
(handler)(message, &mut cx);
log::info!(
"RPC message handled. duration:{:?}",
start_time.elapsed()
);
this.state
.write()
.model_handlers
.insert(handler_key, handler);
} else {
log::info!("unhandled message {}", message.payload_type_name());
Some((extract_entity_id)(message.as_ref()))
} else {
None
};
let handler_key = (payload_type_id, entity_id);
if let Some(handler) = state.model_handlers.get_mut(&handler_key) {
let mut handler = handler.take().unwrap();
drop(state); // Avoid deadlocks if the handler interacts with rpc::Client
let start_time = Instant::now();
log::info!("RPC client message {}", message.payload_type_name());
(handler)(message, &mut cx);
log::info!("RPC message handled. duration:{:?}", start_time.elapsed());
let mut state = this.state.write();
if state.model_handlers.contains_key(&handler_key) {
state.model_handlers.insert(handler_key, Some(handler));
}
} else {
log::info!("unhandled message {}", message.payload_type_name());
@@ -521,20 +518,57 @@ impl Client {
format!("{} {}", credentials.user_id, credentials.access_token),
)
.header("X-Zed-Protocol-Version", rpc::PROTOCOL_VERSION);
let http = self.http.clone();
cx.background().spawn(async move {
if let Some(host) = ZED_SERVER_URL.strip_prefix("https://") {
let stream = smol::net::TcpStream::connect(host).await?;
let request = request.uri(format!("wss://{}/rpc", host)).body(())?;
let (stream, _) =
async_tungstenite::async_tls::client_async_tls(request, stream).await?;
Ok(Connection::new(stream))
} else if let Some(host) = ZED_SERVER_URL.strip_prefix("http://") {
let stream = smol::net::TcpStream::connect(host).await?;
let request = request.uri(format!("ws://{}/rpc", host)).body(())?;
let (stream, _) = async_tungstenite::client_async(request, stream).await?;
Ok(Connection::new(stream))
} else {
Err(anyhow!("invalid server url: {}", *ZED_SERVER_URL))?
let mut rpc_url = format!("{}/rpc", *ZED_SERVER_URL);
let rpc_request = surf::Request::new(
Method::Get,
surf::Url::parse(&rpc_url).context("invalid ZED_SERVER_URL")?,
);
let rpc_response = http.send(rpc_request).await?;
if rpc_response.status().is_redirection() {
rpc_url = rpc_response
.header("Location")
.ok_or_else(|| anyhow!("missing location header in /rpc response"))?
.as_str()
.to_string();
}
// Until we switch the zed.dev domain to point to the new Next.js app, there
// will be no redirect required, and the app will connect directly to
// wss://zed.dev/rpc.
else if rpc_response.status() != surf::StatusCode::UpgradeRequired {
Err(anyhow!(
"unexpected /rpc response status {}",
rpc_response.status()
))?
}
let mut rpc_url = surf::Url::parse(&rpc_url).context("invalid rpc url")?;
let rpc_host = rpc_url
.host_str()
.zip(rpc_url.port_or_known_default())
.ok_or_else(|| anyhow!("missing host in rpc url"))?;
let stream = smol::net::TcpStream::connect(rpc_host).await?;
log::info!("connected to rpc endpoint {}", rpc_url);
match rpc_url.scheme() {
"https" => {
rpc_url.set_scheme("wss").unwrap();
let request = request.uri(rpc_url.as_str()).body(())?;
let (stream, _) =
async_tungstenite::async_tls::client_async_tls(request, stream).await?;
Ok(Connection::new(stream))
}
"http" => {
rpc_url.set_scheme("ws").unwrap();
let request = request.uri(rpc_url.as_str()).body(())?;
let (stream, _) = async_tungstenite::client_async(request, stream).await?;
Ok(Connection::new(stream))
}
_ => Err(anyhow!("invalid rpc url: {}", rpc_url))?,
}
})
}
@@ -561,7 +595,7 @@ impl Client {
// Open the Zed sign-in page in the user's browser, with query parameters that indicate
// that the user is signing in from a Zed app running on the same device.
let mut url = format!(
"{}/sign_in?native_app_port={}&native_app_public_key={}",
"{}/native_app_signin?native_app_port={}&native_app_public_key={}",
*ZED_SERVER_URL, port, public_key_string
);
@@ -592,9 +626,16 @@ impl Client {
user_id = Some(value.to_string());
}
}
let post_auth_url =
format!("{}/native_app_signin_succeeded", *ZED_SERVER_URL);
req.respond(
tiny_http::Response::from_string(LOGIN_RESPONSE).with_header(
tiny_http::Header::from_bytes("Content-Type", "text/html").unwrap(),
tiny_http::Response::empty(302).with_header(
tiny_http::Header::from_bytes(
&b"Location"[..],
post_auth_url.as_bytes(),
)
.unwrap(),
),
)
.context("failed to respond to login http request")?;
@@ -621,9 +662,9 @@ impl Client {
})
}
pub async fn disconnect(self: &Arc<Self>, cx: &AsyncAppContext) -> Result<()> {
pub fn disconnect(self: &Arc<Self>, cx: &AsyncAppContext) -> Result<()> {
let conn_id = self.connection_id()?;
self.peer.disconnect(conn_id).await;
self.peer.disconnect(conn_id);
self.set_status(Status::SignedOut, cx);
Ok(())
}
@@ -651,6 +692,14 @@ impl Client {
) -> impl Future<Output = Result<()>> {
self.peer.respond(receipt, response)
}
pub fn respond_with_error<T: RequestMessage>(
&self,
receipt: Receipt<T>,
error: proto::Error,
) -> impl Future<Output = Result<()>> {
self.peer.respond_with_error(receipt, error)
}
}
fn read_credentials_from_keychain(cx: &AsyncAppContext) -> Option<Credentials> {
@@ -694,17 +743,10 @@ pub fn decode_worktree_url(url: &str) -> Option<(u64, String)> {
Some((id, access_token.to_string()))
}
const LOGIN_RESPONSE: &'static str = "
<!DOCTYPE html>
<html>
<script>window.close();</script>
</html>
";
#[cfg(test)]
mod tests {
use super::*;
use crate::test::FakeServer;
use crate::test::{FakeHttpClient, FakeServer};
use gpui::TestAppContext;
#[gpui::test(iterations = 10)]
@@ -712,7 +754,7 @@ mod tests {
cx.foreground().forbid_parking();
let user_id = 5;
let mut client = Client::new();
let mut client = Client::new(FakeHttpClient::with_404_response());
let server = FakeServer::for_client(user_id, &mut client, &cx).await;
cx.foreground().advance_clock(Duration::from_secs(10));
@@ -723,7 +765,7 @@ mod tests {
let ping = server.receive::<proto::Ping>().await.unwrap();
server.respond(ping.receipt(), proto::Ack {}).await;
client.disconnect(&cx.to_async()).await.unwrap();
client.disconnect(&cx.to_async()).unwrap();
assert!(server.receive::<proto::Ping>().await.is_err());
}
@@ -732,27 +774,27 @@ mod tests {
cx.foreground().forbid_parking();
let user_id = 5;
let mut client = Client::new();
let mut client = Client::new(FakeHttpClient::with_404_response());
let server = FakeServer::for_client(user_id, &mut client, &cx).await;
let mut status = client.status();
assert!(matches!(
status.recv().await,
status.next().await,
Some(Status::Connected { .. })
));
assert_eq!(server.auth_count(), 1);
server.forbid_connections();
server.disconnect().await;
while !matches!(status.recv().await, Some(Status::ReconnectionError { .. })) {}
server.disconnect();
while !matches!(status.next().await, Some(Status::ReconnectionError { .. })) {}
server.allow_connections();
cx.foreground().advance_clock(Duration::from_secs(10));
while !matches!(status.recv().await, Some(Status::Connected { .. })) {}
while !matches!(status.next().await, Some(Status::Connected { .. })) {}
assert_eq!(server.auth_count(), 1); // Client reused the cached credentials when reconnecting
server.forbid_connections();
server.disconnect().await;
while !matches!(status.recv().await, Some(Status::ReconnectionError { .. })) {}
server.disconnect();
while !matches!(status.next().await, Some(Status::ReconnectionError { .. })) {}
// Clear cached credentials after authentication fails
server.roll_access_token();
@@ -760,7 +802,7 @@ mod tests {
cx.foreground().advance_clock(Duration::from_secs(10));
assert_eq!(server.auth_count(), 1);
cx.foreground().advance_clock(Duration::from_secs(10));
while !matches!(status.recv().await, Some(Status::Connected { .. })) {}
while !matches!(status.next().await, Some(Status::Connected { .. })) {}
assert_eq!(server.auth_count(), 2); // Client re-authenticated due to an invalid token
}
@@ -774,4 +816,113 @@ mod tests {
);
assert_eq!(decode_worktree_url("not://the-right-format"), None);
}
#[gpui::test]
async fn test_subscribing_to_entity(mut cx: TestAppContext) {
cx.foreground().forbid_parking();
let user_id = 5;
let mut client = Client::new(FakeHttpClient::with_404_response());
let server = FakeServer::for_client(user_id, &mut client, &cx).await;
let model = cx.add_model(|_| Model { subscription: None });
let (mut done_tx1, mut done_rx1) = postage::oneshot::channel();
let (mut done_tx2, mut done_rx2) = postage::oneshot::channel();
let _subscription1 = model.update(&mut cx, |_, cx| {
client.subscribe_to_entity(
1,
cx,
move |_, _: TypedEnvelope<proto::UnshareProject>, _, _| {
postage::sink::Sink::try_send(&mut done_tx1, ()).unwrap();
Ok(())
},
)
});
let _subscription2 = model.update(&mut cx, |_, cx| {
client.subscribe_to_entity(
2,
cx,
move |_, _: TypedEnvelope<proto::UnshareProject>, _, _| {
postage::sink::Sink::try_send(&mut done_tx2, ()).unwrap();
Ok(())
},
)
});
// Ensure dropping a subscription for the same entity type still allows receiving of
// messages for other entity IDs of the same type.
let subscription3 = model.update(&mut cx, |_, cx| {
client.subscribe_to_entity(
3,
cx,
move |_, _: TypedEnvelope<proto::UnshareProject>, _, _| Ok(()),
)
});
drop(subscription3);
server.send(proto::UnshareProject { project_id: 1 }).await;
server.send(proto::UnshareProject { project_id: 2 }).await;
done_rx1.next().await.unwrap();
done_rx2.next().await.unwrap();
}
#[gpui::test]
async fn test_subscribing_after_dropping_subscription(mut cx: TestAppContext) {
cx.foreground().forbid_parking();
let user_id = 5;
let mut client = Client::new(FakeHttpClient::with_404_response());
let server = FakeServer::for_client(user_id, &mut client, &cx).await;
let model = cx.add_model(|_| Model { subscription: None });
let (mut done_tx1, _done_rx1) = postage::oneshot::channel();
let (mut done_tx2, mut done_rx2) = postage::oneshot::channel();
let subscription1 = model.update(&mut cx, |_, cx| {
client.subscribe(cx, move |_, _: TypedEnvelope<proto::Ping>, _, _| {
postage::sink::Sink::try_send(&mut done_tx1, ()).unwrap();
Ok(())
})
});
drop(subscription1);
let _subscription2 = model.update(&mut cx, |_, cx| {
client.subscribe(cx, move |_, _: TypedEnvelope<proto::Ping>, _, _| {
postage::sink::Sink::try_send(&mut done_tx2, ()).unwrap();
Ok(())
})
});
server.send(proto::Ping {}).await;
done_rx2.next().await.unwrap();
}
#[gpui::test]
async fn test_dropping_subscription_in_handler(mut cx: TestAppContext) {
cx.foreground().forbid_parking();
let user_id = 5;
let mut client = Client::new(FakeHttpClient::with_404_response());
let server = FakeServer::for_client(user_id, &mut client, &cx).await;
let model = cx.add_model(|_| Model { subscription: None });
let (mut done_tx, mut done_rx) = postage::oneshot::channel();
model.update(&mut cx, |model, cx| {
model.subscription = Some(client.subscribe(
cx,
move |model, _: TypedEnvelope<proto::Ping>, _, _| {
model.subscription.take();
postage::sink::Sink::try_send(&mut done_tx, ()).unwrap();
Ok(())
},
));
});
server.send(proto::Ping {}).await;
done_rx.next().await.unwrap();
}
struct Model {
subscription: Option<Subscription>,
}
impl Entity for Model {
type Event = ();
}
}

View File

@@ -1,10 +1,9 @@
use super::Client;
use super::*;
use crate::http::{HttpClient, Request, Response, ServerResponse};
use futures::{future::BoxFuture, Future};
use gpui::TestAppContext;
use futures::{future::BoxFuture, stream::BoxStream, Future, StreamExt};
use gpui::{ModelHandle, TestAppContext};
use parking_lot::Mutex;
use postage::{mpsc, prelude::Stream};
use rpc::{proto, ConnectionId, Peer, Receipt, TypedEnvelope};
use std::fmt;
use std::sync::atomic::Ordering::SeqCst;
@@ -15,7 +14,7 @@ use std::sync::{
pub struct FakeServer {
peer: Arc<Peer>,
incoming: Mutex<Option<mpsc::Receiver<Box<dyn proto::AnyTypedEnvelope>>>>,
incoming: Mutex<Option<BoxStream<'static, Box<dyn proto::AnyTypedEnvelope>>>>,
connection_id: Mutex<Option<ConnectionId>>,
forbid_connections: AtomicBool,
auth_count: AtomicUsize,
@@ -72,8 +71,8 @@ impl FakeServer {
server
}
pub async fn disconnect(&self) {
self.peer.disconnect(self.connection_id()).await;
pub fn disconnect(&self) {
self.peer.disconnect(self.connection_id());
self.connection_id.lock().take();
self.incoming.lock().take();
}
@@ -129,7 +128,7 @@ impl FakeServer {
.lock()
.as_mut()
.expect("not connected")
.recv()
.next()
.await
.ok_or_else(|| anyhow!("other half hung up"))?;
let type_name = message.payload_type_name();
@@ -155,6 +154,24 @@ impl FakeServer {
fn connection_id(&self) -> ConnectionId {
self.connection_id.lock().expect("not connected")
}
pub async fn build_user_store(
&self,
client: Arc<Client>,
cx: &mut TestAppContext,
) -> ModelHandle<UserStore> {
let http_client = FakeHttpClient::with_404_response();
let user_store = cx.add_model(|cx| UserStore::new(client, http_client, cx));
assert_eq!(
self.receive::<proto::GetUsers>()
.await
.unwrap()
.payload
.user_ids,
&[self.user_id]
);
user_store
}
}
pub struct FakeHttpClient {
@@ -172,6 +189,10 @@ impl FakeHttpClient {
handler: Box::new(move |req| Box::pin(handler(req))),
})
}
pub fn with_404_response() -> Arc<dyn HttpClient> {
Self::new(|_| async move { Ok(ServerResponse::new(404)) })
}
}
impl fmt::Debug for FakeHttpClient {

View File

@@ -20,26 +20,26 @@ pub struct User {
}
#[derive(Debug)]
pub struct Collaborator {
pub struct Contact {
pub user: Arc<User>,
pub worktrees: Vec<WorktreeMetadata>,
pub projects: Vec<ProjectMetadata>,
}
#[derive(Debug)]
pub struct WorktreeMetadata {
pub struct ProjectMetadata {
pub id: u64,
pub root_name: String,
pub is_shared: bool,
pub worktree_root_names: Vec<String>,
pub guests: Vec<Arc<User>>,
}
pub struct UserStore {
users: HashMap<u64, Arc<User>>,
current_user: watch::Receiver<Option<Arc<User>>>,
collaborators: Arc<[Collaborator]>,
rpc: Arc<Client>,
contacts: Arc<[Contact]>,
client: Arc<Client>,
http: Arc<dyn HttpClient>,
_maintain_collaborators: Task<()>,
_maintain_contacts: Task<()>,
_maintain_current_user: Task<()>,
}
@@ -50,39 +50,43 @@ impl Entity for UserStore {
}
impl UserStore {
pub fn new(rpc: Arc<Client>, http: Arc<dyn HttpClient>, cx: &mut ModelContext<Self>) -> Self {
pub fn new(
client: Arc<Client>,
http: Arc<dyn HttpClient>,
cx: &mut ModelContext<Self>,
) -> Self {
let (mut current_user_tx, current_user_rx) = watch::channel();
let (mut update_collaborators_tx, mut update_collaborators_rx) =
watch::channel::<Option<proto::UpdateCollaborators>>();
let update_collaborators_subscription = rpc.subscribe(
let (mut update_contacts_tx, mut update_contacts_rx) =
watch::channel::<Option<proto::UpdateContacts>>();
let update_contacts_subscription = client.subscribe(
cx,
move |_: &mut Self, msg: TypedEnvelope<proto::UpdateCollaborators>, _, _| {
let _ = update_collaborators_tx.blocking_send(Some(msg.payload));
move |_: &mut Self, msg: TypedEnvelope<proto::UpdateContacts>, _, _| {
let _ = update_contacts_tx.blocking_send(Some(msg.payload));
Ok(())
},
);
Self {
users: Default::default(),
current_user: current_user_rx,
collaborators: Arc::from([]),
rpc: rpc.clone(),
contacts: Arc::from([]),
client: client.clone(),
http,
_maintain_collaborators: cx.spawn_weak(|this, mut cx| async move {
let _subscription = update_collaborators_subscription;
while let Some(message) = update_collaborators_rx.recv().await {
_maintain_contacts: cx.spawn_weak(|this, mut cx| async move {
let _subscription = update_contacts_subscription;
while let Some(message) = update_contacts_rx.recv().await {
if let Some((message, this)) = message.zip(this.upgrade(&cx)) {
this.update(&mut cx, |this, cx| this.update_collaborators(message, cx))
this.update(&mut cx, |this, cx| this.update_contacts(message, cx))
.log_err()
.await;
}
}
}),
_maintain_current_user: cx.spawn_weak(|this, mut cx| async move {
let mut status = rpc.status();
let mut status = client.status();
while let Some(status) = status.recv().await {
match status {
Status::Connected { .. } => {
if let Some((this, user_id)) = this.upgrade(&cx).zip(rpc.user_id()) {
if let Some((this, user_id)) = this.upgrade(&cx).zip(client.user_id()) {
let user = this
.update(&mut cx, |this, cx| this.fetch_user(user_id, cx))
.log_err()
@@ -100,35 +104,29 @@ impl UserStore {
}
}
fn update_collaborators(
fn update_contacts(
&mut self,
message: proto::UpdateCollaborators,
message: proto::UpdateContacts,
cx: &mut ModelContext<Self>,
) -> Task<Result<()>> {
let mut user_ids = HashSet::new();
for collaborator in &message.collaborators {
user_ids.insert(collaborator.user_id);
user_ids.extend(
collaborator
.worktrees
.iter()
.flat_map(|w| &w.guests)
.copied(),
);
for contact in &message.contacts {
user_ids.insert(contact.user_id);
user_ids.extend(contact.projects.iter().flat_map(|w| &w.guests).copied());
}
let load_users = self.load_users(user_ids.into_iter().collect(), cx);
cx.spawn(|this, mut cx| async move {
load_users.await?;
let mut collaborators = Vec::new();
for collaborator in message.collaborators {
collaborators.push(Collaborator::from_proto(collaborator, &this, &mut cx).await?);
let mut contacts = Vec::new();
for contact in message.contacts {
contacts.push(Contact::from_proto(contact, &this, &mut cx).await?);
}
this.update(&mut cx, |this, cx| {
collaborators.sort_by(|a, b| a.user.github_login.cmp(&b.user.github_login));
this.collaborators = collaborators.into();
contacts.sort_by(|a, b| a.user.github_login.cmp(&b.user.github_login));
this.contacts = contacts.into();
cx.notify();
});
@@ -136,8 +134,8 @@ impl UserStore {
})
}
pub fn collaborators(&self) -> &Arc<[Collaborator]> {
&self.collaborators
pub fn contacts(&self) -> &Arc<[Contact]> {
&self.contacts
}
pub fn load_users(
@@ -145,7 +143,7 @@ impl UserStore {
mut user_ids: Vec<u64>,
cx: &mut ModelContext<Self>,
) -> Task<Result<()>> {
let rpc = self.rpc.clone();
let rpc = self.client.clone();
let http = self.http.clone();
user_ids.retain(|id| !self.users.contains_key(id));
cx.spawn_weak(|this, mut cx| async move {
@@ -212,21 +210,21 @@ impl User {
}
}
impl Collaborator {
impl Contact {
async fn from_proto(
collaborator: proto::Collaborator,
contact: proto::Contact,
user_store: &ModelHandle<UserStore>,
cx: &mut AsyncAppContext,
) -> Result<Self> {
let user = user_store
.update(cx, |user_store, cx| {
user_store.fetch_user(collaborator.user_id, cx)
user_store.fetch_user(contact.user_id, cx)
})
.await?;
let mut worktrees = Vec::new();
for worktree in collaborator.worktrees {
let mut projects = Vec::new();
for project in contact.projects {
let mut guests = Vec::new();
for participant_id in worktree.guests {
for participant_id in project.guests {
guests.push(
user_store
.update(cx, |user_store, cx| {
@@ -235,14 +233,14 @@ impl Collaborator {
.await?,
);
}
worktrees.push(WorktreeMetadata {
id: worktree.id,
root_name: worktree.root_name,
is_shared: worktree.is_shared,
projects.push(ProjectMetadata {
id: project.id,
worktree_root_names: project.worktree_root_names.clone(),
is_shared: project.is_shared,
guests,
});
}
Ok(Self { user, worktrees })
Ok(Self { user, projects })
}
}

View File

@@ -3,6 +3,9 @@ name = "clock"
version = "0.1.0"
edition = "2018"
[lib]
path = "src/clock.rs"
[dependencies]
smallvec = { version = "1.6", features = ["union"] }
rpc = { path = "../rpc" }

View File

@@ -1,9 +1,8 @@
use smallvec::SmallVec;
use std::{
cmp::{self, Ordering},
fmt,
fmt, iter,
ops::{Add, AddAssign},
slice,
};
pub type ReplicaId = u16;
@@ -22,6 +21,15 @@ pub struct Lamport {
}
impl Local {
pub const MIN: Self = Self {
replica_id: ReplicaId::MIN,
value: Seq::MIN,
};
pub const MAX: Self = Self {
replica_id: ReplicaId::MAX,
value: Seq::MAX,
};
pub fn new(replica_id: ReplicaId) -> Self {
Self {
replica_id,
@@ -59,7 +67,7 @@ impl<'a> AddAssign<&'a Local> for Local {
}
#[derive(Clone, Default, Hash, Eq, PartialEq)]
pub struct Global(SmallVec<[Local; 3]>);
pub struct Global(SmallVec<[u32; 8]>);
impl From<Vec<rpc::proto::VectorClockEntry>> for Global {
fn from(message: Vec<rpc::proto::VectorClockEntry>) -> Self {
@@ -86,81 +94,125 @@ impl<'a> From<&'a Global> for Vec<rpc::proto::VectorClockEntry> {
}
}
impl From<Global> for Vec<rpc::proto::VectorClockEntry> {
fn from(version: Global) -> Self {
(&version).into()
}
}
impl Global {
pub fn new() -> Self {
Self::default()
}
pub fn get(&self, replica_id: ReplicaId) -> Seq {
self.0
.iter()
.find(|t| t.replica_id == replica_id)
.map_or(0, |t| t.value)
self.0.get(replica_id as usize).copied().unwrap_or(0) as Seq
}
pub fn observe(&mut self, timestamp: Local) {
if let Some(entry) = self
.0
.iter_mut()
.find(|t| t.replica_id == timestamp.replica_id)
{
entry.value = cmp::max(entry.value, timestamp.value);
} else {
self.0.push(timestamp);
if timestamp.value > 0 {
let new_len = timestamp.replica_id as usize + 1;
if new_len > self.0.len() {
self.0.resize(new_len, 0);
}
let entry = &mut self.0[timestamp.replica_id as usize];
*entry = cmp::max(*entry, timestamp.value);
}
}
pub fn join(&mut self, other: &Self) {
for timestamp in other.0.iter() {
self.observe(*timestamp);
if other.0.len() > self.0.len() {
self.0.resize(other.0.len(), 0);
}
for (left, right) in self.0.iter_mut().zip(&other.0) {
*left = cmp::max(*left, *right);
}
}
pub fn meet(&mut self, other: &Self) {
for timestamp in other.0.iter() {
if let Some(entry) = self
.0
.iter_mut()
.find(|t| t.replica_id == timestamp.replica_id)
{
entry.value = cmp::min(entry.value, timestamp.value);
} else {
self.0.push(*timestamp);
if other.0.len() > self.0.len() {
self.0.resize(other.0.len(), 0);
}
let mut new_len = 0;
for (ix, (left, right)) in self
.0
.iter_mut()
.zip(other.0.iter().chain(iter::repeat(&0)))
.enumerate()
{
if *left == 0 {
*left = *right;
} else if *right > 0 {
*left = cmp::min(*left, *right);
}
if *left != 0 {
new_len = ix + 1;
}
}
self.0.resize(new_len, 0);
}
pub fn observed(&self, timestamp: Local) -> bool {
self.get(timestamp.replica_id) >= timestamp.value
}
pub fn changed_since(&self, other: &Self) -> bool {
self.0.iter().any(|t| t.value > other.get(t.replica_id))
}
pub fn iter(&self) -> slice::Iter<Local> {
self.0.iter()
}
}
impl PartialOrd for Global {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
let mut global_ordering = Ordering::Equal;
for timestamp in self.0.iter().chain(other.0.iter()) {
let ordering = self
.get(timestamp.replica_id)
.cmp(&other.get(timestamp.replica_id));
if ordering != Ordering::Equal {
if global_ordering == Ordering::Equal {
global_ordering = ordering;
} else if ordering != global_ordering {
return None;
pub fn observed_any(&self, other: &Self) -> bool {
let mut lhs = self.0.iter();
let mut rhs = other.0.iter();
loop {
if let Some(left) = lhs.next() {
if let Some(right) = rhs.next() {
if *right > 0 && left >= right {
return true;
}
} else {
return false;
}
} else {
return false;
}
}
}
Some(global_ordering)
pub fn observed_all(&self, other: &Self) -> bool {
let mut lhs = self.0.iter();
let mut rhs = other.0.iter();
loop {
if let Some(left) = lhs.next() {
if let Some(right) = rhs.next() {
if left < right {
return false;
}
} else {
return true;
}
} else {
return rhs.next().is_none();
}
}
}
pub fn changed_since(&self, other: &Self) -> bool {
if self.0.len() > other.0.len() {
return true;
}
for (left, right) in self.0.iter().zip(other.0.iter()) {
if left > right {
return true;
}
}
false
}
pub fn iter<'a>(&'a self) -> impl 'a + Iterator<Item = Local> {
self.0.iter().enumerate().map(|(replica_id, seq)| Local {
replica_id: replica_id as ReplicaId,
value: *seq,
})
}
}
@@ -213,11 +265,11 @@ impl fmt::Debug for Lamport {
impl fmt::Debug for Global {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Global {{")?;
for (i, element) in self.0.iter().enumerate() {
if i > 0 {
for timestamp in self.iter() {
if timestamp.replica_id > 0 {
write!(f, ", ")?;
}
write!(f, "{}: {}", element.replica_id, element.value)?;
write!(f, "{}: {}", timestamp.replica_id, timestamp.value)?;
}
write!(f, "}}")
}

View File

@@ -0,0 +1,13 @@
[package]
name = "collections"
version = "0.1.0"
edition = "2021"
[lib]
path = "src/collections.rs"
[features]
test-support = ["seahash"]
[dependencies]
seahash = { version = "4.1", optional = true }

View File

@@ -0,0 +1,26 @@
#[cfg(feature = "test-support")]
#[derive(Clone, Default)]
pub struct DeterministicState;
#[cfg(feature = "test-support")]
impl std::hash::BuildHasher for DeterministicState {
type Hasher = seahash::SeaHasher;
fn build_hasher(&self) -> Self::Hasher {
seahash::SeaHasher::new()
}
}
#[cfg(feature = "test-support")]
pub type HashMap<K, V> = std::collections::HashMap<K, V, DeterministicState>;
#[cfg(feature = "test-support")]
pub type HashSet<T> = std::collections::HashSet<T, DeterministicState>;
#[cfg(not(feature = "test-support"))]
pub type HashMap<K, V> = std::collections::HashMap<K, V>;
#[cfg(not(feature = "test-support"))]
pub type HashSet<T> = std::collections::HashSet<T>;
pub use std::collections::*;

View File

@@ -1,8 +1,11 @@
[package]
name = "people_panel"
name = "contacts_panel"
version = "0.1.0"
edition = "2018"
[lib]
path = "src/contacts_panel.rs"
[dependencies]
client = { path = "../client" }
gpui = { path = "../gpui" }

View File

@@ -1,131 +1,70 @@
use client::{Collaborator, UserStore};
use std::sync::Arc;
use client::{Contact, UserStore};
use gpui::{
action,
elements::*,
geometry::{rect::RectF, vector::vec2f},
platform::CursorStyle,
Element, ElementBox, Entity, LayoutContext, ModelHandle, MutableAppContext, RenderContext,
Subscription, View, ViewContext,
Element, ElementBox, Entity, LayoutContext, ModelHandle, RenderContext, Subscription, View,
ViewContext,
};
use postage::watch;
use theme::Theme;
use workspace::{Settings, Workspace};
use workspace::{AppState, JoinProject, JoinProjectParams, Settings};
action!(JoinWorktree, u64);
action!(LeaveWorktree, u64);
action!(ShareWorktree, u64);
action!(UnshareWorktree, u64);
pub fn init(cx: &mut MutableAppContext) {
cx.add_action(PeoplePanel::share_worktree);
cx.add_action(PeoplePanel::unshare_worktree);
cx.add_action(PeoplePanel::join_worktree);
cx.add_action(PeoplePanel::leave_worktree);
}
pub struct PeoplePanel {
collaborators: ListState,
pub struct ContactsPanel {
contacts: ListState,
user_store: ModelHandle<UserStore>,
settings: watch::Receiver<Settings>,
_maintain_collaborators: Subscription,
_maintain_contacts: Subscription,
}
impl PeoplePanel {
pub fn new(
user_store: ModelHandle<UserStore>,
settings: watch::Receiver<Settings>,
cx: &mut ViewContext<Self>,
) -> Self {
impl ContactsPanel {
pub fn new(app_state: Arc<AppState>, cx: &mut ViewContext<Self>) -> Self {
Self {
collaborators: ListState::new(
user_store.read(cx).collaborators().len(),
contacts: ListState::new(
app_state.user_store.read(cx).contacts().len(),
Orientation::Top,
1000.,
{
let user_store = user_store.clone();
let settings = settings.clone();
let app_state = app_state.clone();
move |ix, cx| {
let user_store = user_store.read(cx);
let collaborators = user_store.collaborators().clone();
let user_store = app_state.user_store.read(cx);
let contacts = user_store.contacts().clone();
let current_user_id = user_store.current_user().map(|user| user.id);
Self::render_collaborator(
&collaborators[ix],
&contacts[ix],
current_user_id,
&settings.borrow().theme,
app_state.clone(),
cx,
)
}
},
),
_maintain_collaborators: cx.observe(&user_store, Self::update_collaborators),
user_store,
settings,
_maintain_contacts: cx.observe(&app_state.user_store, Self::update_contacts),
user_store: app_state.user_store.clone(),
settings: app_state.settings.clone(),
}
}
fn share_worktree(
workspace: &mut Workspace,
action: &ShareWorktree,
cx: &mut ViewContext<Workspace>,
) {
workspace
.project()
.update(cx, |p, cx| p.share_worktree(action.0, cx));
}
fn unshare_worktree(
workspace: &mut Workspace,
action: &UnshareWorktree,
cx: &mut ViewContext<Workspace>,
) {
workspace
.project()
.update(cx, |p, cx| p.unshare_worktree(action.0, cx));
}
fn join_worktree(
workspace: &mut Workspace,
action: &JoinWorktree,
cx: &mut ViewContext<Workspace>,
) {
workspace
.project()
.update(cx, |p, cx| p.add_remote_worktree(action.0, cx).detach());
}
fn leave_worktree(
workspace: &mut Workspace,
action: &LeaveWorktree,
cx: &mut ViewContext<Workspace>,
) {
workspace
.project()
.update(cx, |p, cx| p.close_remote_worktree(action.0, cx));
}
fn update_collaborators(&mut self, _: ModelHandle<UserStore>, cx: &mut ViewContext<Self>) {
self.collaborators
.reset(self.user_store.read(cx).collaborators().len());
fn update_contacts(&mut self, _: ModelHandle<UserStore>, cx: &mut ViewContext<Self>) {
self.contacts
.reset(self.user_store.read(cx).contacts().len());
cx.notify();
}
fn render_collaborator(
collaborator: &Collaborator,
collaborator: &Contact,
current_user_id: Option<u64>,
theme: &Theme,
app_state: Arc<AppState>,
cx: &mut LayoutContext,
) -> ElementBox {
let theme = &theme.people_panel;
let worktree_count = collaborator.worktrees.len();
let theme = &app_state.settings.borrow().theme.contacts_panel;
let project_count = collaborator.projects.len();
let font_cache = cx.font_cache();
let line_height = theme.unshared_worktree.name.text.line_height(font_cache);
let cap_height = theme.unshared_worktree.name.text.cap_height(font_cache);
let baseline_offset = theme
.unshared_worktree
.name
.text
.baseline_offset(font_cache)
+ (theme.unshared_worktree.height - line_height) / 2.;
let line_height = theme.unshared_project.name.text.line_height(font_cache);
let cap_height = theme.unshared_project.name.text.cap_height(font_cache);
let baseline_offset = theme.unshared_project.name.text.baseline_offset(font_cache)
+ (theme.unshared_project.height - line_height) / 2.;
let tree_branch_width = theme.tree_branch_width;
let tree_branch_color = theme.tree_branch_color;
let host_avatar_height = theme
@@ -161,11 +100,11 @@ impl PeoplePanel {
)
.with_children(
collaborator
.worktrees
.projects
.iter()
.enumerate()
.map(|(ix, worktree)| {
let worktree_id = worktree.id;
.map(|(ix, project)| {
let project_id = project.id;
Flex::row()
.with_child(
@@ -182,7 +121,7 @@ impl PeoplePanel {
vec2f(start_x, start_y),
vec2f(
start_x + tree_branch_width,
if ix + 1 == worktree_count {
if ix + 1 == project_count {
end_y
} else {
bounds.max_y()
@@ -210,28 +149,28 @@ impl PeoplePanel {
.with_child({
let is_host = Some(collaborator.user.id) == current_user_id;
let is_guest = !is_host
&& worktree
&& project
.guests
.iter()
.any(|guest| Some(guest.id) == current_user_id);
let is_shared = worktree.is_shared;
let is_shared = project.is_shared;
let app_state = app_state.clone();
MouseEventHandler::new::<PeoplePanel, _, _, _>(
worktree_id as usize,
MouseEventHandler::new::<ContactsPanel, _, _, _>(
project_id as usize,
cx,
|mouse_state, _| {
let style = match (worktree.is_shared, mouse_state.hovered)
{
(false, false) => &theme.unshared_worktree,
(false, true) => &theme.hovered_unshared_worktree,
(true, false) => &theme.shared_worktree,
(true, true) => &theme.hovered_shared_worktree,
let style = match (project.is_shared, mouse_state.hovered) {
(false, false) => &theme.unshared_project,
(false, true) => &theme.hovered_unshared_project,
(true, false) => &theme.shared_project,
(true, true) => &theme.hovered_shared_project,
};
Flex::row()
.with_child(
Label::new(
worktree.root_name.clone(),
project.worktree_root_names.join(", "),
style.name.text.clone(),
)
.aligned()
@@ -240,7 +179,7 @@ impl PeoplePanel {
.with_style(style.name.container)
.boxed(),
)
.with_children(worktree.guests.iter().filter_map(
.with_children(project.guests.iter().filter_map(
|participant| {
participant.avatar.clone().map(|avatar| {
Image::new(avatar)
@@ -268,23 +207,18 @@ impl PeoplePanel {
CursorStyle::Arrow
})
.on_click(move |cx| {
if is_shared {
if is_host {
cx.dispatch_action(UnshareWorktree(worktree_id));
} else if is_guest {
cx.dispatch_action(LeaveWorktree(worktree_id));
} else {
cx.dispatch_action(JoinWorktree(worktree_id))
}
} else if is_host {
cx.dispatch_action(ShareWorktree(worktree_id));
if !is_host && !is_guest {
cx.dispatch_global_action(JoinProject(JoinProjectParams {
project_id,
app_state: app_state.clone(),
}));
}
})
.expanded(1.0)
.flexible(1., true)
.boxed()
})
.constrained()
.with_height(theme.unshared_worktree.height)
.with_height(theme.unshared_project.height)
.boxed()
}),
)
@@ -294,18 +228,18 @@ impl PeoplePanel {
pub enum Event {}
impl Entity for PeoplePanel {
impl Entity for ContactsPanel {
type Event = Event;
}
impl View for PeoplePanel {
impl View for ContactsPanel {
fn ui_name() -> &'static str {
"PeoplePanel"
"ContactsPanel"
}
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
let theme = &self.settings.borrow().theme.people_panel;
Container::new(List::new(self.collaborators.clone()).boxed())
let theme = &self.settings.borrow().theme.contacts_panel;
Container::new(List::new(self.contacts.clone()).boxed())
.with_style(theme.container)
.boxed()
}

View File

@@ -0,0 +1,27 @@
[package]
name = "diagnostics"
version = "0.1.0"
edition = "2021"
[lib]
path = "src/diagnostics.rs"
[dependencies]
anyhow = "1.0"
collections = { path = "../collections" }
editor = { path = "../editor" }
language = { path = "../language" }
gpui = { path = "../gpui" }
project = { path = "../project" }
util = { path = "../util" }
workspace = { path = "../workspace" }
postage = { version = "0.4", features = ["futures-traits"] }
[dev-dependencies]
unindent = "0.1"
client = { path = "../client", features = ["test-support"] }
editor = { path = "../editor", features = ["test-support"] }
language = { path = "../language", features = ["test-support"] }
gpui = { path = "../gpui", features = ["test-support"] }
workspace = { path = "../workspace", features = ["test-support"] }
serde_json = { version = "1", features = ["preserve_order"] }

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,87 @@
use gpui::{
elements::*, platform::CursorStyle, Entity, ModelHandle, RenderContext, View, ViewContext,
};
use postage::watch;
use project::Project;
use std::fmt::Write;
use workspace::{Settings, StatusItemView};
pub struct DiagnosticSummary {
settings: watch::Receiver<Settings>,
summary: project::DiagnosticSummary,
in_progress: bool,
}
impl DiagnosticSummary {
pub fn new(
project: &ModelHandle<Project>,
settings: watch::Receiver<Settings>,
cx: &mut ViewContext<Self>,
) -> Self {
cx.subscribe(project, |this, project, event, cx| match event {
project::Event::DiskBasedDiagnosticsUpdated { .. } => {
this.summary = project.read(cx).diagnostic_summary(cx);
cx.notify();
}
project::Event::DiskBasedDiagnosticsStarted => {
this.in_progress = true;
cx.notify();
}
project::Event::DiskBasedDiagnosticsFinished => {
this.in_progress = false;
cx.notify();
}
_ => {}
})
.detach();
Self {
settings,
summary: project.read(cx).diagnostic_summary(cx),
in_progress: project.read(cx).is_running_disk_based_diagnostics(),
}
}
}
impl Entity for DiagnosticSummary {
type Event = ();
}
impl View for DiagnosticSummary {
fn ui_name() -> &'static str {
"DiagnosticSummary"
}
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
enum Tag {}
let theme = &self.settings.borrow().theme.project_diagnostics;
let mut message = String::new();
if self.in_progress {
message.push_str("Checking... ");
}
write!(
message,
"Errors: {}, Warnings: {}",
self.summary.error_count, self.summary.warning_count
)
.unwrap();
MouseEventHandler::new::<Tag, _, _, _>(0, cx, |_, _| {
Label::new(message, theme.status_bar_item.text.clone())
.contained()
.with_style(theme.status_bar_item.container)
.boxed()
})
.with_cursor_style(CursorStyle::PointingHand)
.on_click(|cx| cx.dispatch_action(crate::Deploy))
.boxed()
}
}
impl StatusItemView for DiagnosticSummary {
fn set_active_pane_item(
&mut self,
_: Option<&dyn workspace::ItemViewHandle>,
_: &mut ViewContext<Self>,
) {
}
}

View File

@@ -1,31 +1,51 @@
[package]
name = "editor"
version = "0.1.0"
edition = "2018"
edition = "2021"
[lib]
path = "src/editor.rs"
[features]
test-support = ["buffer/test-support", "gpui/test-support"]
test-support = [
"rand",
"text/test-support",
"language/test-support",
"gpui/test-support",
"util/test-support",
]
[dependencies]
buffer = { path = "../buffer" }
text = { path = "../text" }
clock = { path = "../clock" }
collections = { path = "../collections" }
gpui = { path = "../gpui" }
language = { path = "../language" }
project = { path = "../project" }
sum_tree = { path = "../sum_tree" }
theme = { path = "../theme" }
util = { path = "../util" }
workspace = { path = "../workspace" }
aho-corasick = "0.7"
anyhow = "1.0"
itertools = "0.10"
lazy_static = "1.4"
log = "0.4"
parking_lot = "0.11"
postage = { version = "0.4", features = ["futures-traits"] }
rand = { version = "0.8.3", optional = true }
serde = { version = "1", features = ["derive", "rc"] }
smallvec = { version = "1.6", features = ["union"] }
smol = "1.2"
[dev-dependencies]
buffer = { path = "../buffer", features = ["test-support"] }
text = { path = "../text", features = ["test-support"] }
language = { path = "../language", features = ["test-support"] }
gpui = { path = "../gpui", features = ["test-support"] }
util = { path = "../util", features = ["test-support"] }
ctor = "0.1"
env_logger = "0.8"
rand = "0.8"
unindent = "0.1.7"
tree-sitter = "0.19"
tree-sitter-rust = "0.19"
tree-sitter = "0.20"
tree-sitter-rust = "0.20"

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,14 +1,17 @@
use super::fold_map::{self, FoldEdit, FoldPoint, Snapshot as FoldSnapshot};
use buffer::{rope, HighlightId};
use super::fold_map::{self, FoldEdit, FoldPoint, FoldSnapshot, ToFoldPoint};
use crate::MultiBufferSnapshot;
use language::{rope, Chunk};
use parking_lot::Mutex;
use std::{mem, ops::Range};
use std::{cmp, mem, ops::Range};
use sum_tree::Bias;
use text::Point;
use theme::SyntaxTheme;
pub struct TabMap(Mutex<Snapshot>);
pub struct TabMap(Mutex<TabSnapshot>);
impl TabMap {
pub fn new(input: FoldSnapshot, tab_size: usize) -> (Self, Snapshot) {
let snapshot = Snapshot {
pub fn new(input: FoldSnapshot, tab_size: usize) -> (Self, TabSnapshot) {
let snapshot = TabSnapshot {
fold_snapshot: input,
tab_size,
};
@@ -19,9 +22,10 @@ impl TabMap {
&self,
fold_snapshot: FoldSnapshot,
mut fold_edits: Vec<FoldEdit>,
) -> (Snapshot, Vec<Edit>) {
) -> (TabSnapshot, Vec<TabEdit>) {
let mut old_snapshot = self.0.lock();
let new_snapshot = Snapshot {
let max_offset = old_snapshot.fold_snapshot.len();
let new_snapshot = TabSnapshot {
fold_snapshot,
tab_size: old_snapshot.tab_size,
};
@@ -31,19 +35,19 @@ impl TabMap {
let mut delta = 0;
for chunk in old_snapshot
.fold_snapshot
.chunks_at(fold_edit.old_bytes.end)
.chunks(fold_edit.old.end..max_offset, None)
{
let patterns: &[_] = &['\t', '\n'];
if let Some(ix) = chunk.find(patterns) {
if &chunk[ix..ix + 1] == "\t" {
fold_edit.old_bytes.end.0 += delta + ix + 1;
fold_edit.new_bytes.end.0 += delta + ix + 1;
if let Some(ix) = chunk.text.find(patterns) {
if &chunk.text[ix..ix + 1] == "\t" {
fold_edit.old.end.0 += delta + ix + 1;
fold_edit.new.end.0 += delta + ix + 1;
}
break;
}
delta += chunk.len();
delta += chunk.text.len();
}
}
@@ -52,9 +56,9 @@ impl TabMap {
let (prev_edits, next_edits) = fold_edits.split_at_mut(ix);
let prev_edit = prev_edits.last_mut().unwrap();
let edit = &next_edits[0];
if prev_edit.old_bytes.end >= edit.old_bytes.start {
prev_edit.old_bytes.end = edit.old_bytes.end;
prev_edit.new_bytes.end = edit.new_bytes.end;
if prev_edit.old.end >= edit.old.start {
prev_edit.old.end = edit.old.end;
prev_edit.new.end = edit.new.end;
fold_edits.remove(ix);
} else {
ix += 1;
@@ -62,25 +66,13 @@ impl TabMap {
}
for fold_edit in fold_edits {
let old_start = fold_edit
.old_bytes
.start
.to_point(&old_snapshot.fold_snapshot);
let old_end = fold_edit
.old_bytes
.end
.to_point(&old_snapshot.fold_snapshot);
let new_start = fold_edit
.new_bytes
.start
.to_point(&new_snapshot.fold_snapshot);
let new_end = fold_edit
.new_bytes
.end
.to_point(&new_snapshot.fold_snapshot);
tab_edits.push(Edit {
old_lines: old_snapshot.to_tab_point(old_start)..old_snapshot.to_tab_point(old_end),
new_lines: new_snapshot.to_tab_point(new_start)..new_snapshot.to_tab_point(new_end),
let old_start = fold_edit.old.start.to_point(&old_snapshot.fold_snapshot);
let old_end = fold_edit.old.end.to_point(&old_snapshot.fold_snapshot);
let new_start = fold_edit.new.start.to_point(&new_snapshot.fold_snapshot);
let new_end = fold_edit.new.end.to_point(&new_snapshot.fold_snapshot);
tab_edits.push(TabEdit {
old: old_snapshot.to_tab_point(old_start)..old_snapshot.to_tab_point(old_end),
new: new_snapshot.to_tab_point(new_start)..new_snapshot.to_tab_point(new_end),
});
}
@@ -90,12 +82,16 @@ impl TabMap {
}
#[derive(Clone)]
pub struct Snapshot {
pub struct TabSnapshot {
pub fold_snapshot: FoldSnapshot,
pub tab_size: usize,
}
impl Snapshot {
impl TabSnapshot {
pub fn buffer_snapshot(&self) -> &MultiBufferSnapshot {
self.fold_snapshot.buffer_snapshot()
}
pub fn text_summary(&self) -> TextSummary {
self.text_summary_for_range(TabPoint::zero()..self.max_point())
}
@@ -108,28 +104,31 @@ impl Snapshot {
.text_summary_for_range(input_start..input_end);
let mut first_line_chars = 0;
let mut first_line_bytes = 0;
for c in self.chunks_at(range.start).flat_map(|chunk| chunk.chars()) {
if c == '\n'
|| (range.start.row() == range.end.row() && first_line_bytes == range.end.column())
{
let line_end = if range.start.row() == range.end.row() {
range.end
} else {
self.max_point()
};
for c in self
.chunks(range.start..line_end, None)
.flat_map(|chunk| chunk.text.chars())
{
if c == '\n' {
break;
}
first_line_chars += 1;
first_line_bytes += c.len_utf8() as u32;
}
let mut last_line_chars = 0;
let mut last_line_bytes = 0;
for c in self
.chunks_at(TabPoint::new(range.end.row(), 0).max(range.start))
.flat_map(|chunk| chunk.chars())
{
if last_line_bytes == range.end.column() {
break;
if range.start.row() == range.end.row() {
last_line_chars = first_line_chars;
} else {
for _ in self
.chunks(TabPoint::new(range.end.row(), 0)..range.end, None)
.flat_map(|chunk| chunk.text.chars())
{
last_line_chars += 1;
}
last_line_chars += 1;
last_line_bytes += c.len_utf8() as u32;
}
TextSummary {
@@ -145,21 +144,11 @@ impl Snapshot {
self.fold_snapshot.version
}
pub fn chunks_at(&self, point: TabPoint) -> Chunks {
let (point, expanded_char_column, to_next_stop) = self.to_fold_point(point, Bias::Left);
let fold_chunks = self
.fold_snapshot
.chunks_at(point.to_offset(&self.fold_snapshot));
Chunks {
fold_chunks,
column: expanded_char_column,
tab_size: self.tab_size,
chunk: &SPACES[0..to_next_stop],
skip_leading_tab: to_next_stop > 0,
}
}
pub fn highlighted_chunks(&mut self, range: Range<TabPoint>) -> HighlightedChunks {
pub fn chunks<'a>(
&'a self,
range: Range<TabPoint>,
theme: Option<&'a SyntaxTheme>,
) -> TabChunks<'a> {
let (input_start, expanded_char_column, to_next_stop) =
self.to_fold_point(range.start, Bias::Left);
let input_start = input_start.to_offset(&self.fold_snapshot);
@@ -167,25 +156,35 @@ impl Snapshot {
.to_fold_point(range.end, Bias::Right)
.0
.to_offset(&self.fold_snapshot);
HighlightedChunks {
fold_chunks: self
.fold_snapshot
.highlighted_chunks(input_start..input_end),
let to_next_stop = if range.start.0 + Point::new(0, to_next_stop as u32) > range.end.0 {
(range.end.column() - range.start.column()) as usize
} else {
to_next_stop
};
TabChunks {
fold_chunks: self.fold_snapshot.chunks(input_start..input_end, theme),
column: expanded_char_column,
output_position: range.start.0,
max_output_position: range.end.0,
tab_size: self.tab_size,
chunk: &SPACES[0..to_next_stop],
chunk: Chunk {
text: &SPACES[0..to_next_stop],
..Default::default()
},
skip_leading_tab: to_next_stop > 0,
style_id: Default::default(),
}
}
pub fn buffer_rows(&self, row: u32) -> fold_map::BufferRows {
pub fn buffer_rows(&self, row: u32) -> fold_map::FoldBufferRows {
self.fold_snapshot.buffer_rows(row)
}
#[cfg(test)]
pub fn text(&self) -> String {
self.chunks_at(Default::default()).collect()
self.chunks(TabPoint::zero()..self.max_point(), None)
.map(|chunk| chunk.text)
.collect()
}
pub fn max_point(&self) -> TabPoint {
@@ -205,6 +204,10 @@ impl Snapshot {
TabPoint::new(input.row(), expanded as u32)
}
pub fn from_point(&self, point: Point, bias: Bias) -> TabPoint {
self.to_tab_point(point.to_fold_point(&self.fold_snapshot, bias))
}
pub fn to_fold_point(&self, output: TabPoint, bias: Bias) -> (FoldPoint, usize, usize) {
let chars = self.fold_snapshot.chars_at(FoldPoint::new(output.row(), 0));
let expanded = output.column() as usize;
@@ -217,6 +220,12 @@ impl Snapshot {
)
}
pub fn to_point(&self, point: TabPoint, bias: Bias) -> Point {
self.to_fold_point(point, bias)
.0
.to_buffer_point(&self.fold_snapshot)
}
fn expand_tabs(chars: impl Iterator<Item = char>, column: usize, tab_size: usize) -> usize {
let mut expanded_chars = 0;
let mut expanded_bytes = 0;
@@ -306,11 +315,7 @@ impl From<super::Point> for TabPoint {
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Edit {
pub old_lines: Range<TabPoint>,
pub new_lines: Range<TabPoint>,
}
pub type TabEdit = text::Edit<TabPoint>;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct TextSummary {
@@ -364,23 +369,25 @@ impl<'a> std::ops::AddAssign<&'a Self> for TextSummary {
// Handles a tab width <= 16
const SPACES: &'static str = " ";
pub struct Chunks<'a> {
fold_chunks: fold_map::Chunks<'a>,
chunk: &'a str,
pub struct TabChunks<'a> {
fold_chunks: fold_map::FoldChunks<'a>,
chunk: Chunk<'a>,
column: usize,
output_position: Point,
max_output_position: Point,
tab_size: usize,
skip_leading_tab: bool,
}
impl<'a> Iterator for Chunks<'a> {
type Item = &'a str;
impl<'a> Iterator for TabChunks<'a> {
type Item = Chunk<'a>;
fn next(&mut self) -> Option<Self::Item> {
if self.chunk.is_empty() {
if self.chunk.text.is_empty() {
if let Some(chunk) = self.fold_chunks.next() {
self.chunk = chunk;
if self.skip_leading_tab {
self.chunk = &self.chunk[1..];
self.chunk.text = &self.chunk.text[1..];
self.skip_leading_tab = false;
}
} else {
@@ -388,88 +395,121 @@ impl<'a> Iterator for Chunks<'a> {
}
}
for (ix, c) in self.chunk.char_indices() {
for (ix, c) in self.chunk.text.char_indices() {
match c {
'\t' => {
if ix > 0 {
let (prefix, suffix) = self.chunk.split_at(ix);
self.chunk = suffix;
return Some(prefix);
let (prefix, suffix) = self.chunk.text.split_at(ix);
self.chunk.text = suffix;
return Some(Chunk {
text: prefix,
..self.chunk
});
} else {
self.chunk = &self.chunk[1..];
let len = self.tab_size - self.column % self.tab_size;
self.chunk.text = &self.chunk.text[1..];
let mut len = self.tab_size - self.column % self.tab_size;
let next_output_position = cmp::min(
self.output_position + Point::new(0, len as u32),
self.max_output_position,
);
len = (next_output_position.column - self.output_position.column) as usize;
self.column += len;
return Some(&SPACES[0..len]);
self.output_position = next_output_position;
return Some(Chunk {
text: &SPACES[0..len],
..self.chunk
});
}
}
'\n' => self.column = 0,
_ => self.column += 1,
}
}
let result = Some(self.chunk);
self.chunk = "";
result
}
}
pub struct HighlightedChunks<'a> {
fold_chunks: fold_map::HighlightedChunks<'a>,
chunk: &'a str,
style_id: HighlightId,
column: usize,
tab_size: usize,
skip_leading_tab: bool,
}
impl<'a> Iterator for HighlightedChunks<'a> {
type Item = (&'a str, HighlightId);
fn next(&mut self) -> Option<Self::Item> {
if self.chunk.is_empty() {
if let Some((chunk, style_id)) = self.fold_chunks.next() {
self.chunk = chunk;
self.style_id = style_id;
if self.skip_leading_tab {
self.chunk = &self.chunk[1..];
self.skip_leading_tab = false;
'\n' => {
self.column = 0;
self.output_position += Point::new(1, 0);
}
} else {
return None;
}
}
for (ix, c) in self.chunk.char_indices() {
match c {
'\t' => {
if ix > 0 {
let (prefix, suffix) = self.chunk.split_at(ix);
self.chunk = suffix;
return Some((prefix, self.style_id));
} else {
self.chunk = &self.chunk[1..];
let len = self.tab_size - self.column % self.tab_size;
self.column += len;
return Some((&SPACES[0..len], self.style_id));
}
_ => {
self.column += 1;
self.output_position.column += c.len_utf8() as u32;
}
'\n' => self.column = 0,
_ => self.column += 1,
}
}
Some((mem::take(&mut self.chunk), mem::take(&mut self.style_id)))
Some(mem::take(&mut self.chunk))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{display_map::fold_map::FoldMap, MultiBuffer};
use rand::{prelude::StdRng, Rng};
use text::{RandomCharIter, Rope};
#[test]
fn test_expand_tabs() {
assert_eq!(Snapshot::expand_tabs("\t".chars(), 0, 4), 0);
assert_eq!(Snapshot::expand_tabs("\t".chars(), 1, 4), 4);
assert_eq!(Snapshot::expand_tabs("\ta".chars(), 2, 4), 5);
assert_eq!(TabSnapshot::expand_tabs("\t".chars(), 0, 4), 0);
assert_eq!(TabSnapshot::expand_tabs("\t".chars(), 1, 4), 4);
assert_eq!(TabSnapshot::expand_tabs("\ta".chars(), 2, 4), 5);
}
#[gpui::test(iterations = 100)]
fn test_random_tabs(cx: &mut gpui::MutableAppContext, mut rng: StdRng) {
let tab_size = rng.gen_range(1..=4);
let len = rng.gen_range(0..30);
let buffer = if rng.gen() {
let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
MultiBuffer::build_simple(&text, cx)
} else {
MultiBuffer::build_random(&mut rng, cx)
};
let buffer_snapshot = buffer.read(cx).snapshot(cx);
log::info!("Buffer text: {:?}", buffer_snapshot.text());
let (mut fold_map, _) = FoldMap::new(buffer_snapshot.clone());
fold_map.randomly_mutate(&mut rng);
let (folds_snapshot, _) = fold_map.read(buffer_snapshot.clone(), vec![]);
log::info!("FoldMap text: {:?}", folds_snapshot.text());
let (_, tabs_snapshot) = TabMap::new(folds_snapshot.clone(), tab_size);
let text = Rope::from(tabs_snapshot.text().as_str());
log::info!(
"TabMap text (tab size: {}): {:?}",
tab_size,
tabs_snapshot.text(),
);
for _ in 0..5 {
let end_row = rng.gen_range(0..=text.max_point().row);
let end_column = rng.gen_range(0..=text.line_len(end_row));
let mut end = TabPoint(text.clip_point(Point::new(end_row, end_column), Bias::Right));
let start_row = rng.gen_range(0..=text.max_point().row);
let start_column = rng.gen_range(0..=text.line_len(start_row));
let mut start =
TabPoint(text.clip_point(Point::new(start_row, start_column), Bias::Left));
if start > end {
mem::swap(&mut start, &mut end);
}
let expected_text = text
.chunks_in_range(text.point_to_offset(start.0)..text.point_to_offset(end.0))
.collect::<String>();
let expected_summary = TextSummary::from(expected_text.as_str());
assert_eq!(
expected_text,
tabs_snapshot
.chunks(start..end, None)
.map(|c| c.text)
.collect::<String>(),
"chunks({:?}..{:?})",
start,
end
);
let mut actual_summary = tabs_snapshot.text_summary_for_range(start..end);
if tab_size > 1 && folds_snapshot.text().contains('\t') {
actual_summary.longest_row = expected_summary.longest_row;
actual_summary.longest_row_chars = expected_summary.longest_row_chars;
}
assert_eq!(actual_summary, expected_summary,);
}
}
}

File diff suppressed because it is too large Load Diff

6293
crates/editor/src/editor.rs Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,11 +1,14 @@
use super::{
DisplayPoint, Editor, EditorMode, EditorSettings, EditorStyle, Input, Scroll, Select,
SelectPhase, Snapshot, MAX_LINE_LEN,
display_map::{BlockContext, ToDisplayPoint},
Anchor, DisplayPoint, Editor, EditorMode, EditorSettings, EditorSnapshot, EditorStyle, Input,
Scroll, Select, SelectPhase, SoftWrap, ToPoint, MAX_LINE_LEN,
};
use buffer::HighlightId;
use clock::ReplicaId;
use collections::{BTreeMap, HashMap};
use gpui::{
color::Color,
elements::layout_highlighted_chunks,
fonts::HighlightStyle,
geometry::{
rect::RectF,
vector::{vec2f, Vector2F},
@@ -14,14 +17,14 @@ use gpui::{
json::{self, ToJson},
keymap::Keystroke,
text_layout::{self, RunStyle, TextLayoutCache},
AppContext, Axis, Border, Element, Event, EventContext, FontCache, LayoutContext,
AppContext, Axis, Border, Element, ElementBox, Event, EventContext, FontCache, LayoutContext,
MutableAppContext, PaintContext, Quad, Scene, SizeConstraint, ViewContext, WeakViewHandle,
};
use json::json;
use language::Bias;
use smallvec::SmallVec;
use std::{
cmp::{self, Ordering},
collections::{BTreeMap, HashMap},
fmt::Write,
ops::Range,
};
@@ -47,26 +50,48 @@ impl EditorElement {
self.view.upgrade(cx).unwrap().update(cx, f)
}
fn snapshot(&self, cx: &mut MutableAppContext) -> Snapshot {
fn snapshot(&self, cx: &mut MutableAppContext) -> EditorSnapshot {
self.update_view(cx, |view, cx| view.snapshot(cx))
}
fn mouse_down(
&self,
position: Vector2F,
cmd: bool,
alt: bool,
shift: bool,
mut click_count: usize,
layout: &mut LayoutState,
paint: &mut PaintState,
cx: &mut EventContext,
) -> bool {
if paint.text_bounds.contains_point(position) {
let snapshot = self.snapshot(cx.app);
let position = paint.point_for_position(&snapshot, layout, position);
cx.dispatch_action(Select(SelectPhase::Begin { position, add: cmd }));
true
} else {
false
if paint.gutter_bounds.contains_point(position) {
click_count = 3; // Simulate triple-click when clicking the gutter to select lines
} else if !paint.text_bounds.contains_point(position) {
return false;
}
let snapshot = self.snapshot(cx.app);
let (position, overshoot) = paint.point_for_position(&snapshot, layout, position);
if shift && alt {
cx.dispatch_action(Select(SelectPhase::BeginColumnar {
position,
overshoot,
}));
} else if shift {
cx.dispatch_action(Select(SelectPhase::Extend {
position,
click_count,
}));
} else {
cx.dispatch_action(Select(SelectPhase::Begin {
position,
add: alt,
click_count,
}));
}
true
}
fn mouse_up(&self, _position: Vector2F, cx: &mut EventContext) -> bool {
@@ -118,10 +143,11 @@ impl EditorElement {
let font_cache = cx.font_cache.clone();
let text_layout_cache = cx.text_layout_cache.clone();
let snapshot = self.snapshot(cx.app);
let position = paint.point_for_position(&snapshot, layout, position);
let (position, overshoot) = paint.point_for_position(&snapshot, layout, position);
cx.dispatch_action(Select(SelectPhase::Update {
position,
overshoot,
scroll_position: (snapshot.scroll_position() + scroll_delta).clamp(
Vector2F::zero(),
layout.scroll_max(&font_cache, &text_layout_cache),
@@ -238,6 +264,24 @@ impl EditorElement {
});
}
}
if let Some(highlighted_rows) = &layout.highlighted_rows {
let origin = vec2f(
bounds.origin_x(),
bounds.origin_y() + (layout.line_height * highlighted_rows.start as f32)
- scroll_top,
);
let size = vec2f(
bounds.width(),
layout.line_height * highlighted_rows.len() as f32,
);
cx.scene.push_quad(Quad {
bounds: RectF::new(origin, size),
background: Some(style.highlighted_line_background),
border: Border::default(),
corner_radius: 0.,
});
}
}
}
@@ -287,22 +331,16 @@ impl EditorElement {
let content_origin = bounds.origin() + layout.text_offset;
for (replica_id, selections) in &layout.selections {
let style_ix = *replica_id as usize % (style.guest_selections.len() + 1);
let style = if style_ix == 0 {
&style.selection
} else {
&style.guest_selections[style_ix - 1]
};
let style = style.replica_selection_style(*replica_id);
for selection in selections {
if selection.start != selection.end {
let range_start = cmp::min(selection.start, selection.end);
let range_end = cmp::max(selection.start, selection.end);
let row_range = if range_end.column() == 0 {
cmp::max(range_start.row(), start_row)..cmp::min(range_end.row(), end_row)
let row_range = if selection.end.column() == 0 {
cmp::max(selection.start.row(), start_row)
..cmp::min(selection.end.row(), end_row)
} else {
cmp::max(range_start.row(), start_row)
..cmp::min(range_end.row() + 1, end_row)
cmp::max(selection.start.row(), start_row)
..cmp::min(selection.end.row() + 1, end_row)
};
let selection = Selection {
@@ -315,16 +353,18 @@ impl EditorElement {
.map(|row| {
let line_layout = &layout.line_layouts[(row - start_row) as usize];
SelectionLine {
start_x: if row == range_start.row() {
start_x: if row == selection.start.row() {
content_origin.x()
+ line_layout.x_for_index(range_start.column() as usize)
+ line_layout
.x_for_index(selection.start.column() as usize)
- scroll_left
} else {
content_origin.x() - scroll_left
},
end_x: if row == range_end.row() {
end_x: if row == selection.end.row() {
content_origin.x()
+ line_layout.x_for_index(range_end.column() as usize)
+ line_layout
.x_for_index(selection.end.column() as usize)
- scroll_left
} else {
content_origin.x()
@@ -341,13 +381,13 @@ impl EditorElement {
}
if view.show_local_cursors() || *replica_id != local_replica_id {
let cursor_position = selection.end;
let cursor_position = selection.head();
if (start_row..end_row).contains(&cursor_position.row()) {
let cursor_row_layout =
&layout.line_layouts[(selection.end.row() - start_row) as usize];
let x = cursor_row_layout.x_for_index(selection.end.column() as usize)
&layout.line_layouts[(cursor_position.row() - start_row) as usize];
let x = cursor_row_layout.x_for_index(cursor_position.column() as usize)
- scroll_left;
let y = selection.end.row() as f32 * layout.line_height - scroll_top;
let y = cursor_position.row() as f32 * layout.line_height - scroll_top;
cursors.push(Cursor {
color: style.cursor,
origin: content_origin + vec2f(x, y),
@@ -381,8 +421,26 @@ impl EditorElement {
cx.scene.pop_layer();
}
fn max_line_number_width(&self, snapshot: &Snapshot, cx: &LayoutContext) -> f32 {
let digit_count = (snapshot.buffer_row_count() as f32).log10().floor() as usize + 1;
fn paint_blocks(
&mut self,
bounds: RectF,
visible_bounds: RectF,
layout: &mut LayoutState,
cx: &mut PaintContext,
) {
let scroll_position = layout.snapshot.scroll_position();
let scroll_left = scroll_position.x() * layout.em_width;
let scroll_top = scroll_position.y() * layout.line_height;
for (row, element) in &mut layout.blocks {
let origin = bounds.origin()
+ vec2f(-scroll_left, *row as f32 * layout.line_height - scroll_top);
element.paint(origin, visible_bounds, cx);
}
}
fn max_line_number_width(&self, snapshot: &EditorSnapshot, cx: &LayoutContext) -> f32 {
let digit_count = (snapshot.max_buffer_row() as f32).log10().floor() as usize + 1;
let style = &self.settings.style;
cx.text_layout_cache
@@ -394,24 +452,25 @@ impl EditorElement {
RunStyle {
font_id: style.text.font_id,
color: Color::black(),
underline: false,
underline: None,
},
)],
)
.width()
}
fn layout_line_numbers(
fn layout_rows(
&self,
rows: Range<u32>,
active_rows: &BTreeMap<u32, bool>,
snapshot: &Snapshot,
snapshot: &EditorSnapshot,
cx: &LayoutContext,
) -> Vec<Option<text_layout::Line>> {
let style = &self.settings.style;
let mut layouts = Vec::with_capacity(rows.len());
let include_line_numbers = snapshot.mode == EditorMode::Full;
let mut line_number_layouts = Vec::with_capacity(rows.len());
let mut line_number = String::new();
for (ix, (buffer_row, soft_wrapped)) in snapshot
for (ix, row) in snapshot
.buffer_rows(rows.start)
.take((rows.end - rows.start) as usize)
.enumerate()
@@ -422,33 +481,35 @@ impl EditorElement {
} else {
style.line_number
};
if soft_wrapped {
layouts.push(None);
if let Some(buffer_row) = row {
if include_line_numbers {
line_number.clear();
write!(&mut line_number, "{}", buffer_row + 1).unwrap();
line_number_layouts.push(Some(cx.text_layout_cache.layout_str(
&line_number,
style.text.font_size,
&[(
line_number.len(),
RunStyle {
font_id: style.text.font_id,
color,
underline: None,
},
)],
)));
}
} else {
line_number.clear();
write!(&mut line_number, "{}", buffer_row + 1).unwrap();
layouts.push(Some(cx.text_layout_cache.layout_str(
&line_number,
style.text.font_size,
&[(
line_number.len(),
RunStyle {
font_id: style.text.font_id,
color,
underline: false,
},
)],
)));
line_number_layouts.push(None);
}
}
layouts
line_number_layouts
}
fn layout_lines(
&mut self,
mut rows: Range<u32>,
snapshot: &mut Snapshot,
snapshot: &mut EditorSnapshot,
cx: &LayoutContext,
) -> Vec<text_layout::Line> {
rows.end = cmp::min(rows.end, snapshot.max_point().row() + 1);
@@ -476,83 +537,90 @@ impl EditorElement {
RunStyle {
font_id: placeholder_style.font_id,
color: placeholder_style.color,
underline: false,
underline: None,
},
)],
)
})
.collect();
}
let style = &self.settings.style;
let mut prev_font_properties = style.text.font_properties.clone();
let mut prev_font_id = style.text.font_id;
let mut layouts = Vec::with_capacity(rows.len());
let mut line = String::new();
let mut styles = Vec::new();
let mut row = rows.start;
let mut line_exceeded_max_len = false;
let chunks = snapshot.highlighted_chunks_for_rows(rows.clone());
'outer: for (chunk, style_ix) in chunks.chain(Some(("\n", HighlightId::default()))) {
for (ix, mut line_chunk) in chunk.split('\n').enumerate() {
if ix > 0 {
layouts.push(cx.text_layout_cache.layout_str(
&line,
style.text.font_size,
&styles,
));
line.clear();
styles.clear();
row += 1;
line_exceeded_max_len = false;
if row == rows.end {
break 'outer;
}
}
if !line_chunk.is_empty() && !line_exceeded_max_len {
let highlight_style = style_ix
.style(&style.syntax)
.unwrap_or(style.text.clone().into());
// Avoid a lookup if the font properties match the previous ones.
let font_id = if highlight_style.font_properties == prev_font_properties {
prev_font_id
} else {
let style = &self.settings.style;
let chunks = snapshot
.chunks(rows.clone(), Some(&style.syntax))
.map(|chunk| {
let highlight = if let Some(severity) = chunk.diagnostic {
let underline = Some(super::diagnostic_style(severity, true, style).text);
if let Some(mut highlight) = chunk.highlight_style {
highlight.underline = underline;
Some(highlight)
} else {
Some(HighlightStyle {
underline,
color: style.text.color,
font_properties: style.text.font_properties,
})
}
} else {
cx.font_cache
.select_font(
style.text.font_family_id,
&highlight_style.font_properties,
)
.unwrap_or(style.text.font_id)
chunk.highlight_style
};
(chunk.text, highlight)
});
layout_highlighted_chunks(
chunks,
&style.text,
&cx.text_layout_cache,
&cx.font_cache,
MAX_LINE_LEN,
rows.len() as usize,
)
}
}
fn layout_blocks(
&mut self,
rows: Range<u32>,
snapshot: &EditorSnapshot,
width: f32,
line_number_x: f32,
text_x: f32,
line_height: f32,
style: &EditorStyle,
line_layouts: &[text_layout::Line],
cx: &mut LayoutContext,
) -> Vec<(u32, ElementBox)> {
snapshot
.blocks_in_range(rows.clone())
.map(|(start_row, block)| {
let anchor_row = block
.position()
.to_point(&snapshot.buffer_snapshot)
.to_display_point(snapshot)
.row();
let anchor_x = text_x
+ if rows.contains(&anchor_row) {
line_layouts[(anchor_row - rows.start) as usize]
.x_for_index(block.column() as usize)
} else {
layout_line(anchor_row, snapshot, style, cx.text_layout_cache)
.x_for_index(block.column() as usize)
};
if line.len() + line_chunk.len() > MAX_LINE_LEN {
let mut chunk_len = MAX_LINE_LEN - line.len();
while !line_chunk.is_char_boundary(chunk_len) {
chunk_len -= 1;
}
line_chunk = &line_chunk[..chunk_len];
line_exceeded_max_len = true;
}
line.push_str(line_chunk);
styles.push((
line_chunk.len(),
RunStyle {
font_id,
color: highlight_style.color,
underline: highlight_style.underline,
},
));
prev_font_id = font_id;
prev_font_properties = highlight_style.font_properties;
}
}
}
layouts
let mut element = block.render(&BlockContext {
cx,
anchor_x,
line_number_x,
});
element.layout(
SizeConstraint {
min: Vector2F::zero(),
max: vec2f(width, block.height() as f32 * line_height),
},
cx,
);
(start_row, element)
})
.collect()
}
}
@@ -587,8 +655,13 @@ impl Element for EditorElement {
let text_width = size.x() - gutter_width;
let text_offset = vec2f(-style.text.descent(cx.font_cache), 0.);
let em_width = style.text.em_width(cx.font_cache);
let em_advance = style.text.em_advance(cx.font_cache);
let overscroll = vec2f(em_width, 0.);
let wrap_width = text_width - text_offset.x() - overscroll.x() - em_width;
let wrap_width = match self.settings.soft_wrap {
SoftWrap::None => None,
SoftWrap::EditorWidth => Some(text_width - text_offset.x() - overscroll.x() - em_width),
SoftWrap::Column(column) => Some(column as f32 * em_advance),
};
let snapshot = self.update_view(cx.app, |view, cx| {
if view.set_wrap_width(wrap_width, cx) {
view.snapshot(cx)
@@ -622,49 +695,73 @@ impl Element for EditorElement {
let scroll_top = scroll_position.y() * line_height;
let end_row = ((scroll_top + size.y()) / line_height).ceil() as u32 + 1; // Add 1 to ensure selections bleed off screen
let mut selections = HashMap::new();
let mut active_rows = BTreeMap::new();
self.update_view(cx.app, |view, cx| {
for selection_set_id in view.active_selection_sets(cx).collect::<Vec<_>>() {
let mut set = Vec::new();
for selection in view.selections_in_range(
selection_set_id,
DisplayPoint::new(start_row, 0)..DisplayPoint::new(end_row, 0),
cx,
) {
set.push(selection.clone());
if selection_set_id == view.selection_set_id {
let is_empty = selection.start == selection.end;
let mut selection_start;
let mut selection_end;
if selection.start < selection.end {
selection_start = selection.start;
selection_end = selection.end;
} else {
selection_start = selection.end;
selection_end = selection.start;
};
selection_start = snapshot.prev_row_boundary(selection_start).0;
selection_end = snapshot.next_row_boundary(selection_end).0;
for row in cmp::max(selection_start.row(), start_row)
..=cmp::min(selection_end.row(), end_row)
{
let contains_non_empty_selection =
active_rows.entry(row).or_insert(!is_empty);
*contains_non_empty_selection |= !is_empty;
}
}
}
let start_anchor = if start_row == 0 {
Anchor::min()
} else {
snapshot
.buffer_snapshot
.anchor_before(DisplayPoint::new(start_row, 0).to_offset(&snapshot, Bias::Left))
};
let end_anchor = if end_row > snapshot.max_point().row() {
Anchor::max()
} else {
snapshot
.buffer_snapshot
.anchor_before(DisplayPoint::new(end_row, 0).to_offset(&snapshot, Bias::Right))
};
selections.insert(selection_set_id.replica_id, set);
let mut selections = HashMap::default();
let mut active_rows = BTreeMap::new();
let mut highlighted_rows = None;
self.update_view(cx.app, |view, cx| {
highlighted_rows = view.highlighted_rows();
let display_map = view.display_map.update(cx, |map, cx| map.snapshot(cx));
let local_selections = view
.local_selections_in_range(start_anchor.clone()..end_anchor.clone(), &display_map);
for selection in &local_selections {
let is_empty = selection.start == selection.end;
let selection_start = snapshot.prev_line_boundary(selection.start).1;
let selection_end = snapshot.next_line_boundary(selection.end).1;
for row in cmp::max(selection_start.row(), start_row)
..=cmp::min(selection_end.row(), end_row)
{
let contains_non_empty_selection = active_rows.entry(row).or_insert(!is_empty);
*contains_non_empty_selection |= !is_empty;
}
}
selections.insert(
view.replica_id(cx),
local_selections
.into_iter()
.map(|selection| crate::Selection {
id: selection.id,
goal: selection.goal,
reversed: selection.reversed,
start: selection.start.to_display_point(&display_map),
end: selection.end.to_display_point(&display_map),
})
.collect(),
);
for (replica_id, selection) in display_map
.buffer_snapshot
.remote_selections_in_range(&(start_anchor..end_anchor))
{
selections
.entry(replica_id)
.or_insert(Vec::new())
.push(crate::Selection {
id: selection.id,
goal: selection.goal,
reversed: selection.reversed,
start: selection.start.to_display_point(&display_map),
end: selection.end.to_display_point(&display_map),
});
}
});
let line_number_layouts = if snapshot.mode == EditorMode::Full {
self.layout_line_numbers(start_row..end_row, &active_rows, &snapshot, cx)
} else {
Vec::new()
};
let line_number_layouts = self.layout_rows(start_row..end_row, &active_rows, &snapshot, cx);
let mut max_visible_line_width = 0.0;
let line_layouts = self.layout_lines(start_row..end_row, &mut snapshot, cx);
@@ -674,6 +771,18 @@ impl Element for EditorElement {
}
}
let blocks = self.layout_blocks(
start_row..end_row,
&snapshot,
size.x(),
gutter_padding,
gutter_width + text_offset.x(),
line_height,
&style,
&line_layouts,
cx,
);
let mut layout = LayoutState {
size,
gutter_size,
@@ -684,10 +793,13 @@ impl Element for EditorElement {
snapshot,
style: self.settings.style.clone(),
active_rows,
highlighted_rows,
line_layouts,
line_number_layouts,
blocks,
line_height,
em_width,
em_advance,
selections,
max_visible_line_width,
};
@@ -740,11 +852,13 @@ impl Element for EditorElement {
self.paint_gutter(gutter_bounds, visible_bounds, layout, cx);
}
self.paint_text(text_bounds, visible_bounds, layout, cx);
self.paint_blocks(bounds, visible_bounds, layout, cx);
cx.scene.pop_layer();
Some(PaintState {
bounds,
gutter_bounds,
text_bounds,
})
} else {
@@ -762,9 +876,13 @@ impl Element for EditorElement {
) -> bool {
if let (Some(layout), Some(paint)) = (layout, paint) {
match event {
Event::LeftMouseDown { position, cmd } => {
self.mouse_down(*position, *cmd, layout, paint, cx)
}
Event::LeftMouseDown {
position,
alt,
shift,
click_count,
..
} => self.mouse_down(*position, *alt, *shift, *click_count, layout, paint, cx),
Event::LeftMouseUp { position } => self.mouse_up(*position, cx),
Event::LeftMouseDragged { position } => {
self.mouse_dragged(*position, layout, paint, cx)
@@ -804,13 +922,16 @@ pub struct LayoutState {
gutter_padding: f32,
text_size: Vector2F,
style: EditorStyle,
snapshot: Snapshot,
snapshot: EditorSnapshot,
active_rows: BTreeMap<u32, bool>,
highlighted_rows: Option<Range<u32>>,
line_layouts: Vec<text_layout::Line>,
line_number_layouts: Vec<Option<text_layout::Line>>,
blocks: Vec<(u32, ElementBox)>,
line_height: f32,
em_width: f32,
selections: HashMap<ReplicaId, Vec<Range<DisplayPoint>>>,
em_advance: f32,
selections: HashMap<ReplicaId, Vec<text::Selection<DisplayPoint>>>,
overscroll: Vector2F,
text_offset: Vector2F,
max_visible_line_width: f32,
@@ -819,7 +940,8 @@ pub struct LayoutState {
impl LayoutState {
fn scroll_width(&self, layout_cache: &TextLayoutCache) -> f32 {
let row = self.snapshot.longest_row();
let longest_line_width = self.layout_line(row, &self.snapshot, layout_cache).width();
let longest_line_width =
layout_line(row, &self.snapshot, &self.style, layout_cache).width();
longest_line_width.max(self.max_visible_line_width) + self.overscroll.x()
}
@@ -834,50 +956,51 @@ impl LayoutState {
max_row.saturating_sub(1) as f32,
)
}
}
pub fn layout_line(
&self,
row: u32,
snapshot: &Snapshot,
layout_cache: &TextLayoutCache,
) -> text_layout::Line {
let mut line = snapshot.line(row);
fn layout_line(
row: u32,
snapshot: &EditorSnapshot,
style: &EditorStyle,
layout_cache: &TextLayoutCache,
) -> text_layout::Line {
let mut line = snapshot.line(row);
if line.len() > MAX_LINE_LEN {
let mut len = MAX_LINE_LEN;
while !line.is_char_boundary(len) {
len -= 1;
}
line.truncate(len);
if line.len() > MAX_LINE_LEN {
let mut len = MAX_LINE_LEN;
while !line.is_char_boundary(len) {
len -= 1;
}
layout_cache.layout_str(
&line,
self.style.text.font_size,
&[(
snapshot.line_len(row) as usize,
RunStyle {
font_id: self.style.text.font_id,
color: Color::black(),
underline: false,
},
)],
)
line.truncate(len);
}
layout_cache.layout_str(
&line,
style.text.font_size,
&[(
snapshot.line_len(row) as usize,
RunStyle {
font_id: style.text.font_id,
color: Color::black(),
underline: None,
},
)],
)
}
pub struct PaintState {
bounds: RectF,
gutter_bounds: RectF,
text_bounds: RectF,
}
impl PaintState {
fn point_for_position(
&self,
snapshot: &Snapshot,
snapshot: &EditorSnapshot,
layout: &LayoutState,
position: Vector2F,
) -> DisplayPoint {
) -> (DisplayPoint, u32) {
let scroll_position = snapshot.scroll_position();
let position = position - self.text_bounds.origin();
let y = position.y().max(0.0).min(layout.size.y());
@@ -889,12 +1012,13 @@ impl PaintState {
let column = if x >= 0.0 {
line.index_for_x(x)
.map(|ix| ix as u32)
.unwrap_or(snapshot.line_len(row))
.unwrap_or_else(|| snapshot.line_len(row))
} else {
0
};
let overshoot = (0f32.max(x - line.width()) / layout.em_advance) as u32;
DisplayPoint::new(row, column)
(DisplayPoint::new(row, column), overshoot)
}
}
@@ -1039,23 +1163,20 @@ fn scale_horizontal_mouse_autoscroll_delta(delta: f32) -> f32 {
#[cfg(test)]
mod tests {
use super::*;
use crate::{
test::sample_text,
{Editor, EditorSettings},
};
use buffer::Buffer;
use crate::{Editor, EditorSettings, MultiBuffer};
use std::sync::Arc;
use util::test::sample_text;
#[gpui::test]
fn test_layout_line_numbers(cx: &mut gpui::MutableAppContext) {
let settings = EditorSettings::test(cx);
let buffer = cx.add_model(|cx| Buffer::new(0, sample_text(6, 6), cx));
let buffer = MultiBuffer::build_simple(&sample_text(6, 6, 'a'), cx);
let (window_id, editor) = cx.add_window(Default::default(), |cx| {
Editor::for_buffer(
buffer,
{
let settings = settings.clone();
move |_| settings.clone()
Arc::new(move |_| settings.clone())
},
cx,
)
@@ -1066,7 +1187,7 @@ mod tests {
let snapshot = editor.snapshot(cx);
let mut presenter = cx.build_presenter(window_id, 30.);
let mut layout_cx = presenter.build_layout_context(false, cx);
element.layout_line_numbers(0..6, &Default::default(), &snapshot, &mut layout_cx)
element.layout_rows(0..6, &Default::default(), &snapshot, &mut layout_cx)
});
assert_eq!(layouts.len(), 6);
}

390
crates/editor/src/items.rs Normal file
View File

@@ -0,0 +1,390 @@
use crate::{Autoscroll, Editor, Event};
use crate::{MultiBuffer, ToPoint as _};
use anyhow::Result;
use gpui::{
elements::*, AppContext, Entity, ModelContext, ModelHandle, MutableAppContext, RenderContext,
Subscription, Task, View, ViewContext, ViewHandle, WeakModelHandle,
};
use language::{Buffer, Diagnostic, File as _};
use postage::watch;
use project::{File, ProjectPath, Worktree};
use std::fmt::Write;
use std::path::Path;
use text::{Point, Selection};
use util::TryFutureExt;
use workspace::{
ItemHandle, ItemView, ItemViewHandle, PathOpener, Settings, StatusItemView, WeakItemHandle,
Workspace,
};
pub struct BufferOpener;
#[derive(Clone)]
pub struct BufferItemHandle(pub ModelHandle<Buffer>);
#[derive(Clone)]
struct WeakBufferItemHandle(WeakModelHandle<Buffer>);
impl PathOpener for BufferOpener {
fn open(
&self,
worktree: &mut Worktree,
project_path: ProjectPath,
cx: &mut ModelContext<Worktree>,
) -> Option<Task<Result<Box<dyn ItemHandle>>>> {
let buffer = worktree.open_buffer(project_path.path, cx);
let task = cx.spawn(|_, _| async move {
let buffer = buffer.await?;
Ok(Box::new(BufferItemHandle(buffer)) as Box<dyn ItemHandle>)
});
Some(task)
}
}
impl ItemHandle for BufferItemHandle {
fn add_view(
&self,
window_id: usize,
workspace: &Workspace,
cx: &mut MutableAppContext,
) -> Box<dyn ItemViewHandle> {
let buffer = cx.add_model(|cx| MultiBuffer::singleton(self.0.clone(), cx));
let weak_buffer = buffer.downgrade();
Box::new(cx.add_view(window_id, |cx| {
Editor::for_buffer(
buffer,
crate::settings_builder(weak_buffer, workspace.settings()),
cx,
)
}))
}
fn boxed_clone(&self) -> Box<dyn ItemHandle> {
Box::new(self.clone())
}
fn to_any(&self) -> gpui::AnyModelHandle {
self.0.clone().into()
}
fn downgrade(&self) -> Box<dyn workspace::WeakItemHandle> {
Box::new(WeakBufferItemHandle(self.0.downgrade()))
}
fn project_path(&self, cx: &AppContext) -> Option<ProjectPath> {
File::from_dyn(self.0.read(cx).file()).map(|f| ProjectPath {
worktree_id: f.worktree_id(cx),
path: f.path().clone(),
})
}
fn id(&self) -> usize {
self.0.id()
}
}
impl WeakItemHandle for WeakBufferItemHandle {
fn upgrade(&self, cx: &AppContext) -> Option<Box<dyn ItemHandle>> {
self.0
.upgrade(cx)
.map(|buffer| Box::new(BufferItemHandle(buffer)) as Box<dyn ItemHandle>)
}
fn id(&self) -> usize {
self.0.id()
}
}
impl ItemView for Editor {
type ItemHandle = BufferItemHandle;
fn item_handle(&self, cx: &AppContext) -> Self::ItemHandle {
BufferItemHandle(self.buffer.read(cx).as_singleton().unwrap())
}
fn title(&self, cx: &AppContext) -> String {
let filename = self
.buffer()
.read(cx)
.file(cx)
.and_then(|file| file.file_name());
if let Some(name) = filename {
name.to_string_lossy().into()
} else {
"untitled".into()
}
}
fn project_path(&self, cx: &AppContext) -> Option<ProjectPath> {
File::from_dyn(self.buffer().read(cx).file(cx)).map(|file| ProjectPath {
worktree_id: file.worktree_id(cx),
path: file.path().clone(),
})
}
fn clone_on_split(&self, cx: &mut ViewContext<Self>) -> Option<Self>
where
Self: Sized,
{
Some(self.clone(cx))
}
fn is_dirty(&self, cx: &AppContext) -> bool {
self.buffer().read(cx).read(cx).is_dirty()
}
fn has_conflict(&self, cx: &AppContext) -> bool {
self.buffer().read(cx).read(cx).has_conflict()
}
fn can_save(&self, cx: &AppContext) -> bool {
self.project_path(cx).is_some()
}
fn save(&mut self, cx: &mut ViewContext<Self>) -> Result<Task<Result<()>>> {
let buffer = self.buffer().clone();
Ok(cx.spawn(|editor, mut cx| async move {
buffer
.update(&mut cx, |buffer, cx| buffer.format(cx).log_err())
.await;
editor.update(&mut cx, |editor, cx| {
editor.request_autoscroll(Autoscroll::Fit, cx)
});
buffer
.update(&mut cx, |buffer, cx| buffer.save(cx))?
.await?;
Ok(())
}))
}
fn can_save_as(&self, _: &AppContext) -> bool {
true
}
fn save_as(
&mut self,
worktree: ModelHandle<Worktree>,
path: &Path,
cx: &mut ViewContext<Self>,
) -> Task<Result<()>> {
let buffer = self
.buffer()
.read(cx)
.as_singleton()
.expect("cannot call save_as on an excerpt list")
.clone();
buffer.update(cx, |buffer, cx| {
let handle = cx.handle();
let text = buffer.as_rope().clone();
let version = buffer.version();
let save_as = worktree.update(cx, |worktree, cx| {
worktree
.as_local_mut()
.unwrap()
.save_buffer_as(handle, path, text, cx)
});
cx.spawn(|buffer, mut cx| async move {
save_as.await.map(|new_file| {
let (language, language_server) = worktree.update(&mut cx, |worktree, cx| {
let worktree = worktree.as_local_mut().unwrap();
let language = worktree
.language_registry()
.select_language(new_file.full_path())
.cloned();
let language_server = language
.as_ref()
.and_then(|language| worktree.register_language(language, cx));
(language, language_server.clone())
});
buffer.update(&mut cx, |buffer, cx| {
buffer.did_save(version, new_file.mtime, Some(Box::new(new_file)), cx);
buffer.set_language(language, language_server, cx);
});
})
})
})
}
fn should_activate_item_on_event(event: &Event) -> bool {
matches!(event, Event::Activate)
}
fn should_close_item_on_event(event: &Event) -> bool {
matches!(event, Event::Closed)
}
fn should_update_tab_on_event(event: &Event) -> bool {
matches!(
event,
Event::Saved | Event::Dirtied | Event::FileHandleChanged
)
}
}
pub struct CursorPosition {
position: Option<Point>,
selected_count: usize,
settings: watch::Receiver<Settings>,
_observe_active_editor: Option<Subscription>,
}
impl CursorPosition {
pub fn new(settings: watch::Receiver<Settings>) -> Self {
Self {
position: None,
selected_count: 0,
settings,
_observe_active_editor: None,
}
}
fn update_position(&mut self, editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) {
let editor = editor.read(cx);
let buffer = editor.buffer().read(cx).snapshot(cx);
self.selected_count = 0;
let mut last_selection: Option<Selection<usize>> = None;
for selection in editor.local_selections::<usize>(cx) {
self.selected_count += selection.end - selection.start;
if last_selection
.as_ref()
.map_or(true, |last_selection| selection.id > last_selection.id)
{
last_selection = Some(selection);
}
}
self.position = last_selection.map(|s| s.head().to_point(&buffer));
cx.notify();
}
}
impl Entity for CursorPosition {
type Event = ();
}
impl View for CursorPosition {
fn ui_name() -> &'static str {
"CursorPosition"
}
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
if let Some(position) = self.position {
let theme = &self.settings.borrow().theme.workspace.status_bar;
let mut text = format!("{},{}", position.row + 1, position.column + 1);
if self.selected_count > 0 {
write!(text, " ({} selected)", self.selected_count).unwrap();
}
Label::new(text, theme.cursor_position.clone()).boxed()
} else {
Empty::new().boxed()
}
}
}
impl StatusItemView for CursorPosition {
fn set_active_pane_item(
&mut self,
active_pane_item: Option<&dyn ItemViewHandle>,
cx: &mut ViewContext<Self>,
) {
if let Some(editor) = active_pane_item.and_then(|item| item.to_any().downcast::<Editor>()) {
self._observe_active_editor = Some(cx.observe(&editor, Self::update_position));
self.update_position(editor, cx);
} else {
self.position = None;
self._observe_active_editor = None;
}
cx.notify();
}
}
pub struct DiagnosticMessage {
settings: watch::Receiver<Settings>,
diagnostic: Option<Diagnostic>,
_observe_active_editor: Option<Subscription>,
}
impl DiagnosticMessage {
pub fn new(settings: watch::Receiver<Settings>) -> Self {
Self {
diagnostic: None,
settings,
_observe_active_editor: None,
}
}
fn update(&mut self, editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) {
let editor = editor.read(cx);
let buffer = editor.buffer().read(cx);
let cursor_position = editor.newest_selection::<usize>(&buffer.read(cx)).head();
let new_diagnostic = buffer
.read(cx)
.diagnostics_in_range::<_, usize>(cursor_position..cursor_position)
.filter(|entry| !entry.range.is_empty())
.min_by_key(|entry| (entry.diagnostic.severity, entry.range.len()))
.map(|entry| entry.diagnostic);
if new_diagnostic != self.diagnostic {
self.diagnostic = new_diagnostic;
cx.notify();
}
}
}
impl Entity for DiagnosticMessage {
type Event = ();
}
impl View for DiagnosticMessage {
fn ui_name() -> &'static str {
"DiagnosticMessage"
}
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
if let Some(diagnostic) = &self.diagnostic {
let theme = &self.settings.borrow().theme.workspace.status_bar;
Flex::row()
.with_child(
Svg::new("icons/warning.svg")
.with_color(theme.diagnostic_icon_color)
.constrained()
.with_height(theme.diagnostic_icon_size)
.contained()
.with_margin_right(theme.diagnostic_icon_spacing)
.boxed(),
)
.with_child(
Label::new(
diagnostic.message.lines().next().unwrap().to_string(),
theme.diagnostic_message.clone(),
)
.boxed(),
)
.boxed()
} else {
Empty::new().boxed()
}
}
}
impl StatusItemView for DiagnosticMessage {
fn set_active_pane_item(
&mut self,
active_pane_item: Option<&dyn ItemViewHandle>,
cx: &mut ViewContext<Self>,
) {
if let Some(editor) = active_pane_item.and_then(|item| item.to_any().downcast::<Editor>()) {
self._observe_active_editor = Some(cx.observe(&editor, Self::update));
self.update(editor, cx);
} else {
self.diagnostic = Default::default();
self._observe_active_editor = None;
}
cx.notify();
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,9 @@
use super::{Bias, DisplayMapSnapshot, DisplayPoint, SelectionGoal};
use super::{Bias, DisplayPoint, DisplaySnapshot, SelectionGoal, ToDisplayPoint};
use crate::ToPoint;
use anyhow::Result;
use std::{cmp, ops::Range};
pub fn left(map: &DisplayMapSnapshot, mut point: DisplayPoint) -> Result<DisplayPoint> {
pub fn left(map: &DisplaySnapshot, mut point: DisplayPoint) -> Result<DisplayPoint> {
if point.column() > 0 {
*point.column_mut() -= 1;
} else if point.row() > 0 {
@@ -11,7 +13,7 @@ pub fn left(map: &DisplayMapSnapshot, mut point: DisplayPoint) -> Result<Display
Ok(map.clip_point(point, Bias::Left))
}
pub fn right(map: &DisplayMapSnapshot, mut point: DisplayPoint) -> Result<DisplayPoint> {
pub fn right(map: &DisplaySnapshot, mut point: DisplayPoint) -> Result<DisplayPoint> {
let max_column = map.line_len(point.row());
if point.column() < max_column {
*point.column_mut() += 1;
@@ -23,21 +25,26 @@ pub fn right(map: &DisplayMapSnapshot, mut point: DisplayPoint) -> Result<Displa
}
pub fn up(
map: &DisplayMapSnapshot,
mut point: DisplayPoint,
map: &DisplaySnapshot,
start: DisplayPoint,
goal: SelectionGoal,
) -> Result<(DisplayPoint, SelectionGoal)> {
let goal_column = if let SelectionGoal::Column(column) = goal {
let mut goal_column = if let SelectionGoal::Column(column) = goal {
column
} else {
map.column_to_chars(point.row(), point.column())
map.column_to_chars(start.row(), start.column())
};
if point.row() > 0 {
*point.row_mut() -= 1;
let prev_row = start.row().saturating_sub(1);
let mut point = map.clip_point(
DisplayPoint::new(prev_row, map.line_len(prev_row)),
Bias::Left,
);
if point.row() < start.row() {
*point.column_mut() = map.column_from_chars(point.row(), goal_column);
} else {
point = DisplayPoint::new(0, 0);
goal_column = 0;
}
let clip_bias = if point.column() == map.line_len(point.row()) {
@@ -53,22 +60,23 @@ pub fn up(
}
pub fn down(
map: &DisplayMapSnapshot,
mut point: DisplayPoint,
map: &DisplaySnapshot,
start: DisplayPoint,
goal: SelectionGoal,
) -> Result<(DisplayPoint, SelectionGoal)> {
let max_point = map.max_point();
let goal_column = if let SelectionGoal::Column(column) = goal {
let mut goal_column = if let SelectionGoal::Column(column) = goal {
column
} else {
map.column_to_chars(point.row(), point.column())
map.column_to_chars(start.row(), start.column())
};
if point.row() < max_point.row() {
*point.row_mut() += 1;
let next_row = start.row() + 1;
let mut point = map.clip_point(DisplayPoint::new(next_row, 0), Bias::Right);
if point.row() > start.row() {
*point.column_mut() = map.column_from_chars(point.row(), goal_column);
} else {
point = max_point;
point = map.max_point();
goal_column = map.column_to_chars(point.row(), point.column())
}
let clip_bias = if point.column() == map.line_len(point.row()) {
@@ -84,27 +92,24 @@ pub fn down(
}
pub fn line_beginning(
map: &DisplayMapSnapshot,
map: &DisplaySnapshot,
point: DisplayPoint,
toggle_indent: bool,
) -> Result<DisplayPoint> {
) -> DisplayPoint {
let (indent, is_blank) = map.line_indent(point.row());
if toggle_indent && !is_blank && point.column() != indent {
Ok(DisplayPoint::new(point.row(), indent))
DisplayPoint::new(point.row(), indent)
} else {
Ok(DisplayPoint::new(point.row(), 0))
DisplayPoint::new(point.row(), 0)
}
}
pub fn line_end(map: &DisplayMapSnapshot, point: DisplayPoint) -> Result<DisplayPoint> {
pub fn line_end(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
let line_end = DisplayPoint::new(point.row(), map.line_len(point.row()));
Ok(map.clip_point(line_end, Bias::Left))
map.clip_point(line_end, Bias::Left)
}
pub fn prev_word_boundary(
map: &DisplayMapSnapshot,
mut point: DisplayPoint,
) -> Result<DisplayPoint> {
pub fn prev_word_boundary(map: &DisplaySnapshot, mut point: DisplayPoint) -> DisplayPoint {
let mut line_start = 0;
if point.row() > 0 {
if let Some(indent) = map.soft_wrap_indent(point.row() - 1) {
@@ -114,7 +119,7 @@ pub fn prev_word_boundary(
if point.column() == line_start {
if point.row() == 0 {
return Ok(DisplayPoint::new(0, 0));
return DisplayPoint::new(0, 0);
} else {
let row = point.row() - 1;
point = map.clip_point(DisplayPoint::new(row, map.line_len(row)), Bias::Left);
@@ -140,13 +145,10 @@ pub fn prev_word_boundary(
prev_char_kind = char_kind;
column += c.len_utf8() as u32;
}
Ok(boundary)
boundary
}
pub fn next_word_boundary(
map: &DisplayMapSnapshot,
mut point: DisplayPoint,
) -> Result<DisplayPoint> {
pub fn next_word_boundary(map: &DisplaySnapshot, mut point: DisplayPoint) -> DisplayPoint {
let mut prev_char_kind = None;
for c in map.chars_at(point) {
let char_kind = char_kind(c);
@@ -170,14 +172,54 @@ pub fn next_word_boundary(
}
prev_char_kind = Some(char_kind);
}
Ok(point)
map.clip_point(point, Bias::Right)
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub fn is_inside_word(map: &DisplaySnapshot, point: DisplayPoint) -> bool {
let ix = map.clip_point(point, Bias::Left).to_offset(map, Bias::Left);
let text = &map.buffer_snapshot;
let next_char_kind = text.chars_at(ix).next().map(char_kind);
let prev_char_kind = text.reversed_chars_at(ix).next().map(char_kind);
prev_char_kind.zip(next_char_kind) == Some((CharKind::Word, CharKind::Word))
}
pub fn surrounding_word(map: &DisplaySnapshot, point: DisplayPoint) -> Range<DisplayPoint> {
let mut start = map.clip_point(point, Bias::Left).to_offset(map, Bias::Left);
let mut end = start;
let text = &map.buffer_snapshot;
let mut next_chars = text.chars_at(start).peekable();
let mut prev_chars = text.reversed_chars_at(start).peekable();
let word_kind = cmp::max(
prev_chars.peek().copied().map(char_kind),
next_chars.peek().copied().map(char_kind),
);
for ch in prev_chars {
if Some(char_kind(ch)) == word_kind {
start -= ch.len_utf8();
} else {
break;
}
}
for ch in next_chars {
if Some(char_kind(ch)) == word_kind {
end += ch.len_utf8();
} else {
break;
}
}
start.to_point(&map.buffer_snapshot).to_display_point(map)
..end.to_point(&map.buffer_snapshot).to_display_point(map)
}
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
enum CharKind {
Newline,
Whitespace,
Punctuation,
Whitespace,
Word,
}
@@ -196,7 +238,120 @@ fn char_kind(c: char) -> CharKind {
#[cfg(test)]
mod tests {
use super::*;
use crate::{display_map::DisplayMap, Buffer};
use crate::{
display_map::{BlockDisposition, BlockProperties},
Buffer, DisplayMap, ExcerptProperties, MultiBuffer,
};
use gpui::{elements::Empty, Element};
use language::Point;
use std::sync::Arc;
#[gpui::test]
fn test_move_up_and_down_with_excerpts(cx: &mut gpui::MutableAppContext) {
let family_id = cx.font_cache().load_family(&["Helvetica"]).unwrap();
let font_id = cx
.font_cache()
.select_font(family_id, &Default::default())
.unwrap();
let buffer = cx.add_model(|cx| Buffer::new(0, "abc\ndefg\nhijkl\nmn", cx));
let mut excerpt1_header_position = None;
let mut excerpt2_header_position = None;
let multibuffer = cx.add_model(|cx| {
let mut multibuffer = MultiBuffer::new(0);
let excerpt1_id = multibuffer.push_excerpt(
ExcerptProperties {
buffer: &buffer,
range: Point::new(0, 0)..Point::new(1, 4),
},
cx,
);
let excerpt2_id = multibuffer.push_excerpt(
ExcerptProperties {
buffer: &buffer,
range: Point::new(2, 0)..Point::new(3, 2),
},
cx,
);
excerpt1_header_position = Some(
multibuffer
.read(cx)
.anchor_in_excerpt(excerpt1_id, language::Anchor::min()),
);
excerpt2_header_position = Some(
multibuffer
.read(cx)
.anchor_in_excerpt(excerpt2_id, language::Anchor::min()),
);
multibuffer
});
let display_map =
cx.add_model(|cx| DisplayMap::new(multibuffer, 2, font_id, 14.0, None, cx));
display_map.update(cx, |display_map, cx| {
display_map.insert_blocks(
[
BlockProperties {
position: excerpt1_header_position.unwrap(),
height: 2,
render: Arc::new(|_| Empty::new().boxed()),
disposition: BlockDisposition::Above,
},
BlockProperties {
position: excerpt2_header_position.unwrap(),
height: 3,
render: Arc::new(|_| Empty::new().boxed()),
disposition: BlockDisposition::Above,
},
],
cx,
)
});
let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
assert_eq!(snapshot.text(), "\n\nabc\ndefg\n\n\n\nhijkl\nmn");
// Can't move up into the first excerpt's header
assert_eq!(
up(&snapshot, DisplayPoint::new(2, 2), SelectionGoal::Column(2)).unwrap(),
(DisplayPoint::new(2, 0), SelectionGoal::Column(0)),
);
assert_eq!(
up(&snapshot, DisplayPoint::new(2, 0), SelectionGoal::None).unwrap(),
(DisplayPoint::new(2, 0), SelectionGoal::Column(0)),
);
// Move up and down within first excerpt
assert_eq!(
up(&snapshot, DisplayPoint::new(3, 4), SelectionGoal::Column(4)).unwrap(),
(DisplayPoint::new(2, 3), SelectionGoal::Column(4)),
);
assert_eq!(
down(&snapshot, DisplayPoint::new(2, 3), SelectionGoal::Column(4)).unwrap(),
(DisplayPoint::new(3, 4), SelectionGoal::Column(4)),
);
// Move up and down across second excerpt's header
assert_eq!(
up(&snapshot, DisplayPoint::new(7, 5), SelectionGoal::Column(5)).unwrap(),
(DisplayPoint::new(3, 4), SelectionGoal::Column(5)),
);
assert_eq!(
down(&snapshot, DisplayPoint::new(3, 4), SelectionGoal::Column(5)).unwrap(),
(DisplayPoint::new(7, 5), SelectionGoal::Column(5)),
);
// Can't move down off the end
assert_eq!(
down(&snapshot, DisplayPoint::new(8, 0), SelectionGoal::Column(0)).unwrap(),
(DisplayPoint::new(8, 2), SelectionGoal::Column(2)),
);
assert_eq!(
down(&snapshot, DisplayPoint::new(8, 2), SelectionGoal::Column(2)).unwrap(),
(DisplayPoint::new(8, 2), SelectionGoal::Column(2)),
);
}
#[gpui::test]
fn test_prev_next_word_boundary_multibyte(cx: &mut gpui::MutableAppContext) {
@@ -208,50 +363,122 @@ mod tests {
.unwrap();
let font_size = 14.0;
let buffer = cx.add_model(|cx| Buffer::new(0, "a bcΔ defγ hi—jk", cx));
let buffer = MultiBuffer::build_simple("a bcΔ defγ hi—jk", cx);
let display_map =
cx.add_model(|cx| DisplayMap::new(buffer, tab_size, font_id, font_size, None, cx));
let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
assert_eq!(
prev_word_boundary(&snapshot, DisplayPoint::new(0, 12)).unwrap(),
prev_word_boundary(&snapshot, DisplayPoint::new(0, 12)),
DisplayPoint::new(0, 7)
);
assert_eq!(
prev_word_boundary(&snapshot, DisplayPoint::new(0, 7)).unwrap(),
prev_word_boundary(&snapshot, DisplayPoint::new(0, 7)),
DisplayPoint::new(0, 2)
);
assert_eq!(
prev_word_boundary(&snapshot, DisplayPoint::new(0, 6)).unwrap(),
prev_word_boundary(&snapshot, DisplayPoint::new(0, 6)),
DisplayPoint::new(0, 2)
);
assert_eq!(
prev_word_boundary(&snapshot, DisplayPoint::new(0, 2)).unwrap(),
prev_word_boundary(&snapshot, DisplayPoint::new(0, 2)),
DisplayPoint::new(0, 0)
);
assert_eq!(
prev_word_boundary(&snapshot, DisplayPoint::new(0, 1)).unwrap(),
prev_word_boundary(&snapshot, DisplayPoint::new(0, 1)),
DisplayPoint::new(0, 0)
);
assert_eq!(
next_word_boundary(&snapshot, DisplayPoint::new(0, 0)).unwrap(),
next_word_boundary(&snapshot, DisplayPoint::new(0, 0)),
DisplayPoint::new(0, 1)
);
assert_eq!(
next_word_boundary(&snapshot, DisplayPoint::new(0, 1)).unwrap(),
next_word_boundary(&snapshot, DisplayPoint::new(0, 1)),
DisplayPoint::new(0, 6)
);
assert_eq!(
next_word_boundary(&snapshot, DisplayPoint::new(0, 2)).unwrap(),
next_word_boundary(&snapshot, DisplayPoint::new(0, 2)),
DisplayPoint::new(0, 6)
);
assert_eq!(
next_word_boundary(&snapshot, DisplayPoint::new(0, 6)).unwrap(),
next_word_boundary(&snapshot, DisplayPoint::new(0, 6)),
DisplayPoint::new(0, 12)
);
assert_eq!(
next_word_boundary(&snapshot, DisplayPoint::new(0, 7)).unwrap(),
next_word_boundary(&snapshot, DisplayPoint::new(0, 7)),
DisplayPoint::new(0, 12)
);
}
#[gpui::test]
fn test_surrounding_word(cx: &mut gpui::MutableAppContext) {
let tab_size = 4;
let family_id = cx.font_cache().load_family(&["Helvetica"]).unwrap();
let font_id = cx
.font_cache()
.select_font(family_id, &Default::default())
.unwrap();
let font_size = 14.0;
let buffer = MultiBuffer::build_simple("lorem ipsum dolor\n sit", cx);
let display_map =
cx.add_model(|cx| DisplayMap::new(buffer, tab_size, font_id, font_size, None, cx));
let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
assert_eq!(
surrounding_word(&snapshot, DisplayPoint::new(0, 0)),
DisplayPoint::new(0, 0)..DisplayPoint::new(0, 5)
);
assert_eq!(
surrounding_word(&snapshot, DisplayPoint::new(0, 2)),
DisplayPoint::new(0, 0)..DisplayPoint::new(0, 5)
);
assert_eq!(
surrounding_word(&snapshot, DisplayPoint::new(0, 5)),
DisplayPoint::new(0, 0)..DisplayPoint::new(0, 5)
);
assert_eq!(
surrounding_word(&snapshot, DisplayPoint::new(0, 6)),
DisplayPoint::new(0, 6)..DisplayPoint::new(0, 11)
);
assert_eq!(
surrounding_word(&snapshot, DisplayPoint::new(0, 7)),
DisplayPoint::new(0, 6)..DisplayPoint::new(0, 11)
);
assert_eq!(
surrounding_word(&snapshot, DisplayPoint::new(0, 11)),
DisplayPoint::new(0, 6)..DisplayPoint::new(0, 11)
);
assert_eq!(
surrounding_word(&snapshot, DisplayPoint::new(0, 13)),
DisplayPoint::new(0, 11)..DisplayPoint::new(0, 14)
);
assert_eq!(
surrounding_word(&snapshot, DisplayPoint::new(0, 14)),
DisplayPoint::new(0, 14)..DisplayPoint::new(0, 19)
);
assert_eq!(
surrounding_word(&snapshot, DisplayPoint::new(0, 17)),
DisplayPoint::new(0, 14)..DisplayPoint::new(0, 19)
);
assert_eq!(
surrounding_word(&snapshot, DisplayPoint::new(0, 19)),
DisplayPoint::new(0, 14)..DisplayPoint::new(0, 19)
);
assert_eq!(
surrounding_word(&snapshot, DisplayPoint::new(1, 0)),
DisplayPoint::new(1, 0)..DisplayPoint::new(1, 4)
);
assert_eq!(
surrounding_word(&snapshot, DisplayPoint::new(1, 1)),
DisplayPoint::new(1, 0)..DisplayPoint::new(1, 4)
);
assert_eq!(
surrounding_word(&snapshot, DisplayPoint::new(1, 6)),
DisplayPoint::new(1, 4)..DisplayPoint::new(1, 7)
);
assert_eq!(
surrounding_word(&snapshot, DisplayPoint::new(1, 7)),
DisplayPoint::new(1, 4)..DisplayPoint::new(1, 7)
);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,141 @@
use super::{ExcerptId, MultiBufferSnapshot, ToOffset, ToPoint};
use anyhow::Result;
use std::{
cmp::Ordering,
ops::{Range, Sub},
};
use sum_tree::Bias;
use text::{rope::TextDimension, Point};
#[derive(Clone, Eq, PartialEq, Debug, Hash)]
pub struct Anchor {
pub(crate) buffer_id: usize,
pub(crate) excerpt_id: ExcerptId,
pub(crate) text_anchor: text::Anchor,
}
impl Anchor {
pub fn min() -> Self {
Self {
buffer_id: 0,
excerpt_id: ExcerptId::min(),
text_anchor: text::Anchor::min(),
}
}
pub fn max() -> Self {
Self {
buffer_id: 0,
excerpt_id: ExcerptId::max(),
text_anchor: text::Anchor::max(),
}
}
pub fn excerpt_id(&self) -> &ExcerptId {
&self.excerpt_id
}
pub fn cmp<'a>(&self, other: &Anchor, snapshot: &MultiBufferSnapshot) -> Result<Ordering> {
let excerpt_id_cmp = self.excerpt_id.cmp(&other.excerpt_id);
if excerpt_id_cmp.is_eq() {
if self.excerpt_id == ExcerptId::min() || self.excerpt_id == ExcerptId::max() {
Ok(Ordering::Equal)
} else if let Some((buffer_id, buffer_snapshot)) =
snapshot.buffer_snapshot_for_excerpt(&self.excerpt_id)
{
// Even though the anchor refers to a valid excerpt the underlying buffer might have
// changed. In that case, treat the anchor as if it were at the start of that
// excerpt.
if self.buffer_id == buffer_id && other.buffer_id == buffer_id {
self.text_anchor.cmp(&other.text_anchor, buffer_snapshot)
} else if self.buffer_id == buffer_id {
Ok(Ordering::Greater)
} else if other.buffer_id == buffer_id {
Ok(Ordering::Less)
} else {
Ok(Ordering::Equal)
}
} else {
Ok(Ordering::Equal)
}
} else {
Ok(excerpt_id_cmp)
}
}
pub fn bias_left(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
if self.text_anchor.bias != Bias::Left {
if let Some((buffer_id, buffer_snapshot)) =
snapshot.buffer_snapshot_for_excerpt(&self.excerpt_id)
{
if self.buffer_id == buffer_id {
return Self {
buffer_id: self.buffer_id,
excerpt_id: self.excerpt_id.clone(),
text_anchor: self.text_anchor.bias_left(buffer_snapshot),
};
}
}
}
self.clone()
}
pub fn bias_right(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
if self.text_anchor.bias != Bias::Right {
if let Some((buffer_id, buffer_snapshot)) =
snapshot.buffer_snapshot_for_excerpt(&self.excerpt_id)
{
if self.buffer_id == buffer_id {
return Self {
buffer_id: self.buffer_id,
excerpt_id: self.excerpt_id.clone(),
text_anchor: self.text_anchor.bias_right(buffer_snapshot),
};
}
}
}
self.clone()
}
pub fn summary<D>(&self, snapshot: &MultiBufferSnapshot) -> D
where
D: TextDimension + Ord + Sub<D, Output = D>,
{
snapshot.summary_for_anchor(self)
}
}
impl ToOffset for Anchor {
fn to_offset<'a>(&self, snapshot: &MultiBufferSnapshot) -> usize {
self.summary(snapshot)
}
}
impl ToPoint for Anchor {
fn to_point<'a>(&self, snapshot: &MultiBufferSnapshot) -> Point {
self.summary(snapshot)
}
}
pub trait AnchorRangeExt {
fn cmp(&self, b: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Result<Ordering>;
fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<usize>;
fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point>;
}
impl AnchorRangeExt for Range<Anchor> {
fn cmp(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Result<Ordering> {
Ok(match self.start.cmp(&other.start, buffer)? {
Ordering::Equal => other.end.cmp(&self.end, buffer)?,
ord @ _ => ord,
})
}
fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<usize> {
self.start.to_offset(&content)..self.end.to_offset(&content)
}
fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point> {
self.start.to_point(&content)..self.end.to_point(&content)
}
}

View File

@@ -1,39 +1,6 @@
use gpui::{Entity, ModelHandle};
use smol::channel;
use std::marker::PhantomData;
pub fn sample_text(rows: usize, cols: usize) -> String {
let mut text = String::new();
for row in 0..rows {
let c: char = ('a' as u32 + row as u32) as u8 as char;
let mut line = c.to_string().repeat(cols);
if row < rows - 1 {
line.push('\n');
}
text += &line;
}
text
}
pub struct Observer<T>(PhantomData<T>);
impl<T: 'static> Entity for Observer<T> {
type Event = ();
}
impl<T: Entity> Observer<T> {
pub fn new(
handle: &ModelHandle<T>,
cx: &mut gpui::TestAppContext,
) -> (ModelHandle<Self>, channel::Receiver<()>) {
let (notify_tx, notify_rx) = channel::unbounded();
let observer = cx.add_model(|cx| {
cx.observe(handle, move |_, _, _| {
let _ = notify_tx.try_send(());
})
.detach();
Observer(PhantomData)
});
(observer, notify_rx)
}
#[cfg(test)]
#[ctor::ctor]
fn init_logger() {
// std::env::set_var("RUST_LOG", "info");
env_logger::init();
}

View File

@@ -3,6 +3,9 @@ name = "file_finder"
version = "0.1.0"
edition = "2018"
[lib]
path = "src/file_finder.rs"
[dependencies]
editor = { path = "../editor" }
fuzzy = { path = "../fuzzy" }
@@ -14,5 +17,6 @@ workspace = { path = "../workspace" }
postage = { version = "0.4.1", features = ["futures-traits"] }
[dev-dependencies]
gpui = { path = "../gpui", features = ["test-support"] }
serde_json = { version = "1.0.64", features = ["preserve_order"] }
workspace = { path = "../workspace", features = ["test-support"] }

View File

@@ -3,16 +3,12 @@ use fuzzy::PathMatch;
use gpui::{
action,
elements::*,
keymap::{
self,
menu::{SelectNext, SelectPrev},
Binding,
},
keymap::{self, Binding},
AppContext, Axis, Entity, ModelHandle, MutableAppContext, RenderContext, Task, View,
ViewContext, ViewHandle, WeakViewHandle,
};
use postage::watch;
use project::{Project, ProjectPath};
use project::{Project, ProjectPath, WorktreeId};
use std::{
cmp,
path::Path,
@@ -22,7 +18,10 @@ use std::{
},
};
use util::post_inc;
use workspace::{Settings, Workspace};
use workspace::{
menu::{Confirm, SelectNext, SelectPrev},
Settings, Workspace,
};
pub struct FileFinder {
handle: WeakViewHandle<Self>,
@@ -40,7 +39,6 @@ pub struct FileFinder {
}
action!(Toggle);
action!(Confirm);
action!(Select, ProjectPath);
pub fn init(cx: &mut MutableAppContext) {
@@ -53,7 +51,6 @@ pub fn init(cx: &mut MutableAppContext) {
cx.add_bindings(vec![
Binding::new("cmd-p", Toggle, None),
Binding::new("escape", Toggle, Some("FileFinder")),
Binding::new("enter", Confirm, Some("FileFinder")),
]);
}
@@ -83,7 +80,7 @@ impl View for FileFinder {
.with_style(settings.theme.selector.input_editor.container)
.boxed(),
)
.with_child(Flexible::new(1.0, self.render_matches()).boxed())
.with_child(Flexible::new(1.0, false, self.render_matches()).boxed())
.boxed(),
)
.with_style(settings.theme.selector.container)
@@ -175,6 +172,7 @@ impl FileFinder {
.with_child(
Flexible::new(
1.0,
false,
Flex::column()
.with_child(
Label::new(file_name.to_string(), style.label.clone())
@@ -195,7 +193,7 @@ impl FileFinder {
.with_style(style.container);
let action = Select(ProjectPath {
worktree_id: path_match.worktree_id,
worktree_id: WorktreeId::from_usize(path_match.worktree_id),
path: path_match.path.clone(),
});
EventHandler::new(container.boxed())
@@ -249,8 +247,8 @@ impl FileFinder {
match event {
Event::Selected(project_path) => {
workspace
.open_entry(project_path.clone(), cx)
.map(|d| d.detach());
.open_path(project_path.clone(), cx)
.detach_and_log_err(cx);
workspace.dismiss_modal(cx);
}
Event::Dismissed => {
@@ -270,13 +268,14 @@ impl FileFinder {
Editor::single_line(
{
let settings = settings.clone();
move |_| {
Arc::new(move |_| {
let settings = settings.borrow();
EditorSettings {
style: settings.theme.selector.input_editor.as_editor(),
tab_size: settings.tab_size,
soft_wrap: editor::SoftWrap::None,
}
}
})
},
cx,
)
@@ -285,7 +284,7 @@ impl FileFinder {
.detach();
Self {
handle: cx.handle().downgrade(),
handle: cx.weak_handle(),
settings,
project,
query_editor,
@@ -351,7 +350,8 @@ impl FileFinder {
let mat = &self.matches[selected_index];
self.selected = Some((mat.worktree_id, mat.path.clone()));
}
self.list_state.scroll_to(selected_index);
self.list_state
.scroll_to(ScrollTarget::Show(selected_index));
cx.notify();
}
@@ -362,14 +362,15 @@ impl FileFinder {
let mat = &self.matches[selected_index];
self.selected = Some((mat.worktree_id, mat.path.clone()));
}
self.list_state.scroll_to(selected_index);
self.list_state
.scroll_to(ScrollTarget::Show(selected_index));
cx.notify();
}
fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
if let Some(m) = self.matches.get(self.selected_index()) {
cx.emit(Event::Selected(ProjectPath {
worktree_id: m.worktree_id,
worktree_id: WorktreeId::from_usize(m.worktree_id),
path: m.path.clone(),
}));
}
@@ -413,7 +414,8 @@ impl FileFinder {
}
self.latest_search_query = query;
self.latest_search_did_cancel = did_cancel;
self.list_state.scroll_to(self.selected_index());
self.list_state
.scroll_to(ScrollTarget::Show(self.selected_index()));
cx.notify();
}
}
@@ -429,7 +431,14 @@ mod tests {
#[gpui::test]
async fn test_matching_paths(mut cx: gpui::TestAppContext) {
let params = cx.update(WorkspaceParams::test);
let mut path_openers = Vec::new();
cx.update(|cx| {
super::init(cx);
editor::init(cx, &mut path_openers);
});
let mut params = cx.update(WorkspaceParams::test);
params.path_openers = Arc::from(path_openers);
params
.fs
.as_fake()
@@ -443,10 +452,6 @@ mod tests {
}),
)
.await;
cx.update(|cx| {
super::init(cx);
editor::init(cx);
});
let (window_id, workspace) = cx.add_window(|cx| Workspace::new(&params, cx));
workspace

View File

@@ -4,6 +4,9 @@ version = "2.0.2"
license = "MIT"
edition = "2018"
[lib]
path = "src/fsevent.rs"
[dependencies]
bitflags = "1"
fsevent-sys = "3.0.2"

View File

@@ -3,6 +3,9 @@ name = "fuzzy"
version = "0.1.0"
edition = "2018"
[lib]
path = "src/fuzzy.rs"
[dependencies]
gpui = { path = "../gpui" }
util = { path = "../util" }

View File

@@ -9,6 +9,7 @@ impl CharBag {
}
fn insert(&mut self, c: char) {
let c = c.to_ascii_lowercase();
if c >= 'a' && c <= 'z' {
let mut count = self.0;
let idx = c as u8 - 'a' as u8;

View File

@@ -55,6 +55,7 @@ pub struct PathMatch {
#[derive(Clone, Debug)]
pub struct StringMatchCandidate {
pub id: usize,
pub string: String,
pub char_bag: CharBag,
}
@@ -109,6 +110,7 @@ impl<'a> MatchCandidate for &'a StringMatchCandidate {
#[derive(Clone, Debug)]
pub struct StringMatch {
pub candidate_id: usize,
pub score: f64,
pub positions: Vec<usize>,
pub string: String,
@@ -116,7 +118,7 @@ pub struct StringMatch {
impl PartialEq for StringMatch {
fn eq(&self, other: &Self) -> bool {
self.score.eq(&other.score)
self.cmp(other).is_eq()
}
}
@@ -133,13 +135,13 @@ impl Ord for StringMatch {
self.score
.partial_cmp(&other.score)
.unwrap_or(Ordering::Equal)
.then_with(|| self.string.cmp(&other.string))
.then_with(|| self.candidate_id.cmp(&other.candidate_id))
}
}
impl PartialEq for PathMatch {
fn eq(&self, other: &Self) -> bool {
self.score.eq(&other.score)
self.cmp(other).is_eq()
}
}
@@ -187,8 +189,8 @@ pub async fn match_strings(
for (segment_idx, results) in segment_results.iter_mut().enumerate() {
let cancel_flag = &cancel_flag;
scope.spawn(async move {
let segment_start = segment_idx * segment_size;
let segment_end = segment_start + segment_size;
let segment_start = cmp::min(segment_idx * segment_size, candidates.len());
let segment_end = cmp::min(segment_start + segment_size, candidates.len());
let mut matcher = Matcher::new(
query,
lowercase_query,
@@ -330,6 +332,7 @@ impl<'a> Matcher<'a> {
results,
cancel_flag,
|candidate, score| StringMatch {
candidate_id: candidate.id,
score,
positions: Vec::new(),
string: candidate.string.to_string(),
@@ -433,13 +436,17 @@ impl<'a> Matcher<'a> {
}
}
fn find_last_positions(&mut self, prefix: &[char], path: &[char]) -> bool {
let mut path = path.iter();
let mut prefix_iter = prefix.iter();
for (i, char) in self.query.iter().enumerate().rev() {
if let Some(j) = path.rposition(|c| c == char) {
self.last_positions[i] = j + prefix.len();
} else if let Some(j) = prefix_iter.rposition(|c| c == char) {
fn find_last_positions(
&mut self,
lowercase_prefix: &[char],
lowercase_candidate: &[char],
) -> bool {
let mut lowercase_prefix = lowercase_prefix.iter();
let mut lowercase_candidate = lowercase_candidate.iter();
for (i, char) in self.lowercase_query.iter().enumerate().rev() {
if let Some(j) = lowercase_candidate.rposition(|c| c == char) {
self.last_positions[i] = j + lowercase_prefix.len();
} else if let Some(j) = lowercase_prefix.rposition(|c| c == char) {
self.last_positions[i] = j;
} else {
return false;

View File

@@ -0,0 +1,14 @@
[package]
name = "go_to_line"
version = "0.1.0"
edition = "2018"
[lib]
path = "src/go_to_line.rs"
[dependencies]
text = { path = "../text" }
editor = { path = "../editor" }
gpui = { path = "../gpui" }
workspace = { path = "../workspace" }
postage = { version = "0.4", features = ["futures-traits"] }

View File

@@ -0,0 +1,227 @@
use editor::{display_map::ToDisplayPoint, Autoscroll, Editor, EditorSettings};
use gpui::{
action, elements::*, geometry::vector::Vector2F, keymap::Binding, Axis, Entity,
MutableAppContext, RenderContext, View, ViewContext, ViewHandle,
};
use postage::watch;
use std::sync::Arc;
use text::{Bias, Point, Selection};
use workspace::{Settings, Workspace};
action!(Toggle);
action!(Confirm);
pub fn init(cx: &mut MutableAppContext) {
cx.add_bindings([
Binding::new("ctrl-g", Toggle, Some("Editor")),
Binding::new("escape", Toggle, Some("GoToLine")),
Binding::new("enter", Confirm, Some("GoToLine")),
]);
cx.add_action(GoToLine::toggle);
cx.add_action(GoToLine::confirm);
}
pub struct GoToLine {
settings: watch::Receiver<Settings>,
line_editor: ViewHandle<Editor>,
active_editor: ViewHandle<Editor>,
restore_state: Option<RestoreState>,
line_selection_id: Option<usize>,
cursor_point: Point,
max_point: Point,
}
struct RestoreState {
scroll_position: Vector2F,
selections: Vec<Selection<usize>>,
}
pub enum Event {
Dismissed,
}
impl GoToLine {
pub fn new(
active_editor: ViewHandle<Editor>,
settings: watch::Receiver<Settings>,
cx: &mut ViewContext<Self>,
) -> Self {
let line_editor = cx.add_view(|cx| {
Editor::single_line(
{
let settings = settings.clone();
Arc::new(move |_| {
let settings = settings.borrow();
EditorSettings {
tab_size: settings.tab_size,
style: settings.theme.selector.input_editor.as_editor(),
soft_wrap: editor::SoftWrap::None,
}
})
},
cx,
)
});
cx.subscribe(&line_editor, Self::on_line_editor_event)
.detach();
let (restore_state, cursor_point, max_point) = active_editor.update(cx, |editor, cx| {
let restore_state = Some(RestoreState {
scroll_position: editor.scroll_position(cx),
selections: editor.local_selections::<usize>(cx),
});
let buffer = editor.buffer().read(cx).read(cx);
(
restore_state,
editor.newest_selection(&buffer).head(),
buffer.max_point(),
)
});
Self {
settings: settings.clone(),
line_editor,
active_editor,
restore_state,
line_selection_id: None,
cursor_point,
max_point,
}
}
fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) {
workspace.toggle_modal(cx, |cx, workspace| {
let editor = workspace
.active_item(cx)
.unwrap()
.to_any()
.downcast::<Editor>()
.unwrap();
let view = cx.add_view(|cx| GoToLine::new(editor, workspace.settings.clone(), cx));
cx.subscribe(&view, Self::on_event).detach();
view
});
}
fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
self.restore_state.take();
cx.emit(Event::Dismissed);
}
fn on_event(
workspace: &mut Workspace,
_: ViewHandle<Self>,
event: &Event,
cx: &mut ViewContext<Workspace>,
) {
match event {
Event::Dismissed => workspace.dismiss_modal(cx),
}
}
fn on_line_editor_event(
&mut self,
_: ViewHandle<Editor>,
event: &editor::Event,
cx: &mut ViewContext<Self>,
) {
match event {
editor::Event::Blurred => cx.emit(Event::Dismissed),
editor::Event::Edited => {
let line_editor = self.line_editor.read(cx).buffer().read(cx).read(cx).text();
let mut components = line_editor.trim().split(&[',', ':'][..]);
let row = components.next().and_then(|row| row.parse::<u32>().ok());
let column = components.next().and_then(|row| row.parse::<u32>().ok());
if let Some(point) = row.map(|row| {
Point::new(
row.saturating_sub(1),
column.map(|column| column.saturating_sub(1)).unwrap_or(0),
)
}) {
self.line_selection_id = self.active_editor.update(cx, |active_editor, cx| {
let snapshot = active_editor.snapshot(cx).display_snapshot;
let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
let display_point = point.to_display_point(&snapshot);
let row = display_point.row();
active_editor.select_ranges([point..point], Some(Autoscroll::Center), cx);
active_editor.set_highlighted_rows(Some(row..row + 1));
Some(
active_editor
.newest_selection::<usize>(&snapshot.buffer_snapshot)
.id,
)
});
cx.notify();
}
}
_ => {}
}
}
}
impl Entity for GoToLine {
type Event = Event;
fn release(&mut self, cx: &mut MutableAppContext) {
let line_selection_id = self.line_selection_id.take();
let restore_state = self.restore_state.take();
self.active_editor.update(cx, |editor, cx| {
editor.set_highlighted_rows(None);
if let Some((line_selection_id, restore_state)) = line_selection_id.zip(restore_state) {
let newest_selection =
editor.newest_selection::<usize>(&editor.buffer().read(cx).read(cx));
if line_selection_id == newest_selection.id {
editor.set_scroll_position(restore_state.scroll_position, cx);
editor.update_selections(restore_state.selections, None, cx);
}
}
})
}
}
impl View for GoToLine {
fn ui_name() -> &'static str {
"GoToLine"
}
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
let theme = &self.settings.borrow().theme.selector;
let label = format!(
"{},{} of {} lines",
self.cursor_point.row + 1,
self.cursor_point.column + 1,
self.max_point.row + 1
);
Align::new(
ConstrainedBox::new(
Container::new(
Flex::new(Axis::Vertical)
.with_child(
Container::new(ChildView::new(self.line_editor.id()).boxed())
.with_style(theme.input_editor.container)
.boxed(),
)
.with_child(
Container::new(Label::new(label, theme.empty.label.clone()).boxed())
.with_style(theme.empty.container)
.boxed(),
)
.boxed(),
)
.with_style(theme.container)
.boxed(),
)
.with_max_width(500.0)
.boxed(),
)
.top()
.named("go to line")
}
fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
cx.focus(&self.line_editor);
}
}

View File

@@ -4,6 +4,9 @@ edition = "2018"
name = "gpui"
version = "0.1.0"
[lib]
path = "src/gpui.rs"
[features]
test-support = ["env_logger"]
@@ -15,6 +18,7 @@ backtrace = "0.3"
ctor = "0.1"
env_logger = { version = "0.8", optional = true }
etagere = "0.2"
futures = "0.3"
image = "0.23"
lazy_static = "1.4.0"
log = "0.4"
@@ -34,7 +38,7 @@ smallvec = { version = "1.6", features = ["union"] }
smol = "1.2"
time = { version = "0.3" }
tiny-skia = "0.5"
tree-sitter = "0.19"
tree-sitter = "0.20"
usvg = "0.14"
waker-fn = "1.1.0"

View File

@@ -62,7 +62,7 @@ impl gpui::Element for TextElement {
.select_font(family, &Default::default())
.unwrap(),
color: Color::default(),
underline: false,
underline: None,
};
let bold = RunStyle {
font_id: cx
@@ -76,7 +76,7 @@ impl gpui::Element for TextElement {
)
.unwrap(),
color: Color::default(),
underline: false,
underline: None,
};
let text = "Hello world!";

View File

@@ -14,7 +14,7 @@ include = ["bindings/rust/*", "grammar.js", "queries/*", "src/*"]
path = "bindings/rust/lib.rs"
[dependencies]
tree-sitter = "0.19.3"
tree-sitter = "0.20"
[build-dependencies]
cc = "1.0"

View File

@@ -23,6 +23,7 @@ use std::{
mem,
ops::{Deref, DerefMut},
path::{Path, PathBuf},
pin::Pin,
rc::{self, Rc},
sync::{
atomic::{AtomicUsize, Ordering::SeqCst},
@@ -35,6 +36,12 @@ pub trait Entity: 'static {
type Event;
fn release(&mut self, _: &mut MutableAppContext) {}
fn app_will_quit(
&mut self,
_: &mut MutableAppContext,
) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>> {
None
}
}
pub trait View: Entity + Sized {
@@ -198,8 +205,6 @@ pub struct App(Rc<RefCell<MutableAppContext>>);
#[derive(Clone)]
pub struct AsyncAppContext(Rc<RefCell<MutableAppContext>>);
pub struct BackgroundAppContext(*const RefCell<MutableAppContext>);
#[derive(Clone)]
pub struct TestAppContext {
cx: Rc<RefCell<MutableAppContext>>,
@@ -220,20 +225,29 @@ impl App {
asset_source,
))));
let cx = app.0.clone();
foreground_platform.on_menu_command(Box::new(move |action| {
let mut cx = cx.borrow_mut();
if let Some(key_window_id) = cx.cx.platform.key_window_id() {
if let Some((presenter, _)) = cx.presenters_and_platform_windows.get(&key_window_id)
{
let presenter = presenter.clone();
let path = presenter.borrow().dispatch_path(cx.as_ref());
cx.dispatch_action_any(key_window_id, &path, action);
foreground_platform.on_quit(Box::new({
let cx = app.0.clone();
move || {
cx.borrow_mut().quit();
}
}));
foreground_platform.on_menu_command(Box::new({
let cx = app.0.clone();
move |action| {
let mut cx = cx.borrow_mut();
if let Some(key_window_id) = cx.cx.platform.key_window_id() {
if let Some((presenter, _)) =
cx.presenters_and_platform_windows.get(&key_window_id)
{
let presenter = presenter.clone();
let path = presenter.borrow().dispatch_path(cx.as_ref());
cx.dispatch_action_any(key_window_id, &path, action);
} else {
cx.dispatch_global_action_any(action);
}
} else {
cx.dispatch_global_action_any(action);
}
} else {
cx.dispatch_global_action_any(action);
}
}));
@@ -265,6 +279,18 @@ impl App {
self
}
pub fn on_quit<F>(self, mut callback: F) -> Self
where
F: 'static + FnMut(&mut MutableAppContext),
{
let cx = self.0.clone();
self.0
.borrow_mut()
.foreground_platform
.on_quit(Box::new(move || callback(&mut *cx.borrow_mut())));
self
}
pub fn on_event<F>(self, mut callback: F) -> Self
where
F: 'static + FnMut(Event, &mut MutableAppContext) -> bool,
@@ -316,10 +342,8 @@ impl App {
fn update<T, F: FnOnce(&mut MutableAppContext) -> T>(&mut self, callback: F) -> T {
let mut state = self.0.borrow_mut();
state.pending_flushes += 1;
let result = callback(&mut *state);
let result = state.update(callback);
state.pending_notifications.clear();
state.flush_effects();
result
}
}
@@ -380,11 +404,7 @@ impl TestAppContext {
T: Entity,
F: FnOnce(&mut ModelContext<T>) -> T,
{
let mut state = self.cx.borrow_mut();
state.pending_flushes += 1;
let handle = state.add_model(build_model);
state.flush_effects();
handle
self.cx.borrow_mut().add_model(build_model)
}
pub fn add_window<T, F>(&mut self, build_root_view: F) -> (usize, ViewHandle<T>)
@@ -410,11 +430,7 @@ impl TestAppContext {
T: View,
F: FnOnce(&mut ViewContext<T>) -> T,
{
let mut state = self.cx.borrow_mut();
state.pending_flushes += 1;
let handle = state.add_view(window_id, build_view);
state.flush_effects();
handle
self.cx.borrow_mut().add_view(window_id, build_view)
}
pub fn add_option_view<T, F>(
@@ -426,11 +442,7 @@ impl TestAppContext {
T: View,
F: FnOnce(&mut ViewContext<T>) -> Option<T>,
{
let mut state = self.cx.borrow_mut();
state.pending_flushes += 1;
let handle = state.add_option_view(window_id, build_view);
state.flush_effects();
handle
self.cx.borrow_mut().add_option_view(window_id, build_view)
}
pub fn read<T, F: FnOnce(&AppContext) -> T>(&self, callback: F) -> T {
@@ -509,11 +521,7 @@ impl AsyncAppContext {
}
pub fn update<T, F: FnOnce(&mut MutableAppContext) -> T>(&mut self, callback: F) -> T {
let mut state = self.0.borrow_mut();
state.pending_flushes += 1;
let result = callback(&mut *state);
state.flush_effects();
result
self.0.borrow_mut().update(callback)
}
pub fn add_model<T, F>(&mut self, build_model: F) -> ModelHandle<T>
@@ -543,11 +551,7 @@ impl UpdateModel for AsyncAppContext {
handle: &ModelHandle<E>,
update: &mut dyn FnMut(&mut E, &mut ModelContext<E>) -> O,
) -> O {
let mut state = self.0.borrow_mut();
state.pending_flushes += 1;
let result = state.update_model(handle, update);
state.flush_effects();
result
self.0.borrow_mut().update_model(handle, update)
}
}
@@ -581,11 +585,7 @@ impl UpdateView for AsyncAppContext {
where
T: View,
{
let mut state = self.0.borrow_mut();
state.pending_flushes += 1;
let result = state.update_view(handle, update);
state.flush_effects();
result
self.0.borrow_mut().update_view(handle, update)
}
}
@@ -610,11 +610,7 @@ impl UpdateModel for TestAppContext {
handle: &ModelHandle<T>,
update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> O,
) -> O {
let mut state = self.cx.borrow_mut();
state.pending_flushes += 1;
let result = state.update_model(handle, update);
state.flush_effects();
result
self.cx.borrow_mut().update_model(handle, update)
}
}
@@ -639,11 +635,7 @@ impl UpdateView for TestAppContext {
where
T: View,
{
let mut state = self.cx.borrow_mut();
state.pending_flushes += 1;
let result = state.update_view(handle, update);
state.flush_effects();
result
self.cx.borrow_mut().update_view(handle, update)
}
}
@@ -675,7 +667,7 @@ pub struct MutableAppContext {
assets: Arc<AssetCache>,
cx: AppContext,
actions: HashMap<TypeId, HashMap<TypeId, Vec<Box<ActionCallback>>>>,
global_actions: HashMap<TypeId, Vec<Box<GlobalActionCallback>>>,
global_actions: HashMap<TypeId, Box<GlobalActionCallback>>,
keystroke_matcher: keymap::Matcher,
next_entity_id: usize,
next_window_id: usize,
@@ -701,6 +693,7 @@ impl MutableAppContext {
foreground_platform: Rc<dyn platform::ForegroundPlatform>,
font_cache: Arc<FontCache>,
asset_source: impl AssetSource,
// entity_drop_tx:
) -> Self {
Self {
weak_self: None,
@@ -739,6 +732,39 @@ impl MutableAppContext {
App(self.weak_self.as_ref().unwrap().upgrade().unwrap())
}
pub fn quit(&mut self) {
let mut futures = Vec::new();
for model_id in self.cx.models.keys().copied().collect::<Vec<_>>() {
let mut model = self.cx.models.remove(&model_id).unwrap();
futures.extend(model.app_will_quit(self));
self.cx.models.insert(model_id, model);
}
for view_id in self.cx.views.keys().copied().collect::<Vec<_>>() {
let mut view = self.cx.views.remove(&view_id).unwrap();
futures.extend(view.app_will_quit(self));
self.cx.views.insert(view_id, view);
}
self.remove_all_windows();
let futures = futures::future::join_all(futures);
if self
.background
.block_with_timeout(Duration::from_millis(100), futures)
.is_err()
{
log::error!("timed out waiting on app_will_quit");
}
}
fn remove_all_windows(&mut self) {
for (window_id, _) in self.cx.windows.drain() {
self.presenters_and_platform_windows.remove(&window_id);
}
self.remove_dropped_entities();
}
pub fn platform(&self) -> Arc<dyn platform::Platform> {
self.cx.platform.clone()
}
@@ -812,10 +838,13 @@ impl MutableAppContext {
handler(action, cx);
});
self.global_actions
.entry(TypeId::of::<A>())
.or_default()
.push(handler);
if self
.global_actions
.insert(TypeId::of::<A>(), handler)
.is_some()
{
panic!("registered multiple global handlers for the same action type");
}
}
pub fn window_ids(&self) -> impl Iterator<Item = usize> + '_ {
@@ -882,9 +911,9 @@ impl MutableAppContext {
.collect()
}
pub fn update<T, F: FnOnce() -> T>(&mut self, callback: F) -> T {
pub fn update<T, F: FnOnce(&mut Self) -> T>(&mut self, callback: F) -> T {
self.pending_flushes += 1;
let result = callback();
let result = callback(self);
self.flush_effects();
result
}
@@ -963,7 +992,7 @@ impl MutableAppContext {
})
}
fn observe<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
pub fn observe<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
where
E: Entity,
E::Event: 'static,
@@ -1065,61 +1094,60 @@ impl MutableAppContext {
path: &[usize],
action: &dyn AnyAction,
) -> bool {
self.pending_flushes += 1;
let mut halted_dispatch = false;
self.update(|this| {
let mut halted_dispatch = false;
for view_id in path.iter().rev() {
if let Some(mut view) = this.cx.views.remove(&(window_id, *view_id)) {
let type_id = view.as_any().type_id();
for view_id in path.iter().rev() {
if let Some(mut view) = self.cx.views.remove(&(window_id, *view_id)) {
let type_id = view.as_any().type_id();
if let Some((name, mut handlers)) = self
.actions
.get_mut(&type_id)
.and_then(|h| h.remove_entry(&action.id()))
{
for handler in handlers.iter_mut().rev() {
let halt_dispatch =
handler(view.as_mut(), action, self, window_id, *view_id);
if halt_dispatch {
halted_dispatch = true;
break;
}
}
self.actions
if let Some((name, mut handlers)) = this
.actions
.get_mut(&type_id)
.unwrap()
.insert(name, handlers);
}
.and_then(|h| h.remove_entry(&action.id()))
{
for handler in handlers.iter_mut().rev() {
let halt_dispatch =
handler(view.as_mut(), action, this, window_id, *view_id);
if halt_dispatch {
halted_dispatch = true;
break;
}
}
this.actions
.get_mut(&type_id)
.unwrap()
.insert(name, handlers);
}
self.cx.views.insert((window_id, *view_id), view);
this.cx.views.insert((window_id, *view_id), view);
if halted_dispatch {
break;
if halted_dispatch {
break;
}
}
}
}
if !halted_dispatch {
self.dispatch_global_action_any(action);
}
self.flush_effects();
halted_dispatch
if !halted_dispatch {
halted_dispatch = this.dispatch_global_action_any(action);
}
halted_dispatch
})
}
pub fn dispatch_global_action<A: Action>(&mut self, action: A) {
self.dispatch_global_action_any(&action);
}
fn dispatch_global_action_any(&mut self, action: &dyn AnyAction) {
if let Some((name, mut handlers)) = self.global_actions.remove_entry(&action.id()) {
self.pending_flushes += 1;
for handler in handlers.iter_mut().rev() {
handler(action, self);
fn dispatch_global_action_any(&mut self, action: &dyn AnyAction) -> bool {
self.update(|this| {
if let Some((name, mut handler)) = this.global_actions.remove_entry(&action.id()) {
handler(action, this);
this.global_actions.insert(name, handler);
true
} else {
false
}
self.global_actions.insert(name, handlers);
self.flush_effects();
}
})
}
pub fn add_bindings<T: IntoIterator<Item = keymap::Binding>>(&mut self, bindings: T) {
@@ -1133,11 +1161,9 @@ impl MutableAppContext {
keystroke: &Keystroke,
) -> Result<bool> {
let mut context_chain = Vec::new();
let mut context = keymap::Context::default();
for view_id in &responder_chain {
if let Some(view) = self.cx.views.get(&(window_id, *view_id)) {
context.extend(view.keymap_context(self.as_ref()));
context_chain.push(context.clone());
context_chain.push(view.keymap_context(self.as_ref()));
} else {
return Err(anyhow!(
"View {} in responder chain does not exist",
@@ -1157,6 +1183,7 @@ impl MutableAppContext {
MatchResult::Action(action) => {
if self.dispatch_action_any(window_id, &responder_chain[0..=i], action.as_ref())
{
self.keystroke_matcher.clear_pending();
return Ok(true);
}
}
@@ -1171,14 +1198,14 @@ impl MutableAppContext {
T: Entity,
F: FnOnce(&mut ModelContext<T>) -> T,
{
self.pending_flushes += 1;
let model_id = post_inc(&mut self.next_entity_id);
let handle = ModelHandle::new(model_id, &self.cx.ref_counts);
let mut cx = ModelContext::new(self, model_id);
let model = build_model(&mut cx);
self.cx.models.insert(model_id, Box::new(model));
self.flush_effects();
handle
self.update(|this| {
let model_id = post_inc(&mut this.next_entity_id);
let handle = ModelHandle::new(model_id, &this.cx.ref_counts);
let mut cx = ModelContext::new(this, model_id);
let model = build_model(&mut cx);
this.cx.models.insert(model_id, Box::new(model));
handle
})
}
pub fn add_window<T, F>(
@@ -1190,26 +1217,26 @@ impl MutableAppContext {
T: View,
F: FnOnce(&mut ViewContext<T>) -> T,
{
self.pending_flushes += 1;
let window_id = post_inc(&mut self.next_window_id);
let root_view = self.add_view(window_id, build_root_view);
self.update(|this| {
let window_id = post_inc(&mut this.next_window_id);
let root_view = this.add_view(window_id, build_root_view);
self.cx.windows.insert(
window_id,
Window {
root_view: root_view.clone().into(),
focused_view_id: root_view.id(),
invalidation: None,
},
);
self.open_platform_window(window_id, window_options);
root_view.update(self, |view, cx| {
view.on_focus(cx);
cx.notify();
});
self.flush_effects();
this.cx.windows.insert(
window_id,
Window {
root_view: root_view.clone().into(),
focused_view_id: root_view.id(),
invalidation: None,
},
);
this.open_platform_window(window_id, window_options);
root_view.update(this, |view, cx| {
view.on_focus(cx);
cx.notify();
});
(window_id, root_view)
(window_id, root_view)
})
}
pub fn remove_window(&mut self, window_id: usize) {
@@ -1318,25 +1345,24 @@ impl MutableAppContext {
T: View,
F: FnOnce(&mut ViewContext<T>) -> Option<T>,
{
let view_id = post_inc(&mut self.next_entity_id);
self.pending_flushes += 1;
let handle = ViewHandle::new(window_id, view_id, &self.cx.ref_counts);
let mut cx = ViewContext::new(self, window_id, view_id);
let handle = if let Some(view) = build_view(&mut cx) {
self.cx.views.insert((window_id, view_id), Box::new(view));
if let Some(window) = self.cx.windows.get_mut(&window_id) {
window
.invalidation
.get_or_insert_with(Default::default)
.updated
.insert(view_id);
}
Some(handle)
} else {
None
};
self.flush_effects();
handle
self.update(|this| {
let view_id = post_inc(&mut this.next_entity_id);
let mut cx = ViewContext::new(this, window_id, view_id);
let handle = if let Some(view) = build_view(&mut cx) {
this.cx.views.insert((window_id, view_id), Box::new(view));
if let Some(window) = this.cx.windows.get_mut(&window_id) {
window
.invalidation
.get_or_insert_with(Default::default)
.updated
.insert(view_id);
}
Some(ViewHandle::new(window_id, view_id, &this.cx.ref_counts))
} else {
None
};
handle
})
}
pub fn element_state<Tag: 'static, T: 'static + Default>(
@@ -1588,27 +1614,25 @@ impl MutableAppContext {
return;
}
self.pending_flushes += 1;
self.update(|this| {
let blurred_id = this.cx.windows.get_mut(&window_id).map(|window| {
let blurred_id = window.focused_view_id;
window.focused_view_id = focused_id;
blurred_id
});
let blurred_id = self.cx.windows.get_mut(&window_id).map(|window| {
let blurred_id = window.focused_view_id;
window.focused_view_id = focused_id;
blurred_id
});
if let Some(blurred_id) = blurred_id {
if let Some(mut blurred_view) = self.cx.views.remove(&(window_id, blurred_id)) {
blurred_view.on_blur(self, window_id, blurred_id);
self.cx.views.insert((window_id, blurred_id), blurred_view);
if let Some(blurred_id) = blurred_id {
if let Some(mut blurred_view) = this.cx.views.remove(&(window_id, blurred_id)) {
blurred_view.on_blur(this, window_id, blurred_id);
this.cx.views.insert((window_id, blurred_id), blurred_view);
}
}
}
if let Some(mut focused_view) = self.cx.views.remove(&(window_id, focused_id)) {
focused_view.on_focus(self, window_id, focused_id);
self.cx.views.insert((window_id, focused_id), focused_view);
}
self.flush_effects();
if let Some(mut focused_view) = this.cx.views.remove(&(window_id, focused_id)) {
focused_view.on_focus(this, window_id, focused_id);
this.cx.views.insert((window_id, focused_id), focused_view);
}
})
}
pub fn spawn<F, Fut, T>(&self, f: F) -> Task<T>
@@ -1654,18 +1678,18 @@ impl UpdateModel for MutableAppContext {
update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> V,
) -> V {
if let Some(mut model) = self.cx.models.remove(&handle.model_id) {
self.pending_flushes += 1;
let mut cx = ModelContext::new(self, handle.model_id);
let result = update(
model
.as_any_mut()
.downcast_mut()
.expect("downcast is type safe"),
&mut cx,
);
self.cx.models.insert(handle.model_id, model);
self.flush_effects();
result
self.update(|this| {
let mut cx = ModelContext::new(this, handle.model_id);
let result = update(
model
.as_any_mut()
.downcast_mut()
.expect("downcast is type safe"),
&mut cx,
);
this.cx.models.insert(handle.model_id, model);
result
})
} else {
panic!("circular model update");
}
@@ -1700,25 +1724,25 @@ impl UpdateView for MutableAppContext {
where
T: View,
{
self.pending_flushes += 1;
let mut view = self
.cx
.views
.remove(&(handle.window_id, handle.view_id))
.expect("circular view update");
self.update(|this| {
let mut view = this
.cx
.views
.remove(&(handle.window_id, handle.view_id))
.expect("circular view update");
let mut cx = ViewContext::new(self, handle.window_id, handle.view_id);
let result = update(
view.as_any_mut()
.downcast_mut()
.expect("downcast is type safe"),
&mut cx,
);
self.cx
.views
.insert((handle.window_id, handle.view_id), view);
self.flush_effects();
result
let mut cx = ViewContext::new(this, handle.window_id, handle.view_id);
let result = update(
view.as_any_mut()
.downcast_mut()
.expect("downcast is type safe"),
&mut cx,
);
this.cx
.views
.insert((handle.window_id, handle.view_id), view);
result
})
}
}
@@ -1879,6 +1903,10 @@ pub trait AnyModel {
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
fn release(&mut self, cx: &mut MutableAppContext);
fn app_will_quit(
&mut self,
cx: &mut MutableAppContext,
) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>>;
}
impl<T> AnyModel for T
@@ -1896,12 +1924,23 @@ where
fn release(&mut self, cx: &mut MutableAppContext) {
self.release(cx);
}
fn app_will_quit(
&mut self,
cx: &mut MutableAppContext,
) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>> {
self.app_will_quit(cx)
}
}
pub trait AnyView {
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
fn release(&mut self, cx: &mut MutableAppContext);
fn app_will_quit(
&mut self,
cx: &mut MutableAppContext,
) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>>;
fn ui_name(&self) -> &'static str;
fn render<'a>(
&mut self,
@@ -1932,6 +1971,13 @@ where
self.release(cx);
}
fn app_will_quit(
&mut self,
cx: &mut MutableAppContext,
) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>> {
self.app_will_quit(cx)
}
fn ui_name(&self) -> &'static str {
T::ui_name()
}
@@ -2029,7 +2075,7 @@ impl<'a, T: Entity> ModelContext<'a, T> {
S::Event: 'static,
F: 'static + FnMut(&mut T, ModelHandle<S>, &S::Event, &mut ModelContext<T>),
{
let subscriber = self.handle().downgrade();
let subscriber = self.weak_handle();
self.app
.subscribe_internal(handle, move |emitter, event, cx| {
if let Some(subscriber) = subscriber.upgrade(cx) {
@@ -2048,7 +2094,7 @@ impl<'a, T: Entity> ModelContext<'a, T> {
S: Entity,
F: 'static + FnMut(&mut T, ModelHandle<S>, &mut ModelContext<T>),
{
let observer = self.handle().downgrade();
let observer = self.weak_handle();
self.app.observe_internal(handle, move |observed, cx| {
if let Some(observer) = observer.upgrade(cx) {
observer.update(cx, |observer, cx| {
@@ -2065,6 +2111,10 @@ impl<'a, T: Entity> ModelContext<'a, T> {
ModelHandle::new(self.model_id, &self.app.cx.ref_counts)
}
pub fn weak_handle(&self) -> WeakModelHandle<T> {
WeakModelHandle::new(self.model_id)
}
pub fn spawn<F, Fut, S>(&self, f: F) -> Task<S>
where
F: FnOnce(ModelHandle<T>, AsyncAppContext) -> Fut,
@@ -2081,7 +2131,7 @@ impl<'a, T: Entity> ModelContext<'a, T> {
Fut: 'static + Future<Output = S>,
S: 'static,
{
let handle = self.handle().downgrade();
let handle = self.weak_handle();
self.app.spawn(|cx| f(handle, cx))
}
}
@@ -2160,6 +2210,10 @@ impl<'a, T: View> ViewContext<'a, T> {
ViewHandle::new(self.window_id, self.view_id, &self.app.cx.ref_counts)
}
pub fn weak_handle(&self) -> WeakViewHandle<T> {
WeakViewHandle::new(self.window_id, self.view_id)
}
pub fn window_id(&self) -> usize {
self.window_id
}
@@ -2255,7 +2309,7 @@ impl<'a, T: View> ViewContext<'a, T> {
H: Handle<E>,
F: 'static + FnMut(&mut T, H, &E::Event, &mut ViewContext<T>),
{
let subscriber = self.handle().downgrade();
let subscriber = self.weak_handle();
self.app
.subscribe_internal(handle, move |emitter, event, cx| {
if let Some(subscriber) = subscriber.upgrade(cx) {
@@ -2275,7 +2329,7 @@ impl<'a, T: View> ViewContext<'a, T> {
H: Handle<E>,
F: 'static + FnMut(&mut T, H, &mut ViewContext<T>),
{
let observer = self.handle().downgrade();
let observer = self.weak_handle();
self.app.observe_internal(handle, move |observed, cx| {
if let Some(observer) = observer.upgrade(cx) {
observer.update(cx, |observer, cx| {
@@ -2319,7 +2373,7 @@ impl<'a, T: View> ViewContext<'a, T> {
Fut: 'static + Future<Output = S>,
S: 'static,
{
let handle = self.handle().downgrade();
let handle = self.weak_handle();
self.app.spawn(|cx| f(handle, cx))
}
}
@@ -2337,6 +2391,10 @@ impl<'a, T: View> RenderContext<'a, T> {
pub fn handle(&self) -> WeakViewHandle<T> {
WeakViewHandle::new(self.window_id, self.view_id)
}
pub fn view_id(&self) -> usize {
self.view_id
}
}
impl AsRef<AppContext> for &AppContext {
@@ -2375,6 +2433,12 @@ impl<V: View> UpdateModel for RenderContext<'_, V> {
}
}
impl<V: View> ReadView for RenderContext<'_, V> {
fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
self.app.read_view(handle)
}
}
impl<M> AsRef<AppContext> for ViewContext<'_, M> {
fn as_ref(&self) -> &AppContext {
&self.app.cx
@@ -2608,9 +2672,11 @@ impl<T: Entity> ModelHandle<T> {
}
}
cx.borrow().foreground().start_waiting();
rx.recv()
.await
.expect("model dropped with pending condition");
cx.borrow().foreground().finish_waiting();
}
})
.await
@@ -2707,6 +2773,10 @@ impl<T: Entity> WeakModelHandle<T> {
}
}
pub fn id(&self) -> usize {
self.model_id
}
pub fn upgrade(self, cx: &impl UpgradeModelHandle) -> Option<ModelHandle<T>> {
cx.upgrade_model_handle(self)
}
@@ -2800,6 +2870,28 @@ impl<T: View> ViewHandle<T> {
.map_or(false, |focused_id| focused_id == self.view_id)
}
pub fn next_notification(&self, cx: &TestAppContext) -> impl Future<Output = ()> {
let (mut tx, mut rx) = mpsc::channel(1);
let mut cx = cx.cx.borrow_mut();
let subscription = cx.observe(self, move |_, _| {
tx.blocking_send(()).ok();
});
let duration = if std::env::var("CI").is_ok() {
Duration::from_secs(5)
} else {
Duration::from_secs(1)
};
async move {
let notification = timeout(duration, rx.recv())
.await
.expect("next notification timed out");
drop(subscription);
notification.expect("model dropped while test was waiting for its next notification")
}
}
pub fn condition(
&self,
cx: &TestAppContext,
@@ -2850,9 +2942,11 @@ impl<T: View> ViewHandle<T> {
}
}
cx.borrow().foreground().start_waiting();
rx.recv()
.await
.expect("view dropped with pending condition");
cx.borrow().foreground().finish_waiting();
}
})
.await
@@ -3025,14 +3119,39 @@ impl Drop for AnyViewHandle {
pub struct AnyModelHandle {
model_id: usize,
model_type: TypeId,
ref_counts: Arc<Mutex<RefCounts>>,
}
impl AnyModelHandle {
pub fn downcast<T: Entity>(self) -> Option<ModelHandle<T>> {
if self.is::<T>() {
let result = Some(ModelHandle {
model_id: self.model_id,
model_type: PhantomData,
ref_counts: self.ref_counts.clone(),
});
unsafe {
Arc::decrement_strong_count(&self.ref_counts);
}
std::mem::forget(self);
result
} else {
None
}
}
pub fn is<T: Entity>(&self) -> bool {
self.model_type == TypeId::of::<T>()
}
}
impl<T: Entity> From<ModelHandle<T>> for AnyModelHandle {
fn from(handle: ModelHandle<T>) -> Self {
handle.ref_counts.lock().inc_model(handle.model_id);
Self {
model_id: handle.model_id,
model_type: TypeId::of::<T>(),
ref_counts: handle.ref_counts.clone(),
}
}
@@ -3237,7 +3356,9 @@ struct RefCounts {
impl RefCounts {
fn inc_model(&mut self, model_id: usize) {
match self.entity_counts.entry(model_id) {
Entry::Occupied(mut entry) => *entry.get_mut() += 1,
Entry::Occupied(mut entry) => {
*entry.get_mut() += 1;
}
Entry::Vacant(entry) => {
entry.insert(1);
self.dropped_models.remove(&model_id);
@@ -3304,16 +3425,11 @@ impl RefCounts {
HashSet<(usize, usize)>,
HashSet<(TypeId, ElementStateId)>,
) {
let mut dropped_models = HashSet::new();
let mut dropped_views = HashSet::new();
let mut dropped_element_states = HashSet::new();
std::mem::swap(&mut self.dropped_models, &mut dropped_models);
std::mem::swap(&mut self.dropped_views, &mut dropped_views);
std::mem::swap(
&mut self.dropped_element_states,
&mut dropped_element_states,
);
(dropped_models, dropped_views, dropped_element_states)
(
std::mem::take(&mut self.dropped_models),
std::mem::take(&mut self.dropped_views),
std::mem::take(&mut self.dropped_element_states),
)
}
}
@@ -3559,7 +3675,11 @@ mod tests {
presenter.borrow_mut().dispatch_event(
Event::LeftMouseDown {
position: Default::default(),
ctrl: false,
alt: false,
shift: false,
cmd: false,
click_count: 1,
},
cx,
);
@@ -3616,7 +3736,7 @@ mod tests {
assert!(!*model_released.lock());
assert!(!*view_released.lock());
cx.update(move || {
cx.update(move |_| {
drop(model);
});
assert!(*model_released.lock());
@@ -3722,7 +3842,7 @@ mod tests {
cx.subscribe(&observed_model, |_, _, _, _| {}).detach();
});
cx.update(|| {
cx.update(|_| {
drop(observing_view);
drop(observing_model);
});
@@ -3814,7 +3934,7 @@ mod tests {
cx.observe(&observed_model, |_, _, _| {}).detach();
});
cx.update(|| {
cx.update(|_| {
drop(observing_view);
drop(observing_model);
});
@@ -3925,12 +4045,7 @@ mod tests {
let actions_clone = actions.clone();
cx.add_global_action(move |_: &Action, _: &mut MutableAppContext| {
actions_clone.borrow_mut().push("global a".to_string());
});
let actions_clone = actions.clone();
cx.add_global_action(move |_: &Action, _: &mut MutableAppContext| {
actions_clone.borrow_mut().push("global b".to_string());
actions_clone.borrow_mut().push("global".to_string());
});
let actions_clone = actions.clone();
@@ -3986,7 +4101,7 @@ mod tests {
assert_eq!(
*actions.borrow(),
vec!["4 d", "4 c", "3 b", "3 a", "2 d", "2 c", "global b", "global a"]
vec!["4 d", "4 c", "3 b", "3 a", "2 d", "2 c", "global"]
);
}
@@ -4032,7 +4147,10 @@ mod tests {
let mut view_2 = View::new(2);
let mut view_3 = View::new(3);
view_1.keymap_context.set.insert("a".into());
view_2.keymap_context.set.insert("a".into());
view_2.keymap_context.set.insert("b".into());
view_3.keymap_context.set.insert("a".into());
view_3.keymap_context.set.insert("b".into());
view_3.keymap_context.set.insert("c".into());
let (window_id, view_1) = cx.add_window(Default::default(), |_| view_1);

View File

@@ -4,6 +4,7 @@ mod constrained_box;
mod container;
mod empty;
mod event_handler;
mod expanded;
mod flex;
mod hook;
mod image;
@@ -16,6 +17,7 @@ mod svg;
mod text;
mod uniform_list;
use self::expanded::Expanded;
pub use self::{
align::*, canvas::*, constrained_box::*, container::*, empty::*, event_handler::*, flex::*,
hook::*, image::*, label::*, list::*, mouse_event_handler::*, overlay::*, stack::*, svg::*,
@@ -130,11 +132,18 @@ pub trait Element {
Container::new(self.boxed())
}
fn expanded(self, flex: f32) -> Expanded
fn expanded(self) -> Expanded
where
Self: 'static + Sized,
{
Expanded::new(flex, self.boxed())
Expanded::new(self.boxed())
}
fn flexible(self, flex: f32, expanded: bool) -> Flexible
where
Self: 'static + Sized,
{
Flexible::new(flex, expanded, self.boxed())
}
}
@@ -301,6 +310,10 @@ impl<T: Element> Default for Lifecycle<T> {
}
impl ElementBox {
pub fn name(&self) -> Option<&str> {
self.0.name.as_deref()
}
pub fn metadata<T: 'static>(&self) -> Option<&T> {
let element = unsafe { &*self.0.element.as_ptr() };
element.metadata().and_then(|m| m.downcast_ref())

View File

@@ -25,6 +25,11 @@ impl Align {
self
}
pub fn bottom(mut self) -> Self {
self.alignment.set_y(1.0);
self
}
pub fn left(mut self) -> Self {
self.alignment.set_x(-1.0);
self

View File

@@ -52,6 +52,11 @@ impl Container {
self
}
pub fn with_margin_bottom(mut self, margin: f32) -> Self {
self.style.margin.bottom = margin;
self
}
pub fn with_margin_left(mut self, margin: f32) -> Self {
self.style.margin.left = margin;
self

View File

@@ -0,0 +1,90 @@
use crate::{
geometry::{rect::RectF, vector::Vector2F},
json, DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, PaintContext,
SizeConstraint,
};
use serde_json::json;
pub struct Expanded {
child: ElementBox,
full_width: bool,
full_height: bool,
}
impl Expanded {
pub fn new(child: ElementBox) -> Self {
Self {
child,
full_width: true,
full_height: true,
}
}
pub fn to_full_width(mut self) -> Self {
self.full_width = true;
self.full_height = false;
self
}
pub fn to_full_height(mut self) -> Self {
self.full_width = false;
self.full_height = true;
self
}
}
impl Element for Expanded {
type LayoutState = ();
type PaintState = ();
fn layout(
&mut self,
mut constraint: SizeConstraint,
cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) {
if self.full_width {
constraint.min.set_x(constraint.max.x());
}
if self.full_height {
constraint.min.set_y(constraint.max.y());
}
let size = self.child.layout(constraint, cx);
(size, ())
}
fn paint(
&mut self,
bounds: RectF,
visible_bounds: RectF,
_: &mut Self::LayoutState,
cx: &mut PaintContext,
) -> Self::PaintState {
self.child.paint(bounds.origin(), visible_bounds, cx);
}
fn dispatch_event(
&mut self,
event: &Event,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
cx: &mut EventContext,
) -> bool {
self.child.dispatch_event(event, cx)
}
fn debug(
&self,
_: RectF,
_: &Self::LayoutState,
_: &Self::PaintState,
cx: &DebugContext,
) -> json::Value {
json!({
"type": "Expanded",
"full_width": self.full_width,
"full_height": self.full_height,
"child": self.child.debug(cx)
})
}
}

View File

@@ -228,88 +228,15 @@ struct FlexParentData {
expanded: bool,
}
pub struct Expanded {
metadata: FlexParentData,
child: ElementBox,
}
impl Expanded {
pub fn new(flex: f32, child: ElementBox) -> Self {
Expanded {
metadata: FlexParentData {
flex,
expanded: true,
},
child,
}
}
}
impl Element for Expanded {
type LayoutState = ();
type PaintState = ();
fn layout(
&mut self,
constraint: SizeConstraint,
cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) {
let size = self.child.layout(constraint, cx);
(size, ())
}
fn paint(
&mut self,
bounds: RectF,
visible_bounds: RectF,
_: &mut Self::LayoutState,
cx: &mut PaintContext,
) -> Self::PaintState {
self.child.paint(bounds.origin(), visible_bounds, cx)
}
fn dispatch_event(
&mut self,
event: &Event,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
cx: &mut EventContext,
) -> bool {
self.child.dispatch_event(event, cx)
}
fn metadata(&self) -> Option<&dyn Any> {
Some(&self.metadata)
}
fn debug(
&self,
_: RectF,
_: &Self::LayoutState,
_: &Self::PaintState,
cx: &DebugContext,
) -> Value {
json!({
"type": "Expanded",
"flex": self.metadata.flex,
"child": self.child.debug(cx)
})
}
}
pub struct Flexible {
metadata: FlexParentData,
child: ElementBox,
}
impl Flexible {
pub fn new(flex: f32, child: ElementBox) -> Self {
pub fn new(flex: f32, expanded: bool, child: ElementBox) -> Self {
Flexible {
metadata: FlexParentData {
flex,
expanded: false,
},
metadata: FlexParentData { flex, expanded },
child,
}
}

View File

@@ -207,7 +207,7 @@ mod tests {
"Menlo",
12.,
Default::default(),
false,
None,
Color::black(),
cx.font_cache(),
)
@@ -216,7 +216,7 @@ mod tests {
"Menlo",
12.,
*FontProperties::new().weight(Weight::BOLD),
false,
None,
Color::new(255, 0, 0, 255),
cx.font_cache(),
)

View File

@@ -1,35 +1,56 @@
use std::{ops::Range, sync::Arc};
use crate::{
color::Color,
fonts::TextStyle,
fonts::{HighlightStyle, TextStyle},
geometry::{
rect::RectF,
vector::{vec2f, Vector2F},
},
json::{ToJson, Value},
text_layout::{Line, ShapedBoundary},
DebugContext, Element, Event, EventContext, LayoutContext, PaintContext, SizeConstraint,
text_layout::{Line, RunStyle, ShapedBoundary},
DebugContext, Element, Event, EventContext, FontCache, LayoutContext, PaintContext,
SizeConstraint, TextLayoutCache,
};
use serde_json::json;
pub struct Text {
text: String,
style: TextStyle,
soft_wrap: bool,
highlights: Vec<(Range<usize>, HighlightStyle)>,
}
pub struct LayoutState {
lines: Vec<(Line, Vec<ShapedBoundary>)>,
shaped_lines: Vec<Line>,
wrap_boundaries: Vec<Vec<ShapedBoundary>>,
line_height: f32,
}
impl Text {
pub fn new(text: String, style: TextStyle) -> Self {
Self { text, style }
Self {
text,
style,
soft_wrap: true,
highlights: Vec::new(),
}
}
pub fn with_default_color(mut self, color: Color) -> Self {
self.style.color = color;
self
}
pub fn with_highlights(mut self, runs: Vec<(Range<usize>, HighlightStyle)>) -> Self {
self.highlights = runs;
self
}
pub fn with_soft_wrap(mut self, soft_wrap: bool) -> Self {
self.soft_wrap = soft_wrap;
self
}
}
impl Element for Text {
@@ -41,28 +62,59 @@ impl Element for Text {
constraint: SizeConstraint,
cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) {
let font_id = self.style.font_id;
let line_height = cx.font_cache.line_height(font_id, self.style.font_size);
// Convert the string and highlight ranges into an iterator of highlighted chunks.
let mut offset = 0;
let mut highlight_ranges = self.highlights.iter().peekable();
let chunks = std::iter::from_fn(|| {
let result;
if let Some((range, highlight)) = highlight_ranges.peek() {
if offset < range.start {
result = Some((&self.text[offset..range.start], None));
offset = range.start;
} else {
result = Some((&self.text[range.clone()], Some(*highlight)));
highlight_ranges.next();
offset = range.end;
}
} else if offset < self.text.len() {
result = Some((&self.text[offset..], None));
offset = self.text.len();
} else {
result = None;
}
result
});
let mut wrapper = cx.font_cache.line_wrapper(font_id, self.style.font_size);
let mut lines = Vec::new();
// Perform shaping on these highlighted chunks
let shaped_lines = layout_highlighted_chunks(
chunks,
&self.style,
cx.text_layout_cache,
&cx.font_cache,
usize::MAX,
self.text.matches('\n').count() + 1,
);
// If line wrapping is enabled, wrap each of the shaped lines.
let font_id = self.style.font_id;
let mut line_count = 0;
let mut max_line_width = 0_f32;
for line in self.text.lines() {
let shaped_line = cx.text_layout_cache.layout_str(
line,
self.style.font_size,
&[(line.len(), self.style.to_run())],
);
let wrap_boundaries = wrapper
.wrap_shaped_line(line, &shaped_line, constraint.max.x())
.collect::<Vec<_>>();
let mut wrap_boundaries = Vec::new();
let mut wrapper = cx.font_cache.line_wrapper(font_id, self.style.font_size);
for (line, shaped_line) in self.text.lines().zip(&shaped_lines) {
if self.soft_wrap {
let boundaries = wrapper
.wrap_shaped_line(line, shaped_line, constraint.max.x())
.collect::<Vec<_>>();
line_count += boundaries.len() + 1;
wrap_boundaries.push(boundaries);
} else {
line_count += 1;
}
max_line_width = max_line_width.max(shaped_line.width());
line_count += wrap_boundaries.len() + 1;
lines.push((shaped_line, wrap_boundaries));
}
let line_height = cx.font_cache.line_height(font_id, self.style.font_size);
let size = vec2f(
max_line_width
.ceil()
@@ -70,7 +122,14 @@ impl Element for Text {
.min(constraint.max.x()),
(line_height * line_count as f32).ceil(),
);
(size, LayoutState { lines, line_height })
(
size,
LayoutState {
shaped_lines,
wrap_boundaries,
line_height,
},
)
}
fn paint(
@@ -81,8 +140,10 @@ impl Element for Text {
cx: &mut PaintContext,
) -> Self::PaintState {
let mut origin = bounds.origin();
for (line, wrap_boundaries) in &layout.lines {
let wrapped_line_boundaries = RectF::new(
let empty = Vec::new();
for (ix, line) in layout.shaped_lines.iter().enumerate() {
let wrap_boundaries = layout.wrap_boundaries.get(ix).unwrap_or(&empty);
let boundaries = RectF::new(
origin,
vec2f(
bounds.width(),
@@ -90,16 +151,20 @@ impl Element for Text {
),
);
if wrapped_line_boundaries.intersects(visible_bounds) {
line.paint_wrapped(
origin,
visible_bounds,
layout.line_height,
wrap_boundaries.iter().copied(),
cx,
);
if boundaries.intersects(visible_bounds) {
if self.soft_wrap {
line.paint_wrapped(
origin,
visible_bounds,
layout.line_height,
wrap_boundaries.iter().copied(),
cx,
);
} else {
line.paint(origin, visible_bounds, layout.line_height, cx);
}
}
origin.set_y(wrapped_line_boundaries.max_y());
origin.set_y(boundaries.max_y());
}
}
@@ -129,3 +194,71 @@ impl Element for Text {
})
}
}
/// Perform text layout on a series of highlighted chunks of text.
pub fn layout_highlighted_chunks<'a>(
chunks: impl Iterator<Item = (&'a str, Option<HighlightStyle>)>,
style: &'a TextStyle,
text_layout_cache: &'a TextLayoutCache,
font_cache: &'a Arc<FontCache>,
max_line_len: usize,
max_line_count: usize,
) -> Vec<Line> {
let mut layouts = Vec::with_capacity(max_line_count);
let mut prev_font_properties = style.font_properties.clone();
let mut prev_font_id = style.font_id;
let mut line = String::new();
let mut styles = Vec::new();
let mut row = 0;
let mut line_exceeded_max_len = false;
for (chunk, highlight_style) in chunks.chain([("\n", None)]) {
for (ix, mut line_chunk) in chunk.split('\n').enumerate() {
if ix > 0 {
layouts.push(text_layout_cache.layout_str(&line, style.font_size, &styles));
line.clear();
styles.clear();
row += 1;
line_exceeded_max_len = false;
if row == max_line_count {
return layouts;
}
}
if !line_chunk.is_empty() && !line_exceeded_max_len {
let highlight_style = highlight_style.unwrap_or(style.clone().into());
// Avoid a lookup if the font properties match the previous ones.
let font_id = if highlight_style.font_properties == prev_font_properties {
prev_font_id
} else {
font_cache
.select_font(style.font_family_id, &highlight_style.font_properties)
.unwrap_or(style.font_id)
};
if line.len() + line_chunk.len() > max_line_len {
let mut chunk_len = max_line_len - line.len();
while !line_chunk.is_char_boundary(chunk_len) {
chunk_len -= 1;
}
line_chunk = &line_chunk[..chunk_len];
line_exceeded_max_len = true;
}
line.push_str(line_chunk);
styles.push((
line_chunk.len(),
RunStyle {
font_id,
color: highlight_style.color,
underline: highlight_style.underline,
},
));
prev_font_id = font_id;
prev_font_properties = highlight_style.font_properties;
}
}
}
layouts
}

View File

@@ -14,9 +14,15 @@ use std::{cmp, ops::Range, sync::Arc};
#[derive(Clone, Default)]
pub struct UniformListState(Arc<Mutex<StateInner>>);
#[derive(Debug)]
pub enum ScrollTarget {
Show(usize),
Center(usize),
}
impl UniformListState {
pub fn scroll_to(&self, item_ix: usize) {
self.0.lock().scroll_to = Some(item_ix);
pub fn scroll_to(&self, scroll_to: ScrollTarget) {
self.0.lock().scroll_to = Some(scroll_to);
}
pub fn scroll_top(&self) -> f32 {
@@ -27,7 +33,7 @@ impl UniformListState {
#[derive(Default)]
struct StateInner {
scroll_top: f32,
scroll_to: Option<usize>,
scroll_to: Option<ScrollTarget>,
}
pub struct LayoutState {
@@ -93,20 +99,38 @@ where
fn autoscroll(&mut self, scroll_max: f32, list_height: f32, item_height: f32) {
let mut state = self.state.0.lock();
if state.scroll_top > scroll_max {
state.scroll_top = scroll_max;
}
if let Some(scroll_to) = state.scroll_to.take() {
let item_ix;
let center;
match scroll_to {
ScrollTarget::Show(ix) => {
item_ix = ix;
center = false;
}
ScrollTarget::Center(ix) => {
item_ix = ix;
center = true;
}
}
if let Some(item_ix) = state.scroll_to.take() {
let item_top = self.padding_top + item_ix as f32 * item_height;
let item_bottom = item_top + item_height;
if item_top < state.scroll_top {
state.scroll_top = item_top;
} else if item_bottom > (state.scroll_top + list_height) {
state.scroll_top = item_bottom - list_height;
if center {
let item_center = item_top + item_height / 2.;
state.scroll_top = (item_center - list_height / 2.).max(0.);
} else {
let scroll_bottom = state.scroll_top + list_height;
if item_top < state.scroll_top {
state.scroll_top = item_top;
} else if item_bottom > scroll_bottom {
state.scroll_top = item_bottom - list_height;
}
}
}
if state.scroll_top > scroll_max {
state.scroll_top = scroll_max;
}
}
fn scroll_top(&self) -> f32 {

View File

@@ -7,7 +7,7 @@ use rand::prelude::*;
use smol::{channel, prelude::*, Executor, Timer};
use std::{
any::Any,
fmt::{self, Debug},
fmt::{self, Debug, Display},
marker::PhantomData,
mem,
ops::RangeInclusive,
@@ -25,7 +25,7 @@ use waker_fn::waker_fn;
use crate::{
platform::{self, Dispatcher},
util,
util, MutableAppContext,
};
pub enum Foreground {
@@ -38,7 +38,9 @@ pub enum Foreground {
}
pub enum Background {
Deterministic(Arc<Deterministic>),
Deterministic {
executor: Arc<Deterministic>,
},
Production {
executor: Arc<smol::Executor<'static>>,
_stop: channel::Sender<()>,
@@ -50,7 +52,9 @@ type AnyFuture = Pin<Box<dyn 'static + Send + Future<Output = Box<dyn Any + Send
type AnyTask = async_task::Task<Box<dyn Any + Send + 'static>>;
type AnyLocalTask = async_task::Task<Box<dyn Any + 'static>>;
#[must_use]
pub enum Task<T> {
Ready(Option<T>),
Local {
any_task: AnyLocalTask,
result_type: PhantomData<T>,
@@ -73,6 +77,7 @@ struct DeterministicState {
block_on_ticks: RangeInclusive<usize>,
now: Instant,
pending_timers: Vec<(Instant, barrier::Sender)>,
waiting_backtrace: Option<Backtrace>,
}
pub struct Deterministic {
@@ -93,6 +98,7 @@ impl Deterministic {
block_on_ticks: 0..=1000,
now: Instant::now(),
pending_timers: Default::default(),
waiting_backtrace: None,
})),
parker: Default::default(),
}
@@ -139,8 +145,8 @@ impl Deterministic {
return result;
}
if !woken.load(SeqCst) && self.state.lock().forbid_parking {
panic!("deterministic executor parked after a call to forbid_parking");
if !woken.load(SeqCst) {
self.state.lock().will_park();
}
woken.store(false, SeqCst);
@@ -202,6 +208,7 @@ impl Deterministic {
}
let state = self.state.lock();
if state.scheduled_from_foreground.is_empty()
&& state.scheduled_from_background.is_empty()
&& state.spawned_from_foreground.is_empty()
@@ -240,11 +247,9 @@ impl Deterministic {
if let Poll::Ready(result) = future.as_mut().poll(&mut cx) {
return Some(result);
}
let state = self.state.lock();
let mut state = self.state.lock();
if state.scheduled_from_background.is_empty() {
if state.forbid_parking {
panic!("deterministic executor parked after a call to forbid_parking");
}
state.will_park();
drop(state);
self.parker.lock().park();
}
@@ -257,6 +262,26 @@ impl Deterministic {
}
}
impl DeterministicState {
fn will_park(&mut self) {
if self.forbid_parking {
let mut backtrace_message = String::new();
if let Some(backtrace) = self.waiting_backtrace.as_mut() {
backtrace.resolve();
backtrace_message = format!(
"\nbacktrace of waiting future:\n{:?}",
CwdBacktrace::new(backtrace)
);
}
panic!(
"deterministic executor parked after a call to forbid_parking{}",
backtrace_message
);
}
}
}
#[derive(Default)]
struct Trace {
executed: Vec<Backtrace>,
@@ -302,32 +327,53 @@ impl Trace {
}
}
impl Debug for Trace {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
struct FirstCwdFrameInBacktrace<'a>(&'a Backtrace);
struct CwdBacktrace<'a> {
backtrace: &'a Backtrace,
first_frame_only: bool,
}
impl<'a> Debug for FirstCwdFrameInBacktrace<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
let cwd = std::env::current_dir().unwrap();
let mut print_path = |fmt: &mut fmt::Formatter<'_>, path: BytesOrWideString<'_>| {
fmt::Display::fmt(&path, fmt)
};
let mut fmt = BacktraceFmt::new(f, backtrace::PrintFmt::Full, &mut print_path);
for frame in self.0.frames() {
let mut formatted_frame = fmt.frame();
if frame
.symbols()
.iter()
.any(|s| s.filename().map_or(false, |f| f.starts_with(&cwd)))
{
formatted_frame.backtrace_frame(frame)?;
break;
}
impl<'a> CwdBacktrace<'a> {
fn new(backtrace: &'a Backtrace) -> Self {
Self {
backtrace,
first_frame_only: false,
}
}
fn first_frame(backtrace: &'a Backtrace) -> Self {
Self {
backtrace,
first_frame_only: true,
}
}
}
impl<'a> Debug for CwdBacktrace<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
let cwd = std::env::current_dir().unwrap();
let mut print_path = |fmt: &mut fmt::Formatter<'_>, path: BytesOrWideString<'_>| {
fmt::Display::fmt(&path, fmt)
};
let mut fmt = BacktraceFmt::new(f, backtrace::PrintFmt::Full, &mut print_path);
for frame in self.backtrace.frames() {
let mut formatted_frame = fmt.frame();
if frame
.symbols()
.iter()
.any(|s| s.filename().map_or(false, |f| f.starts_with(&cwd)))
{
formatted_frame.backtrace_frame(frame)?;
if self.first_frame_only {
break;
}
fmt.finish()
}
}
fmt.finish()
}
}
impl Debug for Trace {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for ((backtrace, scheduled), spawned_from_foreground) in self
.executed
.iter()
@@ -336,7 +382,7 @@ impl Debug for Trace {
{
writeln!(f, "Scheduled")?;
for backtrace in scheduled {
writeln!(f, "- {:?}", FirstCwdFrameInBacktrace(backtrace))?;
writeln!(f, "- {:?}", CwdBacktrace::first_frame(backtrace))?;
}
if scheduled.is_empty() {
writeln!(f, "None")?;
@@ -345,14 +391,14 @@ impl Debug for Trace {
writeln!(f, "Spawned from foreground")?;
for backtrace in spawned_from_foreground {
writeln!(f, "- {:?}", FirstCwdFrameInBacktrace(backtrace))?;
writeln!(f, "- {:?}", CwdBacktrace::first_frame(backtrace))?;
}
if spawned_from_foreground.is_empty() {
writeln!(f, "None")?;
}
writeln!(f, "==========")?;
writeln!(f, "Run: {:?}", FirstCwdFrameInBacktrace(backtrace))?;
writeln!(f, "Run: {:?}", CwdBacktrace::first_frame(backtrace))?;
writeln!(f, "+++++++++++++++++++")?;
}
@@ -429,6 +475,31 @@ impl Foreground {
*any_value.downcast().unwrap()
}
pub fn parking_forbidden(&self) -> bool {
match self {
Self::Deterministic(executor) => executor.state.lock().forbid_parking,
_ => panic!("this method can only be called on a deterministic executor"),
}
}
pub fn start_waiting(&self) {
match self {
Self::Deterministic(executor) => {
executor.state.lock().waiting_backtrace = Some(Backtrace::new_unresolved());
}
_ => panic!("this method can only be called on a deterministic executor"),
}
}
pub fn finish_waiting(&self) {
match self {
Self::Deterministic(executor) => {
executor.state.lock().waiting_backtrace.take();
}
_ => panic!("this method can only be called on a deterministic executor"),
}
}
pub fn forbid_parking(&self) {
match self {
Self::Deterministic(executor) => {
@@ -515,7 +586,7 @@ impl Background {
let future = any_future(future);
let any_task = match self {
Self::Production { executor, .. } => executor.spawn(future),
Self::Deterministic(executor) => executor.spawn(future),
Self::Deterministic { executor, .. } => executor.spawn(future),
};
Task::send(any_task)
}
@@ -533,7 +604,7 @@ impl Background {
if !timeout.is_zero() {
let output = match self {
Self::Production { .. } => smol::block_on(util::timeout(timeout, &mut future)).ok(),
Self::Deterministic(executor) => executor.block_on(&mut future),
Self::Deterministic { executor, .. } => executor.block_on(&mut future),
};
if let Some(output) = output {
return Ok(*output.downcast().unwrap());
@@ -586,11 +657,15 @@ pub fn deterministic(seed: u64) -> (Rc<Foreground>, Arc<Background>) {
let executor = Arc::new(Deterministic::new(seed));
(
Rc::new(Foreground::Deterministic(executor.clone())),
Arc::new(Background::Deterministic(executor)),
Arc::new(Background::Deterministic { executor }),
)
}
impl<T> Task<T> {
pub fn ready(value: T) -> Self {
Self::Ready(Some(value))
}
fn local(any_task: AnyLocalTask) -> Self {
Self::Local {
any_task,
@@ -600,12 +675,24 @@ impl<T> Task<T> {
pub fn detach(self) {
match self {
Task::Ready(_) => {}
Task::Local { any_task, .. } => any_task.detach(),
Task::Send { any_task, .. } => any_task.detach(),
}
}
}
impl<T: 'static, E: 'static + Display> Task<Result<T, E>> {
pub fn detach_and_log_err(self, cx: &mut MutableAppContext) {
cx.spawn(|_| async move {
if let Err(err) = self.await {
log::error!("{}", err);
}
})
.detach();
}
}
impl<T: Send> Task<T> {
fn send(any_task: AnyTask) -> Self {
Self::Send {
@@ -618,6 +705,7 @@ impl<T: Send> Task<T> {
impl<T: fmt::Debug> fmt::Debug for Task<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Task::Ready(value) => value.fmt(f),
Task::Local { any_task, .. } => any_task.fmt(f),
Task::Send { any_task, .. } => any_task.fmt(f),
}
@@ -629,6 +717,7 @@ impl<T: 'static> Future for Task<T> {
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match unsafe { self.get_unchecked_mut() } {
Task::Ready(value) => Poll::Ready(value.take().unwrap()),
Task::Local { any_task, .. } => {
any_task.poll(cx).map(|value| *value.downcast().unwrap())
}

View File

@@ -157,6 +157,17 @@ impl FontCache {
bounds.width() * self.em_scale(font_id, font_size)
}
pub fn em_advance(&self, font_id: FontId, font_size: f32) -> f32 {
let glyph_id;
let advance;
{
let state = self.0.read();
glyph_id = state.fonts.glyph_for_char(font_id, 'm').unwrap();
advance = state.fonts.advance(font_id, glyph_id).unwrap();
}
advance.x() * self.em_scale(font_id, font_size)
}
pub fn line_height(&self, font_id: FontId, font_size: f32) -> f32 {
let height = self.metric(font_id, |m| m.bounding_box.height());
(height * self.em_scale(font_id, font_size)).ceil()

View File

@@ -27,14 +27,14 @@ pub struct TextStyle {
pub font_id: FontId,
pub font_size: f32,
pub font_properties: Properties,
pub underline: bool,
pub underline: Option<Color>,
}
#[derive(Clone, Debug, Default)]
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct HighlightStyle {
pub color: Color,
pub font_properties: Properties,
pub underline: bool,
pub underline: Option<Color>,
}
#[allow(non_camel_case_types)]
@@ -64,7 +64,7 @@ struct TextStyleJson {
#[serde(default)]
italic: bool,
#[serde(default)]
underline: bool,
underline: UnderlineStyleJson,
}
#[derive(Deserialize)]
@@ -74,7 +74,14 @@ struct HighlightStyleJson {
#[serde(default)]
italic: bool,
#[serde(default)]
underline: bool,
underline: UnderlineStyleJson,
}
#[derive(Deserialize)]
#[serde(untagged)]
enum UnderlineStyleJson {
Underlined(bool),
UnderlinedWithColor(Color),
}
impl TextStyle {
@@ -82,7 +89,7 @@ impl TextStyle {
font_family_name: impl Into<Arc<str>>,
font_size: f32,
font_properties: Properties,
underline: bool,
underline: Option<Color>,
color: Color,
font_cache: &FontCache,
) -> anyhow::Result<Self> {
@@ -116,7 +123,7 @@ impl TextStyle {
json.family,
json.size,
font_properties,
json.underline,
underline_from_json(json.underline, json.color),
json.color,
font_cache,
)
@@ -144,6 +151,10 @@ impl TextStyle {
font_cache.em_width(self.font_id, self.font_size)
}
pub fn em_advance(&self, font_cache: &FontCache) -> f32 {
font_cache.em_advance(self.font_id, self.font_size)
}
pub fn descent(&self, font_cache: &FontCache) -> f32 {
font_cache.metric(self.font_id, |m| m.descent) * self.em_scale(font_cache)
}
@@ -167,6 +178,12 @@ impl From<TextStyle> for HighlightStyle {
}
}
impl Default for UnderlineStyleJson {
fn default() -> Self {
Self::Underlined(false)
}
}
impl Default for TextStyle {
fn default() -> Self {
FONT_CACHE.with(|font_cache| {
@@ -199,7 +216,7 @@ impl HighlightStyle {
Self {
color: json.color,
font_properties,
underline: json.underline,
underline: underline_from_json(json.underline, json.color),
}
}
}
@@ -209,7 +226,7 @@ impl From<Color> for HighlightStyle {
Self {
color,
font_properties: Default::default(),
underline: false,
underline: None,
}
}
}
@@ -248,12 +265,20 @@ impl<'de> Deserialize<'de> for HighlightStyle {
Ok(Self {
color: serde_json::from_value(json).map_err(de::Error::custom)?,
font_properties: Properties::new(),
underline: false,
underline: None,
})
}
}
}
fn underline_from_json(json: UnderlineStyleJson, text_color: Color) -> Option<Color> {
match json {
UnderlineStyleJson::Underlined(false) => None,
UnderlineStyleJson::Underlined(true) => Some(text_color),
UnderlineStyleJson::UnderlinedWithColor(color) => Some(color),
}
}
fn properties_from_json(weight: Option<WeightJson>, italic: bool) -> Properties {
let weight = match weight.unwrap_or(WeightJson::normal) {
WeightJson::thin => Weight::THIN,

View File

@@ -23,6 +23,7 @@ struct Pending {
context: Option<Context>,
}
#[derive(Default)]
pub struct Keymap(Vec<Binding>);
pub struct Binding {
@@ -93,6 +94,10 @@ impl Matcher {
self.keymap.add_bindings(bindings);
}
pub fn clear_pending(&mut self) {
self.pending.clear();
}
pub fn push_keystroke(
&mut self,
keystroke: Keystroke,
@@ -149,24 +154,6 @@ impl Keymap {
}
}
pub mod menu {
use crate::action;
action!(SelectPrev);
action!(SelectNext);
}
impl Default for Keymap {
fn default() -> Self {
Self(vec![
Binding::new("up", menu::SelectPrev, Some("menu")),
Binding::new("ctrl-p", menu::SelectPrev, Some("menu")),
Binding::new("down", menu::SelectNext, Some("menu")),
Binding::new("ctrl-n", menu::SelectNext, Some("menu")),
])
}
}
impl Binding {
pub fn new<A: Action>(keystrokes: &str, action: A, context: Option<&str>) -> Self {
let context = if let Some(context) = context {

View File

@@ -12,7 +12,7 @@ use crate::{
fonts::{FontId, GlyphId, Metrics as FontMetrics, Properties as FontProperties},
geometry::{
rect::{RectF, RectI},
vector::{vec2f, Vector2F},
vector::Vector2F,
},
text_layout::{LineLayout, RunStyle},
AnyAction, ClipboardItem, Menu, Scene,
@@ -53,11 +53,14 @@ pub trait Platform: Send + Sync {
fn set_cursor_style(&self, style: CursorStyle);
fn local_timezone(&self) -> UtcOffset;
fn path_for_resource(&self, name: Option<&str>, extension: Option<&str>) -> Result<PathBuf>;
}
pub(crate) trait ForegroundPlatform {
fn on_become_active(&self, callback: Box<dyn FnMut()>);
fn on_resign_active(&self, callback: Box<dyn FnMut()>);
fn on_quit(&self, callback: Box<dyn FnMut()>);
fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>);
fn on_open_files(&self, callback: Box<dyn FnMut(Vec<PathBuf>)>);
fn run(&self, on_finish_launching: Box<dyn FnOnce() -> ()>);
@@ -102,13 +105,20 @@ pub trait WindowContext {
fn present_scene(&mut self, scene: Scene);
}
#[derive(Debug)]
pub struct WindowOptions<'a> {
pub bounds: RectF,
pub bounds: WindowBounds,
pub title: Option<&'a str>,
pub titlebar_appears_transparent: bool,
pub traffic_light_position: Option<Vector2F>,
}
#[derive(Debug)]
pub enum WindowBounds {
Maximized,
Fixed(RectF),
}
pub struct PathPromptOptions {
pub files: bool,
pub directories: bool,
@@ -138,6 +148,7 @@ pub trait FontSystem: Send + Sync {
) -> anyhow::Result<FontId>;
fn font_metrics(&self, font_id: FontId) -> FontMetrics;
fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF>;
fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<Vector2F>;
fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
fn rasterize_glyph(
&self,
@@ -154,7 +165,7 @@ pub trait FontSystem: Send + Sync {
impl<'a> Default for WindowOptions<'a> {
fn default() -> Self {
Self {
bounds: RectF::new(Default::default(), vec2f(1024.0, 768.0)),
bounds: WindowBounds::Maximized,
title: Default::default(),
titlebar_appears_transparent: Default::default(),
traffic_light_position: Default::default(),

View File

@@ -14,7 +14,11 @@ pub enum Event {
},
LeftMouseDown {
position: Vector2F,
ctrl: bool,
alt: bool,
shift: bool,
cmd: bool,
click_count: usize,
},
LeftMouseUp {
position: Vector2F,

View File

@@ -1,10 +1,4 @@
use crate::{geometry::vector::vec2f, keymap::Keystroke, platform::Event};
use cocoa::appkit::{
NSDeleteFunctionKey as DELETE_KEY, NSDownArrowFunctionKey as ARROW_DOWN_KEY,
NSLeftArrowFunctionKey as ARROW_LEFT_KEY, NSPageDownFunctionKey as PAGE_DOWN_KEY,
NSPageUpFunctionKey as PAGE_UP_KEY, NSRightArrowFunctionKey as ARROW_RIGHT_KEY,
NSUpArrowFunctionKey as ARROW_UP_KEY,
};
use cocoa::{
appkit::{NSEvent, NSEventModifierFlags, NSEventType},
base::{id, nil, YES},
@@ -12,11 +6,6 @@ use cocoa::{
};
use std::{ffi::CStr, os::raw::c_char};
const BACKSPACE_KEY: u16 = 0x7f;
const ENTER_KEY: u16 = 0x0d;
const ESCAPE_KEY: u16 = 0x1b;
const TAB_KEY: u16 = 0x09;
impl Event {
pub unsafe fn from_native(native_event: id, window_height: Option<f32>) -> Option<Self> {
let event_type = native_event.eventType();
@@ -39,18 +28,41 @@ impl Event {
.unwrap();
let unmodified_chars = if let Some(first_char) = unmodified_chars.chars().next() {
use cocoa::appkit::*;
const BACKSPACE_KEY: u16 = 0x7f;
const ENTER_KEY: u16 = 0x0d;
const ESCAPE_KEY: u16 = 0x1b;
const TAB_KEY: u16 = 0x09;
const SHIFT_TAB_KEY: u16 = 0x19;
#[allow(non_upper_case_globals)]
match first_char as u16 {
ARROW_UP_KEY => "up",
ARROW_DOWN_KEY => "down",
ARROW_LEFT_KEY => "left",
ARROW_RIGHT_KEY => "right",
PAGE_UP_KEY => "pageup",
PAGE_DOWN_KEY => "pagedown",
BACKSPACE_KEY => "backspace",
ENTER_KEY => "enter",
DELETE_KEY => "delete",
ESCAPE_KEY => "escape",
TAB_KEY => "tab",
SHIFT_TAB_KEY => "tab",
NSUpArrowFunctionKey => "up",
NSDownArrowFunctionKey => "down",
NSLeftArrowFunctionKey => "left",
NSRightArrowFunctionKey => "right",
NSPageUpFunctionKey => "pageup",
NSPageDownFunctionKey => "pagedown",
NSDeleteFunctionKey => "delete",
NSF1FunctionKey => "f1",
NSF2FunctionKey => "f2",
NSF3FunctionKey => "f3",
NSF4FunctionKey => "f4",
NSF5FunctionKey => "f5",
NSF6FunctionKey => "f6",
NSF7FunctionKey => "f7",
NSF8FunctionKey => "f8",
NSF9FunctionKey => "f9",
NSF10FunctionKey => "f10",
NSF11FunctionKey => "f11",
NSF12FunctionKey => "f12",
_ => unmodified_chars,
}
} else {
@@ -76,14 +88,17 @@ impl Event {
})
}
NSEventType::NSLeftMouseDown => {
let modifiers = native_event.modifierFlags();
window_height.map(|window_height| Self::LeftMouseDown {
position: vec2f(
native_event.locationInWindow().x as f32,
window_height - native_event.locationInWindow().y as f32,
),
cmd: native_event
.modifierFlags()
.contains(NSEventModifierFlags::NSCommandKeyMask),
ctrl: modifiers.contains(NSEventModifierFlags::NSControlKeyMask),
alt: modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask),
shift: modifiers.contains(NSEventModifierFlags::NSShiftKeyMask),
cmd: modifiers.contains(NSEventModifierFlags::NSCommandKeyMask),
click_count: native_event.clickCount() as usize,
})
}
NSEventType::NSLeftMouseUp => window_height.map(|window_height| Self::LeftMouseUp {

View File

@@ -69,6 +69,10 @@ impl platform::FontSystem for FontSystem {
self.0.read().typographic_bounds(font_id, glyph_id)
}
fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<Vector2F> {
self.0.read().advance(font_id, glyph_id)
}
fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
self.0.read().glyph_for_char(font_id, ch)
}
@@ -137,6 +141,10 @@ impl FontSystemState {
Ok(self.fonts[font_id.0].typographic_bounds(glyph_id)?)
}
fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<Vector2F> {
Ok(self.fonts[font_id.0].advance(glyph_id)?)
}
fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
self.fonts[font_id.0].glyph_for_char(ch)
}
@@ -417,21 +425,21 @@ mod tests {
let menlo_regular = RunStyle {
font_id: fonts.select_font(&menlo, &Properties::new()).unwrap(),
color: Default::default(),
underline: false,
underline: None,
};
let menlo_italic = RunStyle {
font_id: fonts
.select_font(&menlo, &Properties::new().style(Style::Italic))
.unwrap(),
color: Default::default(),
underline: false,
underline: None,
};
let menlo_bold = RunStyle {
font_id: fonts
.select_font(&menlo, &Properties::new().weight(Weight::BOLD))
.unwrap(),
color: Default::default(),
underline: false,
underline: None,
};
assert_ne!(menlo_regular, menlo_italic);
assert_ne!(menlo_regular, menlo_bold);
@@ -458,13 +466,13 @@ mod tests {
let zapfino_regular = RunStyle {
font_id: fonts.select_font(&zapfino, &Properties::new())?,
color: Default::default(),
underline: false,
underline: None,
};
let menlo = fonts.load_family("Menlo")?;
let menlo_regular = RunStyle {
font_id: fonts.select_font(&menlo, &Properties::new())?,
color: Default::default(),
underline: false,
underline: None,
};
let text = "This is, m𐍈re 𐍈r less, Zapfino!𐍈";
@@ -543,7 +551,7 @@ mod tests {
let style = RunStyle {
font_id: fonts.select_font(&font_ids, &Default::default()).unwrap(),
color: Default::default(),
underline: false,
underline: None,
};
let line = "\u{feff}";

View File

@@ -14,7 +14,9 @@ use cocoa::{
NSPasteboardTypeString, NSSavePanel, NSWindow,
},
base::{id, nil, selector, YES},
foundation::{NSArray, NSAutoreleasePool, NSData, NSInteger, NSString, NSURL},
foundation::{
NSArray, NSAutoreleasePool, NSBundle, NSData, NSInteger, NSString, NSUInteger, NSURL,
},
};
use core_foundation::{
base::{CFType, CFTypeRef, OSStatus, TCFType as _},
@@ -45,6 +47,9 @@ use std::{
};
use time::UtcOffset;
#[allow(non_upper_case_globals)]
const NSUTF8StringEncoding: NSUInteger = 4;
const MAC_PLATFORM_IVAR: &'static str = "platform";
static mut APP_CLASS: *const Class = ptr::null();
static mut APP_DELEGATE_CLASS: *const Class = ptr::null();
@@ -76,6 +81,10 @@ unsafe fn build_classes() {
sel!(applicationDidResignActive:),
did_resign_active as extern "C" fn(&mut Object, Sel, id),
);
decl.add_method(
sel!(applicationWillTerminate:),
will_terminate as extern "C" fn(&mut Object, Sel, id),
);
decl.add_method(
sel!(handleGPUIMenuItem:),
handle_menu_item as extern "C" fn(&mut Object, Sel, id),
@@ -95,6 +104,7 @@ pub struct MacForegroundPlatform(RefCell<MacForegroundPlatformState>);
pub struct MacForegroundPlatformState {
become_active: Option<Box<dyn FnMut()>>,
resign_active: Option<Box<dyn FnMut()>>,
quit: Option<Box<dyn FnMut()>>,
event: Option<Box<dyn FnMut(crate::Event) -> bool>>,
menu_command: Option<Box<dyn FnMut(&dyn AnyAction)>>,
open_files: Option<Box<dyn FnMut(Vec<PathBuf>)>>,
@@ -191,6 +201,10 @@ impl platform::ForegroundPlatform for MacForegroundPlatform {
self.0.borrow_mut().resign_active = Some(callback);
}
fn on_quit(&self, callback: Box<dyn FnMut()>) {
self.0.borrow_mut().quit = Some(callback);
}
fn on_event(&self, callback: Box<dyn FnMut(crate::Event) -> bool>) {
self.0.borrow_mut().event = Some(callback);
}
@@ -588,6 +602,27 @@ impl platform::Platform for MacPlatform {
UtcOffset::from_whole_seconds(seconds_from_gmt.try_into().unwrap()).unwrap()
}
}
fn path_for_resource(&self, name: Option<&str>, extension: Option<&str>) -> Result<PathBuf> {
unsafe {
let bundle: id = NSBundle::mainBundle();
if bundle.is_null() {
Err(anyhow!("app is not running inside a bundle"))
} else {
let name = name.map_or(nil, |name| ns_string(name));
let extension = extension.map_or(nil, |extension| ns_string(extension));
let path: id = msg_send![bundle, pathForResource: name ofType: extension];
if path.is_null() {
Err(anyhow!("resource could not be found"))
} else {
let len = msg_send![path, lengthOfBytesUsingEncoding: NSUTF8StringEncoding];
let bytes = path.UTF8String() as *const u8;
let path = str::from_utf8(slice::from_raw_parts(bytes, len)).unwrap();
Ok(PathBuf::from(path))
}
}
}
}
}
unsafe fn get_foreground_platform(object: &mut Object) -> &MacForegroundPlatform {
@@ -638,6 +673,13 @@ extern "C" fn did_resign_active(this: &mut Object, _: Sel, _: id) {
}
}
extern "C" fn will_terminate(this: &mut Object, _: Sel, _: id) {
let platform = unsafe { get_foreground_platform(this) };
if let Some(callback) = platform.0.borrow_mut().quit.as_mut() {
callback();
}
}
extern "C" fn open_files(this: &mut Object, _: Sel, _: id, paths: id) {
let paths = unsafe {
(0..paths.count())

View File

@@ -40,6 +40,7 @@ impl Renderer {
pub fn new(
device: metal::Device,
pixel_format: metal::MTLPixelFormat,
scale_factor: f32,
fonts: Arc<dyn platform::FontSystem>,
) -> Self {
let library = device
@@ -64,7 +65,7 @@ impl Renderer {
MTLResourceOptions::StorageModeManaged,
);
let sprite_cache = SpriteCache::new(device.clone(), vec2i(1024, 768), fonts);
let sprite_cache = SpriteCache::new(device.clone(), vec2i(1024, 768), scale_factor, fonts);
let image_cache = ImageCache::new(device.clone(), vec2i(1024, 768));
let path_atlases =
AtlasAllocator::new(device.clone(), build_path_atlas_texture_descriptor());
@@ -106,7 +107,7 @@ impl Renderer {
"path_atlas",
"path_atlas_vertex",
"path_atlas_fragment",
MTLPixelFormat::R8Unorm,
MTLPixelFormat::R16Float,
);
Self {
sprite_cache,
@@ -522,6 +523,8 @@ impl Renderer {
return;
}
self.sprite_cache.set_scale_factor(scale_factor);
let mut sprites_by_atlas = HashMap::new();
for glyph in glyphs {
@@ -530,7 +533,6 @@ impl Renderer {
glyph.font_size,
glyph.id,
glyph.origin,
scale_factor,
) {
// Snap sprite to pixel grid.
let origin = (glyph.origin * scale_factor).floor() + sprite.offset.to_f32();
@@ -825,7 +827,7 @@ fn build_path_atlas_texture_descriptor() -> metal::TextureDescriptor {
let texture_descriptor = metal::TextureDescriptor::new();
texture_descriptor.set_width(2048);
texture_descriptor.set_height(2048);
texture_descriptor.set_pixel_format(MTLPixelFormat::R8Unorm);
texture_descriptor.set_pixel_format(MTLPixelFormat::R16Float);
texture_descriptor
.set_usage(metal::MTLTextureUsage::RenderTarget | metal::MTLTextureUsage::ShaderRead);
texture_descriptor.set_storage_mode(metal::MTLStorageMode::Private);

View File

@@ -205,8 +205,6 @@ vertex SpriteFragmentInput sprite_vertex(
};
}
#define MAX_WINDINGS 32.
fragment float4 sprite_fragment(
SpriteFragmentInput input [[stage_in]],
texture2d<float> atlas [[ texture(GPUISpriteFragmentInputIndexAtlas) ]]
@@ -216,7 +214,7 @@ fragment float4 sprite_fragment(
float4 sample = atlas.sample(atlas_sampler, input.atlas_position);
float mask;
if (input.compute_winding) {
mask = 1. - abs(1. - fmod(sample.r * MAX_WINDINGS, 2.));
mask = 1. - abs(1. - fmod(sample.r, 2.));
} else {
mask = sample.a;
}
@@ -303,6 +301,6 @@ fragment float4 path_atlas_fragment(
);
float f = (input.st_position.x * input.st_position.x) - input.st_position.y;
float distance = f / length(gradient);
float alpha = saturate(0.5 - distance) / MAX_WINDINGS;
float alpha = saturate(0.5 - distance);
return float4(alpha, 0., 0., 1.);
}

View File

@@ -43,12 +43,14 @@ pub struct SpriteCache {
atlases: AtlasAllocator,
glyphs: HashMap<GlyphDescriptor, Option<GlyphSprite>>,
icons: HashMap<IconDescriptor, IconSprite>,
scale_factor: f32,
}
impl SpriteCache {
pub fn new(
device: metal::Device,
size: Vector2I,
scale_factor: f32,
fonts: Arc<dyn platform::FontSystem>,
) -> Self {
let descriptor = TextureDescriptor::new();
@@ -60,19 +62,29 @@ impl SpriteCache {
atlases: AtlasAllocator::new(device, descriptor),
glyphs: Default::default(),
icons: Default::default(),
scale_factor,
}
}
pub fn set_scale_factor(&mut self, scale_factor: f32) {
if scale_factor != self.scale_factor {
self.icons.clear();
self.glyphs.clear();
self.atlases.clear();
}
self.scale_factor = scale_factor;
}
pub fn render_glyph(
&mut self,
font_id: FontId,
font_size: f32,
glyph_id: GlyphId,
target_position: Vector2F,
scale_factor: f32,
) -> Option<GlyphSprite> {
const SUBPIXEL_VARIANTS: u8 = 4;
let scale_factor = self.scale_factor;
let target_position = target_position * scale_factor;
let fonts = &self.fonts;
let atlases = &mut self.atlases;

View File

@@ -1,8 +1,11 @@
use crate::{
executor,
geometry::vector::Vector2F,
geometry::{
rect::RectF,
vector::{vec2f, Vector2F},
},
keymap::Keystroke,
platform::{self, Event, WindowContext},
platform::{self, Event, WindowBounds, WindowContext},
Scene,
};
use block::ConcreteBlock;
@@ -25,7 +28,6 @@ use objc::{
runtime::{Class, Object, Protocol, Sel, BOOL, NO, YES},
sel, sel_impl,
};
use pathfinder_geometry::vector::vec2f;
use smol::Timer;
use std::{
any::Any,
@@ -158,7 +160,11 @@ impl Window {
unsafe {
let pool = NSAutoreleasePool::new(nil);
let frame = options.bounds.to_ns_rect();
let frame = match options.bounds {
WindowBounds::Maximized => RectF::new(Default::default(), vec2f(1024., 768.)),
WindowBounds::Fixed(rect) => rect,
}
.to_ns_rect();
let mut style_mask = NSWindowStyleMask::NSClosableWindowMask
| NSWindowStyleMask::NSMiniaturizableWindowMask
| NSWindowStyleMask::NSResizableWindowMask
@@ -177,6 +183,11 @@ impl Window {
);
assert!(!native_window.is_null());
if matches!(options.bounds, WindowBounds::Maximized) {
let screen = native_window.screen();
native_window.setFrame_display_(screen.visibleFrame(), YES);
}
let device =
metal::Device::system_default().expect("could not find default metal device");
@@ -205,7 +216,12 @@ impl Window {
synthetic_drag_counter: 0,
executor,
scene_to_render: Default::default(),
renderer: Renderer::new(device.clone(), PIXEL_FORMAT, fonts),
renderer: Renderer::new(
device.clone(),
PIXEL_FORMAT,
get_scale_factor(native_window),
fonts,
),
command_queue: device.new_command_queue(),
last_fresh_keydown: None,
layer,
@@ -405,10 +421,7 @@ impl platform::WindowContext for WindowState {
}
fn scale_factor(&self) -> f32 {
unsafe {
let screen: id = msg_send![self.native_window, screen];
NSScreen::backingScaleFactor(screen) as f32
}
get_scale_factor(self.native_window)
}
fn titlebar_height(&self) -> f32 {
@@ -427,6 +440,13 @@ impl platform::WindowContext for WindowState {
}
}
fn get_scale_factor(native_window: id) -> f32 {
unsafe {
let screen: id = msg_send![native_window, screen];
NSScreen::backingScaleFactor(screen) as f32
}
}
unsafe fn get_window_state(object: &Object) -> Rc<RefCell<WindowState>> {
let raw: *mut c_void = *object.get_ivar(WINDOW_STATE_IVAR);
let rc1 = Rc::from_raw(raw as *mut RefCell<WindowState>);

View File

@@ -1,8 +1,10 @@
use super::CursorStyle;
use crate::{AnyAction, ClipboardItem};
use anyhow::Result;
use super::{CursorStyle, WindowBounds};
use crate::{
geometry::vector::{vec2f, Vector2F},
AnyAction, ClipboardItem,
};
use anyhow::{anyhow, Result};
use parking_lot::Mutex;
use pathfinder_geometry::vector::Vector2F;
use std::{
any::Any,
cell::RefCell,
@@ -58,6 +60,8 @@ impl super::ForegroundPlatform for ForegroundPlatform {
fn on_resign_active(&self, _: Box<dyn FnMut()>) {}
fn on_quit(&self, _: Box<dyn FnMut()>) {}
fn on_event(&self, _: Box<dyn FnMut(crate::Event) -> bool>) {}
fn on_open_files(&self, _: Box<dyn FnMut(Vec<std::path::PathBuf>)>) {}
@@ -110,7 +114,10 @@ impl super::Platform for Platform {
options: super::WindowOptions,
_executor: Rc<super::executor::Foreground>,
) -> Box<dyn super::Window> {
Box::new(Window::new(options.bounds.size()))
Box::new(Window::new(match options.bounds {
WindowBounds::Maximized => vec2f(1024., 768.),
WindowBounds::Fixed(rect) => rect.size(),
}))
}
fn key_window_id(&self) -> Option<usize> {
@@ -148,6 +155,10 @@ impl super::Platform for Platform {
fn local_timezone(&self) -> UtcOffset {
UtcOffset::UTC
}
fn path_for_resource(&self, _name: Option<&str>, _extension: Option<&str>) -> Result<PathBuf> {
Err(anyhow!("app not running inside a bundle"))
}
}
impl Window {

View File

@@ -7,7 +7,13 @@ use std::{
},
};
use crate::{executor, platform, FontCache, MutableAppContext, Platform, TestAppContext};
use futures::StreamExt;
use smol::channel;
use crate::{
executor, platform, Entity, FontCache, Handle, MutableAppContext, Platform, Subscription,
TestAppContext,
};
#[cfg(test)]
#[ctor::ctor]
@@ -87,3 +93,47 @@ pub fn run_test(
}
}
}
pub struct Observation<T> {
rx: channel::Receiver<T>,
_subscription: Subscription,
}
impl<T> futures::Stream for Observation<T> {
type Item = T;
fn poll_next(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
self.rx.poll_next_unpin(cx)
}
}
pub fn observe<T: Entity>(entity: &impl Handle<T>, cx: &mut TestAppContext) -> Observation<()> {
let (tx, rx) = smol::channel::unbounded();
let _subscription = cx.update(|cx| {
cx.observe(entity, move |_, _| {
let _ = smol::block_on(tx.send(()));
})
});
Observation { rx, _subscription }
}
pub fn subscribe<T: Entity>(
entity: &impl Handle<T>,
cx: &mut TestAppContext,
) -> Observation<T::Event>
where
T::Event: Clone,
{
let (tx, rx) = smol::channel::unbounded();
let _subscription = cx.update(|cx| {
cx.subscribe(entity, move |_, event, _| {
let _ = smol::block_on(tx.send(event.clone()));
})
});
Observation { rx, _subscription }
}

View File

@@ -28,7 +28,7 @@ pub struct TextLayoutCache {
pub struct RunStyle {
pub color: Color,
pub font_id: FontId,
pub underline: bool,
pub underline: Option<Color>,
}
impl TextLayoutCache {
@@ -167,7 +167,7 @@ impl<'a> Hash for CacheKeyRef<'a> {
#[derive(Default, Debug)]
pub struct Line {
layout: Arc<LineLayout>,
style_runs: SmallVec<[(u32, Color, bool); 32]>,
style_runs: SmallVec<[(u32, Color, Option<Color>); 32]>,
}
#[derive(Default, Debug)]
@@ -249,7 +249,7 @@ impl Line {
let mut style_runs = self.style_runs.iter();
let mut run_end = 0;
let mut color = Color::black();
let mut underline_start = None;
let mut underline = None;
for run in &self.layout.runs {
let max_glyph_width = cx
@@ -259,33 +259,20 @@ impl Line {
for glyph in &run.glyphs {
let glyph_origin = origin + baseline_offset + glyph.position;
if glyph_origin.x() + max_glyph_width < visible_bounds.origin().x() {
continue;
}
if glyph_origin.x() > visible_bounds.upper_right().x() {
break;
}
let mut finished_underline = None;
if glyph.index >= run_end {
if let Some((run_len, run_color, run_underlined)) = style_runs.next() {
if let Some(underline_origin) = underline_start {
if !*run_underlined || *run_color != color {
cx.scene.push_underline(scene::Quad {
bounds: RectF::from_points(
underline_origin,
glyph_origin + vec2f(0., 1.),
),
background: Some(color),
border: Default::default(),
corner_radius: 0.,
});
underline_start = None;
if let Some((run_len, run_color, run_underline_color)) = style_runs.next() {
if let Some((_, underline_color)) = underline {
if *run_underline_color != Some(underline_color) {
finished_underline = underline.take();
}
}
if *run_underlined {
underline_start.get_or_insert(glyph_origin);
if let Some(run_underline_color) = run_underline_color {
underline.get_or_insert((glyph_origin, *run_underline_color));
}
run_end += *run_len as usize;
@@ -293,20 +280,23 @@ impl Line {
} else {
run_end = self.layout.len;
color = Color::black();
if let Some(underline_origin) = underline_start.take() {
cx.scene.push_underline(scene::Quad {
bounds: RectF::from_points(
underline_origin,
glyph_origin + vec2f(0., 1.),
),
background: Some(color),
border: Default::default(),
corner_radius: 0.,
});
}
finished_underline = underline.take();
}
}
if glyph_origin.x() + max_glyph_width < visible_bounds.origin().x() {
continue;
}
if let Some((underline_origin, underline_color)) = finished_underline {
cx.scene.push_underline(scene::Quad {
bounds: RectF::from_points(underline_origin, glyph_origin + vec2f(0., 1.)),
background: Some(underline_color),
border: Default::default(),
corner_radius: 0.,
});
}
cx.scene.push_glyph(scene::Glyph {
font_id: run.font_id,
font_size: self.layout.font_size,
@@ -317,12 +307,12 @@ impl Line {
}
}
if let Some(underline_start) = underline_start.take() {
if let Some((underline_start, underline_color)) = underline.take() {
let line_end = origin + baseline_offset + vec2f(self.layout.width, 0.);
cx.scene.push_underline(scene::Quad {
bounds: RectF::from_points(underline_start, line_end + vec2f(0., 1.)),
background: Some(color),
background: Some(underline_color),
border: Default::default(),
corner_radius: 0.,
});
@@ -597,7 +587,7 @@ impl LineWrapper {
RunStyle {
font_id: self.font_id,
color: Default::default(),
underline: false,
underline: None,
},
)],
)
@@ -681,7 +671,7 @@ mod tests {
let normal = RunStyle {
font_id,
color: Default::default(),
underline: false,
underline: None,
};
let bold = RunStyle {
font_id: font_cache
@@ -694,7 +684,7 @@ mod tests {
)
.unwrap(),
color: Default::default(),
underline: false,
underline: None,
};
let text = "aa bbb cccc ddddd eeee";

View File

@@ -42,7 +42,7 @@ impl Select {
render_item: F,
) -> Self {
Self {
handle: cx.handle().downgrade(),
handle: cx.weak_handle(),
render_item: Box::new(render_item),
selected_item_ix: 0,
item_count,

View File

@@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2018"
[lib]
path = "src/gpui_macros.rs"
proc-macro = true
[dependencies]

16
crates/journal/Cargo.toml Normal file
View File

@@ -0,0 +1,16 @@
[package]
name = "journal"
version = "0.1.0"
edition = "2021"
[lib]
path = "src/journal.rs"
[dependencies]
editor = { path = "../editor" }
gpui = { path = "../gpui" }
util = { path = "../util" }
workspace = { path = "../workspace" }
chrono = "0.4"
dirs = "4.0"
log = "0.4"

View File

@@ -0,0 +1,75 @@
use chrono::{Datelike, Local, Timelike};
use editor::{Autoscroll, Editor};
use gpui::{action, keymap::Binding, MutableAppContext};
use std::{fs::OpenOptions, sync::Arc};
use util::TryFutureExt as _;
use workspace::AppState;
action!(NewJournalEntry);
pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
cx.add_bindings(vec![Binding::new("ctrl-alt-cmd-j", NewJournalEntry, None)]);
cx.add_global_action(move |_: &NewJournalEntry, cx| new_journal_entry(app_state.clone(), cx));
}
pub fn new_journal_entry(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
let now = Local::now();
let home_dir = match dirs::home_dir() {
Some(home_dir) => home_dir,
None => {
log::error!("can't determine home directory");
return;
}
};
let journal_dir = home_dir.join("journal");
let month_dir = journal_dir
.join(format!("{:02}", now.year()))
.join(format!("{:02}", now.month()));
let entry_path = month_dir.join(format!("{:02}.md", now.day()));
let now = now.time();
let (pm, hour) = now.hour12();
let am_or_pm = if pm { "PM" } else { "AM" };
let entry_heading = format!("# {}:{:02} {}\n\n", hour, now.minute(), am_or_pm);
let create_entry = cx.background().spawn(async move {
std::fs::create_dir_all(month_dir)?;
OpenOptions::new()
.create(true)
.write(true)
.open(&entry_path)?;
Ok::<_, std::io::Error>((journal_dir, entry_path))
});
cx.spawn(|mut cx| {
async move {
let (journal_dir, entry_path) = create_entry.await?;
let workspace = cx
.update(|cx| workspace::open_paths(&[journal_dir], &app_state, cx))
.await;
let opened = workspace
.update(&mut cx, |workspace, cx| {
workspace.open_paths(&[entry_path], cx)
})
.await;
if let Some(Some(Ok(item))) = opened.first() {
if let Some(editor) = item.to_any().downcast::<Editor>() {
editor.update(&mut cx, |editor, cx| {
let len = editor.buffer().read(cx).read(cx).len();
editor.select_ranges([len..len], Some(Autoscroll::Center), cx);
if len > 0 {
editor.insert("\n\n", cx);
}
editor.insert(&entry_heading, cx);
});
}
}
Ok(())
}
.log_err()
})
.detach();
}

View File

@@ -0,0 +1,55 @@
[package]
name = "language"
version = "0.1.0"
edition = "2021"
[lib]
path = "src/language.rs"
[features]
test-support = [
"rand",
"collections/test-support",
"lsp/test-support",
"text/test-support",
"tree-sitter-rust",
"util/test-support",
]
[dependencies]
clock = { path = "../clock" }
collections = { path = "../collections" }
fuzzy = { path = "../fuzzy" }
gpui = { path = "../gpui" }
lsp = { path = "../lsp" }
rpc = { path = "../rpc" }
sum_tree = { path = "../sum_tree" }
text = { path = "../text" }
theme = { path = "../theme" }
util = { path = "../util" }
anyhow = "1.0.38"
async-trait = "0.1"
futures = "0.3"
lazy_static = "1.4"
log = "0.4"
parking_lot = "0.11.1"
postage = { version = "0.4.1", features = ["futures-traits"] }
rand = { version = "0.8.3", optional = true }
serde = { version = "1", features = ["derive"] }
similar = "1.3"
smallvec = { version = "1.6", features = ["union"] }
smol = "1.2"
tree-sitter = "0.20.0"
tree-sitter-rust = { version = "0.20.0", optional = true }
[dev-dependencies]
collections = { path = "../collections", features = ["test-support"] }
gpui = { path = "../gpui", features = ["test-support"] }
lsp = { path = "../lsp", features = ["test-support"] }
text = { path = "../text", features = ["test-support"] }
util = { path = "../util", features = ["test-support"] }
ctor = "0.1"
env_logger = "0.8"
rand = "0.8.3"
tree-sitter-rust = "0.20.0"
unindent = "0.1.7"

5
crates/language/build.rs Normal file
View File

@@ -0,0 +1,5 @@
fn main() {
if let Ok(bundled) = std::env::var("ZED_BUNDLE") {
println!("cargo:rustc-env=ZED_BUNDLE={}", bundled);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,211 @@
use crate::Diagnostic;
use collections::HashMap;
use std::{
cmp::{Ordering, Reverse},
iter,
ops::Range,
};
use sum_tree::{self, Bias, SumTree};
use text::{Anchor, FromAnchor, Point, ToOffset};
#[derive(Clone, Debug)]
pub struct DiagnosticSet {
diagnostics: SumTree<DiagnosticEntry<Anchor>>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DiagnosticEntry<T> {
pub range: Range<T>,
pub diagnostic: Diagnostic,
}
#[derive(Debug)]
pub struct DiagnosticGroup<T> {
pub entries: Vec<DiagnosticEntry<T>>,
pub primary_ix: usize,
}
#[derive(Clone, Debug)]
pub struct Summary {
start: Anchor,
end: Anchor,
min_start: Anchor,
max_end: Anchor,
count: usize,
}
impl DiagnosticSet {
pub fn from_sorted_entries<I>(iter: I, buffer: &text::BufferSnapshot) -> Self
where
I: IntoIterator<Item = DiagnosticEntry<Anchor>>,
{
Self {
diagnostics: SumTree::from_iter(iter, buffer),
}
}
pub fn new<I>(iter: I, buffer: &text::BufferSnapshot) -> Self
where
I: IntoIterator<Item = DiagnosticEntry<Point>>,
{
let mut entries = iter.into_iter().collect::<Vec<_>>();
entries.sort_unstable_by_key(|entry| (entry.range.start, Reverse(entry.range.end)));
Self {
diagnostics: SumTree::from_iter(
entries.into_iter().map(|entry| DiagnosticEntry {
range: buffer.anchor_before(entry.range.start)
..buffer.anchor_after(entry.range.end),
diagnostic: entry.diagnostic,
}),
buffer,
),
}
}
pub fn iter(&self) -> impl Iterator<Item = &DiagnosticEntry<Anchor>> {
self.diagnostics.iter()
}
pub fn range<'a, T, O>(
&'a self,
range: Range<T>,
buffer: &'a text::BufferSnapshot,
inclusive: bool,
) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
where
T: 'a + ToOffset,
O: FromAnchor,
{
let end_bias = if inclusive { Bias::Right } else { Bias::Left };
let range = buffer.anchor_before(range.start)..buffer.anchor_at(range.end, end_bias);
let mut cursor = self.diagnostics.filter::<_, ()>(
{
move |summary: &Summary| {
let start_cmp = range.start.cmp(&summary.max_end, buffer).unwrap();
let end_cmp = range.end.cmp(&summary.min_start, buffer).unwrap();
if inclusive {
start_cmp <= Ordering::Equal && end_cmp >= Ordering::Equal
} else {
start_cmp == Ordering::Less && end_cmp == Ordering::Greater
}
}
},
buffer,
);
iter::from_fn({
move || {
if let Some(diagnostic) = cursor.item() {
cursor.next(buffer);
Some(diagnostic.resolve(buffer))
} else {
None
}
}
})
}
pub fn groups(&self, output: &mut Vec<DiagnosticGroup<Anchor>>, buffer: &text::BufferSnapshot) {
let mut groups = HashMap::default();
for entry in self.diagnostics.iter() {
groups
.entry(entry.diagnostic.group_id)
.or_insert(Vec::new())
.push(entry.clone());
}
let start_ix = output.len();
output.extend(groups.into_values().filter_map(|mut entries| {
entries.sort_unstable_by(|a, b| a.range.start.cmp(&b.range.start, buffer).unwrap());
entries
.iter()
.position(|entry| entry.diagnostic.is_primary)
.map(|primary_ix| DiagnosticGroup {
entries,
primary_ix,
})
}));
output[start_ix..].sort_unstable_by(|a, b| {
a.entries[a.primary_ix]
.range
.start
.cmp(&b.entries[b.primary_ix].range.start, buffer)
.unwrap()
});
}
pub fn group<'a, O: FromAnchor>(
&'a self,
group_id: usize,
buffer: &'a text::BufferSnapshot,
) -> impl 'a + Iterator<Item = DiagnosticEntry<O>> {
self.iter()
.filter(move |entry| entry.diagnostic.group_id == group_id)
.map(|entry| entry.resolve(buffer))
}
}
impl Default for DiagnosticSet {
fn default() -> Self {
Self {
diagnostics: Default::default(),
}
}
}
impl sum_tree::Item for DiagnosticEntry<Anchor> {
type Summary = Summary;
fn summary(&self) -> Self::Summary {
Summary {
start: self.range.start.clone(),
end: self.range.end.clone(),
min_start: self.range.start.clone(),
max_end: self.range.end.clone(),
count: 1,
}
}
}
impl DiagnosticEntry<Anchor> {
pub fn resolve<O: FromAnchor>(&self, buffer: &text::BufferSnapshot) -> DiagnosticEntry<O> {
DiagnosticEntry {
range: O::from_anchor(&self.range.start, buffer)
..O::from_anchor(&self.range.end, buffer),
diagnostic: self.diagnostic.clone(),
}
}
}
impl Default for Summary {
fn default() -> Self {
Self {
start: Anchor::min(),
end: Anchor::max(),
min_start: Anchor::max(),
max_end: Anchor::min(),
count: 0,
}
}
}
impl sum_tree::Summary for Summary {
type Context = text::BufferSnapshot;
fn add_summary(&mut self, other: &Self, buffer: &Self::Context) {
if other
.min_start
.cmp(&self.min_start, buffer)
.unwrap()
.is_lt()
{
self.min_start = other.min_start.clone();
}
if other.max_end.cmp(&self.max_end, buffer).unwrap().is_gt() {
self.max_end = other.max_end.clone();
}
self.start = other.start.clone();
self.end = other.end.clone();
self.count += other.count;
}
}

View File

@@ -0,0 +1,276 @@
mod buffer;
mod diagnostic_set;
mod highlight_map;
mod outline;
pub mod proto;
#[cfg(test)]
mod tests;
use anyhow::{anyhow, Result};
pub use buffer::Operation;
pub use buffer::*;
use collections::HashSet;
pub use diagnostic_set::DiagnosticEntry;
use gpui::AppContext;
use highlight_map::HighlightMap;
use lazy_static::lazy_static;
pub use outline::{Outline, OutlineItem};
use parking_lot::Mutex;
use serde::Deserialize;
use std::{ops::Range, path::Path, str, sync::Arc};
use theme::SyntaxTheme;
use tree_sitter::{self, Query};
pub use tree_sitter::{Parser, Tree};
lazy_static! {
pub static ref PLAIN_TEXT: Arc<Language> = Arc::new(Language::new(
LanguageConfig {
name: "Plain Text".to_string(),
path_suffixes: Default::default(),
brackets: Default::default(),
line_comment: None,
language_server: None,
},
None,
));
}
pub trait ToPointUtf16 {
fn to_point_utf16(self) -> PointUtf16;
}
#[derive(Default, Deserialize)]
pub struct LanguageConfig {
pub name: String,
pub path_suffixes: Vec<String>,
pub brackets: Vec<BracketPair>,
pub line_comment: Option<String>,
pub language_server: Option<LanguageServerConfig>,
}
#[derive(Default, Deserialize)]
pub struct LanguageServerConfig {
pub binary: String,
pub disk_based_diagnostic_sources: HashSet<String>,
pub disk_based_diagnostics_progress_token: Option<String>,
#[cfg(any(test, feature = "test-support"))]
#[serde(skip)]
pub fake_server: Option<(Arc<lsp::LanguageServer>, Arc<std::sync::atomic::AtomicBool>)>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct BracketPair {
pub start: String,
pub end: String,
pub close: bool,
pub newline: bool,
}
pub struct Language {
pub(crate) config: LanguageConfig,
pub(crate) grammar: Option<Arc<Grammar>>,
}
pub struct Grammar {
pub(crate) ts_language: tree_sitter::Language,
pub(crate) highlights_query: Query,
pub(crate) brackets_query: Query,
pub(crate) indents_query: Query,
pub(crate) outline_query: Query,
pub(crate) highlight_map: Mutex<HighlightMap>,
}
#[derive(Default)]
pub struct LanguageRegistry {
languages: Vec<Arc<Language>>,
}
impl LanguageRegistry {
pub fn new() -> Self {
Self::default()
}
pub fn add(&mut self, language: Arc<Language>) {
self.languages.push(language);
}
pub fn set_theme(&self, theme: &SyntaxTheme) {
for language in &self.languages {
language.set_theme(theme);
}
}
pub fn get_language(&self, name: &str) -> Option<&Arc<Language>> {
self.languages
.iter()
.find(|language| language.name() == name)
}
pub fn select_language(&self, path: impl AsRef<Path>) -> Option<&Arc<Language>> {
let path = path.as_ref();
let filename = path.file_name().and_then(|name| name.to_str());
let extension = path.extension().and_then(|name| name.to_str());
let path_suffixes = [extension, filename];
self.languages.iter().find(|language| {
language
.config
.path_suffixes
.iter()
.any(|suffix| path_suffixes.contains(&Some(suffix.as_str())))
})
}
}
impl Language {
pub fn new(config: LanguageConfig, ts_language: Option<tree_sitter::Language>) -> Self {
Self {
config,
grammar: ts_language.map(|ts_language| {
Arc::new(Grammar {
brackets_query: Query::new(ts_language, "").unwrap(),
highlights_query: Query::new(ts_language, "").unwrap(),
indents_query: Query::new(ts_language, "").unwrap(),
outline_query: Query::new(ts_language, "").unwrap(),
ts_language,
highlight_map: Default::default(),
})
}),
}
}
pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
let grammar = self
.grammar
.as_mut()
.and_then(Arc::get_mut)
.ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
grammar.highlights_query = Query::new(grammar.ts_language, source)?;
Ok(self)
}
pub fn with_brackets_query(mut self, source: &str) -> Result<Self> {
let grammar = self
.grammar
.as_mut()
.and_then(Arc::get_mut)
.ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
grammar.brackets_query = Query::new(grammar.ts_language, source)?;
Ok(self)
}
pub fn with_indents_query(mut self, source: &str) -> Result<Self> {
let grammar = self
.grammar
.as_mut()
.and_then(Arc::get_mut)
.ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
grammar.indents_query = Query::new(grammar.ts_language, source)?;
Ok(self)
}
pub fn with_outline_query(mut self, source: &str) -> Result<Self> {
let grammar = self
.grammar
.as_mut()
.and_then(Arc::get_mut)
.ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
grammar.outline_query = Query::new(grammar.ts_language, source)?;
Ok(self)
}
pub fn name(&self) -> &str {
self.config.name.as_str()
}
pub fn line_comment_prefix(&self) -> Option<&str> {
self.config.line_comment.as_deref()
}
pub fn start_server(
&self,
root_path: &Path,
cx: &AppContext,
) -> Result<Option<Arc<lsp::LanguageServer>>> {
if let Some(config) = &self.config.language_server {
#[cfg(any(test, feature = "test-support"))]
if let Some((server, started)) = &config.fake_server {
started.store(true, std::sync::atomic::Ordering::SeqCst);
return Ok(Some(server.clone()));
}
const ZED_BUNDLE: Option<&'static str> = option_env!("ZED_BUNDLE");
let binary_path = if ZED_BUNDLE.map_or(Ok(false), |b| b.parse())? {
cx.platform()
.path_for_resource(Some(&config.binary), None)?
} else {
Path::new(&config.binary).to_path_buf()
};
lsp::LanguageServer::new(&binary_path, root_path, cx.background().clone()).map(Some)
} else {
Ok(None)
}
}
pub fn disk_based_diagnostic_sources(&self) -> Option<&HashSet<String>> {
self.config
.language_server
.as_ref()
.map(|config| &config.disk_based_diagnostic_sources)
}
pub fn disk_based_diagnostics_progress_token(&self) -> Option<&String> {
self.config
.language_server
.as_ref()
.and_then(|config| config.disk_based_diagnostics_progress_token.as_ref())
}
pub fn brackets(&self) -> &[BracketPair] {
&self.config.brackets
}
pub fn set_theme(&self, theme: &SyntaxTheme) {
if let Some(grammar) = self.grammar.as_ref() {
*grammar.highlight_map.lock() =
HighlightMap::new(grammar.highlights_query.capture_names(), theme);
}
}
}
impl Grammar {
pub fn highlight_map(&self) -> HighlightMap {
self.highlight_map.lock().clone()
}
}
#[cfg(any(test, feature = "test-support"))]
impl LanguageServerConfig {
pub async fn fake(
executor: Arc<gpui::executor::Background>,
) -> (Self, lsp::FakeLanguageServer) {
let (server, fake) = lsp::LanguageServer::fake(executor).await;
fake.started
.store(false, std::sync::atomic::Ordering::SeqCst);
let started = fake.started.clone();
(
Self {
fake_server: Some((server, started)),
disk_based_diagnostics_progress_token: Some("fakeServer/check".to_string()),
..Default::default()
},
fake,
)
}
}
impl ToPointUtf16 for lsp::Position {
fn to_point_utf16(self) -> PointUtf16 {
PointUtf16::new(self.line, self.character)
}
}
pub fn range_from_lsp(range: lsp::Range) -> Range<PointUtf16> {
let start = PointUtf16::new(range.start.line, range.start.character);
let end = PointUtf16::new(range.end.line, range.end.character);
start..end
}

View File

@@ -0,0 +1,146 @@
use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{executor::Background, fonts::HighlightStyle};
use std::{ops::Range, sync::Arc};
#[derive(Debug)]
pub struct Outline<T> {
pub items: Vec<OutlineItem<T>>,
candidates: Vec<StringMatchCandidate>,
path_candidates: Vec<StringMatchCandidate>,
path_candidate_prefixes: Vec<usize>,
}
#[derive(Clone, Debug)]
pub struct OutlineItem<T> {
pub depth: usize,
pub range: Range<T>,
pub text: String,
pub highlight_ranges: Vec<(Range<usize>, HighlightStyle)>,
pub name_ranges: Vec<Range<usize>>,
}
impl<T> Outline<T> {
pub fn new(items: Vec<OutlineItem<T>>) -> Self {
let mut candidates = Vec::new();
let mut path_candidates = Vec::new();
let mut path_candidate_prefixes = Vec::new();
let mut path_text = String::new();
let mut path_stack = Vec::new();
for (id, item) in items.iter().enumerate() {
if item.depth < path_stack.len() {
path_stack.truncate(item.depth);
path_text.truncate(path_stack.last().copied().unwrap_or(0));
}
if !path_text.is_empty() {
path_text.push(' ');
}
path_candidate_prefixes.push(path_text.len());
path_text.push_str(&item.text);
path_stack.push(path_text.len());
let candidate_text = item
.name_ranges
.iter()
.map(|range| &item.text[range.start as usize..range.end as usize])
.collect::<String>();
path_candidates.push(StringMatchCandidate {
id,
char_bag: path_text.as_str().into(),
string: path_text.clone(),
});
candidates.push(StringMatchCandidate {
id,
char_bag: candidate_text.as_str().into(),
string: candidate_text,
});
}
Self {
candidates,
path_candidates,
path_candidate_prefixes,
items,
}
}
pub async fn search(&self, query: &str, executor: Arc<Background>) -> Vec<StringMatch> {
let query = query.trim_start();
let is_path_query = query.contains(' ');
let smart_case = query.chars().any(|c| c.is_uppercase());
let mut matches = fuzzy::match_strings(
if is_path_query {
&self.path_candidates
} else {
&self.candidates
},
query,
smart_case,
100,
&Default::default(),
executor.clone(),
)
.await;
matches.sort_unstable_by_key(|m| m.candidate_id);
let mut tree_matches = Vec::new();
let mut prev_item_ix = 0;
for mut string_match in matches {
let outline_match = &self.items[string_match.candidate_id];
if is_path_query {
let prefix_len = self.path_candidate_prefixes[string_match.candidate_id];
string_match
.positions
.retain(|position| *position >= prefix_len);
for position in &mut string_match.positions {
*position -= prefix_len;
}
} else {
let mut name_ranges = outline_match.name_ranges.iter();
let mut name_range = name_ranges.next().unwrap();
let mut preceding_ranges_len = 0;
for position in &mut string_match.positions {
while *position >= preceding_ranges_len + name_range.len() as usize {
preceding_ranges_len += name_range.len();
name_range = name_ranges.next().unwrap();
}
*position = name_range.start as usize + (*position - preceding_ranges_len);
}
}
let insertion_ix = tree_matches.len();
let mut cur_depth = outline_match.depth;
for (ix, item) in self.items[prev_item_ix..string_match.candidate_id]
.iter()
.enumerate()
.rev()
{
if cur_depth == 0 {
break;
}
let candidate_index = ix + prev_item_ix;
if item.depth == cur_depth - 1 {
tree_matches.insert(
insertion_ix,
StringMatch {
candidate_id: candidate_index,
score: Default::default(),
positions: Default::default(),
string: Default::default(),
},
);
cur_depth -= 1;
}
}
prev_item_ix = string_match.candidate_id + 1;
tree_matches.push(string_match);
}
tree_matches
}
}

View File

@@ -0,0 +1,367 @@
use crate::{diagnostic_set::DiagnosticEntry, Diagnostic, Operation};
use anyhow::{anyhow, Result};
use clock::ReplicaId;
use collections::HashSet;
use lsp::DiagnosticSeverity;
use rpc::proto;
use std::sync::Arc;
use text::*;
pub use proto::{Buffer, SelectionSet};
pub fn serialize_operation(operation: &Operation) -> proto::Operation {
proto::Operation {
variant: Some(match operation {
Operation::Buffer(text::Operation::Edit(edit)) => {
proto::operation::Variant::Edit(serialize_edit_operation(edit))
}
Operation::Buffer(text::Operation::Undo {
undo,
lamport_timestamp,
}) => proto::operation::Variant::Undo(proto::operation::Undo {
replica_id: undo.id.replica_id as u32,
local_timestamp: undo.id.value,
lamport_timestamp: lamport_timestamp.value,
ranges: undo
.ranges
.iter()
.map(|r| proto::Range {
start: r.start.0 as u64,
end: r.end.0 as u64,
})
.collect(),
counts: undo
.counts
.iter()
.map(|(edit_id, count)| proto::UndoCount {
replica_id: edit_id.replica_id as u32,
local_timestamp: edit_id.value,
count: *count,
})
.collect(),
version: From::from(&undo.version),
}),
Operation::UpdateSelections {
replica_id,
selections,
lamport_timestamp,
} => proto::operation::Variant::UpdateSelections(proto::operation::UpdateSelections {
replica_id: *replica_id as u32,
lamport_timestamp: lamport_timestamp.value,
selections: serialize_selections(selections),
}),
Operation::UpdateDiagnostics {
diagnostics,
lamport_timestamp,
} => proto::operation::Variant::UpdateDiagnostics(proto::UpdateDiagnostics {
replica_id: lamport_timestamp.replica_id as u32,
lamport_timestamp: lamport_timestamp.value,
diagnostics: serialize_diagnostics(diagnostics.iter()),
}),
}),
}
}
pub fn serialize_edit_operation(operation: &EditOperation) -> proto::operation::Edit {
let ranges = operation
.ranges
.iter()
.map(|range| proto::Range {
start: range.start.0 as u64,
end: range.end.0 as u64,
})
.collect();
proto::operation::Edit {
replica_id: operation.timestamp.replica_id as u32,
local_timestamp: operation.timestamp.local,
lamport_timestamp: operation.timestamp.lamport,
version: From::from(&operation.version),
ranges,
new_text: operation.new_text.clone(),
}
}
pub fn serialize_undo_map_entry(
(edit_id, counts): (&clock::Local, &[(clock::Local, u32)]),
) -> proto::UndoMapEntry {
proto::UndoMapEntry {
replica_id: edit_id.replica_id as u32,
local_timestamp: edit_id.value,
counts: counts
.iter()
.map(|(undo_id, count)| proto::UndoCount {
replica_id: undo_id.replica_id as u32,
local_timestamp: undo_id.value,
count: *count,
})
.collect(),
}
}
pub fn serialize_buffer_fragment(fragment: &text::Fragment) -> proto::BufferFragment {
proto::BufferFragment {
replica_id: fragment.insertion_timestamp.replica_id as u32,
local_timestamp: fragment.insertion_timestamp.local,
lamport_timestamp: fragment.insertion_timestamp.lamport,
insertion_offset: fragment.insertion_offset as u32,
len: fragment.len as u32,
visible: fragment.visible,
deletions: fragment
.deletions
.iter()
.map(|clock| proto::VectorClockEntry {
replica_id: clock.replica_id as u32,
timestamp: clock.value,
})
.collect(),
max_undos: From::from(&fragment.max_undos),
}
}
pub fn serialize_selections(selections: &Arc<[Selection<Anchor>]>) -> Vec<proto::Selection> {
selections
.iter()
.map(|selection| proto::Selection {
id: selection.id as u64,
start: Some(serialize_anchor(&selection.start)),
end: Some(serialize_anchor(&selection.end)),
reversed: selection.reversed,
})
.collect()
}
pub fn serialize_diagnostics<'a>(
diagnostics: impl IntoIterator<Item = &'a DiagnosticEntry<Anchor>>,
) -> Vec<proto::Diagnostic> {
diagnostics
.into_iter()
.map(|entry| proto::Diagnostic {
start: Some(serialize_anchor(&entry.range.start)),
end: Some(serialize_anchor(&entry.range.end)),
message: entry.diagnostic.message.clone(),
severity: match entry.diagnostic.severity {
DiagnosticSeverity::ERROR => proto::diagnostic::Severity::Error,
DiagnosticSeverity::WARNING => proto::diagnostic::Severity::Warning,
DiagnosticSeverity::INFORMATION => proto::diagnostic::Severity::Information,
DiagnosticSeverity::HINT => proto::diagnostic::Severity::Hint,
_ => proto::diagnostic::Severity::None,
} as i32,
group_id: entry.diagnostic.group_id as u64,
is_primary: entry.diagnostic.is_primary,
is_valid: entry.diagnostic.is_valid,
code: entry.diagnostic.code.clone(),
is_disk_based: entry.diagnostic.is_disk_based,
})
.collect()
}
fn serialize_anchor(anchor: &Anchor) -> proto::Anchor {
proto::Anchor {
replica_id: anchor.timestamp.replica_id as u32,
local_timestamp: anchor.timestamp.value,
offset: anchor.offset as u64,
bias: match anchor.bias {
Bias::Left => proto::Bias::Left as i32,
Bias::Right => proto::Bias::Right as i32,
},
}
}
pub fn deserialize_operation(message: proto::Operation) -> Result<Operation> {
Ok(
match message
.variant
.ok_or_else(|| anyhow!("missing operation variant"))?
{
proto::operation::Variant::Edit(edit) => {
Operation::Buffer(text::Operation::Edit(deserialize_edit_operation(edit)))
}
proto::operation::Variant::Undo(undo) => Operation::Buffer(text::Operation::Undo {
lamport_timestamp: clock::Lamport {
replica_id: undo.replica_id as ReplicaId,
value: undo.lamport_timestamp,
},
undo: UndoOperation {
id: clock::Local {
replica_id: undo.replica_id as ReplicaId,
value: undo.local_timestamp,
},
counts: undo
.counts
.into_iter()
.map(|c| {
(
clock::Local {
replica_id: c.replica_id as ReplicaId,
value: c.local_timestamp,
},
c.count,
)
})
.collect(),
ranges: undo
.ranges
.into_iter()
.map(|r| FullOffset(r.start as usize)..FullOffset(r.end as usize))
.collect(),
version: undo.version.into(),
},
}),
proto::operation::Variant::UpdateSelections(message) => {
let selections = message
.selections
.into_iter()
.filter_map(|selection| {
Some(Selection {
id: selection.id as usize,
start: deserialize_anchor(selection.start?)?,
end: deserialize_anchor(selection.end?)?,
reversed: selection.reversed,
goal: SelectionGoal::None,
})
})
.collect::<Vec<_>>();
Operation::UpdateSelections {
replica_id: message.replica_id as ReplicaId,
lamport_timestamp: clock::Lamport {
replica_id: message.replica_id as ReplicaId,
value: message.lamport_timestamp,
},
selections: Arc::from(selections),
}
}
proto::operation::Variant::UpdateDiagnostics(message) => Operation::UpdateDiagnostics {
diagnostics: deserialize_diagnostics(message.diagnostics),
lamport_timestamp: clock::Lamport {
replica_id: message.replica_id as ReplicaId,
value: message.lamport_timestamp,
},
},
},
)
}
pub fn deserialize_edit_operation(edit: proto::operation::Edit) -> EditOperation {
let ranges = edit
.ranges
.into_iter()
.map(|range| FullOffset(range.start as usize)..FullOffset(range.end as usize))
.collect();
EditOperation {
timestamp: InsertionTimestamp {
replica_id: edit.replica_id as ReplicaId,
local: edit.local_timestamp,
lamport: edit.lamport_timestamp,
},
version: edit.version.into(),
ranges,
new_text: edit.new_text,
}
}
pub fn deserialize_undo_map_entry(
entry: proto::UndoMapEntry,
) -> (clock::Local, Vec<(clock::Local, u32)>) {
(
clock::Local {
replica_id: entry.replica_id as u16,
value: entry.local_timestamp,
},
entry
.counts
.into_iter()
.map(|undo_count| {
(
clock::Local {
replica_id: undo_count.replica_id as u16,
value: undo_count.local_timestamp,
},
undo_count.count,
)
})
.collect(),
)
}
pub fn deserialize_buffer_fragment(
message: proto::BufferFragment,
ix: usize,
count: usize,
) -> Fragment {
Fragment {
id: locator::Locator::from_index(ix, count),
insertion_timestamp: InsertionTimestamp {
replica_id: message.replica_id as ReplicaId,
local: message.local_timestamp,
lamport: message.lamport_timestamp,
},
insertion_offset: message.insertion_offset as usize,
len: message.len as usize,
visible: message.visible,
deletions: HashSet::from_iter(message.deletions.into_iter().map(|entry| clock::Local {
replica_id: entry.replica_id as ReplicaId,
value: entry.timestamp,
})),
max_undos: From::from(message.max_undos),
}
}
pub fn deserialize_selections(selections: Vec<proto::Selection>) -> Arc<[Selection<Anchor>]> {
Arc::from(
selections
.into_iter()
.filter_map(|selection| {
Some(Selection {
id: selection.id as usize,
start: deserialize_anchor(selection.start?)?,
end: deserialize_anchor(selection.end?)?,
reversed: selection.reversed,
goal: SelectionGoal::None,
})
})
.collect::<Vec<_>>(),
)
}
pub fn deserialize_diagnostics(
diagnostics: Vec<proto::Diagnostic>,
) -> Arc<[DiagnosticEntry<Anchor>]> {
diagnostics
.into_iter()
.filter_map(|diagnostic| {
Some(DiagnosticEntry {
range: deserialize_anchor(diagnostic.start?)?..deserialize_anchor(diagnostic.end?)?,
diagnostic: Diagnostic {
severity: match proto::diagnostic::Severity::from_i32(diagnostic.severity)? {
proto::diagnostic::Severity::Error => DiagnosticSeverity::ERROR,
proto::diagnostic::Severity::Warning => DiagnosticSeverity::WARNING,
proto::diagnostic::Severity::Information => DiagnosticSeverity::INFORMATION,
proto::diagnostic::Severity::Hint => DiagnosticSeverity::HINT,
proto::diagnostic::Severity::None => return None,
},
message: diagnostic.message,
group_id: diagnostic.group_id as usize,
code: diagnostic.code,
is_valid: diagnostic.is_valid,
is_primary: diagnostic.is_primary,
is_disk_based: diagnostic.is_disk_based,
},
})
})
.collect()
}
fn deserialize_anchor(anchor: proto::Anchor) -> Option<Anchor> {
Some(Anchor {
timestamp: clock::Local {
replica_id: anchor.replica_id as ReplicaId,
value: anchor.local_timestamp,
},
offset: anchor.offset as usize,
bias: match proto::Bias::from_i32(anchor.bias)? {
proto::Bias::Left => Bias::Left,
proto::Bias::Right => Bias::Right,
},
})
}

1170
crates/language/src/tests.rs Normal file

File diff suppressed because it is too large Load Diff

31
crates/lsp/Cargo.toml Normal file
View File

@@ -0,0 +1,31 @@
[package]
name = "lsp"
version = "0.1.0"
edition = "2018"
[lib]
path = "src/lsp.rs"
[features]
test-support = ["async-pipe"]
[dependencies]
gpui = { path = "../gpui" }
util = { path = "../util" }
anyhow = "1.0"
async-pipe = { git = "https://github.com/routerify/async-pipe-rs", rev = "feeb77e83142a9ff837d0767652ae41bfc5d8e47", optional = true }
futures = "0.3"
log = "0.4"
lsp-types = "0.91"
parking_lot = "0.11"
postage = { version = "0.4.1", features = ["futures-traits"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["raw_value"] }
smol = "1.2"
[dev-dependencies]
gpui = { path = "../gpui", features = ["test-support"] }
util = { path = "../util", features = ["test-support"] }
async-pipe = { git = "https://github.com/routerify/async-pipe-rs", rev = "feeb77e83142a9ff837d0767652ae41bfc5d8e47" }
simplelog = "0.9"
unindent = "0.1.7"

738
crates/lsp/src/lsp.rs Normal file
View File

@@ -0,0 +1,738 @@
use anyhow::{anyhow, Context, Result};
use futures::{io::BufWriter, AsyncRead, AsyncWrite};
use gpui::{executor, Task};
use parking_lot::{Mutex, RwLock};
use postage::{barrier, oneshot, prelude::Stream, sink::Sink};
use serde::{Deserialize, Serialize};
use serde_json::{json, value::RawValue, Value};
use smol::{
channel,
io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader},
process::Command,
};
use std::{
collections::HashMap,
future::Future,
io::Write,
str::FromStr,
sync::{
atomic::{AtomicUsize, Ordering::SeqCst},
Arc,
},
};
use std::{path::Path, process::Stdio};
use util::TryFutureExt;
pub use lsp_types::*;
const JSON_RPC_VERSION: &'static str = "2.0";
const CONTENT_LEN_HEADER: &'static str = "Content-Length: ";
type NotificationHandler = Box<dyn Send + Sync + FnMut(&str)>;
type ResponseHandler = Box<dyn Send + FnOnce(Result<&str, Error>)>;
pub struct LanguageServer {
next_id: AtomicUsize,
outbound_tx: RwLock<Option<channel::Sender<Vec<u8>>>>,
notification_handlers: Arc<RwLock<HashMap<&'static str, NotificationHandler>>>,
response_handlers: Arc<Mutex<HashMap<usize, ResponseHandler>>>,
executor: Arc<executor::Background>,
io_tasks: Mutex<Option<(Task<Option<()>>, Task<Option<()>>)>>,
initialized: barrier::Receiver,
output_done_rx: Mutex<Option<barrier::Receiver>>,
}
pub struct Subscription {
method: &'static str,
notification_handlers: Arc<RwLock<HashMap<&'static str, NotificationHandler>>>,
}
#[derive(Serialize, Deserialize)]
struct Request<'a, T> {
jsonrpc: &'a str,
id: usize,
method: &'a str,
params: T,
}
#[derive(Serialize, Deserialize)]
struct AnyResponse<'a> {
id: usize,
#[serde(default)]
error: Option<Error>,
#[serde(borrow)]
result: Option<&'a RawValue>,
}
#[derive(Serialize, Deserialize)]
struct Notification<'a, T> {
#[serde(borrow)]
jsonrpc: &'a str,
#[serde(borrow)]
method: &'a str,
params: T,
}
#[derive(Deserialize)]
struct AnyNotification<'a> {
#[serde(borrow)]
method: &'a str,
#[serde(borrow)]
params: &'a RawValue,
}
#[derive(Debug, Serialize, Deserialize)]
struct Error {
message: String,
}
impl LanguageServer {
pub fn new(
binary_path: &Path,
root_path: &Path,
background: Arc<executor::Background>,
) -> Result<Arc<Self>> {
let mut server = Command::new(binary_path)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()?;
let stdin = server.stdin.take().unwrap();
let stdout = server.stdout.take().unwrap();
Self::new_internal(stdin, stdout, root_path, background)
}
fn new_internal<Stdin, Stdout>(
stdin: Stdin,
stdout: Stdout,
root_path: &Path,
executor: Arc<executor::Background>,
) -> Result<Arc<Self>>
where
Stdin: AsyncWrite + Unpin + Send + 'static,
Stdout: AsyncRead + Unpin + Send + 'static,
{
let mut stdin = BufWriter::new(stdin);
let mut stdout = BufReader::new(stdout);
let (outbound_tx, outbound_rx) = channel::unbounded::<Vec<u8>>();
let notification_handlers = Arc::new(RwLock::new(HashMap::<_, NotificationHandler>::new()));
let response_handlers = Arc::new(Mutex::new(HashMap::<_, ResponseHandler>::new()));
let input_task = executor.spawn(
{
let notification_handlers = notification_handlers.clone();
let response_handlers = response_handlers.clone();
async move {
let mut buffer = Vec::new();
loop {
buffer.clear();
stdout.read_until(b'\n', &mut buffer).await?;
stdout.read_until(b'\n', &mut buffer).await?;
let message_len: usize = std::str::from_utf8(&buffer)?
.strip_prefix(CONTENT_LEN_HEADER)
.ok_or_else(|| anyhow!("invalid header"))?
.trim_end()
.parse()?;
buffer.resize(message_len, 0);
stdout.read_exact(&mut buffer).await?;
if let Ok(AnyNotification { method, params }) =
serde_json::from_slice(&buffer)
{
if let Some(handler) = notification_handlers.write().get_mut(method) {
handler(params.get());
} else {
log::info!(
"unhandled notification {}:\n{}",
method,
serde_json::to_string_pretty(
&Value::from_str(params.get()).unwrap()
)
.unwrap()
);
}
} else if let Ok(AnyResponse { id, error, result }) =
serde_json::from_slice(&buffer)
{
if let Some(handler) = response_handlers.lock().remove(&id) {
if let Some(error) = error {
handler(Err(error));
} else if let Some(result) = result {
handler(Ok(result.get()));
} else {
handler(Ok("null"));
}
}
} else {
return Err(anyhow!(
"failed to deserialize message:\n{}",
std::str::from_utf8(&buffer)?
));
}
}
}
}
.log_err(),
);
let (output_done_tx, output_done_rx) = barrier::channel();
let output_task = executor.spawn(
async move {
let mut content_len_buffer = Vec::new();
while let Ok(message) = outbound_rx.recv().await {
content_len_buffer.clear();
write!(content_len_buffer, "{}", message.len()).unwrap();
stdin.write_all(CONTENT_LEN_HEADER.as_bytes()).await?;
stdin.write_all(&content_len_buffer).await?;
stdin.write_all("\r\n\r\n".as_bytes()).await?;
stdin.write_all(&message).await?;
stdin.flush().await?;
}
drop(output_done_tx);
Ok(())
}
.log_err(),
);
let (initialized_tx, initialized_rx) = barrier::channel();
let this = Arc::new(Self {
notification_handlers,
response_handlers,
next_id: Default::default(),
outbound_tx: RwLock::new(Some(outbound_tx)),
executor: executor.clone(),
io_tasks: Mutex::new(Some((input_task, output_task))),
initialized: initialized_rx,
output_done_rx: Mutex::new(Some(output_done_rx)),
});
let root_uri =
lsp_types::Url::from_file_path(root_path).map_err(|_| anyhow!("invalid root path"))?;
executor
.spawn({
let this = this.clone();
async move {
this.init(root_uri).log_err().await;
drop(initialized_tx);
}
})
.detach();
Ok(this)
}
async fn init(self: Arc<Self>, root_uri: lsp_types::Url) -> Result<()> {
#[allow(deprecated)]
let params = lsp_types::InitializeParams {
process_id: Default::default(),
root_path: Default::default(),
root_uri: Some(root_uri),
initialization_options: Default::default(),
capabilities: lsp_types::ClientCapabilities {
experimental: Some(json!({
"serverStatusNotification": true,
})),
window: Some(lsp_types::WindowClientCapabilities {
work_done_progress: Some(true),
..Default::default()
}),
..Default::default()
},
trace: Default::default(),
workspace_folders: Default::default(),
client_info: Default::default(),
locale: Default::default(),
};
let this = self.clone();
let request = Self::request_internal::<lsp_types::request::Initialize>(
&this.next_id,
&this.response_handlers,
this.outbound_tx.read().as_ref(),
params,
);
request.await?;
Self::notify_internal::<lsp_types::notification::Initialized>(
this.outbound_tx.read().as_ref(),
lsp_types::InitializedParams {},
)?;
Ok(())
}
pub fn shutdown(&self) -> Option<impl 'static + Send + Future<Output = Result<()>>> {
if let Some(tasks) = self.io_tasks.lock().take() {
let response_handlers = self.response_handlers.clone();
let outbound_tx = self.outbound_tx.write().take();
let next_id = AtomicUsize::new(self.next_id.load(SeqCst));
let mut output_done = self.output_done_rx.lock().take().unwrap();
Some(async move {
Self::request_internal::<lsp_types::request::Shutdown>(
&next_id,
&response_handlers,
outbound_tx.as_ref(),
(),
)
.await?;
Self::notify_internal::<lsp_types::notification::Exit>(outbound_tx.as_ref(), ())?;
drop(outbound_tx);
output_done.recv().await;
drop(tasks);
Ok(())
})
} else {
None
}
}
pub fn on_notification<T, F>(&self, mut f: F) -> Subscription
where
T: lsp_types::notification::Notification,
F: 'static + Send + Sync + FnMut(T::Params),
{
let prev_handler = self.notification_handlers.write().insert(
T::METHOD,
Box::new(
move |notification| match serde_json::from_str(notification) {
Ok(notification) => f(notification),
Err(err) => log::error!("error parsing notification {}: {}", T::METHOD, err),
},
),
);
assert!(
prev_handler.is_none(),
"registered multiple handlers for the same notification"
);
Subscription {
method: T::METHOD,
notification_handlers: self.notification_handlers.clone(),
}
}
pub fn request<T: lsp_types::request::Request>(
self: Arc<Self>,
params: T::Params,
) -> impl Future<Output = Result<T::Result>>
where
T::Result: 'static + Send,
{
let this = self.clone();
async move {
this.initialized.clone().recv().await;
Self::request_internal::<T>(
&this.next_id,
&this.response_handlers,
this.outbound_tx.read().as_ref(),
params,
)
.await
}
}
fn request_internal<T: lsp_types::request::Request>(
next_id: &AtomicUsize,
response_handlers: &Mutex<HashMap<usize, ResponseHandler>>,
outbound_tx: Option<&channel::Sender<Vec<u8>>>,
params: T::Params,
) -> impl 'static + Future<Output = Result<T::Result>>
where
T::Result: 'static + Send,
{
let id = next_id.fetch_add(1, SeqCst);
let message = serde_json::to_vec(&Request {
jsonrpc: JSON_RPC_VERSION,
id,
method: T::METHOD,
params,
})
.unwrap();
let mut response_handlers = response_handlers.lock();
let (mut tx, mut rx) = oneshot::channel();
response_handlers.insert(
id,
Box::new(move |result| {
let response = match result {
Ok(response) => {
serde_json::from_str(response).context("failed to deserialize response")
}
Err(error) => Err(anyhow!("{}", error.message)),
};
let _ = tx.try_send(response);
}),
);
let send = outbound_tx
.as_ref()
.ok_or_else(|| {
anyhow!("tried to send a request to a language server that has been shut down")
})
.and_then(|outbound_tx| {
outbound_tx.try_send(message)?;
Ok(())
});
async move {
send?;
rx.recv().await.unwrap()
}
}
pub fn notify<T: lsp_types::notification::Notification>(
self: &Arc<Self>,
params: T::Params,
) -> impl Future<Output = Result<()>> {
let this = self.clone();
async move {
this.initialized.clone().recv().await;
Self::notify_internal::<T>(this.outbound_tx.read().as_ref(), params)?;
Ok(())
}
}
fn notify_internal<T: lsp_types::notification::Notification>(
outbound_tx: Option<&channel::Sender<Vec<u8>>>,
params: T::Params,
) -> Result<()> {
let message = serde_json::to_vec(&Notification {
jsonrpc: JSON_RPC_VERSION,
method: T::METHOD,
params,
})
.unwrap();
let outbound_tx = outbound_tx
.as_ref()
.ok_or_else(|| anyhow!("tried to notify a language server that has been shut down"))?;
outbound_tx.try_send(message)?;
Ok(())
}
}
impl Drop for LanguageServer {
fn drop(&mut self) {
if let Some(shutdown) = self.shutdown() {
self.executor.spawn(shutdown).detach();
}
}
}
impl Subscription {
pub fn detach(mut self) {
self.method = "";
}
}
impl Drop for Subscription {
fn drop(&mut self) {
self.notification_handlers.write().remove(self.method);
}
}
#[cfg(any(test, feature = "test-support"))]
pub struct FakeLanguageServer {
buffer: Vec<u8>,
stdin: smol::io::BufReader<async_pipe::PipeReader>,
stdout: smol::io::BufWriter<async_pipe::PipeWriter>,
pub started: Arc<std::sync::atomic::AtomicBool>,
}
#[cfg(any(test, feature = "test-support"))]
pub struct RequestId<T> {
id: usize,
_type: std::marker::PhantomData<T>,
}
#[cfg(any(test, feature = "test-support"))]
impl LanguageServer {
pub async fn fake(executor: Arc<executor::Background>) -> (Arc<Self>, FakeLanguageServer) {
let stdin = async_pipe::pipe();
let stdout = async_pipe::pipe();
let mut fake = FakeLanguageServer {
stdin: smol::io::BufReader::new(stdin.1),
stdout: smol::io::BufWriter::new(stdout.0),
buffer: Vec::new(),
started: Arc::new(std::sync::atomic::AtomicBool::new(true)),
};
let server = Self::new_internal(stdin.0, stdout.1, Path::new("/"), executor).unwrap();
let (init_id, _) = fake.receive_request::<request::Initialize>().await;
fake.respond(init_id, InitializeResult::default()).await;
fake.receive_notification::<notification::Initialized>()
.await;
(server, fake)
}
}
#[cfg(any(test, feature = "test-support"))]
impl FakeLanguageServer {
pub async fn notify<T: notification::Notification>(&mut self, params: T::Params) {
if !self.started.load(std::sync::atomic::Ordering::SeqCst) {
panic!("can't simulate an LSP notification before the server has been started");
}
let message = serde_json::to_vec(&Notification {
jsonrpc: JSON_RPC_VERSION,
method: T::METHOD,
params,
})
.unwrap();
self.send(message).await;
}
pub async fn respond<'a, T: request::Request>(
&mut self,
request_id: RequestId<T>,
result: T::Result,
) {
let result = serde_json::to_string(&result).unwrap();
let message = serde_json::to_vec(&AnyResponse {
id: request_id.id,
error: None,
result: Some(&RawValue::from_string(result).unwrap()),
})
.unwrap();
self.send(message).await;
}
pub async fn receive_request<T: request::Request>(&mut self) -> (RequestId<T>, T::Params) {
loop {
self.receive().await;
if let Ok(request) = serde_json::from_slice::<Request<T::Params>>(&self.buffer) {
assert_eq!(request.method, T::METHOD);
assert_eq!(request.jsonrpc, JSON_RPC_VERSION);
return (
RequestId {
id: request.id,
_type: std::marker::PhantomData,
},
request.params,
);
} else {
println!(
"skipping message in fake language server {:?}",
std::str::from_utf8(&self.buffer)
);
}
}
}
pub async fn receive_notification<T: notification::Notification>(&mut self) -> T::Params {
self.receive().await;
let notification = serde_json::from_slice::<Notification<T::Params>>(&self.buffer).unwrap();
assert_eq!(notification.method, T::METHOD);
notification.params
}
pub async fn start_progress(&mut self, token: impl Into<String>) {
self.notify::<notification::Progress>(ProgressParams {
token: NumberOrString::String(token.into()),
value: ProgressParamsValue::WorkDone(WorkDoneProgress::Begin(Default::default())),
})
.await;
}
pub async fn end_progress(&mut self, token: impl Into<String>) {
self.notify::<notification::Progress>(ProgressParams {
token: NumberOrString::String(token.into()),
value: ProgressParamsValue::WorkDone(WorkDoneProgress::End(Default::default())),
})
.await;
}
async fn send(&mut self, message: Vec<u8>) {
self.stdout
.write_all(CONTENT_LEN_HEADER.as_bytes())
.await
.unwrap();
self.stdout
.write_all((format!("{}", message.len())).as_bytes())
.await
.unwrap();
self.stdout.write_all("\r\n\r\n".as_bytes()).await.unwrap();
self.stdout.write_all(&message).await.unwrap();
self.stdout.flush().await.unwrap();
}
async fn receive(&mut self) {
self.buffer.clear();
self.stdin
.read_until(b'\n', &mut self.buffer)
.await
.unwrap();
self.stdin
.read_until(b'\n', &mut self.buffer)
.await
.unwrap();
let message_len: usize = std::str::from_utf8(&self.buffer)
.unwrap()
.strip_prefix(CONTENT_LEN_HEADER)
.unwrap()
.trim_end()
.parse()
.unwrap();
self.buffer.resize(message_len, 0);
self.stdin.read_exact(&mut self.buffer).await.unwrap();
}
}
#[cfg(test)]
mod tests {
use super::*;
use gpui::TestAppContext;
use simplelog::SimpleLogger;
use unindent::Unindent;
use util::test::temp_tree;
#[gpui::test]
async fn test_basic(cx: TestAppContext) {
let lib_source = r#"
fn fun() {
let hello = "world";
}
"#
.unindent();
let root_dir = temp_tree(json!({
"Cargo.toml": r#"
[package]
name = "temp"
version = "0.1.0"
edition = "2018"
"#.unindent(),
"src": {
"lib.rs": &lib_source
}
}));
let lib_file_uri =
lsp_types::Url::from_file_path(root_dir.path().join("src/lib.rs")).unwrap();
let server = cx.read(|cx| {
LanguageServer::new(
Path::new("rust-analyzer"),
root_dir.path(),
cx.background().clone(),
)
.unwrap()
});
server.next_idle_notification().await;
server
.notify::<lsp_types::notification::DidOpenTextDocument>(
lsp_types::DidOpenTextDocumentParams {
text_document: lsp_types::TextDocumentItem::new(
lib_file_uri.clone(),
"rust".to_string(),
0,
lib_source,
),
},
)
.await
.unwrap();
let hover = server
.request::<lsp_types::request::HoverRequest>(lsp_types::HoverParams {
text_document_position_params: lsp_types::TextDocumentPositionParams {
text_document: lsp_types::TextDocumentIdentifier::new(lib_file_uri),
position: lsp_types::Position::new(1, 21),
},
work_done_progress_params: Default::default(),
})
.await
.unwrap()
.unwrap();
assert_eq!(
hover.contents,
lsp_types::HoverContents::Markup(lsp_types::MarkupContent {
kind: lsp_types::MarkupKind::Markdown,
value: "&str".to_string()
})
);
}
#[gpui::test]
async fn test_fake(cx: TestAppContext) {
SimpleLogger::init(log::LevelFilter::Info, Default::default()).unwrap();
let (server, mut fake) = LanguageServer::fake(cx.background()).await;
let (message_tx, message_rx) = channel::unbounded();
let (diagnostics_tx, diagnostics_rx) = channel::unbounded();
server
.on_notification::<notification::ShowMessage, _>(move |params| {
message_tx.try_send(params).unwrap()
})
.detach();
server
.on_notification::<notification::PublishDiagnostics, _>(move |params| {
diagnostics_tx.try_send(params).unwrap()
})
.detach();
server
.notify::<notification::DidOpenTextDocument>(DidOpenTextDocumentParams {
text_document: TextDocumentItem::new(
Url::from_str("file://a/b").unwrap(),
"rust".to_string(),
0,
"".to_string(),
),
})
.await
.unwrap();
assert_eq!(
fake.receive_notification::<notification::DidOpenTextDocument>()
.await
.text_document
.uri
.as_str(),
"file://a/b"
);
fake.notify::<notification::ShowMessage>(ShowMessageParams {
typ: MessageType::ERROR,
message: "ok".to_string(),
})
.await;
fake.notify::<notification::PublishDiagnostics>(PublishDiagnosticsParams {
uri: Url::from_str("file://b/c").unwrap(),
version: Some(5),
diagnostics: vec![],
})
.await;
assert_eq!(message_rx.recv().await.unwrap().message, "ok");
assert_eq!(
diagnostics_rx.recv().await.unwrap().uri.as_str(),
"file://b/c"
);
drop(server);
let (shutdown_request, _) = fake.receive_request::<lsp_types::request::Shutdown>().await;
fake.respond(shutdown_request, ()).await;
fake.receive_notification::<lsp_types::notification::Exit>()
.await;
}
impl LanguageServer {
async fn next_idle_notification(self: &Arc<Self>) {
let (tx, rx) = channel::unbounded();
let _subscription =
self.on_notification::<ServerStatusNotification, _>(move |params| {
if params.quiescent {
tx.try_send(()).unwrap();
}
});
let _ = rx.recv().await;
}
}
pub enum ServerStatusNotification {}
impl lsp_types::notification::Notification for ServerStatusNotification {
type Params = ServerStatusParams;
const METHOD: &'static str = "experimental/serverStatus";
}
#[derive(Deserialize, Serialize, PartialEq, Eq, Clone)]
pub struct ServerStatusParams {
pub quiescent: bool,
}
}

18
crates/outline/Cargo.toml Normal file
View File

@@ -0,0 +1,18 @@
[package]
name = "outline"
version = "0.1.0"
edition = "2021"
[lib]
path = "src/outline.rs"
[dependencies]
editor = { path = "../editor" }
fuzzy = { path = "../fuzzy" }
gpui = { path = "../gpui" }
language = { path = "../language" }
text = { path = "../text" }
workspace = { path = "../workspace" }
ordered-float = "2.1.1"
postage = { version = "0.4", features = ["futures-traits"] }
smol = "1.2"

View File

@@ -0,0 +1,540 @@
use editor::{
display_map::ToDisplayPoint, Anchor, AnchorRangeExt, Autoscroll, Editor, EditorSettings,
ToPoint,
};
use fuzzy::StringMatch;
use gpui::{
action,
elements::*,
fonts::{self, HighlightStyle},
geometry::vector::Vector2F,
keymap::{self, Binding},
AppContext, Axis, Entity, MutableAppContext, RenderContext, View, ViewContext, ViewHandle,
WeakViewHandle,
};
use language::{Outline, Selection};
use ordered_float::OrderedFloat;
use postage::watch;
use std::{
cmp::{self, Reverse},
ops::Range,
sync::Arc,
};
use workspace::{
menu::{Confirm, SelectFirst, SelectLast, SelectNext, SelectPrev},
Settings, Workspace,
};
action!(Toggle);
pub fn init(cx: &mut MutableAppContext) {
cx.add_bindings([
Binding::new("cmd-shift-O", Toggle, Some("Editor")),
Binding::new("escape", Toggle, Some("OutlineView")),
]);
cx.add_action(OutlineView::toggle);
cx.add_action(OutlineView::confirm);
cx.add_action(OutlineView::select_prev);
cx.add_action(OutlineView::select_next);
cx.add_action(OutlineView::select_first);
cx.add_action(OutlineView::select_last);
}
struct OutlineView {
handle: WeakViewHandle<Self>,
active_editor: ViewHandle<Editor>,
outline: Outline<Anchor>,
selected_match_index: usize,
restore_state: Option<RestoreState>,
symbol_selection_id: Option<usize>,
matches: Vec<StringMatch>,
query_editor: ViewHandle<Editor>,
list_state: UniformListState,
settings: watch::Receiver<Settings>,
}
struct RestoreState {
scroll_position: Vector2F,
selections: Vec<Selection<usize>>,
}
pub enum Event {
Dismissed,
}
impl Entity for OutlineView {
type Event = Event;
fn release(&mut self, cx: &mut MutableAppContext) {
self.restore_active_editor(cx);
}
}
impl View for OutlineView {
fn ui_name() -> &'static str {
"OutlineView"
}
fn keymap_context(&self, _: &AppContext) -> keymap::Context {
let mut cx = Self::default_keymap_context();
cx.set.insert("menu".into());
cx
}
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
let settings = self.settings.borrow();
Flex::new(Axis::Vertical)
.with_child(
Container::new(ChildView::new(self.query_editor.id()).boxed())
.with_style(settings.theme.selector.input_editor.container)
.boxed(),
)
.with_child(Flexible::new(1.0, false, self.render_matches()).boxed())
.contained()
.with_style(settings.theme.selector.container)
.constrained()
.with_max_width(800.0)
.with_max_height(1200.0)
.aligned()
.top()
.named("outline view")
}
fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
cx.focus(&self.query_editor);
}
}
impl OutlineView {
fn new(
outline: Outline<Anchor>,
editor: ViewHandle<Editor>,
settings: watch::Receiver<Settings>,
cx: &mut ViewContext<Self>,
) -> Self {
let query_editor = cx.add_view(|cx| {
Editor::single_line(
{
let settings = settings.clone();
Arc::new(move |_| {
let settings = settings.borrow();
EditorSettings {
style: settings.theme.selector.input_editor.as_editor(),
tab_size: settings.tab_size,
soft_wrap: editor::SoftWrap::None,
}
})
},
cx,
)
});
cx.subscribe(&query_editor, Self::on_query_editor_event)
.detach();
let restore_state = editor.update(cx, |editor, cx| {
Some(RestoreState {
scroll_position: editor.scroll_position(cx),
selections: editor.local_selections::<usize>(cx),
})
});
let mut this = Self {
handle: cx.weak_handle(),
active_editor: editor,
matches: Default::default(),
selected_match_index: 0,
restore_state,
symbol_selection_id: None,
outline,
query_editor,
list_state: Default::default(),
settings,
};
this.update_matches(cx);
this
}
fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) {
if let Some(editor) = workspace
.active_item(cx)
.and_then(|item| item.to_any().downcast::<Editor>())
{
let settings = workspace.settings();
let buffer = editor
.read(cx)
.buffer()
.read(cx)
.read(cx)
.outline(Some(settings.borrow().theme.editor.syntax.as_ref()));
if let Some(outline) = buffer {
workspace.toggle_modal(cx, |cx, _| {
let view = cx.add_view(|cx| OutlineView::new(outline, editor, settings, cx));
cx.subscribe(&view, Self::on_event).detach();
view
})
}
}
}
fn select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext<Self>) {
if self.selected_match_index > 0 {
self.select(self.selected_match_index - 1, true, false, cx);
}
}
fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
if self.selected_match_index + 1 < self.matches.len() {
self.select(self.selected_match_index + 1, true, false, cx);
}
}
fn select_first(&mut self, _: &SelectFirst, cx: &mut ViewContext<Self>) {
self.select(0, true, false, cx);
}
fn select_last(&mut self, _: &SelectLast, cx: &mut ViewContext<Self>) {
self.select(self.matches.len().saturating_sub(1), true, false, cx);
}
fn select(&mut self, index: usize, navigate: bool, center: bool, cx: &mut ViewContext<Self>) {
self.selected_match_index = index;
self.list_state.scroll_to(if center {
ScrollTarget::Center(index)
} else {
ScrollTarget::Show(index)
});
if navigate {
let selected_match = &self.matches[self.selected_match_index];
let outline_item = &self.outline.items[selected_match.candidate_id];
self.symbol_selection_id = self.active_editor.update(cx, |active_editor, cx| {
let snapshot = active_editor.snapshot(cx).display_snapshot;
let buffer_snapshot = &snapshot.buffer_snapshot;
let start = outline_item.range.start.to_point(&buffer_snapshot);
let end = outline_item.range.end.to_point(&buffer_snapshot);
let display_rows = start.to_display_point(&snapshot).row()
..end.to_display_point(&snapshot).row() + 1;
active_editor.select_ranges([start..start], Some(Autoscroll::Center), cx);
active_editor.set_highlighted_rows(Some(display_rows));
Some(active_editor.newest_selection::<usize>(&buffer_snapshot).id)
});
cx.notify();
}
}
fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
self.restore_state.take();
cx.emit(Event::Dismissed);
}
fn restore_active_editor(&mut self, cx: &mut MutableAppContext) {
let symbol_selection_id = self.symbol_selection_id.take();
self.active_editor.update(cx, |editor, cx| {
editor.set_highlighted_rows(None);
if let Some((symbol_selection_id, restore_state)) =
symbol_selection_id.zip(self.restore_state.as_ref())
{
let newest_selection =
editor.newest_selection::<usize>(&editor.buffer().read(cx).read(cx));
if symbol_selection_id == newest_selection.id {
editor.set_scroll_position(restore_state.scroll_position, cx);
editor.update_selections(restore_state.selections.clone(), None, cx);
}
}
})
}
fn on_event(
workspace: &mut Workspace,
_: ViewHandle<Self>,
event: &Event,
cx: &mut ViewContext<Workspace>,
) {
match event {
Event::Dismissed => workspace.dismiss_modal(cx),
}
}
fn on_query_editor_event(
&mut self,
_: ViewHandle<Editor>,
event: &editor::Event,
cx: &mut ViewContext<Self>,
) {
match event {
editor::Event::Blurred => cx.emit(Event::Dismissed),
editor::Event::Edited => self.update_matches(cx),
_ => {}
}
}
fn update_matches(&mut self, cx: &mut ViewContext<Self>) {
let selected_index;
let navigate_to_selected_index;
let query = self.query_editor.update(cx, |buffer, cx| buffer.text(cx));
if query.is_empty() {
self.restore_active_editor(cx);
self.matches = self
.outline
.items
.iter()
.enumerate()
.map(|(index, _)| StringMatch {
candidate_id: index,
score: Default::default(),
positions: Default::default(),
string: Default::default(),
})
.collect();
let editor = self.active_editor.read(cx);
let buffer = editor.buffer().read(cx).read(cx);
let cursor_offset = editor.newest_selection::<usize>(&buffer).head();
selected_index = self
.outline
.items
.iter()
.enumerate()
.map(|(ix, item)| {
let range = item.range.to_offset(&buffer);
let distance_to_closest_endpoint = cmp::min(
(range.start as isize - cursor_offset as isize).abs() as usize,
(range.end as isize - cursor_offset as isize).abs() as usize,
);
let depth = if range.contains(&cursor_offset) {
Some(item.depth)
} else {
None
};
(ix, depth, distance_to_closest_endpoint)
})
.max_by_key(|(_, depth, distance)| (*depth, Reverse(*distance)))
.unwrap()
.0;
navigate_to_selected_index = false;
} else {
self.matches = smol::block_on(self.outline.search(&query, cx.background().clone()));
selected_index = self
.matches
.iter()
.enumerate()
.max_by_key(|(_, m)| OrderedFloat(m.score))
.map(|(ix, _)| ix)
.unwrap_or(0);
navigate_to_selected_index = !self.matches.is_empty();
}
self.select(selected_index, navigate_to_selected_index, true, cx);
}
fn render_matches(&self) -> ElementBox {
if self.matches.is_empty() {
let settings = self.settings.borrow();
return Container::new(
Label::new(
"No matches".into(),
settings.theme.selector.empty.label.clone(),
)
.boxed(),
)
.with_style(settings.theme.selector.empty.container)
.named("empty matches");
}
let handle = self.handle.clone();
let list = UniformList::new(
self.list_state.clone(),
self.matches.len(),
move |mut range, items, cx| {
let cx = cx.as_ref();
let view = handle.upgrade(cx).unwrap();
let view = view.read(cx);
let start = range.start;
range.end = cmp::min(range.end, view.matches.len());
items.extend(
view.matches[range]
.iter()
.enumerate()
.map(move |(ix, m)| view.render_match(m, start + ix)),
);
},
);
Container::new(list.boxed())
.with_margin_top(6.0)
.named("matches")
}
fn render_match(&self, string_match: &StringMatch, index: usize) -> ElementBox {
let settings = self.settings.borrow();
let style = if index == self.selected_match_index {
&settings.theme.selector.active_item
} else {
&settings.theme.selector.item
};
let outline_item = &self.outline.items[string_match.candidate_id];
Text::new(outline_item.text.clone(), style.label.text.clone())
.with_soft_wrap(false)
.with_highlights(combine_syntax_and_fuzzy_match_highlights(
&outline_item.text,
style.label.text.clone().into(),
&outline_item.highlight_ranges,
&string_match.positions,
))
.contained()
.with_padding_left(20. * outline_item.depth as f32)
.contained()
.with_style(style.container)
.boxed()
}
}
fn combine_syntax_and_fuzzy_match_highlights(
text: &str,
default_style: HighlightStyle,
syntax_ranges: &[(Range<usize>, HighlightStyle)],
match_indices: &[usize],
) -> Vec<(Range<usize>, HighlightStyle)> {
let mut result = Vec::new();
let mut match_indices = match_indices.iter().copied().peekable();
for (range, mut syntax_highlight) in syntax_ranges
.iter()
.cloned()
.chain([(usize::MAX..0, Default::default())])
{
syntax_highlight.font_properties.weight(Default::default());
// Add highlights for any fuzzy match characters before the next
// syntax highlight range.
while let Some(&match_index) = match_indices.peek() {
if match_index >= range.start {
break;
}
match_indices.next();
let end_index = char_ix_after(match_index, text);
let mut match_style = default_style;
match_style.font_properties.weight(fonts::Weight::BOLD);
result.push((match_index..end_index, match_style));
}
if range.start == usize::MAX {
break;
}
// Add highlights for any fuzzy match characters within the
// syntax highlight range.
let mut offset = range.start;
while let Some(&match_index) = match_indices.peek() {
if match_index >= range.end {
break;
}
match_indices.next();
if match_index > offset {
result.push((offset..match_index, syntax_highlight));
}
let mut end_index = char_ix_after(match_index, text);
while let Some(&next_match_index) = match_indices.peek() {
if next_match_index == end_index && next_match_index < range.end {
end_index = char_ix_after(next_match_index, text);
match_indices.next();
} else {
break;
}
}
let mut match_style = syntax_highlight;
match_style.font_properties.weight(fonts::Weight::BOLD);
result.push((match_index..end_index, match_style));
offset = end_index;
}
if offset < range.end {
result.push((offset..range.end, syntax_highlight));
}
}
result
}
fn char_ix_after(ix: usize, text: &str) -> usize {
ix + text[ix..].chars().next().unwrap().len_utf8()
}
#[cfg(test)]
mod tests {
use super::*;
use gpui::{color::Color, fonts::HighlightStyle};
#[test]
fn test_combine_syntax_and_fuzzy_match_highlights() {
let string = "abcdefghijklmnop";
let default = HighlightStyle::default();
let syntax_ranges = [
(
0..3,
HighlightStyle {
color: Color::red(),
..default
},
),
(
4..8,
HighlightStyle {
color: Color::green(),
..default
},
),
];
let match_indices = [4, 6, 7, 8];
assert_eq!(
combine_syntax_and_fuzzy_match_highlights(
&string,
default,
&syntax_ranges,
&match_indices,
),
&[
(
0..3,
HighlightStyle {
color: Color::red(),
..default
},
),
(
4..5,
HighlightStyle {
color: Color::green(),
font_properties: *fonts::Properties::default().weight(fonts::Weight::BOLD),
..default
},
),
(
5..6,
HighlightStyle {
color: Color::green(),
..default
},
),
(
6..8,
HighlightStyle {
color: Color::green(),
font_properties: *fonts::Properties::default().weight(fonts::Weight::BOLD),
..default
},
),
(
8..9,
HighlightStyle {
font_properties: *fonts::Properties::default().weight(fonts::Weight::BOLD),
..default
},
),
]
);
}
}

Some files were not shown because too many files have changed in this diff Show More