Remove terminal container view, switch to notify errors

Mikayla Maki created

Change summary

crates/collab/src/integration_tests.rs              |   2 
crates/collab_ui/src/collab_ui.rs                   |   2 
crates/terminal_view/src/terminal_container_view.rs | 771 ---------------
crates/terminal_view/src/terminal_view.rs           | 620 +++++++++++
crates/workspace/src/dock.rs                        |  23 
crates/workspace/src/notifications.rs               |  80 +
crates/workspace/src/workspace.rs                   |  12 
crates/zed/src/main.rs                              |  31 
8 files changed, 700 insertions(+), 841 deletions(-)

Detailed changes

crates/collab/src/integration_tests.rs 🔗

@@ -6022,7 +6022,7 @@ impl TestServer {
             fs: fs.clone(),
             build_window_options: Default::default,
             initialize_workspace: |_, _, _| unimplemented!(),
-            default_item_factory: |_, _| unimplemented!(),
+            dock_default_item_factory: |_, _| unimplemented!(),
         });
 
         Project::init(&client);

crates/collab_ui/src/collab_ui.rs 🔗

@@ -54,7 +54,7 @@ pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
                         Default::default(),
                         0,
                         project,
-                        app_state.default_item_factory,
+                        app_state.dock_default_item_factory,
                         cx,
                     );
                     (app_state.initialize_workspace)(&mut workspace, &app_state, cx);

crates/terminal_view/src/terminal_container_view.rs 🔗

