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 glob to match filetype #10960

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions crates/language/src/language_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use futures::{
future::Shared,
Future, FutureExt as _,
};
use globset::GlobSet;
use gpui::{AppContext, BackgroundExecutor, Task};
use lsp::LanguageServerId;
use parking_lot::{Mutex, RwLock};
Expand Down Expand Up @@ -453,7 +454,7 @@ impl LanguageRegistry {
self.language_for_file_internal(
&file.full_path(cx),
content,
Some(&user_file_types.file_types),
user_file_types.file_types.as_ref(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
user_file_types.file_types.as_ref(),
Some(&user_file_types.file_types),

)
}

Expand All @@ -469,12 +470,11 @@ impl LanguageRegistry {
self: &Arc<Self>,
path: &Path,
content: Option<&Rope>,
user_file_types: Option<&HashMap<Arc<str>, Vec<String>>>,
user_file_types: Option<&HashMap<Arc<str>, GlobSet>>,
) -> impl Future<Output = Result<Arc<Language>>> {
let filename = path.file_name().and_then(|name| name.to_str());
let extension = path.extension_or_hidden_file_name();
let path_suffixes = [extension, filename];
let empty = Vec::new();

let rx = self.get_or_load_language(move |language_name, config| {
let path_matches_default_suffix = config
Expand All @@ -483,9 +483,11 @@ impl LanguageRegistry {
.any(|suffix| path_suffixes.contains(&Some(suffix.as_str())));
let path_matches_custom_suffix = user_file_types
.and_then(|types| types.get(language_name))
.unwrap_or(&empty)
.iter()
.any(|suffix| path_suffixes.contains(&Some(suffix.as_str())));
.map_or(false, |globset| {
path_suffixes
.iter()
.any(|&suffix| suffix.map_or(false, |suffix| globset.is_match(suffix)))
});
let content_matches = content.zip(config.first_line_pattern.as_ref()).map_or(
false,
|(content, pattern)| {
Expand Down
16 changes: 10 additions & 6 deletions crates/language/src/language_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{File, Language, LanguageServerName};
use anyhow::Result;
use collections::{HashMap, HashSet};
use globset::GlobMatcher;
use globset::{Glob, GlobMatcher, GlobSet, GlobSetBuilder};
use gpui::AppContext;
use itertools::{Either, Itertools};
use schemars::{
Expand Down Expand Up @@ -54,7 +54,7 @@ pub struct AllLanguageSettings {
pub copilot: CopilotSettings,
defaults: LanguageSettings,
languages: HashMap<Arc<str>, LanguageSettings>,
pub(crate) file_types: HashMap<Arc<str>, Vec<String>>,
pub(crate) file_types: Option<HashMap<Arc<str>, GlobSet>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub(crate) file_types: Option<HashMap<Arc<str>, GlobSet>>,
pub(crate) file_types: HashMap<Arc<str>, GlobSet>,

}

/// The settings for a particular language.
Expand Down Expand Up @@ -563,7 +563,7 @@ impl settings::Settings for AllLanguageSettings {
.and_then(|c| c.disabled_globs.as_ref())
.ok_or_else(Self::missing_default)?;

let mut file_types: HashMap<Arc<str>, Vec<String>> = HashMap::default();
let mut file_types: Option<HashMap<Arc<str>, GlobSet>> = Some(HashMap::default());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut file_types: Option<HashMap<Arc<str>, GlobSet>> = Some(HashMap::default());
let mut file_types: HashMap<Arc<str>, GlobSet> = HashMap::default();

I'm pretty sure that we may be able to omit the type altogether, though I can't check that rn:

Suggested change
let mut file_types: Option<HashMap<Arc<str>, GlobSet>> = Some(HashMap::default());
let mut file_types = HashMap::default();

for user_settings in sources.customizations() {
if let Some(copilot) = user_settings.features.as_ref().and_then(|f| f.copilot) {
copilot_enabled = copilot;
Expand Down Expand Up @@ -594,10 +594,14 @@ impl settings::Settings for AllLanguageSettings {
}

for (language, suffixes) in &user_settings.file_types {
let mut builder = GlobSetBuilder::new();
for suffix in suffixes {
builder.add(Glob::new(suffix)?);
}
file_types
.entry(language.clone())
.or_default()
.extend_from_slice(suffixes);
.as_mut()
.unwrap()
.insert(language.clone(), builder.build()?);
Comment on lines +602 to +604
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.as_mut()
.unwrap()
.insert(language.clone(), builder.build()?);
.insert(language.clone(), builder.build()?);

}
}

Expand Down