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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use crate::autocomplete::autocomplete_state::{JsonFieldType, Suggestion, SuggestionType};
use crate::query::ResultType;
use serde_json::Value;
use std::collections::HashSet;
use std::sync::Arc;
pub struct ResultAnalyzer;
#[inline]
fn dot_prefix(needs_leading_dot: bool) -> &'static str {
if needs_leading_dot { "." } else { "" }
}
impl ResultAnalyzer {
/// Check if a field name can use jq's simple dot syntax (e.g., .foo)
/// According to jq manual: "The .foo syntax only works for simple, identifier-like keys,
/// that is, keys that are all made of alphanumeric characters and underscore,
/// and which do not start with a digit."
/// (https://jqlang.org/manual/#object-identifier-index)
/// Names that don't fit require quoted access: ."field-name"
fn is_simple_jq_identifier(name: &str) -> bool {
if name.is_empty() {
return false;
}
let first_char = name.chars().next().unwrap();
!first_char.is_numeric() && name.chars().all(|c| c.is_alphanumeric() || c == '_')
}
/// Format a field name for jq syntax, quoting if it doesn't fit simple dot syntax
fn format_field_name(prefix: &str, name: &str) -> String {
if Self::is_simple_jq_identifier(name) {
format!("{}{}", prefix, name)
} else {
format!("{}\"{}\"", prefix, name)
}
}
fn extract_object_fields(
map: &serde_json::Map<String, Value>,
prefix: &str,
suggestions: &mut Vec<Suggestion>,
) {
for (key, val) in map {
let field_type = Self::detect_json_type(val);
let field_text = Self::format_field_name(prefix, key);
suggestions.push(Suggestion::new_with_type(
field_text,
SuggestionType::Field,
Some(field_type),
));
}
}
/// Extract field suggestions from multiple array elements, deduplicating by key name.
/// Type detection uses the first occurrence of each key (first-occurrence-wins).
fn extract_union_fields_from_array(
arr: &[Value],
sample_size: usize,
prefix: &str,
suppress_array_brackets: bool,
seen_keys: &mut HashSet<String>,
suggestions: &mut Vec<Suggestion>,
) {
for element in arr.iter().take(sample_size) {
if let Value::Object(map) = element {
for (key, val) in map {
if seen_keys.insert(key.clone()) {
let field_type = Self::detect_json_type(val);
let field_text = if suppress_array_brackets {
Self::format_field_name(prefix, key)
} else if Self::is_simple_jq_identifier(key) {
format!("{}[].{}", prefix, key)
} else {
format!("{}[].\"{}\"", prefix, key)
};
suggestions.push(Suggestion::new_with_type(
field_text,
SuggestionType::Field,
Some(field_type),
));
}
}
}
}
}
/// Analyze multiple JSON values for field suggestions, deduplicating across all values.
/// Used when navigate_multi returns multiple values from fan-out array traversal.
pub fn analyze_multi_values(
values: &[&Value],
needs_leading_dot: bool,
suppress_array_brackets: bool,
array_sample_size: usize,
) -> Vec<Suggestion> {
let prefix = dot_prefix(needs_leading_dot);
let mut suggestions = Vec::new();
let mut seen_keys = HashSet::new();
let mut has_array = false;
for &value in values {
match value {
Value::Object(map) => {
for (key, val) in map {
if seen_keys.insert(key.clone()) {
let field_type = Self::detect_json_type(val);
let field_text = Self::format_field_name(prefix, key);
suggestions.push(Suggestion::new_with_type(
field_text,
SuggestionType::Field,
Some(field_type),
));
}
}
}
Value::Array(arr) => {
if !has_array && !suppress_array_brackets {
suggestions.push(Suggestion::new_with_type(
format!("{}[]", prefix),
SuggestionType::Pattern,
None,
));
has_array = true;
}
Self::extract_union_fields_from_array(
arr,
array_sample_size,
prefix,
suppress_array_brackets,
&mut seen_keys,
&mut suggestions,
);
}
_ => {}
}
}
suggestions
}
/// Analyze a JSON value for field suggestions, inferring type from the value itself.
///
/// Delegates to `analyze_multi_values` with a single-element slice.
#[allow(dead_code)]
pub fn analyze_value(
value: &Value,
needs_leading_dot: bool,
suppress_array_brackets: bool,
array_sample_size: usize,
) -> Vec<Suggestion> {
Self::analyze_multi_values(
&[value],
needs_leading_dot,
suppress_array_brackets,
array_sample_size,
)
}
/// Analyze pre-parsed JSON value for field suggestions
///
/// Optimized path that avoids re-parsing on every keystroke.
/// Critical for large files where parsing takes 50-100ms.
///
/// When `suppress_array_brackets` is true, field suggestions for arrays will
/// omit the `[]` prefix. This applies when:
/// - Inside element-context functions (map, select, etc.)
/// - Inside object construction {.field}
pub fn analyze_parsed_result(
value: &Arc<Value>,
result_type: ResultType,
needs_leading_dot: bool,
suppress_array_brackets: bool,
array_sample_size: usize,
) -> Vec<Suggestion> {
Self::extract_suggestions_for_type(
value,
result_type,
needs_leading_dot,
suppress_array_brackets,
array_sample_size,
)
}
fn extract_suggestions_for_type(
value: &Value,
result_type: ResultType,
needs_leading_dot: bool,
suppress_array_brackets: bool,
array_sample_size: usize,
) -> Vec<Suggestion> {
match result_type {
ResultType::ArrayOfObjects => {
let prefix = dot_prefix(needs_leading_dot);
let mut suggestions = Vec::new();
if !suppress_array_brackets {
suggestions.push(Suggestion::new_with_type(
format!("{}[]", prefix),
SuggestionType::Pattern,
None,
));
}
if let Value::Array(arr) = value {
let mut seen_keys = HashSet::new();
Self::extract_union_fields_from_array(
arr,
array_sample_size,
prefix,
suppress_array_brackets,
&mut seen_keys,
&mut suggestions,
);
}
suggestions
}
ResultType::DestructuredObjects => {
let prefix = dot_prefix(needs_leading_dot);
let mut suggestions = Vec::new();
if let Value::Object(map) = value {
Self::extract_object_fields(map, prefix, &mut suggestions);
}
suggestions
}
ResultType::Object => {
let prefix = dot_prefix(needs_leading_dot);
let mut suggestions = Vec::new();
if let Value::Object(map) = value {
Self::extract_object_fields(map, prefix, &mut suggestions);
}
suggestions
}
ResultType::Array => {
let prefix = dot_prefix(needs_leading_dot);
vec![Suggestion::new_with_type(
format!("{}[]", prefix),
SuggestionType::Pattern,
None,
)]
}
_ => Vec::new(),
}
}
fn detect_json_type(value: &Value) -> JsonFieldType {
match value {
Value::Null => JsonFieldType::Null,
Value::Bool(_) => JsonFieldType::Boolean,
Value::Number(_) => JsonFieldType::Number,
Value::String(_) => JsonFieldType::String,
Value::Array(arr) => {
if arr.is_empty() {
JsonFieldType::Array
} else {
let inner_type = Self::detect_json_type(&arr[0]);
JsonFieldType::ArrayOf(Box::new(inner_type))
}
}
Value::Object(_) => JsonFieldType::Object,
}
}
}
#[cfg(test)]
#[path = "result_analyzer_tests.rs"]
mod result_analyzer_tests;