Skip to content

Commit d21c3ad

Browse files
ndmitchellfacebook-github-bot
authored andcommitted
Add get_environment and render_as_load to the LspContext
Summary: Required in future diffs to do auto-insertion of loads and to do completion of globals. Reviewed By: lmvasquezg Differential Revision: D47208502 fbshipit-source-id: 68c8cdcccf1515c32738e4412d03d18c2d27df55
1 parent 2b4ac54 commit d21c3ad

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

starlark/bin/eval.rs

+14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use starlark::docs::get_registered_starlark_docs;
3030
use starlark::docs::render_docs_as_code;
3131
use starlark::docs::Doc;
3232
use starlark::docs::DocItem;
33+
use starlark::docs::DocModule;
3334
use starlark::environment::FrozenModule;
3435
use starlark::environment::Globals;
3536
use starlark::environment::Module;
@@ -352,6 +353,19 @@ impl LspContext for Context {
352353
) -> anyhow::Result<Option<LspUrl>> {
353354
Ok(self.builtin_symbols.get(symbol).cloned())
354355
}
356+
357+
fn render_as_load(
358+
&self,
359+
_target: &LspUrl,
360+
_current_file: &LspUrl,
361+
_workspace_root: Option<&Path>,
362+
) -> anyhow::Result<String> {
363+
Err(anyhow::anyhow!("Not yet implemented, render_as_load"))
364+
}
365+
366+
fn get_environment(&self, _uri: &LspUrl) -> DocModule {
367+
DocModule::default()
368+
}
355369
}
356370

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

starlark/src/lsp/server.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ use serde::Serializer;
6969

7070
use crate::codemap::ResolvedSpan;
7171
use crate::codemap::Span;
72+
use crate::docs::DocModule;
7273
use crate::lsp::definition::Definition;
7374
use crate::lsp::definition::DottedDefinition;
7475
use crate::lsp::definition::IdentifierDefinition;
@@ -272,7 +273,19 @@ pub trait LspContext {
272273
workspace_root: Option<&Path>,
273274
) -> anyhow::Result<LspUrl>;
274275

275-
/// Resolve a string literal into a Url and a function that specifies a locaction within that
276+
/// Render the target URL to use as a path in a `load()` statement. If `target` is
277+
/// in the same package as `current_file`, the result is a relative path.
278+
///
279+
/// `target` is the file that should be loaded by `load()`.
280+
/// `current_file` is the file that the `load()` statement will be inserted into.
281+
fn render_as_load(
282+
&self,
283+
target: &LspUrl,
284+
current_file: &LspUrl,
285+
workspace_root: Option<&Path>,
286+
) -> anyhow::Result<String>;
287+
288+
/// Resolve a string literal into a Url and a function that specifies a location within that
276289
/// target file.
277290
///
278291
/// This can be used for things like file paths in string literals, build targets, etc.
@@ -296,6 +309,9 @@ pub trait LspContext {
296309
Ok(result)
297310
}
298311

312+
/// Get the preloaded environment for a particular file.
313+
fn get_environment(&self, uri: &LspUrl) -> DocModule;
314+
299315
/// Get the LSPUrl for a global symbol if possible.
300316
///
301317
/// The current file is provided in case different files have different global symbols

starlark/src/lsp/test.rs

+66
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ use crate::docs::render_docs_as_code;
6161
use crate::docs::Doc;
6262
use crate::docs::DocFunction;
6363
use crate::docs::DocItem;
64+
use crate::docs::DocMember;
65+
use crate::docs::DocModule;
6466
use crate::docs::Identifier;
6567
use crate::docs::Location;
6668
use crate::errors::EvalMessage;
@@ -95,6 +97,14 @@ enum ResolveLoadError {
9597
WrongScheme(String, LspUrl),
9698
}
9799

100+
#[derive(thiserror::Error, Debug)]
101+
enum RenderLoadError {
102+
#[error("Path `{}` provided, which does not seem to contain a filename", .0.display())]
103+
MissingTargetFilename(PathBuf),
104+
#[error("Urls `{}` and `{}` was expected to be of type `{}`", .1, .2, .0)]
105+
WrongScheme(String, LspUrl, LspUrl),
106+
}
107+
98108
#[derive(thiserror::Error, Debug)]
99109
pub(crate) enum TestServerError {
100110
#[error("Attempted to set the contents of a file with a non-absolute path `{}`", .0.display())]
@@ -170,6 +180,51 @@ impl LspContext for TestServerContext {
170180
}
171181
}
172182

183+
fn render_as_load(
184+
&self,
185+
target: &LspUrl,
186+
current_file: &LspUrl,
187+
workspace_root: Option<&Path>,
188+
) -> anyhow::Result<String> {
189+
match (target, current_file) {
190+
(LspUrl::File(target_path), LspUrl::File(current_file_path)) => {
191+
let target_package = target_path.parent();
192+
let current_file_package = current_file_path.parent();
193+
let target_filename = target_path.file_name();
194+
195+
// If both are in the same package, return a relative path.
196+
if matches!((target_package, current_file_package), (Some(a), Some(b)) if a == b) {
197+
return match target_filename {
198+
Some(filename) => Ok(format!(":{}", filename.to_string_lossy())),
199+
None => {
200+
Err(RenderLoadError::MissingTargetFilename(target_path.clone()).into())
201+
}
202+
};
203+
}
204+
205+
let target_path = workspace_root
206+
.and_then(|root| target_path.strip_prefix(root).ok())
207+
.unwrap_or(target_path);
208+
209+
Ok(format!(
210+
"//{}:{}",
211+
target_package
212+
.map(|path| path.to_string_lossy())
213+
.unwrap_or_default(),
214+
target_filename
215+
.unwrap_or(target_path.as_os_str())
216+
.to_string_lossy()
217+
))
218+
}
219+
_ => Err(RenderLoadError::WrongScheme(
220+
"file://".to_owned(),
221+
target.clone(),
222+
current_file.clone(),
223+
)
224+
.into()),
225+
}
226+
}
227+
173228
fn resolve_string_literal(
174229
&self,
175230
literal: &str,
@@ -228,6 +283,17 @@ impl LspContext for TestServerContext {
228283
) -> anyhow::Result<Option<LspUrl>> {
229284
Ok(self.builtin_symbols.get(symbol).cloned())
230285
}
286+
287+
fn get_environment(&self, _uri: &LspUrl) -> DocModule {
288+
DocModule {
289+
docs: None,
290+
members: self
291+
.builtin_symbols
292+
.keys()
293+
.map(|name| (name.clone(), DocMember::Function(DocFunction::default())))
294+
.collect(),
295+
}
296+
}
231297
}
232298

233299
/// A server for use in testing that provides helpers for sending requests, correlating

0 commit comments

Comments
 (0)