@@ -1,771 +0,0 @@
-use crate::persistence::TERMINAL_DB;
-use crate::TerminalView;
-use terminal::alacritty_terminal::index::Point;
-use terminal::{Event, Terminal, TerminalError};
-
-use crate::regex_search_for_query;
-use dirs::home_dir;
-use gpui::{
-    actions, elements::*, AnyViewHandle, AppContext, Entity, ModelHandle, MutableAppContext, Task,
-    View, ViewContext, ViewHandle, WeakViewHandle,
-};
-use util::{truncate_and_trailoff, ResultExt};
-use workspace::searchable::{SearchEvent, SearchOptions, SearchableItem, SearchableItemHandle};
-use workspace::{
-    item::{Item, ItemEvent},
-    ToolbarItemLocation, Workspace,
-};
-use workspace::{register_deserializable_item, Pane, WorkspaceId};
-
-use project::{LocalWorktree, Project, ProjectPath};
-use settings::{Settings, WorkingDirectory};
-use smallvec::SmallVec;
-use std::ops::RangeInclusive;
-use std::path::{Path, PathBuf};
-
-use crate::terminal_element::TerminalElement;
-
-actions!(terminal, [DeployModal]);
-
-pub fn init(cx: &mut MutableAppContext) {
-    cx.add_action(TerminalContainer::deploy);
-
-    register_deserializable_item::<TerminalContainer>(cx);
-
-    // terminal_view::init(cx);
-}
-
-//Make terminal view an enum, that can give you views for the error and non-error states
-//Take away all the result unwrapping in the current TerminalView by making it 'infallible'
-//Bubble up to deploy(_modal)() calls
-
-pub enum TerminalContainerContent {
-    Connected(ViewHandle<TerminalView>),
-    Error(ViewHandle<ErrorView>),
-}
-
-impl TerminalContainerContent {
-    fn handle(&self) -> AnyViewHandle {
-        match self {
-            Self::Connected(handle) => handle.into(),
-            Self::Error(handle) => handle.into(),
-        }
-    }
-}
-
-pub struct TerminalContainer {
-    pub content: TerminalContainerContent,
-    associated_directory: Option<PathBuf>,
-}
-
-pub struct ErrorView {
-    error: TerminalError,
-}
-
-impl Entity for TerminalContainer {
-    type Event = Event;
-}
-
-impl Entity for ErrorView {
-    type Event = Event;
-}
-
-impl TerminalContainer {
-    ///Create a new Terminal in the current working directory or the user's home directory
-    pub fn deploy(
-        workspace: &mut Workspace,
-        _: &workspace::NewTerminal,
-        cx: &mut ViewContext<Workspace>,
-    ) {
-        let strategy = cx.global::<Settings>().terminal_strategy();
-
-        let working_directory = get_working_directory(workspace, cx, strategy);
-
-        let window_id = cx.window_id();
-        let project = workspace.project().clone();
-        let terminal = workspace.project().update(cx, |project, cx| {
-            project.create_terminal(working_directory, window_id, cx)
-        });
-
-        let view = cx.add_view(|cx| TerminalContainer::new(terminal, workspace.database_id(), cx));
-        workspace.add_item(Box::new(view), cx);
-    }
-
-    ///Create a new Terminal view.
-    pub fn new(
-        maybe_terminal: anyhow::Result<ModelHandle<Terminal>>,
-        workspace_id: WorkspaceId,
-        cx: &mut ViewContext<Self>,
-    ) -> Self {
-        let content = match maybe_terminal {
-            Ok(terminal) => {
-                let item_id = cx.view_id();
-                let view = cx.add_view(|cx| {
-                    TerminalView::from_terminal(terminal, false, workspace_id, item_id, cx)
-                });
-                cx.subscribe(&view, |_this, _content, event, cx| cx.emit(*event))
-                    .detach();
-                TerminalContainerContent::Connected(view)
-            }
-            Err(error) => {
-                let view = cx.add_view(|_| ErrorView {
-                    error: error.downcast::<TerminalError>().unwrap(),
-                });
-                TerminalContainerContent::Error(view)
-            }
-        };
-
-        TerminalContainer {
-            content,
-            associated_directory: None, //working_directory,
-        }
-    }
-
-    fn connected(&self) -> Option<ViewHandle<TerminalView>> {
-        match &self.content {
-            TerminalContainerContent::Connected(vh) => Some(vh.clone()),
-            TerminalContainerContent::Error(_) => None,
-        }
-    }
-}
-
-impl View for TerminalContainer {
-    fn ui_name() -> &'static str {
-        "Terminal"
-    }
-
-    fn render(&mut self, cx: &mut gpui::RenderContext<'_, Self>) -> ElementBox {
-        match &self.content {
-            TerminalContainerContent::Connected(connected) => ChildView::new(connected, cx),
-            TerminalContainerContent::Error(error) => ChildView::new(error, cx),
-        }
-        .boxed()
-    }
-
-    fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
-        if cx.is_self_focused() {
-            cx.focus(self.content.handle());
-        }
-    }
-}
-
-impl View for ErrorView {
-    fn ui_name() -> &'static str {
-        "Terminal Error"
-    }
-
-    fn render(&mut self, cx: &mut gpui::RenderContext<'_, Self>) -> ElementBox {
-        let settings = cx.global::<Settings>();
-        let style = TerminalElement::make_text_style(cx.font_cache(), settings);
-
-        //TODO:
-        //We want markdown style highlighting so we can format the program and working directory with ``
-        //We want a max-width of 75% with word-wrap
-        //We want to be able to select the text
-        //Want to be able to scroll if the error message is massive somehow (resiliency)
-
-        let program_text = format!("Shell Program: `{}`", self.error.shell_to_string());
-
-        let directory_text = {
-            match self.error.directory.as_ref() {
-                Some(path) => format!("Working directory: `{}`", path.to_string_lossy()),
-                None => "No working directory specified".to_string(),
-            }
-        };
-
-        let error_text = self.error.source.to_string();
-
-        Flex::column()
-            .with_child(
-                Text::new("Failed to open the terminal.".to_string(), style.clone())
-                    .contained()
-                    .boxed(),
-            )
-            .with_child(Text::new(program_text, style.clone()).contained().boxed())
-            .with_child(Text::new(directory_text, style.clone()).contained().boxed())
-            .with_child(Text::new(error_text, style).contained().boxed())
-            .aligned()
-            .boxed()
-    }
-}
-
-impl Item for TerminalContainer {
-    fn tab_content(
-        &self,
-        _detail: Option<usize>,
-        tab_theme: &theme::Tab,
-        cx: &gpui::AppContext,
-    ) -> ElementBox {
-        let title = match &self.content {
-            TerminalContainerContent::Connected(connected) => connected
-                .read(cx)
-                .handle()
-                .read(cx)
-                .foreground_process_info
-                .as_ref()
-                .map(|fpi| {
-                    format!(
-                        "{} — {}",
-                        truncate_and_trailoff(
-                            &fpi.cwd
-                                .file_name()
-                                .map(|name| name.to_string_lossy().to_string())
-                                .unwrap_or_default(),
-                            25
-                        ),
-                        truncate_and_trailoff(
-                            &{
-                                format!(
-                                    "{}{}",
-                                    fpi.name,
-                                    if fpi.argv.len() >= 1 {
-                                        format!(" {}", (&fpi.argv[1..]).join(" "))
-                                    } else {
-                                        "".to_string()
-                                    }
-                                )
-                            },
-                            25
-                        )
-                    )
-                })
-                .unwrap_or_else(|| "Terminal".to_string()),
-            TerminalContainerContent::Error(_) => "Terminal".to_string(),
-        };
-
-        Flex::row()
-            .with_child(
-                Label::new(title, tab_theme.label.clone())
-                    .aligned()
-                    .contained()
-                    .boxed(),
-            )
-            .boxed()
-    }
-
-    fn clone_on_split(
-        &self,
-        workspace_id: WorkspaceId,
-        cx: &mut ViewContext<Self>,
-    ) -> Option<Self> {
-        //From what I can tell, there's no  way to tell the current working
-        //Directory of the terminal from outside the shell. There might be
-        //solutions to this, but they are non-trivial and require more IPC
-        Some(TerminalContainer::new(
-            Err(anyhow::anyhow!("failed to instantiate terminal")),
-            workspace_id,
-            cx,
-        ))
-    }
-
-    fn project_path(&self, _cx: &gpui::AppContext) -> Option<ProjectPath> {
-        None
-    }
-
-    fn project_entry_ids(&self, _cx: &gpui::AppContext) -> SmallVec<[project::ProjectEntryId; 3]> {
-        SmallVec::new()
-    }
-
-    fn is_singleton(&self, _cx: &gpui::AppContext) -> bool {
-        false
-    }
-
-    fn set_nav_history(&mut self, _: workspace::ItemNavHistory, _: &mut ViewContext<Self>) {}
-
-    fn can_save(&self, _cx: &gpui::AppContext) -> bool {
-        false
-    }
-
-    fn save(
-        &mut self,
-        _project: gpui::ModelHandle<Project>,
-        _cx: &mut ViewContext<Self>,
-    ) -> gpui::Task<gpui::anyhow::Result<()>> {
-        unreachable!("save should not have been called");
-    }
-
-    fn save_as(
-        &mut self,
-        _project: gpui::ModelHandle<Project>,
-        _abs_path: std::path::PathBuf,
-        _cx: &mut ViewContext<Self>,
-    ) -> gpui::Task<gpui::anyhow::Result<()>> {
-        unreachable!("save_as should not have been called");
-    }
-
-    fn reload(
-        &mut self,
-        _project: gpui::ModelHandle<Project>,
-        _cx: &mut ViewContext<Self>,
-    ) -> gpui::Task<gpui::anyhow::Result<()>> {
-        gpui::Task::ready(Ok(()))
-    }
-
-    fn is_dirty(&self, cx: &gpui::AppContext) -> bool {
-        if let TerminalContainerContent::Connected(connected) = &self.content {
-            connected.read(cx).has_bell()
-        } else {
-            false
-        }
-    }
-
-    fn has_conflict(&self, _cx: &AppContext) -> bool {
-        false
-    }
-
-    fn as_searchable(&self, handle: &ViewHandle<Self>) -> Option<Box<dyn SearchableItemHandle>> {
-        Some(Box::new(handle.clone()))
-    }
-
-    fn to_item_events(event: &Self::Event) -> Vec<ItemEvent> {
-        match event {
-            Event::BreadcrumbsChanged => vec![ItemEvent::UpdateBreadcrumbs],
-            Event::TitleChanged | Event::Wakeup => vec![ItemEvent::UpdateTab],
-            Event::CloseTerminal => vec![ItemEvent::CloseItem],
-            _ => vec![],
-        }
-    }
-
-    fn breadcrumb_location(&self) -> ToolbarItemLocation {
-        if self.connected().is_some() {
-            ToolbarItemLocation::PrimaryLeft { flex: None }
-        } else {
-            ToolbarItemLocation::Hidden
-        }
-    }
-
-    fn breadcrumbs(&self, theme: &theme::Theme, cx: &AppContext) -> Option<Vec<ElementBox>> {
-        let connected = self.connected()?;
-
-        Some(vec![Text::new(
-            connected
-                .read(cx)
-                .terminal()
-                .read(cx)
-                .breadcrumb_text
-                .to_string(),
-            theme.breadcrumbs.text.clone(),
-        )
-        .boxed()])
-    }
-
-    fn serialized_item_kind() -> Option<&'static str> {
-        Some("Terminal")
-    }
-
-    fn deserialize(
-        project: ModelHandle<Project>,
-        _workspace: WeakViewHandle<Workspace>,
-        workspace_id: workspace::WorkspaceId,
-        item_id: workspace::ItemId,
-        cx: &mut ViewContext<Pane>,
-    ) -> Task<anyhow::Result<ViewHandle<Self>>> {
-        let window_id = cx.window_id();
-        cx.spawn(|pane, mut cx| async move {
-            let cwd = TERMINAL_DB
-                .take_working_directory(item_id, workspace_id)
-                .await
-                .log_err()
-                .flatten();
-
-            cx.update(|cx| {
-                let terminal = project.update(cx, |project, cx| {
-                    project.create_terminal(cwd, window_id, cx)
-                });
-
-                Ok(cx.add_view(pane, |cx| {
-                    TerminalContainer::new(terminal, workspace_id, cx)
-                }))
-            })
-        })
-    }
-
-    fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext<Self>) {
-        if let Some(connected) = self.connected() {
-            connected.update(cx, |connected_view, cx| {
-                connected_view.added_to_workspace(workspace.database_id(), cx);
-            })
-        }
-    }
-}
-
-impl SearchableItem for TerminalContainer {
-    type Match = RangeInclusive<Point>;
-
-    fn supported_options() -> SearchOptions {
-        SearchOptions {
-            case: false,
-            word: false,
-            regex: false,
-        }
-    }
-
-    /// Convert events raised by this item into search-relevant events (if applicable)
-    fn to_search_event(event: &Self::Event) -> Option<SearchEvent> {
-        match event {
-            Event::Wakeup => Some(SearchEvent::MatchesInvalidated),
-            Event::SelectionsChanged => Some(SearchEvent::ActiveMatchChanged),
-            _ => None,
-        }
-    }
-
-    /// Clear stored matches
-    fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
-        if let TerminalContainerContent::Connected(connected) = &self.content {
-            let terminal = connected.read(cx).terminal().clone();
-            terminal.update(cx, |term, _| term.matches.clear())
-        }
-    }
-
-    /// Store matches returned from find_matches somewhere for rendering
-    fn update_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
-        if let TerminalContainerContent::Connected(connected) = &self.content {
-            let terminal = connected.read(cx).terminal().clone();
-            terminal.update(cx, |term, _| term.matches = matches)
-        }
-    }
-
-    /// Return the selection content to pre-load into this search
-    fn query_suggestion(&mut self, cx: &mut ViewContext<Self>) -> String {
-        if let TerminalContainerContent::Connected(connected) = &self.content {
-            let terminal = connected.read(cx).terminal().clone();
-            terminal
-                .read(cx)
-                .last_content
-                .selection_text
-                .clone()
-                .unwrap_or_default()
-        } else {
-            Default::default()
-        }
-    }
-
-    /// Focus match at given index into the Vec of matches
-    fn activate_match(&mut self, index: usize, _: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
-        if let TerminalContainerContent::Connected(connected) = &self.content {
-            let terminal = connected.read(cx).terminal().clone();
-            terminal.update(cx, |term, _| term.activate_match(index));
-            cx.notify();
-        }
-    }
-
-    /// Get all of the matches for this query, should be done on the background
-    fn find_matches(
-        &mut self,
-        query: project::search::SearchQuery,
-        cx: &mut ViewContext<Self>,
-    ) -> Task<Vec<Self::Match>> {
-        if let TerminalContainerContent::Connected(connected) = &self.content {
-            let terminal = connected.read(cx).terminal().clone();
-            if let Some(searcher) = regex_search_for_query(query) {
-                terminal.update(cx, |term, cx| term.find_matches(searcher, cx))
-            } else {
-                cx.background().spawn(async { Vec::new() })
-            }
-        } else {
-            Task::ready(Vec::new())
-        }
-    }
-
-    /// Reports back to the search toolbar what the active match should be (the selection)
-    fn active_match_index(
-        &mut self,
-        matches: Vec<Self::Match>,
-        cx: &mut ViewContext<Self>,
-    ) -> Option<usize> {
-        let connected = self.connected();
-        // Selection head might have a value if there's a selection that isn't
-        // associated with a match. Therefore, if there are no matches, we should
-        // report None, no matter the state of the terminal
-        let res = if matches.len() > 0 && connected.is_some() {
-            if let Some(selection_head) = connected
-                .unwrap()
-                .read(cx)
-                .terminal()
-                .read(cx)
-                .selection_head
-            {
-                // If selection head is contained in a match. Return that match
-                if let Some(ix) = matches
-                    .iter()
-                    .enumerate()
-                    .find(|(_, search_match)| {
-                        search_match.contains(&selection_head)
-                            || search_match.start() > &selection_head
-                    })
-                    .map(|(ix, _)| ix)
-                {
-                    Some(ix)
-                } else {
-                    // If no selection after selection head, return the last match
-                    Some(matches.len().saturating_sub(1))
-                }
-            } else {
-                // Matches found but no active selection, return the first last one (closest to cursor)
-                Some(matches.len().saturating_sub(1))
-            }
-        } else {
-            None
-        };
-
-        res
-    }
-}
-
-///Get's the working directory for the given workspace, respecting the user's settings.
-pub fn get_working_directory(
-    workspace: &Workspace,
-    cx: &AppContext,
-    strategy: WorkingDirectory,
-) -> Option<PathBuf> {
-    let res = match strategy {
-        WorkingDirectory::CurrentProjectDirectory => current_project_directory(workspace, cx)
-            .or_else(|| first_project_directory(workspace, cx)),
-        WorkingDirectory::FirstProjectDirectory => first_project_directory(workspace, cx),
-        WorkingDirectory::AlwaysHome => None,
-        WorkingDirectory::Always { directory } => {
-            shellexpand::full(&directory) //TODO handle this better
-                .ok()
-                .map(|dir| Path::new(&dir.to_string()).to_path_buf())
-                .filter(|dir| dir.is_dir())
-        }
-    };
-    res.or_else(home_dir)
-}
-
-///Get's the first project's home directory, or the home directory
-fn first_project_directory(workspace: &Workspace, cx: &AppContext) -> Option<PathBuf> {
-    workspace
-        .worktrees(cx)
-        .next()
-        .and_then(|worktree_handle| worktree_handle.read(cx).as_local())
-        .and_then(get_path_from_wt)
-}
-
-///Gets the intuitively correct working directory from the given workspace
-///If there is an active entry for this project, returns that entry's worktree root.
-///If there's no active entry but there is a worktree, returns that worktrees root.
-///If either of these roots are files, or if there are any other query failures,
-///  returns the user's home directory
-fn current_project_directory(workspace: &Workspace, cx: &AppContext) -> Option<PathBuf> {
-    let project = workspace.project().read(cx);
-
-    project
-        .active_entry()
-        .and_then(|entry_id| project.worktree_for_entry(entry_id, cx))
-        .or_else(|| workspace.worktrees(cx).next())
-        .and_then(|worktree_handle| worktree_handle.read(cx).as_local())
-        .and_then(get_path_from_wt)
-}
-
-fn get_path_from_wt(wt: &LocalWorktree) -> Option<PathBuf> {
-    wt.root_entry()
-        .filter(|re| re.is_dir())
-        .map(|_| wt.abs_path().to_path_buf())
-}
-
-#[cfg(test)]
-mod tests {
-
-    use super::*;
-    use gpui::TestAppContext;
-    use project::{Entry, Worktree};
-    use workspace::AppState;
-
-    use std::path::Path;
-
-    ///Working directory calculation tests
-
-    ///No Worktrees in project -> home_dir()
-    #[gpui::test]
-    async fn no_worktree(cx: &mut TestAppContext) {
-        //Setup variables
-        let (project, workspace) = blank_workspace(cx).await;
-        //Test
-        cx.read(|cx| {
-            let workspace = workspace.read(cx);
-            let active_entry = project.read(cx).active_entry();
-
-            //Make sure enviroment is as expeted
-            assert!(active_entry.is_none());
-            assert!(workspace.worktrees(cx).next().is_none());
-
-            let res = current_project_directory(workspace, cx);
-            assert_eq!(res, None);
-            let res = first_project_directory(workspace, cx);
-            assert_eq!(res, None);
-        });
-    }
-
-    ///No active entry, but a worktree, worktree is a file -> home_dir()
-    #[gpui::test]
-    async fn no_active_entry_worktree_is_file(cx: &mut TestAppContext) {
-        //Setup variables
-
-        let (project, workspace) = blank_workspace(cx).await;
-        create_file_wt(project.clone(), "/root.txt", cx).await;
-
-        cx.read(|cx| {
-            let workspace = workspace.read(cx);
-            let active_entry = project.read(cx).active_entry();
-
-            //Make sure enviroment is as expeted
-            assert!(active_entry.is_none());
-            assert!(workspace.worktrees(cx).next().is_some());
-
-            let res = current_project_directory(workspace, cx);
-            assert_eq!(res, None);
-            let res = first_project_directory(workspace, cx);
-            assert_eq!(res, None);
-        });
-    }
-
-    //No active entry, but a worktree, worktree is a folder -> worktree_folder
-    #[gpui::test]
-    async fn no_active_entry_worktree_is_dir(cx: &mut TestAppContext) {
-        //Setup variables
-        let (project, workspace) = blank_workspace(cx).await;
-        let (_wt, _entry) = create_folder_wt(project.clone(), "/root/", cx).await;
-
-        //Test
-        cx.update(|cx| {
-            let workspace = workspace.read(cx);
-            let active_entry = project.read(cx).active_entry();
-
-            assert!(active_entry.is_none());
-            assert!(workspace.worktrees(cx).next().is_some());
-
-            let res = current_project_directory(workspace, cx);
-            assert_eq!(res, Some((Path::new("/root/")).to_path_buf()));
-            let res = first_project_directory(workspace, cx);
-            assert_eq!(res, Some((Path::new("/root/")).to_path_buf()));
-        });
-    }
-
-    //Active entry with a work tree, worktree is a file -> home_dir()
-    #[gpui::test]
-    async fn active_entry_worktree_is_file(cx: &mut TestAppContext) {
-        //Setup variables
-
-        let (project, workspace) = blank_workspace(cx).await;
-        let (_wt, _entry) = create_folder_wt(project.clone(), "/root1/", cx).await;
-        let (wt2, entry2) = create_file_wt(project.clone(), "/root2.txt", cx).await;
-        insert_active_entry_for(wt2, entry2, project.clone(), cx);
-
-        //Test
-        cx.update(|cx| {
-            let workspace = workspace.read(cx);
-            let active_entry = project.read(cx).active_entry();
-
-            assert!(active_entry.is_some());
-
-            let res = current_project_directory(workspace, cx);
-            assert_eq!(res, None);
-            let res = first_project_directory(workspace, cx);
-            assert_eq!(res, Some((Path::new("/root1/")).to_path_buf()));
-        });
-    }
-
-    //Active entry, with a worktree, worktree is a folder -> worktree_folder
-    #[gpui::test]
-    async fn active_entry_worktree_is_dir(cx: &mut TestAppContext) {
-        //Setup variables
-        let (project, workspace) = blank_workspace(cx).await;
-        let (_wt, _entry) = create_folder_wt(project.clone(), "/root1/", cx).await;
-        let (wt2, entry2) = create_folder_wt(project.clone(), "/root2/", cx).await;
-        insert_active_entry_for(wt2, entry2, project.clone(), cx);
-
-        //Test
-        cx.update(|cx| {
-            let workspace = workspace.read(cx);
-            let active_entry = project.read(cx).active_entry();
-
-            assert!(active_entry.is_some());
-
-            let res = current_project_directory(workspace, cx);
-            assert_eq!(res, Some((Path::new("/root2/")).to_path_buf()));
-            let res = first_project_directory(workspace, cx);
-            assert_eq!(res, Some((Path::new("/root1/")).to_path_buf()));
-        });
-    }
-
-    ///Creates a worktree with 1 file: /root.txt
-    pub async fn blank_workspace(
-        cx: &mut TestAppContext,
-    ) -> (ModelHandle<Project>, ViewHandle<Workspace>) {
-        let params = cx.update(AppState::test);
-
-        let project = Project::test(params.fs.clone(), [], cx).await;
-        let (_, workspace) = cx.add_window(|cx| {
-            Workspace::new(
-                Default::default(),
-                0,
-                project.clone(),
-                |_, _| unimplemented!(),
-                cx,
-            )
-        });
-
-        (project, workspace)
-    }
-
-    ///Creates a worktree with 1 folder: /root{suffix}/
-    async fn create_folder_wt(
-        project: ModelHandle<Project>,
-        path: impl AsRef<Path>,
-        cx: &mut TestAppContext,
-    ) -> (ModelHandle<Worktree>, Entry) {
-        create_wt(project, true, path, cx).await
-    }
-
-    ///Creates a worktree with 1 file: /root{suffix}.txt
-    async fn create_file_wt(
-        project: ModelHandle<Project>,
-        path: impl AsRef<Path>,
-        cx: &mut TestAppContext,
-    ) -> (ModelHandle<Worktree>, Entry) {
-        create_wt(project, false, path, cx).await
-    }
-
-    async fn create_wt(
-        project: ModelHandle<Project>,
-        is_dir: bool,
-        path: impl AsRef<Path>,
-        cx: &mut TestAppContext,
-    ) -> (ModelHandle<Worktree>, Entry) {
-        let (wt, _) = project
-            .update(cx, |project, cx| {
-                project.find_or_create_local_worktree(path, true, cx)
-            })
-            .await
-            .unwrap();
-
-        let entry = cx
-            .update(|cx| {
-                wt.update(cx, |wt, cx| {
-                    wt.as_local()
-                        .unwrap()
-                        .create_entry(Path::new(""), is_dir, cx)
-                })
-            })
-            .await
-            .unwrap();
-
-        (wt, entry)
-    }
-
-    pub fn insert_active_entry_for(
-        wt: ModelHandle<Worktree>,
-        entry: Entry,
-        project: ModelHandle<Project>,
-        cx: &mut TestAppContext,
-    ) {
-        cx.update(|cx| {
-            let p = ProjectPath {
-                worktree_id: wt.read(cx).id(),
-                path: entry.path,
-            };
-            project.update(cx, |project, cx| project.set_active_path(Some(p), cx));
-        });
-    }
-}

