Unified · Split
| @@ -33,7 +33,6 @@ use mappings::mouse::{ · 32 unmodified lines | ||
| 33 | 33 | use procinfo::LocalProcessInfo; |
| 34 | 34 | use schemars::JsonSchema; |
| 35 | 35 | use serde::{Deserialize, Serialize}; |
| 36 | −use smol::channel::Sender; | |
| 37 | 36 | use util::truncate_and_trailoff; |
| 38 | 37 | |
| 39 | 38 | use std::{ |
| @@ -90,12 +89,18 @@ pub enum Event { · 50 unmodified lines | ||
| 90 | 89 | Wakeup, |
| 91 | 90 | BlinkChanged, |
| 92 | 91 | SelectionsChanged, |
| 93 | − OpenUrl(String), | |
| 94 | − ProbePathOpen { | |
| 95 | − maybe_path: String, | |
| 96 | − can_open_tx: Sender<bool>, | |
| 97 | − }, | |
| 98 | − OpenPath(String), | |
| 92 | + NewNavigationTarget(MaybeNavigationTarget), | |
| 93 | + Open(MaybeNavigationTarget), | |
| 94 | +} | |
| 95 | + | |
| 96 | +/// A string inside terminal, potentially useful as a URI that can be opened. | |
| 97 | +#[derive(Clone, Debug)] | |
| 98 | +pub enum MaybeNavigationTarget { | |
| 99 | + /// HTTP, git, etc. string determined by the [`URL_REGEX`] regex. | |
| 100 | + Url(String), | |
| 101 | + /// File system path, absolute or relative, existing or not. | |
| 102 | + /// Might have line and column number(s) attached as `file.rs:1:23` | |
| 103 | + PathLike(String), | |
| 99 | 104 | } |
| 100 | 105 | |
| 101 | 106 | #[derive(Clone)] |
| @@ -502,6 +507,7 @@ impl TerminalBuilder { · 400 unmodified lines | ||
| 502 | 507 | next_link_id: 0, |
| 503 | 508 | selection_phase: SelectionPhase::Ended, |
| 504 | 509 | cmd_pressed: false, |
| 510 | + found_word: false, | |
| 505 | 511 | }; |
| 506 | 512 | |
| 507 | 513 | Ok(TerminalBuilder { |
| @@ -654,6 +660,7 @@ pub struct Terminal { · 146 unmodified lines | ||
| 654 | 660 | next_link_id: usize, |
| 655 | 661 | selection_phase: SelectionPhase, |
| 656 | 662 | cmd_pressed: bool, |
| 663 | + found_word: bool, | |
| 657 | 664 | } |
| 658 | 665 | |
| 659 | 666 | impl Terminal { |
| @@ -834,7 +841,7 @@ impl Terminal { · 174 unmodified lines | ||
| 834 | 841 | .grid_clamp(term, alacritty_terminal::index::Boundary::Cursor); |
| 835 | 842 | |
| 836 | 843 | let link = term.grid().index(point).hyperlink(); |
| 837 | − let found_url = if link.is_some() { | |
| 844 | + let found_word = if link.is_some() { | |
| 838 | 845 | let mut min_index = point; |
| 839 | 846 | loop { |
| 840 | 847 | let new_min_index = |
| @@ -875,20 +882,21 @@ impl Terminal { · 34 unmodified lines | ||
| 875 | 882 | None |
| 876 | 883 | }; |
| 877 | 884 | |
| 878 | − if let Some((maybe_url_or_path, is_url, url_match)) = found_url { | |
| 885 | + self.found_word = found_word.is_some(); | |
| 886 | + if let Some((maybe_url_or_path, is_url, url_match)) = found_word { | |
| 879 | 887 | if *open { |
| 880 | − let event = if is_url { | |
| 881 | − Event::OpenUrl(maybe_url_or_path) | |
| 888 | + let target = if is_url { | |
| 889 | + MaybeNavigationTarget::Url(maybe_url_or_path) | |
| 882 | 890 | } else { |
| 883 | − Event::OpenPath(maybe_url_or_path) | |
| 891 | + MaybeNavigationTarget::PathLike(maybe_url_or_path) | |
| 884 | 892 | }; |
| 885 | − cx.emit(event); | |
| 893 | + cx.emit(Event::Open(target)); | |
| 886 | 894 | } else { |
| 887 | 895 | self.update_selected_word( |
| 888 | 896 | prev_hovered_word, |
| 889 | − maybe_url_or_path, | |
| 890 | 897 | url_match, |
| 891 | − !is_url, | |
| 898 | + maybe_url_or_path, | |
| 899 | + is_url, | |
| 892 | 900 | cx, |
| 893 | 901 | ); |
| 894 | 902 | } |
| @@ -900,9 +908,9 @@ impl Terminal { · 5 unmodified lines | ||
| 900 | 908 | fn update_selected_word( |
| 901 | 909 | &mut self, |
| 902 | 910 | prev_word: Option<HoveredWord>, |
| 903 | − word: String, | |
| 904 | 911 | word_match: RangeInclusive<Point>, |
| 905 | − should_probe_word: bool, | |
| 912 | + word: String, | |
| 913 | + is_url: bool, | |
| 906 | 914 | cx: &mut ModelContext<Self>, |
| 907 | 915 | ) { |
| 908 | 916 | if let Some(prev_word) = prev_word { |
| @@ -912,43 +920,21 @@ impl Terminal { · 3 unmodified lines | ||
| 912 | 920 | word_match, |
| 913 | 921 | id: prev_word.id, |
| 914 | 922 | }); |
| 915 | − } else if should_probe_word { | |
| 916 | − let (can_open_tx, can_open_rx) = smol::channel::bounded(1); | |
| 917 | − cx.emit(Event::ProbePathOpen { | |
| 918 | − maybe_path: word.clone(), | |
| 919 | − can_open_tx, | |
| 920 | − }); | |
| 921 | − | |
| 922 | − cx.spawn(|terminal, mut cx| async move { | |
| 923 | − let can_open = can_open_rx.recv().await.unwrap_or(false); | |
| 924 | − terminal.update(&mut cx, |terminal, cx| { | |
| 925 | − if can_open { | |
| 926 | − terminal.last_content.last_hovered_word = Some(HoveredWord { | |
| 927 | − word, | |
| 928 | − word_match, | |
| 929 | − id: terminal.next_link_id(), | |
| 930 | − }); | |
| 931 | − } else { | |
| 932 | − terminal.last_content.last_hovered_word.take(); | |
| 933 | − } | |
| 934 | − cx.notify(); | |
| 935 | − }); | |
| 936 | − }) | |
| 937 | − .detach(); | |
| 938 | − } else { | |
| 939 | − self.last_content.last_hovered_word = Some(HoveredWord { | |
| 940 | − word, | |
| 941 | − word_match, | |
| 942 | − id: self.next_link_id(), | |
| 943 | − }); | |
| 923 | + return; | |
| 944 | 924 | } |
| 945 | − } else { | |
| 946 | − self.last_content.last_hovered_word = Some(HoveredWord { | |
| 947 | − word, | |
| 948 | − word_match, | |
| 949 | − id: self.next_link_id(), | |
| 950 | − }); | |
| 951 | 925 | } |
| 926 | + | |
| 927 | + self.last_content.last_hovered_word = Some(HoveredWord { | |
| 928 | + word: word.clone(), | |
| 929 | + word_match, | |
| 930 | + id: self.next_link_id(), | |
| 931 | + }); | |
| 932 | + let navigation_target = if is_url { | |
| 933 | + MaybeNavigationTarget::Url(word) | |
| 934 | + } else { | |
| 935 | + MaybeNavigationTarget::PathLike(word) | |
| 936 | + }; | |
| 937 | + cx.emit(Event::NewNavigationTarget(navigation_target)); | |
| 952 | 938 | } |
| 953 | 939 | |
| 954 | 940 | fn next_link_id(&mut self) -> usize { |
| @@ -1031,17 +1017,8 @@ impl Terminal { · 76 unmodified lines | ||
| 1031 | 1017 | } |
| 1032 | 1018 | |
| 1033 | 1019 | pub fn try_modifiers_change(&mut self, modifiers: &Modifiers) -> bool { |
| 1034 | − let cmd = modifiers.cmd; | |
| 1035 | − let changed = self.cmd_pressed != cmd; | |
| 1036 | − if changed { | |
| 1037 | − self.cmd_pressed = cmd; | |
| 1038 | − if cmd { | |
| 1039 | − self.refresh_hovered_word(); | |
| 1040 | − } else { | |
| 1041 | − self.last_content.last_hovered_word.take(); | |
| 1042 | − } | |
| 1043 | − } | |
| 1044 | − | |
| 1020 | + let changed = self.cmd_pressed != modifiers.cmd; | |
| 1021 | + self.cmd_pressed = modifiers.cmd; | |
| 1045 | 1022 | changed |
| 1046 | 1023 | } |
| 1047 | 1024 | |
| @@ -1336,7 +1313,7 @@ impl Terminal { · 288 unmodified lines | ||
| 1336 | 1313 | } |
| 1337 | 1314 | } |
| 1338 | 1315 | |
| 1339 | − pub fn refresh_hovered_word(&mut self) { | |
| 1316 | + fn refresh_hovered_word(&mut self) { | |
| 1340 | 1317 | self.word_from_position(self.last_mouse_position); |
| 1341 | 1318 | } |
| 1342 | 1319 | |
| @@ -1415,6 +1392,10 @@ impl Terminal { · 72 unmodified lines | ||
| 1415 | 1392 | }) |
| 1416 | 1393 | .unwrap_or_else(|| "Terminal".to_string()) |
| 1417 | 1394 | } |
| 1395 | + | |
| 1396 | + pub fn can_navigate_to_selected_word(&self) -> bool { | |
| 1397 | + self.cmd_pressed && self.found_word | |
| 1398 | + } | |
| 1418 | 1399 | } |
| 1419 | 1400 | |
| 1420 | 1401 | impl Drop for Terminal { |
| @@ -163,6 +163,7 @@ pub struct TerminalElement { · 162 unmodified lines | ||
| 163 | 163 | terminal: WeakModelHandle<Terminal>, |
| 164 | 164 | focused: bool, |
| 165 | 165 | cursor_visible: bool, |
| 166 | + can_navigate_to_selected_word: bool, | |
| 166 | 167 | } |
| 167 | 168 | |
| 168 | 169 | impl TerminalElement { |
| @@ -170,11 +171,13 @@ impl TerminalElement { · 1 unmodified line | ||
| 170 | 171 | terminal: WeakModelHandle<Terminal>, |
| 171 | 172 | focused: bool, |
| 172 | 173 | cursor_visible: bool, |
| 174 | + can_navigate_to_selected_word: bool, | |
| 173 | 175 | ) -> TerminalElement { |
| 174 | 176 | TerminalElement { |
| 175 | 177 | terminal, |
| 176 | 178 | focused, |
| 177 | 179 | cursor_visible, |
| 180 | + can_navigate_to_selected_word, | |
| 178 | 181 | } |
| 179 | 182 | } |
| 180 | 183 | |
| @@ -580,13 +583,17 @@ impl Element<TerminalView> for TerminalElement { · 399 unmodified lines | ||
| 580 | 583 | let background_color = terminal_theme.background; |
| 581 | 584 | let terminal_handle = self.terminal.upgrade(cx).unwrap(); |
| 582 | 585 | |
| 583 | − let last_hovered_hyperlink = terminal_handle.update(cx, |terminal, cx| { | |
| 586 | + let last_hovered_word = terminal_handle.update(cx, |terminal, cx| { | |
| 584 | 587 | terminal.set_size(dimensions); |
| 585 | 588 | terminal.try_sync(cx); |
| 586 | − terminal.last_content.last_hovered_word.clone() | |
| 589 | + if self.can_navigate_to_selected_word && terminal.can_navigate_to_selected_word() { | |
| 590 | + terminal.last_content.last_hovered_word.clone() | |
| 591 | + } else { | |
| 592 | + None | |
| 593 | + } | |
| 587 | 594 | }); |
| 588 | 595 | |
| 589 | − let hyperlink_tooltip = last_hovered_hyperlink.map(|hovered_word| { | |
| 596 | + let hyperlink_tooltip = last_hovered_word.clone().map(|hovered_word| { | |
| 590 | 597 | let mut tooltip = Overlay::new( |
| 591 | 598 | Empty::new() |
| 592 | 599 | .contained() |
| @@ -619,7 +626,6 @@ impl Element<TerminalView> for TerminalElement { · 26 unmodified lines | ||
| 619 | 626 | cursor_char, |
| 620 | 627 | selection, |
| 621 | 628 | cursor, |
| 622 | − last_hovered_word, | |
| 623 | 629 | .. |
| 624 | 630 | } = { &terminal_handle.read(cx).last_content }; |
| 625 | 631 | |
| @@ -33,7 +33,7 @@ use terminal::{ · 32 unmodified lines | ||
| 33 | 33 | index::Point, |
| 34 | 34 | term::{search::RegexSearch, TermMode}, |
| 35 | 35 | }, |
| 36 | − Event, Terminal, TerminalBlink, WorkingDirectory, | |
| 36 | + Event, MaybeNavigationTarget, Terminal, TerminalBlink, WorkingDirectory, | |
| 37 | 37 | }; |
| 38 | 38 | use util::{paths::PathLikeWithPosition, ResultExt}; |
| 39 | 39 | use workspace::{ |
| @@ -93,6 +93,7 @@ pub struct TerminalView { · 53 unmodified lines | ||
| 93 | 93 | blinking_on: bool, |
| 94 | 94 | blinking_paused: bool, |
| 95 | 95 | blink_epoch: usize, |
| 96 | + can_navigate_to_selected_word: bool, | |
| 96 | 97 | workspace_id: WorkspaceId, |
| 97 | 98 | } |
| 98 | 99 | |
| @@ -169,55 +170,61 @@ impl TerminalView { · 70 unmodified lines | ||
| 169 | 170 | .detach(); |
| 170 | 171 | } |
| 171 | 172 | } |
| 172 | − Event::ProbePathOpen { | |
| 173 | − maybe_path, | |
| 174 | − can_open_tx, | |
| 175 | − } => { | |
| 176 | − let can_open = !possible_open_targets(&workspace, maybe_path, cx).is_empty(); | |
| 177 | − can_open_tx.send_blocking(can_open).ok(); | |
| 173 | + Event::NewNavigationTarget(maybe_navigation_target) => { | |
| 174 | + this.can_navigate_to_selected_word = match maybe_navigation_target { | |
| 175 | + MaybeNavigationTarget::Url(_) => true, | |
| 176 | + MaybeNavigationTarget::PathLike(maybe_path) => { | |
| 177 | + !possible_open_targets(&workspace, maybe_path, cx).is_empty() | |
| 178 | + } | |
| 179 | + } | |
| 178 | 180 | } |
| 179 | − Event::OpenUrl(url) => cx.platform().open_url(url), | |
| 180 | − Event::OpenPath(maybe_path) => { | |
| 181 | − let potential_abs_paths = possible_open_targets(&workspace, maybe_path, cx); | |
| 182 | − if let Some(path) = potential_abs_paths.into_iter().next() { | |
| 183 | − let visible = path.path_like.is_dir(); | |
| 184 | − let task_workspace = workspace.clone(); | |
| 185 | − cx.spawn(|_, mut cx| async move { | |
| 186 | − let opened_item = task_workspace | |
| 187 | − .update(&mut cx, |workspace, cx| { | |
| 188 | − workspace.open_abs_path(path.path_like, visible, cx) | |
| 189 | − }) | |
| 190 | − .context("workspace update")? | |
| 191 | − .await | |
| 192 | − .context("workspace update")?; | |
| 193 | − if let Some(row) = path.row { | |
| 194 | − let col = path.column.unwrap_or(0); | |
| 195 | − if let Some(active_editor) = opened_item.downcast::<Editor>() { | |
| 196 | − active_editor | |
| 197 | − .downgrade() | |
| 198 | − .update(&mut cx, |editor, cx| { | |
| 199 | − let snapshot = editor.snapshot(cx).display_snapshot; | |
| 200 | − let point = snapshot.buffer_snapshot.clip_point( | |
| 201 | − language::Point::new( | |
| 202 | − row.saturating_sub(1), | |
| 203 | − col.saturating_sub(1), | |
| 204 | − ), | |
| 205 | − Bias::Left, | |
| 206 | − ); | |
| 207 | − editor.change_selections( | |
| 208 | − Some(Autoscroll::center()), | |
| 209 | − cx, | |
| 210 | − |s| s.select_ranges([point..point]), | |
| 211 | − ); | |
| 212 | − }) | |
| 213 | − .log_err(); | |
| 181 | + Event::Open(maybe_navigation_target) => match maybe_navigation_target { | |
| 182 | + MaybeNavigationTarget::Url(url) => cx.platform().open_url(url), | |
| 183 | + MaybeNavigationTarget::PathLike(maybe_path) => { | |
| 184 | + if !this.can_navigate_to_selected_word { | |
| 185 | + return; | |
| 186 | + } | |
| 187 | + let potential_abs_paths = possible_open_targets(&workspace, maybe_path, cx); | |
| 188 | + if let Some(path) = potential_abs_paths.into_iter().next() { | |
| 189 | + let visible = path.path_like.is_dir(); | |
| 190 | + let task_workspace = workspace.clone(); | |
| 191 | + cx.spawn(|_, mut cx| async move { | |
| 192 | + let opened_item = task_workspace | |
| 193 | + .update(&mut cx, |workspace, cx| { | |
| 194 | + workspace.open_abs_path(path.path_like, visible, cx) | |
| 195 | + }) | |
| 196 | + .context("workspace update")? | |
| 197 | + .await | |
| 198 | + .context("workspace update")?; | |
| 199 | + if let Some(row) = path.row { | |
| 200 | + let col = path.column.unwrap_or(0); | |
| 201 | + if let Some(active_editor) = opened_item.downcast::<Editor>() { | |
| 202 | + active_editor | |
| 203 | + .downgrade() | |
| 204 | + .update(&mut cx, |editor, cx| { | |
| 205 | + let snapshot = editor.snapshot(cx).display_snapshot; | |
| 206 | + let point = snapshot.buffer_snapshot.clip_point( | |
| 207 | + language::Point::new( | |
| 208 | + row.saturating_sub(1), | |
| 209 | + col.saturating_sub(1), | |
| 210 | + ), | |
| 211 | + Bias::Left, | |
| 212 | + ); | |
| 213 | + editor.change_selections( | |
| 214 | + Some(Autoscroll::center()), | |
| 215 | + cx, | |
| 216 | + |s| s.select_ranges([point..point]), | |
| 217 | + ); | |
| 218 | + }) | |
| 219 | + .log_err(); | |
| 220 | + } | |
| 214 | 221 | } |
| 215 | − } | |
| 216 | − anyhow::Ok(()) | |
| 217 | − }) | |
| 218 | − .detach_and_log_err(cx); | |
| 222 | + anyhow::Ok(()) | |
| 223 | + }) | |
| 224 | + .detach_and_log_err(cx); | |
| 225 | + } | |
| 219 | 226 | } |
| 220 | − } | |
| 227 | + }, | |
| 221 | 228 | _ => cx.emit(event.clone()), |
| 222 | 229 | }) |
| 223 | 230 | .detach(); |
| @@ -231,6 +238,7 @@ impl TerminalView { · 7 unmodified lines | ||
| 231 | 238 | blinking_on: false, |
| 232 | 239 | blinking_paused: false, |
| 233 | 240 | blink_epoch: 0, |
| 241 | + can_navigate_to_selected_word: false, | |
| 234 | 242 | workspace_id, |
| 235 | 243 | } |
| 236 | 244 | } |
| @@ -466,6 +474,7 @@ impl View for TerminalView { · 229 unmodified lines | ||
| 466 | 474 | terminal_handle, |
| 467 | 475 | focused, |
| 468 | 476 | self.should_show_cursor(focused, cx), |
| 477 | + self.can_navigate_to_selected_word, | |
| 469 | 478 | ) |
| 470 | 479 | .contained(), |
| 471 | 480 | ) |