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

Fix cursor position bugs related to o and O #281

Merged
merged 2 commits into from
Jun 17, 2021
Merged
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
77 changes: 40 additions & 37 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl<'a> Context<'a> {
self.callbacks.push(callback);
}

/// Returns 1 if no explicit count was provided
#[inline]
pub fn count(&self) -> usize {
self.count.map_or(1, |v| v.get())
Expand Down Expand Up @@ -1495,53 +1496,55 @@ fn open(cx: &mut Context, open: Open) {
enter_insert_mode(doc);

let text = doc.text().slice(..);
let contents = doc.text();
let selection = doc.selection(view.id);

let mut ranges = SmallVec::with_capacity(selection.len());
let mut offs = 0;

let changes: Vec<Change> = selection
.iter()
.map(|range| {
let line = text.char_to_line(range.head);

let line = match open {
// adjust position to the end of the line (next line - 1)
Open::Below => line + 1,
// adjust position to the end of the previous line (current line - 1)
Open::Above => line,
};

let index = doc.text().line_to_char(line).saturating_sub(1);
let mut transaction = Transaction::change_by_selection(contents, selection, |range| {
let line = text.char_to_line(range.head);

// TODO: share logic with insert_newline for indentation
let indent_level = indent::suggested_indent_for_pos(
doc.language_config(),
doc.syntax(),
text,
index,
true,
);
let indent = doc.indent_unit().repeat(indent_level);
let mut text = String::with_capacity(1 + indent.len());
text.push('\n');
text.push_str(&indent);
let text = text.repeat(count);
let line = match open {
// adjust position to the end of the line (next line - 1)
Open::Below => line + 1,
// adjust position to the end of the previous line (current line - 1)
Open::Above => line,
};

// calculate new selection range
let pos = index + text.chars().count();
ranges.push(Range::new(pos, pos));
// insert newlines after this index for both Above and Below variants
let linend_index = doc.text().line_to_char(line).saturating_sub(1);

(index, index, Some(text.into()))
})
.collect();
// TODO: share logic with insert_newline for indentation
let indent_level = indent::suggested_indent_for_pos(
doc.language_config(),
doc.syntax(),
text,
linend_index,
true,
);
let indent = doc.indent_unit().repeat(indent_level);
let mut text = String::with_capacity(1 + indent.len());
text.push('\n');
text.push_str(&indent);
let text = text.repeat(count);

// calculate new selection ranges
let pos = if line == 0 {
0 // Required since text will have a min len of 1 (\n)
} else {
offs + linend_index + 1
};
for i in 0..count {
ranges.push(Range::new(pos + i, pos + i));
}

// TODO: count actually inserts "n" new lines and starts editing on all of them.
// TODO: append "count" newlines and modify cursors to those lines
offs += text.chars().count();

let selection = Selection::new(ranges, 0);
(linend_index, linend_index, Some(text.into()))
});

let transaction =
Transaction::change(doc.text(), changes.into_iter()).with_selection(selection);
transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));

doc.apply(&transaction, view.id);
}
Expand Down