Skip to main content

DeclarationProvider

Struct DeclarationProvider 

Source
pub struct DeclarationProvider<'a> {
    pub ast: Arc<Node>,
    /* private fields */
}
Expand description

Provider for finding declarations in Perl source code.

This provider implements LSP go-to-declaration functionality with enhanced workspace navigation support. Maintains ≤1ms response time for symbol lookup operations through optimized AST traversal and parent mapping.

§Performance Characteristics

  • Declaration resolution: <500μs for typical Perl files
  • Memory usage: O(n) where n is AST node count
  • Parent map validation: Debug-only with cycle detection

§LSP Workflow Integration

Parse → Index → Navigate → Complete → Analyze pipeline integration:

  1. Parse: AST generation from Perl source
  2. Index: Symbol table construction with qualified name resolution
  3. Navigate: Declaration provider for go-to-definition requests
  4. Complete: Symbol context for completion providers
  5. Analyze: Cross-reference analysis for workspace refactoring

Fields§

§ast: Arc<Node>

The parsed AST for the current document

Implementations§

Source§

impl<'a> DeclarationProvider<'a>

Source

pub fn new( ast: Arc<Node>, content: String, document_uri: String, ) -> DeclarationProvider<'a>

Creates a new declaration provider for the given AST and document.

§Arguments
  • ast - The parsed AST tree for declaration lookup
  • content - The source code content for text extraction
  • document_uri - The URI of the document being analyzed
§Performance
  • Initialization: <10μs for typical Perl files
  • Memory overhead: Minimal, shares AST reference
§Examples
use perl_parser::declaration::DeclarationProvider;
use perl_parser::ast::Node;
use std::sync::Arc;

let ast = Arc::new(Node::new_root());
let provider = DeclarationProvider::new(
    ast,
    "package MyPackage; sub example { }".to_string(),
    "file:///path/to/file.pl".to_string()
);
Source

pub fn with_parent_map( self, parent_map: &'a HashMap<*const Node, *const Node, FxBuildHasher>, ) -> DeclarationProvider<'a>

Configures the provider with a pre-built parent map for enhanced traversal.

The parent map enables efficient upward AST traversal for scope resolution and context analysis. Debug builds include comprehensive validation.

§Arguments
  • parent_map - Mapping from child nodes to their parents
§Performance
  • Parent lookup: O(1) hash table access
  • Validation overhead: Debug-only, ~100μs for large files
§Panics

In debug builds, panics if:

  • Parent map is empty for non-trivial AST
  • Root node has a parent (cycle detection)
  • Cycles detected in parent relationships
§Examples
use perl_parser::declaration::{DeclarationProvider, ParentMap};
use perl_parser::ast::Node;
use std::sync::Arc;

let ast = Arc::new(Node::new_root());
let mut parent_map = ParentMap::default();
DeclarationProvider::build_parent_map(&ast, &mut parent_map, None);

let provider = DeclarationProvider::new(
    ast, "content".to_string(), "uri".to_string()
).with_parent_map(&parent_map);
Source

pub fn with_doc_version(self, version: i32) -> DeclarationProvider<'a>

Sets the document version for staleness detection.

Version tracking ensures the provider operates on current data and prevents usage after document updates in LSP workflows.

§Arguments
  • version - Document version number from LSP client
§Performance
  • Version check: <1μs per operation
  • Debug validation: Additional consistency checks
§Examples
use perl_parser::declaration::DeclarationProvider;
use perl_parser::ast::Node;
use std::sync::Arc;

let provider = DeclarationProvider::new(
    Arc::new(Node::new_root()),
    "content".to_string(),
    "uri".to_string()
).with_doc_version(42);
Source

pub fn build_parent_map( node: &Node, map: &mut HashMap<*const Node, *const Node, FxBuildHasher>, parent: Option<*const Node>, )

Build a parent map for efficient scope walking Builds a parent map for efficient upward AST traversal.

Recursively traverses the AST to construct a mapping from each node to its parent, enabling O(1) parent lookups for scope resolution.

§Arguments
  • node - Current node to process
  • map - Mutable parent map to populate
  • parent - Parent of the current node (None for root)
§Performance
  • Time complexity: O(n) where n is node count
  • Space complexity: O(n) for parent pointers
  • Typical build time: <100μs for 1000-node AST
§Safety

Uses raw pointers for performance. Safe as long as AST nodes remain valid during provider lifetime.

§Examples
use perl_parser::declaration::{DeclarationProvider, ParentMap};
use perl_parser::ast::Node;

let ast = Node::new_root();
let mut parent_map = ParentMap::default();
DeclarationProvider::build_parent_map(&ast, &mut parent_map, None);
Source

pub fn find_declaration( &self, offset: usize, current_version: i32, ) -> Option<Vec<LocationLink>>

Find the declaration of the symbol at the given position

Source

pub fn get_node_text(&self, node: &Node) -> String

Extracts the source code text for a given AST node.

Returns the substring of the document content corresponding to the node’s location range. Used for symbol name extraction and text-based analysis.

§Arguments
  • node - AST node to extract text from
§Performance
  • Time complexity: O(m) where m is node text length
  • Memory: Creates owned string copy
  • Typical latency: <10μs for identifier names
§Examples
use perl_parser::declaration::DeclarationProvider;
use perl_parser::ast::Node;
use std::sync::Arc;

let provider = DeclarationProvider::new(
    Arc::new(Node::new_root()),
    "sub example { }".to_string(),
    "uri".to_string()
);
// let text = provider.get_node_text(&some_node);

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more