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
3333 use procinfo::LocalProcessInfo;
3434 use schemars::JsonSchema;
3535 use serde::{Deserialize, Serialize};
36−use smol::channel::Sender;
3736 use util::truncate_and_trailoff;
3837
3938 use std::{
@@ -90,12 +89,18 @@ pub enum Event { · 50 unmodified lines
9089 Wakeup,
9190 BlinkChanged,
9291 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),
99104 }
100105
101106 #[derive(Clone)]
@@ -502,6 +507,7 @@ impl TerminalBuilder { · 400 unmodified lines
502507 next_link_id: 0,
503508 selection_phase: SelectionPhase::Ended,
504509 cmd_pressed: false,
510+ found_word: false,
505511 };
506512
507513 Ok(TerminalBuilder {
@@ -654,6 +660,7 @@ pub struct Terminal { · 146 unmodified lines
654660 next_link_id: usize,
655661 selection_phase: SelectionPhase,
656662 cmd_pressed: bool,
663+ found_word: bool,
657664 }
658665
659666 impl Terminal {
@@ -834,7 +841,7 @@ impl Terminal { · 174 unmodified lines
834841 .grid_clamp(term, alacritty_terminal::index::Boundary::Cursor);
835842
836843 let link = term.grid().index(point).hyperlink();
837− let found_url = if link.is_some() {
844+ let found_word = if link.is_some() {
838845 let mut min_index = point;
839846 loop {
840847 let new_min_index =
@@ -875,20 +882,21 @@ impl Terminal { · 34 unmodified lines
875882 None
876883 };
877884
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 {
879887 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)
882890 } else {
883− Event::OpenPath(maybe_url_or_path)
891+ MaybeNavigationTarget::PathLike(maybe_url_or_path)
884892 };
885− cx.emit(event);
893+ cx.emit(Event::Open(target));
886894 } else {
887895 self.update_selected_word(
888896 prev_hovered_word,
889− maybe_url_or_path,
890897 url_match,
891− !is_url,
898+ maybe_url_or_path,
899+ is_url,
892900 cx,
893901 );
894902 }
@@ -900,9 +908,9 @@ impl Terminal { · 5 unmodified lines
900908 fn update_selected_word(
901909 &mut self,
902910 prev_word: Option<HoveredWord>,
903− word: String,
904911 word_match: RangeInclusive<Point>,
905− should_probe_word: bool,
912+ word: String,
913+ is_url: bool,
906914 cx: &mut ModelContext<Self>,
907915 ) {
908916 if let Some(prev_word) = prev_word {
@@ -912,43 +920,21 @@ impl Terminal { · 3 unmodified lines
912920 word_match,
913921 id: prev_word.id,
914922 });
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;
944924 }
945− } else {
946− self.last_content.last_hovered_word = Some(HoveredWord {
947− word,
948− word_match,
949− id: self.next_link_id(),
950− });
951925 }
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));
952938 }
953939
954940 fn next_link_id(&mut self) -> usize {
@@ -1031,17 +1017,8 @@ impl Terminal { · 76 unmodified lines
10311017 }
10321018
10331019 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;
10451022 changed
10461023 }
10471024
@@ -1336,7 +1313,7 @@ impl Terminal { · 288 unmodified lines
13361313 }
13371314 }
13381315
1339− pub fn refresh_hovered_word(&mut self) {
1316+ fn refresh_hovered_word(&mut self) {
13401317 self.word_from_position(self.last_mouse_position);
13411318 }
13421319
@@ -1415,6 +1392,10 @@ impl Terminal { · 72 unmodified lines
14151392 })
14161393 .unwrap_or_else(|| "Terminal".to_string())
14171394 }
1395+
1396+ pub fn can_navigate_to_selected_word(&self) -> bool {
1397+ self.cmd_pressed && self.found_word
1398+ }
14181399 }
14191400
14201401 impl Drop for Terminal {
crates/terminal_view/src/terminal_element.rs+10−4
@@ -163,6 +163,7 @@ pub struct TerminalElement { · 162 unmodified lines
163163 terminal: WeakModelHandle<Terminal>,
164164 focused: bool,
165165 cursor_visible: bool,
166+ can_navigate_to_selected_word: bool,
166167 }
167168
168169 impl TerminalElement {
@@ -170,11 +171,13 @@ impl TerminalElement { · 1 unmodified line
170171 terminal: WeakModelHandle<Terminal>,
171172 focused: bool,
172173 cursor_visible: bool,
174+ can_navigate_to_selected_word: bool,
173175 ) -> TerminalElement {
174176 TerminalElement {
175177 terminal,
176178 focused,
177179 cursor_visible,
180+ can_navigate_to_selected_word,
178181 }
179182 }
180183
@@ -580,13 +583,17 @@ impl Element<TerminalView> for TerminalElement { · 399 unmodified lines
580583 let background_color = terminal_theme.background;
581584 let terminal_handle = self.terminal.upgrade(cx).unwrap();
582585
583− let last_hovered_hyperlink = terminal_handle.update(cx, |terminal, cx| {
586+ let last_hovered_word = terminal_handle.update(cx, |terminal, cx| {
584587 terminal.set_size(dimensions);
585588 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+ }
587594 });
588595
589− let hyperlink_tooltip = last_hovered_hyperlink.map(|hovered_word| {
596+ let hyperlink_tooltip = last_hovered_word.clone().map(|hovered_word| {
590597 let mut tooltip = Overlay::new(
591598 Empty::new()
592599 .contained()
@@ -619,7 +626,6 @@ impl Element<TerminalView> for TerminalElement { · 26 unmodified lines
619626 cursor_char,
620627 selection,
621628 cursor,
622− last_hovered_word,
623629 ..
624630 } = { &terminal_handle.read(cx).last_content };
625631
crates/terminal_view/src/terminal_view.rs+56−47
@@ -33,7 +33,7 @@ use terminal::{ · 32 unmodified lines
3333 index::Point,
3434 term::{search::RegexSearch, TermMode},
3535 },
36− Event, Terminal, TerminalBlink, WorkingDirectory,
36+ Event, MaybeNavigationTarget, Terminal, TerminalBlink, WorkingDirectory,
3737 };
3838 use util::{paths::PathLikeWithPosition, ResultExt};
3939 use workspace::{
@@ -93,6 +93,7 @@ pub struct TerminalView { · 53 unmodified lines
9393 blinking_on: bool,
9494 blinking_paused: bool,
9595 blink_epoch: usize,
96+ can_navigate_to_selected_word: bool,
9697 workspace_id: WorkspaceId,
9798 }
9899
@@ -169,55 +170,61 @@ impl TerminalView { · 70 unmodified lines
169170 .detach();
170171 }
171172 }
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+ }
178180 }
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+ }
214221 }
215− }
216− anyhow::Ok(())
217− })
218− .detach_and_log_err(cx);
222+ anyhow::Ok(())
223+ })
224+ .detach_and_log_err(cx);
225+ }
219226 }
220− }
227+ },
221228 _ => cx.emit(event.clone()),
222229 })
223230 .detach();
@@ -231,6 +238,7 @@ impl TerminalView { · 7 unmodified lines
231238 blinking_on: false,
232239 blinking_paused: false,
233240 blink_epoch: 0,
241+ can_navigate_to_selected_word: false,
234242 workspace_id,
235243 }
236244 }
@@ -466,6 +474,7 @@ impl View for TerminalView { · 229 unmodified lines
466474 terminal_handle,
467475 focused,
468476 self.should_show_cursor(focused, cx),
477+ self.can_navigate_to_selected_word,
469478 )
470479 .contained(),
471480 )

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