Skip to content

Commit

Permalink
wip replace
Browse files Browse the repository at this point in the history
  • Loading branch information
nmeylan committed Jul 12, 2024
1 parent d94dbb8 commit a3d230b
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 26 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ eframe = { version = "0.28.1", default-features = false, features = [
"x11"] }
egui = { version = "0.28.1", default-features = false, features = [] }
egui_extras = { version = "0.28.1", default-features = false, features = ["svg"] }
json-flat-parser = {git = "https://github.com/nmeylan/json-parser-flat-format.git", rev = "2a953c7", features = ["indexmap", "simdutf8"]}
json-flat-parser = {git = "https://github.com/nmeylan/json-parser-flat-format.git", rev = "d6d52bf", features = ["indexmap", "simdutf8"]}
rayon = {version = "1.10.0"}
rfd = {version = "0.14.1"}
indexmap = "2.2.6"
Expand Down
17 changes: 15 additions & 2 deletions src/array_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use crate::components::icon::ButtonWithIcon;
use crate::components::popover::PopupMenu;
use crate::components::table::{CellLocation, TableBody, TableRow};
use crate::fonts::{COPY, FILTER, PENCIL, PLUS, TABLE, TABLE_CELLS, THUMBTACK};
use crate::parser::{row_number_entry, search_occurrences};
use crate::panels::SearchReplaceResponse;
use crate::parser::{replace_occurrences, row_number_entry, search_occurrences};
use crate::subtable_window::SubTable;

#[derive(Clone, Debug)]
Expand All @@ -31,6 +32,7 @@ pub struct Column {
pub value_type: ValueType,
pub seen_count: usize,
pub order: usize,
pub id: usize,
}

impl Hash for Column {
Expand All @@ -47,6 +49,7 @@ impl Column {
value_type,
seen_count: 0,
order: 0,
id: 0,
}
}
}
Expand Down Expand Up @@ -348,6 +351,7 @@ impl ArrayTable {
value_type: Default::default(),
seen_count: 0,
order: 0,
id: 0,
})
}
None
Expand Down Expand Up @@ -611,6 +615,7 @@ impl ArrayTable {
value_type: columns[col_index].value_type,
depth: columns[col_index].depth,
position: 0,
column_id: columns[col_index].id,
};
updated_value = Some((pointer, mem::take(ref_mut)))
} else {
Expand Down Expand Up @@ -824,6 +829,7 @@ impl ArrayTable {
value_type: ValueType::Array(self.nodes.len()),
depth: 0,
position: 0,
column_id: 0,
};
entries.push(FlatJsonValue { pointer: parent_pointer.clone(), value: None });
let updated_array = serialize_to_json_with_option::<String>(&mut entries, updated_pointer.depth - 1).to_json();
Expand Down Expand Up @@ -873,6 +879,7 @@ impl ArrayTable {
value_type: ValueType::Object(true),
depth,
position: 0,
column_id: 0,
}, value: Some("{}".to_string()) }
],
index: new_index
Expand Down Expand Up @@ -1057,7 +1064,7 @@ impl ArrayTable {
let columns = self.columns(cell_location.is_pinned_column_table);
let pointer = Self::pointer_key(&self.parent_pointer, row_index, &columns.get(cell_location.column_index).as_ref().unwrap().name);
let flat_json_value = FlatJsonValue::<String> {
pointer: PointerKey { pointer, value_type: columns[cell_location.column_index].value_type, depth: columns[cell_location.column_index].depth, position: 0, },
pointer: PointerKey { pointer, value_type: columns[cell_location.column_index].value_type, depth: columns[cell_location.column_index].depth, position: 0, column_id: columns[cell_location.column_index].id },
value: None,
};
self.update_value(flat_json_value, row_index, !self.is_sub_table);
Expand All @@ -1071,6 +1078,7 @@ impl ArrayTable {
value_type: columns[cell_location.column_index].value_type,
depth: columns[cell_location.column_index].depth,
position: 0,
column_id: columns[cell_location.column_index].id,
},
value: Some(v.clone()),
};
Expand All @@ -1096,4 +1104,9 @@ impl ArrayTable {
ui.ctx().copy_text(value.clone());
}
}

pub fn replace_columns(&mut self, search_replace_response: SearchReplaceResponse) {
replace_occurrences(&mut self.nodes, search_replace_response);
self.cache.borrow_mut().evict();
}
}
52 changes: 38 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ pub mod fonts;
mod web;
mod compatibility;
mod panels;
mod replace_panel;

use std::{env, mem};

use std::any::Any;
use std::collections::{BTreeSet};
use std::fs::File;
use std::io::Read;
Expand All @@ -25,13 +26,13 @@ use eframe::{CreationContext, Renderer};
use eframe::egui::Context;
use eframe::Theme::Light;
use eframe::egui::{Align, Align2, Button, Color32, ComboBox, CursorIcon, Id, Key, KeyboardShortcut, Label, LayerId, Layout, Modifiers, Order, RichText, Sense, Separator, TextEdit, TextStyle, Vec2, Widget};

