Skip to content

Commit

Permalink
Add language icons to the language selector (#21298)
Browse files Browse the repository at this point in the history
Closes #21290

This is a first attempt to show the language icons to the selector.
Ideally, I wouldn't like to have yet another place mapping extensions to
icons, as we already have the `file_types.json` file doing that, but I'm
not so sure how to pull from it yet. Maybe in a future pass we'll
improve this and make it more solid.

<img width="700" alt="Screenshot 2024-11-28 at 16 10 27"
src="https://github.com/user-attachments/assets/683c3bef-5389-470f-a41e-3d510b927b61">

Release Notes:

- N/A

---------

Co-authored-by: Kirill Bulatov <[email protected]>
Co-authored-by: Piotr Osiewicz <[email protected]>
  • Loading branch information
3 people authored Dec 2, 2024
1 parent 995b40f commit f795ce9
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 18 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions assets/icons/file_icons/diff.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions assets/icons/file_icons/file_types.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"dat": "storage",
"db": "storage",
"dbf": "storage",
"diff": "diff",
"dll": "storage",
"doc": "document",
"docx": "document",
Expand Down Expand Up @@ -112,6 +113,7 @@
"mkv": "video",
"ml": "ocaml",
"mli": "ocaml",
"mod": "go",
"mov": "video",
"mp3": "audio",
"mp4": "video",
Expand Down Expand Up @@ -185,6 +187,7 @@
"wmv": "video",
"woff": "font",
"woff2": "font",
"work": "go",
"wv": "audio",
"xls": "document",
"xlsx": "document",
Expand Down Expand Up @@ -239,6 +242,9 @@
"default": {
"icon": "icons/file_icons/file.svg"
},
"diff": {
"icon": "icons/file_icons/diff.svg"
},
"docker": {
"icon": "icons/file_icons/docker.svg"
},
Expand Down
4 changes: 3 additions & 1 deletion crates/extension/src/extension_host_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ pub trait ExtensionLanguageProxy: Send + Sync + 'static {
language: LanguageName,
grammar: Option<Arc<str>>,
matcher: LanguageMatcher,
hidden: bool,
load: Arc<dyn Fn() -> Result<LoadedLanguage> + Send + Sync + 'static>,
);

Expand All @@ -175,13 +176,14 @@ impl ExtensionLanguageProxy for ExtensionHostProxy {
language: LanguageName,
grammar: Option<Arc<str>>,
matcher: LanguageMatcher,
hidden: bool,
load: Arc<dyn Fn() -> Result<LoadedLanguage> + Send + Sync + 'static>,
) {
let Some(proxy) = self.language_proxy.read().clone() else {
return;
};

proxy.register_language(language, grammar, matcher, load)
proxy.register_language(language, grammar, matcher, hidden, load)
}

