Skip to content

Commit

Permalink
Fixed selection bug when finding or replacing text
Browse files Browse the repository at this point in the history
Fixed bug where the selection wouldn't reset after finding or replacing text
Also added functionality to find to select text being searched for
  • Loading branch information
RaspberryProgramming authored and jackpot51 committed Oct 7, 2024
1 parent 3fe4380 commit 5546ed9
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cosmic::{
widget::icon,
};
use cosmic_files::mime_icon::{mime_for_path, mime_icon, FALLBACK_MIME_ICON};
use cosmic_text::{Attrs, Buffer, Edit, Shaping, SyntaxEditor, ViEditor, Wrap};
use cosmic_text::{Attrs, Buffer, Cursor, Edit, Selection, Shaping, SyntaxEditor, ViEditor, Wrap};
use notify::Watcher;
use regex::Regex;
use std::{
Expand Down Expand Up @@ -235,6 +235,8 @@ impl EditorTab {
editor.delete_range(cursor, end);
cursor = editor.insert_at(cursor, replace, None);
editor.set_cursor(cursor);
// Need to disable selection to prevent the new cursor showing selection to old location
editor.set_selection(Selection::None);
editor.finish_change();

return true;
Expand All @@ -261,20 +263,25 @@ impl EditorTab {

if forwards {
while cursor.line < editor.with_buffer(|buffer| buffer.lines.len()) {
if let Some(index) = editor.with_buffer(|buffer| {
if let Some((start, end)) = editor.with_buffer(|buffer| {
regex
.find_iter(buffer.lines[cursor.line].text())
.filter_map(|m| {
if cursor.line != start_line || m.start() > cursor.index {
Some(m.start())
Some((m.start(), m.end()))
} else {
None
}
})
.next()
}) {
cursor.index = index;
cursor.index = start;
editor.set_cursor(cursor);

// Highlight searched text
let selection = Selection::Normal(Cursor::new(cursor.line, end));
editor.set_selection(selection);

return true;
}

Expand All @@ -292,20 +299,25 @@ impl EditorTab {
while cursor.line > 0 {
cursor.line -= 1;

if let Some(index) = editor.with_buffer(|buffer| {
if let Some((start, end)) = editor.with_buffer(|buffer| {
regex
.find_iter(buffer.lines[cursor.line].text())
.filter_map(|m| {
if cursor.line != start_line || m.start() < cursor.index {
Some(m.start())
Some((m.start(), m.end()))
} else {
None
}
})
.last()
}) {
cursor.index = index;
cursor.index = start;
editor.set_cursor(cursor);

// Highlight searched text
let selection = Selection::Normal(Cursor::new(cursor.line, end));
editor.set_selection(selection);

return true;
}

Expand Down Expand Up @@ -334,4 +346,4 @@ fn title_with_parent(path: &std::path::Path, file_name: &str) -> String {
Some(parent) => [parent, "/", file_name].concat(),
None => file_name.to_string(),
}
}
}

0 comments on commit 5546ed9

Please sign in to comment.