Skip to content

Commit 401e180

Browse files
committed
feat: provide signature information during function calls
1 parent 09f359f commit 401e180

File tree

8 files changed

+427
-99
lines changed

8 files changed

+427
-99
lines changed

starlark/src/docs/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ impl DocParam {
647647
Some(indented)
648648
}
649649

650-
fn render_as_code(&self) -> String {
650+
pub fn render_as_code(&self) -> String {
651651
match self {
652652
DocParam::Arg {
653653
name,

starlark_lsp/src/completion.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use lsp_types::TextEdit;
3131
use starlark::codemap::ResolvedSpan;
3232
use starlark::docs::markdown::render_doc_item;
3333
use starlark::docs::markdown::render_doc_param;
34-
use starlark::docs::DocItem;
3534
use starlark::docs::DocMember;
3635
use starlark::docs::DocParam;
3736
use starlark_syntax::codemap::ResolvedPos;
@@ -95,17 +94,14 @@ impl<T: LspContext> Backend<T> {
9594
(
9695
key,
9796
CompletionItem {
98-
kind: Some(match value.kind {
99-
SymbolKind::Method { .. } => CompletionItemKind::METHOD,
100-
SymbolKind::Variable => CompletionItemKind::VARIABLE,
101-
}),
97+
kind: Some(value.kind.into()),
10298
detail: value.detail,
10399
documentation: value
104100
.doc
105101
.map(|doc| {
106102
Documentation::MarkupContent(MarkupContent {
107103
kind: MarkupKind::Markdown,
108-
value: render_doc_item(&value.name, &doc),
104+
value: render_doc_item(&value.name, &doc.to_doc_item()),
109105
})
110106
})
111107
.or_else(|| {
@@ -258,10 +254,10 @@ impl<T: LspContext> Backend<T> {
258254
.remove(name)
259255
.and_then(|symbol| match symbol.kind {
260256
SymbolKind::Method { .. } => symbol.doc,
261-
SymbolKind::Variable => None,
257+
SymbolKind::Constant | SymbolKind::Variable => None,
262258
})
263259
.and_then(|docs| match docs {
264-
DocItem::Function(doc_function) => Some(
260+
DocMember::Function(doc_function) => Some(
265261
doc_function
266262
.params
267263
.into_iter()
@@ -285,7 +281,7 @@ impl<T: LspContext> Backend<T> {
285281
self.get_ast_or_load_from_disk(&load_uri)?
286282
.and_then(|ast| ast.find_exported_symbol(name))
287283
.and_then(|symbol| match symbol.kind {
288-
SymbolKind::Variable => None,
284+
SymbolKind::Constant | SymbolKind::Variable => None,
289285
SymbolKind::Method { argument_names } => Some(
290286
argument_names
291287
.into_iter()

starlark_lsp/src/definition.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,8 @@ impl LspModule {
455455
/// Attempt to find the location in this module where an exported symbol is defined.
456456
pub(crate) fn find_exported_symbol_span(&self, name: &str) -> Option<ResolvedSpan> {
457457
self.find_exported_symbol(name)
458-
.map(|symbol| self.ast.codemap().resolve_span(symbol.span))
458+
.and_then(|symbol| symbol.span)
459+
.map(|span| self.ast.codemap().resolve_span(span))
459460
}
460461

461462
/// Attempt to find the location in this module where a member of a struct (named `name`)

starlark_lsp/src/exported.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use lsp_types::MarkupContent;
2121
use lsp_types::MarkupKind;
2222
use starlark::collections::SmallMap;
2323
use starlark::docs::markdown::render_doc_item;
24-
use starlark::docs::DocItem;
24+
use starlark::docs::DocMember;
2525
use starlark::syntax::AstModule;
2626
use starlark_syntax::syntax::ast::AstAssignIdent;
2727
use starlark_syntax::syntax::ast::Stmt;
@@ -38,7 +38,7 @@ impl From<Symbol> for CompletionItem {
3838
let documentation = value.doc.map(|doc| {
3939
Documentation::MarkupContent(MarkupContent {
4040
kind: MarkupKind::Markdown,
41-
value: render_doc_item(&value.name, &doc),
41+
value: render_doc_item(&value.name, &doc.to_doc_item()),
4242
})
4343
});
4444
Self {
@@ -63,17 +63,16 @@ impl AstModuleExportedSymbols for AstModule {
6363
let mut result: SmallMap<&str, _> = SmallMap::new();
6464

6565
fn add<'a>(
66-
me: &AstModule,
6766
result: &mut SmallMap<&'a str, Symbol>,
6867
name: &'a AstAssignIdent,
6968
kind: SymbolKind,
70-
resolve_docs: impl FnOnce() -> Option<DocItem>,
69+
resolve_docs: impl FnOnce() -> Option<DocMember>,
7170
) {
7271
if !name.ident.starts_with('_') {
7372
result.entry(&name.ident).or_insert(Symbol {
7473
name: name.ident.clone(),
7574
detail: None,
76-
span: name.span,
75+
span: Some(name.span),
7776
kind,
7877
doc: resolve_docs(),
7978
param: None,
@@ -87,25 +86,24 @@ impl AstModuleExportedSymbols for AstModule {
8786
Stmt::Assign(assign) => {
8887
assign.lhs.visit_lvalue(|name| {
8988
let kind = SymbolKind::from_expr(&assign.rhs);
90-
add(self, &mut result, name, kind, || {
89+
add(&mut result, name, kind, || {
9190
last_node
9291
.and_then(|last| get_doc_item_for_assign(last, &assign.lhs))
93-
.map(DocItem::Property)
92+
.map(DocMember::Property)
9493
});
9594
});
9695
}
9796
Stmt::AssignModify(dest, _, _) => {
9897
dest.visit_lvalue(|name| {
99-
add(self, &mut result, name, SymbolKind::Variable, || {
98+
add(&mut result, name, SymbolKind::Variable, || {
10099
last_node
101100
.and_then(|last| get_doc_item_for_assign(last, dest))
102-
.map(DocItem::Property)
101+
.map(DocMember::Property)
103102
});
104103
});
105104
}
106105
Stmt::Def(def) => {
107106
add(
108-
self,
109107
&mut result,
110108
&def.name,
111109
SymbolKind::Method {
@@ -115,7 +113,7 @@ impl AstModuleExportedSymbols for AstModule {
115113
.filter_map(|param| param.split().0.map(|name| name.to_string()))
116114
.collect(),
117115
},
118-
|| get_doc_item_for_def(def).map(DocItem::Function),
116+
|| get_doc_item_for_def(def).map(DocMember::Function),
119117
);
120118
}
121119
_ => {}
@@ -150,7 +148,11 @@ d = 2
150148
);
151149
let res = modu.exported_symbols();
152150
assert_eq!(
153-
res.map(|symbol| format!("{} {}", modu.file_span(symbol.span), symbol.name)),
151+
res.map(|symbol| format!(
152+
"{} {}",
153+
modu.file_span(symbol.span.expect("span should be set")),
154+
symbol.name
155+
)),
154156
&["X:3:5-6 b", "X:4:1-2 d"]
155157
);
156158
}

starlark_lsp/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mod exported;
3030
pub(crate) mod inspect;
3131
pub(crate) mod loaded;
3232
pub mod server;
33+
pub(crate) mod signature;
3334
mod symbols;
3435
#[cfg(all(test, not(windows)))]
3536
mod test;

0 commit comments

Comments
 (0)