fn remove_languages(
Expand Down
3 changes: 3 additions & 0 deletions crates/extension_host/src/extension_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ pub struct ExtensionIndexLanguageEntry {
pub extension: Arc<str>,
pub path: PathBuf,
pub matcher: LanguageMatcher,
pub hidden: bool,
pub grammar: Option<Arc<str>>,
}

Expand Down Expand Up @@ -1097,6 +1098,7 @@ impl ExtensionStore {
language_name.clone(),
language.grammar.clone(),
language.matcher.clone(),
language.hidden,
Arc::new(move || {
let config = std::fs::read_to_string(language_path.join("config.toml"))?;
let config: LanguageConfig = ::toml::from_str(&config)?;
Expand Down Expand Up @@ -1324,6 +1326,7 @@ impl ExtensionStore {
extension: extension_id.clone(),
path: relative_path,
matcher: config.matcher,
hidden: config.hidden,
grammar: config.grammar,
},
);
Expand Down
2 changes: 2 additions & 0 deletions crates/extension_host/src/extension_store_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ async fn test_extension_store(cx: &mut TestAppContext) {
extension: "zed-ruby".into(),
path: "languages/erb".into(),
grammar: Some("embedded_template".into()),
hidden: false,
matcher: LanguageMatcher {
path_suffixes: vec!["erb".into()],
first_line_pattern: None,
Expand All @@ -215,6 +216,7 @@ async fn test_extension_store(cx: &mut TestAppContext) {
extension: "zed-ruby".into(),
path: "languages/ruby".into(),
grammar: Some("ruby".into()),
hidden: false,
matcher: LanguageMatcher {
path_suffixes: vec!["rb".into()],
first_line_pattern: None,
Expand Down
1 change: 1 addition & 0 deletions crates/extension_host/src/headless_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ impl HeadlessExtensionStore {
config.name.clone(),
None,
config.matcher.clone(),
config.hidden,
Arc::new(move || {
Ok(LoadedLanguage {
config: config.clone(),
Expand Down
2 changes: 1 addition & 1 deletion crates/file_finder/src/file_finder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
mod file_finder_tests;

mod file_finder_settings;
pub mod file_finder_settings;
mod new_path_prompt;
mod open_path_prompt;

Expand Down
8 changes: 8 additions & 0 deletions crates/language/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ pub static PLAIN_TEXT: LazyLock<Arc<Language>> = LazyLock::new(|| {
LanguageConfig {
name: "Plain Text".into(),
soft_wrap: Some(SoftWrap::EditorWidth),
matcher: LanguageMatcher {
path_suffixes: vec!["txt".to_owned()],
first_line_pattern: None,
},
..Default::default()
},
None,
Expand Down Expand Up @@ -1418,6 +1422,10 @@ impl Language {
pub fn prettier_parser_name(&self) -> Option<&str> {
self.config.prettier_parser_name.as_deref()
}

pub fn config(&self) -> &LanguageConfig {
&self.config
}
}

impl LanguageScope {
Expand Down
15 changes: 10 additions & 5 deletions crates/language/src/language_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ pub struct AvailableLanguage {
name: LanguageName,
grammar: Option<Arc<str>>,
matcher: LanguageMatcher,
hidden: bool,
load: Arc<dyn Fn() -> Result<LoadedLanguage> + 'static + Send + Sync>,
loaded: bool,
}
Expand All @@ -142,6 +143,9 @@ impl AvailableLanguage {
pub fn matcher(&self) -> &LanguageMatcher {
&self.matcher
}
pub fn hidden(&self) -> bool {
self.hidden
}
}

enum AvailableGrammar {
Expand Down Expand Up @@ -288,6 +292,7 @@ impl LanguageRegistry {
config.name.clone(),
config.grammar.clone(),
config.matcher.clone(),
config.hidden,
Arc::new(move || {
Ok(LoadedLanguage {
config: config.clone(),
Expand Down Expand Up @@ -436,6 +441,7 @@ impl LanguageRegistry {
name: LanguageName,
grammar_name: Option<Arc<str>>,
matcher: LanguageMatcher,
hidden: bool,
load: Arc<dyn Fn() -> Result<LoadedLanguage> + 'static + Send + Sync>,
) {
let state = &mut *self.state.write();
Expand All @@ -455,6 +461,7 @@ impl LanguageRegistry {
grammar: grammar_name,
matcher,
load,
hidden,
loaded: false,
});
state.version += 1;
Expand Down Expand Up @@ -522,6 +529,7 @@ impl LanguageRegistry {
name: language.name(),
grammar: language.config.grammar.clone(),
matcher: language.config.matcher.clone(),
hidden: language.config.hidden,
load: Arc::new(|| Err(anyhow!("already loaded"))),
loaded: true,
});
Expand Down Expand Up @@ -590,15 +598,12 @@ impl LanguageRegistry {
async move { rx.await? }
}

pub fn available_language_for_name(
self: &Arc<Self>,
name: &LanguageName,
) -> Option<AvailableLanguage> {
pub fn available_language_for_name(self: &Arc<Self>, name: &str) -> Option<AvailableLanguage> {
let state = self.state.read();
state
.available_languages
.iter()
.find(|l| &l.name == name)
.find(|l| l.name.0.as_ref() == name)
.cloned()
}

Expand Down
3 changes: 2 additions & 1 deletion crates/language_extension/src/language_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ impl ExtensionLanguageProxy for LanguageServerRegistryProxy {
language: LanguageName,
grammar: Option<Arc<str>>,
matcher: LanguageMatcher,
hidden: bool,
load: Arc<dyn Fn() -> Result<LoadedLanguage> + Send + Sync + 'static>,
) {
self.language_registry
.register_language(language, grammar, matcher, load);
.register_language(language, grammar, matcher, hidden, load);
}

fn remove_languages(
Expand Down
3 changes: 3 additions & 0 deletions crates/language_selector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ doctest = false
[dependencies]
anyhow.workspace = true
editor.workspace = true
file_finder.workspace = true
file_icons.workspace = true
fuzzy.workspace = true
gpui.workspace = true
language.workspace = true
picker.workspace = true
project.workspace = true
settings.workspace = true
ui.workspace = true
util.workspace = true
workspace.workspace = true
Expand Down
76 changes: 66 additions & 10 deletions crates/language_selector/src/language_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ mod active_buffer_language;
pub use active_buffer_language::ActiveBufferLanguage;
use anyhow::anyhow;
use editor::Editor;
use file_finder::file_finder_settings::FileFinderSettings;
use file_icons::FileIcons;
use fuzzy::{match_strings, StringMatch, StringMatchCandidate};
use gpui::{
actions, AppContext, DismissEvent, EventEmitter, FocusHandle, FocusableView, Model,
ParentElement, Render, Styled, View, ViewContext, VisualContext, WeakView,
};
use language::{Buffer, LanguageRegistry};
use language::{Buffer, LanguageMatcher, LanguageName, LanguageRegistry};
use picker::{Picker, PickerDelegate};
use project::Project;
use std::sync::Arc;
use settings::Settings;
use std::{ops::Not as _, path::Path, sync::Arc};
use ui::{prelude::*, HighlightedLabel, ListItem, ListItemSpacing};
use util::ResultExt;
use workspace::{ModalView, Workspace};
Expand Down Expand Up @@ -102,7 +105,13 @@ impl LanguageSelectorDelegate {
.language_names()
.into_iter()
.enumerate()
.map(|(candidate_id, name)| StringMatchCandidate::new(candidate_id, name))
.filter_map(|(candidate_id, name)| {
language_registry
.available_language_for_name(&name)?
.hidden()
.not()
.then(|| StringMatchCandidate::new(candidate_id, name))
})
.collect::<Vec<_>>();

Self {
Expand All @@ -115,13 +124,64 @@ impl LanguageSelectorDelegate {
selected_index: 0,
}
}

fn language_data_for_match(
&self,
mat: &StringMatch,
cx: &AppContext,
) -> (String, Option<Icon>) {
let mut label = mat.string.clone();
let buffer_language = self.buffer.read(cx).language();
let need_icon = FileFinderSettings::get_global(cx).file_icons;
if let Some(buffer_language) = buffer_language {
let buffer_language_name = buffer_language.name();
if buffer_language_name.0.as_ref() == mat.string.as_str() {
label.push_str(" (current)");
let icon = need_icon
.then(|| self.language_icon(&buffer_language.config().matcher, cx))
.flatten();
return (label, icon);
}
}

if need_icon {
let language_name = LanguageName::new(mat.string.as_str());
match self
.language_registry
.available_language_for_name(&language_name.0)
{
Some(available_language) => {
let icon = self.language_icon(available_language.matcher(), cx);
(label, icon)
}
None => (label, None),
}
} else {
(label, None)
}
}

fn language_icon(&self, matcher: &LanguageMatcher, cx: &AppContext) -> Option<Icon> {
matcher
.path_suffixes
.iter()
.find_map(|extension| {
if extension.contains('.') {
None
} else {
FileIcons::get_icon(Path::new(&format!("file.{extension}")), cx)
}
})
.map(Icon::from_path)
.map(|icon| icon.color(Color::Muted))
}
}

impl PickerDelegate for LanguageSelectorDelegate {
type ListItem = ListItem;

fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
"Select a language...".into()
"Select a language".into()
}

fn match_count(&self) -> usize {
Expand Down Expand Up @@ -215,17 +275,13 @@ impl PickerDelegate for LanguageSelectorDelegate {
cx: &mut ViewContext<Picker<Self>>,
) -> Option<Self::ListItem> {
let mat = &self.matches[ix];
let buffer_language_name = self.buffer.read(cx).language().map(|l| l.name());
let mut label = mat.string.clone();
if buffer_language_name.map(|n| n.0).as_deref() == Some(mat.string.as_str()) {
label.push_str(" (current)");
}

let (label, language_icon) = self.language_data_for_match(mat, cx);
Some(
ListItem::new(ix)
.inset(true)
.spacing(ListItemSpacing::Sparse)
.selected(selected)
.start_slot::<Icon>(language_icon)
.child(HighlightedLabel::new(label, mat.positions.clone())),
)
}
Expand Down
1 change: 1 addition & 0 deletions crates/languages/src/jsdoc/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ brackets = [
{ start = "{", end = "}", close = true, newline = false },
{ start = "[", end = "]", close = true, newline = false },
]
hidden = true
Loading

0 comments on commit f795ce9

Please sign in to comment.