A simple CPU rendered GUI IDE experience.
file picking
bendn 7 weeks ago
parent 576f932 · commit 1cb65d4
-rw-r--r--Cargo.toml1
-rw-r--r--src/commands.rs3
-rw-r--r--src/edi.rs49
-rw-r--r--src/edi/input_handlers/keyboard.rs37
4 files changed, 58 insertions, 32 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 1d1b828..0d879c8 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -77,6 +77,7 @@ json_value_merge = "2.0.1"
annotate-snippets = "0.12.16"
toml = "1.1.2"
bendncode = { git = "https://git.bendn.org/bendncode" }
+rfd = "0.17.2"
[profile.dev.package]
rust-analyzer.opt-level = 3
fimg.opt-level = 3
diff --git a/src/commands.rs b/src/commands.rs
index 2a56aac..4498513 100644
--- a/src/commands.rs
+++ b/src/commands.rs
@@ -115,6 +115,8 @@ commands!(
@ Incoming: "callers-of",
/// Functions this function calls
@ Outgoing: "calling",
+ /// Reloads the file from disk.
+ | Reload: "reload",
// /// View child modules
// @ ViewChildModules: "child-modules",
/// GoTo line,
@@ -272,6 +274,7 @@ impl Editor {
..default()
});
}
+ Cmd::Reload => self.reload(),
z if z.needs_lsp() => return self.handle_lsp_command(z, w),
x => unimplemented!("{x:?}"),
}
diff --git a/src/edi.rs b/src/edi.rs
index 75b3e84..beac4ab 100644
--- a/src/edi.rs
+++ b/src/edi.rs
@@ -201,14 +201,17 @@ impl Editor {
let mut o = std::env::args()
.nth(1)
.and_then(|x| PathBuf::try_from(x).ok())
- .and_then(|x| x.canonicalize().ok());
+ .and_then(|x| x.canonicalize().ok())
+ .or_else(|| {
+ rfd::FileDialog::new()
+ .set_can_create_directories(true)
+ .set_title("pick dir to open")
+ .pick_folder()
+ });
if let Some(x) = &o {
match std::fs::read_to_string(x) {
- Ok(x) => {
- me.text.insert(&x);
- me.text.cursor = default();
- }
+ Ok(_) => {}
Err(e)
if e.kind() == ErrorKind::IsADirectory
&& let h = hash(&x)
@@ -227,12 +230,24 @@ impl Editor {
.clone(),
);
}
+ Err(e) if e.kind() == ErrorKind::IsADirectory =>
+ o = rfd::FileDialog::new()
+ .set_directory(x)
+ .set_can_create_directories(true)
+ .set_title("pick file in project to open")
+ .pick_file(),
Err(e) => {
eprintln!("path could not be opened: {e}");
std::process::exit(5);
}
}
};
+ if let Some(o) = o.as_deref()
+ && let o = std::fs::read_to_string(o).unwrap()
+ {
+ me.text.insert(&o);
+ me.text.cursor = default();
+ }
let n = o.as_deref().and_then(|o| LOADER.language_for_filename(o));
me.language = n;
@@ -309,7 +324,7 @@ impl Editor {
.map(|x| x.path().to_owned())
.collect::<Vec<_>>()
});
- let l = me.workspace.as_ref().zip(l).map(|(workspace, l)| {
+ let l = me.workspace.as_ref().zip(l).and_then(|(workspace, l)| {
let (Connection { sender, receiver }, conf) = if l.language_id
== "rust"
{
@@ -338,11 +353,14 @@ impl Editor {
.ok()
.zip(Some((lc, l)))
})
- .ok_or(report!(
- "no lsp for this language; install one of {:?}",
- l.language_servers
- ))
- .unwrap();
+ .ok_or_else(|| {
+ log::error!(
+ "no lsp for this language; install one of \
+ {:?}",
+ l.language_servers
+ )
+ })
+ .ok()?;
super let (x, _iot) =
Connection::stdio(
BufReader::new(c.stdout.take().unwrap()),
@@ -365,7 +383,7 @@ impl Editor {
conf,
)
.unwrap();
- (&*Box::leak(Box::new(c)), (t2), Some(changed))
+ Some((&*Box::leak(Box::new(c)), (t2), Some(changed)))
});
let g = me.git_dir.clone();
if let Some(o) = me.origin.clone()
@@ -374,7 +392,12 @@ impl Editor {
let w = me.workspace.clone();
let la = me.language;
let t = me.tree.clone();
- assert!(me.files.len() != 0);
+ if me.files.len() != 0 {
+ log::error!(
+ "too many files? {:?}",
+ me.files.keys().collect::<Vec<_>>()
+ );
+ }
me.open_or_restore(&o, l, la, None, w)?;
me.git_dir = g;
me.tree = t;
diff --git a/src/edi/input_handlers/keyboard.rs b/src/edi/input_handlers/keyboard.rs
index e84f5c2..97d3b3f 100644
--- a/src/edi/input_handlers/keyboard.rs
+++ b/src/edi/input_handlers/keyboard.rs
@@ -522,25 +522,7 @@ impl Editor {
self.text.scroll_to_cursor_centering();
inlay!(self);
}
- Do::Boolean(BoolRequest::ReloadFile, true) => {
- self.hist.push_if_changed(&mut self.text);
- self.text.rope = Rope::from_str(
- &std::fs::read_to_string(
- self.origin.as_ref().unwrap(),
- )
- .unwrap(),
- );
-
- self.text.cursor.first_mut().position = self
- .text
- .cursor
- .first()
- .position
- .min(self.text.rope.len_chars());
- self.mtime = Self::modify(self.origin.as_deref());
- self.bar.last_action = "reloaded".into();
- self.hist.push(&mut self.text)
- }
+ Do::Boolean(BoolRequest::ReloadFile, true) => self.reload(),
Do::Boolean(BoolRequest::ReloadFile, false) => {}
Do::InsertCursor(dir) => {
self.text
@@ -863,4 +845,21 @@ impl Editor {
.request(lsp.runtime.spawn(fut));
}
}
+ pub fn reload(&mut self) {
+ self.hist.push_if_changed(&mut self.text);
+ self.text.rope = Rope::from_str(
+ &std::fs::read_to_string(self.origin.as_ref().unwrap())
+ .unwrap(),
+ );
+
+ self.text.cursor.first_mut().position = self
+ .text
+ .cursor
+ .first()
+ .position
+ .min(self.text.rope.len_chars());
+ self.mtime = Self::modify(self.origin.as_deref());
+ self.bar.last_action = "reloaded".into();
+ self.hist.push(&mut self.text)
+ }
}