@@ -247,7 +247,8 @@ impl MappableCommand {
247247 extend_search_prev, "Add previous search match to selection",
248248 search_selection, "Use current selection as search pattern",
249249 global_search, "Global search in workspace folder",
250− extend_line, "Select current line, if already selected, extend to next line",
250+ extend_line, "Select current line, if already selected, extend to another line based on the anchor",
251+ extend_line_below, "Select current line, if already selected, extend to next line",
251252 extend_line_above, "Select current line, if already selected, extend to previous line",
252253 extend_to_line_bounds, "Extend selection to line bounds",
253254 shrink_to_line_bounds, "Shrink selection to line bounds",
@@ -1945,6 +1946,15 @@ enum Extend {
19451946}
19461947
19471948fn extend_line(cx: &mut Context) {
1949+ let (view, doc) = current_ref!(cx.editor);
1950+ let extend = match doc.selection(view.id).primary().direction() {
1951+ Direction::Forward => Extend::Below,
1952+ Direction::Backward => Extend::Above,
1953+ };
1954+ extend_line_impl(cx, extend);
1955+}
1956+
1957+fn extend_line_below(cx: &mut Context) {
19481958 extend_line_impl(cx, Extend::Below);
19491959}
19501960
@@ -1960,20 +1970,32 @@ fn extend_line_impl(cx: &mut Context, extend: Extend) {
19601970 let selection = doc.selection(view.id).clone().transform(|range| {
19611971 let (start_line, end_line) = range.line_range(text.slice(..));
19621972
1963− let start = text.line_to_char(start_line);
1964− let end = text.line_to_char((end_line + count).min(text.len_lines()));
1973+ let start = text.line_to_char(match extend {
1974+ Extend::Above => start_line.saturating_sub(count),
1975+ Extend::Below => start_line,
1976+ });
1977+ let end = text.line_to_char(
1978+ match extend {
1979+ Extend::Above => end_line + 1, // the start of next line
1980+ Extend::Below => (end_line + count),
1981+ }
1982+ .min(text.len_lines()),
1983+ );
19651984
19661985 // extend to previous/next line if current line is selected
19671986 let (anchor, head) = if range.from() == start && range.to() == end {
19681987 match extend {
1969− Extend::Above => (end, text.line_to_char(start_line.saturating_sub(1))),
1988+ Extend::Above => (end, text.line_to_char(start_line.saturating_sub(count + 1))),
19701989 Extend::Below => (
19711990 start,
19721991 text.line_to_char((end_line + count + 1).min(text.len_lines())),
19731992 ),
19741993 }
19751994 } else {
1976− (start, end)
1995+ match extend {
1996+ Extend::Above => (end, start),
1997+ Extend::Below => (start, end),
1998+ }
19771999 };
19782000
19792001 Range::new(anchor, head)