language_name.rs

  1use gpui::SharedString;
  2use schemars::JsonSchema;
  3use serde::{Deserialize, Serialize};
  4use std::{
  5    borrow::Borrow,
  6    sync::atomic::{AtomicUsize, Ordering::SeqCst},
  7};
  8
  9static NEXT_LANGUAGE_ID: AtomicUsize = AtomicUsize::new(0);
 10
 11#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
 12pub struct LanguageId(usize);
 13
 14impl LanguageId {
 15    pub fn new() -> Self {
 16        Self(NEXT_LANGUAGE_ID.fetch_add(1, SeqCst))
 17    }
 18}
 19
 20impl Default for LanguageId {
 21    fn default() -> Self {
 22        Self::new()
 23    }
 24}
 25
 26#[derive(
 27    Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, JsonSchema,
 28)]
 29pub struct LanguageName(pub SharedString);
 30
 31impl LanguageName {
 32    pub fn new(s: &str) -> Self {
 33        Self(SharedString::new(s))
 34    }
 35
 36    pub fn new_static(s: &'static str) -> Self {
 37        Self(SharedString::new_static(s))
 38    }
 39
 40    pub fn from_proto(s: String) -> Self {
 41        Self(SharedString::from(s))
 42    }
 43
 44    pub fn to_proto(&self) -> String {
 45        self.0.to_string()
 46    }
 47
 48    pub fn lsp_id(&self) -> String {
 49        match self.0.as_ref() {
 50            "Plain Text" => "plaintext".to_string(),
 51            language_name => language_name.to_lowercase(),
 52        }
 53    }
 54}
 55
 56impl From<LanguageName> for SharedString {
 57    fn from(value: LanguageName) -> Self {
 58        value.0
 59    }
 60}
 61
 62impl From<SharedString> for LanguageName {
 63    fn from(value: SharedString) -> Self {
 64        LanguageName(value)
 65    }
 66}
 67
 68impl AsRef<str> for LanguageName {
 69    fn as_ref(&self) -> &str {
 70        self.0.as_ref()
 71    }
 72}
 73
 74impl Borrow<str> for LanguageName {
 75    fn borrow(&self) -> &str {
 76        self.0.as_ref()
 77    }
 78}
 79
 80impl PartialEq<str> for LanguageName {
 81    fn eq(&self, other: &str) -> bool {
 82        self.0.as_ref() == other
 83    }
 84}
 85
 86impl PartialEq<&str> for LanguageName {
 87    fn eq(&self, other: &&str) -> bool {
 88        self.0.as_ref() == *other
 89    }
 90}
 91
 92impl std::fmt::Display for LanguageName {
 93    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
 94        write!(f, "{}", self.0)
 95    }
 96}
 97
 98impl From<&'static str> for LanguageName {
 99    fn from(str: &'static str) -> Self {
100        Self(SharedString::new_static(str))
101    }
102}
103
104impl From<LanguageName> for String {
105    fn from(value: LanguageName) -> Self {
106        let value: &str = &value.0;
107        Self::from(value)
108    }
109}