use std::fmt::Debug;
use clap::ValueEnum;
use const_format::formatcp;
use super::{LanguageScoper, QuerySource, TSLanguage, TSQuery, TSQueryError};
use crate::find::Find;
use crate::scoping::langs::IGNORE;
#[derive(Debug)]
pub struct CompiledQuery(super::CompiledQuery);
impl TryFrom<QuerySource> for CompiledQuery {
type Error = TSQueryError;
fn try_from(query: QuerySource) -> Result<Self, Self::Error> {
let q = super::CompiledQuery::from_source(&tree_sitter_c_sharp::LANGUAGE.into(), &query)?;
Ok(Self(q))
}
}
impl From<PreparedQuery> for CompiledQuery {
fn from(query: PreparedQuery) -> Self {
Self(super::CompiledQuery::from_prepared_query(
&tree_sitter_c_sharp::LANGUAGE.into(),
query.as_str(),
))
}
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum PreparedQuery {
Comments,
Strings,
Usings,
Struct,
Enum,
Interface,
Class,
Method,
VariableDeclaration,
Property,
Constructor,
Destructor,
Field,
Attribute,
Identifier,
}
impl PreparedQuery {
const fn as_str(self) -> &'static str {
match self {
Self::Comments => "(comment) @comment",
Self::Usings => r"(using_directive [(identifier) (qualified_name)] @import)",
Self::Strings => {
formatcp!(
r"
[
(interpolated_string_expression (interpolation) @{0})
(string_literal)
(raw_string_literal)
(verbatim_string_literal)
]
@string
",
IGNORE
)
}
Self::Struct => "(struct_declaration) @struct",
Self::Enum => "(enum_declaration) @enum",
Self::Interface => "(interface_declaration) @interface",
Self::Class => "(class_declaration) @class",
Self::Method => "(method_declaration) @method",
Self::VariableDeclaration => "(variable_declaration) @variable",
Self::Property => "(property_declaration) @property",
Self::Constructor => "(constructor_declaration) @constructor",
Self::Destructor => "(destructor_declaration) @destructor",
Self::Field => "(field_declaration) @field",
Self::Attribute => "(attribute) @attribute",
Self::Identifier => "(identifier) @identifier",
}
}
}
impl LanguageScoper for CompiledQuery {
fn lang() -> TSLanguage {
tree_sitter_c_sharp::LANGUAGE.into()
}
fn pos_query(&self) -> &TSQuery {
&self.0.positive_query
}
fn neg_query(&self) -> Option<&TSQuery> {
self.0.negative_query.as_ref()
}
}
impl Find for CompiledQuery {
fn extensions(&self) -> &'static [&'static str] {
&["cs"]
}
}