crates/terminal_view/src/terminal_view.rs 🔗

@@ -1,21 +1,27 @@
 mod persistence;
-pub mod terminal_container_view;
 pub mod terminal_element;
 
-use std::{ops::RangeInclusive, time::Duration};
+use std::{
+    ops::RangeInclusive,
+    path::{Path, PathBuf},
+    time::Duration,
+};
 
 use context_menu::{ContextMenu, ContextMenuItem};
+use dirs::home_dir;
 use gpui::{
     actions,
-    elements::{AnchorCorner, ChildView, ParentElement, Stack},
+    elements::{AnchorCorner, ChildView, Flex, Label, ParentElement, Stack, Text},
     geometry::vector::Vector2F,
     impl_actions, impl_internal_actions,
     keymap::Keystroke,
     AnyViewHandle, AppContext, Element, ElementBox, Entity, ModelHandle, MutableAppContext, Task,
-    View, ViewContext, ViewHandle,
+    View, ViewContext, ViewHandle, WeakViewHandle,
 };
+use project::{LocalWorktree, Project, ProjectPath};
 use serde::Deserialize;
-use settings::{Settings, TerminalBlink};
+use settings::{Settings, TerminalBlink, WorkingDirectory};
+use smallvec::SmallVec;
 use smol::Timer;
 use terminal::{
     alacritty_terminal::{
@@ -24,8 +30,14 @@ use terminal::{
     },
     Event, Terminal,
 };
-use util::ResultExt;
-use workspace::{pane, ItemId, WorkspaceId};
+use util::{truncate_and_trailoff, ResultExt};
+use workspace::{
+    item::{Item, ItemEvent},
+    notifications::NotifyResultExt,
+    pane, register_deserializable_item,
+    searchable::{SearchEvent, SearchOptions, SearchableItem, SearchableItemHandle},
+    Pane, ToolbarItemLocation, Workspace, WorkspaceId,
+};
 
 use crate::{persistence::TERMINAL_DB, terminal_element::TerminalElement};
 
@@ -56,7 +68,10 @@ impl_actions!(terminal, [SendText, SendKeystroke]);
 impl_internal_actions!(project_panel, [DeployContextMenu]);
 
 pub fn init(cx: &mut MutableAppContext) {
-    terminal_container_view::init(cx);
+    cx.add_action(TerminalView::deploy);
+
+    register_deserializable_item::<TerminalView>(cx);
+
     //Useful terminal views
     cx.add_action(TerminalView::send_text);
     cx.add_action(TerminalView::send_keystroke);
@@ -73,15 +88,12 @@ pub struct TerminalView {
     has_new_content: bool,
     //Currently using iTerm bell, show bell emoji in tab until input is received
     has_bell: bool,
-    // Only for styling purposes. Doesn't effect behavior
-    modal: bool,
     context_menu: ViewHandle<ContextMenu>,
     blink_state: bool,
     blinking_on: bool,
     blinking_paused: bool,
     blink_epoch: usize,
     workspace_id: WorkspaceId,
-    item_id: ItemId,
 }
 
 impl Entity for TerminalView {
@@ -89,11 +101,33 @@ impl Entity for TerminalView {
 }
 
 impl TerminalView {
-    pub fn from_terminal(
+    ///Create a new Terminal in the current working directory or the user's home directory
+    pub fn deploy(
+        workspace: &mut Workspace,
+        _: &workspace::NewTerminal,
+        cx: &mut ViewContext<Workspace>,
+    ) {
+        let strategy = cx.global::<Settings>().terminal_strategy();
+
+        let working_directory = get_working_directory(workspace, cx, strategy);
+
+        let window_id = cx.window_id();
+        let terminal = workspace
+            .project()
+            .update(cx, |project, cx| {
+                project.create_terminal(working_directory, window_id, cx)
+            })
+            .notify_err(workspace, cx);
+
+        if let Some(terminal) = terminal {
+            let view = cx.add_view(|cx| TerminalView::new(terminal, workspace.database_id(), cx));
+            workspace.add_item(Box::new(view), cx)
+        }
+    }
+
+    pub fn new(
         terminal: ModelHandle<Terminal>,
-        modal: bool,
         workspace_id: WorkspaceId,
-        item_id: ItemId,
         cx: &mut ViewContext<Self>,
     ) -> Self {
         cx.observe(&terminal, |_, _, cx| cx.notify()).detach();
@@ -114,7 +148,7 @@ impl TerminalView {
                 if let Some(foreground_info) = &this.terminal().read(cx).foreground_process_info {
                     let cwd = foreground_info.cwd.clone();
 
-                    let item_id = this.item_id;
+                    let item_id = cx.view_id();
                     let workspace_id = this.workspace_id;
                     cx.background()
                         .spawn(async move {
@@ -134,14 +168,12 @@ impl TerminalView {
             terminal,
             has_new_content: true,
             has_bell: false,
-            modal,
             context_menu: cx.add_view(ContextMenu::new),
             blink_state: true,
             blinking_on: false,
             blinking_paused: false,
             blink_epoch: 0,
             workspace_id,
-            item_id,
         }
     }
 
@@ -293,13 +325,6 @@ impl TerminalView {
         &self.terminal
     }
 
-    pub fn added_to_workspace(&mut self, new_id: WorkspaceId, cx: &mut ViewContext<Self>) {
-        cx.background()
-            .spawn(TERMINAL_DB.update_workspace_id(new_id, self.workspace_id, self.item_id))
-            .detach();
-        self.workspace_id = new_id;
-    }
-
     fn next_blink_epoch(&mut self) -> usize {
         self.blink_epoch += 1;
         self.blink_epoch
@@ -442,9 +467,7 @@ impl View for TerminalView {
 
     fn keymap_context(&self, cx: &gpui::AppContext) -> gpui::keymap::Context {
         let mut context = Self::default_keymap_context();
-        if self.modal {
-            context.set.insert("ModalTerminal".into());
-        }
+
         let mode = self.terminal.read(cx).last_content.mode;
         context.map.insert(
             "screen".to_string(),
@@ -523,3 +546,546 @@ impl View for TerminalView {
         context
     }
 }
+
+impl Item for TerminalView {
+    fn tab_content(
+        &self,
+        _detail: Option<usize>,
+        tab_theme: &theme::Tab,
+        cx: &gpui::AppContext,
+    ) -> ElementBox {
+        let title = self
+            .terminal()
+            .read(cx)
+            .foreground_process_info
+            .as_ref()
+            .map(|fpi| {
+                format!(
+                    "{} — {}",
+                    truncate_and_trailoff(
+                        &fpi.cwd
+                            .file_name()
+                            .map(|name| name.to_string_lossy().to_string())
+                            .unwrap_or_default(),
+                        25
+                    ),
+                    truncate_and_trailoff(
+                        &{
+                            format!(
+                                "{}{}",
+                                fpi.name,
+                                if fpi.argv.len() >= 1 {
+                                    format!(" {}", (&fpi.argv[1..]).join(" "))
+                                } else {
+                                    "".to_string()
+                                }
+                            )
+                        },
+                        25
+                    )
+                )
+            })
+            .unwrap_or_else(|| "Terminal".to_string());
+
+        Flex::row()
+            .with_child(
+                Label::new(title, tab_theme.label.clone())
+                    .aligned()
+                    .contained()
+                    .boxed(),
+            )
+            .boxed()
+    }
+
+    fn clone_on_split(
+        &self,
+        _workspace_id: WorkspaceId,
+        _cx: &mut ViewContext<Self>,
+    ) -> Option<Self> {
+        //From what I can tell, there's no  way to tell the current working
+        //Directory of the terminal from outside the shell. There might be
+        //solutions to this, but they are non-trivial and require more IPC
+
+        // Some(TerminalContainer::new(
+        //     Err(anyhow::anyhow!("failed to instantiate terminal")),
+        //     workspace_id,
+        //     cx,
+        // ))
+
+        // TODO
+        None
+    }
+
+    fn project_path(&self, _cx: &gpui::AppContext) -> Option<ProjectPath> {
+        None
+    }
+
+    fn project_entry_ids(&self, _cx: &gpui::AppContext) -> SmallVec<[project::ProjectEntryId; 3]> {
+        SmallVec::new()
+    }
+
+    fn is_singleton(&self, _cx: &gpui::AppContext) -> bool {
+        false
+    }
+
+    fn set_nav_history(&mut self, _: workspace::ItemNavHistory, _: &mut ViewContext<Self>) {}
+
+    fn can_save(&self, _cx: &gpui::AppContext) -> bool {
+        false
+    }
+
+    fn save(
+        &mut self,
+        _project: gpui::ModelHandle<Project>,
+        _cx: &mut ViewContext<Self>,
+    ) -> gpui::Task<gpui::anyhow::Result<()>> {
+        unreachable!("save should not have been called");
+    }
+
+    fn save_as(
+        &mut self,
+        _project: gpui::ModelHandle<Project>,
+        _abs_path: std::path::PathBuf,
+        _cx: &mut ViewContext<Self>,
+    ) -> gpui::Task<gpui::anyhow::Result<()>> {
+        unreachable!("save_as should not have been called");
+    }
+
+    fn reload(
+        &mut self,
+        _project: gpui::ModelHandle<Project>,
+        _cx: &mut ViewContext<Self>,
+    ) -> gpui::Task<gpui::anyhow::Result<()>> {
+        gpui::Task::ready(Ok(()))
+    }
+
+    fn is_dirty(&self, _cx: &gpui::AppContext) -> bool {
+        self.has_bell()
+    }
+
+    fn has_conflict(&self, _cx: &AppContext) -> bool {
+        false
+    }
+
+    fn as_searchable(&self, handle: &ViewHandle<Self>) -> Option<Box<dyn SearchableItemHandle>> {
+        Some(Box::new(handle.clone()))
+    }
+
+    fn to_item_events(event: &Self::Event) -> Vec<ItemEvent> {
+        match event {
+            Event::BreadcrumbsChanged => vec![ItemEvent::UpdateBreadcrumbs],
+            Event::TitleChanged | Event::Wakeup => vec![ItemEvent::UpdateTab],
+            Event::CloseTerminal => vec![ItemEvent::CloseItem],
+            _ => vec![],
+        }
+    }
+
+    fn breadcrumb_location(&self) -> ToolbarItemLocation {
+        ToolbarItemLocation::PrimaryLeft { flex: None }
+    }
+
+    fn breadcrumbs(&self, theme: &theme::Theme, cx: &AppContext) -> Option<Vec<ElementBox>> {
+        Some(vec![Text::new(
+            self.terminal().read(cx).breadcrumb_text.to_string(),
+            theme.breadcrumbs.text.clone(),
+        )
+        .boxed()])
+    }
+
+    fn serialized_item_kind() -> Option<&'static str> {
+        Some("Terminal")
+    }
+
+    fn deserialize(
+        project: ModelHandle<Project>,
+        _workspace: WeakViewHandle<Workspace>,
+        workspace_id: workspace::WorkspaceId,
+        item_id: workspace::ItemId,
+        cx: &mut ViewContext<Pane>,
+    ) -> Task<anyhow::Result<ViewHandle<Self>>> {
+        let window_id = cx.window_id();
+        cx.spawn(|pane, mut cx| async move {
+            let cwd = TERMINAL_DB
+                .take_working_directory(item_id, workspace_id)
+                .await
+                .log_err()
+                .flatten();
+
+            cx.update(|cx| {
+                let terminal = project.update(cx, |project, cx| {
+                    project.create_terminal(cwd, window_id, cx)
+                })?;
+
+                Ok(cx.add_view(pane, |cx| TerminalView::new(terminal, workspace_id, cx)))
+            })
+        })
+    }
+
+    fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext<Self>) {
+        cx.background()
+            .spawn(TERMINAL_DB.update_workspace_id(
+                workspace.database_id(),
+                self.workspace_id,
+                cx.view_id(),
+            ))
+            .detach();
+        self.workspace_id = workspace.database_id();
+    }
+}
+
+impl SearchableItem for TerminalView {
+    type Match = RangeInclusive<Point>;
+
+    fn supported_options() -> SearchOptions {
+        SearchOptions {
+            case: false,
+            word: false,
+            regex: false,
+        }
+    }
+
+    /// Convert events raised by this item into search-relevant events (if applicable)
+    fn to_search_event(event: &Self::Event) -> Option<SearchEvent> {
+        match event {
+            Event::Wakeup => Some(SearchEvent::MatchesInvalidated),
+            Event::SelectionsChanged => Some(SearchEvent::ActiveMatchChanged),
+            _ => None,
+        }
+    }
+
+    /// Clear stored matches
+    fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
+        self.terminal().update(cx, |term, _| term.matches.clear())
+    }
+
+    /// Store matches returned from find_matches somewhere for rendering
+    fn update_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
+        self.terminal().update(cx, |term, _| term.matches = matches)
+    }
+
+    /// Return the selection content to pre-load into this search
+    fn query_suggestion(&mut self, cx: &mut ViewContext<Self>) -> String {
+        self.terminal()
+            .read(cx)
+            .last_content
+            .selection_text
+            .clone()
+            .unwrap_or_default()
+    }
+
+    /// Focus match at given index into the Vec of matches
+    fn activate_match(&mut self, index: usize, _: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
+        self.terminal()
+            .update(cx, |term, _| term.activate_match(index));
+        cx.notify();
+    }
+
+    /// Get all of the matches for this query, should be done on the background
+    fn find_matches(
+        &mut self,
+        query: project::search::SearchQuery,
+        cx: &mut ViewContext<Self>,
+    ) -> Task<Vec<Self::Match>> {
+        if let Some(searcher) = regex_search_for_query(query) {
+            self.terminal()
+                .update(cx, |term, cx| term.find_matches(searcher, cx))
+        } else {
+            Task::ready(vec![])
+        }
+    }
+
+    /// Reports back to the search toolbar what the active match should be (the selection)
+    fn active_match_index(
+        &mut self,
+        matches: Vec<Self::Match>,
+        cx: &mut ViewContext<Self>,
+    ) -> Option<usize> {
+        // Selection head might have a value if there's a selection that isn't
+        // associated with a match. Therefore, if there are no matches, we should
+        // report None, no matter the state of the terminal
+        let res = if matches.len() > 0 {
+            if let Some(selection_head) = self.terminal().read(cx).selection_head {
+                // If selection head is contained in a match. Return that match
+                if let Some(ix) = matches
+                    .iter()
+                    .enumerate()
+                    .find(|(_, search_match)| {
+                        search_match.contains(&selection_head)
+                            || search_match.start() > &selection_head
+                    })
+                    .map(|(ix, _)| ix)
+                {
+                    Some(ix)
+                } else {
+                    // If no selection after selection head, return the last match
+                    Some(matches.len().saturating_sub(1))
+                }
+            } else {
+                // Matches found but no active selection, return the first last one (closest to cursor)
+                Some(matches.len().saturating_sub(1))
+            }
+        } else {
+            None
+        };
+
+        res
+    }
+}
+
+///Get's the working directory for the given workspace, respecting the user's settings.
+pub fn get_working_directory(
+    workspace: &Workspace,
+    cx: &AppContext,
+    strategy: WorkingDirectory,
+) -> Option<PathBuf> {
+    let res = match strategy {
+        WorkingDirectory::CurrentProjectDirectory => current_project_directory(workspace, cx)
+            .or_else(|| first_project_directory(workspace, cx)),
+        WorkingDirectory::FirstProjectDirectory => first_project_directory(workspace, cx),
+        WorkingDirectory::AlwaysHome => None,
+        WorkingDirectory::Always { directory } => {
+            shellexpand::full(&directory) //TODO handle this better
+                .ok()
+                .map(|dir| Path::new(&dir.to_string()).to_path_buf())
+                .filter(|dir| dir.is_dir())
+        }
+    };
+    res.or_else(home_dir)
+}
+
+///Get's the first project's home directory, or the home directory
+fn first_project_directory(workspace: &Workspace, cx: &AppContext) -> Option<PathBuf> {
+    workspace
+        .worktrees(cx)
+        .next()
+        .and_then(|worktree_handle| worktree_handle.read(cx).as_local())
+        .and_then(get_path_from_wt)
+}
+
+///Gets the intuitively correct working directory from the given workspace
+///If there is an active entry for this project, returns that entry's worktree root.
+///If there's no active entry but there is a worktree, returns that worktrees root.
+///If either of these roots are files, or if there are any other query failures,
+///  returns the user's home directory
+fn current_project_directory(workspace: &Workspace, cx: &AppContext) -> Option<PathBuf> {
+    let project = workspace.project().read(cx);
+
+    project
+        .active_entry()
+        .and_then(|entry_id| project.worktree_for_entry(entry_id, cx))
+        .or_else(|| workspace.worktrees(cx).next())
+        .and_then(|worktree_handle| worktree_handle.read(cx).as_local())
+        .and_then(get_path_from_wt)
+}
+
+fn get_path_from_wt(wt: &LocalWorktree) -> Option<PathBuf> {
+    wt.root_entry()
+        .filter(|re| re.is_dir())
+        .map(|_| wt.abs_path().to_path_buf())
+}
+
+#[cfg(test)]
+mod tests {
+
+    use super::*;
+    use gpui::TestAppContext;
+    use project::{Entry, Project, ProjectPath, Worktree};
+    use workspace::AppState;
+
+    use std::path::Path;
+
+    ///Working directory calculation tests
+
+    ///No Worktrees in project -> home_dir()
+    #[gpui::test]
+    async fn no_worktree(cx: &mut TestAppContext) {
+        //Setup variables
+        let (project, workspace) = blank_workspace(cx).await;
+        //Test
+        cx.read(|cx| {
+            let workspace = workspace.read(cx);
+            let active_entry = project.read(cx).active_entry();
+
+            //Make sure enviroment is as expeted
+            assert!(active_entry.is_none());
+            assert!(workspace.worktrees(cx).next().is_none());
+
+            let res = current_project_directory(workspace, cx);
+            assert_eq!(res, None);
+            let res = first_project_directory(workspace, cx);
+            assert_eq!(res, None);
+        });
+    }
+
+    ///No active entry, but a worktree, worktree is a file -> home_dir()
+    #[gpui::test]
+    async fn no_active_entry_worktree_is_file(cx: &mut TestAppContext) {
+        //Setup variables
+
+        let (project, workspace) = blank_workspace(cx).await;
+        create_file_wt(project.clone(), "/root.txt", cx).await;
+
+        cx.read(|cx| {
+            let workspace = workspace.read(cx);
+            let active_entry = project.read(cx).active_entry();
+
+            //Make sure enviroment is as expeted
+            assert!(active_entry.is_none());
+            assert!(workspace.worktrees(cx).next().is_some());
+
+            let res = current_project_directory(workspace, cx);
+            assert_eq!(res, None);
+            let res = first_project_directory(workspace, cx);
+            assert_eq!(res, None);
+        });
+    }
+
+    //No active entry, but a worktree, worktree is a folder -> worktree_folder
+    #[gpui::test]
+    async fn no_active_entry_worktree_is_dir(cx: &mut TestAppContext) {
+        //Setup variables
+        let (project, workspace) = blank_workspace(cx).await;
+        let (_wt, _entry) = create_folder_wt(project.clone(), "/root/", cx).await;
+
+        //Test
+        cx.update(|cx| {
+            let workspace = workspace.read(cx);
+            let active_entry = project.read(cx).active_entry();
+
+            assert!(active_entry.is_none());
+            assert!(workspace.worktrees(cx).next().is_some());
+
+            let res = current_project_directory(workspace, cx);
+            assert_eq!(res, Some((Path::new("/root/")).to_path_buf()));
+            let res = first_project_directory(workspace, cx);
+            assert_eq!(res, Some((Path::new("/root/")).to_path_buf()));
+        });
+    }
+
+    //Active entry with a work tree, worktree is a file -> home_dir()
+    #[gpui::test]
+    async fn active_entry_worktree_is_file(cx: &mut TestAppContext) {
+        //Setup variables
+
+        let (project, workspace) = blank_workspace(cx).await;
+        let (_wt, _entry) = create_folder_wt(project.clone(), "/root1/", cx).await;
+        let (wt2, entry2) = create_file_wt(project.clone(), "/root2.txt", cx).await;
+        insert_active_entry_for(wt2, entry2, project.clone(), cx);
+
+        //Test
+        cx.update(|cx| {
+            let workspace = workspace.read(cx);
+            let active_entry = project.read(cx).active_entry();
+
+            assert!(active_entry.is_some());
+
+            let res = current_project_directory(workspace, cx);
+            assert_eq!(res, None);
+            let res = first_project_directory(workspace, cx);
+            assert_eq!(res, Some((Path::new("/root1/")).to_path_buf()));
+        });
+    }
+
+    //Active entry, with a worktree, worktree is a folder -> worktree_folder
+    #[gpui::test]
+    async fn active_entry_worktree_is_dir(cx: &mut TestAppContext) {
+        //Setup variables
+        let (project, workspace) = blank_workspace(cx).await;
+        let (_wt, _entry) = create_folder_wt(project.clone(), "/root1/", cx).await;
+        let (wt2, entry2) = create_folder_wt(project.clone(), "/root2/", cx).await;
+        insert_active_entry_for(wt2, entry2, project.clone(), cx);
+
+        //Test
+        cx.update(|cx| {
+            let workspace = workspace.read(cx);
+            let active_entry = project.read(cx).active_entry();
+
+            assert!(active_entry.is_some());
+
+            let res = current_project_directory(workspace, cx);
+            assert_eq!(res, Some((Path::new("/root2/")).to_path_buf()));
+            let res = first_project_directory(workspace, cx);
+            assert_eq!(res, Some((Path::new("/root1/")).to_path_buf()));
+        });
+    }
+
+    ///Creates a worktree with 1 file: /root.txt
+    pub async fn blank_workspace(
+        cx: &mut TestAppContext,
+    ) -> (ModelHandle<Project>, ViewHandle<Workspace>) {
+        let params = cx.update(AppState::test);
+
+        let project = Project::test(params.fs.clone(), [], cx).await;
+        let (_, workspace) = cx.add_window(|cx| {
+            Workspace::new(
+                Default::default(),
+                0,
+                project.clone(),
+                |_, _| unimplemented!(),
+                cx,
+            )
+        });
+
+        (project, workspace)
+    }
+
+    ///Creates a worktree with 1 folder: /root{suffix}/
+    async fn create_folder_wt(
+        project: ModelHandle<Project>,
+        path: impl AsRef<Path>,
+        cx: &mut TestAppContext,
+    ) -> (ModelHandle<Worktree>, Entry) {
+        create_wt(project, true, path, cx).await
+    }
+
+    ///Creates a worktree with 1 file: /root{suffix}.txt
+    async fn create_file_wt(
+        project: ModelHandle<Project>,
+        path: impl AsRef<Path>,
+        cx: &mut TestAppContext,
+    ) -> (ModelHandle<Worktree>, Entry) {
+        create_wt(project, false, path, cx).await
+    }
+
+    async fn create_wt(
+        project: ModelHandle<Project>,
+        is_dir: bool,
+        path: impl AsRef<Path>,
+        cx: &mut TestAppContext,
+    ) -> (ModelHandle<Worktree>, Entry) {
+        let (wt, _) = project
+            .update(cx, |project, cx| {
+                project.find_or_create_local_worktree(path, true, cx)
+            })
+            .await
+            .unwrap();
+
+        let entry = cx
+            .update(|cx| {
+                wt.update(cx, |wt, cx| {
+                    wt.as_local()
+                        .unwrap()
+                        .create_entry(Path::new(""), is_dir, cx)
+                })
+            })
+            .await
+            .unwrap();
+
+        (wt, entry)
+    }
+
+    pub fn insert_active_entry_for(
+        wt: ModelHandle<Worktree>,
+        entry: Entry,
+        project: ModelHandle<Project>,
+        cx: &mut TestAppContext,
+    ) {
+        cx.update(|cx| {
+            let p = ProjectPath {
+                worktree_id: wt.read(cx).id(),
+                path: entry.path,
+            };
+            project.update(cx, |project, cx| project.set_active_path(Some(p), cx));
+        });
+    }
+}

crates/workspace/src/dock.rs 🔗

@@ -126,18 +126,21 @@ impl DockPosition {
     }
 }
 
-pub type DefaultItemFactory =
-    fn(&mut Workspace, &mut ViewContext<Workspace>) -> Box<dyn ItemHandle>;
+pub type DockDefaultItemFactory =
+    fn(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> Option<Box<dyn ItemHandle>>;
 
 pub struct Dock {
     position: DockPosition,
     panel_sizes: HashMap<DockAnchor, f32>,
     pane: ViewHandle<Pane>,
-    default_item_factory: DefaultItemFactory,
+    default_item_factory: DockDefaultItemFactory,
 }
 
 impl Dock {
-    pub fn new(default_item_factory: DefaultItemFactory, cx: &mut ViewContext<Workspace>) -> Self {
+    pub fn new(
+        default_item_factory: DockDefaultItemFactory,
+        cx: &mut ViewContext<Workspace>,
+    ) -> Self {
         let position = DockPosition::Hidden(cx.global::<Settings>().default_dock_anchor);
 
         let pane = cx.add_view(|cx| Pane::new(Some(position.anchor()), cx));
@@ -192,9 +195,11 @@ impl Dock {
             // Ensure that the pane has at least one item or construct a default item to put in it
             let pane = workspace.dock.pane.clone();
             if pane.read(cx).items().next().is_none() {
-                let item_to_add = (workspace.dock.default_item_factory)(workspace, cx);
-                // Adding the item focuses the pane by default
-                Pane::add_item(workspace, &pane, item_to_add, true, true, None, cx);
+                if let Some(item_to_add) = (workspace.dock.default_item_factory)(workspace, cx) {
+                    Pane::add_item(workspace, &pane, item_to_add, true, true, None, cx);
+                } else {
+                    workspace.dock.position = workspace.dock.position.hide();
+                }
             } else {
                 cx.focus(pane);
             }
@@ -465,8 +470,8 @@ mod tests {
     pub fn default_item_factory(
         _workspace: &mut Workspace,
         cx: &mut ViewContext<Workspace>,
-    ) -> Box<dyn ItemHandle> {
-        Box::new(cx.add_view(|_| TestItem::new()))
+    ) -> Option<Box<dyn ItemHandle>> {
+        Some(Box::new(cx.add_view(|_| TestItem::new())))
     }
 
     #[gpui::test]

crates/workspace/src/notifications.rs 🔗

@@ -161,8 +161,8 @@ pub mod simple_message_notification {
 
     pub struct MessageNotification {
         message: String,
-        click_action: Box<dyn Action>,
-        click_message: String,
+        click_action: Option<Box<dyn Action>>,
+        click_message: Option<String>,
     }
 
     pub enum MessageNotificationEvent {
@@ -174,6 +174,14 @@ pub mod simple_message_notification {
     }
 
     impl MessageNotification {
+        pub fn new_messsage<S: AsRef<str>>(message: S) -> MessageNotification {
+            Self {
+                message: message.as_ref().to_string(),
+                click_action: None,
+                click_message: None,
+            }
+        }
+
         pub fn new<S1: AsRef<str>, A: Action, S2: AsRef<str>>(
             message: S1,
             click_action: A,
@@ -181,8 +189,8 @@ pub mod simple_message_notification {
         ) -> Self {
             Self {
                 message: message.as_ref().to_string(),
-                click_action: Box::new(click_action) as Box<dyn Action>,
-                click_message: click_message.as_ref().to_string(),
+                click_action: Some(Box::new(click_action) as Box<dyn Action>),
+                click_message: Some(click_message.as_ref().to_string()),
             }
         }
 
@@ -202,8 +210,11 @@ pub mod simple_message_notification {
 
             enum MessageNotificationTag {}
 
-            let click_action = self.click_action.boxed_clone();
-            let click_message = self.click_message.clone();
+            let click_action = self
+                .click_action
+                .as_ref()
+                .map(|action| action.boxed_clone());
+            let click_message = self.click_message.as_ref().map(|message| message.clone());
             let message = self.message.clone();
 
             MouseEventHandler::<MessageNotificationTag>::new(0, cx, |state, cx| {
@@ -251,20 +262,28 @@ pub mod simple_message_notification {
                             )
                             .boxed(),
                     )
-                    .with_child({
+                    .with_children({
                         let style = theme.action_message.style_for(state, false);
-
-                        Text::new(click_message, style.text.clone())
-                            .contained()
-                            .with_style(style.container)
-                            .boxed()
+                        if let Some(click_message) = click_message {
+                            Some(
+                                Text::new(click_message, style.text.clone())
+                                    .contained()
+                                    .with_style(style.container)
+                                    .boxed(),
+                            )
+                        } else {
+                            None
+                        }
+                        .into_iter()
                     })
                     .contained()
                     .boxed()
             })
             .with_cursor_style(CursorStyle::PointingHand)
             .on_click(MouseButton::Left, move |_, cx| {
-                cx.dispatch_any_action(click_action.boxed_clone())
+                if let Some(click_action) = click_action.as_ref() {
+                    cx.dispatch_any_action(click_action.boxed_clone())
+                }
             })
             .boxed()
         }
@@ -278,3 +297,38 @@ pub mod simple_message_notification {
         }
     }
 }
+
+pub trait NotifyResultExt {
+    type Ok;
+
+    fn notify_err(
+        self,
+        workspace: &mut Workspace,
+        cx: &mut ViewContext<Workspace>,
+    ) -> Option<Self::Ok>;
+}
+
+impl<T, E> NotifyResultExt for Result<T, E>
+where
+    E: std::fmt::Debug,
+{
+    type Ok = T;
+
+    fn notify_err(self, workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> Option<T> {
+        match self {
+            Ok(value) => Some(value),
+            Err(err) => {
+                workspace.show_notification(0, cx, |cx| {
+                    cx.add_view(|_cx| {
+                        simple_message_notification::MessageNotification::new_messsage(format!(
+                            "Error: {:?}",
+                            err,
+                        ))
+                    })
+                });
+
+                None
+            }
+        }
+    }
+}

crates/workspace/src/workspace.rs 🔗

@@ -27,7 +27,7 @@ use anyhow::{anyhow, Context, Result};
 use call::ActiveCall;
 use client::{proto, Client, PeerId, TypedEnvelope, UserStore};
 use collections::{hash_map, HashMap, HashSet};
-use dock::{DefaultItemFactory, Dock, ToggleDockButton};
+use dock::{Dock, DockDefaultItemFactory, ToggleDockButton};
 use drag_and_drop::DragAndDrop;
 use fs::{self, Fs};
 use futures::{channel::oneshot, FutureExt, StreamExt};
@@ -375,7 +375,7 @@ pub struct AppState {
     pub fs: Arc<dyn fs::Fs>,
     pub build_window_options: fn() -> WindowOptions<'static>,
     pub initialize_workspace: fn(&mut Workspace, &Arc<AppState>, &mut ViewContext<Workspace>),
-    pub default_item_factory: DefaultItemFactory,
+    pub dock_default_item_factory: DockDefaultItemFactory,
 }
 
 impl AppState {
@@ -401,7 +401,7 @@ impl AppState {
             user_store,
             initialize_workspace: |_, _, _| {},
             build_window_options: Default::default,
-            default_item_factory: |_, _| unimplemented!(),
+            dock_default_item_factory: |_, _| unimplemented!(),
         })
     }
 }
@@ -515,7 +515,7 @@ impl Workspace {
         serialized_workspace: Option<SerializedWorkspace>,
         workspace_id: WorkspaceId,
         project: ModelHandle<Project>,
-        dock_default_factory: DefaultItemFactory,
+        dock_default_factory: DockDefaultItemFactory,
         cx: &mut ViewContext<Self>,
     ) -> Self {
         cx.observe_fullscreen(|_, _, cx| cx.notify()).detach();
@@ -703,7 +703,7 @@ impl Workspace {
                     serialized_workspace,
                     workspace_id,
                     project_handle,
-                    app_state.default_item_factory,
+                    app_state.dock_default_item_factory,
                     cx,
                 );
                 (app_state.initialize_workspace)(&mut workspace, &app_state, cx);
@@ -2694,7 +2694,7 @@ mod tests {
     pub fn default_item_factory(
         _workspace: &mut Workspace,
         _cx: &mut ViewContext<Workspace>,
-    ) -> Box<dyn ItemHandle> {
+    ) -> Option<Box<dyn ItemHandle>> {
         unimplemented!();
     }
 

crates/zed/src/main.rs 🔗

@@ -32,13 +32,15 @@ use settings::{
 use smol::process::Command;
 use std::fs::OpenOptions;
 use std::{env, ffi::OsStr, panic, path::PathBuf, sync::Arc, thread, time::Duration};
-use terminal_view::terminal_container_view::{get_working_directory, TerminalContainer};
+use terminal_view::{get_working_directory, TerminalView};
 
 use fs::RealFs;
 use settings::watched_json::{watch_keymap_file, watch_settings_file, WatchedJsonFile};
 use theme::ThemeRegistry;
 use util::{channel::RELEASE_CHANNEL, paths, ResultExt, TryFutureExt};
-use workspace::{self, item::ItemHandle, AppState, NewFile, OpenPaths, Workspace};
+use workspace::{
+    self, item::ItemHandle, notifications::NotifyResultExt, AppState, NewFile, OpenPaths, Workspace,
+};
 use zed::{self, build_window_options, initialize_workspace, languages, menus};
 
 fn main() {
@@ -150,7 +152,7 @@ fn main() {
             fs,
             build_window_options,
             initialize_workspace,
-            default_item_factory,
+            dock_default_item_factory,
         });
         auto_update::init(http, client::ZED_SERVER_URL.clone(), cx);
 
@@ -581,10 +583,10 @@ async fn handle_cli_connection(
     }
 }
 
-pub fn default_item_factory(
+pub fn dock_default_item_factory(
     workspace: &mut Workspace,
     cx: &mut ViewContext<Workspace>,
-) -> Box<dyn ItemHandle> {
+) -> Option<Box<dyn ItemHandle>> {
     let strategy = cx
         .global::<Settings>()
         .terminal_overrides
@@ -594,12 +596,15 @@ pub fn default_item_factory(
 
     let working_directory = get_working_directory(workspace, cx, strategy);
 
-    let terminal_handle = cx.add_view(|cx| {
-        TerminalContainer::new(
-            Err(anyhow!("Don't have a project to open a terminal")),
-            workspace.database_id(),
-            cx,
-        )
-    });
-    Box::new(terminal_handle)
+    let window_id = cx.window_id();
+    let terminal = workspace
+        .project()
+        .update(cx, |project, cx| {
+            project.create_terminal(working_directory, window_id, cx)
+        })
+        .notify_err(workspace, cx)?;
+
+    let terminal_view = cx.add_view(|cx| TerminalView::new(terminal, workspace.database_id(), cx));
+
+    Some(Box::new(terminal_view))
 }