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 2 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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/language/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ collections.workspace = true
futures.workspace = true
fuzzy.workspace = true
git.workspace = true
glob = "0.3.1"
globset.workspace = true
gpui.workspace = true
itertools.workspace = true
Expand Down
12 changes: 11 additions & 1 deletion 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 glob::Pattern;
use gpui::{AppContext, BackgroundExecutor, Task};
use lsp::LanguageServerId;
use parking_lot::{Mutex, RwLock};
Expand Down Expand Up @@ -485,7 +486,16 @@ impl LanguageRegistry {
.and_then(|types| types.get(language_name))
.unwrap_or(&empty)
.iter()
.any(|suffix| path_suffixes.contains(&Some(suffix.as_str())));
.any(|suffix| {
path_suffixes.iter().any(|path_suffix| {
Copy link
Contributor

@osiewicz osiewicz Apr 25, 2024

Choose a reason for hiding this comment

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

Here we'd be parsing globs on each invokation of language_for_file_internal, which is a pretty hot function; I think a better option would be to change type of user_file_types to Option<&HashMap<Arc<str>, Vec<Pattern>>> (and the corresponding type in settings: https://github.com/zed-industries/zed/blob/main/crates/language/src/language_settings.rs#L57) and do the parsing in settings deserialization (https://github.com/zed-industries/zed/blob/main/crates/language/src/language_settings.rs#L566). That way, invalid globs will be rejected at settings load time, which should be better.

Also, mea culpa but we already have a dependency for glob parsing (globset) - we can use that instead of glob as it should also be presumably more efficient for our use case (of multiple globs) with GlobSet. This is my fault, as I've suggested using glob initially. Sorry!
Thus, user_file_types will be HashMap<Arc<str>, GlobSet> in the end.

Overall though this looks good to me; once we move glob parsing to Settings-load time, we should be good to merge :)

Copy link
Author

Choose a reason for hiding this comment

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

I started refactor the code to accept Option<&HashMap<Arc<str>, Vec<Pattern>>> on AllLanguageSettings but I faced some issues to add the Lifetime parameter (need a lot of refactors to add it), so I add the type of file_types as Option<HashMap<Arc<str>, GlobSet>>, treated it on impl settings::Settings for AllLanguageSettings method like this:

            for (language, suffixes) in &user_settings.file_types {
                let mut builder = GlobSetBuilder::new();
                for suffix in suffixes {
                    builder.add(Glob::new(suffix)?);
                }
                file_types
                    .as_mut()
                    .unwrap()
                    .insert(language.clone(), builder.build()?);
            }

With this I need get the reference on

pub fn language_for_file(
using user_file_types.file_types.as_ref() and refactor the path_matches_custom_suffix to be something like:

            let path_matches_custom_suffix = user_file_types
                .and_then(|types| types.get(language_name))
                .map_or(false, |globset| {
                    path_suffixes
                        .iter()
                        .any(|&suffix| suffix.map_or(false, |suffix| globset.is_match(suffix)))
                });

I'll push the changes, if have some appointments, how I said, I'm a newcomer and I little bit afraid to make some big changes on the code.

Copy link
Contributor

@osiewicz osiewicz Apr 28, 2024

Choose a reason for hiding this comment

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

Yeah, what you have done is pretty much fine; I didn't mean to use a reference type on the AllLanguageSettings itself, since - as you've pointed out - we'd have to carry that lifetime around everywhere, and that would be a pain in the bum.

if let Some(suffix_str) = path_suffix {
Pattern::new(suffix)
.map_or(false, |pattern| pattern.matches(suffix_str))
} else {
false
}
})
});
let content_matches = content.zip(config.first_line_pattern.as_ref()).map_or(
false,
|(content, pattern)| {
Expand Down