Skip to content

Commit

Permalink
Replace unicode-general-category with icu-properties (helix-editor#10989
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kirawi authored and Grulfen committed Jul 4, 2024
1 parent 0edf609 commit af6115a
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 19 deletions.
8 changes: 1 addition & 7 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion helix-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ smallvec = "1.13"
smartstring = "1.0.1"
unicode-segmentation = "1.11"
unicode-width = "0.1"
unicode-general-category = "0.6"
icu_properties = "1.5"
slotmap.workspace = true
tree-sitter.workspace = true
once_cell = "1.19"
Expand Down
4 changes: 2 additions & 2 deletions helix-core/src/chars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ pub fn char_is_whitespace(ch: char) -> bool {

#[inline]
pub fn char_is_punctuation(ch: char) -> bool {
use unicode_general_category::{get_general_category, GeneralCategory};
use icu_properties::{maps::general_category, GeneralCategory};

matches!(
get_general_category(ch),
general_category().get(ch),
GeneralCategory::OtherPunctuation
| GeneralCategory::OpenPunctuation
| GeneralCategory::ClosePunctuation
Expand Down
2 changes: 1 addition & 1 deletion helix-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mod transaction;
pub mod wrap;

pub mod unicode {
pub use unicode_general_category as category;
pub use icu_properties as properties;
pub use unicode_segmentation as segmentation;
pub use unicode_width as width;
}
Expand Down
68 changes: 62 additions & 6 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ impl MappableCommand {
jumplist_picker, "Open jumplist picker",
symbol_picker, "Open symbol picker",
changed_file_picker, "Open changed file picker",
show_hunk, "Show content of hunk under cursor",
select_references_to_symbol_under_cursor, "Select symbol references",
workspace_symbol_picker, "Open workspace symbol picker",
diagnostics_picker, "Open diagnostic picker",
Expand Down Expand Up @@ -3136,6 +3137,61 @@ fn changed_file_picker(cx: &mut Context) {
cx.push_layer(Box::new(overlaid(picker)));
}

fn show_hunk(cx: &mut Context) {
let (view, doc) = current!(cx.editor);

if let Some(handle) = doc.diff_handle() {
if let Some(popup) = hunk_popup(handle, doc, view, &cx.editor.theme) {
cx.push_layer(Box::new(popup));
};
};
}

fn hunk_popup(
handle: &helix_vcs::DiffHandle,
doc: &Document,
view: &View,
theme: &helix_view::Theme,
) -> Option<Popup<ui::Text>> {
let diff = handle.load();
let text = doc.text().slice(..);
let line = doc.selection(view.id).primary().cursor_line(text);
let hunk = diff
.hunk_at(line as u32, false)
.map(|idx| diff.nth_hunk(idx))?;

let deleted = diff_string(&hunk.before, diff.diff_base().slice(..), "-");
let added = diff_string(&hunk.after, diff.doc().slice(..), "+");
let deleted_styled = ui::markdown::styled_multiline_text(&deleted, theme.get("diff.minus"));
let added_styled = ui::markdown::styled_multiline_text(&added, theme.get("diff.plus"));
let hunk_text = if hunk.is_pure_insertion() {
added_styled
} else if hunk.is_pure_removal() {
deleted_styled
} else {
let mut tmp_text = tui::text::Text::from("");
tmp_text.extend(deleted_styled);
tmp_text.extend(added_styled);
tmp_text
};
let ui_text = ui::Text::from(hunk_text);
Some(
Popup::new("hunk", ui_text)
.auto_close(true)
.with_scrollbar(true),
)
}

fn diff_string(range: &std::ops::Range<u32>, text: RopeSlice, prefix: &str) -> String {
let char_range = line_range_to_char_range(range, text);
text.slice(char_range.anchor..char_range.head)
.to_string()
.lines()
.map(|line| format!("{} {}", prefix, line))
.collect::<Vec<_>>()
.join("\n")
}

impl ui::menu::Item for MappableCommand {
type Data = ReverseKeymap;

Expand Down Expand Up @@ -3660,7 +3716,7 @@ fn goto_first_change_impl(cx: &mut Context, reverse: bool) {
diff.nth_hunk(idx)
};
if hunk != Hunk::NONE {
let range = hunk_range(hunk, doc.text().slice(..));
let range = line_range_to_char_range(&hunk.after, doc.text().slice(..));
doc.set_selection(view.id, Selection::single(range.anchor, range.head));
}
}
Expand Down Expand Up @@ -3702,7 +3758,7 @@ fn goto_next_change_impl(cx: &mut Context, direction: Direction) {
return range;
};
let hunk = diff.nth_hunk(hunk_idx);
let new_range = hunk_range(hunk, doc_text);
let new_range = line_range_to_char_range(&hunk.after, doc_text);
if editor.mode == Mode::Select {
let head = if new_range.head < range.anchor {
new_range.anchor
Expand All @@ -3724,12 +3780,12 @@ fn goto_next_change_impl(cx: &mut Context, direction: Direction) {
/// Returns the [Range] for a [Hunk] in the given text.
/// Additions and modifications cover the added and modified ranges.
/// Deletions are represented as the point at the start of the deletion hunk.
fn hunk_range(hunk: Hunk, text: RopeSlice) -> Range {
let anchor = text.line_to_char(hunk.after.start as usize);
let head = if hunk.after.is_empty() {
fn line_range_to_char_range(line_range: &std::ops::Range<u32>, text: RopeSlice) -> Range {
let anchor = text.line_to_char(line_range.start as usize);
let head = if line_range.is_empty() {
anchor + 1
} else {
text.line_to_char(hunk.after.end as usize)
text.line_to_char(line_range.end as usize)
};

Range::new(anchor, head)
Expand Down
1 change: 1 addition & 0 deletions helix-term/src/keymap/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"d" => diagnostics_picker,
"D" => workspace_diagnostics_picker,
"g" => changed_file_picker,
"H" => show_hunk,
"a" => code_action,
"'" => last_picker,
"G" => { "Debug (experimental)" sticky=true
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use helix_view::{
Theme,
};

fn styled_multiline_text<'a>(text: &str, style: Style) -> Text<'a> {
pub fn styled_multiline_text<'a>(text: &str, style: Style) -> Text<'a> {
let spans: Vec<_> = text
.lines()
.map(|line| Span::styled(line.to_string(), style))
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod document;
pub(crate) mod editor;
mod info;
pub mod lsp;
mod markdown;
pub mod markdown;
pub mod menu;
pub mod overlay;
pub mod picker;
Expand Down

0 comments on commit af6115a

Please sign in to comment.