full search structure

This commit is contained in:
Penwing 2024-01-22 17:03:57 +01:00
parent af1f57df36
commit c87ad99dcc
4 changed files with 58 additions and 16 deletions

2
Cargo.lock generated
View file

@ -570,7 +570,7 @@ dependencies = [
[[package]]
name = "calcifer"
version = "0.1.0"
version = "0.1.1"
dependencies = [
"eframe",
"egui_code_editor",

View file

@ -1,4 +1,5 @@
use eframe::egui;
use eframe::egui::{text::CCursor, text_edit::CCursorRange};
use std::{env, path::Path, path::PathBuf, cmp::max, io, fs, cmp::min};
use crate::tools;
use crate::tools::themes::CustomColorTheme;
@ -119,7 +120,7 @@ impl super::Calcifer {
}
pub fn draw_content_panel(&mut self, ctx: &egui::Context) {
egui::CentralPanel::default().show(ctx, |ui| {
egui::CentralPanel::default().show(ctx, |ui| {
ui.horizontal(|ui| {
ui.label("Picked file:");
ui.monospace(self.tabs[self.selected_tab.to_index()].path.to_string_lossy().to_string());
@ -216,7 +217,7 @@ impl super::Calcifer {
let lines = current_tab.code.chars().filter(|&c| c == '\n').count() + 1;
egui::ScrollArea::vertical().show(ui, |ui| {
CodeEditor::default()
let mut output = CodeEditor::default()
.id_source("code editor")
.with_rows(max(80, lines))
.with_fontsize(14.0)
@ -224,6 +225,14 @@ impl super::Calcifer {
.with_syntax(tools::to_syntax(&current_tab.language))
.with_numlines(true)
.show(ui, &mut current_tab.code);
if !self.search.result_selected {
output.state.set_ccursor_range(Some(CCursorRange::two(
CCursor::new(self.search.get_cursor_start()),
CCursor::new(self.search.get_cursor_end()),
)));
println!("Changed Cursor : from {} to {}", self.search.get_cursor_start(), self.search.get_cursor_end());
self.search.result_selected = true;
}
});
if current_tab.history.len() < 1 {

View file

@ -104,6 +104,11 @@ impl eframe::App for Calcifer {
if self.searching {
self.search.show(ctx, &mut self.searching, &mut self.tabs, &mut self.selected_tab);
}
if !self.search.tab_selected && self.search.get_tab() != self.selected_tab {
self.selected_tab = self.search.get_tab();
}
self.search.tab_selected = true;
}
fn on_exit(&mut self, _gl : std::option::Option<&eframe::glow::Context>) {

View file

@ -32,10 +32,15 @@ impl Default for Selection {
pub struct SearchWindow {
search_text: String,
searched_text: String,
results: Vec<Selection>,
current_result: Selection,
across_documents: bool,
replace_text: String,
across_documents: bool,
results: Vec<Selection>,
current_result: usize,
pub tab_selected: bool,
pub result_selected: bool,
}
@ -44,10 +49,15 @@ impl Default for SearchWindow {
Self {
search_text: "".into(),
searched_text: "".into(),
results: vec![],
current_result: Selection::default(),
across_documents: false,
replace_text: "".into(),
across_documents: false,
results: vec![],
current_result: 0,
tab_selected: true,
result_selected: true,
}
}
}
@ -115,6 +125,18 @@ impl View for SearchWindow {
}
impl SearchWindow {
pub fn get_tab(&self) -> TabNumber {
self.results[self.current_result].tab.clone()
}
pub fn get_cursor_start(&self) -> usize {
self.results[self.current_result].start.clone()
}
pub fn get_cursor_end(&self) -> usize {
self.results[self.current_result].end.clone()
}
fn search(&mut self, tabs: &Vec<Tab>, selected_tab: &TabNumber) {
let mut search_results: Vec<Selection> = vec![];
@ -130,9 +152,7 @@ impl SearchWindow {
self.searched_text = self.search_text.clone();
self.results = search_results.clone();
if self.results.len() > 0 {
self.current_result = self.results[0].clone();
}
self.current_result = 0;
}
fn match_text(&self, tab_text: String, tab_number: TabNumber) -> Vec<Selection> {
@ -148,8 +168,12 @@ impl SearchWindow {
fn find_next(&mut self, tabs: &Vec<Tab>, selected_tab: &TabNumber) {
if self.searched_text != self.search_text && self.search_text.len() > 1 {
self.search(tabs, selected_tab);
} else {
println!("just need to get next result");
} else if self.results.len() > 1 {
self.current_result = (self.current_result.clone() + 1) % self.results.len();
self.result_selected = false;
if self.results[self.current_result].tab != *selected_tab {
self.tab_selected = false;
}
}
}
@ -157,11 +181,15 @@ impl SearchWindow {
if self.searched_text != self.search_text && self.search_text.len() > 1 {
self.search(tabs, selected_tab);
} else {
println!("just need to get next result");
self.current_result = (self.current_result.clone() - 1 + self.results.len()) % self.results.len();
self.result_selected = false;
if self.results[self.current_result].tab != *selected_tab {
self.tab_selected = false;
}
}
}
fn replace(&mut self, tabs: &Vec<Tab>, selected_tab: &TabNumber) {
println!("Searched to replace {} with {}", &self.search_text, &self.replace_text);
println!("Searched to replace {} with {}, tab lang : {} ", &self.search_text, &self.replace_text, tabs[TabNumber::to_index(selected_tab)].language);
}
}