1use anyhow::{Context as _, Result};
2use async_trait::async_trait;
3use futures::StreamExt;
4use gpui::AsyncApp;
5use language::{LspAdapter, LspAdapterDelegate, LspInstaller, Toolchain};
6use lsp::{LanguageServerBinary, LanguageServerName};
7use node_runtime::{NodeRuntime, VersionStrategy};
8use project::lsp_store::language_server_settings;
9use serde_json::json;
10use smol::fs;
11use std::{
12 ffi::OsString,
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 = String;
38
39 async fn fetch_latest_server_version(
40 &self,
41 _: &dyn LspAdapterDelegate,
42 _: bool,
43 _: &mut AsyncApp,
44 ) -> Result<String> {
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: &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 async fn fetch_server_binary(
69 &self,
70 latest_version: String,
71 container_dir: PathBuf,
72 _: &dyn LspAdapterDelegate,
73 ) -> Result<LanguageServerBinary> {
74 let server_path = container_dir.join(SERVER_PATH);
75
76 self.node
77 .npm_install_packages(
78 &container_dir,
79 &[(Self::PACKAGE_NAME, latest_version.as_str())],
80 )
81 .await?;
82
83 Ok(LanguageServerBinary {
84 path: self.node.binary_path().await?,
85 env: None,
86 arguments: server_binary_arguments(&server_path),
87 })
88 }
89
90 async fn check_if_version_installed(
91 &self,
92 version: &String,
93 container_dir: &PathBuf,
94 _: &dyn LspAdapterDelegate,
95 ) -> Option<LanguageServerBinary> {
96 let server_path = container_dir.join(SERVER_PATH);
97
98 let should_install_language_server = self
99 .node
100 .should_install_npm_package(
101 Self::PACKAGE_NAME,
102 &server_path,
103 container_dir,
104 VersionStrategy::Latest(version),
105 )
106 .await;
107
108 if should_install_language_server {
109 None
110 } else {
111 Some(LanguageServerBinary {
112 path: self.node.binary_path().await.ok()?,
113 env: None,
114 arguments: server_binary_arguments(&server_path),
115 })
116 }
117 }
118
119 async fn cached_server_binary(
120 &self,
121 container_dir: PathBuf,
122 _: &dyn LspAdapterDelegate,
123 ) -> Option<LanguageServerBinary> {
124 get_cached_server_binary(container_dir, &self.node).await
125 }
126}
127
128#[async_trait(?Send)]
129impl LspAdapter for CssLspAdapter {
130 fn name(&self) -> LanguageServerName {
131 LanguageServerName("vscode-css-language-server".into())
132 }
133
134 async fn initialization_options(
135 self: Arc<Self>,
136 _: &Arc<dyn LspAdapterDelegate>,
137 ) -> Result<Option<serde_json::Value>> {
138 Ok(Some(json!({
139 "provideFormatter": true
140 })))
141 }
142
143 async fn workspace_configuration(
144 self: Arc<Self>,
145 delegate: &Arc<dyn LspAdapterDelegate>,
146 _: Option<Toolchain>,
147 cx: &mut AsyncApp,
148 ) -> Result<serde_json::Value> {
149 let mut default_config = json!({
150 "css": {
151 "lint": {}
152 },
153 "less": {
154 "lint": {}
155 },
156 "scss": {
157 "lint": {}
158 }
159 });
160
161 let project_options = cx.update(|cx| {
162 language_server_settings(delegate.as_ref(), &self.name(), cx)
163 .and_then(|s| s.settings.clone())
164 })?;
165
166 if let Some(override_options) = project_options {
167 merge_json_value_into(override_options, &mut default_config);
168 }
169
170 Ok(default_config)
171 }
172}
173
174async fn get_cached_server_binary(
175 container_dir: PathBuf,
176 node: &NodeRuntime,
177) -> Option<LanguageServerBinary> {
178 maybe!(async {
179 let mut last_version_dir = None;
180 let mut entries = fs::read_dir(&container_dir).await?;
181 while let Some(entry) = entries.next().await {
182 let entry = entry?;
183 if entry.file_type().await?.is_dir() {
184 last_version_dir = Some(entry.path());
185 }
186 }
187 let last_version_dir = last_version_dir.context("no cached binary")?;
188 let server_path = last_version_dir.join(SERVER_PATH);
189 anyhow::ensure!(
190 server_path.exists(),
191 "missing executable in directory {last_version_dir:?}"
192 );
193 Ok(LanguageServerBinary {
194 path: node.binary_path().await?,
195 env: None,
196 arguments: server_binary_arguments(&server_path),
197 })
198 })
199 .await
200 .log_err()
201}
202
203#[cfg(test)]
204mod tests {
205 use gpui::{AppContext as _, TestAppContext};
206 use unindent::Unindent;
207
208 #[gpui::test]
209 async fn test_outline(cx: &mut TestAppContext) {
210 let language = crate::language("css", tree_sitter_css::LANGUAGE.into());
211
212 let text = r#"
213 /* Import statement */
214 @import './fonts.css';
215
216 /* multiline list of selectors with nesting */
217 .test-class,
218 div {
219 .nested-class {
220 color: red;
221 }
222 }
223
224 /* descendant selectors */
225 .test .descendant {}
226
227 /* pseudo */
228 .test:not(:hover) {}
229
230 /* media queries */
231 @media screen and (min-width: 3000px) {
232 .desktop-class {}
233 }
234 "#
235 .unindent();
236
237 let buffer = cx.new(|cx| language::Buffer::local(text, cx).with_language(language, cx));
238 let outline = buffer.read_with(cx, |buffer, _| buffer.snapshot().outline(None));
239 assert_eq!(
240 outline
241 .items
242 .iter()
243 .map(|item| (item.text.as_str(), item.depth))
244 .collect::<Vec<_>>(),
245 &[
246 ("@import './fonts.css'", 0),
247 (".test-class, div", 0),
248 (".nested-class", 1),
249 (".test .descendant", 0),
250 (".test:not(:hover)", 0),
251 ("@media screen and (min-width: 3000px)", 0),
252 (".desktop-class", 1),
253 ]
254 );
255 }
256}