Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add language icons to the language selector #21298

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
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.

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
4 changes: 4 additions & 0 deletions crates/language/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,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
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
62 changes: 53 additions & 9 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::{path::Path, sync::Arc};
use ui::{prelude::*, HighlightedLabel, ListItem, ListItemSpacing};
use util::ResultExt;
use workspace::{ModalView, Workspace};
Expand Down Expand Up @@ -115,13 +118,58 @@ 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)
{
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| 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 +263,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
Loading