Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inlay hints #14

Open
wants to merge 5 commits into
base: steel-event-system
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3468,14 +3468,23 @@ async fn make_format_callback(
let doc = doc_mut!(editor, &doc_id);
let view = view_mut!(editor, view_id);

if let Ok(format) = format {
if doc.version() == doc_version {
doc.apply(&format, view.id);
doc.append_changes_to_history(view);
doc.detect_indent_and_line_ending();
view.ensure_cursor_in_view(doc, scrolloff);
} else {
log::info!("discarded formatting changes because the document changed");
match format {
Ok(format) => {
if doc.version() == doc_version {
doc.apply(&format, view.id);
doc.append_changes_to_history(view);
doc.detect_indent_and_line_ending();
view.ensure_cursor_in_view(doc, scrolloff);
} else {
log::info!("discarded formatting changes because the document changed");
}
}
Err(err) => {
if write.is_none() {
editor.set_error(err.to_string());
return;
}
log::info!("failed to format '{}': {err}", doc.display_name());
}
}

Expand Down
76 changes: 74 additions & 2 deletions helix-term/src/commands/engine/components.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::{collections::HashMap, sync::Arc};
use std::{any::Any, collections::HashMap, fmt::Pointer, sync::Arc};

use helix_core::Position;
use helix_core::{text_annotations::InlineAnnotation, Position};
use helix_view::{
document::{DocumentInlayHints, DocumentInlayHintsId},
graphics::{Color, CursorKind, Rect, UnderlineStyle},
input::{Event, KeyEvent, MouseButton, MouseEvent},
keyboard::{KeyCode, KeyModifiers},
theme::{Modifier, Style},
Editor,
};
use steel::{
gc::unsafe_erased_pointers::ReferenceCustomType,
rvals::{as_underlying_type, Custom, FromSteelVal, IntoSteelVal, SteelString},
steel_vm::{builtin::BuiltInModule, engine::Engine, register_fn::RegisterFn},
SteelVal,
Expand Down Expand Up @@ -423,6 +425,76 @@ pub fn helix_component_module() -> BuiltInModule {
false.into_steelval()
}
})
.register_fn(
"add-inlay-hint",
|cx: &mut Context, char_index: usize, completion: SteelString| {
let view_id = cx.editor.tree.focus;
if !cx.editor.tree.contains(view_id) {
return false.into_steelval();
}
let view = cx.editor.tree.get(view_id);
let doc_id = cx.editor.tree.get(view_id).doc;
let doc = match cx.editor.documents.get_mut(&doc_id) {
Some(x) => x,
None => return false.into_steelval(),
};
let mut new_inlay_hints = doc
.inlay_hints(view_id)
.map(|x| x.clone())
.unwrap_or_else(|| {
let doc_text = doc.text();
let len_lines = doc_text.len_lines();

let view_height = view.inner_height();
let first_visible_line = doc_text.char_to_line(
doc.view_offset(view_id).anchor.min(doc_text.len_chars()),
);
let first_line = first_visible_line.saturating_sub(view_height);
let last_line = first_visible_line
.saturating_add(view_height.saturating_mul(2))
.min(len_lines);

let new_doc_inlay_hints_id = DocumentInlayHintsId {
first_line,
last_line,
};

DocumentInlayHints::empty_with_id(new_doc_inlay_hints_id)
});
new_inlay_hints
.other_inlay_hints
.push(InlineAnnotation::new(char_index, completion.to_string()));

doc.set_inlay_hints(view_id, new_inlay_hints);
true.into_steelval()
},
)
.register_fn(
"remove-inlay-hint",
|cx: &mut Context, char_index: usize, completion: SteelString| {
let text = completion.to_string();
let view_id = cx.editor.tree.focus;
if !cx.editor.tree.contains(view_id) {
return false.into_steelval();
}
let doc_id = cx.editor.tree.get_mut(view_id).doc;
let doc = match cx.editor.documents.get_mut(&doc_id) {
Some(x) => x,
None => return false.into_steelval(),
};

let inlay_hints = match doc.inlay_hints(view_id) {
Some(inlay_hints) => inlay_hints,
None => return false.into_steelval(),
};
let mut new_inlay_hints = inlay_hints.clone();
new_inlay_hints
.other_inlay_hints
.retain(|x| x.char_idx != char_index && x.text != text);
doc.set_inlay_hints(view_id, new_inlay_hints);
true.into_steelval()
},
)
// Is this mouse event within the area provided
.register_fn("mouse-event-within-area?", |event: Event, area: Rect| {
if let Event::Mouse(MouseEvent { row, column, .. }) = event {
Expand Down
29 changes: 19 additions & 10 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,11 @@ impl Document {
))
})
{
log::debug!(
"formatting '{}' with command '{}', args {fmt_args:?}",
self.display_name(),
fmt_cmd.display(),
);
use std::process::Stdio;
let text = self.text().clone();

Expand All @@ -797,17 +802,21 @@ impl Document {
command: fmt_cmd.to_string_lossy().into(),
error: e.kind(),
})?;
{
let mut stdin = process.stdin.take().ok_or(FormatterError::BrokenStdin)?;
to_writer(&mut stdin, (encoding::UTF_8, false), &text)
.await
.map_err(|_| FormatterError::BrokenStdin)?;
}

let output = process
.wait_with_output()
.await
.map_err(|_| FormatterError::WaitForOutputFailed)?;
let mut stdin = process.stdin.take().ok_or(FormatterError::BrokenStdin)?;
let input_text = text.clone();
let input_task = tokio::spawn(async move {
to_writer(&mut stdin, (encoding::UTF_8, false), &input_text).await
// Note that `stdin` is dropped here, causing the pipe to close. This can
// avoid a deadlock with `wait_with_output` below if the process is waiting on
// stdin to close before exiting.
});
let (input_result, output_result) = tokio::join! {
input_task,
process.wait_with_output(),
};
let _ = input_result.map_err(|_| FormatterError::BrokenStdin)?;
let output = output_result.map_err(|_| FormatterError::WaitForOutputFailed)?;

if !output.status.success() {
if !output.stderr.is_empty() {
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
edition = "2018"
2 changes: 1 addition & 1 deletion steel
Submodule steel updated 100 files