mod kind;
use serde::{Deserialize, Serialize};
pub use kind::{SymbolKind, UnknownSymbolKind};
use crate::text::Span;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Symbol {
pub kind: SymbolKind,
pub name: String,
pub name_span: Span,
pub span: Span,
pub extent: Span,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub visibility: Option<Modifier>,
}
impl Symbol {
pub fn validate_in(&self, source: &str) -> Result<(), crate::SpanError> {
for span in [self.name_span, self.span, self.extent] {
span.validate_in(source)?;
}
if let Some(modifier) = &self.visibility {
modifier.span.validate_in(source)?;
}
Ok(())
}
pub fn plain(kind: SymbolKind, name: impl Into<String>, name_span: Span, span: Span) -> Self {
Self {
kind,
name: name.into(),
name_span,
span,
extent: span,
visibility: None,
}
}
pub fn modifier(&self) -> Option<&str> {
self.visibility.as_ref().map(|m| m.text.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Modifier {
pub span: Span,
pub text: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ModifierAt {
Child(&'static str),
Parent(&'static str),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SymbolRule {
pub node: &'static str,
pub name_field: Option<&'static str>,
pub kind: SymbolKind,
pub under: Option<&'static str>,
pub within: Option<&'static str>,
pub leading: &'static [&'static str],
pub visibility: Option<ModifierAt>,
pub name_inner: Option<&'static str>,
pub companion_of: &'static [SymbolKind],
pub movable: bool,
pub move_scope_wrappers: &'static [&'static str],
}
impl SymbolRule {
pub const fn new(node: &'static str, name_field: &'static str, kind: SymbolKind) -> Self {
Self {
node,
name_field: Some(name_field),
kind,
under: None,
within: None,
leading: &[],
visibility: None,
name_inner: None,
companion_of: &[],
move_scope_wrappers: &[],
movable: true,
}
}
pub const fn self_named(node: &'static str, kind: SymbolKind) -> Self {
Self {
node,
name_field: None,
kind,
under: None,
within: None,
leading: &[],
visibility: None,
name_inner: None,
companion_of: &[],
move_scope_wrappers: &[],
movable: true,
}
}
pub const fn movable(mut self, supported: bool) -> Self {
self.movable = supported;
self
}
pub const fn move_scope_wrappers(mut self, kinds: &'static [&'static str]) -> Self {
self.move_scope_wrappers = kinds;
self
}
pub const fn companion_of(mut self, kinds: &'static [SymbolKind]) -> Self {
self.companion_of = kinds;
self
}
pub const fn name_inner(mut self, field: &'static str) -> Self {
self.name_inner = Some(field);
self
}
pub const fn leading(mut self, kinds: &'static [&'static str]) -> Self {
self.leading = kinds;
self
}
pub const fn visibility(mut self, at: ModifierAt) -> Self {
self.visibility = Some(at);
self
}
pub const fn under(mut self, parent: &'static str) -> Self {
self.under = Some(parent);
self
}
pub const fn within(mut self, ancestor: &'static str) -> Self {
self.within = Some(ancestor);
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct DeclarationPieces {
pub declaration: Span,
pub supported: bool,
pub scope: Span,
pub top_level: bool,
pub companions: Vec<CompanionPiece>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct CompanionPiece {
pub span: Span,
pub ownership: CompanionOwnership,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum CompanionOwnership {
SameScopeTarget,
AmbiguousTarget,
UnsupportedTarget,
}