Skip to repository content143 lines · 3.6 KB · rust
tenant.openagents/omega
No repository description is available.
OpenAgents Git authority 2026-07-28T01:52:22.926Z Public web read
NIP-34 coordinate
30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omegaMaintainersHidden in public view
References2 branches · 1 tag
Read-only clone
git clone https://openagents.com/git/tenant.openagents/omega.gitBrowse files
language_name.rs
1use gpui_shared_string::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 /// Identifier used to name and look up this language's snippet file.
56 ///
57 /// Path separators are stripped from the `lsp_id` because the snippet file
58 /// is stored as a flat file directly under the snippets directory; a `/`
59 /// (or `\`) would otherwise place it in a subdirectory that is never
60 /// scanned, making the snippet impossible to use.
61 pub fn snippet_scope_id(&self) -> String {
62 self.lsp_id().replace(['/', '\\'], "")
63 }
64}
65
66impl From<LanguageName> for SharedString {
67 fn from(value: LanguageName) -> Self {
68 value.0
69 }
70}
71
72impl From<SharedString> for LanguageName {
73 fn from(value: SharedString) -> Self {
74 LanguageName(value)
75 }
76}
77
78impl AsRef<str> for LanguageName {
79 fn as_ref(&self) -> &str {
80 self.0.as_ref()
81 }
82}
83
84impl Borrow<str> for LanguageName {
85 fn borrow(&self) -> &str {
86 self.0.as_ref()
87 }
88}
89
90impl PartialEq<str> for LanguageName {
91 fn eq(&self, other: &str) -> bool {
92 self.0.as_ref() == other
93 }
94}
95
96impl PartialEq<&str> for LanguageName {
97 fn eq(&self, other: &&str) -> bool {
98 self.0.as_ref() == *other
99 }
100}
101
102impl std::fmt::Display for LanguageName {
103 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
104 write!(f, "{}", self.0)
105 }
106}
107
108impl From<&'static str> for LanguageName {
109 fn from(str: &'static str) -> Self {
110 Self(SharedString::new_static(str))
111 }
112}
113
114impl From<LanguageName> for String {
115 fn from(value: LanguageName) -> Self {
116 let value: &str = &value.0;
117 Self::from(value)
118 }
119}
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124
125 #[test]
126 fn snippet_scope_id_strips_path_separators() {
127 // A `/` or `\` in the language name would route the snippet file into a
128 // subdirectory that is never scanned (see issue #59620).
129 assert_eq!(LanguageName::new("PL/X").snippet_scope_id(), "plx");
130 assert_eq!(LanguageName::new("a\\b").snippet_scope_id(), "ab");
131 // Names without separators are unchanged beyond `lsp_id`'s lowercasing.
132 assert_eq!(LanguageName::new("Rust").snippet_scope_id(), "rust");
133 assert_eq!(
134 LanguageName::new("Shell Script").snippet_scope_id(),
135 "shell script"
136 );
137 assert_eq!(
138 LanguageName::new("Plain Text").snippet_scope_id(),
139 "plaintext"
140 );
141 }
142}
143