Skip to content

Commit 4813a8d

Browse files
Natalie Jamesonfacebook-github-bot
authored andcommitted
Add ability to get a URL for a global symbol
Summary: Add the ability to fetch an LspUrl for a given global symbol if it's present. This will be used in future revisions to be able to "navigate" to native symbols. Reviewed By: bobyangyf Differential Revision: D38452372 fbshipit-source-id: 765759d54e6c5adb1076e1a2a5c985b0b3e52cac
1 parent 04cb6c4 commit 4813a8d

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

starlark/bin/eval.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub(crate) struct Context {
5757
pub(crate) prelude: Vec<FrozenModule>,
5858
pub(crate) module: Option<Module>,
5959
pub(crate) builtin_docs: HashMap<LspUrl, String>,
60+
pub(crate) builtin_symbols: HashMap<String, LspUrl>,
6061
}
6162

6263
/// The outcome of evaluating (checking, parsing or running) given starlark code.
@@ -91,8 +92,10 @@ impl Context {
9192
None
9293
};
9394
let mut builtins: HashMap<LspUrl, Vec<Doc>> = HashMap::new();
95+
let mut builtin_symbols: HashMap<String, LspUrl> = HashMap::new();
9496
for doc in get_registered_docs() {
9597
let uri = Self::url_for_doc(&doc);
98+
builtin_symbols.insert(doc.id.name.clone(), uri.clone());
9699
builtins.entry(uri).or_default().push(doc);
97100
}
98101
let builtin_docs = builtins
@@ -106,6 +109,7 @@ impl Context {
106109
prelude,
107110
module,
108111
builtin_docs,
112+
builtin_symbols,
109113
})
110114
}
111115

@@ -302,6 +306,14 @@ impl LspContext for Context {
302306
_ => Err(LoadContentsError::WrongScheme("file://".to_owned(), uri.clone()).into()),
303307
}
304308
}
309+
310+
fn get_url_for_global_symbol(
311+
&self,
312+
_current_file: &LspUrl,
313+
symbol: &str,
314+
) -> anyhow::Result<Option<LspUrl>> {
315+
Ok(self.builtin_symbols.get(symbol).cloned())
316+
}
305317
}
306318

307319
pub(crate) fn globals() -> Globals {

starlark/src/lsp/server.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,16 @@ pub trait LspContext {
279279
.map(|content| self.parse_file_with_contents(uri, content));
280280
Ok(result)
281281
}
282+
283+
/// Get the LSPUrl for a global symbol if possible.
284+
///
285+
/// The current file is provided in case different files have different global symbols
286+
/// defined.
287+
fn get_url_for_global_symbol(
288+
&self,
289+
current_file: &LspUrl,
290+
symbol: &str,
291+
) -> anyhow::Result<Option<LspUrl>>;
282292
}
283293

284294
/// Errors when [`LspContext::resolve_load()`] cannot resolve a given path.

starlark/src/lsp/test.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ struct TestServerContext {
107107
file_contents: Arc<RwLock<HashMap<PathBuf, String>>>,
108108
dirs: Arc<RwLock<HashSet<PathBuf>>>,
109109
builtin_docs: Arc<HashMap<LspUrl, String>>,
110+
builtin_symbols: Arc<HashMap<String, LspUrl>>,
110111
}
111112

112113
impl LspContext for TestServerContext {
@@ -208,6 +209,14 @@ impl LspContext for TestServerContext {
208209
_ => Ok(None),
209210
}
210211
}
212+
213+
fn get_url_for_global_symbol(
214+
&self,
215+
_current_file: &LspUrl,
216+
symbol: &str,
217+
) -> anyhow::Result<Option<LspUrl>> {
218+
Ok(self.builtin_symbols.get(symbol).cloned())
219+
}
211220
}
212221

213222
/// A server for use in testing that provides helpers for sending requests, correlating
@@ -335,12 +344,20 @@ impl TestServer {
335344
pub(crate) fn new_with_settings(settings: Option<LspServerSettings>) -> anyhow::Result<Self> {
336345
let (server_connection, client_connection) = Connection::memory();
337346

338-
let builtin_docs = Arc::new(
339-
Self::testing_builtins(&std::env::current_dir()?)?
340-
.into_iter()
341-
.map(|(u, ds)| (u, render_docs_as_code(&ds).join("\n\n")))
342-
.collect::<HashMap<_, _>>(),
343-
);
347+
let builtin = Self::testing_builtins(&std::env::current_dir()?)?;
348+
let mut builtin_docs = HashMap::with_capacity(builtin.len());
349+
let mut builtin_symbols = HashMap::new();
350+
351+
for (u, ds) in builtin {
352+
builtin_docs.insert(u.clone(), render_docs_as_code(&ds).join("\n\n"));
353+
for d in ds {
354+
builtin_symbols.insert(d.id.name, u.clone());
355+
}
356+
}
357+
358+
let builtin_docs = Arc::new(builtin_docs);
359+
let builtin_symbols = Arc::new(builtin_symbols);
360+
344361
let prelude_file_contents = builtin_docs
345362
.iter()
346363
.filter_map(|(u, d)| match u {
@@ -354,6 +371,7 @@ impl TestServer {
354371
file_contents: file_contents.dupe(),
355372
dirs: dirs.dupe(),
356373
builtin_docs: builtin_docs.dupe(),
374+
builtin_symbols,
357375
};
358376

359377
let server_thread = std::thread::spawn(|| {

0 commit comments

Comments
 (0)