Skip to content

Commit

Permalink
refactor: tidying up expand logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sminez committed Sep 21, 2024
1 parent d202813 commit 0236db2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
11 changes: 9 additions & 2 deletions src/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl BufferKind {
match &self {
BufferKind::File(p) => p.parent(),
BufferKind::Directory(p) => Some(p.as_ref()),
BufferKind::Output(s) => Path::new(s).parent(),
_ => None,
}
}
Expand Down Expand Up @@ -770,7 +771,7 @@ impl Buffer {
};

let path = Path::new(fname);
if path.exists() {
if path.is_absolute() && path.exists() {
return Some(dot);
} else if let Some(dir) = self.dir() {
if dir.join(path).exists() {
Expand Down Expand Up @@ -1138,6 +1139,7 @@ pub(crate) mod tests {
use crate::key::Arrow;
use edit::tests::{del_c, del_s, in_c, in_s};
use simple_test_case::test_case;
use std::env;

#[test_case(0, 1; "n0")]
#[test_case(5, 1; "n5")]
Expand Down Expand Up @@ -1400,9 +1402,14 @@ pub(crate) mod tests {
#[test_case("http://example.com?foo=1&bar=2", Some("http://example.com?foo=1&bar=2"); "http url with multi query string")]
#[test]
fn try_expand_known_works(s: &str, expected: Option<&str>) {
let cwd = env::current_dir().unwrap().display().to_string();
// Check with surrounding whitespace and delimiters
for (l, r) in [(" ", " "), ("(", ")"), ("[", "]"), ("<", ">"), ("{", "}")] {
let b = Buffer::new_unnamed(0, &format!("abc_123 {l}{s}{r}\tmore text"));
let b = Buffer::new_output(
0,
format!("{cwd}/+output"),
format!("abc_123 {l}{s}{r}\tmore text"),
);

// Check with the initial cursor position being at any offset within the target
for i in 0..s.len() {
Expand Down
31 changes: 14 additions & 17 deletions src/editor/actions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Editor actions in response to user input
use crate::{
buffer::{BufferKind, Buffers, MiniBuffer, MiniBufferSelection},
buffer::{BufferKind, MiniBuffer, MiniBufferSelection},
config::Config,
config_handle, die,
dot::{Cur, Dot, TextObject},
Expand Down Expand Up @@ -441,29 +441,26 @@ impl Editor {
None => (dot.as_str(), None),
};

let try_set_addr = |buffers: &mut Buffers| {
if let Some(mut addr) = maybe_addr {
let b = buffers.active_mut();
b.dot = b.map_addr(&mut addr);
}
};

let path = Path::new(&maybe_path);
let mut path = Path::new(&maybe_path).to_path_buf();
let mut is_file = path.is_absolute() && path.exists();

if let Some(dir) = b.dir() {
let full_path = dir.join(path);
if let (false, Some(dir)) = (is_file, b.dir()) {
let full_path = dir.join(&path);
if full_path.exists() {
self.open_file(full_path);
return try_set_addr(&mut self.buffers);
path = full_path;
is_file = true;
}
}

if path.exists() {
if is_file {
self.open_file(path);
return try_set_addr(&mut self.buffers);
if let Some(mut addr) = maybe_addr {
let b = self.buffers.active_mut();
b.dot = b.map_addr(&mut addr);
}
} else {
b.find_forward(&dot);
}

b.find_forward(&dot);
}

/// Default semantics for attempting to execute the current dot:
Expand Down

0 comments on commit 0236db2

Please sign in to comment.