Refactor to use SharedString in more places (#23813)

Splitting this off from
https://github.com/zed-industries/zed/pull/23808, per @maxdeviant's
suggestion!

Release Notes:

- N/A

---------

Co-authored-by: Nathan <nathan@zed.dev>
This commit is contained in:
Richard Feldman 2025-01-28 19:04:21 -05:00 committed by GitHub
parent 92a1cb893f
commit 33d1145c3f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 45 additions and 25 deletions

View file

@ -2424,7 +2424,7 @@ fn test_language_at_with_hidden_languages(cx: &mut App) {
assert_eq!(config.language_name(), "Markdown".into());
let language = snapshot.language_at(point).unwrap();
assert_eq!(language.name().0.as_ref(), "Markdown");
assert_eq!(language.name().as_ref(), "Markdown");
}
buffer

View file

@ -256,7 +256,7 @@ impl CachedLspAdapter {
pub fn language_id(&self, language_name: &LanguageName) -> String {
self.language_ids
.get(language_name.0.as_ref())
.get(language_name.as_ref())
.cloned()
.unwrap_or_else(|| language_name.lsp_id())
}
@ -1462,7 +1462,7 @@ impl Language {
self.config
.code_fence_block_name
.clone()
.unwrap_or_else(|| self.config.name.0.to_lowercase().into())
.unwrap_or_else(|| self.config.name.as_ref().to_lowercase().into())
}
pub fn context_provider(&self) -> Option<Arc<dyn ContextProvider>> {

View file

@ -14,7 +14,7 @@ use futures::{
Future,
};
use globset::GlobSet;
use gpui::{App, BackgroundExecutor};
use gpui::{App, BackgroundExecutor, SharedString};
use lsp::LanguageServerId;
use parking_lot::{Mutex, RwLock};
use postage::watch;
@ -36,15 +36,15 @@ use util::{maybe, paths::PathExt, post_inc, ResultExt};
#[derive(
Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, JsonSchema,
)]
pub struct LanguageName(pub Arc<str>);
pub struct LanguageName(SharedString);
impl LanguageName {
pub fn new(s: &str) -> Self {
Self(Arc::from(s))
Self(SharedString::new(s))
}
pub fn from_proto(s: String) -> Self {
Self(Arc::from(s))
Self(SharedString::from(s))
}
pub fn to_proto(self) -> String {
self.0.to_string()
@ -57,6 +57,18 @@ impl LanguageName {
}
}
impl From<LanguageName> for SharedString {
fn from(value: LanguageName) -> Self {
value.0
}
}
impl AsRef<str> for LanguageName {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
impl Borrow<str> for LanguageName {
fn borrow(&self) -> &str {
self.0.as_ref()
@ -71,7 +83,7 @@ impl std::fmt::Display for LanguageName {
impl<'a> From<&'a str> for LanguageName {
fn from(str: &'a str) -> LanguageName {
LanguageName(str.into())
LanguageName(SharedString::new(str))
}
}
@ -657,7 +669,7 @@ impl LanguageRegistry {
.iter()
.any(|suffix| path_suffixes.contains(&Some(suffix.as_str())));
let custom_suffixes = user_file_types
.and_then(|types| types.get(&language_name.0))
.and_then(|types| types.get(language_name.as_ref()))
.unwrap_or(&empty);
let path_matches_custom_suffix = path_suffixes
.iter()