History

Rework terminal highlight event flow

Kirill Bulatov committed 3 years ago

Commit 10db05f1 parent 6f7a6e53 files · +111 −115Browse files

Unified · Split

3 files
  1. crates/terminal/src/terminal.rs +45 −64
  2. crates/terminal_view/src/terminal_element.rs +10 −4
  3. crates/terminal_view/src/terminal_view.rs +56 −47
crates/terminal/src/terminal.rs+45−64
@@ -33,7 +33,6 @@ use mappings::mouse::{ · 32 unmodified lines
33use procinfo::LocalProcessInfo;33use procinfo::LocalProcessInfo;
34use schemars::JsonSchema;34use schemars::JsonSchema;
35use serde::{Deserialize, Serialize};35use serde::{Deserialize, Serialize};
36use smol::channel::Sender;
37use util::truncate_and_trailoff;36use util::truncate_and_trailoff;
38 37
39use std::{38use std::{
@@ -90,12 +89,18 @@ pub enum Event { · 50 unmodified lines
90 Wakeup,89 Wakeup,
91 BlinkChanged,90 BlinkChanged,
92 SelectionsChanged,91 SelectionsChanged,
93 OpenUrl(String),92 NewNavigationTarget(MaybeNavigationTarget),
94 ProbePathOpen {93 Open(MaybeNavigationTarget),
95 maybe_path: String,94}
96 can_open_tx: Sender<bool>,95
97 },96/// A string inside terminal, potentially useful as a URI that can be opened.
98 OpenPath(String),97#[derive(Clone, Debug)]
98pub 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#[derive(Clone)]106#[derive(Clone)]
@@ -502,6 +507,7 @@ impl TerminalBuilder { · 400 unmodified lines
502 next_link_id: 0,507 next_link_id: 0,
503 selection_phase: SelectionPhase::Ended,508 selection_phase: SelectionPhase::Ended,
504 cmd_pressed: false,509 cmd_pressed: false,
510 found_word: false,
505 };511 };
506 512
507 Ok(TerminalBuilder {513 Ok(TerminalBuilder {
@@ -654,6 +660,7 @@ pub struct Terminal { · 146 unmodified lines
654 next_link_id: usize,660 next_link_id: usize,
655 selection_phase: SelectionPhase,661 selection_phase: SelectionPhase,
656 cmd_pressed: bool,662 cmd_pressed: bool,
663 found_word: bool,
657}664}
658 665
659impl Terminal {666impl Terminal {
@@ -834,7 +841,7 @@ impl Terminal { · 174 unmodified lines
834 .grid_clamp(term, alacritty_terminal::index::Boundary::Cursor);841 .grid_clamp(term, alacritty_terminal::index::Boundary::Cursor);
835 842
836 let link = term.grid().index(point).hyperlink();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 let mut min_index = point;845 let mut min_index = point;
839 loop {846 loop {
840 let new_min_index =847 let new_min_index =
@@ -875,20 +882,21 @@ impl Terminal { · 34 unmodified lines
875 None882 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 if *open {887 if *open {
880 let event = if is_url {888 let target = if is_url {
881 Event::OpenUrl(maybe_url_or_path)889 MaybeNavigationTarget::Url(maybe_url_or_path)
882 } else {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 } else {894 } else {
887 self.update_selected_word(895 self.update_selected_word(
888 prev_hovered_word,896 prev_hovered_word,
889 maybe_url_or_path,
890 url_match,897 url_match,
891 !is_url,898 maybe_url_or_path,
899 is_url,
892 cx,900 cx,
893 );901 );
894 }902 }
@@ -900,9 +908,9 @@ impl Terminal { · 5 unmodified lines
900 fn update_selected_word(908 fn update_selected_word(
901 &mut self,909 &mut self,
902 prev_word: Option<HoveredWord>,910 prev_word: Option<HoveredWord>,
903 word: String,
904 word_match: RangeInclusive<Point>,911 word_match: RangeInclusive<Point>,
905 should_probe_word: bool,912 word: String,
913 is_url: bool,
906 cx: &mut ModelContext<Self>,914 cx: &mut ModelContext<Self>,
907 ) {915 ) {
908 if let Some(prev_word) = prev_word {916 if let Some(prev_word) = prev_word {
@@ -912,43 +920,21 @@ impl Terminal { · 3 unmodified lines
912 word_match,920 word_match,
913 id: prev_word.id,921 id: prev_word.id,
914 });922 });
915 } else if should_probe_word {923 return;
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 });
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 fn next_link_id(&mut self) -> usize {940 fn next_link_id(&mut self) -> usize {
@@ -1031,17 +1017,8 @@ impl Terminal { · 76 unmodified lines
1031 }1017 }
1032 1018
1033 pub fn try_modifiers_change(&mut self, modifiers: &Modifiers) -> bool {1019 pub fn try_modifiers_change(&mut self, modifiers: &Modifiers) -> bool {
1034 let cmd = modifiers.cmd;1020 let changed = self.cmd_pressed != modifiers.cmd;
1035 let changed = self.cmd_pressed != cmd;1021 self.cmd_pressed = modifiers.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
1045 changed1022 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 self.word_from_position(self.last_mouse_position);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 .unwrap_or_else(|| "Terminal".to_string())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
1420impl Drop for Terminal {1401impl Drop for Terminal {
crates/terminal_view/src/terminal_element.rs+10−4
@@ -163,6 +163,7 @@ pub struct TerminalElement { · 162 unmodified lines
163 terminal: WeakModelHandle<Terminal>,163 terminal: WeakModelHandle<Terminal>,
164 focused: bool,164 focused: bool,
165 cursor_visible: bool,165 cursor_visible: bool,
166 can_navigate_to_selected_word: bool,
166}167}
167 168
168impl TerminalElement {169impl TerminalElement {
@@ -170,11 +171,13 @@ impl TerminalElement { · 1 unmodified line
170 terminal: WeakModelHandle<Terminal>,171 terminal: WeakModelHandle<Terminal>,
171 focused: bool,172 focused: bool,
172 cursor_visible: bool,173 cursor_visible: bool,
174 can_navigate_to_selected_word: bool,
173 ) -> TerminalElement {175 ) -> TerminalElement {
174 TerminalElement {176 TerminalElement {
175 terminal,177 terminal,
176 focused,178 focused,
177 cursor_visible,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 let background_color = terminal_theme.background;583 let background_color = terminal_theme.background;
581 let terminal_handle = self.terminal.upgrade(cx).unwrap();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 terminal.set_size(dimensions);587 terminal.set_size(dimensions);
585 terminal.try_sync(cx);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 let mut tooltip = Overlay::new(597 let mut tooltip = Overlay::new(
591 Empty::new()598 Empty::new()
592 .contained()599 .contained()
@@ -619,7 +626,6 @@ impl Element<TerminalView> for TerminalElement { · 26 unmodified lines
619 cursor_char,626 cursor_char,
620 selection,627 selection,
621 cursor,628 cursor,
622 last_hovered_word,
623 ..629 ..
624 } = { &terminal_handle.read(cx).last_content };630 } = { &terminal_handle.read(cx).last_content };
625 631
crates/terminal_view/src/terminal_view.rs+56−47
@@ -33,7 +33,7 @@ use terminal::{ · 32 unmodified lines
33 index::Point,33 index::Point,
34 term::{search::RegexSearch, TermMode},34 term::{search::RegexSearch, TermMode},
35 },35 },
36 Event, Terminal, TerminalBlink, WorkingDirectory,36 Event, MaybeNavigationTarget, Terminal, TerminalBlink, WorkingDirectory,
37};37};
38use util::{paths::PathLikeWithPosition, ResultExt};38use util::{paths::PathLikeWithPosition, ResultExt};
39use workspace::{39use workspace::{
@@ -93,6 +93,7 @@ pub struct TerminalView { · 53 unmodified lines
93 blinking_on: bool,93 blinking_on: bool,
94 blinking_paused: bool,94 blinking_paused: bool,
95 blink_epoch: usize,95 blink_epoch: usize,
96 can_navigate_to_selected_word: bool,
96 workspace_id: WorkspaceId,97 workspace_id: WorkspaceId,
97}98}
98 99
@@ -169,55 +170,61 @@ impl TerminalView { · 70 unmodified lines
169 .detach();170 .detach();
170 }171 }
171 }172 }
172 Event::ProbePathOpen {173 Event::NewNavigationTarget(maybe_navigation_target) => {
173 maybe_path,174 this.can_navigate_to_selected_word = match maybe_navigation_target {
174 can_open_tx,175 MaybeNavigationTarget::Url(_) => true,
175 } => {176 MaybeNavigationTarget::PathLike(maybe_path) => {
176 let can_open = !possible_open_targets(&workspace, maybe_path, cx).is_empty();177 !possible_open_targets(&workspace, maybe_path, cx).is_empty()
177 can_open_tx.send_blocking(can_open).ok();178 }
179 }
178 }180 }
179 Event::OpenUrl(url) => cx.platform().open_url(url),181 Event::Open(maybe_navigation_target) => match maybe_navigation_target {
180 Event::OpenPath(maybe_path) => {182 MaybeNavigationTarget::Url(url) => cx.platform().open_url(url),
181 let potential_abs_paths = possible_open_targets(&workspace, maybe_path, cx);183 MaybeNavigationTarget::PathLike(maybe_path) => {
182 if let Some(path) = potential_abs_paths.into_iter().next() {184 if !this.can_navigate_to_selected_word {
183 let visible = path.path_like.is_dir();185 return;
184 let task_workspace = workspace.clone();186 }
185 cx.spawn(|_, mut cx| async move {187 let potential_abs_paths = possible_open_targets(&workspace, maybe_path, cx);
186 let opened_item = task_workspace188 if let Some(path) = potential_abs_paths.into_iter().next() {
187 .update(&mut cx, |workspace, cx| {189 let visible = path.path_like.is_dir();
188 workspace.open_abs_path(path.path_like, visible, cx)190 let task_workspace = workspace.clone();
189 })191 cx.spawn(|_, mut cx| async move {
190 .context("workspace update")?192 let opened_item = task_workspace
191 .await193 .update(&mut cx, |workspace, cx| {
192 .context("workspace update")?;194 workspace.open_abs_path(path.path_like, visible, cx)
193 if let Some(row) = path.row {195 })
194 let col = path.column.unwrap_or(0);196 .context("workspace update")?
195 if let Some(active_editor) = opened_item.downcast::<Editor>() {197 .await
196 active_editor198 .context("workspace update")?;
197 .downgrade()199 if let Some(row) = path.row {
198 .update(&mut cx, |editor, cx| {200 let col = path.column.unwrap_or(0);
199 let snapshot = editor.snapshot(cx).display_snapshot;201 if let Some(active_editor) = opened_item.downcast::<Editor>() {
200 let point = snapshot.buffer_snapshot.clip_point(202 active_editor
201 language::Point::new(203 .downgrade()
202 row.saturating_sub(1),204 .update(&mut cx, |editor, cx| {
203 col.saturating_sub(1),205 let snapshot = editor.snapshot(cx).display_snapshot;
204 ),206 let point = snapshot.buffer_snapshot.clip_point(
205 Bias::Left,207 language::Point::new(
206 );208 row.saturating_sub(1),
207 editor.change_selections(209 col.saturating_sub(1),
208 Some(Autoscroll::center()),210 ),
209 cx,211 Bias::Left,
210 |s| s.select_ranges([point..point]),212 );
211 );213 editor.change_selections(
212 })214 Some(Autoscroll::center()),
213 .log_err();215 cx,
216 |s| s.select_ranges([point..point]),
217 );
218 })
219 .log_err();
220 }
214 }221 }
215 }222 anyhow::Ok(())
216 anyhow::Ok(())223 })
217 })224 .detach_and_log_err(cx);
218 .detach_and_log_err(cx);225 }
219 }226 }
220 }227 },
221 _ => cx.emit(event.clone()),228 _ => cx.emit(event.clone()),
222 })229 })
223 .detach();230 .detach();
@@ -231,6 +238,7 @@ impl TerminalView { · 7 unmodified lines
231 blinking_on: false,238 blinking_on: false,
232 blinking_paused: false,239 blinking_paused: false,
233 blink_epoch: 0,240 blink_epoch: 0,
241 can_navigate_to_selected_word: false,
234 workspace_id,242 workspace_id,
235 }243 }
236 }244 }
@@ -466,6 +474,7 @@ impl View for TerminalView { · 229 unmodified lines
466 terminal_handle,474 terminal_handle,
467 focused,475 focused,
468 self.should_show_cursor(focused, cx),476 self.should_show_cursor(focused, cx),
477 self.can_navigate_to_selected_word,
469 )478 )
470 .contained(),479 .contained(),
471 )480 )

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t