#![deny(unsafe_code)]
#![allow(dead_code)]
pub mod decl;
pub mod java;
pub mod java_decl;
pub mod py;
pub mod py_decl;
pub mod rust;
pub mod rust_decl;
pub mod scan;
pub mod ts;
pub mod ts_decl;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Kind {
Func,
Type,
Trait,
Class,
Interface,
Impl,
Mod,
Const,
Alias,
Import,
Macro,
}
impl Kind {
pub fn label(self) -> &'static str {
match self {
Kind::Func => "fn",
Kind::Type => "type",
Kind::Trait => "trait",
Kind::Class => "class",
Kind::Interface => "interface",
Kind::Impl => "impl",
Kind::Mod => "mod",
Kind::Const => "const",
Kind::Alias => "alias",
Kind::Import => "use",
Kind::Macro => "macro",
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Symbol {
pub kind: Kind,
pub name: String,
pub path: String,
pub depth: u8,
pub doc: (usize, usize),
pub sig: (usize, usize),
pub body: (usize, usize),
}
impl Symbol {
pub fn span(&self) -> (usize, usize) {
(self.doc.0.min(self.sig.0), self.body.1.max(self.sig.1))
}
pub fn hidden(&self) -> usize {
self.body.1.saturating_sub(self.body.0)
}
}
pub fn disambiguate(symbols: &mut [Symbol]) {
let mut seen: Vec<(String, usize)> = Vec::new();
for s in symbols.iter_mut() {
match seen.iter_mut().find(|(p, _)| *p == s.path) {
Some((_, n)) => {
*n += 1;
s.path = format!("{}-{}", s.path, n);
}
None => seen.push((s.path.clone(), 1)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sym(path: &str) -> Symbol {
Symbol {
kind: Kind::Func,
name: path.into(),
path: path.into(),
depth: 0,
doc: (0, 0),
sig: (0, 1),
body: (1, 1),
}
}
#[test]
fn a_repeated_path_is_suffixed_so_a_fold_id_names_one_symbol() {
let mut v = vec![sym("new"), sym("open"), sym("new"), sym("new")];
disambiguate(&mut v);
let paths: Vec<&str> = v.iter().map(|s| s.path.as_str()).collect();
assert_eq!(paths, vec!["new", "open", "new-2", "new-3"]);
}
#[test]
fn a_symbols_span_covers_its_doc_comment_and_body() {
let s = Symbol {
doc: (3, 5),
sig: (5, 6),
body: (6, 20),
..sym("f")
};
assert_eq!(s.span(), (3, 20));
assert_eq!(s.hidden(), 14);
}
}