Skip to repository content255 lines · 7.2 KB · rust
tenant.openagents/omega
No repository description is available.
OpenAgents Git authority 2026-07-28T03:29:43.116Z 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
css.rs
1use anyhow::Result;
2use async_trait::async_trait;
3use gpui::AsyncApp;
4use language::{LspAdapter, LspAdapterDelegate, LspInstaller, Toolchain};
5use lsp::{LanguageServerBinary, LanguageServerName, Uri};
6use node_runtime::{NodeRuntime, VersionStrategy};
7use project::lsp_store::language_server_settings;
8use semver::Version;
9use serde_json::json;
10use std::{
11 ffi::OsString,
12 future::Future,
13 path::{Path, PathBuf},
14 sync::Arc,
15};
16use util::{ResultExt, maybe, merge_json_value_into};
17
18const SERVER_PATH: &str =
19 "node_modules/vscode-langservers-extracted/bin/vscode-css-language-server";
20
21fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
22 vec![server_path.into(), "--stdio".into()]
23}
24
25pub struct CssLspAdapter {
26 node: NodeRuntime,
27}
28
29impl CssLspAdapter {
30 const PACKAGE_NAME: &str = "vscode-langservers-extracted";
31 pub fn new(node: NodeRuntime) -> Self {
32 CssLspAdapter { node }
33 }
34}
35
36impl LspInstaller for CssLspAdapter {
37 type BinaryVersion = Version;
38
39 async fn fetch_latest_server_version(
40 &self,
41 _: &Arc<dyn LspAdapterDelegate>,
42 _: bool,
43 _: &mut AsyncApp,
44 ) -> Result<Self::BinaryVersion> {
45 self.node
46 .npm_package_latest_version("vscode-langservers-extracted")
47 .await
48 }
49
50 async fn check_if_user_installed(
51 &self,
52 delegate: &Arc<dyn LspAdapterDelegate>,
53 _: Option<Toolchain>,
54 _: &AsyncApp,
55 ) -> Option<LanguageServerBinary> {
56 let path = delegate
57 .which("vscode-css-language-server".as_ref())
58 .await?;
59 let env = delegate.shell_env().await;
60
61 Some(LanguageServerBinary {
62 path,
63 env: Some(env),
64 arguments: vec!["--stdio".into()],
65 })
66 }
67
68 fn fetch_server_binary(
69 &self,
70 _latest_version: Self::BinaryVersion,
71 container_dir: PathBuf,
72 _: &Arc<dyn LspAdapterDelegate>,
73 ) -> impl Send + Future<Output = Result<LanguageServerBinary>> + use<> {
74 let node = self.node.clone();
75
76 async move {
77 let server_path = container_dir.join(SERVER_PATH);
78
79 node.npm_install_latest_packages(&container_dir, &[Self::PACKAGE_NAME])
80 .await?;
81
82 Ok(LanguageServerBinary {
83 path: node.binary_path().await?,
84 env: None,
85 arguments: server_binary_arguments(&server_path),
86 })
87 }
88 }
89
90 fn check_if_version_installed(
91 &self,
92 version: &Self::BinaryVersion,
93 container_dir: &PathBuf,
94 _: &Arc<dyn LspAdapterDelegate>,
95 ) -> impl Send + Future<Output = Option<LanguageServerBinary>> + use<> {
96 let node = self.node.clone();
97 let version = version.clone();
98 let container_dir = container_dir.clone();
99
100 async move {
101 let server_path = container_dir.join(SERVER_PATH);
102
103 let should_install_language_server = node
104 .should_install_npm_package(
105 Self::PACKAGE_NAME,
106 &server_path,
107 &container_dir,
108 VersionStrategy::Latest(&version),
109 )
110 .await;
111
112 if should_install_language_server {
113 None
114 } else {
115 Some(LanguageServerBinary {
116 path: node.binary_path().await.ok()?,
117 env: None,
118 arguments: server_binary_arguments(&server_path),
119 })
120 }
121 }
122 }
123
124 async fn cached_server_binary(
125 &self,
126 container_dir: PathBuf,
127 _: &dyn LspAdapterDelegate,
128 ) -> Option<LanguageServerBinary> {
129 get_cached_server_binary(container_dir, &self.node).await
130 }
131}
132
133#[async_trait(?Send)]
134impl LspAdapter for CssLspAdapter {
135 fn name(&self) -> LanguageServerName {
136 LanguageServerName("vscode-css-language-server".into())
137 }
138
139 async fn initialization_options(
140 self: Arc<Self>,
141 _: &Arc<dyn LspAdapterDelegate>,
142 _: &mut AsyncApp,
143 ) -> Result<Option<serde_json::Value>> {
144 Ok(Some(json!({
145 "provideFormatter": true
146 })))
147 }
148
149 async fn workspace_configuration(
150 self: Arc<Self>,
151 delegate: &Arc<dyn LspAdapterDelegate>,
152 _: Option<Toolchain>,
153 _: Option<Uri>,
154 cx: &mut AsyncApp,
155 ) -> Result<serde_json::Value> {
156 let mut default_config = json!({
157 "css": {
158 "lint": {}
159 },
160 "less": {
161 "lint": {}
162 },
163 "scss": {
164 "lint": {}
165 }
166 });
167
168 let project_options = cx.update(|cx| {
169 language_server_settings(delegate.as_ref(), &self.name(), cx)
170 .and_then(|s| s.settings.clone())
171 });
172
173 if let Some(override_options) = project_options {
174 merge_json_value_into(override_options, &mut default_config);
175 }
176
177 Ok(default_config)
178 }
179}
180
181async fn get_cached_server_binary(
182 container_dir: PathBuf,
183 node: &NodeRuntime,
184) -> Option<LanguageServerBinary> {
185 maybe!(async {
186 let server_path = container_dir.join(SERVER_PATH);
187 anyhow::ensure!(
188 server_path.exists(),
189 "missing executable in directory {server_path:?}"
190 );
191 Ok(LanguageServerBinary {
192 path: node.binary_path().await?,
193 env: None,
194 arguments: server_binary_arguments(&server_path),
195 })
196 })
197 .await
198 .log_err()
199}
200
201#[cfg(test)]
202mod tests {
203 use gpui::{AppContext as _, TestAppContext};
204 use unindent::Unindent;
205
206 #[gpui::test]
207 async fn test_outline(cx: &mut TestAppContext) {
208 let language = crate::language("css", tree_sitter_css::LANGUAGE.into());
209
210 let text = r#"
211 /* Import statement */
212 @import './fonts.css';
213
214 /* multiline list of selectors with nesting */
215 .test-class,
216 div {
217 .nested-class {
218 color: red;
219 }
220 }
221
222 /* descendant selectors */
223 .test .descendant {}
224
225 /* pseudo */
226 .test:not(:hover) {}
227
228 /* media queries */
229 @media screen and (min-width: 3000px) {
230 .desktop-class {}
231 }
232 "#
233 .unindent();
234
235 let buffer = cx.new(|cx| language::Buffer::local(text, cx).with_language(language, cx));
236 let outline = buffer.read_with(cx, |buffer, _| buffer.snapshot().outline(None));
237 assert_eq!(
238 outline
239 .items
240 .iter()
241 .map(|item| (item.text.as_str(), item.depth))
242 .collect::<Vec<_>>(),
243 &[
244 ("@import './fonts.css'", 0),
245 (".test-class, div", 0),
246 (".nested-class", 1),
247 (".test .descendant", 0),
248 (".test:not(:hover)", 0),
249 ("@media screen and (min-width: 3000px)", 0),
250 (".desktop-class", 1),
251 ]
252 );
253 }
254}
255