terminal_view.rs

   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(
 287                "Close",
 288                pane::CloseActiveItem {
 289                    save_behavior: None,
 290                },
 291            ),
 292        ];
 293
 294        self.context_menu.update(cx, |menu, cx| {
 295            menu.show(position, AnchorCorner::TopLeft, menu_entries, cx)
 296        });
 297
 298        cx.notify();
 299    }
 300
 301    fn show_character_palette(&mut self, _: &ShowCharacterPalette, cx: &mut ViewContext<Self>) {
 302        if !self
 303            .terminal
 304            .read(cx)
 305            .last_content
 306            .mode
 307            .contains(TermMode::ALT_SCREEN)
 308        {
 309            cx.show_character_palette();
 310        } else {
 311            self.terminal.update(cx, |term, cx| {
 312                term.try_keystroke(
 313                    &Keystroke::parse("ctrl-cmd-space").unwrap(),
 314                    settings::get::<TerminalSettings>(cx).option_as_meta,
 315                )
 316            });
 317        }
 318    }
 319
 320    fn select_all(&mut self, _: &editor::SelectAll, cx: &mut ViewContext<Self>) {
 321        self.terminal.update(cx, |term, _| term.select_all());
 322        cx.notify();
 323    }
 324
 325    fn clear(&mut self, _: &Clear, cx: &mut ViewContext<Self>) {
 326        self.terminal.update(cx, |term, _| term.clear());
 327        cx.notify();
 328    }
 329
 330    pub fn should_show_cursor(&self, focused: bool, cx: &mut gpui::ViewContext<Self>) -> bool {
 331        //Don't blink the cursor when not focused, blinking is disabled, or paused
 332        if !focused
 333            || !self.blinking_on
 334            || self.blinking_paused
 335            || self
 336                .terminal
 337                .read(cx)
 338                .last_content
 339                .mode
 340                .contains(TermMode::ALT_SCREEN)
 341        {
 342            return true;
 343        }
 344
 345        match settings::get::<TerminalSettings>(cx).blinking {
 346            //If the user requested to never blink, don't blink it.
 347            TerminalBlink::Off => true,
 348            //If the terminal is controlling it, check terminal mode
 349            TerminalBlink::TerminalControlled | TerminalBlink::On => self.blink_state,
 350        }
 351    }
 352
 353    fn blink_cursors(&mut self, epoch: usize, cx: &mut ViewContext<Self>) {
 354        if epoch == self.blink_epoch && !self.blinking_paused {
 355            self.blink_state = !self.blink_state;
 356            cx.notify();
 357
 358            let epoch = self.next_blink_epoch();
 359            cx.spawn(|this, mut cx| async move {
 360                Timer::after(CURSOR_BLINK_INTERVAL).await;
 361                this.update(&mut cx, |this, cx| this.blink_cursors(epoch, cx))
 362                    .log_err();
 363            })
 364            .detach();
 365        }
 366    }
 367
 368    pub fn pause_cursor_blinking(&mut self, cx: &mut ViewContext<Self>) {
 369        self.blink_state = true;
 370        cx.notify();
 371
 372        let epoch = self.next_blink_epoch();
 373        cx.spawn(|this, mut cx| async move {
 374            Timer::after(CURSOR_BLINK_INTERVAL).await;
 375            this.update(&mut cx, |this, cx| this.resume_cursor_blinking(epoch, cx))
 376                .ok();
 377        })
 378        .detach();
 379    }
 380
 381    pub fn find_matches(
 382        &mut self,
 383        query: project::search::SearchQuery,
 384        cx: &mut ViewContext<Self>,
 385    ) -> Task<Vec<RangeInclusive<Point>>> {
 386        let searcher = regex_search_for_query(query);
 387
 388        if let Some(searcher) = searcher {
 389            self.terminal
 390                .update(cx, |term, cx| term.find_matches(searcher, cx))
 391        } else {
 392            cx.background().spawn(async { Vec::new() })
 393        }
 394    }
 395
 396    pub fn terminal(&self) -> &ModelHandle<Terminal> {
 397        &self.terminal
 398    }
 399
 400    fn next_blink_epoch(&mut self) -> usize {
 401        self.blink_epoch += 1;
 402        self.blink_epoch
 403    }
 404
 405    fn resume_cursor_blinking(&mut self, epoch: usize, cx: &mut ViewContext<Self>) {
 406        if epoch == self.blink_epoch {
 407            self.blinking_paused = false;
 408            self.blink_cursors(epoch, cx);
 409        }
 410    }
 411
 412    ///Attempt to paste the clipboard into the terminal
 413    fn copy(&mut self, _: &Copy, cx: &mut ViewContext<Self>) {
 414        self.terminal.update(cx, |term, _| term.copy())
 415    }
 416
 417    ///Attempt to paste the clipboard into the terminal
 418    fn paste(&mut self, _: &Paste, cx: &mut ViewContext<Self>) {
 419        if let Some(item) = cx.read_from_clipboard() {
 420            self.terminal
 421                .update(cx, |terminal, _cx| terminal.paste(item.text()));
 422        }
 423    }
 424
 425    fn send_text(&mut self, text: &SendText, cx: &mut ViewContext<Self>) {
 426        self.clear_bel(cx);
 427        self.terminal.update(cx, |term, _| {
 428            term.input(text.0.to_string());
 429        });
 430    }
 431
 432    fn send_keystroke(&mut self, text: &SendKeystroke, cx: &mut ViewContext<Self>) {
 433        if let Some(keystroke) = Keystroke::parse(&text.0).log_err() {
 434            self.clear_bel(cx);
 435            self.terminal.update(cx, |term, cx| {
 436                term.try_keystroke(
 437                    &keystroke,
 438                    settings::get::<TerminalSettings>(cx).option_as_meta,
 439                );
 440            });
 441        }
 442    }
 443}
 444
 445fn possible_open_targets(
 446    workspace: &WeakViewHandle<Workspace>,
 447    maybe_path: &String,
 448    cx: &mut ViewContext<'_, '_, TerminalView>,
 449) -> Vec<PathLikeWithPosition<PathBuf>> {
 450    let path_like = PathLikeWithPosition::parse_str(maybe_path.as_str(), |path_str| {
 451        Ok::<_, std::convert::Infallible>(Path::new(path_str).to_path_buf())
 452    })
 453    .expect("infallible");
 454    let maybe_path = path_like.path_like;
 455    let potential_abs_paths = if maybe_path.is_absolute() {
 456        vec![maybe_path]
 457    } else if maybe_path.starts_with("~") {
 458        if let Some(abs_path) = maybe_path
 459            .strip_prefix("~")
 460            .ok()
 461            .and_then(|maybe_path| Some(dirs::home_dir()?.join(maybe_path)))
 462        {
 463            vec![abs_path]
 464        } else {
 465            Vec::new()
 466        }
 467    } else if let Some(workspace) = workspace.upgrade(cx) {
 468        workspace.update(cx, |workspace, cx| {
 469            workspace
 470                .worktrees(cx)
 471                .map(|worktree| worktree.read(cx).abs_path().join(&maybe_path))
 472                .collect()
 473        })
 474    } else {
 475        Vec::new()
 476    };
 477
 478    potential_abs_paths
 479        .into_iter()
 480        .filter(|path| path.exists())
 481        .map(|path| PathLikeWithPosition {
 482            path_like: path,
 483            row: path_like.row,
 484            column: path_like.column,
 485        })
 486        .collect()
 487}
 488
 489pub fn regex_search_for_query(query: project::search::SearchQuery) -> Option<RegexSearch> {
 490    let query = query.as_str();
 491    let searcher = RegexSearch::new(&query);
 492    searcher.ok()
 493}
 494
 495impl View for TerminalView {
 496    fn ui_name() -> &'static str {
 497        "Terminal"
 498    }
 499
 500    fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> AnyElement<Self> {
 501        let terminal_handle = self.terminal.clone().downgrade();
 502
 503        let self_id = cx.view_id();
 504        let focused = cx
 505            .focused_view_id()
 506            .filter(|view_id| *view_id == self_id)
 507            .is_some();
 508
 509        Stack::new()
 510            .with_child(
 511                TerminalElement::new(
 512                    terminal_handle,
 513                    focused,
 514                    self.should_show_cursor(focused, cx),
 515                    self.can_navigate_to_selected_word,
 516                )
 517                .contained(),
 518            )
 519            .with_child(ChildView::new(&self.context_menu, cx))
 520            .into_any()
 521    }
 522
 523    fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
 524        self.has_new_content = false;
 525        self.terminal.read(cx).focus_in();
 526        self.blink_cursors(self.blink_epoch, cx);
 527        cx.notify();
 528    }
 529
 530    fn focus_out(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
 531        self.terminal.update(cx, |terminal, _| {
 532            terminal.focus_out();
 533        });
 534        cx.notify();
 535    }
 536
 537    fn modifiers_changed(
 538        &mut self,
 539        event: &ModifiersChangedEvent,
 540        cx: &mut ViewContext<Self>,
 541    ) -> bool {
 542        let handled = self
 543            .terminal()
 544            .update(cx, |term, _| term.try_modifiers_change(&event.modifiers));
 545        if handled {
 546            cx.notify();
 547        }
 548        handled
 549    }
 550
 551    fn key_down(&mut self, event: &KeyDownEvent, cx: &mut ViewContext<Self>) -> bool {
 552        self.clear_bel(cx);
 553        self.pause_cursor_blinking(cx);
 554
 555        self.terminal.update(cx, |term, cx| {
 556            term.try_keystroke(
 557                &event.keystroke,
 558                settings::get::<TerminalSettings>(cx).option_as_meta,
 559            )
 560        })
 561    }
 562
 563    //IME stuff
 564    fn selected_text_range(&self, cx: &AppContext) -> Option<std::ops::Range<usize>> {
 565        if self
 566            .terminal
 567            .read(cx)
 568            .last_content
 569            .mode
 570            .contains(TermMode::ALT_SCREEN)
 571        {
 572            None
 573        } else {
 574            Some(0..0)
 575        }
 576    }
 577
 578    fn replace_text_in_range(
 579        &mut self,
 580        _: Option<std::ops::Range<usize>>,
 581        text: &str,
 582        cx: &mut ViewContext<Self>,
 583    ) {
 584        self.terminal.update(cx, |terminal, _| {
 585            terminal.input(text.into());
 586        });
 587    }
 588
 589    fn update_keymap_context(&self, keymap: &mut KeymapContext, cx: &gpui::AppContext) {
 590        Self::reset_to_default_keymap_context(keymap);
 591
 592        let mode = self.terminal.read(cx).last_content.mode;
 593        keymap.add_key(
 594            "screen",
 595            if mode.contains(TermMode::ALT_SCREEN) {
 596                "alt"
 597            } else {
 598                "normal"
 599            },
 600        );
 601
 602        if mode.contains(TermMode::APP_CURSOR) {
 603            keymap.add_identifier("DECCKM");
 604        }
 605        if mode.contains(TermMode::APP_KEYPAD) {
 606            keymap.add_identifier("DECPAM");
 607        } else {
 608            keymap.add_identifier("DECPNM");
 609        }
 610        if mode.contains(TermMode::SHOW_CURSOR) {
 611            keymap.add_identifier("DECTCEM");
 612        }
 613        if mode.contains(TermMode::LINE_WRAP) {
 614            keymap.add_identifier("DECAWM");
 615        }
 616        if mode.contains(TermMode::ORIGIN) {
 617            keymap.add_identifier("DECOM");
 618        }
 619        if mode.contains(TermMode::INSERT) {
 620            keymap.add_identifier("IRM");
 621        }
 622        //LNM is apparently the name for this. https://vt100.net/docs/vt510-rm/LNM.html
 623        if mode.contains(TermMode::LINE_FEED_NEW_LINE) {
 624            keymap.add_identifier("LNM");
 625        }
 626        if mode.contains(TermMode::FOCUS_IN_OUT) {
 627            keymap.add_identifier("report_focus");
 628        }
 629        if mode.contains(TermMode::ALTERNATE_SCROLL) {
 630            keymap.add_identifier("alternate_scroll");
 631        }
 632        if mode.contains(TermMode::BRACKETED_PASTE) {
 633            keymap.add_identifier("bracketed_paste");
 634        }
 635        if mode.intersects(TermMode::MOUSE_MODE) {
 636            keymap.add_identifier("any_mouse_reporting");
 637        }
 638        {
 639            let mouse_reporting = if mode.contains(TermMode::MOUSE_REPORT_CLICK) {
 640                "click"
 641            } else if mode.contains(TermMode::MOUSE_DRAG) {
 642                "drag"
 643            } else if mode.contains(TermMode::MOUSE_MOTION) {
 644                "motion"
 645            } else {
 646                "off"
 647            };
 648            keymap.add_key("mouse_reporting", mouse_reporting);
 649        }
 650        {
 651            let format = if mode.contains(TermMode::SGR_MOUSE) {
 652                "sgr"
 653            } else if mode.contains(TermMode::UTF8_MOUSE) {
 654                "utf8"
 655            } else {
 656                "normal"
 657            };
 658            keymap.add_key("mouse_format", format);
 659        }
 660    }
 661}
 662
 663impl Item for TerminalView {
 664    fn tab_tooltip_text(&self, cx: &AppContext) -> Option<Cow<str>> {
 665        Some(self.terminal().read(cx).title().into())
 666    }
 667
 668    fn tab_content<T: 'static>(
 669        &self,
 670        _detail: Option<usize>,
 671        tab_theme: &theme::Tab,
 672        cx: &gpui::AppContext,
 673    ) -> AnyElement<T> {
 674        let title = self.terminal().read(cx).title();
 675
 676        Flex::row()
 677            .with_child(
 678                gpui::elements::Svg::new("icons/terminal.svg")
 679                    .with_color(tab_theme.label.text.color)
 680                    .constrained()
 681                    .with_width(tab_theme.type_icon_width)
 682                    .aligned()
 683                    .contained()
 684                    .with_margin_right(tab_theme.spacing),
 685            )
 686            .with_child(Label::new(title, tab_theme.label.clone()).aligned())
 687            .into_any()
 688    }
 689
 690    fn clone_on_split(
 691        &self,
 692        _workspace_id: WorkspaceId,
 693        _cx: &mut ViewContext<Self>,
 694    ) -> Option<Self> {
 695        //From what I can tell, there's no  way to tell the current working
 696        //Directory of the terminal from outside the shell. There might be
 697        //solutions to this, but they are non-trivial and require more IPC
 698
 699        // Some(TerminalContainer::new(
 700        //     Err(anyhow::anyhow!("failed to instantiate terminal")),
 701        //     workspace_id,
 702        //     cx,
 703        // ))
 704
 705        // TODO
 706        None
 707    }
 708
 709    fn is_dirty(&self, _cx: &gpui::AppContext) -> bool {
 710        self.has_bell()
 711    }
 712
 713    fn has_conflict(&self, _cx: &AppContext) -> bool {
 714        false
 715    }
 716
 717    fn as_searchable(&self, handle: &ViewHandle<Self>) -> Option<Box<dyn SearchableItemHandle>> {
 718        Some(Box::new(handle.clone()))
 719    }
 720
 721    fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
 722        match event {
 723            Event::BreadcrumbsChanged => smallvec![ItemEvent::UpdateBreadcrumbs],
 724            Event::TitleChanged | Event::Wakeup => smallvec![ItemEvent::UpdateTab],
 725            Event::CloseTerminal => smallvec![ItemEvent::CloseItem],
 726            _ => smallvec![],
 727        }
 728    }
 729
 730    fn breadcrumb_location(&self) -> ToolbarItemLocation {
 731        ToolbarItemLocation::PrimaryLeft { flex: None }
 732    }
 733
 734    fn breadcrumbs(&self, _: &theme::Theme, cx: &AppContext) -> Option<Vec<BreadcrumbText>> {
 735        Some(vec![BreadcrumbText {
 736            text: self.terminal().read(cx).breadcrumb_text.clone(),
 737            highlights: None,
 738        }])
 739    }
 740
 741    fn serialized_item_kind() -> Option<&'static str> {
 742        Some("Terminal")
 743    }
 744
 745    fn deserialize(
 746        project: ModelHandle<Project>,
 747        workspace: WeakViewHandle<Workspace>,
 748        workspace_id: workspace::WorkspaceId,
 749        item_id: workspace::ItemId,
 750        cx: &mut ViewContext<Pane>,
 751    ) -> Task<anyhow::Result<ViewHandle<Self>>> {
 752        let window = cx.window();
 753        cx.spawn(|pane, mut cx| async move {
 754            let cwd = TERMINAL_DB
 755                .get_working_directory(item_id, workspace_id)
 756                .log_err()
 757                .flatten()
 758                .or_else(|| {
 759                    cx.read(|cx| {
 760                        let strategy = settings::get::<TerminalSettings>(cx)
 761                            .working_directory
 762                            .clone();
 763                        workspace
 764                            .upgrade(cx)
 765                            .map(|workspace| {
 766                                get_working_directory(workspace.read(cx), cx, strategy)
 767                            })
 768                            .flatten()
 769                    })
 770                });
 771
 772            let terminal = project.update(&mut cx, |project, cx| {
 773                project.create_terminal(cwd, window, cx)
 774            })?;
 775            Ok(pane.update(&mut cx, |_, cx| {
 776                cx.add_view(|cx| TerminalView::new(terminal, workspace, workspace_id, cx))
 777            })?)
 778        })
 779    }
 780
 781    fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext<Self>) {
 782        cx.background()
 783            .spawn(TERMINAL_DB.update_workspace_id(
 784                workspace.database_id(),
 785                self.workspace_id,
 786                cx.view_id(),
 787            ))
 788            .detach();
 789        self.workspace_id = workspace.database_id();
 790    }
 791}
 792
 793impl SearchableItem for TerminalView {
 794    type Match = RangeInclusive<Point>;
 795
 796    fn supported_options() -> SearchOptions {
 797        SearchOptions {
 798            case: false,
 799            word: false,
 800            regex: false,
 801        }
 802    }
 803
 804    /// Convert events raised by this item into search-relevant events (if applicable)
 805    fn to_search_event(
 806        &mut self,
 807        event: &Self::Event,
 808        _: &mut ViewContext<Self>,
 809    ) -> Option<SearchEvent> {
 810        match event {
 811            Event::Wakeup => Some(SearchEvent::MatchesInvalidated),
 812            Event::SelectionsChanged => Some(SearchEvent::ActiveMatchChanged),
 813            _ => None,
 814        }
 815    }
 816
 817    /// Clear stored matches
 818    fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
 819        self.terminal().update(cx, |term, _| term.matches.clear())
 820    }
 821
 822    /// Store matches returned from find_matches somewhere for rendering
 823    fn update_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
 824        self.terminal().update(cx, |term, _| term.matches = matches)
 825    }
 826
 827    /// Return the selection content to pre-load into this search
 828    fn query_suggestion(&mut self, cx: &mut ViewContext<Self>) -> String {
 829        self.terminal()
 830            .read(cx)
 831            .last_content
 832            .selection_text
 833            .clone()
 834            .unwrap_or_default()
 835    }
 836
 837    /// Focus match at given index into the Vec of matches
 838    fn activate_match(&mut self, index: usize, _: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
 839        self.terminal()
 840            .update(cx, |term, _| term.activate_match(index));
 841        cx.notify();
 842    }
 843
 844    /// Add selections for all matches given.
 845    fn select_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
 846        self.terminal()
 847            .update(cx, |term, _| term.select_matches(matches));
 848        cx.notify();
 849    }
 850
 851    /// Get all of the matches for this query, should be done on the background
 852    fn find_matches(
 853        &mut self,
 854        query: project::search::SearchQuery,
 855        cx: &mut ViewContext<Self>,
 856    ) -> Task<Vec<Self::Match>> {
 857        if let Some(searcher) = regex_search_for_query(query) {
 858            self.terminal()
 859                .update(cx, |term, cx| term.find_matches(searcher, cx))
 860        } else {
 861            Task::ready(vec![])
 862        }
 863    }
 864
 865    /// Reports back to the search toolbar what the active match should be (the selection)
 866    fn active_match_index(
 867        &mut self,
 868        matches: Vec<Self::Match>,
 869        cx: &mut ViewContext<Self>,
 870    ) -> Option<usize> {
 871        // Selection head might have a value if there's a selection that isn't
 872        // associated with a match. Therefore, if there are no matches, we should
 873        // report None, no matter the state of the terminal
 874        let res = if matches.len() > 0 {
 875            if let Some(selection_head) = self.terminal().read(cx).selection_head {
 876                // If selection head is contained in a match. Return that match
 877                if let Some(ix) = matches
 878                    .iter()
 879                    .enumerate()
 880                    .find(|(_, search_match)| {
 881                        search_match.contains(&selection_head)
 882                            || search_match.start() > &selection_head
 883                    })
 884                    .map(|(ix, _)| ix)
 885                {
 886                    Some(ix)
 887                } else {
 888                    // If no selection after selection head, return the last match
 889                    Some(matches.len().saturating_sub(1))
 890                }
 891            } else {
 892                // Matches found but no active selection, return the first last one (closest to cursor)
 893                Some(matches.len().saturating_sub(1))
 894            }
 895        } else {
 896            None
 897        };
 898
 899        res
 900    }
 901}
 902
 903///Get's the working directory for the given workspace, respecting the user's settings.
 904pub fn get_working_directory(
 905    workspace: &Workspace,
 906    cx: &AppContext,
 907    strategy: WorkingDirectory,
 908) -> Option<PathBuf> {
 909    let res = match strategy {
 910        WorkingDirectory::CurrentProjectDirectory => current_project_directory(workspace, cx)
 911            .or_else(|| first_project_directory(workspace, cx)),
 912        WorkingDirectory::FirstProjectDirectory => first_project_directory(workspace, cx),
 913        WorkingDirectory::AlwaysHome => None,
 914        WorkingDirectory::Always { directory } => {
 915            shellexpand::full(&directory) //TODO handle this better
 916                .ok()
 917                .map(|dir| Path::new(&dir.to_string()).to_path_buf())
 918                .filter(|dir| dir.is_dir())
 919        }
 920    };
 921    res.or_else(home_dir)
 922}
 923
 924///Get's the first project's home directory, or the home directory
 925fn first_project_directory(workspace: &Workspace, cx: &AppContext) -> Option<PathBuf> {
 926    workspace
 927        .worktrees(cx)
 928        .next()
 929        .and_then(|worktree_handle| worktree_handle.read(cx).as_local())
 930        .and_then(get_path_from_wt)
 931}
 932
 933///Gets the intuitively correct working directory from the given workspace
 934///If there is an active entry for this project, returns that entry's worktree root.
 935///If there's no active entry but there is a worktree, returns that worktrees root.
 936///If either of these roots are files, or if there are any other query failures,
 937///  returns the user's home directory
 938fn current_project_directory(workspace: &Workspace, cx: &AppContext) -> Option<PathBuf> {
 939    let project = workspace.project().read(cx);
 940
 941    project
 942        .active_entry()
 943        .and_then(|entry_id| project.worktree_for_entry(entry_id, cx))
 944        .or_else(|| workspace.worktrees(cx).next())
 945        .and_then(|worktree_handle| worktree_handle.read(cx).as_local())
 946        .and_then(get_path_from_wt)
 947}
 948
 949fn get_path_from_wt(wt: &LocalWorktree) -> Option<PathBuf> {
 950    wt.root_entry()
 951        .filter(|re| re.is_dir())
 952        .map(|_| wt.abs_path().to_path_buf())
 953}
 954
 955#[cfg(test)]
 956mod tests {
 957    use super::*;
 958    use gpui::TestAppContext;
 959    use project::{Entry, Project, ProjectPath, Worktree};
 960    use std::path::Path;
 961    use workspace::AppState;
 962
 963    // Working directory calculation tests
 964
 965    // No Worktrees in project -> home_dir()
 966    #[gpui::test]
 967    async fn no_worktree(cx: &mut TestAppContext) {
 968        let (project, workspace) = init_test(cx).await;
 969        cx.read(|cx| {
 970            let workspace = workspace.read(cx);
 971            let active_entry = project.read(cx).active_entry();
 972
 973            //Make sure environment is as expected
 974            assert!(active_entry.is_none());
 975            assert!(workspace.worktrees(cx).next().is_none());
 976
 977            let res = current_project_directory(workspace, cx);
 978            assert_eq!(res, None);
 979            let res = first_project_directory(workspace, cx);
 980            assert_eq!(res, None);
 981        });
 982    }
 983
 984    // No active entry, but a worktree, worktree is a file -> home_dir()
 985    #[gpui::test]
 986    async fn no_active_entry_worktree_is_file(cx: &mut TestAppContext) {
 987        let (project, workspace) = init_test(cx).await;
 988
 989        create_file_wt(project.clone(), "/root.txt", cx).await;
 990        cx.read(|cx| {
 991            let workspace = workspace.read(cx);
 992            let active_entry = project.read(cx).active_entry();
 993
 994            //Make sure environment is as expected
 995            assert!(active_entry.is_none());
 996            assert!(workspace.worktrees(cx).next().is_some());
 997
 998            let res = current_project_directory(workspace, cx);
 999            assert_eq!(res, None);
1000            let res = first_project_directory(workspace, cx);
1001            assert_eq!(res, None);
1002        });
1003    }
1004
1005    // No active entry, but a worktree, worktree is a folder -> worktree_folder
1006    #[gpui::test]
1007    async fn no_active_entry_worktree_is_dir(cx: &mut TestAppContext) {
1008        let (project, workspace) = init_test(cx).await;
1009
1010        let (_wt, _entry) = create_folder_wt(project.clone(), "/root/", cx).await;
1011        cx.update(|cx| {
1012            let workspace = workspace.read(cx);
1013            let active_entry = project.read(cx).active_entry();
1014
1015            assert!(active_entry.is_none());
1016            assert!(workspace.worktrees(cx).next().is_some());
1017
1018            let res = current_project_directory(workspace, cx);
1019            assert_eq!(res, Some((Path::new("/root/")).to_path_buf()));
1020            let res = first_project_directory(workspace, cx);
1021            assert_eq!(res, Some((Path::new("/root/")).to_path_buf()));
1022        });
1023    }
1024
1025    // Active entry with a work tree, worktree is a file -> home_dir()
1026    #[gpui::test]
1027    async fn active_entry_worktree_is_file(cx: &mut TestAppContext) {
1028        let (project, workspace) = init_test(cx).await;
1029
1030        let (_wt, _entry) = create_folder_wt(project.clone(), "/root1/", cx).await;
1031        let (wt2, entry2) = create_file_wt(project.clone(), "/root2.txt", cx).await;
1032        insert_active_entry_for(wt2, entry2, project.clone(), cx);
1033
1034        cx.update(|cx| {
1035            let workspace = workspace.read(cx);
1036            let active_entry = project.read(cx).active_entry();
1037
1038            assert!(active_entry.is_some());
1039
1040            let res = current_project_directory(workspace, cx);
1041            assert_eq!(res, None);
1042            let res = first_project_directory(workspace, cx);
1043            assert_eq!(res, Some((Path::new("/root1/")).to_path_buf()));
1044        });
1045    }
1046
1047    // Active entry, with a worktree, worktree is a folder -> worktree_folder
1048    #[gpui::test]
1049    async fn active_entry_worktree_is_dir(cx: &mut TestAppContext) {
1050        let (project, workspace) = init_test(cx).await;
1051
1052        let (_wt, _entry) = create_folder_wt(project.clone(), "/root1/", cx).await;
1053        let (wt2, entry2) = create_folder_wt(project.clone(), "/root2/", cx).await;
1054        insert_active_entry_for(wt2, entry2, project.clone(), cx);
1055
1056        cx.update(|cx| {
1057            let workspace = workspace.read(cx);
1058            let active_entry = project.read(cx).active_entry();
1059
1060            assert!(active_entry.is_some());
1061
1062            let res = current_project_directory(workspace, cx);
1063            assert_eq!(res, Some((Path::new("/root2/")).to_path_buf()));
1064            let res = first_project_directory(workspace, cx);
1065            assert_eq!(res, Some((Path::new("/root1/")).to_path_buf()));
1066        });
1067    }
1068
1069    /// Creates a worktree with 1 file: /root.txt
1070    pub async fn init_test(
1071        cx: &mut TestAppContext,
1072    ) -> (ModelHandle<Project>, ViewHandle<Workspace>) {
1073        let params = cx.update(AppState::test);
1074        cx.update(|cx| {
1075            theme::init((), cx);
1076            Project::init_settings(cx);
1077            language::init(cx);
1078        });
1079
1080        let project = Project::test(params.fs.clone(), [], cx).await;
1081        let workspace = cx
1082            .add_window(|cx| Workspace::test_new(project.clone(), cx))
1083            .root(cx);
1084
1085        (project, workspace)
1086    }
1087
1088    /// Creates a worktree with 1 folder: /root{suffix}/
1089    async fn create_folder_wt(
1090        project: ModelHandle<Project>,
1091        path: impl AsRef<Path>,
1092        cx: &mut TestAppContext,
1093    ) -> (ModelHandle<Worktree>, Entry) {
1094        create_wt(project, true, path, cx).await
1095    }
1096
1097    /// Creates a worktree with 1 file: /root{suffix}.txt
1098    async fn create_file_wt(
1099        project: ModelHandle<Project>,
1100        path: impl AsRef<Path>,
1101        cx: &mut TestAppContext,
1102    ) -> (ModelHandle<Worktree>, Entry) {
1103        create_wt(project, false, path, cx).await
1104    }
1105
1106    async fn create_wt(
1107        project: ModelHandle<Project>,
1108        is_dir: bool,
1109        path: impl AsRef<Path>,
1110        cx: &mut TestAppContext,
1111    ) -> (ModelHandle<Worktree>, Entry) {
1112        let (wt, _) = project
1113            .update(cx, |project, cx| {
1114                project.find_or_create_local_worktree(path, true, cx)
1115            })
1116            .await
1117            .unwrap();
1118
1119        let entry = cx
1120            .update(|cx| {
1121                wt.update(cx, |wt, cx| {
1122                    wt.as_local()
1123                        .unwrap()
1124                        .create_entry(Path::new(""), is_dir, cx)
1125                })
1126            })
1127            .await
1128            .unwrap();
1129
1130        (wt, entry)
1131    }
1132
1133    pub fn insert_active_entry_for(
1134        wt: ModelHandle<Worktree>,
1135        entry: Entry,
1136        project: ModelHandle<Project>,
1137        cx: &mut TestAppContext,
1138    ) {
1139        cx.update(|cx| {
1140            let p = ProjectPath {
1141                worktree_id: wt.read(cx).id(),
1142                path: entry.path,
1143            };
1144            project.update(cx, |project, cx| project.set_active_path(Some(p), cx));
1145        });
1146    }
1147}