A simple CPU rendered GUI IDE experience.
indents on enter
bendn 7 months ago
parent f91d130 · commit 06a42df
-rw-r--r--Cargo.toml2
-rw-r--r--src/main.rs3
-rw-r--r--src/text.rs17
3 files changed, 18 insertions, 4 deletions
diff --git a/Cargo.toml b/Cargo.toml
index db5fff6..6c5f68d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -20,6 +20,8 @@ car = "0.1.2"
memchr = "2.7.5"
lower = "0.2.0"
amap = "0.1.2"
+bind = { git = "https://github.com/bend-n/bind", version = "0.1.0" }
+run_times = "0.1.0"
[build-dependencies]
cc = "*"
diff --git a/src/main.rs b/src/main.rs
index ae71b6b..3dbda22 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -176,8 +176,7 @@ pub(crate) fn entry(event_loop: EventLoop<()>) {
Named(ArrowRight)=> text.right(),
Named(ArrowUp) => text.up(),
Named(ArrowDown) => text.down(r),
- Named(Enter)=>
- text.insert("\n"),
+ Named(Enter)=> text.enter(),
Character(x) => {
text.insert(&*x);
}
diff --git a/src/text.rs b/src/text.rs
index de085e8..46bf764 100644
--- a/src/text.rs
+++ b/src/text.rs
@@ -68,12 +68,17 @@ impl TextArea {
}
#[implicit_fn::implicit_fn]
+ fn indentation(&self) -> usize {
+ let l = self.rope.line(self.rope.char_to_line(self.cursor));
+ l.chars().take_while(_.is_whitespace()).count()
+ }
+
+ #[implicit_fn::implicit_fn]
pub fn home(&mut self) {
let beg =
self.rope.line_to_char(self.rope.char_to_line(self.cursor));
let i = self.cursor - beg;
- let l = self.rope.line(self.rope.char_to_line(self.cursor));
- let whitespaces = l.chars().take_while(_.is_whitespace()).count();
+ let whitespaces = self.indentation();
if i == whitespaces {
self.cursor = beg;
self.column = 0;
@@ -100,6 +105,14 @@ impl TextArea {
self.setc();
}
+ pub fn enter(&mut self) {
+ use bind::*;
+ use run::Run;
+ let n = self.indentation();
+ self.insert("\n");
+ (|| self.insert(" ")).run(n);
+ }
+
pub fn down(&mut self, r: usize) {
let l = self.rope.try_char_to_line(self.cursor).unwrap_or(0);