pub trait LanguageExt: Language {
// Required method
fn get_ts_language(&self) -> TSLanguage;
// Provided methods
fn ast_grep<S: AsRef<str>>(&self, source: S) -> AstGrep<StrDoc<Self>> { ... }
fn injectable_languages(&self) -> Option<&'static [&'static str]> { ... }
fn extract_injections<L: LanguageExt>(
&self,
_root: Node<'_, StrDoc<L>>,
) -> RapidMap<String, Vec<TSRange>> { ... }
}Expand description
Extension trait for languages that integrate with tree-sitter parsing.
LanguageExt extends the base Language trait with tree-sitter specific
functionality. Languages implementing this trait can be used with tree-sitter
parsers to create ASTs, handle language injections, and work with the
thread-ast-engine ecosystem.
§Key Capabilities
- AST Creation: Convenient methods to create AST-grep instances
- Tree-sitter Integration: Access to underlying tree-sitter language objects
- Language Injection: Support for multi-language documents (e.g., JavaScript in HTML)
- Parsing: Direct parsing of source code into
StrDocinstances
§Example Implementation
use thread_ast_engine::tree_sitter::{LanguageExt, TSLanguage};
use thread_ast_engine::Language;
#[derive(Clone)]
struct JavaScript;
impl Language for JavaScript {
// ... base Language implementation
}
impl LanguageExt for JavaScript {
fn get_ts_language(&self) -> TSLanguage {
tree_sitter_javascript::LANGUAGE.into()
}
}Required Methods§
Sourcefn get_ts_language(&self) -> TSLanguage
fn get_ts_language(&self) -> TSLanguage
Get the tree-sitter language object for parsing.
Returns the tree-sitter language grammar that this language uses for parsing source code. This is the core integration point between thread-ast-engine and tree-sitter.
§Returns
The tree-sitter TSLanguage object for this language
Provided Methods§
Sourcefn ast_grep<S: AsRef<str>>(&self, source: S) -> AstGrep<StrDoc<Self>>
fn ast_grep<S: AsRef<str>>(&self, source: S) -> AstGrep<StrDoc<Self>>
Create an AstGrep instance for parsing and pattern matching.
Convenience method that parses the source code and returns an AstGrep
instance ready for pattern matching and tree manipulation.
§Parameters
source- Source code to parse
§Example
let ast = JavaScript.ast_grep("const x = 42;");
let root = ast.root();Sourcefn injectable_languages(&self) -> Option<&'static [&'static str]>
fn injectable_languages(&self) -> Option<&'static [&'static str]>
List of languages that can be injected into this language.
For languages that support embedding other languages (like HTML with CSS/JavaScript),
returns the names of languages that can be injected. Returns None if this
language doesn’t support injections.
§Returns
Array of injectable language names, or None if no injections supported
Sourcefn extract_injections<L: LanguageExt>(
&self,
_root: Node<'_, StrDoc<L>>,
) -> RapidMap<String, Vec<TSRange>>
fn extract_injections<L: LanguageExt>( &self, _root: Node<'_, StrDoc<L>>, ) -> RapidMap<String, Vec<TSRange>>
Extract language injection regions from a parsed document.
Analyzes the AST to find regions where other languages are embedded.
For example, finds JavaScript code blocks within HTML <script> tags
or CSS within <style> tags.
Returns a map where keys are language names and values are lists of byte ranges in the source where that language appears.
See tree-sitter documentation for more details on language injection.
§Parameters
root- The root node of the document to analyze
§Returns
Map of language names to their byte ranges in the document
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.