Skip to main content

Doc

Trait Doc 

Source
pub trait Doc:
    Clone
    + Debug
    + Send
    + Sync
    + 'static {
    type Source: Content;
    type Lang: Language;
    type Node<'r>: SgNode<'r>;

    // Required methods
    fn get_lang(&self) -> &Self::Lang;
    fn get_source(&self) -> &Self::Source;
    fn do_edit(&mut self, edit: &Edit<Self::Source>) -> Result<(), String>;
    fn root_node(&self) -> Self::Node<'_>;
    fn get_node_text<'a>(&'a self, node: &Self::Node<'a>) -> Cow<'a, str>;
}
Expand description

Represents a source code document with its language and parsed AST.

Doc provides the core interface for working with parsed source code documents. It combines the source text, language information, and AST representation in a single abstraction that supports editing and node operations.

§Type Parameters

  • Source: Content - The text representation (String, UTF-16, etc.)
  • Lang: Language - The programming language implementation
  • Node: SgNode - The AST node implementation

§Example

// Documents provide access to source, language, and AST
let doc = StrDoc::new("const x = 42;", JavaScript);

// Access different aspects of the document
let source = doc.get_source();  // Get source text
let lang = doc.get_lang();      // Get language info
let root = doc.root_node();     // Get AST root

// Extract text from specific nodes
let node_text = doc.get_node_text(&some_node);

Required Associated Types§

Source

type Source: Content

The source code representation (String, UTF-16, etc.)

Source

type Lang: Language

The programming language implementation

Source

type Node<'r>: SgNode<'r>

The AST node type for this document

Required Methods§

Source

fn get_lang(&self) -> &Self::Lang

Get the language implementation for this document

Source

fn get_source(&self) -> &Self::Source

Get the source code content

Source

fn do_edit(&mut self, edit: &Edit<Self::Source>) -> Result<(), String>

Apply an edit to the document, updating both source and AST

Source

fn root_node(&self) -> Self::Node<'_>

Get the root AST node

Source

fn get_node_text<'a>(&'a self, node: &Self::Node<'a>) -> Cow<'a, str>

Extract the text content of a specific AST node

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<L: LanguageExt> Doc for StrDoc<L>

Source§

type Source = String

Source§

type Lang = L

Source§

type Node<'r> = Node<'r>