Skip to content

Commit

Permalink
Clean the branch from rebase leftovers
Browse files Browse the repository at this point in the history
  • Loading branch information
sireliah committed Nov 20, 2022
1 parent 6f7d5d4 commit 54e8a2b
Showing 1 changed file with 73 additions and 41 deletions.
114 changes: 73 additions & 41 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use helix_core::{
selection, shellwords, surround, textobject,
tree_sitter::Node,
unicode::width::UnicodeWidthChar,
visual_coords_at_pos, Change, LineEnding, Position, Range, Rope, RopeGraphemes, RopeSlice,
Selection, SmallVec, Tendril, Transaction,
visual_coords_at_pos, Change, LineEnding, Position, Range, Rope, RopeGraphemes, RopeSlice, Selection,
SmallVec, Tendril, Transaction,
};
use helix_view::{
apply_transaction,
Expand Down Expand Up @@ -212,17 +212,18 @@ impl MappableCommand {
copy_selection_on_prev_line, "Copy selection on previous line",
move_next_word_start, "Move to start of next word",
move_prev_word_start, "Move to start of previous word",
move_prev_word_end, "Move to end of previous word",
move_next_word_end, "Move to end of next word",
move_prev_word_end, "Move to end of previous word",
move_next_long_word_start, "Move to start of next long word",
move_prev_long_word_start, "Move to start of previous long word",
move_next_long_word_end, "Move to end of next long word",
extend_next_word_start, "Extend to start of next word",
extend_prev_word_start, "Extend to start of previous word",
extend_next_word_end, "Extend to end of next word",
extend_prev_word_end, "Extend to end of previous word",
extend_next_long_word_start, "Extend to start of next long word",
extend_prev_long_word_start, "Extend to start of previous long word",
extend_next_long_word_end, "Extend to end of next long word",
extend_next_word_end, "Extend to end of next word",
find_till_char, "Move till next occurrence of char",
find_next_char, "Move to next occurrence of char",
extend_till_char, "Extend till next occurrence of char",
Expand Down Expand Up @@ -315,8 +316,7 @@ impl MappableCommand {
goto_line_end, "Goto line end",
goto_next_buffer, "Goto next buffer",
goto_previous_buffer, "Goto previous buffer",
// TODO: different description ?
goto_line_end_newline, "Goto line end",
goto_line_end_newline, "Goto newline at line end",
goto_first_nonwhitespace, "Goto first non-blank in line",
trim_selections, "Trim whitespace from selections",
extend_to_line_start, "Extend to line start",
Expand Down Expand Up @@ -1094,6 +1094,10 @@ fn extend_next_word_end(cx: &mut Context) {
extend_word_impl(cx, movement::move_next_word_end)
}

fn extend_prev_word_end(cx: &mut Context) {
extend_word_impl(cx, movement::move_prev_word_end)
}

fn extend_next_long_word_start(cx: &mut Context) {
extend_word_impl(cx, movement::move_next_long_word_start)
}
Expand Down Expand Up @@ -3769,7 +3773,7 @@ fn format_selections(cx: &mut Context) {
let (view, doc) = current!(cx.editor);

// via lsp if available
// else via tree-sitter indentation calculations
// TODO: else via tree-sitter indentation calculations

let language_server = match doc.language_server() {
Some(language_server) => language_server,
Expand All @@ -3782,33 +3786,34 @@ fn format_selections(cx: &mut Context) {
.map(|range| range_to_lsp_range(doc.text(), *range, language_server.offset_encoding()))
.collect();

// TODO: all of the TODO's and commented code inside the loop,
// to make this actually work.
for _range in ranges {
let _language_server = match doc.language_server() {
Some(language_server) => language_server,
None => return,
};
// TODO: handle fails
// TODO: concurrent map
if ranges.len() != 1 {
cx.editor
.set_error("format_selections only supports a single selection for now");
return;
}

// TODO: need to block to get the formatting
// TODO: handle fails
// TODO: concurrent map over all ranges

// let edits = block_on(language_server.text_document_range_formatting(
// doc.identifier(),
// range,
// lsp::FormattingOptions::default(),
// ))
// .unwrap_or_default();
let range = ranges[0];

// let transaction = helix_lsp::util::generate_transaction_from_edits(
// doc.text(),
// edits,
// language_server.offset_encoding(),
// );
let edits = tokio::task::block_in_place(|| {
helix_lsp::block_on(language_server.text_document_range_formatting(
doc.identifier(),
range,
lsp::FormattingOptions::default(),
None,
))
})
.unwrap_or_default();

// apply_transaction(&transaction, doc, view);
}
let transaction = helix_lsp::util::generate_transaction_from_edits(
doc.text(),
edits,
language_server.offset_encoding(),
);

apply_transaction(&transaction, doc, view);
}

fn join_selections_impl(cx: &mut Context, select_space: bool) {
Expand Down Expand Up @@ -4794,6 +4799,8 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
let mut ranges = SmallVec::with_capacity(selection.len());
let text = doc.text().slice(..);

let mut offset = 0isize;

for range in selection.ranges() {
let fragment = range.slice(text);
let (output, success) = match shell_impl(shell, cmd, pipe.then(|| fragment.into())) {
Expand All @@ -4809,13 +4816,23 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
return;
}

let (from, to) = match behavior {
ShellBehavior::Replace => (range.from(), range.to()),
ShellBehavior::Insert => (range.from(), range.from()),
ShellBehavior::Append => (range.to(), range.to()),
_ => (range.from(), range.from()),
let output_len = output.chars().count();

let (from, to, deleted_len) = match behavior {
ShellBehavior::Replace => (range.from(), range.to(), range.len()),
ShellBehavior::Insert => (range.from(), range.from(), 0),
ShellBehavior::Append => (range.to(), range.to(), 0),
_ => (range.from(), range.from(), 0),
};
ranges.push(Range::new(to, to + output.chars().count()));

// These `usize`s cannot underflow because selection ranges cannot overlap.
// Once the MSRV is 1.66.0 (mixed_integer_ops is stabilized), we can use checked
// arithmetic to assert this.
let anchor = (to as isize + offset - deleted_len as isize) as usize;
let new_range = Range::new(anchor, anchor + output_len).with_direction(range.direction());
ranges.push(new_range);
offset = offset + output_len as isize - deleted_len as isize;

changes.push((from, to, Some(output)));
}

Expand Down Expand Up @@ -4872,8 +4889,6 @@ fn add_newline_impl(cx: &mut Context, open: Open) {

let changes = selection.into_iter().map(|range| {
let (start, end) = range.line_range(slice);

log::info!("Selection: {}, {}", start, end);
let line = match open {
Open::Above => start,
Open::Below => end + 1,
Expand Down Expand Up @@ -5052,14 +5067,18 @@ fn move_selection_above(cx: &mut Context) {
move_selection(cx, MoveSelection::Above)
}

enum IncrementDirection {
Increase,
Decrease,
}
/// Increment object under cursor by count.
fn increment(cx: &mut Context) {
increment_impl(cx, cx.count() as i64);
increment_impl(cx, IncrementDirection::Increase);
}

/// Decrement object under cursor by count.
fn decrement(cx: &mut Context) {
increment_impl(cx, -(cx.count() as i64));
increment_impl(cx, IncrementDirection::Decrease);
}

/// This function differs from find_next_char_impl in that it stops searching at the newline, but also
Expand All @@ -5083,7 +5102,7 @@ fn find_next_char_until_newline<M: CharMatcher>(
}

/// Decrement object under cursor by `amount`.
fn increment_impl(cx: &mut Context, amount: i64) {
fn increment_impl(cx: &mut Context, increment_direction: IncrementDirection) {
// TODO: when incrementing or decrementing a number that gets a new digit or lose one, the
// selection is updated improperly.
find_char_impl(
Expand All @@ -5095,6 +5114,17 @@ fn increment_impl(cx: &mut Context, amount: i64) {
1,
);

// Increase by 1 if `IncrementDirection` is `Increase`
// Decrease by 1 if `IncrementDirection` is `Decrease`
let sign = match increment_direction {
IncrementDirection::Increase => 1,
IncrementDirection::Decrease => -1,
};
let mut amount = sign * cx.count() as i64;

// If the register is `#` then increase or decrease the `amount` by 1 per element
let increase_by = if cx.register == Some('#') { sign } else { 0 };

let (view, doc) = current!(cx.editor);
let selection = doc.selection(view.id);
let text = doc.text().slice(..);
Expand All @@ -5114,6 +5144,8 @@ fn increment_impl(cx: &mut Context, amount: i64) {

let (range, new_text) = incrementor.increment(amount);

amount += increase_by;

Some((range.from(), range.to(), Some(new_text)))
})
.collect();
Expand Down

0 comments on commit 54e8a2b

Please # to comment.