Skip to main content

oak_navigation/
lib.rs

1#![feature(new_range_api)]
2#![warn(missing_docs)]
3//! Navigation support for the Oak language framework.
4//!
5//! This crate provides traits and structures for code navigation features
6//! like "Go to Definition" and "Find All References".
7use core::range::Range;
8use oak_core::{Language, TokenType, language::UniversalTokenRole, tree::RedNode, visitor::Visitor};
9
10/// Represents a location in a source file.
11#[derive(Debug, Clone, PartialEq, Eq, Hash)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
13pub struct Location {
14    /// The URI of the resource.
15    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_arc_str"))]
16    pub uri: oak_core::Arc<str>,
17    /// The byte range within the resource.
18    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
19    pub range: Range<usize>,
20}
21
22/// Trait for languages that support jumping to definition.
23pub trait DefinitionProvider<L: Language> {
24    /// Returns the definition(s) of the symbol at the given offset.
25    fn definition(&self, root: &RedNode<L>, offset: usize) -> Vec<Location>;
26}
27
28/// Trait for languages that support finding references.
29pub trait ReferencesProvider<L: Language> {
30    /// Returns the references to the symbol at the given offset.
31    fn references(&self, root: &RedNode<L>, offset: usize, include_declaration: bool) -> Vec<Location>;
32}
33
34/// A helper to find all references of a name in a tree.
35pub struct SimpleReferenceFinder<'a, L: Language> {
36    /// The name of the symbol to find references for.
37    name: &'a str,
38    /// The full source text.
39    source: &'a str,
40    /// The URI of the source file.
41    uri: oak_core::Arc<str>,
42    /// The list of locations where the name was found.
43    references: Vec<Location>,
44    /// Phantom data for the language type.
45    _phantom: std::marker::PhantomData<L>,
46}
47
48impl<'a, L: Language> SimpleReferenceFinder<'a, L> {
49    /// Finds all references of the given name in the tree.
50    ///
51    /// # Arguments
52    /// * `root` - The root node of the syntax tree.
53    /// * `name` - The name of the symbol to find.
54    /// * `source` - The source text.
55    /// * `uri` - The URI of the source file.
56    pub fn find(root: &RedNode<'a, L>, name: &'a str, source: &'a str, uri: impl Into<oak_core::Arc<str>>) -> Vec<Location> {
57        let mut finder = Self { name, source, uri: uri.into(), references: Vec::new(), _phantom: std::marker::PhantomData };
58        finder.visit_node(*root);
59        finder.references
60    }
61}
62
63impl<'a, L: Language> Visitor<'a, L> for SimpleReferenceFinder<'a, L> {
64    fn visit_node(&mut self, node: RedNode<'a, L>) {
65        self.walk_node(node);
66    }
67
68    fn visit_token(&mut self, token: oak_core::tree::RedLeaf<L>) {
69        if token.kind.is_universal(UniversalTokenRole::Name) {
70            let text = &self.source[token.span.clone()];
71            if text == self.name {
72                self.references.push(Location { uri: self.uri.clone(), range: token.span });
73            }
74        }
75    }
76}