mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-07 20:39:04 +03:00
Introduce a new language picker that displays available languages
Right now this panics when trying to select a language, so that's what we're going to implement next. Co-Authored-By: Julia Risley <julia@zed.dev>
This commit is contained in:
parent
d39c761de5
commit
b402f27d50
16
Cargo.lock
generated
16
Cargo.lock
generated
@ -3286,6 +3286,21 @@ dependencies = [
|
|||||||
"util",
|
"util",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "language_selector"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"editor",
|
||||||
|
"fuzzy",
|
||||||
|
"gpui",
|
||||||
|
"language",
|
||||||
|
"picker",
|
||||||
|
"project",
|
||||||
|
"settings",
|
||||||
|
"theme",
|
||||||
|
"workspace",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
version = "1.4.0"
|
version = "1.4.0"
|
||||||
@ -8399,6 +8414,7 @@ dependencies = [
|
|||||||
"isahc",
|
"isahc",
|
||||||
"journal",
|
"journal",
|
||||||
"language",
|
"language",
|
||||||
|
"language_selector",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"libc",
|
"libc",
|
||||||
"log",
|
"log",
|
||||||
|
@ -28,6 +28,7 @@ members = [
|
|||||||
"crates/gpui_macros",
|
"crates/gpui_macros",
|
||||||
"crates/journal",
|
"crates/journal",
|
||||||
"crates/language",
|
"crates/language",
|
||||||
|
"crates/language_selector",
|
||||||
"crates/live_kit_client",
|
"crates/live_kit_client",
|
||||||
"crates/live_kit_server",
|
"crates/live_kit_server",
|
||||||
"crates/lsp",
|
"crates/lsp",
|
||||||
|
20
crates/language_selector/Cargo.toml
Normal file
20
crates/language_selector/Cargo.toml
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
[package]
|
||||||
|
name = "language_selector"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
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" }
|
||||||
|
workspace = { path = "../workspace" }
|
196
crates/language_selector/src/language_selector.rs
Normal file
196
crates/language_selector/src/language_selector.rs
Normal file
@ -0,0 +1,196 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use fuzzy::{match_strings, StringMatch, StringMatchCandidate};
|
||||||
|
use gpui::{
|
||||||
|
actions, elements::*, AnyViewHandle, AppContext, Entity, MouseState, MutableAppContext,
|
||||||
|
RenderContext, View, ViewContext, ViewHandle,
|
||||||
|
};
|
||||||
|
use language::LanguageRegistry;
|
||||||
|
use picker::{Picker, PickerDelegate};
|
||||||
|
use settings::Settings;
|
||||||
|
use workspace::{AppState, Workspace};
|
||||||
|
|
||||||
|
actions!(language_selector, [Toggle]);
|
||||||
|
|
||||||
|
pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
|
||||||
|
Picker::<LanguageSelector>::init(cx);
|
||||||
|
cx.add_action({
|
||||||
|
let language_registry = app_state.languages.clone();
|
||||||
|
move |workspace, _: &Toggle, cx| {
|
||||||
|
LanguageSelector::toggle(workspace, language_registry.clone(), cx)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Event {
|
||||||
|
Dismissed,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct LanguageSelector {
|
||||||
|
language_registry: Arc<LanguageRegistry>,
|
||||||
|
matches: Vec<StringMatch>,
|
||||||
|
picker: ViewHandle<Picker<Self>>,
|
||||||
|
selected_index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LanguageSelector {
|
||||||
|
fn new(language_registry: Arc<LanguageRegistry>, cx: &mut ViewContext<Self>) -> Self {
|
||||||
|
let handle = cx.weak_handle();
|
||||||
|
let picker = cx.add_view(|cx| Picker::new("Select Language...", handle, cx));
|
||||||
|
|
||||||
|
let mut matches = language_registry
|
||||||
|
.language_names()
|
||||||
|
.into_iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(candidate_id, name)| StringMatch {
|
||||||
|
candidate_id,
|
||||||
|
score: 0.0,
|
||||||
|
positions: Default::default(),
|
||||||
|
string: name,
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
matches.sort_unstable_by(|mat1, mat2| mat1.string.cmp(&mat2.string));
|
||||||
|
|
||||||
|
Self {
|
||||||
|
language_registry,
|
||||||
|
matches,
|
||||||
|
picker,
|
||||||
|
selected_index: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn toggle(
|
||||||
|
workspace: &mut Workspace,
|
||||||
|
registry: Arc<LanguageRegistry>,
|
||||||
|
cx: &mut ViewContext<Workspace>,
|
||||||
|
) {
|
||||||
|
workspace.toggle_modal(cx, |_, cx| {
|
||||||
|
let this = cx.add_view(|cx| Self::new(registry, cx));
|
||||||
|
cx.subscribe(&this, Self::on_event).detach();
|
||||||
|
this
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_event(
|
||||||
|
workspace: &mut Workspace,
|
||||||
|
_: ViewHandle<LanguageSelector>,
|
||||||
|
event: &Event,
|
||||||
|
cx: &mut ViewContext<Workspace>,
|
||||||
|
) {
|
||||||
|
match event {
|
||||||
|
Event::Dismissed => {
|
||||||
|
workspace.dismiss_modal(cx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Entity for LanguageSelector {
|
||||||
|
type Event = Event;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl View for LanguageSelector {
|
||||||
|
fn ui_name() -> &'static str {
|
||||||
|
"LanguageSelector"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
|
||||||
|
ChildView::new(self.picker.clone(), cx).boxed()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
|
||||||
|
if cx.is_self_focused() {
|
||||||
|
cx.focus(&self.picker);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PickerDelegate for LanguageSelector {
|
||||||
|
fn match_count(&self) -> usize {
|
||||||
|
self.matches.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn confirm(&mut self, cx: &mut ViewContext<Self>) {
|
||||||
|
todo!();
|
||||||
|
cx.emit(Event::Dismissed);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn dismiss(&mut self, cx: &mut ViewContext<Self>) {
|
||||||
|
cx.emit(Event::Dismissed);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn selected_index(&self) -> usize {
|
||||||
|
self.selected_index
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<Self>) {
|
||||||
|
self.selected_index = ix;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_matches(&mut self, query: String, cx: &mut ViewContext<Self>) -> gpui::Task<()> {
|
||||||
|
let background = cx.background().clone();
|
||||||
|
let candidates = self
|
||||||
|
.language_registry
|
||||||
|
.language_names()
|
||||||
|
.into_iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(id, name)| StringMatchCandidate {
|
||||||
|
id,
|
||||||
|
char_bag: name.as_str().into(),
|
||||||
|
string: name.clone(),
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
cx.spawn(|this, mut cx| async move {
|
||||||
|
let matches = if query.is_empty() {
|
||||||
|
candidates
|
||||||
|
.into_iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(index, candidate)| StringMatch {
|
||||||
|
candidate_id: index,
|
||||||
|
string: candidate.string,
|
||||||
|
positions: Vec::new(),
|
||||||
|
score: 0.0,
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
} else {
|
||||||
|
match_strings(
|
||||||
|
&candidates,
|
||||||
|
&query,
|
||||||
|
false,
|
||||||
|
100,
|
||||||
|
&Default::default(),
|
||||||
|
background,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
};
|
||||||
|
|
||||||
|
this.update(&mut cx, |this, cx| {
|
||||||
|
this.matches = matches;
|
||||||
|
this.selected_index = this
|
||||||
|
.selected_index
|
||||||
|
.min(this.matches.len().saturating_sub(1));
|
||||||
|
cx.notify();
|
||||||
|
});
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_match(
|
||||||
|
&self,
|
||||||
|
ix: usize,
|
||||||
|
mouse_state: &mut MouseState,
|
||||||
|
selected: bool,
|
||||||
|
cx: &AppContext,
|
||||||
|
) -> ElementBox {
|
||||||
|
let settings = cx.global::<Settings>();
|
||||||
|
let theme = &settings.theme;
|
||||||
|
let theme_match = &self.matches[ix];
|
||||||
|
let style = theme.picker.item.style_for(mouse_state, selected);
|
||||||
|
|
||||||
|
Label::new(theme_match.string.clone(), style.label.clone())
|
||||||
|
.with_highlights(theme_match.positions.clone())
|
||||||
|
.contained()
|
||||||
|
.with_style(style.container)
|
||||||
|
.boxed()
|
||||||
|
}
|
||||||
|
}
|
@ -40,6 +40,7 @@ go_to_line = { path = "../go_to_line" }
|
|||||||
gpui = { path = "../gpui" }
|
gpui = { path = "../gpui" }
|
||||||
journal = { path = "../journal" }
|
journal = { path = "../journal" }
|
||||||
language = { path = "../language" }
|
language = { path = "../language" }
|
||||||
|
language_selector = { path = "../language_selector" }
|
||||||
lsp = { path = "../lsp" }
|
lsp = { path = "../lsp" }
|
||||||
outline = { path = "../outline" }
|
outline = { path = "../outline" }
|
||||||
plugin_runtime = { path = "../plugin_runtime" }
|
plugin_runtime = { path = "../plugin_runtime" }
|
||||||
|
@ -185,6 +185,7 @@ fn main() {
|
|||||||
workspace::init(app_state.clone(), cx);
|
workspace::init(app_state.clone(), cx);
|
||||||
|
|
||||||
journal::init(app_state.clone(), cx);
|
journal::init(app_state.clone(), cx);
|
||||||
|
language_selector::init(app_state.clone(), cx);
|
||||||
theme_selector::init(app_state.clone(), cx);
|
theme_selector::init(app_state.clone(), cx);
|
||||||
zed::init(&app_state, cx);
|
zed::init(&app_state, cx);
|
||||||
collab_ui::init(app_state.clone(), cx);
|
collab_ui::init(app_state.clone(), cx);
|
||||||
|
Loading…
Reference in New Issue
Block a user