From 5eab62858004493879172ff4576ca32ced8e6bea Mon Sep 17 00:00:00 2001 From: KCaverly Date: Thu, 13 Jul 2023 14:33:31 -0400 Subject: [PATCH] Added go parsing for semantic search, and added preceeding comments on go and rust. Co-authored-by: Alex Co-authored-by: maxbrunsfeld --- crates/language/src/language.rs | 4 -- crates/vector_store/src/parsing.rs | 23 ++++---- crates/zed/src/languages/go/embedding.scm | 24 +++++++++ crates/zed/src/languages/rust/embedding.scm | 58 ++++++++------------- 4 files changed, 55 insertions(+), 54 deletions(-) create mode 100644 crates/zed/src/languages/go/embedding.scm diff --git a/crates/language/src/language.rs b/crates/language/src/language.rs index dbd35f0e87bc602ac91aaf8196b81a4a017fff93..4ec5e88a7edde55c90e975c4bd944e3f38c1bb8b 100644 --- a/crates/language/src/language.rs +++ b/crates/language/src/language.rs @@ -525,7 +525,6 @@ pub struct EmbeddingConfig { pub item_capture_ix: u32, pub name_capture_ix: u32, pub context_capture_ix: Option, - pub extra_context_capture_ix: Option, } struct InjectionConfig { @@ -1246,14 +1245,12 @@ impl Language { let mut item_capture_ix = None; let mut name_capture_ix = None; let mut context_capture_ix = None; - let mut extra_context_capture_ix = None; get_capture_indices( &query, &mut [ ("item", &mut item_capture_ix), ("name", &mut name_capture_ix), ("context", &mut context_capture_ix), - ("context.extra", &mut extra_context_capture_ix), ], ); if let Some((item_capture_ix, name_capture_ix)) = item_capture_ix.zip(name_capture_ix) { @@ -1262,7 +1259,6 @@ impl Language { item_capture_ix, name_capture_ix, context_capture_ix, - extra_context_capture_ix, }); } Ok(self) diff --git a/crates/vector_store/src/parsing.rs b/crates/vector_store/src/parsing.rs index 91dcf699f8c3add9088b2af4f0d4df59b7551ac2..3e697399b1fa5b6dc2a42a29ec97f1e490643613 100644 --- a/crates/vector_store/src/parsing.rs +++ b/crates/vector_store/src/parsing.rs @@ -53,7 +53,7 @@ impl CodeContextRetriever { .ok_or_else(|| anyhow!("parsing failed"))?; let mut documents = Vec::new(); - let mut context_spans = Vec::new(); + let mut document_texts = Vec::new(); // Iterate through query matches for mat in self.cursor.matches( @@ -61,11 +61,10 @@ impl CodeContextRetriever { tree.root_node(), content.as_bytes(), ) { - // log::info!("-----MATCH-----"); - let mut name: Vec<&str> = vec![]; let mut item: Option<&str> = None; let mut offset: Option = None; + let mut context_spans: Vec<&str> = vec![]; for capture in mat.captures { if capture.index == embedding_config.item_capture_ix { offset = Some(capture.node.byte_range().start); @@ -79,25 +78,21 @@ impl CodeContextRetriever { if let Some(context_capture_ix) = embedding_config.context_capture_ix { if capture.index == context_capture_ix { if let Some(context) = content.get(capture.node.byte_range()) { - name.push(context); + context_spans.push(context); } } } } if item.is_some() && offset.is_some() && name.len() > 0 { - let context_span = CODE_CONTEXT_TEMPLATE + let item = format!("{}\n{}", context_spans.join("\n"), item.unwrap()); + + let document_text = CODE_CONTEXT_TEMPLATE .replace("", pending_file.relative_path.to_str().unwrap()) .replace("", &pending_file.language.name().to_lowercase()) - .replace("", item.unwrap()); - - let mut truncated_span = context_span.clone(); - truncated_span.truncate(100); - - // log::info!("Name: {:?}", name); - // log::info!("Span: {:?}", truncated_span); + .replace("", item.as_str()); - context_spans.push(context_span); + document_texts.push(document_text); documents.push(Document { name: name.join(" "), offset: offset.unwrap(), @@ -112,7 +107,7 @@ impl CodeContextRetriever { mtime: pending_file.modified_time, documents, }, - context_spans, + document_texts, )); } } diff --git a/crates/zed/src/languages/go/embedding.scm b/crates/zed/src/languages/go/embedding.scm new file mode 100644 index 0000000000000000000000000000000000000000..9d8700cdfb57d1008acc09c11013f2046e7bd157 --- /dev/null +++ b/crates/zed/src/languages/go/embedding.scm @@ -0,0 +1,24 @@ +( + (comment)* @context + . + (type_declaration + (type_spec + name: (_) @name) + ) @item +) + +( + (comment)* @context + . + (function_declaration + name: (_) @name + ) @item +) + +( + (comment)* @context + . + (method_declaration + name: (_) @name + ) @item +) diff --git a/crates/zed/src/languages/rust/embedding.scm b/crates/zed/src/languages/rust/embedding.scm index ea8bab9f68113a9b725e094a4d31f3e572c4bed7..3aec101e9fbb5d63a49db52869f34757135b0ab2 100644 --- a/crates/zed/src/languages/rust/embedding.scm +++ b/crates/zed/src/languages/rust/embedding.scm @@ -1,36 +1,22 @@ -(struct_item - (visibility_modifier)? @context - "struct" @context - name: (_) @name) @item - -(enum_item - (visibility_modifier)? @context - "enum" @context - name: (_) @name) @item - -(impl_item - "impl" @context - trait: (_)? @name - "for"? @context - type: (_) @name) @item - -(trait_item - (visibility_modifier)? @context - "trait" @context - name: (_) @name) @item - -(function_item - (visibility_modifier)? @context - (function_modifiers)? @context - "fn" @context - name: (_) @name) @item - -(function_signature_item - (visibility_modifier)? @context - (function_modifiers)? @context - "fn" @context - name: (_) @name) @item - -(macro_definition - . "macro_rules!" @context - name: (_) @name) @item +( + (line_comment)* @context + . + [ + (enum_item + name: (_) @name) @item + (struct_item + name: (_) @name) @item + (impl_item + trait: (_)? @name + "for"? @name + type: (_) @name) @item + (trait_item + name: (_) @name) @item + (function_item + name: (_) @name) @item + (macro_definition + name: (_) @name) @item + (function_signature_item + name: (_) @name) @item + ] +)