use eframe::epaint::text::TextWrapMode;
use json_flat_parser::{FlatJsonValue, JSONParser, ParseOptions, ValueType};
use crate::array_table::{ArrayTable, ScrollToRowMode};
use crate::components::icon;
use crate::components::table::HoverData;
use crate::fonts::{CHEVRON_DOWN, CHEVRON_UP};
use crate::panels::AboutPanel;
use crate::panels::{AboutPanel, SearchReplacePanel};
use crate::parser::save_to_file;

pub const ACTIVE_COLOR: Color32 = Color32::from_rgb(63, 142, 252);
Expand All @@ -48,7 +49,7 @@ pub trait View<R> {
}

/// Something to view
pub trait Window {
pub trait Window<R> {
/// Is the demo enabled for this integration?
fn is_enabled(&self, _ctx: &eframe::egui::Context) -> bool {
true
Expand All @@ -58,7 +59,9 @@ pub trait Window {
fn name(&self) -> &'static str;

/// Show windows, etc
fn show(&mut self, ctx: &eframe::egui::Context, open: &mut bool);
fn show(&mut self, ctx: &eframe::egui::Context, open: &mut bool) -> R;
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
}

#[derive(Default, Clone)]
Expand Down Expand Up @@ -117,8 +120,9 @@ fn main() {
struct MyApp {
frame_history: FrameHistory,
table: Option<ArrayTable>,
windows: Vec<Box<dyn Window>>,
open: BTreeSet<String>,
about_panel: AboutPanel,
search_replace_panel: SearchReplacePanel,
max_depth: u8,
depth: u8,
selected_file: Option<PathBuf>,
Expand Down Expand Up @@ -150,9 +154,10 @@ impl MyApp {
Self {
frame_history: FrameHistory::default(),
table: None,
windows: vec![Box::<AboutPanel>::default()],
max_depth: 0,
open: Default::default(),
about_panel: Default::default(),
search_replace_panel: Default::default(),
max_depth: 0,
depth: 0,
selected_file: None,
parsing_invalid: false,
Expand All @@ -166,11 +171,16 @@ impl MyApp {
}
}
pub fn windows(&mut self, ctx: &Context) {
let Self { windows, open, .. } = self;
for window in windows {
let mut is_open = open.contains(window.name());
window.show(ctx, &mut is_open);
set_open(open, window.name(), is_open);
let Self { open, .. } = self;
let mut is_open = open.contains(self.about_panel.name());
self.about_panel.show(ctx, &mut is_open);
set_open(open, self.about_panel.name(), is_open);
let mut is_open = open.contains(self.search_replace_panel.name());
let response = self.search_replace_panel.show(ctx, &mut is_open);
set_open(open, self.search_replace_panel.name(), is_open);
if let Some(search_replace_response) = response {
let table = self.table.as_mut().unwrap();
table.replace_columns(search_replace_response);
}
}

Expand Down Expand Up @@ -321,6 +331,11 @@ impl MyApp {
self.unsaved_changes = false;
}
}

fn open_replace_panel(&mut self) {
set_open(&mut self.open, "Replace", true);
self.search_replace_panel.set_columns(self.table.as_ref().unwrap().all_columns().clone());
}
}

fn set_open(open: &mut BTreeSet<String>, key: &'static str, is_open: bool) {
Expand Down Expand Up @@ -356,7 +371,7 @@ impl eframe::App for MyApp {
#[cfg(not(target_arch = "wasm32"))] {
ui.menu_button("File", |ui| {
ui.set_min_width(220.0);
ui.style_mut().wrap = Some(false);
ui.style_mut().wrap_mode = Some(TextWrapMode::Extend);
if ui.button("Open json file").clicked() {
ui.close_menu();
self.file_picker();
Expand All @@ -375,6 +390,15 @@ impl eframe::App for MyApp {
}
});
}

ui.separator();
ui.menu_button("Edit", |ui| {
ui.set_min_width(220.0);
if ui.button("Replace").clicked() {
ui.close_menu();
self.open_replace_panel();
}
});
}
if let Some(ref mut table) = self.table {
ui.separator();
Expand Down
1 change: 1 addition & 0 deletions src/object_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ impl ObjectTable {
value_type: ValueType::Array(array_entries.len()),
depth: 0,
position: 0,
column_id: 0,
};
array_entries.push(FlatJsonValue { pointer: parent_pointer, value: None });
let updated_array = serialize_to_json_with_option::<String>(&mut array_entries, depth + 1).to_json();
Expand Down
Loading

0 comments on commit a3d230b

Please sign in to comment.