Skip to content

Commit

Permalink
feat: provide signature information during function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
MaartenStaa committed Mar 15, 2024
1 parent 09f359f commit 401e180
Show file tree
Hide file tree
Showing 8 changed files with 427 additions and 99 deletions.
2 changes: 1 addition & 1 deletion starlark/src/docs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ impl DocParam {
Some(indented)
}

fn render_as_code(&self) -> String {
pub fn render_as_code(&self) -> String {
match self {
DocParam::Arg {
name,
Expand Down
14 changes: 5 additions & 9 deletions starlark_lsp/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use lsp_types::TextEdit;
use starlark::codemap::ResolvedSpan;
use starlark::docs::markdown::render_doc_item;
use starlark::docs::markdown::render_doc_param;
use starlark::docs::DocItem;
use starlark::docs::DocMember;
use starlark::docs::DocParam;
use starlark_syntax::codemap::ResolvedPos;
Expand Down Expand Up @@ -95,17 +94,14 @@ impl<T: LspContext> Backend<T> {
(
key,
CompletionItem {
kind: Some(match value.kind {
SymbolKind::Method { .. } => CompletionItemKind::METHOD,
SymbolKind::Variable => CompletionItemKind::VARIABLE,
}),
kind: Some(value.kind.into()),
detail: value.detail,
documentation: value
.doc
.map(|doc| {
Documentation::MarkupContent(MarkupContent {
kind: MarkupKind::Markdown,
value: render_doc_item(&value.name, &doc),
value: render_doc_item(&value.name, &doc.to_doc_item()),
})
})
.or_else(|| {
Expand Down Expand Up @@ -258,10 +254,10 @@ impl<T: LspContext> Backend<T> {
.remove(name)
.and_then(|symbol| match symbol.kind {
SymbolKind::Method { .. } => symbol.doc,
SymbolKind::Variable => None,
SymbolKind::Constant | SymbolKind::Variable => None,
})
.and_then(|docs| match docs {
DocItem::Function(doc_function) => Some(
DocMember::Function(doc_function) => Some(
doc_function
.params
.into_iter()
Expand All @@ -285,7 +281,7 @@ impl<T: LspContext> Backend<T> {
self.get_ast_or_load_from_disk(&load_uri)?
.and_then(|ast| ast.find_exported_symbol(name))
.and_then(|symbol| match symbol.kind {
SymbolKind::Variable => None,
SymbolKind::Constant | SymbolKind::Variable => None,
SymbolKind::Method { argument_names } => Some(
argument_names
.into_iter()
Expand Down
3 changes: 2 additions & 1 deletion starlark_lsp/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,8 @@ impl LspModule {
/// Attempt to find the location in this module where an exported symbol is defined.
pub(crate) fn find_exported_symbol_span(&self, name: &str) -> Option<ResolvedSpan> {
self.find_exported_symbol(name)
.map(|symbol| self.ast.codemap().resolve_span(symbol.span))
.and_then(|symbol| symbol.span)
.map(|span| self.ast.codemap().resolve_span(span))
}

/// Attempt to find the location in this module where a member of a struct (named `name`)
Expand Down
26 changes: 14 additions & 12 deletions starlark_lsp/src/exported.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use lsp_types::MarkupContent;
use lsp_types::MarkupKind;
use starlark::collections::SmallMap;
use starlark::docs::markdown::render_doc_item;
use starlark::docs::DocItem;
use starlark::docs::DocMember;
use starlark::syntax::AstModule;
use starlark_syntax::syntax::ast::AstAssignIdent;
use starlark_syntax::syntax::ast::Stmt;
Expand All @@ -38,7 +38,7 @@ impl From<Symbol> for CompletionItem {
let documentation = value.doc.map(|doc| {
Documentation::MarkupContent(MarkupContent {
kind: MarkupKind::Markdown,
value: render_doc_item(&value.name, &doc),
value: render_doc_item(&value.name, &doc.to_doc_item()),
})
});
Self {
Expand All @@ -63,17 +63,16 @@ impl AstModuleExportedSymbols for AstModule {
let mut result: SmallMap<&str, _> = SmallMap::new();

fn add<'a>(
me: &AstModule,
result: &mut SmallMap<&'a str, Symbol>,
name: &'a AstAssignIdent,
kind: SymbolKind,
resolve_docs: impl FnOnce() -> Option<DocItem>,
resolve_docs: impl FnOnce() -> Option<DocMember>,
) {
if !name.ident.starts_with('_') {
result.entry(&name.ident).or_insert(Symbol {
name: name.ident.clone(),
detail: None,
span: name.span,
span: Some(name.span),
kind,
doc: resolve_docs(),
param: None,
Expand All @@ -87,25 +86,24 @@ impl AstModuleExportedSymbols for AstModule {
Stmt::Assign(assign) => {
assign.lhs.visit_lvalue(|name| {
let kind = SymbolKind::from_expr(&assign.rhs);
add(self, &mut result, name, kind, || {
add(&mut result, name, kind, || {
last_node
.and_then(|last| get_doc_item_for_assign(last, &assign.lhs))
.map(DocItem::Property)
.map(DocMember::Property)
});
});
}
Stmt::AssignModify(dest, _, _) => {
dest.visit_lvalue(|name| {
add(self, &mut result, name, SymbolKind::Variable, || {
add(&mut result, name, SymbolKind::Variable, || {
last_node
.and_then(|last| get_doc_item_for_assign(last, dest))
.map(DocItem::Property)
.map(DocMember::Property)
});
});
}
Stmt::Def(def) => {
add(
self,
&mut result,
&def.name,
SymbolKind::Method {
Expand All @@ -115,7 +113,7 @@ impl AstModuleExportedSymbols for AstModule {
.filter_map(|param| param.split().0.map(|name| name.to_string()))
.collect(),
},
|| get_doc_item_for_def(def).map(DocItem::Function),
|| get_doc_item_for_def(def).map(DocMember::Function),
);
}
_ => {}
Expand Down Expand Up @@ -150,7 +148,11 @@ d = 2
);
let res = modu.exported_symbols();
assert_eq!(
res.map(|symbol| format!("{} {}", modu.file_span(symbol.span), symbol.name)),
res.map(|symbol| format!(
"{} {}",
modu.file_span(symbol.span.expect("span should be set")),
symbol.name
)),
&["X:3:5-6 b", "X:4:1-2 d"]
);
}
Expand Down
1 change: 1 addition & 0 deletions starlark_lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod exported;
pub(crate) mod inspect;
pub(crate) mod loaded;
pub mod server;
pub(crate) mod signature;
mod symbols;
#[cfg(all(test, not(windows)))]
mod test;
Loading

0 comments on commit 401e180

Please sign in to comment.