Skip to content

Commit 4e610b3

Browse files
committed
Implement LspContext::get_environment, to list & document builtin globals
1 parent e853969 commit 4e610b3

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

app/buck2_server/src/lsp.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl DocsCacheManager {
147147
let mut builtin_docs = Vec::new();
148148

149149
let cell_resolver = dice_ctx.get_cell_resolver().await?;
150-
builtin_docs.push((None, get_builtin_globals_docs(dice_ctx).await?));
150+
builtin_docs.push((None, get_builtin_globals_docs(dice_ctx).await?.clone()));
151151

152152
let builtin_names = builtin_docs
153153
.iter()
@@ -205,6 +205,10 @@ struct DocsCache {
205205
global_urls: HashMap<String, LspUrl>,
206206
/// Mapping of starlark: urls to a synthesized starlark representation.
207207
native_starlark_files: HashMap<LspUrl, String>,
208+
/// A DocModule for all the globals, without prelude symbols.
209+
builtin_docs: DocModule,
210+
/// A DocModule for build files specifically, with prelude symbols.
211+
buildfile_docs: DocModule,
208212
}
209213

210214
#[derive(buck2_error::Error, Debug)]
@@ -267,12 +271,18 @@ impl DocsCache {
267271
};
268272

269273
let mut native_starlark_files = HashMap::new();
274+
275+
let mut builtin_docs = DocModule::default();
276+
let mut buildfile_docs = DocModule::default();
277+
270278
for (import_path, docs) in builtin_symbols {
271279
match import_path {
272280
Some(l) => {
273281
let url = location_lookup(l).await?;
274-
for (sym, _) in &docs.members {
282+
for (sym, mem) in &docs.members {
275283
insert_global(sym.clone(), url.clone())?;
284+
// Only for buildfiles, as this is a prelude symbol
285+
buildfile_docs.members.insert(sym.clone(), mem.clone());
276286
}
277287
}
278288
None => {
@@ -294,13 +304,17 @@ impl DocsCache {
294304
assert!(prev.is_none());
295305

296306
insert_global(sym.clone(), url)?;
307+
builtin_docs.members.insert(sym.clone(), mem.clone());
308+
buildfile_docs.members.insert(sym.clone(), mem.clone());
297309
}
298310
}
299311
};
300312
}
301313
Ok(Self {
302314
global_urls,
303315
native_starlark_files,
316+
builtin_docs,
317+
buildfile_docs,
304318
})
305319
}
306320

@@ -933,8 +947,32 @@ impl LspContext for BuckLspContext<'_> {
933947
.into())
934948
}
935949

936-
fn get_environment(&self, _uri: &LspUrl) -> DocModule {
937-
DocModule::default()
950+
fn get_environment(&self, url: &LspUrl) -> DocModule {
951+
let dispatcher = self.server_ctx.events().dupe();
952+
self.runtime
953+
.block_on(with_dispatcher_async(dispatcher, async {
954+
let Ok(import_path) = self.import_path_from_url(url).await else {
955+
return Ok(DocModule::default());
956+
};
957+
958+
// TODO: investigate whether this performs badly when editing the prelude
959+
// cell itself. We are recomputing the prelude cell docs whether they are
960+
// needed or not. Right here, we know whether the current file is in the prelude
961+
// and we can behave differently in that case.
962+
let docs_cache = self
963+
.with_dice_ctx(|dice_ctx| async move {
964+
self.docs_cache_manager.get_cache(dice_ctx).await
965+
})
966+
.await?;
967+
968+
let docs = match import_path.borrow() {
969+
StarlarkPath::BuildFile(_) => docs_cache.buildfile_docs.clone(),
970+
_ => docs_cache.builtin_docs.clone(),
971+
};
972+
973+
buck2_error::Ok(docs)
974+
}))
975+
.unwrap_or_default()
938976
}
939977
}
940978

0 commit comments

Comments
 (0)