diff --git a/Cargo.lock b/Cargo.lock index 70beafd..c2c348a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -570,7 +570,7 @@ dependencies = [ [[package]] name = "calcifer" -version = "0.1.0" +version = "0.1.1" dependencies = [ "eframe", "egui_code_editor", diff --git a/src/calcifer.rs b/src/calcifer.rs index fce00f5..60c8cef 100644 --- a/src/calcifer.rs +++ b/src/calcifer.rs @@ -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(¤t_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 { diff --git a/src/main.rs b/src/main.rs index a37f03c..346d57b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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>) { diff --git a/src/tools/search.rs b/src/tools/search.rs index 8757b19..97b85c2 100644 --- a/src/tools/search.rs +++ b/src/tools/search.rs @@ -32,10 +32,15 @@ impl Default for Selection { pub struct SearchWindow { search_text: String, searched_text: String, - results: Vec, - current_result: Selection, - across_documents: bool, replace_text: String, + + across_documents: bool, + + results: Vec, + 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, selected_tab: &TabNumber) { let mut search_results: Vec = 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 { @@ -148,8 +168,12 @@ impl SearchWindow { fn find_next(&mut self, tabs: &Vec, 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, 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); } } \ No newline at end of file