Remove 2 suffix for language selector, project panel, recent_projects, copilot_button, breadcrumbs, activity_indicator
Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
parent
252694390a
commit
292b3397ab
37 changed files with 1297 additions and 6979 deletions
|
@ -9,17 +9,18 @@ path = "src/language_selector.rs"
|
|||
doctest = false
|
||||
|
||||
[dependencies]
|
||||
editor = { path = "../editor" }
|
||||
fuzzy = { path = "../fuzzy" }
|
||||
language = { path = "../language" }
|
||||
gpui = { path = "../gpui" }
|
||||
picker = { path = "../picker" }
|
||||
project = { path = "../project" }
|
||||
theme = { path = "../theme" }
|
||||
settings = { path = "../settings" }
|
||||
editor = { package = "editor2", path = "../editor2" }
|
||||
fuzzy = { package = "fuzzy2", path = "../fuzzy2" }
|
||||
language = { package = "language2", path = "../language2" }
|
||||
gpui = { package = "gpui2", path = "../gpui2" }
|
||||
picker = { package = "picker2", path = "../picker2" }
|
||||
project = { package = "project2", path = "../project2" }
|
||||
theme = { package = "theme2", path = "../theme2" }
|
||||
ui = { package = "ui2", path = "../ui2" }
|
||||
settings = { package = "settings2", path = "../settings2" }
|
||||
util = { path = "../util" }
|
||||
workspace = { path = "../workspace" }
|
||||
workspace = { package = "workspace2", path = "../workspace2" }
|
||||
anyhow.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
editor = { path = "../editor", features = ["test-support"] }
|
||||
editor = { package = "editor2", path = "../editor2", features = ["test-support"] }
|
||||
|
|
|
@ -1,15 +1,14 @@
|
|||
use editor::Editor;
|
||||
use gpui::{
|
||||
elements::*,
|
||||
platform::{CursorStyle, MouseButton},
|
||||
Entity, Subscription, View, ViewContext, ViewHandle, WeakViewHandle,
|
||||
};
|
||||
use gpui::{div, IntoElement, ParentElement, Render, Subscription, View, ViewContext, WeakView};
|
||||
use std::sync::Arc;
|
||||
use ui::{Button, ButtonCommon, Clickable, LabelSize, Tooltip};
|
||||
use workspace::{item::ItemHandle, StatusItemView, Workspace};
|
||||
|
||||
use crate::LanguageSelector;
|
||||
|
||||
pub struct ActiveBufferLanguage {
|
||||
active_language: Option<Option<Arc<str>>>,
|
||||
workspace: WeakViewHandle<Workspace>,
|
||||
workspace: WeakView<Workspace>,
|
||||
_observe_active_editor: Option<Subscription>,
|
||||
}
|
||||
|
||||
|
@ -22,7 +21,7 @@ impl ActiveBufferLanguage {
|
|||
}
|
||||
}
|
||||
|
||||
fn update_language(&mut self, editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) {
|
||||
fn update_language(&mut self, editor: View<Editor>, cx: &mut ViewContext<Self>) {
|
||||
self.active_language = Some(None);
|
||||
|
||||
let editor = editor.read(cx);
|
||||
|
@ -36,44 +35,28 @@ impl ActiveBufferLanguage {
|
|||
}
|
||||
}
|
||||
|
||||
impl Entity for ActiveBufferLanguage {
|
||||
type Event = ();
|
||||
}
|
||||
|
||||
impl View for ActiveBufferLanguage {
|
||||
fn ui_name() -> &'static str {
|
||||
"ActiveBufferLanguage"
|
||||
}
|
||||
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
|
||||
if let Some(active_language) = self.active_language.as_ref() {
|
||||
impl Render for ActiveBufferLanguage {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
div().when_some(self.active_language.as_ref(), |el, active_language| {
|
||||
let active_language_text = if let Some(active_language_text) = active_language {
|
||||
active_language_text.to_string()
|
||||
} else {
|
||||
"Unknown".to_string()
|
||||
};
|
||||
let theme = theme::current(cx).clone();
|
||||
|
||||
MouseEventHandler::new::<Self, _>(0, cx, |state, cx| {
|
||||
let theme = &theme::current(cx).workspace.status_bar;
|
||||
let style = theme.active_language.style_for(state);
|
||||
Label::new(active_language_text, style.text.clone())
|
||||
.contained()
|
||||
.with_style(style.container)
|
||||
})
|
||||
.with_cursor_style(CursorStyle::PointingHand)
|
||||
.on_click(MouseButton::Left, |_, this, cx| {
|
||||
if let Some(workspace) = this.workspace.upgrade(cx) {
|
||||
workspace.update(cx, |workspace, cx| {
|
||||
crate::toggle(workspace, &Default::default(), cx)
|
||||
});
|
||||
}
|
||||
})
|
||||
.with_tooltip::<Self>(0, "Select Language", None, theme.tooltip.clone(), cx)
|
||||
.into_any()
|
||||
} else {
|
||||
Empty::new().into_any()
|
||||
}
|
||||
el.child(
|
||||
Button::new("change-language", active_language_text)
|
||||
.label_size(LabelSize::Small)
|
||||
.on_click(cx.listener(|this, _, cx| {
|
||||
if let Some(workspace) = this.workspace.upgrade() {
|
||||
workspace.update(cx, |workspace, cx| {
|
||||
LanguageSelector::toggle(workspace, cx)
|
||||
});
|
||||
}
|
||||
}))
|
||||
.tooltip(|cx| Tooltip::text("Select Language", cx)),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,46 +4,87 @@ pub use active_buffer_language::ActiveBufferLanguage;
|
|||
use anyhow::anyhow;
|
||||
use editor::Editor;
|
||||
use fuzzy::{match_strings, StringMatch, StringMatchCandidate};
|
||||
use gpui::{actions, elements::*, AppContext, ModelHandle, MouseState, ViewContext};
|
||||
use gpui::{
|
||||
actions, AppContext, DismissEvent, EventEmitter, FocusHandle, FocusableView, Model,
|
||||
ParentElement, Render, Styled, View, ViewContext, VisualContext, WeakView,
|
||||
};
|
||||
use language::{Buffer, LanguageRegistry};
|
||||
use picker::{Picker, PickerDelegate, PickerEvent};
|
||||
use picker::{Picker, PickerDelegate};
|
||||
use project::Project;
|
||||
use std::sync::Arc;
|
||||
use ui::{prelude::*, HighlightedLabel, ListItem, ListItemSpacing};
|
||||
use util::ResultExt;
|
||||
use workspace::Workspace;
|
||||
use workspace::{ModalView, Workspace};
|
||||
|
||||
actions!(language_selector, [Toggle]);
|
||||
|
||||
pub fn init(cx: &mut AppContext) {
|
||||
Picker::<LanguageSelectorDelegate>::init(cx);
|
||||
cx.add_action(toggle);
|
||||
cx.observe_new_views(LanguageSelector::register).detach();
|
||||
}
|
||||
|
||||
pub fn toggle(
|
||||
workspace: &mut Workspace,
|
||||
_: &Toggle,
|
||||
cx: &mut ViewContext<Workspace>,
|
||||
) -> Option<()> {
|
||||
let (_, buffer, _) = workspace
|
||||
.active_item(cx)?
|
||||
.act_as::<Editor>(cx)?
|
||||
.read(cx)
|
||||
.active_excerpt(cx)?;
|
||||
workspace.toggle_modal(cx, |workspace, cx| {
|
||||
let registry = workspace.app_state().languages.clone();
|
||||
cx.add_view(|cx| {
|
||||
Picker::new(
|
||||
LanguageSelectorDelegate::new(buffer, workspace.project().clone(), registry),
|
||||
cx,
|
||||
)
|
||||
})
|
||||
});
|
||||
Some(())
|
||||
pub struct LanguageSelector {
|
||||
picker: View<Picker<LanguageSelectorDelegate>>,
|
||||
}
|
||||
|
||||
impl LanguageSelector {
|
||||
fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
|
||||
workspace.register_action(move |workspace, _: &Toggle, cx| {
|
||||
Self::toggle(workspace, cx);
|
||||
});
|
||||
}
|
||||
|
||||
fn toggle(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> Option<()> {
|
||||
let registry = workspace.app_state().languages.clone();
|
||||
let (_, buffer, _) = workspace
|
||||
.active_item(cx)?
|
||||
.act_as::<Editor>(cx)?
|
||||
.read(cx)
|
||||
.active_excerpt(cx)?;
|
||||
let project = workspace.project().clone();
|
||||
|
||||
workspace.toggle_modal(cx, move |cx| {
|
||||
LanguageSelector::new(buffer, project, registry, cx)
|
||||
});
|
||||
Some(())
|
||||
}
|
||||
|
||||
fn new(
|
||||
buffer: Model<Buffer>,
|
||||
project: Model<Project>,
|
||||
language_registry: Arc<LanguageRegistry>,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Self {
|
||||
let delegate = LanguageSelectorDelegate::new(
|
||||
cx.view().downgrade(),
|
||||
buffer,
|
||||
project,
|
||||
language_registry,
|
||||
);
|
||||
|
||||
let picker = cx.new_view(|cx| Picker::new(delegate, cx));
|
||||
Self { picker }
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for LanguageSelector {
|
||||
fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
v_stack().w(rems(34.)).child(self.picker.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl FocusableView for LanguageSelector {
|
||||
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
|
||||
self.picker.focus_handle(cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl EventEmitter<DismissEvent> for LanguageSelector {}
|
||||
impl ModalView for LanguageSelector {}
|
||||
|
||||
pub struct LanguageSelectorDelegate {
|
||||
buffer: ModelHandle<Buffer>,
|
||||
project: ModelHandle<Project>,
|
||||
language_selector: WeakView<LanguageSelector>,
|
||||
buffer: Model<Buffer>,
|
||||
project: Model<Project>,
|
||||
language_registry: Arc<LanguageRegistry>,
|
||||
candidates: Vec<StringMatchCandidate>,
|
||||
matches: Vec<StringMatch>,
|
||||
|
@ -52,8 +93,9 @@ pub struct LanguageSelectorDelegate {
|
|||
|
||||
impl LanguageSelectorDelegate {
|
||||
fn new(
|
||||
buffer: ModelHandle<Buffer>,
|
||||
project: ModelHandle<Project>,
|
||||
language_selector: WeakView<LanguageSelector>,
|
||||
buffer: Model<Buffer>,
|
||||
project: Model<Project>,
|
||||
language_registry: Arc<LanguageRegistry>,
|
||||
) -> Self {
|
||||
let candidates = language_registry
|
||||
|
@ -62,29 +104,22 @@ impl LanguageSelectorDelegate {
|
|||
.enumerate()
|
||||
.map(|(candidate_id, name)| StringMatchCandidate::new(candidate_id, name))
|
||||
.collect::<Vec<_>>();
|
||||
let mut matches = candidates
|
||||
.iter()
|
||||
.map(|candidate| StringMatch {
|
||||
candidate_id: candidate.id,
|
||||
score: 0.,
|
||||
positions: Default::default(),
|
||||
string: candidate.string.clone(),
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
matches.sort_unstable_by(|mat1, mat2| mat1.string.cmp(&mat2.string));
|
||||
|
||||
Self {
|
||||
language_selector,
|
||||
buffer,
|
||||
project,
|
||||
language_registry,
|
||||
candidates,
|
||||
matches,
|
||||
matches: vec![],
|
||||
selected_index: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PickerDelegate for LanguageSelectorDelegate {
|
||||
type ListItem = ListItem;
|
||||
|
||||
fn placeholder_text(&self) -> Arc<str> {
|
||||
"Select a language...".into()
|
||||
}
|
||||
|
@ -102,23 +137,25 @@ impl PickerDelegate for LanguageSelectorDelegate {
|
|||
cx.spawn(|_, mut cx| async move {
|
||||
let language = language.await?;
|
||||
let project = project
|
||||
.upgrade(&cx)
|
||||
.upgrade()
|
||||
.ok_or_else(|| anyhow!("project was dropped"))?;
|
||||
let buffer = buffer
|
||||
.upgrade(&cx)
|
||||
.upgrade()
|
||||
.ok_or_else(|| anyhow!("buffer was dropped"))?;
|
||||
project.update(&mut cx, |project, cx| {
|
||||
project.set_language_for_buffer(&buffer, language, cx);
|
||||
});
|
||||
anyhow::Ok(())
|
||||
})
|
||||
})
|
||||
.detach_and_log_err(cx);
|
||||
}
|
||||
|
||||
cx.emit(PickerEvent::Dismiss);
|
||||
self.dismissed(cx);
|
||||
}
|
||||
|
||||
fn dismissed(&mut self, _cx: &mut ViewContext<Picker<Self>>) {}
|
||||
fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>) {
|
||||
self.language_selector
|
||||
.update(cx, |_, cx| cx.emit(DismissEvent))
|
||||
.log_err();
|
||||
}
|
||||
|
||||
fn selected_index(&self) -> usize {
|
||||
self.selected_index
|
||||
|
@ -133,7 +170,7 @@ impl PickerDelegate for LanguageSelectorDelegate {
|
|||
query: String,
|
||||
cx: &mut ViewContext<Picker<Self>>,
|
||||
) -> gpui::Task<()> {
|
||||
let background = cx.background().clone();
|
||||
let background = cx.background_executor().clone();
|
||||
let candidates = self.candidates.clone();
|
||||
cx.spawn(|this, mut cx| async move {
|
||||
let matches = if query.is_empty() {
|
||||
|
@ -160,7 +197,7 @@ impl PickerDelegate for LanguageSelectorDelegate {
|
|||
};
|
||||
|
||||
this.update(&mut cx, |this, cx| {
|
||||
let delegate = this.delegate_mut();
|
||||
let delegate = &mut this.delegate;
|
||||
delegate.matches = matches;
|
||||
delegate.selected_index = delegate
|
||||
.selected_index
|
||||
|
@ -174,23 +211,22 @@ impl PickerDelegate for LanguageSelectorDelegate {
|
|||
fn render_match(
|
||||
&self,
|
||||
ix: usize,
|
||||
mouse_state: &mut MouseState,
|
||||
selected: bool,
|
||||
cx: &AppContext,
|
||||
) -> AnyElement<Picker<Self>> {
|
||||
let theme = theme::current(cx);
|
||||
cx: &mut ViewContext<Picker<Self>>,
|
||||
) -> Option<Self::ListItem> {
|
||||
let mat = &self.matches[ix];
|
||||
let style = theme.picker.item.in_state(selected).style_for(mouse_state);
|
||||
let buffer_language_name = self.buffer.read(cx).language().map(|l| l.name());
|
||||
let mut label = mat.string.clone();
|
||||
if buffer_language_name.as_deref() == Some(mat.string.as_str()) {
|
||||
label.push_str(" (current)");
|
||||
}
|
||||
|
||||
Label::new(label, style.label.clone())
|
||||
.with_highlights(mat.positions.clone())
|
||||
.contained()
|
||||
.with_style(style.container)
|
||||
.into_any()
|
||||
Some(
|
||||
ListItem::new(ix)
|
||||
.inset(true)
|
||||
.spacing(ListItemSpacing::Sparse)
|
||||
.selected(selected)
|
||||
.child(HighlightedLabel::new(label, mat.positions.clone())),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue