1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2025 Muvon Un Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! JSON language implementation for the indexer
use crate::indexer::languages::Language;
use tree_sitter::Node;
pub struct Json {}
impl Language for Json {
fn name(&self) -> &'static str {
"json"
}
fn get_ts_language(&self) -> tree_sitter::Language {
tree_sitter_json::LANGUAGE.into()
}
fn get_meaningful_kinds(&self) -> Vec<&'static str> {
vec!["object", "array"]
}
fn descend_first_kinds(&self) -> Vec<&'static str> {
// tree-sitter-json's document is a single top-level value, so without
// this an entire file's object/array becomes one oversized region.
// Descending finds nested object/array values first, falling back to
// the whole node only when it has none (e.g. a flat object with only
// scalar values — that case is a known, accepted gap: a flat file
// with thousands of scalar keys still becomes one region).
vec!["object", "array"]
}
fn extract_symbols(&self, node: Node, contents: &str) -> Vec<String> {
let mut symbols = Vec::new();
// For JSON we mostly care about property names (keys) in objects
if node.kind() == "object" {
self.extract_json_keys(node, contents, &mut symbols);
} else {
self.extract_identifiers(node, contents, &mut symbols);
}
// Deduplicate symbols before returning
symbols.sort();
symbols.dedup();
symbols
}
fn extract_identifiers(&self, node: Node, contents: &str, symbols: &mut Vec<String>) {
let kind = node.kind();
// For JSON, we're mostly interested in property names (keys)
if kind == "string" {
let parent_kind = node.parent().map(|p| p.kind()).unwrap_or("");
if parent_kind == "pair" {
// This is likely a key in a JSON object
if let Ok(text) = node.utf8_text(contents.as_bytes()) {
// Strip the quotes from the string
let t = text.trim_matches('"').trim();
// Dedup happens once in extract_symbols via sort+dedup on
// the full result, so no need to scan on every push here.
if !t.is_empty() {
symbols.push(t.to_string());
}
}
}
}
// Continue with recursive traversal
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
self.extract_identifiers(cursor.node(), contents, symbols);
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn are_node_types_equivalent(&self, type1: &str, type2: &str) -> bool {
// Direct match
if type1 == type2 {
return true;
}
// JSON-specific semantic groups
let semantic_groups = [
// JSON structures
&["object", "array"] as &[&str],
// JSON values
&["string", "number", "true", "false", "null"],
];
// Check if both types belong to the same semantic group
for group in &semantic_groups {
let contains_type1 = group.contains(&type1);
let contains_type2 = group.contains(&type2);
if contains_type1 && contains_type2 {
return true;
}
}
false
}
fn get_node_type_description(&self, node_type: &str) -> &'static str {
match node_type {
"object" => "JSON objects",
"array" => "JSON arrays",
"string" => "JSON strings",
"number" => "JSON numbers",
"true" | "false" => "JSON booleans",
"null" => "JSON null values",
"pair" => "JSON key-value pairs",
_ => "JSON structures",
}
}
fn resolve_import(
&self,
_import_path: &str,
_source_file: &str,
_all_files: &super::resolution_utils::FileRegistry,
) -> Option<String> {
// JSON doesn't have imports
None
}
fn get_file_extensions(&self) -> Vec<&'static str> {
vec!["json"]
}
}
impl Json {
/// Extract key names from JSON objects
#[allow(clippy::only_used_in_recursion)]
fn extract_json_keys(&self, node: Node, contents: &str, symbols: &mut Vec<String>) {
for child in node.children(&mut node.walk()) {
if child.kind() == "pair" {
let mut pair_cursor = child.walk();
if pair_cursor.goto_first_child() {
// The first child of a pair is the key
let key_node = pair_cursor.node();
if key_node.kind() == "string" {
if let Ok(text) = key_node.utf8_text(contents.as_bytes()) {
// Strip the quotes from the string
let t = text.trim_matches('"').trim();
// Dedup happens once in extract_symbols via sort+dedup on
// the full result, so no need to scan on every push here.
if !t.is_empty() {
symbols.push(t.to_string());
}
}
}
}
// Check if this is a nested object
if pair_cursor.goto_next_sibling() && pair_cursor.goto_next_sibling() {
// Skip the colon
let value_node = pair_cursor.node();
if value_node.kind() == "object" || value_node.kind() == "array" {
self.extract_json_keys(value_node, contents, symbols);
}
}
}
}
}
}