srgn/scoping/langs/
csharp.rs1use 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#[derive(Debug)]
12pub struct CompiledQuery(super::CompiledQuery);
13
14impl TryFrom<QuerySource> for CompiledQuery {
15 type Error = TSQueryError;
16
17 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#[derive(Debug, Clone, Copy, ValueEnum)]
40pub enum PreparedQuery {
41 Comments,
43 Strings,
48 Usings,
50 Struct,
52 Enum,
54 Interface,
56 Class,
58 Method,
60 VariableDeclaration,
62 Property,
64 Constructor,
66 Destructor,
68 Field,
70 Attribute,
72 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}