languages: Remove a eager conversion from LanguageName to String (#35667)

This PR changes the signature of `language_names` from

```rust
pub fn language_names(&self) -> Vec<String>
// Into
pub fn language_names(&self) -> Vec<LanguageName>
```

The function previously eagerly converted `LanguageName`'s to
`String`'s, which requires the reallocation of all of the elements. The
functions get called in many places in the code base, but only one of
which actually requires the conversion to a `String`. In one case it
would do a `SharedString` -> `String` -> `SharedString` conversion,
which is now totally bypassed.

Release Notes:

- N/A
This commit is contained in:
tidely 2025-08-05 23:46:57 +03:00 committed by GitHub
parent a508a9536f
commit c595ed19d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 44 additions and 28 deletions

View file

@ -2353,9 +2353,9 @@ mod tests {
assert_eq!(
languages.language_names(),
&[
"JSON".to_string(),
"Plain Text".to_string(),
"Rust".to_string(),
LanguageName::new("JSON"),
LanguageName::new("Plain Text"),
LanguageName::new("Rust"),
]
);
@ -2366,9 +2366,9 @@ mod tests {
assert_eq!(
languages.language_names(),
&[
"JSON".to_string(),
"Plain Text".to_string(),
"Rust".to_string(),
LanguageName::new("JSON"),
LanguageName::new("Plain Text"),
LanguageName::new("Rust"),
]
);
@ -2379,9 +2379,9 @@ mod tests {
assert_eq!(
languages.language_names(),
&[
"JSON".to_string(),
"Plain Text".to_string(),
"Rust".to_string(),
LanguageName::new("JSON"),
LanguageName::new("Plain Text"),
LanguageName::new("Rust"),
]
);

View file

@ -547,15 +547,15 @@ impl LanguageRegistry {
self.state.read().language_settings.clone()
}
pub fn language_names(&self) -> Vec<String> {
pub fn language_names(&self) -> Vec<LanguageName> {
let state = self.state.read();
let mut result = state
.available_languages
.iter()
.filter_map(|l| l.loaded.not().then_some(l.name.to_string()))
.chain(state.languages.iter().map(|l| l.config.name.to_string()))
.filter_map(|l| l.loaded.not().then_some(l.name.clone()))
.chain(state.languages.iter().map(|l| l.config.name.clone()))
.collect::<Vec<_>>();
result.sort_unstable_by_key(|language_name| language_name.to_lowercase());
result.sort_unstable_by_key(|language_name| language_name.as_ref().to_lowercase());
result
}