Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade starlark-rust to current HEAD #64

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 47 additions & 49 deletions src/bazel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ use prost::Message;
use starlark::analysis::find_call_name::AstModuleFindCallName;
use starlark::analysis::AstModuleLint;
use starlark::collections::SmallMap;
use starlark::docs::get_registered_starlark_docs;
use starlark::docs::render_docs_as_code;
use starlark::docs::Doc;
use starlark::docs::DocItem;
use starlark::docs::DocModule;
use starlark::errors::EvalMessage;
Expand Down Expand Up @@ -132,8 +129,6 @@ struct FilesystemCompletionOptions {
pub(crate) struct BazelContext<Client> {
workspaces: RefCell<HashMap<PathBuf, Rc<BazelWorkspace>>>,
query_output_base: Option<PathBuf>,
pub(crate) builtin_docs: HashMap<LspUrl, String>,
pub(crate) builtin_symbols: HashMap<String, LspUrl>,
pub(crate) client: Client,
}

Expand All @@ -150,38 +145,13 @@ fn is_workspace_file(uri: &LspUrl) -> bool {

impl<Client: BazelClient> BazelContext<Client> {
pub(crate) fn new(client: Client, query_output_base: Option<PathBuf>) -> anyhow::Result<Self> {
let mut builtins: HashMap<LspUrl, Vec<Doc>> = HashMap::new();
let mut builtin_symbols: HashMap<String, LspUrl> = HashMap::new();
for doc in get_registered_starlark_docs() {
let uri = Self::url_for_doc(&doc);
builtin_symbols.insert(doc.id.name.clone(), uri.clone());
builtins.entry(uri).or_default().push(doc);
}
let builtin_docs = builtins
.into_iter()
.map(|(u, ds)| (u, render_docs_as_code(&ds)))
.collect();

Ok(Self {
workspaces: RefCell::new(HashMap::new()),
query_output_base,
builtin_docs,
builtin_symbols,
client,
})
}

fn url_for_doc(doc: &Doc) -> LspUrl {
let url = match &doc.item {
DocItem::Module(_) => Url::parse("starlark:/native/builtins.bzl").unwrap(),
DocItem::Type(_) => {
Url::parse(&format!("starlark:/native/builtins/{}.bzl", doc.id.name)).unwrap()
}
DocItem::Member(_) => Url::parse("starlark:/native/builtins.bzl").unwrap(),
};
LspUrl::try_from(url).unwrap()
}

fn lint_module(&self, uri: &LspUrl, ast: &AstModule) -> Vec<EvalMessage> {
let globals = self.get_bazel_globals_names(uri);

Expand Down Expand Up @@ -676,7 +646,7 @@ impl<Client: BazelClient> LspContext for BazelContext<Client> {
},
false => Err(ContextError::NotAbsolute(uri.clone()).into()),
},
LspUrl::Starlark(_) => Ok(self.builtin_docs.get(uri).cloned()),
LspUrl::Starlark(_) => Ok(None),
_ => Err(ContextError::WrongScheme("file://".to_owned(), uri.clone()).into()),
}
}
Expand All @@ -688,9 +658,9 @@ impl<Client: BazelClient> LspContext for BazelContext<Client> {
fn get_url_for_global_symbol(
&self,
_current_file: &LspUrl,
symbol: &str,
_symbol: &str,
) -> anyhow::Result<Option<LspUrl>> {
Ok(self.builtin_symbols.get(symbol).cloned())
Ok(None)
}

fn get_string_completion_options(
Expand Down Expand Up @@ -812,7 +782,7 @@ mod tests {
use lsp_types::CompletionItemKind;
use serde_json::json;
use starlark::{
docs::{DocItem, DocMember, DocModule, DocParam, DocString},
docs::{DocFunction, DocItem, DocMember, DocModule, DocParam, DocString},
typing::Ty,
};
use starlark_lsp::{
Expand Down Expand Up @@ -1140,21 +1110,23 @@ mod tests {

let module = context.get_environment(&LspUrl::File(PathBuf::from("/foo/bar/BUILD")));

let (_, glob_member) = module
.members
.iter()
.find(|(member, _)| *member == "glob")
.unwrap();
fn doc_function<'a>(module: &'a DocModule, func_name: &str) -> &'a DocFunction {
let (_, glob_member) = module
.members
.iter()
.find(|(member, _)| *member == func_name)
.unwrap();

let f = match glob_member {
DocItem::Member(DocMember::Function(f)) => f,
_ => panic!(),
};
match glob_member {
DocItem::Member(DocMember::Function(f)) => f,
_ => panic!(),
}
}

assert_eq!(
*f.params,
doc_function(&module, "glob").params.pos_or_named,
vec![
DocParam::Arg {
DocParam {
name: "include".into(),
default_value: Some("[]".into()),
docs: Some(DocString {
Expand All @@ -1163,7 +1135,7 @@ mod tests {
}),
typ: Ty::any(),
},
DocParam::Arg {
DocParam {
name: "exclude".into(),
default_value: Some("[]".into()),
docs: Some(DocString {
Expand All @@ -1172,7 +1144,7 @@ mod tests {
}),
typ: Ty::any(),
},
DocParam::Arg {
DocParam {
name: "exclude_directories".into(),
default_value: Some("1".into()),
docs: Some(DocString {
Expand All @@ -1181,7 +1153,7 @@ mod tests {
}),
typ: Ty::any(),
},
DocParam::Arg {
DocParam {
name: "allow_empty".into(),
// TODO: Fix this
default_value: Some("unbound".into()),
Expand All @@ -1194,6 +1166,32 @@ mod tests {
]
);

assert_eq!(
doc_function(&module, "max").params.args,
Some(DocParam {
name: "args".into(),
default_value: None,
docs: Some(DocString {
summary: "The elements to be checked.".into(),
details: None,
}),
typ: Ty::any(),
})
);

assert_eq!(
doc_function(&module, "dict").params.kwargs,
Some(DocParam {
name: "kwargs".into(),
default_value: None,
docs: Some(DocString {
summary: "Dictionary of additional entries.".into(),
details: None,
}),
typ: Ty::any(),
})
);

Ok(())
}

Expand Down Expand Up @@ -1231,7 +1229,7 @@ mod tests {
match member {
DocMember::Function(function) => {
validate_doc_string(function.docs.as_ref());
for param in &function.params {
for param in &function.params.pos_or_named {
validate_doc_string(param.get_doc_string());
}
}
Expand Down
Loading
Loading