use crate::core::Span;
use crate::semantic::types::SemanticRole;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SymbolId(pub u32);
impl SymbolId {
pub fn new(index: usize) -> Self {
Self(index as u32)
}
pub fn index(self) -> usize {
self.0 as usize
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Symbol {
Package {
name: String,
qualified_name: String,
scope_id: usize,
source_file: Option<String>,
span: Option<Span>,
documentation: Option<String>,
},
Classifier {
name: String,
qualified_name: String,
kind: String,
is_abstract: bool,
scope_id: usize,
source_file: Option<String>,
span: Option<Span>,
documentation: Option<String>,
},
Feature {
name: String,
qualified_name: String,
scope_id: usize,
feature_type: Option<String>,
source_file: Option<String>,
span: Option<Span>,
documentation: Option<String>,
},
Definition {
name: String,
qualified_name: String,
kind: String,
semantic_role: Option<SemanticRole>,
scope_id: usize,
source_file: Option<String>,
span: Option<Span>,
documentation: Option<String>,
},
Usage {
name: String,
qualified_name: String,
kind: String,
semantic_role: Option<SemanticRole>,
usage_type: Option<String>,
scope_id: usize,
source_file: Option<String>,
span: Option<Span>,
documentation: Option<String>,
},
Alias {
name: String,
qualified_name: String,
target: String,
target_span: Option<Span>,
scope_id: usize,
source_file: Option<String>,
span: Option<Span>,
},
Import {
path: String,
path_span: Option<Span>,
qualified_name: String,
is_recursive: bool,
scope_id: usize,
source_file: Option<String>,
span: Option<Span>,
},
}
impl Symbol {
pub fn qualified_name(&self) -> &str {
match self {
Symbol::Package { qualified_name, .. }
| Symbol::Classifier { qualified_name, .. }
| Symbol::Feature { qualified_name, .. }
| Symbol::Definition { qualified_name, .. }
| Symbol::Usage { qualified_name, .. }
| Symbol::Alias { qualified_name, .. }
| Symbol::Import { qualified_name, .. } => qualified_name,
}
}
pub fn name(&self) -> &str {
match self {
Symbol::Package { name, .. }
| Symbol::Classifier { name, .. }
| Symbol::Feature { name, .. }
| Symbol::Definition { name, .. }
| Symbol::Usage { name, .. }
| Symbol::Alias { name, .. } => name,
Symbol::Import { path, .. } => path,
}
}
pub fn scope_id(&self) -> usize {
match self {
Symbol::Package { scope_id, .. }
| Symbol::Classifier { scope_id, .. }
| Symbol::Feature { scope_id, .. }
| Symbol::Definition { scope_id, .. }
| Symbol::Usage { scope_id, .. }
| Symbol::Alias { scope_id, .. }
| Symbol::Import { scope_id, .. } => *scope_id,
}
}
pub fn source_file(&self) -> Option<&str> {
match self {
Symbol::Package { source_file, .. }
| Symbol::Classifier { source_file, .. }
| Symbol::Feature { source_file, .. }
| Symbol::Definition { source_file, .. }
| Symbol::Usage { source_file, .. }
| Symbol::Alias { source_file, .. }
| Symbol::Import { source_file, .. } => source_file.as_deref(),
}
}
pub fn span(&self) -> Option<Span> {
match self {
Symbol::Package { span, .. }
| Symbol::Classifier { span, .. }
| Symbol::Feature { span, .. }
| Symbol::Definition { span, .. }
| Symbol::Usage { span, .. }
| Symbol::Alias { span, .. }
| Symbol::Import { span, .. } => *span,
}
}
pub fn is_type(&self) -> bool {
matches!(self, Symbol::Classifier { .. } | Symbol::Definition { .. })
}
pub fn type_reference(&self) -> Option<&str> {
match self {
Symbol::Feature { feature_type, .. } => feature_type.as_deref(),
_ => None,
}
}
pub fn documentation(&self) -> Option<&str> {
match self {
Symbol::Package { documentation, .. }
| Symbol::Classifier { documentation, .. }
| Symbol::Feature { documentation, .. }
| Symbol::Definition { documentation, .. }
| Symbol::Usage { documentation, .. } => documentation.as_deref(),
Symbol::Alias { .. } | Symbol::Import { .. } => None,
}
}
}