feat: classify associated and instance symbols
This commit is contained in:
1 parent
0bbe735c3c
commit
b624084745
3 files changed
+68
-3
No files matched your search
+3
-1
@@ -962,6 +962,7 @@ fn try_main(cli: Cli) -> Result<i32> {
|
||||
"id": item.id.as_str(),
|
||||
"module": item.module.as_str(),
|
||||
"kind": item.kind.as_str(),
|
||||
"callable": item.callable.map(|kind| kind.as_str()),
|
||||
"name": item.name,
|
||||
"declaration_path": item.declaration_path,
|
||||
"source": {
|
||||
@@ -974,9 +975,10 @@ fn try_main(cli: Cli) -> Result<i32> {
|
||||
} else {
|
||||
for item in &candidates {
|
||||
println!(
|
||||
"{}\n {} {} · module {}\n {}:{}",
|
||||
"{}\n {}{} {} · module {}\n {}:{}",
|
||||
item.id,
|
||||
item.kind.as_str(),
|
||||
item.callable.map(|kind| format!(" ({})", kind.as_str())).unwrap_or_default(),
|
||||
item.declaration_path.join("."),
|
||||
item.module,
|
||||
item.source.display(),
|
||||
|
||||
+1
-1
@@ -6,4 +6,4 @@
|
||||
|
||||
pub mod symbols;
|
||||
|
||||
pub use symbols::{ModuleId, ProjectIndex, SymbolDecl, SymbolId, SymbolKind};
|
||||
pub use symbols::{CallableKind, ModuleId, ProjectIndex, SymbolDecl, SymbolId, SymbolKind};
|
||||
+64
-1
@@ -58,12 +58,34 @@ impl SymbolKind {
|
||||
}
|
||||
}
|
||||
|
||||
/// Callable classification is semantic metadata, separate from `SymbolKind`.
|
||||
/// This keeps stable SymbolIds independent of whether a body later gains or
|
||||
/// loses a receiver dependency.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum CallableKind {
|
||||
FreeFunction,
|
||||
AssociatedFunction,
|
||||
InstanceMethod,
|
||||
}
|
||||
|
||||
impl CallableKind {
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::FreeFunction => "free_function",
|
||||
Self::AssociatedFunction => "associated_function",
|
||||
Self::InstanceMethod => "instance_method",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One source declaration, including a display path for human-facing tools.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct SymbolDecl {
|
||||
pub id: SymbolId,
|
||||
pub module: ModuleId,
|
||||
pub kind: SymbolKind,
|
||||
/// Present only for functions; never inferred from the display name.
|
||||
pub callable: Option<CallableKind>,
|
||||
pub name: String,
|
||||
pub declaration_path: Vec<String>,
|
||||
pub source: PathBuf,
|
||||
@@ -107,6 +129,7 @@ impl ProjectIndex {
|
||||
&module_id,
|
||||
&file,
|
||||
function,
|
||||
None,
|
||||
&mut Vec::new(),
|
||||
&mut symbols,
|
||||
)?;
|
||||
@@ -173,6 +196,7 @@ fn collect_symbols(
|
||||
module_id: &ModuleId,
|
||||
source: &Path,
|
||||
function: &Function,
|
||||
parent: Option<&Function>,
|
||||
parent_path: &mut Vec<String>,
|
||||
out: &mut Vec<SymbolDecl>,
|
||||
) -> Result<()> {
|
||||
@@ -181,6 +205,17 @@ fn collect_symbols(
|
||||
} else {
|
||||
SymbolKind::Function
|
||||
};
|
||||
let callable = if function.is_object() {
|
||||
None
|
||||
} else if parent.is_some_and(Function::is_object) {
|
||||
Some(if crate::ast::function_uses_receiver(function) {
|
||||
CallableKind::InstanceMethod
|
||||
} else {
|
||||
CallableKind::AssociatedFunction
|
||||
})
|
||||
} else {
|
||||
Some(CallableKind::FreeFunction)
|
||||
};
|
||||
parent_path.push(function.name.clone());
|
||||
let id = symbol_id(project_id, module_id, kind, parent_path);
|
||||
if out.iter().any(|existing| existing.id == id) {
|
||||
@@ -193,13 +228,22 @@ fn collect_symbols(
|
||||
id,
|
||||
module: module_id.clone(),
|
||||
kind,
|
||||
callable,
|
||||
name: function.name.clone(),
|
||||
declaration_path: parent_path.clone(),
|
||||
source: source.to_path_buf(),
|
||||
line: function.span.line,
|
||||
});
|
||||
for child in &function.children {
|
||||
collect_symbols(project_id, module_id, source, child, parent_path, out)?;
|
||||
collect_symbols(
|
||||
project_id,
|
||||
module_id,
|
||||
source,
|
||||
child,
|
||||
Some(function),
|
||||
parent_path,
|
||||
out,
|
||||
)?;
|
||||
}
|
||||
parent_path.pop();
|
||||
Ok(())
|
||||
@@ -375,4 +419,23 @@ id: knowledge.policy
|
||||
"%E6%9D%8E%E7%99%BD%2F%E5%87%BA%E7%94%9F%20%E5%B9%B4%E4%BB%BD"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn object_children_are_classified_without_changing_their_symbol_identity() {
|
||||
let root = fixture_root("callable-kind");
|
||||
fs::write(
|
||||
root.join("facts.mq.md"),
|
||||
"# 李白\n\n## 出生年份\n\n*701*\n\n## 名称\n\n*`self`*",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let index = ProjectIndex::build(ProjectRoot::new(&root).unwrap()).unwrap();
|
||||
let fact = index.symbols_named("出生年份").pop().unwrap();
|
||||
let method = index.symbols_named("名称").pop().unwrap();
|
||||
assert_eq!(fact.kind, SymbolKind::Function);
|
||||
assert_eq!(fact.callable, Some(CallableKind::AssociatedFunction));
|
||||
assert_eq!(method.callable, Some(CallableKind::InstanceMethod));
|
||||
assert!(fact.id.as_str().contains("/function/"));
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user