srgn/scoping/langs/
typescript.rs1use std::fmt::Debug;
2
3use clap::ValueEnum;
4
5use super::{Find, LanguageScoper, QuerySource, TSLanguage, TSQuery, TSQueryError};
6
7#[derive(Debug)]
9pub struct CompiledQuery(super::CompiledQuery);
10
11impl TryFrom<QuerySource> for CompiledQuery {
12 type Error = TSQueryError;
13
14 fn try_from(query: QuerySource) -> Result<Self, Self::Error> {
20 let q = super::CompiledQuery::from_source(
21 &tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
22 &query,
23 )?;
24 Ok(Self(q))
25 }
26}
27
28impl From<PreparedQuery> for CompiledQuery {
29 fn from(query: PreparedQuery) -> Self {
30 Self(super::CompiledQuery::from_prepared_query(
31 &tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
32 query.as_str(),
33 ))
34 }
35}
36
37#[derive(Debug, Clone, Copy, ValueEnum)]
39pub enum PreparedQuery {
40 Comments,
42 Strings,
44 Imports,
46 Function,
48 AsyncFunction,
50 SyncFunction,
52 Method,
54 Constructor,
56 Class,
58 Enum,
60 Interface,
62 TryCatch,
64 VarDecl,
66 Let,
68 Const,
70 Var,
72 TypeParams,
74 TypeAlias,
76 Namespace,
78 Export,
80}
81
82impl PreparedQuery {
83 const fn as_str(self) -> &'static str {
84 match self {
85 Self::Comments => "(comment) @comment",
86 Self::Imports => r"(import_statement source: (string (string_fragment) @sf))",
87 Self::Strings => "(string_fragment) @string",
88 Self::Function => "(function_declaration) @func",
89 Self::AsyncFunction => {
90 r#"(
91 (function_declaration) @func (#match? @func "^async")
92 )"#
93 }
94 Self::SyncFunction => {
95 r#"(
96 (function_declaration) @func (#not-match? @func "^async")
97 )"#
98 }
99 Self::Method => "(method_definition) @method",
100 Self::Constructor => {
101 r#"(method_definition
102 name: (_) @name (#eq? @name "constructor")
103 ) @constructor"#
104 }
105 Self::Class => "(class_declaration) @class",
106 Self::Enum => "(enum_declaration) @enum",
107 Self::Interface => "(interface_declaration) @interface",
108 Self::TryCatch => "(try_statement) @try",
109 Self::VarDecl => "(variable_declarator) @var_decl",
110 Self::Let => {
111 r#"(
112 (lexical_declaration) @let_decl (#match? @let_decl "^let ")
113 )"#
114 }
115 Self::Const => {
116 r#"(
117 (lexical_declaration) @const_decl (#match? @const_decl "^const ")
118 )"#
119 }
120 Self::Var => {
121 r#"(
122 (variable_declaration) @var_decl (#match? @var_decl "^var ")
123 )"#
124 }
125 Self::TypeParams => "(type_parameters) @type_parameters",
126 Self::TypeAlias => "(type_alias_declaration) @type_alias_declaration",
127 Self::Namespace => "(internal_module) @internal_module",
128 Self::Export => "(export_statement) @export",
129 }
130 }
131}
132
133impl LanguageScoper for CompiledQuery {
134 fn lang() -> TSLanguage {
135 tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into()
136 }
137
138 fn pos_query(&self) -> &TSQuery {
139 &self.0.positive_query
140 }
141
142 fn neg_query(&self) -> Option<&TSQuery> {
143 self.0.negative_query.as_ref()
144 }
145}
146
147impl Find for CompiledQuery {
148 fn extensions(&self) -> &'static [&'static str] {
149 &["ts", "tsx"]
150 }
151}