Skip to content

Commit

Permalink
feat: use egui code editor
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenLoc committed Mar 25, 2024
1 parent 02818ad commit 9bc80a7
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 190 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rust-analyzer.linkedProjects": [
"./Cargo.toml"
]
}
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ egui = { version = "0.26", default-features = true }
eframe = { version = "0.26", default-features = true, features = [
"default_fonts", # Embed the default egui fonts.
] }
egui_code_editor = { version = "0.2.2"}
ehttp = { version = "0.5"}
poll-promise = { version = "0.3", default-features = false }
serde_json = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn create_tree() -> egui_tiles::Tree<Pane> {

let root = tiles.insert_tab_tile(tabs);

egui_tiles::Tree::new(egui::Id::new("uniqueGlobal"),root, tiles, )
egui_tiles::Tree::new(egui::Id::new("uniqueGlobal"), root, tiles)
}

struct Pane {
Expand Down
138 changes: 0 additions & 138 deletions src/editor/highlighter.rs

This file was deleted.

69 changes: 19 additions & 50 deletions src/editor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use eframe::egui;
use egui_code_editor::{CodeEditor, ColorTheme};

use self::{
highlighter::{highlight, CodeTheme},
parser::Parser,
};
use self::parser::Parser;

mod highlighter;
mod parser;
mod syntax;

pub struct Editor {
text: String,
parser: Parser,
marker: usize,
syntax: egui_code_editor::Syntax,
}

const ABSTRACT: &str = r"# -------------------------------------------------------------------
Expand All @@ -31,14 +30,13 @@ impl Default for Editor {
text: ABSTRACT.to_owned(),
parser: Parser::default(),
marker: usize::default(),
syntax: syntax::hurl(),
}
}
}

impl Editor {
pub fn render(&mut self, ui: &mut egui::Ui) {
self.parser.parse(&self.text);
let theme = CodeTheme::default();

if let Some(err) = self.parser.try_to_get_err() {
self.marker = err.pos.line;
Expand All @@ -63,11 +61,6 @@ impl Editor {
ui.add_space(1.0);

ui.vertical(|ui| {
let mut layouter = |ui: &egui::Ui, string: &str, _wrap_width: f32| {
let layout_job = highlight(ui.ctx(), &theme, string);
ui.fonts(|f| f.layout_job(layout_job))
};

egui::ScrollArea::vertical()
.id_source("some inner 3")
.min_scrolled_height(750.0)
Expand All @@ -76,48 +69,24 @@ impl Editor {
.show(ui, |ui| {
ui.push_id("second_some", |ui| {
ui.horizontal_top(|ui| {
let mut current: String = self
.text
.lines()
.take(1000)
.enumerate()
.map(|(s, _)| {
(s + 1).to_string()
+ {
if s + 1 == self.marker {
" >"
} else {
""
}
}
+ "\n"
})
.collect();

egui::TextEdit::multiline(&mut current)
.font(egui::TextStyle::Monospace)
.interactive(false)
.desired_width(60.0)
.code_editor()
.font(egui::FontId::monospace(15.0))
.show(ui);

egui::TextEdit::multiline(&mut self.text)
.font(egui::TextStyle::Monospace)
.desired_width(f32::INFINITY)
.code_editor()
.lock_focus(true)
.layouter(&mut layouter)
.show(ui);
let mut editor = CodeEditor::default()
.id_source("code editor")
.with_rows(10)
.with_fontsize(14.0)
.with_theme(ColorTheme::SONOKAI)
.with_syntax(self.syntax.to_owned())
.with_numlines(true)
.vscroll(true);
editor.show(ui, &mut self.text);
});
});
});

if let Err(err) = self.parser.try_to_get_file() {
render_error(&err, ui);
}
if let Err(err) = self.parser.try_to_get_file() {
render_error(&err, ui);
}

ui.add_space(10.0);
ui.add_space(10.0);
});
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/editor/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ pub fn post(url: impl ToString, body: Vec<u8>) -> ehttp::Request {
]),
mode: ehttp::Mode::NoCors,
}
}
}
49 changes: 49 additions & 0 deletions src/editor/syntax.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use egui_code_editor::Syntax;
use std::collections::BTreeSet;

pub fn hurl() -> Syntax {
Syntax {
language: "Hurl",
case_sensitive: true,
comment: "#",
comment_multiline: ["#", "#"],
hyperlinks: BTreeSet::from([]),
keywords: BTreeSet::from([
"jsonpath",
"count",
"==",
">=",
"<=",
"<",
">",
"!=",
"not",
"isString",
"isCollection",
"isDate",
"isBoolean",
"isFloat",
"isInteger",
"includes",
"isEmpty",
"exists",
"matches",
"contains",
"endsWith",
"startsWith",
"",
]),
types: BTreeSet::from([
"[Captures]",
"[Asserts]",
"[FormParams]",
"[Captures]",
"[Asserts]",
"[Asserts]",
"HTTP",
]),
special: BTreeSet::from([
"GET", "POST", "HTTP", "HEAD", "PUT", "DELETE", "OPTIONS", "TRACE", "PATCH",
]),
}
}

0 comments on commit 9bc80a7

Please sign in to comment.