1mod persistence;
2pub mod terminal_element;
3pub mod terminal_panel;
4
5use crate::{persistence::TERMINAL_DB, terminal_element::TerminalElement};
6use anyhow::Context;
7use context_menu::{ContextMenu, ContextMenuItem};
8use dirs::home_dir;
9use editor::{scroll::autoscroll::Autoscroll, Editor};
10use gpui::{
11 actions,
12 elements::{AnchorCorner, ChildView, Flex, Label, ParentElement, Stack},
13 geometry::vector::Vector2F,
14 impl_actions,
15 keymap_matcher::{KeymapContext, Keystroke},
16 platform::{KeyDownEvent, ModifiersChangedEvent},
17 AnyElement, AnyViewHandle, AppContext, Element, Entity, ModelHandle, Task, View, ViewContext,
18 ViewHandle, WeakViewHandle,
19};
20use language::Bias;
21use project::{LocalWorktree, Project};
22use serde::Deserialize;
23use smallvec::{smallvec, SmallVec};
24use smol::Timer;
25use std::{
26 borrow::Cow,
27 ops::RangeInclusive,
28 path::{Path, PathBuf},
29 time::Duration,
30};
31use terminal::{
32 alacritty_terminal::{
33 index::Point,
34 term::{search::RegexSearch, TermMode},
35 },
36 terminal_settings::{TerminalBlink, TerminalSettings, WorkingDirectory},
37 Event, MaybeNavigationTarget, Terminal,
38};
39use util::{paths::PathLikeWithPosition, ResultExt};
40use workspace::{
41 item::{BreadcrumbText, Item, ItemEvent},
42 notifications::NotifyResultExt,
43 pane, register_deserializable_item,
44 searchable::{SearchEvent, SearchOptions, SearchableItem, SearchableItemHandle},
45 NewCenterTerminal, Pane, ToolbarItemLocation, Workspace, WorkspaceId,
46};
47
48const CURSOR_BLINK_INTERVAL: Duration = Duration::from_millis(500);
49
50///Event to transmit the scroll from the element to the view
51#[derive(Clone, Debug, PartialEq)]
52pub struct ScrollTerminal(pub i32);
53
54#[derive(Clone, Default, Deserialize, PartialEq)]
55pub struct SendText(String);
56
57#[derive(Clone, Default, Deserialize, PartialEq)]
58pub struct SendKeystroke(String);
59
60actions!(
61 terminal,
62 [Clear, Copy, Paste, ShowCharacterPalette, SearchTest]
63);
64
65impl_actions!(terminal, [SendText, SendKeystroke]);
66
67pub fn init(cx: &mut AppContext) {
68 terminal_panel::init(cx);
69 terminal::init(cx);
70
71 register_deserializable_item::<TerminalView>(cx);
72
73 cx.add_action(TerminalView::deploy);
74
75 //Useful terminal views
76 cx.add_action(TerminalView::send_text);
77 cx.add_action(TerminalView::send_keystroke);
78 cx.add_action(TerminalView::copy);
79 cx.add_action(TerminalView::paste);
80 cx.add_action(TerminalView::clear);
81 cx.add_action(TerminalView::show_character_palette);
82 cx.add_action(TerminalView::select_all)
83}
84
85///A terminal view, maintains the PTY's file handles and communicates with the terminal
86pub struct TerminalView {
87 terminal: ModelHandle<Terminal>,
88 has_new_content: bool,
89 //Currently using iTerm bell, show bell emoji in tab until input is received
90 has_bell: bool,
91 context_menu: ViewHandle<ContextMenu>,
92 blink_state: bool,
93 blinking_on: bool,
94 blinking_paused: bool,
95 blink_epoch: usize,
96 can_navigate_to_selected_word: bool,
97 workspace_id: WorkspaceId,
98}
99
100impl Entity for TerminalView {
101 type Event = Event;
102}
103
104impl TerminalView {
105 ///Create a new Terminal in the current working directory or the user's home directory
106 pub fn deploy(
107 workspace: &mut Workspace,
108 _: &NewCenterTerminal,
109 cx: &mut ViewContext<Workspace>,
110 ) {
111 let strategy = settings::get::<TerminalSettings>(cx);
112 let working_directory =
113 get_working_directory(workspace, cx, strategy.working_directory.clone());
114
115 let window = cx.window();
116 let terminal = workspace
117 .project()
118 .update(cx, |project, cx| {
119 project.create_terminal(working_directory, window, cx)
120 })
121 .notify_err(workspace, cx);
122
123 if let Some(terminal) = terminal {
124 let view = cx.add_view(|cx| {
125 TerminalView::new(
126 terminal,
127 workspace.weak_handle(),
128 workspace.database_id(),
129 cx,
130 )
131 });
132 workspace.add_item(Box::new(view), cx)
133 }
134 }
135
136 pub fn new(
137 terminal: ModelHandle<Terminal>,
138 workspace: WeakViewHandle<Workspace>,
139 workspace_id: WorkspaceId,
140 cx: &mut ViewContext<Self>,
141 ) -> Self {
142 let view_id = cx.view_id();
143 cx.observe(&terminal, |_, _, cx| cx.notify()).detach();
144 cx.subscribe(&terminal, move |this, _, event, cx| match event {
145 Event::Wakeup => {
146 if !cx.is_self_focused() {
147 this.has_new_content = true;
148 }
149 cx.notify();
150 cx.emit(Event::Wakeup);
151 }
152 Event::Bell => {
153 this.has_bell = true;
154 cx.emit(Event::Wakeup);
155 }
156 Event::BlinkChanged => this.blinking_on = !this.blinking_on,
157 Event::TitleChanged => {
158 if let Some(foreground_info) = &this.terminal().read(cx).foreground_process_info {
159 let cwd = foreground_info.cwd.clone();
160
161 let item_id = cx.view_id();
162 let workspace_id = this.workspace_id;
163 cx.background()
164 .spawn(async move {
165 TERMINAL_DB
166 .save_working_directory(item_id, workspace_id, cwd)
167 .await
168 .log_err();
169 })
170 .detach();
171 }
172 }
173 Event::NewNavigationTarget(maybe_navigation_target) => {
174 this.can_navigate_to_selected_word = match maybe_navigation_target {
175 Some(MaybeNavigationTarget::Url(_)) => true,
176 Some(MaybeNavigationTarget::PathLike(maybe_path)) => {
177 !possible_open_targets(&workspace, maybe_path, cx).is_empty()
178 }
179 None => false,
180 }
181 }
182 Event::Open(maybe_navigation_target) => match maybe_navigation_target {
183 MaybeNavigationTarget::Url(url) => cx.platform().open_url(url),
184 MaybeNavigationTarget::PathLike(maybe_path) => {
185 if !this.can_navigate_to_selected_word {
186 return;
187 }
188 let potential_abs_paths = possible_open_targets(&workspace, maybe_path, cx);
189 if let Some(path) = potential_abs_paths.into_iter().next() {
190 let is_dir = path.path_like.is_dir();
191 let task_workspace = workspace.clone();
192 cx.spawn(|_, mut cx| async move {
193 let opened_items = task_workspace
194 .update(&mut cx, |workspace, cx| {
195 workspace.open_paths(vec![path.path_like], is_dir, cx)
196 })
197 .context("workspace update")?
198 .await;
199 anyhow::ensure!(
200 opened_items.len() == 1,
201 "For a single path open, expected single opened item"
202 );
203 let opened_item = opened_items
204 .into_iter()
205 .next()
206 .unwrap()
207 .transpose()
208 .context("path open")?;
209 if is_dir {
210 task_workspace.update(&mut cx, |workspace, cx| {
211 workspace.project().update(cx, |_, cx| {
212 cx.emit(project::Event::ActivateProjectPanel);
213 })
214 })?;
215 } else {
216 if let Some(row) = path.row {
217 let col = path.column.unwrap_or(0);
218 if let Some(active_editor) =
219 opened_item.and_then(|item| item.downcast::<Editor>())
220 {
221 active_editor
222 .downgrade()
223 .update(&mut cx, |editor, cx| {
224 let snapshot = editor.snapshot(cx).display_snapshot;
225 let point = snapshot.buffer_snapshot.clip_point(
226 language::Point::new(
227 row.saturating_sub(1),
228 col.saturating_sub(1),
229 ),
230 Bias::Left,
231 );
232 editor.change_selections(
233 Some(Autoscroll::center()),
234 cx,
235 |s| s.select_ranges([point..point]),
236 );
237 })
238 .log_err();
239 }
240 }
241 }
242 anyhow::Ok(())
243 })
244 .detach_and_log_err(cx);
245 }
246 }
247 },
248 _ => cx.emit(event.clone()),
249 })
250 .detach();
251
252 Self {
253 terminal,
254 has_new_content: true,
255 has_bell: false,
256 context_menu: cx.add_view(|cx| ContextMenu::new(view_id, cx)),
257 blink_state: true,
258 blinking_on: false,
259 blinking_paused: false,
260 blink_epoch: 0,
261 can_navigate_to_selected_word: false,
262 workspace_id,
263 }
264 }
265
266 pub fn model(&self) -> &ModelHandle<Terminal> {
267 &self.terminal
268 }
269
270 pub fn has_new_content(&self) -> bool {
271 self.has_new_content
272 }
273
274 pub fn has_bell(&self) -> bool {
275 self.has_bell
276 }
277
278 pub fn clear_bel(&mut self, cx: &mut ViewContext<TerminalView>) {
279 self.has_bell = false;
280 cx.emit(Event::Wakeup);
281 }
282
283 pub fn deploy_context_menu(&mut self, position: Vector2F, cx: &mut ViewContext<Self>) {
284 let menu_entries = vec![
285 ContextMenuItem::action("Clear", Clear),
286 ContextMenuItem::action("Close", pane::CloseActiveItem),
287 ];
288
289 self.context_menu.update(cx, |menu, cx| {
290 menu.show(position, AnchorCorner::TopLeft, menu_entries, cx)
291 });
292
293 cx.notify();
294 }
295
296 fn show_character_palette(&mut self, _: &ShowCharacterPalette, cx: &mut ViewContext<Self>) {
297 if !self
298 .terminal
299 .read(cx)
300 .last_content
301 .mode
302 .contains(TermMode::ALT_SCREEN)
303 {
304 cx.show_character_palette();
305 } else {
306 self.terminal.update(cx, |term, cx| {
307 term.try_keystroke(
308 &Keystroke::parse("ctrl-cmd-space").unwrap(),
309 settings::get::<TerminalSettings>(cx).option_as_meta,
310 )
311 });
312 }
313 }
314
315 fn select_all(&mut self, _: &editor::SelectAll, cx: &mut ViewContext<Self>) {
316 self.terminal.update(cx, |term, _| term.select_all());
317 cx.notify();
318 }
319
320 fn clear(&mut self, _: &Clear, cx: &mut ViewContext<Self>) {
321 self.terminal.update(cx, |term, _| term.clear());
322 cx.notify();
323 }
324
325 pub fn should_show_cursor(&self, focused: bool, cx: &mut gpui::ViewContext<Self>) -> bool {
326 //Don't blink the cursor when not focused, blinking is disabled, or paused
327 if !focused
328 || !self.blinking_on
329 || self.blinking_paused
330 || self
331 .terminal
332 .read(cx)
333 .last_content
334 .mode
335 .contains(TermMode::ALT_SCREEN)
336 {
337 return true;
338 }
339
340 match settings::get::<TerminalSettings>(cx).blinking {
341 //If the user requested to never blink, don't blink it.
342 TerminalBlink::Off => true,
343 //If the terminal is controlling it, check terminal mode
344 TerminalBlink::TerminalControlled | TerminalBlink::On => self.blink_state,
345 }
346 }
347
348 fn blink_cursors(&mut self, epoch: usize, cx: &mut ViewContext<Self>) {
349 if epoch == self.blink_epoch && !self.blinking_paused {
350 self.blink_state = !self.blink_state;
351 cx.notify();
352
353 let epoch = self.next_blink_epoch();
354 cx.spawn(|this, mut cx| async move {
355 Timer::after(CURSOR_BLINK_INTERVAL).await;
356 this.update(&mut cx, |this, cx| this.blink_cursors(epoch, cx))
357 .log_err();
358 })
359 .detach();
360 }
361 }
362
363 pub fn pause_cursor_blinking(&mut self, cx: &mut ViewContext<Self>) {
364 self.blink_state = true;
365 cx.notify();
366
367 let epoch = self.next_blink_epoch();
368 cx.spawn(|this, mut cx| async move {
369 Timer::after(CURSOR_BLINK_INTERVAL).await;
370 this.update(&mut cx, |this, cx| this.resume_cursor_blinking(epoch, cx))
371 .ok();
372 })
373 .detach();
374 }
375
376 pub fn find_matches(
377 &mut self,
378 query: project::search::SearchQuery,
379 cx: &mut ViewContext<Self>,
380 ) -> Task<Vec<RangeInclusive<Point>>> {
381 let searcher = regex_search_for_query(query);
382
383 if let Some(searcher) = searcher {
384 self.terminal
385 .update(cx, |term, cx| term.find_matches(searcher, cx))
386 } else {
387 cx.background().spawn(async { Vec::new() })
388 }
389 }
390
391 pub fn terminal(&self) -> &ModelHandle<Terminal> {
392 &self.terminal
393 }
394
395 fn next_blink_epoch(&mut self) -> usize {
396 self.blink_epoch += 1;
397 self.blink_epoch
398 }
399
400 fn resume_cursor_blinking(&mut self, epoch: usize, cx: &mut ViewContext<Self>) {
401 if epoch == self.blink_epoch {
402 self.blinking_paused = false;
403 self.blink_cursors(epoch, cx);
404 }
405 }
406
407 ///Attempt to paste the clipboard into the terminal
408 fn copy(&mut self, _: &Copy, cx: &mut ViewContext<Self>) {
409 self.terminal.update(cx, |term, _| term.copy())
410 }
411
412 ///Attempt to paste the clipboard into the terminal
413 fn paste(&mut self, _: &Paste, cx: &mut ViewContext<Self>) {
414 if let Some(item) = cx.read_from_clipboard() {
415 self.terminal
416 .update(cx, |terminal, _cx| terminal.paste(item.text()));
417 }
418 }
419
420 fn send_text(&mut self, text: &SendText, cx: &mut ViewContext<Self>) {
421 self.clear_bel(cx);
422 self.terminal.update(cx, |term, _| {
423 term.input(text.0.to_string());
424 });
425 }
426
427 fn send_keystroke(&mut self, text: &SendKeystroke, cx: &mut ViewContext<Self>) {
428 if let Some(keystroke) = Keystroke::parse(&text.0).log_err() {
429 self.clear_bel(cx);
430 self.terminal.update(cx, |term, cx| {
431 term.try_keystroke(
432 &keystroke,
433 settings::get::<TerminalSettings>(cx).option_as_meta,
434 );
435 });
436 }
437 }
438}
439
440fn possible_open_targets(
441 workspace: &WeakViewHandle<Workspace>,
442 maybe_path: &String,
443 cx: &mut ViewContext<'_, '_, TerminalView>,
444) -> Vec<PathLikeWithPosition<PathBuf>> {
445 let path_like = PathLikeWithPosition::parse_str(maybe_path.as_str(), |path_str| {
446 Ok::<_, std::convert::Infallible>(Path::new(path_str).to_path_buf())
447 })
448 .expect("infallible");
449 let maybe_path = path_like.path_like;
450 let potential_abs_paths = if maybe_path.is_absolute() {
451 vec![maybe_path]
452 } else if maybe_path.starts_with("~") {
453 if let Some(abs_path) = maybe_path
454 .strip_prefix("~")
455 .ok()
456 .and_then(|maybe_path| Some(dirs::home_dir()?.join(maybe_path)))
457 {
458 vec![abs_path]
459 } else {
460 Vec::new()
461 }
462 } else if let Some(workspace) = workspace.upgrade(cx) {
463 workspace.update(cx, |workspace, cx| {
464 workspace
465 .worktrees(cx)
466 .map(|worktree| worktree.read(cx).abs_path().join(&maybe_path))
467 .collect()
468 })
469 } else {
470 Vec::new()
471 };
472
473 potential_abs_paths
474 .into_iter()
475 .filter(|path| path.exists())
476 .map(|path| PathLikeWithPosition {
477 path_like: path,
478 row: path_like.row,
479 column: path_like.column,
480 })
481 .collect()
482}
483
484pub fn regex_search_for_query(query: project::search::SearchQuery) -> Option<RegexSearch> {
485 let query = query.as_str();
486 let searcher = RegexSearch::new(&query);
487 searcher.ok()
488}
489
490impl View for TerminalView {
491 fn ui_name() -> &'static str {
492 "Terminal"
493 }
494
495 fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> AnyElement<Self> {
496 let terminal_handle = self.terminal.clone().downgrade();
497
498 let self_id = cx.view_id();
499 let focused = cx
500 .focused_view_id()
501 .filter(|view_id| *view_id == self_id)
502 .is_some();
503
504 Stack::new()
505 .with_child(
506 TerminalElement::new(
507 terminal_handle,
508 focused,
509 self.should_show_cursor(focused, cx),
510 self.can_navigate_to_selected_word,
511 )
512 .contained(),
513 )
514 .with_child(ChildView::new(&self.context_menu, cx))
515 .into_any()
516 }
517
518 fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
519 self.has_new_content = false;
520 self.terminal.read(cx).focus_in();
521 self.blink_cursors(self.blink_epoch, cx);
522 cx.notify();
523 }
524
525 fn focus_out(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
526 self.terminal.update(cx, |terminal, _| {
527 terminal.focus_out();
528 });
529 cx.notify();
530 }
531
532 fn modifiers_changed(
533 &mut self,
534 event: &ModifiersChangedEvent,
535 cx: &mut ViewContext<Self>,
536 ) -> bool {
537 let handled = self
538 .terminal()
539 .update(cx, |term, _| term.try_modifiers_change(&event.modifiers));
540 if handled {
541 cx.notify();
542 }
543 handled
544 }
545
546 fn key_down(&mut self, event: &KeyDownEvent, cx: &mut ViewContext<Self>) -> bool {
547 self.clear_bel(cx);
548 self.pause_cursor_blinking(cx);
549
550 self.terminal.update(cx, |term, cx| {
551 term.try_keystroke(
552 &event.keystroke,
553 settings::get::<TerminalSettings>(cx).option_as_meta,
554 )
555 })
556 }
557
558 //IME stuff
559 fn selected_text_range(&self, cx: &AppContext) -> Option<std::ops::Range<usize>> {
560 if self
561 .terminal
562 .read(cx)
563 .last_content
564 .mode
565 .contains(TermMode::ALT_SCREEN)
566 {
567 None
568 } else {
569 Some(0..0)
570 }
571 }
572
573 fn replace_text_in_range(
574 &mut self,
575 _: Option<std::ops::Range<usize>>,
576 text: &str,
577 cx: &mut ViewContext<Self>,
578 ) {
579 self.terminal.update(cx, |terminal, _| {
580 terminal.input(text.into());
581 });
582 }
583
584 fn update_keymap_context(&self, keymap: &mut KeymapContext, cx: &gpui::AppContext) {
585 Self::reset_to_default_keymap_context(keymap);
586
587 let mode = self.terminal.read(cx).last_content.mode;
588 keymap.add_key(
589 "screen",
590 if mode.contains(TermMode::ALT_SCREEN) {
591 "alt"
592 } else {
593 "normal"
594 },
595 );
596
597 if mode.contains(TermMode::APP_CURSOR) {
598 keymap.add_identifier("DECCKM");
599 }
600 if mode.contains(TermMode::APP_KEYPAD) {
601 keymap.add_identifier("DECPAM");
602 } else {
603 keymap.add_identifier("DECPNM");
604 }
605 if mode.contains(TermMode::SHOW_CURSOR) {
606 keymap.add_identifier("DECTCEM");
607 }
608 if mode.contains(TermMode::LINE_WRAP) {
609 keymap.add_identifier("DECAWM");
610 }
611 if mode.contains(TermMode::ORIGIN) {
612 keymap.add_identifier("DECOM");
613 }
614 if mode.contains(TermMode::INSERT) {
615 keymap.add_identifier("IRM");
616 }
617 //LNM is apparently the name for this. https://vt100.net/docs/vt510-rm/LNM.html
618 if mode.contains(TermMode::LINE_FEED_NEW_LINE) {
619 keymap.add_identifier("LNM");
620 }
621 if mode.contains(TermMode::FOCUS_IN_OUT) {
622 keymap.add_identifier("report_focus");
623 }
624 if mode.contains(TermMode::ALTERNATE_SCROLL) {
625 keymap.add_identifier("alternate_scroll");
626 }
627 if mode.contains(TermMode::BRACKETED_PASTE) {
628 keymap.add_identifier("bracketed_paste");
629 }
630 if mode.intersects(TermMode::MOUSE_MODE) {
631 keymap.add_identifier("any_mouse_reporting");
632 }
633 {
634 let mouse_reporting = if mode.contains(TermMode::MOUSE_REPORT_CLICK) {
635 "click"
636 } else if mode.contains(TermMode::MOUSE_DRAG) {
637 "drag"
638 } else if mode.contains(TermMode::MOUSE_MOTION) {
639 "motion"
640 } else {
641 "off"
642 };
643 keymap.add_key("mouse_reporting", mouse_reporting);
644 }
645 {
646 let format = if mode.contains(TermMode::SGR_MOUSE) {
647 "sgr"
648 } else if mode.contains(TermMode::UTF8_MOUSE) {
649 "utf8"
650 } else {
651 "normal"
652 };
653 keymap.add_key("mouse_format", format);
654 }
655 }
656}
657
658impl Item for TerminalView {
659 fn tab_tooltip_text(&self, cx: &AppContext) -> Option<Cow<str>> {
660 Some(self.terminal().read(cx).title().into())
661 }
662
663 fn tab_content<T: 'static>(
664 &self,
665 _detail: Option<usize>,
666 tab_theme: &theme::Tab,
667 cx: &gpui::AppContext,
668 ) -> AnyElement<T> {
669 let title = self.terminal().read(cx).title();
670
671 Flex::row()
672 .with_child(
673 gpui::elements::Svg::new("icons/terminal.svg")
674 .with_color(tab_theme.label.text.color)
675 .constrained()
676 .with_width(tab_theme.type_icon_width)
677 .aligned()
678 .contained()
679 .with_margin_right(tab_theme.spacing),
680 )
681 .with_child(Label::new(title, tab_theme.label.clone()).aligned())
682 .into_any()
683 }
684
685 fn clone_on_split(
686 &self,
687 _workspace_id: WorkspaceId,
688 _cx: &mut ViewContext<Self>,
689 ) -> Option<Self> {
690 //From what I can tell, there's no way to tell the current working
691 //Directory of the terminal from outside the shell. There might be
692 //solutions to this, but they are non-trivial and require more IPC
693
694 // Some(TerminalContainer::new(
695 // Err(anyhow::anyhow!("failed to instantiate terminal")),
696 // workspace_id,
697 // cx,
698 // ))
699
700 // TODO
701 None
702 }
703
704 fn is_dirty(&self, _cx: &gpui::AppContext) -> bool {
705 self.has_bell()
706 }
707
708 fn has_conflict(&self, _cx: &AppContext) -> bool {
709 false
710 }
711
712 fn as_searchable(&self, handle: &ViewHandle<Self>) -> Option<Box<dyn SearchableItemHandle>> {
713 Some(Box::new(handle.clone()))
714 }
715
716 fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
717 match event {
718 Event::BreadcrumbsChanged => smallvec![ItemEvent::UpdateBreadcrumbs],
719 Event::TitleChanged | Event::Wakeup => smallvec![ItemEvent::UpdateTab],
720 Event::CloseTerminal => smallvec![ItemEvent::CloseItem],
721 _ => smallvec![],
722 }
723 }
724
725 fn breadcrumb_location(&self) -> ToolbarItemLocation {
726 ToolbarItemLocation::PrimaryLeft { flex: None }
727 }
728
729 fn breadcrumbs(&self, _: &theme::Theme, cx: &AppContext) -> Option<Vec<BreadcrumbText>> {
730 Some(vec![BreadcrumbText {
731 text: self.terminal().read(cx).breadcrumb_text.clone(),
732 highlights: None,
733 }])
734 }
735
736 fn serialized_item_kind() -> Option<&'static str> {
737 Some("Terminal")
738 }
739
740 fn deserialize(
741 project: ModelHandle<Project>,
742 workspace: WeakViewHandle<Workspace>,
743 workspace_id: workspace::WorkspaceId,
744 item_id: workspace::ItemId,
745 cx: &mut ViewContext<Pane>,
746 ) -> Task<anyhow::Result<ViewHandle<Self>>> {
747 let window = cx.window();
748 cx.spawn(|pane, mut cx| async move {
749 let cwd = TERMINAL_DB
750 .get_working_directory(item_id, workspace_id)
751 .log_err()
752 .flatten()
753 .or_else(|| {
754 cx.read(|cx| {
755 let strategy = settings::get::<TerminalSettings>(cx)
756 .working_directory
757 .clone();
758 workspace
759 .upgrade(cx)
760 .map(|workspace| {
761 get_working_directory(workspace.read(cx), cx, strategy)
762 })
763 .flatten()
764 })
765 });
766
767 let terminal = project.update(&mut cx, |project, cx| {
768 project.create_terminal(cwd, window, cx)
769 })?;
770 Ok(pane.update(&mut cx, |_, cx| {
771 cx.add_view(|cx| TerminalView::new(terminal, workspace, workspace_id, cx))
772 })?)
773 })
774 }
775
776 fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext<Self>) {
777 cx.background()
778 .spawn(TERMINAL_DB.update_workspace_id(
779 workspace.database_id(),
780 self.workspace_id,
781 cx.view_id(),
782 ))
783 .detach();
784 self.workspace_id = workspace.database_id();
785 }
786}
787
788impl SearchableItem for TerminalView {
789 type Match = RangeInclusive<Point>;
790
791 fn supported_options() -> SearchOptions {
792 SearchOptions {
793 case: false,
794 word: false,
795 regex: false,
796 }
797 }
798
799 /// Convert events raised by this item into search-relevant events (if applicable)
800 fn to_search_event(
801 &mut self,
802 event: &Self::Event,
803 _: &mut ViewContext<Self>,
804 ) -> Option<SearchEvent> {
805 match event {
806 Event::Wakeup => Some(SearchEvent::MatchesInvalidated),
807 Event::SelectionsChanged => Some(SearchEvent::ActiveMatchChanged),
808 _ => None,
809 }
810 }
811
812 /// Clear stored matches
813 fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
814 self.terminal().update(cx, |term, _| term.matches.clear())
815 }
816
817 /// Store matches returned from find_matches somewhere for rendering
818 fn update_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
819 self.terminal().update(cx, |term, _| term.matches = matches)
820 }
821
822 /// Return the selection content to pre-load into this search
823 fn query_suggestion(&mut self, cx: &mut ViewContext<Self>) -> String {
824 self.terminal()
825 .read(cx)
826 .last_content
827 .selection_text
828 .clone()
829 .unwrap_or_default()
830 }
831
832 /// Focus match at given index into the Vec of matches
833 fn activate_match(&mut self, index: usize, _: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
834 self.terminal()
835 .update(cx, |term, _| term.activate_match(index));
836 cx.notify();
837 }
838
839 /// Add selections for all matches given.
840 fn select_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
841 self.terminal()
842 .update(cx, |term, _| term.select_matches(matches));
843 cx.notify();
844 }
845
846 /// Get all of the matches for this query, should be done on the background
847 fn find_matches(
848 &mut self,
849 query: project::search::SearchQuery,
850 cx: &mut ViewContext<Self>,
851 ) -> Task<Vec<Self::Match>> {
852 if let Some(searcher) = regex_search_for_query(query) {
853 self.terminal()
854 .update(cx, |term, cx| term.find_matches(searcher, cx))
855 } else {
856 Task::ready(vec![])
857 }
858 }
859
860 /// Reports back to the search toolbar what the active match should be (the selection)
861 fn active_match_index(
862 &mut self,
863 matches: Vec<Self::Match>,
864 cx: &mut ViewContext<Self>,
865 ) -> Option<usize> {
866 // Selection head might have a value if there's a selection that isn't
867 // associated with a match. Therefore, if there are no matches, we should
868 // report None, no matter the state of the terminal
869 let res = if matches.len() > 0 {
870 if let Some(selection_head) = self.terminal().read(cx).selection_head {
871 // If selection head is contained in a match. Return that match
872 if let Some(ix) = matches
873 .iter()
874 .enumerate()
875 .find(|(_, search_match)| {
876 search_match.contains(&selection_head)
877 || search_match.start() > &selection_head
878 })
879 .map(|(ix, _)| ix)
880 {
881 Some(ix)
882 } else {
883 // If no selection after selection head, return the last match
884 Some(matches.len().saturating_sub(1))
885 }
886 } else {
887 // Matches found but no active selection, return the first last one (closest to cursor)
888 Some(matches.len().saturating_sub(1))
889 }
890 } else {
891 None
892 };
893
894 res
895 }
896}
897
898///Get's the working directory for the given workspace, respecting the user's settings.
899pub fn get_working_directory(
900 workspace: &Workspace,
901 cx: &AppContext,
902 strategy: WorkingDirectory,
903) -> Option<PathBuf> {
904 let res = match strategy {
905 WorkingDirectory::CurrentProjectDirectory => current_project_directory(workspace, cx)
906 .or_else(|| first_project_directory(workspace, cx)),
907 WorkingDirectory::FirstProjectDirectory => first_project_directory(workspace, cx),
908 WorkingDirectory::AlwaysHome => None,
909 WorkingDirectory::Always { directory } => {
910 shellexpand::full(&directory) //TODO handle this better
911 .ok()
912 .map(|dir| Path::new(&dir.to_string()).to_path_buf())
913 .filter(|dir| dir.is_dir())
914 }
915 };
916 res.or_else(home_dir)
917}
918
919///Get's the first project's home directory, or the home directory
920fn first_project_directory(workspace: &Workspace, cx: &AppContext) -> Option<PathBuf> {
921 workspace
922 .worktrees(cx)
923 .next()
924 .and_then(|worktree_handle| worktree_handle.read(cx).as_local())
925 .and_then(get_path_from_wt)
926}
927
928///Gets the intuitively correct working directory from the given workspace
929///If there is an active entry for this project, returns that entry's worktree root.
930///If there's no active entry but there is a worktree, returns that worktrees root.
931///If either of these roots are files, or if there are any other query failures,
932/// returns the user's home directory
933fn current_project_directory(workspace: &Workspace, cx: &AppContext) -> Option<PathBuf> {
934 let project = workspace.project().read(cx);
935
936 project
937 .active_entry()
938 .and_then(|entry_id| project.worktree_for_entry(entry_id, cx))
939 .or_else(|| workspace.worktrees(cx).next())
940 .and_then(|worktree_handle| worktree_handle.read(cx).as_local())
941 .and_then(get_path_from_wt)
942}
943
944fn get_path_from_wt(wt: &LocalWorktree) -> Option<PathBuf> {
945 wt.root_entry()
946 .filter(|re| re.is_dir())
947 .map(|_| wt.abs_path().to_path_buf())
948}
949
950#[cfg(test)]
951mod tests {
952 use super::*;
953 use gpui::TestAppContext;
954 use project::{Entry, Project, ProjectPath, Worktree};
955 use std::path::Path;
956 use workspace::AppState;
957
958 // Working directory calculation tests
959
960 // No Worktrees in project -> home_dir()
961 #[gpui::test]
962 async fn no_worktree(cx: &mut TestAppContext) {
963 let (project, workspace) = init_test(cx).await;
964 cx.read(|cx| {
965 let workspace = workspace.read(cx);
966 let active_entry = project.read(cx).active_entry();
967
968 //Make sure environment is as expected
969 assert!(active_entry.is_none());
970 assert!(workspace.worktrees(cx).next().is_none());
971
972 let res = current_project_directory(workspace, cx);
973 assert_eq!(res, None);
974 let res = first_project_directory(workspace, cx);
975 assert_eq!(res, None);
976 });
977 }
978
979 // No active entry, but a worktree, worktree is a file -> home_dir()
980 #[gpui::test]
981 async fn no_active_entry_worktree_is_file(cx: &mut TestAppContext) {
982 let (project, workspace) = init_test(cx).await;
983
984 create_file_wt(project.clone(), "/root.txt", cx).await;
985 cx.read(|cx| {
986 let workspace = workspace.read(cx);
987 let active_entry = project.read(cx).active_entry();
988
989 //Make sure environment is as expected
990 assert!(active_entry.is_none());
991 assert!(workspace.worktrees(cx).next().is_some());
992
993 let res = current_project_directory(workspace, cx);
994 assert_eq!(res, None);
995 let res = first_project_directory(workspace, cx);
996 assert_eq!(res, None);
997 });
998 }
999
1000 // No active entry, but a worktree, worktree is a folder -> worktree_folder
1001 #[gpui::test]
1002 async fn no_active_entry_worktree_is_dir(cx: &mut TestAppContext) {
1003 let (project, workspace) = init_test(cx).await;
1004
1005 let (_wt, _entry) = create_folder_wt(project.clone(), "/root/", cx).await;
1006 cx.update(|cx| {
1007 let workspace = workspace.read(cx);
1008 let active_entry = project.read(cx).active_entry();
1009
1010 assert!(active_entry.is_none());
1011 assert!(workspace.worktrees(cx).next().is_some());
1012
1013 let res = current_project_directory(workspace, cx);
1014 assert_eq!(res, Some((Path::new("/root/")).to_path_buf()));
1015 let res = first_project_directory(workspace, cx);
1016 assert_eq!(res, Some((Path::new("/root/")).to_path_buf()));
1017 });
1018 }
1019
1020 // Active entry with a work tree, worktree is a file -> home_dir()
1021 #[gpui::test]
1022 async fn active_entry_worktree_is_file(cx: &mut TestAppContext) {
1023 let (project, workspace) = init_test(cx).await;
1024
1025 let (_wt, _entry) = create_folder_wt(project.clone(), "/root1/", cx).await;
1026 let (wt2, entry2) = create_file_wt(project.clone(), "/root2.txt", cx).await;
1027 insert_active_entry_for(wt2, entry2, project.clone(), cx);
1028
1029 cx.update(|cx| {
1030 let workspace = workspace.read(cx);
1031 let active_entry = project.read(cx).active_entry();
1032
1033 assert!(active_entry.is_some());
1034
1035 let res = current_project_directory(workspace, cx);
1036 assert_eq!(res, None);
1037 let res = first_project_directory(workspace, cx);
1038 assert_eq!(res, Some((Path::new("/root1/")).to_path_buf()));
1039 });
1040 }
1041
1042 // Active entry, with a worktree, worktree is a folder -> worktree_folder
1043 #[gpui::test]
1044 async fn active_entry_worktree_is_dir(cx: &mut TestAppContext) {
1045 let (project, workspace) = init_test(cx).await;
1046
1047 let (_wt, _entry) = create_folder_wt(project.clone(), "/root1/", cx).await;
1048 let (wt2, entry2) = create_folder_wt(project.clone(), "/root2/", cx).await;
1049 insert_active_entry_for(wt2, entry2, project.clone(), cx);
1050
1051 cx.update(|cx| {
1052 let workspace = workspace.read(cx);
1053 let active_entry = project.read(cx).active_entry();
1054
1055 assert!(active_entry.is_some());
1056
1057 let res = current_project_directory(workspace, cx);
1058 assert_eq!(res, Some((Path::new("/root2/")).to_path_buf()));
1059 let res = first_project_directory(workspace, cx);
1060 assert_eq!(res, Some((Path::new("/root1/")).to_path_buf()));
1061 });
1062 }
1063
1064 /// Creates a worktree with 1 file: /root.txt
1065 pub async fn init_test(
1066 cx: &mut TestAppContext,
1067 ) -> (ModelHandle<Project>, ViewHandle<Workspace>) {
1068 let params = cx.update(AppState::test);
1069 cx.update(|cx| {
1070 theme::init((), cx);
1071 Project::init_settings(cx);
1072 language::init(cx);
1073 });
1074
1075 let project = Project::test(params.fs.clone(), [], cx).await;
1076 let workspace = cx
1077 .add_window(|cx| Workspace::test_new(project.clone(), cx))
1078 .root(cx);
1079
1080 (project, workspace)
1081 }
1082
1083 /// Creates a worktree with 1 folder: /root{suffix}/
1084 async fn create_folder_wt(
1085 project: ModelHandle<Project>,
1086 path: impl AsRef<Path>,
1087 cx: &mut TestAppContext,
1088 ) -> (ModelHandle<Worktree>, Entry) {
1089 create_wt(project, true, path, cx).await
1090 }
1091
1092 /// Creates a worktree with 1 file: /root{suffix}.txt
1093 async fn create_file_wt(
1094 project: ModelHandle<Project>,
1095 path: impl AsRef<Path>,
1096 cx: &mut TestAppContext,
1097 ) -> (ModelHandle<Worktree>, Entry) {
1098 create_wt(project, false, path, cx).await
1099 }
1100
1101 async fn create_wt(
1102 project: ModelHandle<Project>,
1103 is_dir: bool,
1104 path: impl AsRef<Path>,
1105 cx: &mut TestAppContext,
1106 ) -> (ModelHandle<Worktree>, Entry) {
1107 let (wt, _) = project
1108 .update(cx, |project, cx| {
1109 project.find_or_create_local_worktree(path, true, cx)
1110 })
1111 .await
1112 .unwrap();
1113
1114 let entry = cx
1115 .update(|cx| {
1116 wt.update(cx, |wt, cx| {
1117 wt.as_local()
1118 .unwrap()
1119 .create_entry(Path::new(""), is_dir, cx)
1120 })
1121 })
1122 .await
1123 .unwrap();
1124
1125 (wt, entry)
1126 }
1127
1128 pub fn insert_active_entry_for(
1129 wt: ModelHandle<Worktree>,
1130 entry: Entry,
1131 project: ModelHandle<Project>,
1132 cx: &mut TestAppContext,
1133 ) {
1134 cx.update(|cx| {
1135 let p = ProjectPath {
1136 worktree_id: wt.read(cx).id(),
1137 path: entry.path,
1138 };
1139 project.update(cx, |project, cx| project.set_active_path(Some(p), cx));
1140 });
1141 }
1142}