Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions apps/native/crates/app/src/files/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

use gpui::{ClipboardItem, Context};
use t3_core::files::{absolute_path, clipboard_path, composer_file_link, file_surface_path};
use t3_core::markdown::links::open_in_editor_request_with_root;
use t3_core::project_search::{OPEN_IN_EDITOR, editor_target, preferred_editor};

use crate::app_state::AppState;
use crate::toast::{Toast, ToastKind};

/// Where the open thread's files live (`RightPanelFileProps`): its worktree, else its project.
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -70,4 +73,27 @@ impl AppState {
let path = clipboard_path(relative_path, &absolute_path(relative_path, &cwd));
cx.write_to_clipboard(ClipboardItem::new_string(path));
}

/// Opens a workspace or host path in the preferred editor, passing `projectRoot` (`#33558`).
pub fn open_file_in_preferred_editor(&mut self, relative_path: &str, line: Option<u64>, cx: &mut Context<Self>) -> bool {
let Some(workspace) = self.file_workspace() else { return false };
let available = self
.server_config
.config
.as_ref()
.map(|config| config.available_editors.clone())
.unwrap_or_default();
let Some((editor, label)) = preferred_editor(&available, None) else { return false };
let target = editor_target(&workspace.cwd, relative_path, line);
let request = self.rpc(OPEN_IN_EDITOR, open_in_editor_request_with_root(&target, editor, Some(&workspace.cwd)));
cx.spawn(async move |this, cx| {
if let Err(error) = request.await {
let _ = this.update(cx, |state, cx| {
state.show_toast(Toast::new(ToastKind::Error, format!("Could not open in {label}")).description(error.to_string()), cx);
});
}
})
.detach();
true
}
}
15 changes: 13 additions & 2 deletions apps/native/crates/app/src/files/explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::collections::VecDeque;
use std::time::Duration;

use gpui::{
App, AppContext, Context, Entity, FocusHandle, KeyBinding, ScrollStrategy, Subscription, Task, UniformListScrollHandle,
Window, actions,
App, AppContext, Context, Entity, EventEmitter, FocusHandle, KeyBinding, ScrollStrategy, Subscription, Task,
UniformListScrollHandle, Window, actions,
};
use gpui_component::input::{InputEvent, InputState};
use t3_core::files::{
Expand Down Expand Up @@ -78,6 +78,12 @@ pub struct Explorer {
_subscription: Subscription,
}

pub(super) enum ExplorerEvent {
Refresh,
}

impl EventEmitter<ExplorerEvent> for Explorer {}

impl Explorer {
pub fn new(state: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self {
let search_input = cx.new(|cx| InputState::new(window, cx).placeholder("Search files").clean_on_escape());
Expand Down Expand Up @@ -210,6 +216,11 @@ impl Explorer {

/// Refresh: lists every listed folder again and reruns the search.
pub fn refresh(&mut self, cx: &mut Context<Self>) {
self.refresh_entries(cx);
cx.emit(ExplorerEvent::Refresh);
}

pub fn refresh_entries(&mut self, cx: &mut Context<Self>) {
if self.root.is_none() {
return;
}
Expand Down
16 changes: 12 additions & 4 deletions apps/native/crates/app/src/files/explorer_rows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,22 @@ impl Explorer {
.when(row.loading, |svg| svg.opacity(0.6)),
)
.child(div().min_w_0().truncate().when(selected, |this| this.font_weight(FontWeight::MEDIUM)).child(label))
.context_menu(move |menu, _, _| {
let item = |label: &'static str, run: fn(&mut crate::app_state::AppState, &str, &mut Context<crate::app_state::AppState>)| {
.context_menu(move |menu, _, cx| {
let item = |label: SharedString, run: fn(&mut crate::app_state::AppState, &str, &mut Context<crate::app_state::AppState>)| {
let state = state.clone();
let path = path.clone();
PopupMenuItem::new(label).on_click(move |_, _, cx| state.update(cx, |state, cx| run(state, &path, cx)))
};
menu.item(item("Copy mention", |state, path, cx| state.copy_file_mention(path, cx)))
.item(item("Add to chat", |state, path, cx| state.add_file_to_chat(path, cx)))
let menu = if let Some(editor) = state.read(cx).preferred_editor_label() {
menu.item(item(format!("Open in {editor}").into(), |state, path, cx| {
state.open_file_in_preferred_editor(path, None, cx);
}))
} else {
menu
};
menu.item(item("Copy path".into(), |state, path, cx| state.copy_file_path(path, cx)))
.item(item("Copy mention".into(), |state, path, cx| state.copy_file_mention(path, cx)))
.item(item("Add to chat".into(), |state, path, cx| state.add_file_to_chat(path, cx)))
})
.into_any_element()
}
Expand Down
8 changes: 6 additions & 2 deletions apps/native/crates/app/src/files/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::app_state::{AppEvent, AppState, Phase};
use crate::icons::Icon;
use crate::right_panel::{Button, Variant};
use crate::theme::{hsla, palette};
use explorer::Explorer;
use explorer::{Explorer, ExplorerEvent};
use preview::Preview;

const STORE: &str = "files";
Expand Down Expand Up @@ -133,6 +133,10 @@ impl FilesPanel {
this.sync(cx);
}
});
cx.subscribe(&explorer, |this, _, event: &ExplorerEvent, cx| match event {
ExplorerEvent::Refresh => this.preview.update(cx, |preview, cx| preview.refetch(cx)),
})
.detach();
cx.observe(&preview, |this, preview, cx| {
// A path that reads as a folder opens in the explorer (`FileBrowserPanel` reveal).
if preview.read(cx).is_folder()
Expand Down Expand Up @@ -227,7 +231,7 @@ impl FilesPanel {
preview.set_target(target, cx);
});
if std::mem::take(&mut self.stale) {
self.explorer.update(cx, |explorer, cx| explorer.refresh(cx));
self.explorer.update(cx, |explorer, cx| explorer.refresh_entries(cx));
self.preview.update(cx, |preview, cx| preview.refetch(cx));
}
if std::mem::take(&mut self.reconnected) {
Expand Down
Loading