Skip to content

Commit 09f359f

Browse files
committed
chore: refactor to unify starlark_lsp::exported::{Symbol, SymbolKind} and starlark_lsp::symbol::{Symbol, SymbolKind}
1 parent 14f1b36 commit 09f359f

File tree

5 files changed

+113
-83
lines changed

5 files changed

+113
-83
lines changed

starlark_lsp/src/completion.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ use crate::definition::Definition;
4242
use crate::definition::DottedDefinition;
4343
use crate::definition::IdentifierDefinition;
4444
use crate::definition::LspModule;
45-
use crate::exported::SymbolKind as ExportedSymbolKind;
4645
use crate::server::Backend;
4746
use crate::server::LspContext;
4847
use crate::server::LspUrl;
@@ -97,7 +96,7 @@ impl<T: LspContext> Backend<T> {
9796
key,
9897
CompletionItem {
9998
kind: Some(match value.kind {
100-
SymbolKind::Method => CompletionItemKind::METHOD,
99+
SymbolKind::Method { .. } => CompletionItemKind::METHOD,
101100
SymbolKind::Variable => CompletionItemKind::VARIABLE,
102101
}),
103102
detail: value.detail,
@@ -258,7 +257,7 @@ impl<T: LspContext> Backend<T> {
258257
)
259258
.remove(name)
260259
.and_then(|symbol| match symbol.kind {
261-
SymbolKind::Method => symbol.doc,
260+
SymbolKind::Method { .. } => symbol.doc,
262261
SymbolKind::Variable => None,
263262
})
264263
.and_then(|docs| match docs {
@@ -286,8 +285,8 @@ impl<T: LspContext> Backend<T> {
286285
self.get_ast_or_load_from_disk(&load_uri)?
287286
.and_then(|ast| ast.find_exported_symbol(name))
288287
.and_then(|symbol| match symbol.kind {
289-
ExportedSymbolKind::Any => None,
290-
ExportedSymbolKind::Function { argument_names } => Some(
288+
SymbolKind::Variable => None,
289+
SymbolKind::Method { argument_names } => Some(
291290
argument_names
292291
.into_iter()
293292
.map(|name| CompletionItem {

starlark_lsp/src/definition.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ use crate::bind::Assigner;
4444
use crate::bind::Bind;
4545
use crate::bind::Scope;
4646
use crate::exported::AstModuleExportedSymbols;
47-
use crate::exported::Symbol;
4847
use crate::loaded::AstModuleLoadedSymbols;
4948
use crate::loaded::LoadedSymbol;
49+
use crate::symbols::Symbol;
5050

5151
/// The location of a definition for a given identifier. See [`AstModule::find_definition_at_location`].
5252
#[derive(Debug, Clone, Eq, PartialEq)]
@@ -275,7 +275,7 @@ impl LspModule {
275275

276276
/// Look at the given scope and child scopes to try to find where the identifier
277277
/// accessed at Pos is defined.
278-
fn find_definition_in_scope<'a>(scope: &'a Scope, pos: Pos) -> TempDefinition<'a> {
278+
fn find_definition_in_scope(scope: &Scope, pos: Pos) -> TempDefinition {
279279
/// Look for a name in the given scope, with a given source, and return the right
280280
/// type of [`TempIdentifierDefinition`] based on whether / how the variable is bound.
281281
fn resolve_get_in_scope<'a>(
@@ -455,7 +455,7 @@ 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| symbol.span.resolve_span())
458+
.map(|symbol| self.ast.codemap().resolve_span(symbol.span))
459459
}
460460

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

starlark_lsp/src/exported.rs

+11-58
Original file line numberDiff line numberDiff line change
@@ -16,78 +16,29 @@
1616
*/
1717

1818
use lsp_types::CompletionItem;
19-
use lsp_types::CompletionItemKind;
2019
use lsp_types::Documentation;
2120
use lsp_types::MarkupContent;
2221
use lsp_types::MarkupKind;
23-
use starlark::codemap::FileSpan;
2422
use starlark::collections::SmallMap;
2523
use starlark::docs::markdown::render_doc_item;
2624
use starlark::docs::DocItem;
2725
use starlark::syntax::AstModule;
2826
use starlark_syntax::syntax::ast::AstAssignIdent;
29-
use starlark_syntax::syntax::ast::Expr;
3027
use starlark_syntax::syntax::ast::Stmt;
3128
use starlark_syntax::syntax::module::AstModuleFields;
3229
use starlark_syntax::syntax::top_level_stmts::top_level_stmts;
3330

3431
use crate::docs::get_doc_item_for_assign;
3532
use crate::docs::get_doc_item_for_def;
36-
37-
/// The type of an exported symbol.
38-
/// If unknown, will use `Any`.
39-
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
40-
pub(crate) enum SymbolKind {
41-
/// Any kind of symbol.
42-
Any,
43-
/// The symbol represents something that can be called, for example
44-
/// a `def` or a variable assigned to a `lambda`.
45-
Function { argument_names: Vec<String> },
46-
}
47-
48-
impl SymbolKind {
49-
pub(crate) fn from_expr(x: &Expr) -> Self {
50-
match x {
51-
Expr::Lambda(lambda) => Self::Function {
52-
argument_names: lambda
53-
.params
54-
.iter()
55-
.filter_map(|param| param.split().0.map(|name| name.to_string()))
56-
.collect(),
57-
},
58-
_ => Self::Any,
59-
}
60-
}
61-
}
62-
63-
impl From<SymbolKind> for CompletionItemKind {
64-
fn from(value: SymbolKind) -> Self {
65-
match value {
66-
SymbolKind::Any => CompletionItemKind::CONSTANT,
67-
SymbolKind::Function { .. } => CompletionItemKind::FUNCTION,
68-
}
69-
}
70-
}
71-
72-
/// A symbol. Returned from [`AstModule::exported_symbols`].
73-
#[derive(Debug, PartialEq, Clone)]
74-
pub(crate) struct Symbol {
75-
/// The name of the symbol.
76-
pub(crate) name: String,
77-
/// The location of its definition.
78-
pub(crate) span: FileSpan,
79-
/// The type of symbol it represents.
80-
pub(crate) kind: SymbolKind,
81-
/// The documentation for this symbol.
82-
pub(crate) docs: Option<DocItem>,
83-
}
33+
use crate::symbols::Symbol;
34+
use crate::symbols::SymbolKind;
8435

8536
impl From<Symbol> for CompletionItem {
8637
fn from(value: Symbol) -> Self {
87-
let documentation = value.docs.map(|docs| {
38+
let documentation = value.doc.map(|doc| {
8839
Documentation::MarkupContent(MarkupContent {
8940
kind: MarkupKind::Markdown,
90-
value: render_doc_item(&value.name, &docs),
41+
value: render_doc_item(&value.name, &doc),
9142
})
9243
});
9344
Self {
@@ -121,9 +72,11 @@ impl AstModuleExportedSymbols for AstModule {
12172
if !name.ident.starts_with('_') {
12273
result.entry(&name.ident).or_insert(Symbol {
12374
name: name.ident.clone(),
124-
span: me.file_span(name.span),
75+
detail: None,
76+
span: name.span,
12577
kind,
126-
docs: resolve_docs(),
78+
doc: resolve_docs(),
79+
param: None,
12780
});
12881
}
12982
}
@@ -143,7 +96,7 @@ impl AstModuleExportedSymbols for AstModule {
14396
}
14497
Stmt::AssignModify(dest, _, _) => {
14598
dest.visit_lvalue(|name| {
146-
add(self, &mut result, name, SymbolKind::Any, || {
99+
add(self, &mut result, name, SymbolKind::Variable, || {
147100
last_node
148101
.and_then(|last| get_doc_item_for_assign(last, dest))
149102
.map(DocItem::Property)
@@ -155,7 +108,7 @@ impl AstModuleExportedSymbols for AstModule {
155108
self,
156109
&mut result,
157110
&def.name,
158-
SymbolKind::Function {
111+
SymbolKind::Method {
159112
argument_names: def
160113
.params
161114
.iter()
@@ -197,7 +150,7 @@ d = 2
197150
);
198151
let res = modu.exported_symbols();
199152
assert_eq!(
200-
res.map(|symbol| format!("{} {}", symbol.span, symbol.name)),
153+
res.map(|symbol| format!("{} {}", modu.file_span(symbol.span), symbol.name)),
201154
&["X:3:5-6 b", "X:4:1-2 d"]
202155
);
203156
}

starlark_lsp/src/server.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ impl<T: LspContext> Backend<T> {
11671167
let load_uri = self.resolve_load_path(&path, document_uri, workspace_root)?;
11681168
self.get_ast_or_load_from_disk(&load_uri)?.and_then(|ast| {
11691169
ast.find_exported_symbol(&name).and_then(|symbol| {
1170-
symbol.docs.map(|docs| Hover {
1170+
symbol.doc.map(|docs| Hover {
11711171
contents: HoverContents::Array(vec![MarkedString::String(
11721172
render_doc_item(&symbol.name, &docs),
11731173
)]),

0 commit comments

Comments
 (0)