refactor: move filtering logic to filter plugins
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
@@ -433,27 +433,15 @@ impl MetaPlugin for TextMetaPlugin {
|
||||
let head_bytes = self.base.options.get("head_bytes")
|
||||
.and_then(|v| v.as_u64())
|
||||
.map(|v| v as usize);
|
||||
let head_words = self.base.options.get("head_words")
|
||||
.and_then(|v| v.as_u64())
|
||||
.map(|v| v as usize);
|
||||
let head_lines = self.base.options.get("head_lines")
|
||||
.and_then(|v| v.as_u64())
|
||||
.map(|v| v as usize);
|
||||
let tail_bytes = self.base.options.get("tail_bytes")
|
||||
.and_then(|v| v.as_u64())
|
||||
.map(|v| v as usize);
|
||||
let tail_words = self.base.options.get("tail_words")
|
||||
.and_then(|v| v.as_u64())
|
||||
.map(|v| v as usize);
|
||||
let tail_lines = self.base.options.get("tail_lines")
|
||||
.and_then(|v| v.as_u64())
|
||||
.map(|v| v as usize);
|
||||
let line_start = self.base.options.get("line_start")
|
||||
.and_then(|v| v.as_u64())
|
||||
.map(|v| v as usize);
|
||||
let line_end = self.base.options.get("line_end")
|
||||
.and_then(|v| v.as_u64())
|
||||
.map(|v| v as usize);
|
||||
|
||||
// Build filter string from individual parameters
|
||||
let mut filter_parts = Vec::new();
|
||||
@@ -469,7 +457,6 @@ impl MetaPlugin for TextMetaPlugin {
|
||||
if let Some(lines) = tail_lines {
|
||||
filter_parts.push(format!("tail_lines({})", lines));
|
||||
}
|
||||
// TODO: Add support for head_words, tail_words, line_start, line_end in filter plugins
|
||||
|
||||
// Use the filter service to process data
|
||||
let processed_data = if !filter_parts.is_empty() {
|
||||
@@ -577,39 +564,56 @@ impl MetaPlugin for TextMetaPlugin {
|
||||
let head_bytes = self.base.options.get("head_bytes")
|
||||
.and_then(|v| v.as_u64())
|
||||
.map(|v| v as usize);
|
||||
let head_words = self.base.options.get("head_words")
|
||||
.and_then(|v| v.as_u64())
|
||||
.map(|v| v as usize);
|
||||
let head_lines = self.base.options.get("head_lines")
|
||||
.and_then(|v| v.as_u64())
|
||||
.map(|v| v as usize);
|
||||
let tail_bytes = self.base.options.get("tail_bytes")
|
||||
.and_then(|v| v.as_u64())
|
||||
.map(|v| v as usize);
|
||||
let tail_words = self.base.options.get("tail_words")
|
||||
.and_then(|v| v.as_u64())
|
||||
.map(|v| v as usize);
|
||||
let tail_lines = self.base.options.get("tail_lines")
|
||||
.and_then(|v| v.as_u64())
|
||||
.map(|v| v as usize);
|
||||
let line_start = self.base.options.get("line_start")
|
||||
.and_then(|v| v.as_u64())
|
||||
.map(|v| v as usize);
|
||||
let line_end = self.base.options.get("line_end")
|
||||
.and_then(|v| v.as_u64())
|
||||
.map(|v| v as usize);
|
||||
|
||||
// If we haven't determined binary status yet, do it now with whatever we have
|
||||
if self.is_binary_content.is_none() {
|
||||
if let Some(buffer) = &self.buffer {
|
||||
if !buffer.is_empty() {
|
||||
// Build filter string from individual parameters
|
||||
let mut filter_parts = Vec::new();
|
||||
if let Some(bytes) = head_bytes {
|
||||
filter_parts.push(format!("head_bytes({})", bytes));
|
||||
}
|
||||
if let Some(lines) = head_lines {
|
||||
filter_parts.push(format!("head_lines({})", lines));
|
||||
}
|
||||
if let Some(bytes) = tail_bytes {
|
||||
filter_parts.push(format!("tail_bytes({})", bytes));
|
||||
}
|
||||
if let Some(lines) = tail_lines {
|
||||
filter_parts.push(format!("tail_lines({})", lines));
|
||||
}
|
||||
|
||||
// Apply content filtering to the buffer if needed
|
||||
let processed_buffer = if head_bytes.is_some() || head_words.is_some() || head_lines.is_some() {
|
||||
self.process_head(buffer, head_bytes, head_words, head_lines)
|
||||
} else if tail_bytes.is_some() || tail_words.is_some() || tail_lines.is_some() {
|
||||
self.process_tail(buffer, tail_bytes, tail_words, tail_lines)
|
||||
} else if line_start.is_some() || line_end.is_some() {
|
||||
self.process_line_range(buffer, line_start, line_end)
|
||||
let processed_buffer = if !filter_parts.is_empty() {
|
||||
let filter_str = filter_parts.join(" | ");
|
||||
let filter_service = crate::services::filter_service::FilterService::new();
|
||||
let mut filter_chain = filter_service.create_filter_chain(Some(&filter_str))
|
||||
.map_err(|e| {
|
||||
log::error!("Failed to create filter chain: {}", e);
|
||||
buffer.clone()
|
||||
})
|
||||
.unwrap_or_else(|_| buffer.clone());
|
||||
|
||||
// Process the data through the filter chain
|
||||
filter_service.process_data(&mut filter_chain, buffer)
|
||||
.and_then(|data| filter_service.finish_processing(&mut filter_chain).map(|mut finish_data| {
|
||||
data.extend_from_slice(&finish_data);
|
||||
data
|
||||
}))
|
||||
.unwrap_or_else(|e| {
|
||||
log::error!("Failed to process data through filter: {}", e);
|
||||
buffer.clone()
|
||||
})
|
||||
} else {
|
||||
buffer.clone()
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user