Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

fix(code_blocks): attach enclosing_block's doc to enclosing_block

bugercae648400e54
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index 75c54a337..846187039 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -7033,7 +7033,11 @@ fn referenced_query_fields(q: &Value, out: &mut Vec<String>) {
/// spent their largest payloads — 182 KB and 65 KB in one run — trial-and-
/// erroring field names before thinking to fetch `_mapping`. The engine knows
/// the schema; staying silent is a choice, and the wrong one.
-fn unknown_field_hint(index: &str, body: &EsSearchBody, schema: &xerj_common::types::Schema) -> Option<Value> {
+fn unknown_field_hint(
+ index: &str,
+ body: &EsSearchBody,
+ schema: &xerj_common::types::Schema,
+) -> Option<Value> {
let query = body.query.as_ref()?;
let mut referenced = Vec::new();
referenced_query_fields(query, &mut referenced);
@@ -7118,11 +7122,8 @@ fn exact_term_clauses(q: &Value, out: &mut Vec<(String, String)>) {
"term" => {
if let Value::Object(inner) = v {
for (f, val) in inner {
- let s = val
- .get("value")
- .unwrap_or(val)
- .as_str()
- .map(str::to_string);
+ let s =
+ val.get("value").unwrap_or(val).as_str().map(str::to_string);
if let Some(s) = s {
out.push((f.clone(), s));
}
@@ -12840,15 +12841,15 @@ async fn search_impl(
}
for (field, value) in clauses.into_iter().take(2) {
let base = field.split('.').next().unwrap_or(&field).to_string();
- let is_keyword = schema
- .field(&base)
- .is_some_and(|f| matches!(f.field_type, xerj_common::types::FieldType::Keyword));
+ let is_keyword = schema.field(&base).is_some_and(|f| {
+ matches!(f.field_type, xerj_common::types::FieldType::Keyword)
+ });
if !is_keyword {
continue;
}
- let Ok(sample_req) =
- xerj_query::parse_request(&json!({"query": {"match_all": {}}, "size": 256}))
- else {
+ let Ok(sample_req) = xerj_query::parse_request(
+ &json!({"query": {"match_all": {}}, "size": 256}),
+ ) else {
continue;
};
let Ok(sample) = idx.search(&sample_req).await else {
diff --git a/engine/crates/xerj-engine/src/code_blocks.rs b/engine/crates/xerj-engine/src/code_blocks.rs
index dba264fd8..f768fd8ec 100644
--- a/engine/crates/xerj-engine/src/code_blocks.rs
+++ b/engine/crates/xerj-engine/src/code_blocks.rs
@@ -93,7 +93,11 @@ const PHP_KINDS: &[&str] = &[
"trait_declaration",
];
-const PYTHON_KINDS: &[&str] = &["function_definition", "class_definition", "decorated_definition"];
+const PYTHON_KINDS: &[&str] = &[
+ "function_definition",
+ "class_definition",
+ "decorated_definition",
+];
const JS_KINDS: &[&str] = &[
"function_declaration",
@@ -277,6 +281,21 @@ fn cache_key(language: &str, body: &str) -> u64 {
xxhash_rust::xxh3::xxh3_64_with_seed(body.as_bytes(), seed)
}
+/// Syntactic wrappers that carry a declaration's modifiers rather than being
+/// blocks in their own right. A resolved block is widened through these so the
+/// passage shows the declaration as it is actually written — `export function
+/// f()`, not a bare `function f()` that reads as un-exported.
+///
+/// Language-agnostic by construction: a kind absent from a grammar simply never
+/// matches, so one table is safe for every language in the registry.
+const WRAPPER_KINDS: &[&str] = &[
+ // JavaScript / TypeScript
+ "export_statement",
+ "export_default_declaration",
+ // Python
+ "decorated_definition",
+];
+
// ── Public API ────────────────────────────────────────────────────────────
/// Find the smallest node enclosing byte range `[start, end)` in `body`
@@ -295,22 +314,12 @@ fn cache_key(language: &str, body: &str) -> u64 {
/// - the smallest acceptable block exceeds [`max_block_bytes`] — since any
/// larger enclosing block is only bigger, this stops at the first
/// candidate rather than searching further up.
-/// Syntactic wrappers that carry a declaration's modifiers rather than being
-/// blocks in their own right. A resolved block is widened through these so the
-/// passage shows the declaration as it is actually written — `export function
-/// f()`, not a bare `function f()` that reads as un-exported.
-///
-/// Language-agnostic by construction: a kind absent from a grammar simply never
-/// matches, so one table is safe for every language in the registry.
-const WRAPPER_KINDS: &[&str] = &[
- // JavaScript / TypeScript
- "export_statement",
- "export_default_declaration",
- // Python
- "decorated_definition",
-];
-
-pub fn enclosing_block(language: &str, body: &str, start: usize, end: usize) -> Option<(usize, usize)> {
+pub fn enclosing_block(
+ language: &str,
+ body: &str,
+ start: usize,
+ end: usize,
+) -> Option<(usize, usize)> {
if start > end || end > body.len() {
return None;
}
@@ -366,7 +375,8 @@ mod tests {
let needle = "y * 2";
let start = body.find(needle).unwrap();
let end = start + needle.len();
- let (s, e) = enclosing_block("rust", body, start, end).expect("expected an enclosing block");
+ let (s, e) =
+ enclosing_block("rust", body, start, end).expect("expected an enclosing block");
let block = &body[s..e];
assert!(
block.starts_with("fn target"),
@@ -397,7 +407,8 @@ mod tests {
let needle = "return y";
let start = body.find(needle).unwrap();
let end = start + needle.len();
- let (s, e) = enclosing_block("python", body, start, end).expect("expected an enclosing block");
+ let (s, e) =
+ enclosing_block("python", body, start, end).expect("expected an enclosing block");
let block = &body[s..e];
assert!(
block.starts_with("def target"),
@@ -440,7 +451,8 @@ mod tests {
#[test]
fn go_package_level_const_resolves_to_its_declaration() {
- let body = "package p\n\nconst (\n\tDefaultTimeout = 30\n\tMaxRetries = 5\n)\n\nfunc f() {}\n";
+ let body =
+ "package p\n\nconst (\n\tDefaultTimeout = 30\n\tMaxRetries = 5\n)\n\nfunc f() {}\n";
let needle = "MaxRetries";
let start = body.find(needle).unwrap();
let (s, e) = enclosing_block("go", body, start, start + needle.len())
@@ -503,7 +515,10 @@ mod tests {
#[test]
fn unsupported_language_falls_back() {
- assert_eq!(enclosing_block("cobol", "IDENTIFICATION DIVISION.", 0, 5), None);
+ assert_eq!(
+ enclosing_block("cobol", "IDENTIFICATION DIVISION.", 0, 5),
+ None
+ );
}
#[test]
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index 81898d806..a2fbe078d 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -25305,8 +25305,13 @@ fn build_highlight_fragments(
let hi = (le + half_window).min(lower_len);
// Snap to char boundaries: text_lower is UTF-8 and a naive slice
// could land mid-codepoint.
- let lo = (0..=lo).rev().find(|&i| text_lower.is_char_boundary(i)).unwrap_or(0);
- let hi = (hi..=lower_len).find(|&i| text_lower.is_char_boundary(i)).unwrap_or(lower_len);
+ let lo = (0..=lo)
+ .rev()
+ .find(|&i| text_lower.is_char_boundary(i))
+ .unwrap_or(0);
+ let hi = (hi..=lower_len)
+ .find(|&i| text_lower.is_char_boundary(i))
+ .unwrap_or(lower_len);
let window = &text_lower[lo..hi];
let distinct = terms.iter().filter(|t| window.contains(t.as_str())).count();
(distinct, orig)
@@ -27213,7 +27218,10 @@ fn refine_code_passage(
if field != "body" {
return None;
}
- if !matches!(get_field_value(source, "symbol_count"), Some(Value::Number(_))) {
+ if !matches!(
+ get_field_value(source, "symbol_count"),
+ Some(Value::Number(_))
+ ) {
return None;
}
let language = get_field_value(source, "language")?;
@@ -27636,7 +27644,13 @@ fn apply_lexical_passages(hits: Vec<Hit>, query: &QueryNode) -> Vec<Hit> {
})
.and_then(|anchor| {
let anchor_end = (anchor + 1).min(text.len());
- refine_code_passage(&hit.source, field_name, text, anchor, anchor_end)
+ refine_code_passage(
+ &hit.source,
+ field_name,
+ text,
+ anchor,
+ anchor_end,
+ )
})
.unwrap_or((start, end));
best = Some((