Skip to content

Commit

Permalink
add incremental read of stdout of child and job control by closing pane
Browse files Browse the repository at this point in the history
  • Loading branch information
Kl4rry committed Dec 12, 2024
1 parent 009fa54 commit a9e694f
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 82 deletions.
42 changes: 35 additions & 7 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ slotmap = "1.0.7"
sublime_fuzzy = "0.7.0"
subprocess = "0.2.9"
tempdir = "0.3.7"
timeout-readwrite = "0.4.0"
toml = { version = "0.7.1", default-features = false }
tracing = "0.1.40"
tracing-log = "0.2.0"
Expand Down
1 change: 1 addition & 0 deletions crates/ferrite-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ serde_json = { workspace = true }
slotmap = { workspace = true }
sublime_fuzzy = { workspace = true }
subprocess = { workspace = true }
timeout-readwrite = { workspace = true}
toml = { workspace = true, features = ["parse", "display"] }
tracing = { workspace = true }
trash = { workspace = true }
Expand Down
46 changes: 46 additions & 0 deletions crates/ferrite-core/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,59 @@ impl Buffer {
})
}

pub fn auto_detect_language(&mut self) {
let syntax = match self.syntax.as_mut() {
Some(syntax) => syntax,
None => {
self.syntax = Some(Syntax::new(get_buffer_proxy()));
self.syntax.as_mut().unwrap()
}
};
if let Some(language) = detect_language(None, self.rope.clone()) {
if let Err(err) = syntax.set_language(language) {
tracing::error!("Error setting language: {err}");
}
syntax.update_text(self.rope.clone());
}
}

pub fn set_text(&mut self, text: &str) {
self.rope = Rope::from(text);
if let Some(ref mut syntax) = self.syntax {
syntax.update_text(self.rope.clone());
}
}

/// Replaces ropye, moves all cursors to end of file and autoscrolls
pub fn replace_rope(&mut self, rope: Rope) {
let added_lines = rope.len_lines().saturating_sub(self.rope.len_lines());
let mut map = SecondaryMap::new();
for view_id in self.views.keys() {
let view = &self.views[view_id];
if view.view_lines + view.line_pos >= self.rope.len_lines() {
let space_left =
(view.view_lines + view.line_pos).saturating_sub(self.rope.len_lines());
let scroll = added_lines.saturating_sub(space_left);
map.insert(view_id, scroll);
}
}

self.rope = rope;
if let Some(ref mut syntax) = self.syntax {
syntax.update_text(self.rope.clone());
}
for view_id in self.views.keys().collect::<Vec<_>>().into_iter() {
if let Some(scroll) = map.get(view_id) {
self.vertical_scroll(view_id, *scroll as i64);
}
// Disable cursor clamping here so pipe from shell command works
let before = self.views[view_id].clamp_cursor;
self.views[view_id].clamp_cursor = false;
self.eof(view_id, false);
self.views[view_id].clamp_cursor = before;
}
}

pub fn cursor(&self, view_id: ViewId, cursor_index: usize) -> Cursor {
self.views[view_id].cursors[cursor_index]
}
Expand Down
18 changes: 10 additions & 8 deletions crates/ferrite-core/src/buffer/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn format(formatter: &str, rope: Rope) -> Result<String, PopenError> {
.args(&parts.collect::<Vec<_>>())
.stdin(Redirection::Pipe)
.stdout(Redirection::Pipe)
.stderr(Redirection::Pipe)
.stderr(Redirection::Merge)
.popen()?;

let mut input = Vec::new();
Expand All @@ -28,15 +28,16 @@ fn format(formatter: &str, rope: Rope) -> Result<String, PopenError> {
let mut com = child
.communicate_start(Some(input))
.limit_time(Duration::from_secs(3));
let (stdout, stderr) = com.read()?;
let (stdout, _) = com.read()?;
let exit_status = child.wait()?;

let output = String::from_utf8_lossy(&stdout.unwrap()).into();
if exit_status.success() {
Ok(String::from_utf8_lossy(&stdout.unwrap()).into())
Ok(output)
} else {
Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
String::from_utf8_lossy(&stderr.unwrap()),
output,
))?
}
}
Expand Down Expand Up @@ -69,7 +70,7 @@ fn format_selection(formatter: &str, rope: Rope, cursor: &Cursor) -> Result<Stri
let mut child = cmd
.stdin(Redirection::Pipe)
.stdout(Redirection::Pipe)
.stderr(Redirection::Pipe)
.stderr(Redirection::Merge)
.popen()?;

let mut input = Vec::new();
Expand All @@ -80,15 +81,16 @@ fn format_selection(formatter: &str, rope: Rope, cursor: &Cursor) -> Result<Stri
let mut com = child
.communicate_start(Some(input))
.limit_time(Duration::from_secs(3));
let (stdout, stderr) = com.read()?;
let (stdout, _) = com.read()?;
let exit_status = child.wait()?;

let output = String::from_utf8_lossy(&stdout.unwrap()).into();
if exit_status.success() {
Ok(String::from_utf8_lossy(&stdout.unwrap()).into())
Ok(output)
} else {
Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
String::from_utf8_lossy(&stderr.unwrap()),
output,
))?
}
}
Expand Down
Loading

0 comments on commit a9e694f

Please sign in to comment.