srgn/scoping/langs/
csharp.rs

1use std::fmt::Debug;
2
3use clap::ValueEnum;
4use const_format::formatcp;
5
6use super::{LanguageScoper, QuerySource, TSLanguage, TSQuery, TSQueryError};
7use crate::find::Find;
8use crate::scoping::langs::IGNORE;
9
10/// A compiled query for the C# language.
11#[derive(Debug)]
12pub struct CompiledQuery(super::CompiledQuery);
13
14impl TryFrom<QuerySource> for CompiledQuery {
15    type Error = TSQueryError;
16
17    /// Create a new compiled query for the C# language.
18    ///
19    /// # Errors
20    ///
21    /// See the concrete type of the [`TSQueryError`](tree_sitter::QueryError)variant for when this method errors.
22    fn try_from(query: QuerySource) -> Result<Self, Self::Error> {
23        let q = super::CompiledQuery::from_source(&tree_sitter_c_sharp::LANGUAGE.into(), &query)
24            .expect("syntax of prepared queries is validated by tests");
25        Ok(Self(q))
26    }
27}
28
29impl From<PreparedQuery> for CompiledQuery {
30    fn from(query: PreparedQuery) -> Self {
31        Self(super::CompiledQuery::from_prepared_query(
32            &tree_sitter_c_sharp::LANGUAGE.into(),
33            query.as_str(),
34        ))
35    }
36}
37
38/// Prepared tree-sitter queries for C#.
39#[derive(Debug, Clone, Copy, ValueEnum)]
40pub enum PreparedQuery {
41    /// Comments (including XML, inline, doc comments).
42    Comments,
43    /// Strings (incl. verbatim, interpolated; incl. quotes, except for interpolated).
44    ///
45    /// Raw strings are [not yet
46    /// supported](https://github.com/tree-sitter/tree-sitter-c-sharp/pull/240).
47    Strings,
48    /// `using` directives (including periods).
49    Usings,
50    /// `struct` definitions (in their entirety).
51    Struct,
52    /// `enum` definitions (in their entirety).
53    Enum,
54    /// `interface` definitions (in their entirety).
55    Interface,
56    /// `class` definitions (in their entirety).
57    Class,
58    /// Method definitions (in their entirety).
59    Method,
60    /// Variable declarations (in their entirety).
61    VariableDeclaration,
62    /// Property definitions (in their entirety).
63    Property,
64    /// Constructor definitions (in their entirety).
65    Constructor,
66    /// Destructor definitions (in their entirety).
67    Destructor,
68    /// Field definitions on types (in their entirety).
69    Field,
70    /// Attribute names.
71    Attribute,
72    /// Identifier names.
73    Identifier,
74}
75
76impl PreparedQuery {
77    const fn as_str(self) -> &'static str {
78        match self {
79            Self::Comments => "(comment) @comment",
80            Self::Usings => r"(using_directive [(identifier) (qualified_name)] @import)",
81            Self::Strings => {
82                formatcp!(
83                    r"
84                    [
85                        (interpolated_string_expression (interpolation) @{0})
86                        (string_literal)
87                        (raw_string_literal)
88                        (verbatim_string_literal)
89                    ]
90                    @string
91                    ",
92                    IGNORE
93                )
94            }
95            Self::Struct => "(struct_declaration) @struct",
96            Self::Enum => "(enum_declaration) @enum",
97            Self::Interface => "(interface_declaration) @interface",
98            Self::Class => "(class_declaration) @class",
99            Self::Method => "(method_declaration) @method",
100            Self::VariableDeclaration => "(variable_declaration) @variable",
101            Self::Property => "(property_declaration) @property",
102            Self::Constructor => "(constructor_declaration) @constructor",
103            Self::Destructor => "(destructor_declaration) @destructor",
104            Self::Field => "(field_declaration) @field",
105            Self::Attribute => "(attribute) @attribute",
106            Self::Identifier => "(identifier) @identifier",
107        }
108    }
109}
110
111impl LanguageScoper for CompiledQuery {
112    fn lang() -> TSLanguage {
113        tree_sitter_c_sharp::LANGUAGE.into()
114    }
115
116    fn pos_query(&self) -> &TSQuery {
117        &self.0.positive_query
118    }
119
120    fn neg_query(&self) -> Option<&TSQuery> {
121        self.0.negative_query.as_ref()
122    }
123}
124
125impl Find for CompiledQuery {
126    fn extensions(&self) -> &'static [&'static str] {
127        &["cs"]
128    }
129}