| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use gpui::{AnyElement, App, Entity, EntityId, Global, IntoElement, WeakEntity, Window}; |
| 4 | use ropey::Rope; |
| 5 | |
| 6 | use super::{ |
| 7 | InputBaseState, InputModeKind, |
| 8 | popovers::{CodeActionMenu, CompletionMenu, DiagnosticPopover, HoverPopover}, |
| 9 | search::SearchPanel, |
| 10 | }; |
| 11 | |
| 12 | struct InputOverlayRegistry<M: OverlayMode> { |
| 13 | hosts: HashMap<EntityId, (WeakEntity<InputBaseState<M>>, InputOverlayHost<M>)>, |
| 14 | } |
| 15 | |
| 16 | impl<M: OverlayMode> Default for InputOverlayRegistry<M> { |
| 17 | fn default() -> Self { |
| 18 | Self { |
| 19 | hosts: HashMap::default(), |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | impl<M: OverlayMode> Global for InputOverlayRegistry<M> {} |
| 25 | |
| 26 | struct InputOverlayHost<M: OverlayMode> { |
| 27 | search: Entity<SearchPanel<M>>, |
| 28 | search_signature: SearchSignature, |
| 29 | /// The language-feature popovers. Only a code editor has them. |
| 30 | lsp: Option<LspOverlays>, |
| 31 | } |
| 32 | |
| 33 | /// Identifies one version of the search session, as the search panel shows it. |
| 34 | /// |
| 35 | /// Everything the panel renders from is here, so an unchanged frame compares |
| 36 | /// equal and rebuilds nothing. `activation_revision` is what separates "the |
| 37 | /// session is open" from "the user just asked for search again": re-invoking an |
| 38 | /// open session leaves every other field alone, and without the revision the |
| 39 | /// second request would compare equal and be dropped. |
| 40 | #[derive(PartialEq, Eq, Default)] |
| 41 | struct SearchSignature { |
| 42 | open: bool, |
| 43 | replace_mode: bool, |
| 44 | query: String, |
| 45 | anchor_offset: Option<usize>, |
| 46 | activation_revision: u64, |
| 47 | } |
| 48 | |
| 49 | /// Identifies one version of an overlay's content. |
| 50 | /// |
| 51 | /// Sync runs on every frame, so the check for "did this change" must not touch |
| 52 | /// the content itself: the completion list can hold hundreds of items, each |
| 53 | /// with its own documentation. The engine bumps a revision when it swaps the |
| 54 | /// content, and the popovers are keyed by cheap identity instead — a revision, |
| 55 | /// an `Rc` pointer, or a range. |
| 56 | #[derive(PartialEq, Eq, Default)] |
| 57 | struct OverlaySignature { |
| 58 | open: bool, |
| 59 | revision: u64, |
| 60 | } |
| 61 | |
| 62 | /// The popovers driven by language features: completion, code actions, hover |
| 63 | /// and diagnostics. They belong to a code editor, so they are built and synced |
| 64 | /// by [`OverlayMode`], where the state's kind is concrete. |
| 65 | pub(crate) struct LspOverlays { |
| 66 | completion: Entity<CompletionMenu>, |
| 67 | code_actions: Entity<CodeActionMenu>, |
| 68 | hover: Option<Entity<HoverPopover>>, |
| 69 | diagnostic: Option<Entity<DiagnosticPopover>>, |
| 70 | completion_signature: OverlaySignature, |
| 71 | code_action_signature: OverlaySignature, |
| 72 | hover_signature: Option<std::ops::Range<usize>>, |
| 73 | diagnostic_signature: Option<std::rc::Rc<gpui_base::input::DiagnosticEntry>>, |
| 74 | } |
| 75 | |
| 76 | /// What the state read out of the engine for one sync pass. |
| 77 | /// |
| 78 | /// Deliberately free of content: only what is needed to decide whether a |
| 79 | /// popover is shown and whether it changed. The content is read from the state |
| 80 | /// again, on the frames where it actually did. |
| 81 | pub(crate) struct LspSnapshot { |
| 82 | completion: OverlaySignature, |
| 83 | completion_start: Option<usize>, |
| 84 | code_action: OverlaySignature, |
| 85 | hover: Option<std::ops::Range<usize>>, |
| 86 | diagnostic: Option<std::rc::Rc<gpui_base::input::DiagnosticEntry>>, |
| 87 | cursor: usize, |
| 88 | } |
| 89 | |
| 90 | impl LspSnapshot { |
| 91 | fn has_overlay(&self) -> bool { |
| 92 | self.completion.open |
| 93 | || self.code_action.open |
| 94 | || self.hover.is_some() |
| 95 | || self.diagnostic.is_some() |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /// What the UI layer needs from each kind of input. |
| 100 | /// |
| 101 | /// The UI layer is generic over the mode, so it cannot name the editor's state |
| 102 | /// type. This trait is the seam: the code-editor implementation is the only one |
| 103 | /// that builds language-feature popovers, and inside it the state is an |
| 104 | /// `Entity<EditorState>`, which those popovers take. |
| 105 | /// |
| 106 | /// Entirely internal: nothing public is generic over the mode any more. |
| 107 | pub(crate) trait OverlayMode: InputModeKind + Sized { |
| 108 | /// Reads the language-feature state this mode shows, if any. |
| 109 | fn lsp_snapshot(_state: &InputBaseState<Self>, _cx: &App) -> Option<LspSnapshot> { |
| 110 | None |
| 111 | } |
| 112 | |
| 113 | /// Installs the routing that lets an open menu consume actions first. |
| 114 | fn install_action_handler(_state: &Entity<InputBaseState<Self>>, _cx: &mut App) {} |
| 115 | |
| 116 | fn build_lsp( |
| 117 | _state: &Entity<InputBaseState<Self>>, |
| 118 | _window: &mut Window, |
| 119 | _cx: &mut App, |
| 120 | ) -> Option<LspOverlays> { |
| 121 | None |
| 122 | } |
| 123 | |
| 124 | fn sync_lsp( |
| 125 | _lsp: &mut LspOverlays, |
| 126 | _state: &Entity<InputBaseState<Self>>, |
| 127 | _snapshot: &LspSnapshot, |
| 128 | _window: &mut Window, |
| 129 | _cx: &mut App, |
| 130 | ) { |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | impl OverlayMode for crate::input::InputMode {} |
| 135 | impl OverlayMode for crate::input::TextareaMode {} |
| 136 | |
| 137 | impl OverlayMode for crate::input::EditorMode { |
| 138 | fn install_action_handler(state: &Entity<InputBaseState<Self>>, cx: &mut App) { |
| 139 | let id = state.entity_id(); |
| 140 | state.update(cx, move |state, _| { |
| 141 | state.set_overlay_action_handler(move |kind, action, window, cx| { |
| 142 | let menus = cx |
| 143 | .try_global::<InputOverlayRegistry<Self>>() |
| 144 | .and_then(|registry| registry.hosts.get(&id)) |
| 145 | .and_then(|(_, host)| host.lsp.as_ref()) |
| 146 | .map(|lsp| (lsp.completion.clone(), lsp.code_actions.clone())); |
| 147 | let Some((completion, code_actions)) = menus else { |
| 148 | return false; |
| 149 | }; |
| 150 | match kind { |
| 151 | gpui_base::input::InputOverlayKind::Completion => { |
| 152 | completion.update(cx, |menu, cx| menu.handle_action(action, window, cx)) |
| 153 | } |
| 154 | gpui_base::input::InputOverlayKind::CodeAction => { |
| 155 | code_actions.update(cx, |menu, cx| menu.handle_action(action, window, cx)) |
| 156 | } |
| 157 | } |
| 158 | }); |
| 159 | }); |
| 160 | } |
| 161 | |
| 162 | fn lsp_snapshot(state: &InputBaseState<Self>, _cx: &App) -> Option<LspSnapshot> { |
| 163 | let completion = state.completion_menu_state(); |
| 164 | let code_actions = state.code_action_menu_state(); |
| 165 | Some(LspSnapshot { |
| 166 | completion: OverlaySignature { |
| 167 | open: completion.open, |
| 168 | revision: completion.revision(), |
| 169 | }, |
| 170 | completion_start: completion.trigger_start_offset, |
| 171 | code_action: OverlaySignature { |
| 172 | open: code_actions.open, |
| 173 | revision: code_actions.revision(), |
| 174 | }, |
| 175 | hover: state |
| 176 | .hover_popover() |
| 177 | .map(|popover| popover.symbol_range.clone()), |
| 178 | diagnostic: state.diagnostic_popover(), |
| 179 | cursor: state.cursor(), |
| 180 | }) |
| 181 | } |
| 182 | |
| 183 | fn build_lsp( |
| 184 | state: &Entity<InputBaseState<Self>>, |
| 185 | window: &mut Window, |
| 186 | cx: &mut App, |
| 187 | ) -> Option<LspOverlays> { |
| 188 | Some(LspOverlays { |
| 189 | completion: CompletionMenu::new(state.clone(), window, cx), |
| 190 | code_actions: CodeActionMenu::new(state.clone(), window, cx), |
| 191 | hover: None, |
| 192 | diagnostic: None, |
| 193 | completion_signature: OverlaySignature::default(), |
| 194 | code_action_signature: OverlaySignature::default(), |
| 195 | hover_signature: None, |
| 196 | diagnostic_signature: None, |
| 197 | }) |
| 198 | } |
| 199 | |
| 200 | fn sync_lsp( |
| 201 | lsp: &mut LspOverlays, |
| 202 | state: &Entity<InputBaseState<Self>>, |
| 203 | snapshot: &LspSnapshot, |
| 204 | window: &mut Window, |
| 205 | cx: &mut App, |
| 206 | ) { |
| 207 | let cursor = snapshot.cursor; |
| 208 | |
| 209 | if snapshot.completion != lsp.completion_signature { |
| 210 | lsp.completion_signature = OverlaySignature { |
| 211 | open: snapshot.completion.open, |
| 212 | revision: snapshot.completion.revision, |
| 213 | }; |
| 214 | let open = snapshot.completion.open; |
| 215 | let start = snapshot.completion_start; |
| 216 | // Read the items only now, on a frame where they changed. |
| 217 | let (query, items) = { |
| 218 | let menu = state.read(cx).completion_menu_state(); |
| 219 | (menu.query.clone(), menu.items.clone()) |
| 220 | }; |
| 221 | lsp.completion.update(cx, |menu, cx| { |
| 222 | if open { |
| 223 | menu.update_query(start.unwrap_or(cursor), query); |
| 224 | menu.show(cursor, items, window, cx); |
| 225 | } else { |
| 226 | menu.hide(cx); |
| 227 | } |
| 228 | }); |
| 229 | } |
| 230 | |
| 231 | if snapshot.code_action != lsp.code_action_signature { |
| 232 | lsp.code_action_signature = OverlaySignature { |
| 233 | open: snapshot.code_action.open, |
| 234 | revision: snapshot.code_action.revision, |
| 235 | }; |
| 236 | let open = snapshot.code_action.open; |
| 237 | let items = state.read(cx).code_action_menu_state().items.clone(); |
| 238 | lsp.code_actions.update(cx, |menu, cx| { |
| 239 | if open { |
| 240 | menu.show(cursor, items, window, cx); |
| 241 | } else { |
| 242 | menu.hide(cx); |
| 243 | } |
| 244 | }); |
| 245 | } |
| 246 | |
| 247 | // A hover popover is anchored to one symbol, so a new range means new |
| 248 | // content and the same range means the same content. |
| 249 | if snapshot.hover != lsp.hover_signature { |
| 250 | lsp.hover_signature = snapshot.hover.clone(); |
| 251 | let popover = state |
| 252 | .read(cx) |
| 253 | .hover_popover() |
| 254 | .map(|popover| (popover.symbol_range.clone(), popover.hover.clone())); |
| 255 | lsp.hover = popover.map(|(symbol_range, hover)| { |
| 256 | HoverPopover::new(state.clone(), symbol_range, &hover, cx) |
| 257 | }); |
| 258 | } |
| 259 | |
| 260 | // The engine hands out the diagnostic behind an `Rc`, so pointer |
| 261 | // identity already answers "is this the same entry". |
| 262 | let diagnostic_changed = match (&snapshot.diagnostic, &lsp.diagnostic_signature) { |
| 263 | (Some(new), Some(old)) => !std::rc::Rc::ptr_eq(new, old), |
| 264 | (None, None) => false, |
| 265 | _ => true, |
| 266 | }; |
| 267 | if diagnostic_changed { |
| 268 | lsp.diagnostic_signature = snapshot.diagnostic.clone(); |
| 269 | lsp.diagnostic = snapshot |
| 270 | .diagnostic |
| 271 | .as_deref() |
| 272 | .map(|entry| DiagnosticPopover::new(entry, state.clone(), cx)); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | #[derive(Default)] |
| 278 | pub(super) struct InputOverlays { |
| 279 | pub search: Option<AnyElement>, |
| 280 | pub floating: Vec<AnyElement>, |
| 281 | } |
| 282 | |
| 283 | impl InputOverlays { |
| 284 | #[cfg(test)] |
| 285 | fn len(&self) -> usize { |
| 286 | usize::from(self.search.is_some()) + self.floating.len() |
| 287 | } |
| 288 | |
| 289 | #[cfg(test)] |
| 290 | fn is_empty(&self) -> bool { |
| 291 | self.search.is_none() && self.floating.is_empty() |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | impl<M: OverlayMode> InputOverlayHost<M> { |
| 296 | fn new(state: Entity<InputBaseState<M>>, window: &mut Window, cx: &mut App) -> Self { |
| 297 | Self { |
| 298 | search: SearchPanel::new(state.clone(), window, cx), |
| 299 | search_signature: SearchSignature::default(), |
| 300 | lsp: M::build_lsp(&state, window, cx), |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | fn sync( |
| 305 | &mut self, |
| 306 | state: &Entity<InputBaseState<M>>, |
| 307 | window: &mut Window, |
| 308 | cx: &mut App, |
| 309 | ) -> InputOverlays { |
| 310 | let snapshot = M::lsp_snapshot(state.read(cx), cx); |
| 311 | let (search_open, replace_mode, activation_revision, search_session) = { |
| 312 | let state = state.read(cx); |
| 313 | let search = state.search_session(); |
| 314 | ( |
| 315 | search.open, |
| 316 | search.replace_mode, |
| 317 | state.search_activation_revision(), |
| 318 | search.clone(), |
| 319 | ) |
| 320 | }; |
| 321 | |
| 322 | let panel_replace_mode = self.search.read(cx).replace_mode(); |
| 323 | self.search |
| 324 | .update(cx, |panel, _| panel.sync_session(&search_session)); |
| 325 | |
| 326 | let search_signature = SearchSignature { |
| 327 | open: search_open, |
| 328 | replace_mode, |
| 329 | query: search_session.query.clone(), |
| 330 | anchor_offset: search_session.anchor_offset, |
| 331 | activation_revision, |
| 332 | }; |
| 333 | if search_signature != self.search_signature { |
| 334 | // The panel writes query and replacement-mode changes back into the |
| 335 | // session. Re-showing it for that echo would select the query or steal |
| 336 | // focus from replacement, so preserve changes the panel already holds. |
| 337 | // External mode changes still move focus out of a hidden replace field. |
| 338 | let was = &self.search_signature; |
| 339 | let query_echo = search_open |
| 340 | && was.open |
| 341 | && (was.replace_mode == search_signature.replace_mode |
| 342 | || panel_replace_mode == search_signature.replace_mode) |
| 343 | && was.anchor_offset == search_signature.anchor_offset |
| 344 | && was.activation_revision == search_signature.activation_revision |
| 345 | && self.search.read(cx).query(cx) == search_session.query; |
| 346 | self.search_signature = search_signature; |
| 347 | if !query_echo { |
| 348 | self.search.update(cx, |panel, cx| { |
| 349 | if search_open { |
| 350 | let selected = Rope::from(search_session.query.clone()); |
| 351 | let visible = search_session.anchor_offset.map(|offset| offset..offset); |
| 352 | panel.show_with_focus( |
| 353 | &selected, |
| 354 | replace_mode, |
| 355 | visible, |
| 356 | !cfg!(test), |
| 357 | window, |
| 358 | cx, |
| 359 | ); |
| 360 | } else { |
| 361 | panel.hide_with_focus(!cfg!(test), window, cx); |
| 362 | } |
| 363 | }); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if let (Some(lsp), Some(snapshot)) = (self.lsp.as_mut(), snapshot.as_ref()) { |
| 368 | M::sync_lsp(lsp, state, snapshot, window, cx); |
| 369 | } |
| 370 | |
| 371 | let search = search_open.then(|| self.search.clone().into_any_element()); |
| 372 | let mut floating = Vec::with_capacity(4); |
| 373 | if let (Some(lsp), Some(snapshot)) = (self.lsp.as_ref(), snapshot.as_ref()) { |
| 374 | if snapshot.completion.open { |
| 375 | floating.push(lsp.completion.clone().into_any_element()); |
| 376 | } |
| 377 | if snapshot.code_action.open { |
| 378 | floating.push(lsp.code_actions.clone().into_any_element()); |
| 379 | } |
| 380 | if let Some(hover) = lsp.hover.as_ref() { |
| 381 | floating.push(hover.clone().into_any_element()); |
| 382 | } |
| 383 | if let Some(diagnostic) = lsp.diagnostic.as_ref() { |
| 384 | floating.push(diagnostic.clone().into_any_element()); |
| 385 | } |
| 386 | } |
| 387 | InputOverlays { search, floating } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | pub(super) fn render_overlays<M: OverlayMode>( |
| 392 | state: &Entity<InputBaseState<M>>, |
| 393 | window: &mut Window, |
| 394 | cx: &mut App, |
| 395 | ) -> InputOverlays { |
| 396 | M::install_action_handler(state, cx); |
| 397 | let has_overlay = { |
| 398 | let state = state.read(cx); |
| 399 | state.search_session().open |
| 400 | || M::lsp_snapshot(state, cx).is_some_and(|lsp| lsp.has_overlay()) |
| 401 | }; |
| 402 | if !has_overlay { |
| 403 | if cx.has_global::<InputOverlayRegistry<M>>() { |
| 404 | let registry = cx.global_mut::<InputOverlayRegistry<M>>(); |
| 405 | registry.hosts.remove(&state.entity_id()); |
| 406 | registry |
| 407 | .hosts |
| 408 | .retain(|_, (owner, _)| owner.upgrade().is_some()); |
| 409 | } |
| 410 | return InputOverlays::default(); |
| 411 | } |
| 412 | |
| 413 | if !cx.has_global::<InputOverlayRegistry<M>>() { |
| 414 | cx.set_global(InputOverlayRegistry::<M>::default()); |
| 415 | } |
| 416 | |
| 417 | let id = state.entity_id(); |
| 418 | let mut host = cx |
| 419 | .global_mut::<InputOverlayRegistry<M>>() |
| 420 | .hosts |
| 421 | .remove(&id) |
| 422 | .map(|(_, host)| host) |
| 423 | .unwrap_or_else(|| InputOverlayHost::new(state.clone(), window, cx)); |
| 424 | let overlays = host.sync(state, window, cx); |
| 425 | cx.global_mut::<InputOverlayRegistry<M>>() |
| 426 | .hosts |
| 427 | .insert(id, (state.downgrade(), host)); |
| 428 | overlays |
| 429 | } |
| 430 | |
| 431 | #[cfg(test)] |
| 432 | mod tests { |
| 433 | use super::*; |
| 434 | use gpui::{AppContext as _, Render, SharedString, div}; |
| 435 | use gpui_base::input::CodeActionItem; |
| 436 | use gpui_base::input::DiagnosticEntry; |
| 437 | use lsp_types::{CodeAction, CompletionItem, Hover, HoverContents, MarkedString}; |
| 438 | |
| 439 | struct OverlayProbe { |
| 440 | state: Entity<crate::input::EditorState>, |
| 441 | } |
| 442 | |
| 443 | impl Render for OverlayProbe { |
| 444 | fn render( |
| 445 | &mut self, |
| 446 | _window: &mut Window, |
| 447 | _cx: &mut gpui::Context<Self>, |
| 448 | ) -> impl IntoElement { |
| 449 | div() |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /// Asking for search again, while it is already open, must re-select the query. |
| 454 | /// |
| 455 | /// Re-invoking search leaves every other part of the session identical, so a |
| 456 | /// sync keyed only on that state reads it as an unchanged frame and drops the |
| 457 | /// request: the field keeps whatever caret the user left in it and the |
| 458 | /// shortcut looks dead. `activation_revision` is what carries the second |
| 459 | /// request through, and the echo guard below it is what must survive that. |
| 460 | #[gpui::test] |
| 461 | fn reopening_search_reselects_the_query(cx: &mut gpui::TestAppContext) { |
| 462 | cx.update(crate::init); |
| 463 | let (probe, cx) = cx.add_window_view(|window, cx| OverlayProbe { |
| 464 | state: cx.new(|cx| crate::input::EditorState::new(window, cx).searchable(true)), |
| 465 | }); |
| 466 | let state = probe.read_with(cx, |probe, _| probe.state.clone()); |
| 467 | |
| 468 | cx.update(|window, cx| { |
| 469 | state.update(cx, |state, cx| { |
| 470 | state.set_value("foo bar foo", window, cx); |
| 471 | state.set_selected_range(0..3, cx); |
| 472 | state.open_search(false, cx); |
| 473 | }); |
| 474 | |
| 475 | let mut host = InputOverlayHost::new(state.clone(), window, cx); |
| 476 | host.sync(&state, window, cx); |
| 477 | |
| 478 | let search_input = host.search.read(cx).search_input().clone(); |
| 479 | assert_eq!(search_input.read(cx).value(), "foo"); |
| 480 | assert_eq!( |
| 481 | search_input.read(cx).selected_range(), |
| 482 | 0..3, |
| 483 | "opening search selects the query it seeded the field with" |
| 484 | ); |
| 485 | |
| 486 | // The user drops the caret into the field. An untouched frame must |
| 487 | // leave it there — this is the echo guard, and a fix that reshows the |
| 488 | // panel on every frame would fail here instead of silently annoying. |
| 489 | search_input.update(cx, |input, cx| input.unselect(window, cx)); |
| 490 | host.sync(&state, window, cx); |
| 491 | assert!( |
| 492 | search_input.read(cx).selected_range().is_empty(), |
| 493 | "an unchanged frame must not touch the field" |
| 494 | ); |
| 495 | |
| 496 | // The shortcut again, with the session already open and nothing else |
| 497 | // about it moved. |
| 498 | state.update(cx, |state, cx| state.open_search(false, cx)); |
| 499 | host.sync(&state, window, cx); |
| 500 | assert_eq!( |
| 501 | search_input.read(cx).selected_range(), |
| 502 | 0..3, |
| 503 | "re-invoking search must select the query again" |
| 504 | ); |
| 505 | }); |
| 506 | } |
| 507 | |
| 508 | /// Reopening search must preserve navigation already owned by Base. |
| 509 | /// |
| 510 | /// Closing and reopening Find keeps the previous occurrence in browsers. |
| 511 | /// Neither Base reopening the session nor the styled panel echoing its |
| 512 | /// retained query may reset the current match to zero. |
| 513 | #[gpui::test] |
| 514 | fn reopening_search_panel_preserves_the_previous_match(cx: &mut gpui::TestAppContext) { |
| 515 | cx.update(crate::init); |
| 516 | let (probe, cx) = cx.add_window_view(|window, cx| OverlayProbe { |
| 517 | state: cx.new(|cx| crate::input::EditorState::new(window, cx).searchable(true)), |
| 518 | }); |
| 519 | let state = probe.read_with(cx, |probe, _| probe.state.clone()); |
| 520 | |
| 521 | cx.update(|window, cx| { |
| 522 | state.update(cx, |state, cx| { |
| 523 | state.set_value("foo bar foo baz foo", window, cx); |
| 524 | state.open_search(false, cx); |
| 525 | state.set_search_query("foo", true, cx); |
| 526 | assert_eq!(state.next_search_match(cx), Some(8..11)); |
| 527 | assert_eq!(state.search_session().matcher.current_match_index(), 1); |
| 528 | state.close_search(cx); |
| 529 | state.open_search(false, cx); |
| 530 | assert_eq!(state.search_session().matcher.current_match_index(), 1); |
| 531 | }); |
| 532 | |
| 533 | let mut host = InputOverlayHost::new(state.clone(), window, cx); |
| 534 | host.sync(&state, window, cx); |
| 535 | |
| 536 | state.read_with(cx, |state, _| { |
| 537 | assert_eq!(state.search_session().matcher.current_match_index(), 1); |
| 538 | assert_eq!(state.search_session().matcher.label(), "2/3"); |
| 539 | }); |
| 540 | |
| 541 | state.update(cx, |state, cx| { |
| 542 | assert_eq!(state.next_search_match(cx), Some(16..19)); |
| 543 | }); |
| 544 | }); |
| 545 | } |
| 546 | |
| 547 | /// A frame that changed nothing must not rebuild the popovers. |
| 548 | /// |
| 549 | /// Sync runs every frame, so the change check has to be cheap and stable. |
| 550 | /// It is keyed on a revision the engine bumps when it swaps the content; |
| 551 | /// forgetting to bump, or comparing the content itself again, both show up |
| 552 | /// here. |
| 553 | #[gpui::test] |
| 554 | fn unchanged_content_keeps_the_overlay_signature(cx: &mut gpui::TestAppContext) { |
| 555 | cx.update(crate::init); |
| 556 | let (probe, cx) = cx.add_window_view(|window, cx| OverlayProbe { |
| 557 | state: cx.new(|cx| crate::input::EditorState::new(window, cx).language("sql")), |
| 558 | }); |
| 559 | let state = probe.read_with(cx, |probe, _| probe.state.clone()); |
| 560 | |
| 561 | cx.update(|window, cx| { |
| 562 | state.update(cx, |state, cx| { |
| 563 | state.present_completion_items( |
| 564 | 0, |
| 565 | "f", |
| 566 | vec![CompletionItem { |
| 567 | label: "foo".into(), |
| 568 | ..Default::default() |
| 569 | }], |
| 570 | cx, |
| 571 | ); |
| 572 | }); |
| 573 | |
| 574 | let mut host = InputOverlayHost::new(state.clone(), window, cx); |
| 575 | host.sync(&state, window, cx); |
| 576 | let after_first = host.lsp.as_ref().unwrap().completion_signature.revision; |
| 577 | |
| 578 | // A second pass over untouched content. |
| 579 | host.sync(&state, window, cx); |
| 580 | assert_eq!( |
| 581 | host.lsp.as_ref().unwrap().completion_signature.revision, |
| 582 | after_first, |
| 583 | "an unchanged frame must not bump the signature" |
| 584 | ); |
| 585 | |
| 586 | // Swapping the items must be noticed. |
| 587 | state.update(cx, |state, cx| { |
| 588 | state.present_completion_items( |
| 589 | 0, |
| 590 | "f", |
| 591 | vec![CompletionItem { |
| 592 | label: "bar".into(), |
| 593 | ..Default::default() |
| 594 | }], |
| 595 | cx, |
| 596 | ); |
| 597 | }); |
| 598 | host.sync(&state, window, cx); |
| 599 | assert_ne!( |
| 600 | host.lsp.as_ref().unwrap().completion_signature.revision, |
| 601 | after_first, |
| 602 | "new items must be noticed" |
| 603 | ); |
| 604 | }); |
| 605 | } |
| 606 | |
| 607 | #[gpui::test] |
| 608 | fn facade_materializes_all_base_overlay_sessions(cx: &mut gpui::TestAppContext) { |
| 609 | cx.update(crate::init); |
| 610 | let (probe, cx) = cx.add_window_view(|window, cx| OverlayProbe { |
| 611 | state: cx.new(|cx| { |
| 612 | crate::input::EditorState::new(window, cx) |
| 613 | .language("sql") |
| 614 | .searchable(true) |
| 615 | .replaceable(true) |
| 616 | }), |
| 617 | }); |
| 618 | let state = probe.read_with(cx, |probe, _| probe.state.clone()); |
| 619 | cx.update(|window, cx| { |
| 620 | assert!(render_overlays(&state, window, cx).is_empty()); |
| 621 | assert!(state.read(cx).has_overlay_action_handler()); |
| 622 | state.update(cx, |state, cx| { |
| 623 | state.set_value("foo bar foo", window, cx); |
| 624 | state.set_selected_range(4..7, cx); |
| 625 | state.open_search(true, cx); |
| 626 | assert_eq!(state.search_session().query, "bar"); |
| 627 | assert!(state.search_session().replace_mode); |
| 628 | state.present_completion_items( |
| 629 | 0, |
| 630 | "f", |
| 631 | vec![CompletionItem { |
| 632 | label: "foo".into(), |
| 633 | ..Default::default() |
| 634 | }], |
| 635 | cx, |
| 636 | ); |
| 637 | state.present_code_actions( |
| 638 | vec![CodeActionItem { |
| 639 | provider_id: SharedString::from("test"), |
| 640 | action: CodeAction { |
| 641 | title: "Fix".into(), |
| 642 | ..Default::default() |
| 643 | }, |
| 644 | }], |
| 645 | cx, |
| 646 | ); |
| 647 | state.present_hover( |
| 648 | 0..1, |
| 649 | Hover { |
| 650 | contents: HoverContents::Scalar(MarkedString::String("docs".into())), |
| 651 | range: None, |
| 652 | }, |
| 653 | cx, |
| 654 | ); |
| 655 | state.present_diagnostic(DiagnosticEntry::default(), cx); |
| 656 | }); |
| 657 | |
| 658 | let mut host = InputOverlayHost::new(state.clone(), window, cx); |
| 659 | let overlays = host.sync(&state, window, cx); |
| 660 | assert!(overlays.search.is_some()); |
| 661 | assert_eq!(overlays.floating.len(), 4); |
| 662 | assert_eq!(overlays.len(), 5); |
| 663 | assert_eq!(render_overlays(&state, window, cx).len(), 5); |
| 664 | assert!( |
| 665 | cx.global::<InputOverlayRegistry<crate::input::EditorMode>>() |
| 666 | .hosts |
| 667 | .contains_key(&state.entity_id()) |
| 668 | ); |
| 669 | |
| 670 | state.update(cx, |state, cx| { |
| 671 | assert!(state.route_overlay_action(Box::new(super::super::Escape), window, cx)); |
| 672 | assert!(!state.completion_menu_state().open); |
| 673 | state.dismiss_code_action_overlay(cx); |
| 674 | state.close_search(cx); |
| 675 | state.clear_hover_state(cx); |
| 676 | state.clear_diagnostic_popover(cx); |
| 677 | }); |
| 678 | assert!(host.sync(&state, window, cx).is_empty()); |
| 679 | assert!(render_overlays(&state, window, cx).is_empty()); |
| 680 | assert!( |
| 681 | !cx.global::<InputOverlayRegistry<crate::input::EditorMode>>() |
| 682 | .hosts |
| 683 | .contains_key(&state.entity_id()) |
| 684 | ); |
| 685 | }); |
| 686 | |
| 687 | let dropped_owner = cx.update(|window, cx| { |
| 688 | let ephemeral = cx.new(|cx| { |
| 689 | crate::input::EditorState::new(window, cx) |
| 690 | .language("sql") |
| 691 | .searchable(true) |
| 692 | }); |
| 693 | ephemeral.update(cx, |state, cx| state.open_search(false, cx)); |
| 694 | assert_eq!(render_overlays(&ephemeral, window, cx).len(), 1); |
| 695 | ephemeral.update(cx, |state, cx| state.close_search(cx)); |
| 696 | assert!(render_overlays(&ephemeral, window, cx).is_empty()); |
| 697 | assert!( |
| 698 | !cx.global::<InputOverlayRegistry<crate::input::EditorMode>>() |
| 699 | .hosts |
| 700 | .contains_key(&ephemeral.entity_id()) |
| 701 | ); |
| 702 | let owner = ephemeral.downgrade(); |
| 703 | drop(ephemeral); |
| 704 | owner |
| 705 | }); |
| 706 | cx.run_until_parked(); |
| 707 | assert!(dropped_owner.upgrade().is_none()); |
| 708 | } |
| 709 | } |